Skip to content

Commit

Permalink
fix: stop emitting any error after stream has ended
Browse files Browse the repository at this point in the history
fent committed Feb 2, 2021
1 parent 1fec7c5 commit 629832c
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 2 additions & 4 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -14,10 +14,7 @@ plugins:
rules:
no-await-in-loop: off
no-compare-neg-zero: error
no-extra-parens:
- warn
- all
- nestedBinaryExpressions: false
no-extra-parens: off
no-template-curly-in-string: error
no-unsafe-negation: error
valid-jsdoc:
@@ -208,3 +205,4 @@ rules:
'@typescript-eslint/no-namespace': off
'@typescript-eslint/ban-ts-ignore': off
'@typescript-eslint/no-unused-vars': off
'@typescript-eslint/no-extra-parens': warn
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -193,6 +193,9 @@ function Miniget(url: string | URL, options: Miniget.Options = {}): Miniget.Stre
}

const onError = (err: Miniget.MinigetError): void => {
if (stream.destroyed || stream.readableEnded) { return; }
// Needed for node v10.
if ((stream as any)._readableState.ended) { return; }
cleanup();
if (!retryRequest({ err })) {
stream.emit('error', err);
@@ -207,12 +210,9 @@ function Miniget(url: string | URL, options: Miniget.Options = {}): Miniget.Stre
};

const cleanup = () => {
activeRequest.removeListener('error', onError);
activeRequest.removeListener('close', onRequestClose);
activeResponse?.removeListener('data', onData);
activeDecodedStream?.removeListener('end', onEnd);
activeDecodedStream?.removeListener('error', onError);
activeResponse?.removeListener('error', onError);
};

const onData = (chunk: Buffer) => { downloaded += chunk.length; };
20 changes: 20 additions & 0 deletions test/request-test.ts
Original file line number Diff line number Diff line change
@@ -870,6 +870,26 @@ describe('Make a request', () => {
});
});

describe('`response` emits error after end', () => {
it('Error does not get emitted to stream', done => {
const scope = nock('https://hello.com')
.get('/one/two')
.reply(200, '<html></html>');
const stream = miniget('https://hello.com/one/two');
stream.resume();
let res: IncomingMessage;
stream.on('response', a => res = a);
stream.on('error', done);
stream.on('end', () => {
process.nextTick(() => {
res.emit('error', Error('random after end error'));
});
scope.done();
done();
});
});
});

describe('with `method = "HEAD"`', () => {
it('Emits `response`', done => {
const scope = nock('http://hello.net')

0 comments on commit 629832c

Please sign in to comment.