Skip to content

Commit

Permalink
fix: handle stream complete/finish events uniformly on errors
Browse files Browse the repository at this point in the history
`teeny-request` and `request` emit different calls when an error occurs.
  • Loading branch information
d-goog committed Oct 11, 2023
1 parent 47d5f59 commit a150c4f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,17 @@ function retryRequest(requestOpts, opts, callback) {
}

function makeRequest() {
let finishHandled = false;
currentRetryAttempt++;
debug(`Current retry attempt: ${currentRetryAttempt}`);

function handleFinish(args = []) {
if (!finishHandled) {
finishHandled = true;
retryStream.emit('complete', ...args);
}
}

if (streamMode) {
streamResponseHandled = false;

Expand Down Expand Up @@ -163,7 +171,8 @@ function retryRequest(requestOpts, opts, callback) {
streamResponseHandled = true;
onResponse(null, resp, body);
})
.on('complete', retryStream.emit.bind(retryStream, 'complete'));
.on('complete', (...params) => handleFinish(params))
.on('finish', (...params) => handleFinish(params));

requestStream.pipe(delayStream);
} else {
Expand Down

0 comments on commit a150c4f

Please sign in to comment.