Skip to content

Commit 5114dc6

Browse files
authored
Merge pull request #2355 from murgatroid99/grpc-js_typescript_update_1.5.x
grpc-js: Update to newest typescript compiler (1.5.x)
2 parents 42ee882 + 0a89a6f commit 5114dc6

10 files changed

+86
-40
lines changed

packages/grpc-js/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"devDependencies": {
1818
"@types/gulp": "^4.0.6",
1919
"@types/gulp-mocha": "0.0.32",
20-
"@types/lodash": "^4.14.108",
20+
"@types/lodash": "^4.14.186",
2121
"@types/mocha": "^5.2.6",
2222
"@types/ncp": "^2.0.1",
2323
"@types/pify": "^3.0.2",
2424
"@types/semver": "^7.3.9",
2525
"clang-format": "^1.0.55",
2626
"execa": "^2.0.3",
27-
"gts": "^2.0.0",
27+
"gts": "^3.1.1",
2828
"gulp": "^4.0.2",
2929
"gulp-mocha": "^6.0.0",
3030
"lodash": "^4.17.4",
@@ -35,7 +35,7 @@
3535
"rimraf": "^3.0.2",
3636
"semver": "^7.3.5",
3737
"ts-node": "^8.3.0",
38-
"typescript": "^3.7.2"
38+
"typescript": "^4.8.4"
3939
},
4040
"contributors": [
4141
{

packages/grpc-js/src/call-credentials-filter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Metadata } from './metadata';
2222
import { Status } from './constants';
2323
import { splitHostPort } from './uri-parser';
2424
import { ServiceError } from './call';
25+
import { getErrorMessage } from './error';
2526

2627
export class CallCredentialsFilter extends BaseFilter implements Filter {
2728
private serviceUrl: string;
@@ -57,7 +58,7 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
5758
} catch (error) {
5859
this.stream.cancelWithStatus(
5960
Status.UNAUTHENTICATED,
60-
`Failed to retrieve auth metadata with error: ${error.message}`
61+
`Failed to retrieve auth metadata with error: ${getErrorMessage(error)}`
6162
);
6263
return Promise.reject<Metadata>('Failed to retrieve auth metadata');
6364
}

packages/grpc-js/src/call-credentials.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export abstract class CallCredentials {
115115
reject(err);
116116
return;
117117
}
118+
if (!headers) {
119+
reject(new Error('Headers not set by metadata plugin'));
120+
return;
121+
}
118122
resolve(headers);
119123
}
120124
);

packages/grpc-js/src/call-stream.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { SubchannelCallStatsTracker, Subchannel } from './subchannel';
2929
import * as logging from './logging';
3030
import { LogVerbosity } from './constants';
3131
import { ServerSurfaceCall } from './server-call';
32+
import { getErrorMessage } from './error';
3233

3334
const TRACER_NAME = 'call_stream';
3435

@@ -556,7 +557,7 @@ export class Http2CallStream implements Call {
556557
} catch (error) {
557558
this.endCall({
558559
code: Status.UNKNOWN,
559-
details: error.message,
560+
details: getErrorMessage(error),
560561
metadata: new Metadata(),
561562
});
562563
return;
@@ -567,7 +568,7 @@ export class Http2CallStream implements Call {
567568
} catch (error) {
568569
this.endCall({
569570
code: Status.UNKNOWN,
570-
details: error.message,
571+
details: getErrorMessage(error),
571572
metadata: new Metadata(),
572573
});
573574
}
@@ -703,7 +704,7 @@ export class Http2CallStream implements Call {
703704
} catch (error) {
704705
this.endCall({
705706
code: Status.UNAVAILABLE,
706-
details: `Write failed with error ${error.message}`,
707+
details: `Write failed with error ${getErrorMessage(error)}`,
707708
metadata: new Metadata()
708709
});
709710
}
@@ -856,7 +857,7 @@ export class Http2CallStream implements Call {
856857
} catch (error) {
857858
this.endCall({
858859
code: Status.UNAVAILABLE,
859-
details: `Write failed with error ${error.message}`,
860+
details: `Write failed with error ${getErrorMessage(error)}`,
860861
metadata: new Metadata()
861862
});
862863
}

packages/grpc-js/src/client-interceptors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Channel } from './channel';
3434
import { CallOptions } from './client';
3535
import { CallCredentials } from './call-credentials';
3636
import { ClientMethodDefinition } from './make-client';
37+
import { getErrorMessage } from './error';
3738

3839
/**
3940
* Error class associated with passing both interceptors and interceptor
@@ -382,7 +383,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
382383
} catch (e) {
383384
this.call.cancelWithStatus(
384385
Status.INTERNAL,
385-
`Request message serialization failure: ${e.message}`
386+
`Request message serialization failure: ${getErrorMessage(e)}`
386387
);
387388
return;
388389
}
@@ -409,7 +410,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
409410
} catch (e) {
410411
readError = {
411412
code: Status.INTERNAL,
412-
details: `Response message parsing error: ${e.message}`,
413+
details: `Response message parsing error: ${getErrorMessage(e)}`,
413414
metadata: new Metadata(),
414415
};
415416
this.call.cancelWithStatus(readError.code, readError.details);

packages/grpc-js/src/error.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
export function getErrorMessage(error: unknown): string {
19+
if (error instanceof Error) {
20+
return error.message;
21+
} else {
22+
return String(error);
23+
}
24+
}
25+
26+
export function getErrorCode(error: unknown): number | null {
27+
if (
28+
typeof error === 'object' &&
29+
error !== null &&
30+
'code' in error &&
31+
typeof (error as Record<string, unknown>).code === 'number'
32+
) {
33+
return (error as Record<string, number>).code;
34+
} else {
35+
return null;
36+
}
37+
}

packages/grpc-js/src/metadata.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import * as http2 from 'http2';
1919
import { log } from './logging';
2020
import { LogVerbosity } from './constants';
21+
import { getErrorMessage } from './error';
2122
const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
2223
const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
2324

@@ -294,7 +295,7 @@ export class Metadata {
294295
}
295296
}
296297
} catch (error) {
297-
const message = `Failed to add metadata entry ${key}: ${values}. ${error.message}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
298+
const message = `Failed to add metadata entry ${key}: ${values}. ${getErrorMessage(error)}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
298299
log(LogVerbosity.ERROR, message);
299300
}
300301
});

packages/grpc-js/src/server-call.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { StreamDecoder } from './stream-decoder';
3333
import { ObjectReadable, ObjectWritable } from './object-stream';
3434
import { ChannelOptions } from './channel-options';
3535
import * as logging from './logging';
36+
import { getErrorCode, getErrorMessage } from './error';
3637

3738
const TRACER_NAME = 'server_call';
3839

@@ -216,8 +217,10 @@ export class ServerWritableStreamImpl<RequestType, ResponseType>
216217
return;
217218
}
218219
} catch (err) {
219-
err.code = Status.INTERNAL;
220-
this.emit('error', err);
220+
this.emit('error', {
221+
details: getErrorMessage(err),
222+
code: Status.INTERNAL
223+
});
221224
}
222225

223226
callback();
@@ -444,7 +447,7 @@ export class Http2ServerCallStream<
444447
private getDecompressedMessage(message: Buffer, encoding: string) {
445448
switch (encoding) {
446449
case 'deflate': {
447-
return new Promise<Buffer | undefined>((resolve, reject) => {
450+
return new Promise<Buffer | void>((resolve, reject) => {
448451
zlib.inflate(message.slice(5), (err, output) => {
449452
if (err) {
450453
this.sendError({
@@ -460,7 +463,7 @@ export class Http2ServerCallStream<
460463
}
461464

462465
case 'gzip': {
463-
return new Promise<Buffer | undefined>((resolve, reject) => {
466+
return new Promise<Buffer | void>((resolve, reject) => {
464467
zlib.unzip(message.slice(5), (err, output) => {
465468
if (err) {
466469
this.sendError({
@@ -539,7 +542,7 @@ export class Http2ServerCallStream<
539542
return metadata;
540543
}
541544

542-
receiveUnaryMessage(encoding: string): Promise<RequestType> {
545+
receiveUnaryMessage(encoding: string): Promise<RequestType | void> {
543546
return new Promise((resolve, reject) => {
544547
const stream = this.stream;
545548
const chunks: Buffer[] = [];
@@ -579,8 +582,10 @@ export class Http2ServerCallStream<
579582
resolve(this.deserializeMessage(decompressedMessage));
580583
}
581584
} catch (err) {
582-
err.code = Status.INTERNAL;
583-
this.sendError(err);
585+
this.sendError({
586+
code: Status.INTERNAL,
587+
details: getErrorMessage(err)
588+
});
584589
resolve();
585590
}
586591
});
@@ -630,8 +635,10 @@ export class Http2ServerCallStream<
630635
this.write(response);
631636
this.sendStatus({ code: Status.OK, details: 'OK', metadata });
632637
} catch (err) {
633-
err.code = Status.INTERNAL;
634-
this.sendError(err);
638+
this.sendError({
639+
details: getErrorMessage(err),
640+
code: Status.INTERNAL
641+
});
635642
}
636643
}
637644

@@ -856,21 +863,15 @@ export class Http2ServerCallStream<
856863
} catch (error) {
857864
// Ignore any remaining messages when errors occur.
858865
this.bufferedMessages.length = 0;
859-
860-
if (
861-
!(
862-
'code' in error &&
863-
typeof error.code === 'number' &&
864-
Number.isInteger(error.code) &&
865-
error.code >= Status.OK &&
866-
error.code <= Status.UNAUTHENTICATED
867-
)
868-
) {
869-
// The error code is not a valid gRPC code so its being overwritten.
870-
error.code = Status.INTERNAL;
866+
let code = getErrorCode(error);
867+
if (code === null || code < Status.OK || code > Status.UNAUTHENTICATED) {
868+
code = Status.INTERNAL
871869
}
872870

873-
readable.emit('error', error);
871+
readable.emit('error', {
872+
details: getErrorMessage(error),
873+
code: code
874+
});
874875
}
875876

876877
this.isPushPending = false;

packages/grpc-js/src/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
import { parseUri } from './uri-parser';
6262
import { ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzServer, registerChannelzSocket, ServerInfo, ServerRef, SocketInfo, SocketRef, TlsInfo, unregisterChannelzRef } from './channelz';
6363
import { CipherNameAndProtocol, TLSSocket } from 'tls';
64+
import { getErrorCode, getErrorMessage } from './error';
6465

6566
const TRACER_NAME = 'server';
6667

@@ -877,11 +878,10 @@ export class Server {
877878
}
878879
}
879880

880-
if (err.code === undefined) {
881-
err.code = Status.INTERNAL;
882-
}
883-
884-
call.sendError(err);
881+
call.sendError({
882+
code: getErrorCode(err) ?? Status.INTERNAL,
883+
details: getErrorMessage(err)
884+
});
885885
}
886886
}
887887
);

packages/proto-loader/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
"@types/node": "^10.17.26",
5959
"@types/yargs": "^16.0.4",
6060
"clang-format": "^1.2.2",
61-
"gts": "^1.1.0",
61+
"gts": "^3.1.0",
6262
"rimraf": "^3.0.2",
63-
"typescript": "~3.8.3"
63+
"typescript": "~4.7.4"
6464
},
6565
"engines": {
6666
"node": ">=6"

0 commit comments

Comments
 (0)