Skip to content

Commit

Permalink
fix: fix destroying stream in middle of decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Nov 20, 2020
1 parent fed57f9 commit e72c5f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
return;
}

let decodedStream = res as unknown as Transform;
activeDecodedStream = res as unknown as Transform;
if (opts.acceptEncoding && res.headers['content-encoding']) {
for (let enc of res.headers['content-encoding'].split(', ').reverse()) {
let fn = opts.acceptEncoding[enc];
if (fn != null) {
decodedStream = decodedStream.pipe(fn());
decodedStream.on('error', onError);
activeDecodedStream = activeDecodedStream.pipe(fn());
activeDecodedStream.on('error', onError);
}
}
}
Expand All @@ -256,10 +256,9 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
contentLength > 0 && opts.maxReconnects > 0;
}
res.on('data', onData);
decodedStream.on('end', onEnd);
decodedStream.pipe(stream, { end: !acceptRanges });
activeDecodedStream.on('end', onEnd);
activeDecodedStream.pipe(stream, { end: !acceptRanges });
activeResponse = res;
activeDecodedStream = decodedStream;
stream.emit('response', res);
res.on('error', onError);
forwardEvents(res, responseEvents);
Expand All @@ -283,7 +282,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
let destroyErr: Error;
const streamDestroy = (err?: Error) => {
activeRequest.destroy(err);
activeDecodedStream?.unpipe(stream);
activeDecodedStream?.destroy();
clearTimeout(retryTimeout);
};

Expand Down
37 changes: 37 additions & 0 deletions test/request-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ describe('Make a request', () => {
.get('/compressedfile')
.reply(200, res, {
'content-length': filesize + '',
// Encoding is in reverse order.
'content-encoding': 'deflate, gzip',
});
const stream = miniget('http://yoursite.com/compressedfile', {
Expand Down Expand Up @@ -798,6 +799,42 @@ describe('Make a request', () => {
scope.done();
});
});

describe('destroy mid-stream', () => {
it('Stops stream without error', (done) => {
const res = fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(zlib.createDeflate());
const scope = nock('http://yoursite.com', {
reqheaders: { 'Accept-Encoding': 'gzip, deflate' }
})
.get('/compressedfile')
.reply(200, res, {
'content-length': filesize + '',
'content-encoding': 'gzip, deflate',
});
const stream = miniget('http://yoursite.com/compressedfile', {
acceptEncoding: {
gzip: (): Transform => zlib.createGunzip(),
deflate: (): Transform => zlib.createInflate(),
},
maxRetries: 0,
});
stream.on('error', done);
stream.resume();
let downloaded = 0;
stream.on('data', chunk => {
downloaded += chunk.length;
if (downloaded / filesize > 0.5) {
stream.destroy();
}
});
stream.on('close', () => {
scope.done();
done();
});
});
});
});

describe('with `method = "HEAD"`', () => {
Expand Down

0 comments on commit e72c5f6

Please sign in to comment.