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

Support for deflate raw (Closes #34) #35

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 27 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,36 @@ export default function decompressResponse(response) {

let isEmpty = true;

function handleContentEncoding(data) {
const decompressStream = contentEncoding === 'br'
? zlib.createBrotliDecompress()
: ((contentEncoding === 'deflate' && data.length > 0 && (data[0] & 0x08) === 0) // eslint-disable-line no-bitwise
? zlib.createInflateRaw()
: zlib.createUnzip());

decompressStream.once('error', error => {
if (isEmpty && !response.readable) {
finalStream.end();
return;
}

finalStream.destroy(error);
});

checker.pipe(decompressStream).pipe(finalStream);
}

const checker = new TransformStream({
transform(data, _encoding, callback) {
if (isEmpty === false) {
callback(null, data);
return;
}

isEmpty = false;

handleContentEncoding(data);

callback(null, data);
},

Expand All @@ -34,19 +60,8 @@ export default function decompressResponse(response) {
},
});

const decompressStream = contentEncoding === 'br' ? zlib.createBrotliDecompress() : zlib.createUnzip();

decompressStream.once('error', error => {
if (isEmpty && !response.readable) {
finalStream.end();
return;
}

finalStream.destroy(error);
});

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

return finalStream;
}
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ test.before('setup', async () => {
response.end(await zlibP.deflate(fixture));
});

server.on('/deflateRaw', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-encoding-type', 'text/plain');
response.setHeader('content-encoding', 'deflate');
response.end(await zlibP.deflateRaw(fixture));
});

server.on('/brotli', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-type', 'text/plain');
Expand Down Expand Up @@ -91,6 +98,17 @@ test('decompress deflated content', async t => {
t.is(await getStream(response), fixture);
});

test('decompress raw-deflated content', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/deflateRaw`));

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

response.setEncoding('utf8');

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

test('decompress brotli content', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/brotli`));

Expand Down