From a150c4f5a2fc4519e245cf69cb3a09099d9769dc Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Tue, 10 Oct 2023 18:32:38 -0700 Subject: [PATCH] fix: handle stream complete/finish events uniformly on errors `teeny-request` and `request` emit different calls when an error occurs. --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b13cf5d..972e92b 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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 {