@@ -182,9 +182,10 @@ export class Http2CallStream extends Duplex implements CallStream {
182
182
this . cancelWithStatus ( Status . UNKNOWN , error . message ) ;
183
183
} ) ;
184
184
} ) ;
185
- stream . on ( 'trailers' , ( headers ) => {
185
+ stream . on ( 'trailers' , ( headers : http2 . IncomingHttpHeaders ) => {
186
186
let code : Status = this . mappedStatusCode ;
187
- if ( headers . hasOwnProperty ( 'grpc-status' ) ) {
187
+ let details = '' ;
188
+ if ( typeof headers [ 'grpc-status' ] === 'string' ) {
188
189
let receivedCode = Number ( headers [ 'grpc-status' ] ) ;
189
190
if ( receivedCode in Status ) {
190
191
code = receivedCode ;
@@ -193,9 +194,8 @@ export class Http2CallStream extends Duplex implements CallStream {
193
194
}
194
195
delete headers [ 'grpc-status' ] ;
195
196
}
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 ) ;
199
199
}
200
200
let metadata : Metadata ;
201
201
try {
@@ -301,7 +301,7 @@ export class Http2CallStream extends Duplex implements CallStream {
301
301
}
302
302
this . endCall ( { code : code , details : details , metadata : new Metadata ( ) } ) ;
303
303
} ) ;
304
- stream . on ( 'error' , ( ) => {
304
+ stream . on ( 'error' , ( err : Error ) => {
305
305
this . endCall ( {
306
306
code : Status . INTERNAL ,
307
307
details : 'Internal HTTP2 error' ,
@@ -325,7 +325,9 @@ export class Http2CallStream extends Duplex implements CallStream {
325
325
326
326
cancelWithStatus ( status : Status , details : string ) : void {
327
327
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 ) {
329
331
/* TODO(murgatroid99): Determine if we want to send different RST_STREAM
330
332
* codes based on the status code */
331
333
this . http2Stream . rstWithCancel ( ) ;
0 commit comments