Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-js: Update to newest typescript compiler #2259

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/grpc-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"devDependencies": {
"@types/gulp": "^4.0.6",
"@types/gulp-mocha": "0.0.32",
"@types/lodash": "4.14.186",
"@types/lodash": "^4.14.186",
"@types/mocha": "^5.2.6",
"@types/ncp": "^2.0.1",
"@types/pify": "^3.0.2",
"@types/semver": "^7.3.9",
"clang-format": "^1.0.55",
"execa": "^2.0.3",
"gts": "^2.0.0",
"gts": "^3.1.1",
"gulp": "^4.0.2",
"gulp-mocha": "^6.0.0",
"lodash": "^4.17.4",
Expand All @@ -35,7 +35,7 @@
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"ts-node": "^8.3.0",
"typescript": "^3.7.2"
"typescript": "^4.8.4"
},
"contributors": [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/grpc-js/src/call-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export abstract class CallCredentials {
reject(err);
return;
}
if (!headers) {
reject(new Error('Headers not set by metadata plugin'));
return;
}
resolve(headers);
}
);
Expand Down
5 changes: 3 additions & 2 deletions packages/grpc-js/src/client-interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { Channel } from './channel';
import { CallOptions } from './client';
import { CallCredentials } from './call-credentials';
import { ClientMethodDefinition } from './make-client';
import { getErrorMessage } from './error';

/**
* Error class associated with passing both interceptors and interceptor
Expand Down Expand Up @@ -374,7 +375,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
} catch (e) {
this.call.cancelWithStatus(
Status.INTERNAL,
`Request message serialization failure: ${e.message}`
`Request message serialization failure: ${getErrorMessage(e)}`
);
return;
}
Expand All @@ -401,7 +402,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
} catch (e) {
readError = {
code: Status.INTERNAL,
details: `Response message parsing error: ${e.message}`,
details: `Response message parsing error: ${getErrorMessage(e)}`,
metadata: new Metadata(),
};
this.call.cancelWithStatus(readError.code, readError.details);
Expand Down
37 changes: 37 additions & 0 deletions packages/grpc-js/src/error.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 2 additions & 1 deletion packages/grpc-js/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import * as http2 from 'http2';
import { log } from './logging';
import { LogVerbosity } from './constants';
import { getErrorMessage } from './error';
const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;

Expand Down Expand Up @@ -294,7 +295,7 @@ export class Metadata {
}
}
} 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);
}
});
Expand Down
45 changes: 23 additions & 22 deletions packages/grpc-js/src/server-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ChannelOptions } from './channel-options';
import * as logging from './logging';
import { StatusObject } from './call-interface';
import { Deadline } from './deadline';
import { getErrorCode, getErrorMessage } from './error';

const TRACER_NAME = 'server_call';

Expand Down Expand Up @@ -230,8 +231,10 @@ export class ServerWritableStreamImpl<RequestType, ResponseType>
return;
}
} catch (err) {
err.code = Status.INTERNAL;
this.emit('error', err);
this.emit('error', {
details: getErrorMessage(err),
code: Status.INTERNAL
});
}

callback();
Expand Down Expand Up @@ -462,7 +465,7 @@ export class Http2ServerCallStream<
private getDecompressedMessage(message: Buffer, encoding: string) {
switch (encoding) {
case 'deflate': {
return new Promise<Buffer | undefined>((resolve, reject) => {
return new Promise<Buffer | void>((resolve, reject) => {
zlib.inflate(message.slice(5), (err, output) => {
if (err) {
this.sendError({
Expand All @@ -478,7 +481,7 @@ export class Http2ServerCallStream<
}

case 'gzip': {
return new Promise<Buffer | undefined>((resolve, reject) => {
return new Promise<Buffer | void>((resolve, reject) => {
zlib.unzip(message.slice(5), (err, output) => {
if (err) {
this.sendError({
Expand Down Expand Up @@ -559,7 +562,7 @@ export class Http2ServerCallStream<
return metadata;
}

receiveUnaryMessage(encoding: string): Promise<RequestType> {
receiveUnaryMessage(encoding: string): Promise<RequestType | void> {
return new Promise((resolve, reject) => {
const stream = this.stream;
const chunks: Buffer[] = [];
Expand Down Expand Up @@ -599,8 +602,10 @@ export class Http2ServerCallStream<
resolve(this.deserializeMessage(decompressedMessage));
}
} catch (err) {
err.code = Status.INTERNAL;
this.sendError(err);
this.sendError({
details: getErrorMessage(err),
code: Status.INTERNAL
});
resolve();
}
});
Expand Down Expand Up @@ -650,8 +655,10 @@ export class Http2ServerCallStream<
this.write(response);
this.sendStatus({ code: Status.OK, details: 'OK', metadata });
} catch (err) {
err.code = Status.INTERNAL;
this.sendError(err);
this.sendError({
details: getErrorMessage(err),
code: Status.INTERNAL
});
}
}

Expand Down Expand Up @@ -878,21 +885,15 @@ export class Http2ServerCallStream<
} catch (error) {
// Ignore any remaining messages when errors occur.
this.bufferedMessages.length = 0;

if (
!(
'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;
let code = getErrorCode(error);
if (code === null || code < Status.OK || code > Status.UNAUTHENTICATED) {
code = Status.INTERNAL;
}

readable.emit('error', error);
readable.emit('error', {
details: getErrorMessage(error),
code: code
});
}

this.isPushPending = false;
Expand Down
10 changes: 5 additions & 5 deletions packages/grpc-js/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
import { parseUri } from './uri-parser';
import { ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzServer, registerChannelzSocket, ServerInfo, ServerRef, SocketInfo, SocketRef, TlsInfo, unregisterChannelzRef } from './channelz';
import { CipherNameAndProtocol, TLSSocket } from 'tls';
import { getErrorCode, getErrorMessage } from './error';

const TRACER_NAME = 'server';

Expand Down Expand Up @@ -865,11 +866,10 @@ export class Server {
}
}

if (err.code === undefined) {
err.code = Status.INTERNAL;
}

call.sendError(err);
call.sendError({
details: getErrorMessage(err),
code: getErrorCode(err) ?? Status.INTERNAL
});
}
}
);
Expand Down