Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js compatibility fixes #27

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';
const {
pipeline: streamPipeline,
PassThrough: PassThroughStream
} = require('stream');
const {Transform, PassThrough} = require('stream');
const zlib = require('zlib');
const mimicResponse = require('mimic-response');

Expand All @@ -16,25 +13,46 @@ const decompressResponse = response => {
// TODO: Remove this when targeting Node.js 12.
const isBrotli = contentEncoding === 'br';
if (isBrotli && typeof zlib.createBrotliDecompress !== 'function') {
response.destroy(new Error('Brotli is not supported on Node.js < 12'));
return response;
}

const decompress = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip();
const stream = new PassThroughStream();
let isEmpty = true;

decompress.on('error', error => {
// Ignore empty response
if (error.code === 'Z_BUF_ERROR') {
stream.end();
return;
const checker = new Transform({
transform(data, _encoding, callback) {
isEmpty = false;

callback(null, data);
},

flush(callback) {
callback();
}
});

const finalStream = new PassThrough({
autoDestroy: false,
destroy(error, callback) {
response.destroy();

stream.emit('error', error);
callback(error);
}
});

const finalStream = streamPipeline(response, decompress, stream, () => {});
const decompressStream = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip();

decompressStream.once('error', error => {
if (isEmpty && response.readableEnded) {
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
finalStream.end();
return;
}

finalStream.destroy(error);
});

mimicResponse(response, finalStream);
response.pipe(checker).pipe(decompressStream).pipe(finalStream);

return finalStream;
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=10"
Expand Down Expand Up @@ -37,7 +37,7 @@
"brotli"
],
"dependencies": {
"mimic-response": "^2.0.0"
"mimic-response": "^3.1.0"
},
"devDependencies": {
"@types/node": "^12.7.1",
Expand Down
9 changes: 7 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ test('decompress gzipped content', async t => {
response.setEncoding('utf8');

t.is(await getStream(response), fixture);

t.false(response.destroyed);
});

test('decompress deflated content', async t => {
Expand All @@ -95,15 +97,18 @@ if (typeof zlib.brotliCompress === 'function') {
});
}

test('ignore missing data', async t => {
test('does not ignore missing data', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/missing-data`));

t.is(typeof response.httpVersion, 'string');
t.truthy(response.headers);

response.setEncoding('utf8');

t.is(await getStream(response), fixture);
await t.throwsAsync(getStream(response), {
message: 'unexpected end of file',
code: 'Z_BUF_ERROR'
});
});

test('preserves custom properties on the stream', async t => {
Expand Down