Merge pull request #2354 from murgatroid99/grpc-js_typescript_update_1.6.x

grpc-js: Update to newest typescript compiler (1.6.x)
This commit is contained in:
Michael Lumish 2023-02-09 17:17:50 -08:00 committed by GitHub
commit fc63719eda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 38 deletions

View File

@ -17,14 +17,14 @@
"devDependencies": { "devDependencies": {
"@types/gulp": "^4.0.6", "@types/gulp": "^4.0.6",
"@types/gulp-mocha": "0.0.32", "@types/gulp-mocha": "0.0.32",
"@types/lodash": "^4.14.108", "@types/lodash": "^4.14.186",
"@types/mocha": "^5.2.6", "@types/mocha": "^5.2.6",
"@types/ncp": "^2.0.1", "@types/ncp": "^2.0.1",
"@types/pify": "^3.0.2", "@types/pify": "^3.0.2",
"@types/semver": "^7.3.9", "@types/semver": "^7.3.9",
"clang-format": "^1.0.55", "clang-format": "^1.0.55",
"execa": "^2.0.3", "execa": "^2.0.3",
"gts": "^2.0.0", "gts": "^3.1.1",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-mocha": "^6.0.0", "gulp-mocha": "^6.0.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
@ -35,7 +35,7 @@
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"semver": "^7.3.5", "semver": "^7.3.5",
"ts-node": "^8.3.0", "ts-node": "^8.3.0",
"typescript": "^3.7.2" "typescript": "^4.8.4"
}, },
"contributors": [ "contributors": [
{ {

View File

@ -22,6 +22,7 @@ import { Metadata } from './metadata';
import { Status } from './constants'; import { Status } from './constants';
import { splitHostPort } from './uri-parser'; import { splitHostPort } from './uri-parser';
import { ServiceError } from './call'; import { ServiceError } from './call';
import { getErrorMessage } from './error';
export class CallCredentialsFilter extends BaseFilter implements Filter { export class CallCredentialsFilter extends BaseFilter implements Filter {
private serviceUrl: string; private serviceUrl: string;
@ -57,7 +58,7 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
} catch (error) { } catch (error) {
this.stream.cancelWithStatus( this.stream.cancelWithStatus(
Status.UNAUTHENTICATED, Status.UNAUTHENTICATED,
`Failed to retrieve auth metadata with error: ${error.message}` `Failed to retrieve auth metadata with error: ${getErrorMessage(error)}`
); );
return Promise.reject<Metadata>('Failed to retrieve auth metadata'); return Promise.reject<Metadata>('Failed to retrieve auth metadata');
} }

View File

@ -115,6 +115,10 @@ export abstract class CallCredentials {
reject(err); reject(err);
return; return;
} }
if (!headers) {
reject(new Error('Headers not set by metadata plugin'));
return;
}
resolve(headers); resolve(headers);
} }
); );

View File

@ -29,6 +29,7 @@ import { SubchannelCallStatsTracker, Subchannel } from './subchannel';
import * as logging from './logging'; import * as logging from './logging';
import { LogVerbosity } from './constants'; import { LogVerbosity } from './constants';
import { ServerSurfaceCall } from './server-call'; import { ServerSurfaceCall } from './server-call';
import { getErrorMessage } from './error';
const TRACER_NAME = 'call_stream'; const TRACER_NAME = 'call_stream';
@ -565,7 +566,7 @@ export class Http2CallStream implements Call {
} catch (error) { } catch (error) {
this.endCall({ this.endCall({
code: Status.UNKNOWN, code: Status.UNKNOWN,
details: error.message, details: getErrorMessage(error),
metadata: new Metadata(), metadata: new Metadata(),
}); });
return; return;
@ -576,7 +577,7 @@ export class Http2CallStream implements Call {
} catch (error) { } catch (error) {
this.endCall({ this.endCall({
code: Status.UNKNOWN, code: Status.UNKNOWN,
details: error.message, details: getErrorMessage(error),
metadata: new Metadata(), metadata: new Metadata(),
}); });
} }
@ -716,7 +717,7 @@ export class Http2CallStream implements Call {
} catch (error) { } catch (error) {
this.endCall({ this.endCall({
code: Status.UNAVAILABLE, code: Status.UNAVAILABLE,
details: `Write failed with error ${error.message}`, details: `Write failed with error ${getErrorMessage(error)}`,
metadata: new Metadata() metadata: new Metadata()
}); });
} }
@ -878,7 +879,7 @@ export class Http2CallStream implements Call {
} catch (error) { } catch (error) {
this.endCall({ this.endCall({
code: Status.UNAVAILABLE, code: Status.UNAVAILABLE,
details: `Write failed with error ${error.message}`, details: `Write failed with error ${getErrorMessage(error)}`,
metadata: new Metadata() metadata: new Metadata()
}); });
} }

View File

@ -34,6 +34,7 @@ import { Channel } from './channel';
import { CallOptions } from './client'; import { CallOptions } from './client';
import { CallCredentials } from './call-credentials'; import { CallCredentials } from './call-credentials';
import { ClientMethodDefinition } from './make-client'; import { ClientMethodDefinition } from './make-client';
import { getErrorMessage } from './error';
/** /**
* Error class associated with passing both interceptors and interceptor * Error class associated with passing both interceptors and interceptor
@ -382,7 +383,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
} catch (e) { } catch (e) {
this.call.cancelWithStatus( this.call.cancelWithStatus(
Status.INTERNAL, Status.INTERNAL,
`Request message serialization failure: ${e.message}` `Request message serialization failure: ${getErrorMessage(e)}`
); );
return; return;
} }
@ -409,7 +410,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
} catch (e) { } catch (e) {
readError = { readError = {
code: Status.INTERNAL, code: Status.INTERNAL,
details: `Response message parsing error: ${e.message}`, details: `Response message parsing error: ${getErrorMessage(e)}`,
metadata: new Metadata(), metadata: new Metadata(),
}; };
this.call.cancelWithStatus(readError.code, readError.details); this.call.cancelWithStatus(readError.code, readError.details);

View File

@ -0,0 +1,37 @@
/*
* Copyright 2022 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
export function getErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
} else {
return String(error);
}
}
export function getErrorCode(error: unknown): number | null {
if (
typeof error === 'object' &&
error !== null &&
'code' in error &&
typeof (error as Record<string, unknown>).code === 'number'
) {
return (error as Record<string, number>).code;
} else {
return null;
}
}

View File

@ -18,6 +18,7 @@
import * as http2 from 'http2'; import * as http2 from 'http2';
import { log } from './logging'; import { log } from './logging';
import { LogVerbosity } from './constants'; import { LogVerbosity } from './constants';
import { getErrorMessage } from './error';
const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/; const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/; const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
@ -294,7 +295,7 @@ export class Metadata {
} }
} }
} catch (error) { } catch (error) {
const message = `Failed to add metadata entry ${key}: ${values}. ${error.message}. For more information see https://github.com/grpc/grpc-node/issues/1173`; const message = `Failed to add metadata entry ${key}: ${values}. ${getErrorMessage(error)}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
log(LogVerbosity.ERROR, message); log(LogVerbosity.ERROR, message);
} }
}); });

View File

@ -33,6 +33,7 @@ import { StreamDecoder } from './stream-decoder';
import { ObjectReadable, ObjectWritable } from './object-stream'; import { ObjectReadable, ObjectWritable } from './object-stream';
import { ChannelOptions } from './channel-options'; import { ChannelOptions } from './channel-options';
import * as logging from './logging'; import * as logging from './logging';
import { getErrorCode, getErrorMessage } from './error';
const TRACER_NAME = 'server_call'; const TRACER_NAME = 'server_call';
@ -216,8 +217,10 @@ export class ServerWritableStreamImpl<RequestType, ResponseType>
return; return;
} }
} catch (err) { } catch (err) {
err.code = Status.INTERNAL; this.emit('error', {
this.emit('error', err); details: getErrorMessage(err),
code: Status.INTERNAL
});
} }
callback(); callback();
@ -444,7 +447,7 @@ export class Http2ServerCallStream<
private getDecompressedMessage(message: Buffer, encoding: string) { private getDecompressedMessage(message: Buffer, encoding: string) {
switch (encoding) { switch (encoding) {
case 'deflate': { case 'deflate': {
return new Promise<Buffer | undefined>((resolve, reject) => { return new Promise<Buffer | void>((resolve, reject) => {
zlib.inflate(message.slice(5), (err, output) => { zlib.inflate(message.slice(5), (err, output) => {
if (err) { if (err) {
this.sendError({ this.sendError({
@ -460,7 +463,7 @@ export class Http2ServerCallStream<
} }
case 'gzip': { case 'gzip': {
return new Promise<Buffer | undefined>((resolve, reject) => { return new Promise<Buffer | void>((resolve, reject) => {
zlib.unzip(message.slice(5), (err, output) => { zlib.unzip(message.slice(5), (err, output) => {
if (err) { if (err) {
this.sendError({ this.sendError({
@ -539,7 +542,7 @@ export class Http2ServerCallStream<
return metadata; return metadata;
} }
receiveUnaryMessage(encoding: string): Promise<RequestType> { receiveUnaryMessage(encoding: string): Promise<RequestType | void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const stream = this.stream; const stream = this.stream;
const chunks: Buffer[] = []; const chunks: Buffer[] = [];
@ -579,8 +582,10 @@ export class Http2ServerCallStream<
resolve(this.deserializeMessage(decompressedMessage)); resolve(this.deserializeMessage(decompressedMessage));
} }
} catch (err) { } catch (err) {
err.code = Status.INTERNAL; this.sendError({
this.sendError(err); code: Status.INTERNAL,
details: getErrorMessage(err)
});
resolve(); resolve();
} }
}); });
@ -630,8 +635,10 @@ export class Http2ServerCallStream<
this.write(response); this.write(response);
this.sendStatus({ code: Status.OK, details: 'OK', metadata }); this.sendStatus({ code: Status.OK, details: 'OK', metadata });
} catch (err) { } catch (err) {
err.code = Status.INTERNAL; this.sendError({
this.sendError(err); details: getErrorMessage(err),
code: Status.INTERNAL
});
} }
} }
@ -856,21 +863,15 @@ export class Http2ServerCallStream<
} catch (error) { } catch (error) {
// Ignore any remaining messages when errors occur. // Ignore any remaining messages when errors occur.
this.bufferedMessages.length = 0; this.bufferedMessages.length = 0;
let code = getErrorCode(error);
if ( if (code === null || code < Status.OK || code > Status.UNAUTHENTICATED) {
!( code = Status.INTERNAL
'code' in error &&
typeof error.code === 'number' &&
Number.isInteger(error.code) &&
error.code >= Status.OK &&
error.code <= Status.UNAUTHENTICATED
)
) {
// The error code is not a valid gRPC code so its being overwritten.
error.code = Status.INTERNAL;
} }
readable.emit('error', error); readable.emit('error', {
details: getErrorMessage(error),
code: code
});
} }
this.isPushPending = false; this.isPushPending = false;

View File

@ -61,6 +61,7 @@ import {
import { parseUri } from './uri-parser'; import { parseUri } from './uri-parser';
import { ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzServer, registerChannelzSocket, ServerInfo, ServerRef, SocketInfo, SocketRef, TlsInfo, unregisterChannelzRef } from './channelz'; import { ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzServer, registerChannelzSocket, ServerInfo, ServerRef, SocketInfo, SocketRef, TlsInfo, unregisterChannelzRef } from './channelz';
import { CipherNameAndProtocol, TLSSocket } from 'tls'; import { CipherNameAndProtocol, TLSSocket } from 'tls';
import { getErrorCode, getErrorMessage } from './error';
const TRACER_NAME = 'server'; const TRACER_NAME = 'server';
@ -859,11 +860,10 @@ export class Server {
} }
} }
if (err.code === undefined) { call.sendError({
err.code = Status.INTERNAL; code: getErrorCode(err) ?? Status.INTERNAL,
} details: getErrorMessage(err)
});
call.sendError(err);
} }
} }
); );