Skip to content

Commit

Permalink
Merge pull request #326 from Phygon/fix-brotli-options
Browse files Browse the repository at this point in the history
Fixed zlib_options used for zlib.BrotliDecompress
  • Loading branch information
tomas authored Sep 1, 2020
2 parents 6493760 + 72a7dd0 commit 765ac3c
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,33 @@ var redirect_codes = [301, 302, 303, 307, 308];
//////////////////////////////////////////
// decompressors for gzip/deflate/br bodies

function bind_opts(fn, options) {
return fn.bind(null, options);
}

var decompressors = {};
var brotli_supported = false;

try {

var zlib = require('zlib');
brotli_supported = typeof zlib.BrotliDecompress === 'function';
decompressors['x-deflate'] = zlib.Inflate;
decompressors['deflate'] = zlib.Inflate;
decompressors['x-gzip'] = zlib.Gunzip;
decompressors['gzip'] = zlib.Gunzip;
if (brotli_supported) {
decompressors['br'] = zlib.BrotliDecompress;
}

// Enable Z_SYNC_FLUSH to avoid Z_BUF_ERROR errors (Node PR #2595)
var zlib_options = {
flush: zlib.Z_SYNC_FLUSH,
finishFlush: zlib.Z_SYNC_FLUSH
};
var br_options = {
flush: zlib.BROTLI_OPERATION_FLUSH,
finishFlush: zlib.BROTLI_OPERATION_FLUSH
};

var zlib = require('zlib');
brotli_supported = typeof zlib.BrotliDecompress === 'function';
decompressors['x-deflate'] = bind_opts(zlib.Inflate, zlib_options);
decompressors['deflate'] = bind_opts(zlib.Inflate, zlib_options);
decompressors['x-gzip'] = bind_opts(zlib.Gunzip, zlib_options);
decompressors['gzip'] = bind_opts(zlib.Gunzip, zlib_options);
if (brotli_supported) {
decompressors['br'] = bind_opts(zlib.BrotliDecompress, br_options);
}

} catch(e) { /* zlib not available */ }
Expand Down Expand Up @@ -571,7 +579,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
// To start, if our body is compressed and we're able to inflate it, do it.
if (headers['content-encoding'] && decompressors[headers['content-encoding']]) {

var decompressor = decompressors[headers['content-encoding']](zlib_options);
var decompressor = decompressors[headers['content-encoding']]();

// make sure we catch errors triggered by the decompressor.
decompressor.on('error', had_error);
Expand Down

0 comments on commit 765ac3c

Please sign in to comment.