Skip to content

Commit c540ef6

Browse files
authored
fix: reduce duplicate code (#1612)
* fix: reduce duplicate code * fix: removed unused dependency * fix: npm run fix * apply Dan's suggestion * add error handling back in * lint
1 parent d3e3bf9 commit c540ef6

File tree

3 files changed

+22
-58
lines changed

3 files changed

+22
-58
lines changed

gax/src/streamingCalls/streaming.ts

+18-48
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,7 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
271271
*/
272272
streamHandoffHelper(stream: CancellableStream, retry: RetryOptions): void {
273273
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);
279275

280276
stream.on('error', error => {
281277
enteredError = true;
@@ -296,20 +292,14 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
296292
});
297293
}
298294

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) {
308296
const eventsToForward = ['metadata', 'response', 'status'];
309297
eventsToForward.forEach(event => {
310298
stream.on(event, this.emit.bind(this, event));
311299
});
300+
}
312301

302+
statusMetadataHelper(stream: Stream) {
313303
// gRPC is guaranteed emit the 'status' event but not 'metadata', and 'status' is the last event to emit.
314304
// Emit the 'response' event if stream has no 'metadata' event.
315305
// This avoids the stream swallowing the other events, such as 'end'.
@@ -340,6 +330,18 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
340330
});
341331
this._responseHasSent = true;
342332
});
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+
343345
stream.on('error', error => {
344346
GoogleError.parseGRPCStatusDetails(error);
345347
});
@@ -368,40 +370,8 @@ export class StreamProxy extends duplexify implements GRPCCallResult {
368370
retry: RetryOptions
369371
): CancellableStream | undefined {
370372
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);
405375

406376
stream.on('error', error => {
407377
const timeout = retry.backoffSettings.totalTimeoutMillis;

gax/src/streamingRetryRequest.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const requestOps = null;
3030
const objectMode = true; // we don't support objectMode being false
3131

3232
interface streamingRetryRequestOptions {
33-
request?: Function;
33+
request: Function;
3434
maxRetries?: number;
3535
}
3636
/**
@@ -40,14 +40,8 @@ interface streamingRetryRequestOptions {
4040
*/
4141
export function streamingRetryRequest(opts: streamingRetryRequestOptions) {
4242
opts = Object.assign({}, DEFAULTS, opts);
43-
4443
if (opts.request === undefined) {
45-
try {
46-
// eslint-disable-next-line node/no-unpublished-require
47-
opts.request = require('request');
48-
} catch (e) {
49-
throw new Error('A request library must be provided to retry-request.');
50-
}
44+
throw new Error('A request function must be provided');
5145
}
5246

5347
let numNoResponseAttempts = 0;

gax/test/unit/streamingRetryRequest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ describe('retry-request', () => {
9696
});
9797
assert.strictEqual(retryStream._readableState.objectMode, true);
9898
});
99-
10099
it('throws request error', done => {
101100
try {
102101
const opts = {};
102+
//@ts-expect-error
103103
streamingRetryRequest(opts);
104104
} catch (err) {
105105
assert(err instanceof Error);
106-
assert.match(err.message, /A request library must be provided/);
106+
assert.match(err.message, /A request function must be provided/);
107107
done();
108108
}
109109
});

0 commit comments

Comments
 (0)