@@ -9,9 +9,10 @@ import {FilterStackFactory} from './filter-stack';
99import { Metadata } from './metadata' ;
1010import { ObjectDuplex } from './object-stream' ;
1111
12- const { HTTP2_HEADER_STATUS , HTTP2_HEADER_CONTENT_TYPE , NGHTTP2_CANCEL } = http2 . constants ;
12+ const { HTTP2_HEADER_STATUS , HTTP2_HEADER_CONTENT_TYPE , NGHTTP2_CANCEL } =
13+ http2 . constants ;
1314
14- export type Deadline = Date | number ;
15+ export type Deadline = Date | number ;
1516
1617export interface CallStreamOptions {
1718 deadline : Deadline ;
@@ -36,20 +37,19 @@ export interface WriteObject {
3637/**
3738 * This interface represents a duplex stream associated with a single gRPC call.
3839 */
39- export type CallStream = {
40- cancelWithStatus ( status : Status , details : string ) : void ;
41- getPeer ( ) : string ;
40+ export type CallStream = {
41+ cancelWithStatus ( status : Status , details : string ) : void ; getPeer ( ) : string ;
4242
4343 getDeadline ( ) : Deadline ;
4444 getCredentials ( ) : CallCredentials ;
4545 /* If the return value is null, the call has not ended yet. Otherwise, it has
4646 * ended with the specified status */
47- getStatus ( ) : StatusObject | null ;
47+ getStatus ( ) : StatusObject | null ;
4848 getMethod ( ) : string ;
4949 getHost ( ) : string ;
50- } & EmitterAugmentation1 < 'metadata' , Metadata >
51- & EmitterAugmentation1 < 'status' , StatusObject >
52- & ObjectDuplex < WriteObject , Buffer > ;
50+ } & EmitterAugmentation1 < 'metadata' , Metadata > &
51+ EmitterAugmentation1 < 'status' , StatusObject > &
52+ ObjectDuplex < WriteObject , Buffer > ;
5353
5454enum ReadState {
5555 NO_DATA ,
@@ -60,7 +60,7 @@ enum ReadState {
6060const emptyBuffer = Buffer . alloc ( 0 ) ;
6161
6262export class Http2CallStream extends Duplex implements CallStream {
63- public filterStack : Filter ;
63+ filterStack : Filter ;
6464 private statusEmitted = false ;
6565 private http2Stream : http2 . ClientHttp2Stream | null = null ;
6666 private pendingRead = false ;
@@ -76,7 +76,7 @@ export class Http2CallStream extends Duplex implements CallStream {
7676 private readPartialMessage : Buffer [ ] = [ ] ;
7777 private readMessageRemaining = 0 ;
7878
79- private unpushedReadMessages : ( Buffer | null ) [ ] = [ ] ;
79+ private unpushedReadMessages : Array < Buffer | null > = [ ] ;
8080
8181 // Status code mapped from :status. To be used if grpc-status is not received
8282 private mappedStatusCode : Status = Status . UNKNOWN ;
@@ -124,20 +124,21 @@ export class Http2CallStream extends Duplex implements CallStream {
124124 }
125125
126126 private handleTrailers ( headers : http2 . IncomingHttpHeaders ) {
127- let code : Status = this . mappedStatusCode ;
128- let details = '' ;
127+ const code : Status = this . mappedStatusCode ;
128+ const details = '' ;
129129 let metadata : Metadata ;
130130 try {
131131 metadata = Metadata . fromHttp2Headers ( headers ) ;
132132 } catch ( e ) {
133133 metadata = new Metadata ( ) ;
134134 }
135- let status : StatusObject = { code, details, metadata} ;
135+ const status : StatusObject = { code, details, metadata} ;
136136 this . handlingTrailers = ( async ( ) => {
137137 let finalStatus ;
138138 try {
139139 // Attempt to assign final status.
140- finalStatus = await this . filterStack . receiveTrailers ( Promise . resolve ( status ) ) ;
140+ finalStatus =
141+ await this . filterStack . receiveTrailers ( Promise . resolve ( status ) ) ;
141142 } catch ( error ) {
142143 await this . handlingHeaders ;
143144 // This is a no-op if the call was already ended when handling headers.
@@ -195,17 +196,26 @@ export class Http2CallStream extends Duplex implements CallStream {
195196 try {
196197 metadata = Metadata . fromHttp2Headers ( headers ) ;
197198 } catch ( error ) {
198- this . endCall ( { code : Status . UNKNOWN , details : error . message , metadata : new Metadata ( ) } ) ;
199+ this . endCall ( {
200+ code : Status . UNKNOWN ,
201+ details : error . message ,
202+ metadata : new Metadata ( )
203+ } ) ;
199204 return ;
200205 }
201206 this . handlingHeaders =
202- this . filterStack . receiveMetadata ( Promise . resolve ( metadata ) )
203- . then ( ( finalMetadata ) => {
204- this . emit ( 'metadata' , finalMetadata ) ;
205- } ) . catch ( ( error ) => {
206- this . destroyHttp2Stream ( ) ;
207- this . endCall ( { code : Status . UNKNOWN , details : error . message , metadata : new Metadata ( ) } ) ;
208- } ) ;
207+ this . filterStack . receiveMetadata ( Promise . resolve ( metadata ) )
208+ . then ( ( finalMetadata ) => {
209+ this . emit ( 'metadata' , finalMetadata ) ;
210+ } )
211+ . catch ( ( error ) => {
212+ this . destroyHttp2Stream ( ) ;
213+ this . endCall ( {
214+ code : Status . UNKNOWN ,
215+ details : error . message ,
216+ metadata : new Metadata ( )
217+ } ) ;
218+ } ) ;
209219 }
210220 } ) ;
211221 stream . on ( 'trailers' , this . handleTrailers . bind ( this ) ) ;
@@ -260,6 +270,9 @@ export class Http2CallStream extends Duplex implements CallStream {
260270 canPush = this . tryPush ( messageBytes , canPush ) ;
261271 this . readState = ReadState . NO_DATA ;
262272 }
273+ break ;
274+ default :
275+ throw new Error ( 'This should never happen' ) ;
263276 }
264277 }
265278 } ) ;
@@ -298,7 +311,7 @@ export class Http2CallStream extends Duplex implements CallStream {
298311 // This is OK, because status codes emitted here correspond to more
299312 // catastrophic issues that prevent us from receiving trailers in the
300313 // first place.
301- this . endCall ( { code : code , details : details , metadata : new Metadata ( ) } ) ;
314+ this . endCall ( { code, details, metadata : new Metadata ( ) } ) ;
302315 } ) ;
303316 stream . on ( 'error' , ( err : Error ) => {
304317 this . endCall ( {
@@ -338,7 +351,7 @@ export class Http2CallStream extends Duplex implements CallStream {
338351 // If trailers are currently being processed, the call should be ended
339352 // by handleTrailers instead.
340353 await this . handlingTrailers ;
341- this . endCall ( { code : status , details : details , metadata : new Metadata ( ) } ) ;
354+ this . endCall ( { code : status , details, metadata : new Metadata ( ) } ) ;
342355 } ) ( ) ;
343356 }
344357
0 commit comments