@@ -271,11 +271,7 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
271
271
*/
272
272
streamHandoffHelper ( stream : CancellableStream , retry : RetryOptions ) : void {
273
273
let enteredError = false ;
274
- const eventsToForward = [ 'metadata' , 'response' , 'status' ] ;
275
-
276
- eventsToForward . forEach ( event => {
277
- stream . on ( event , this . emit . bind ( this , event ) ) ;
278
- } ) ;
274
+ this . eventForwardHelper ( stream ) ;
279
275
280
276
stream . on ( 'error' , error => {
281
277
enteredError = true ;
@@ -296,20 +292,14 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
296
292
} ) ;
297
293
}
298
294
299
- /**
300
- * Forward events from an API request stream to the user's stream.
301
- * @param {Stream } stream - The API request stream.
302
- * @param {RetryOptions } retry - Configures the exceptions upon which the
303
- * function should retry, and the parameters to the exponential backoff retry
304
- * algorithm.
305
- */
306
-
307
- forwardEvents ( stream : Stream ) {
295
+ eventForwardHelper ( stream : Stream ) {
308
296
const eventsToForward = [ 'metadata' , 'response' , 'status' ] ;
309
297
eventsToForward . forEach ( event => {
310
298
stream . on ( event , this . emit . bind ( this , event ) ) ;
311
299
} ) ;
300
+ }
312
301
302
+ statusMetadataHelper ( stream : Stream ) {
313
303
// gRPC is guaranteed emit the 'status' event but not 'metadata', and 'status' is the last event to emit.
314
304
// Emit the 'response' event if stream has no 'metadata' event.
315
305
// This avoids the stream swallowing the other events, such as 'end'.
@@ -340,6 +330,18 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
340
330
} ) ;
341
331
this . _responseHasSent = true ;
342
332
} ) ;
333
+ }
334
+ /**
335
+ * Forward events from an API request stream to the user's stream.
336
+ * @param {Stream } stream - The API request stream.
337
+ * @param {RetryOptions } retry - Configures the exceptions upon which the
338
+ * function should retry, and the parameters to the exponential backoff retry
339
+ * algorithm.
340
+ */
341
+ forwardEvents ( stream : Stream ) {
342
+ this . eventForwardHelper ( stream ) ;
343
+ this . statusMetadataHelper ( stream ) ;
344
+
343
345
stream . on ( 'error' , error => {
344
346
GoogleError . parseGRPCStatusDetails ( error ) ;
345
347
} ) ;
@@ -368,40 +370,8 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
368
370
retry : RetryOptions
369
371
) : CancellableStream | undefined {
370
372
let retryStream = this . stream ;
371
- const eventsToForward = [ 'metadata' , 'response' , 'status' ] ;
372
- eventsToForward . forEach ( event => {
373
- stream . on ( event , this . emit . bind ( this , event ) ) ;
374
- } ) ;
375
- // gRPC is guaranteed emit the 'status' event but not 'metadata', and 'status' is the last event to emit.
376
- // Emit the 'response' event if stream has no 'metadata' event.
377
- // This avoids the stream swallowing the other events, such as 'end'.
378
- stream . on ( 'status' , ( ) => {
379
- if ( ! this . _responseHasSent ) {
380
- stream . emit ( 'response' , {
381
- code : 200 ,
382
- details : '' ,
383
- message : 'OK' ,
384
- } ) ;
385
- }
386
- } ) ;
387
-
388
- // We also want to supply the status data as 'response' event to support
389
- // the behavior of google-cloud-node expects.
390
- // see:
391
- // https://github.com/GoogleCloudPlatform/google-cloud-node/pull/1775#issuecomment-259141029
392
- // https://github.com/GoogleCloudPlatform/google-cloud-node/blob/116436fa789d8b0f7fc5100b19b424e3ec63e6bf/packages/common/src/grpc-service.js#L355
393
- stream . on ( 'metadata' , metadata => {
394
- // Create a response object with succeeds.
395
- // TODO: unify this logic with the decoration of gRPC response when it's
396
- // added. see: https://github.com/googleapis/gax-nodejs/issues/65
397
- stream . emit ( 'response' , {
398
- code : 200 ,
399
- details : '' ,
400
- message : 'OK' ,
401
- metadata,
402
- } ) ;
403
- this . _responseHasSent = true ;
404
- } ) ;
373
+ this . eventForwardHelper ( stream ) ;
374
+ this . statusMetadataHelper ( stream ) ;
405
375
406
376
stream . on ( 'error' , error => {
407
377
const timeout = retry . backoffSettings . totalTimeoutMillis ;
0 commit comments