Skip to content

Commit b7eb3d6

Browse files
committed
grpc-js-core: various fixes
1 parent 09cb531 commit b7eb3d6

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ export class Http2CallStream extends Duplex implements CallStream {
182182
this.cancelWithStatus(Status.UNKNOWN, error.message);
183183
});
184184
});
185-
stream.on('trailers', (headers) => {
185+
stream.on('trailers', (headers: http2.IncomingHttpHeaders) => {
186186
let code: Status = this.mappedStatusCode;
187-
if (headers.hasOwnProperty('grpc-status')) {
187+
let details = '';
188+
if (typeof headers['grpc-status'] === 'string') {
188189
let receivedCode = Number(headers['grpc-status']);
189190
if (receivedCode in Status) {
190191
code = receivedCode;
@@ -193,9 +194,8 @@ export class Http2CallStream extends Duplex implements CallStream {
193194
}
194195
delete headers['grpc-status'];
195196
}
196-
let details = '';
197-
if (headers.hasOwnProperty('grpc-message')) {
198-
details = decodeURI(headers['grpc-message']);
197+
if (typeof headers['grpc-message'] === 'string') {
198+
details = decodeURI(headers['grpc-message'] as string);
199199
}
200200
let metadata: Metadata;
201201
try {
@@ -301,7 +301,7 @@ export class Http2CallStream extends Duplex implements CallStream {
301301
}
302302
this.endCall({code: code, details: details, metadata: new Metadata()});
303303
});
304-
stream.on('error', () => {
304+
stream.on('error', (err: Error) => {
305305
this.endCall({
306306
code: Status.INTERNAL,
307307
details: 'Internal HTTP2 error',
@@ -325,7 +325,9 @@ export class Http2CallStream extends Duplex implements CallStream {
325325

326326
cancelWithStatus(status: Status, details: string): void {
327327
this.endCall({code: status, details: details, metadata: new Metadata()});
328-
if (this.http2Stream !== null) {
328+
// The http2 stream could already have been destroyed if cancelWithStatus
329+
// is called in response to an internal http2 error.
330+
if (this.http2Stream !== null && !this.http2Stream.destroyed) {
329331
/* TODO(murgatroid99): Determine if we want to send different RST_STREAM
330332
* codes based on the status code */
331333
this.http2Stream.rstWithCancel();

packages/grpc-js-core/src/metadata.ts

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export class Metadata {
166166
* Creates an OutgoingHttpHeaders object that can be used with the http2 API.
167167
*/
168168
toHttp2Headers(): http2.OutgoingHttpHeaders {
169+
// NOTE: Node <8.9 formats http2 headers incorrectly.
169170
const result: http2.OutgoingHttpHeaders = {};
170171
forOwn(this.internalRepr, (values, key) => {
171172
// We assume that the user's interaction with this object is limited to

0 commit comments

Comments
 (0)