Skip to content

Commit

Permalink
Turn on gzip encoding in request.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Jul 9, 2019
1 parent 4cb8217 commit 0acaa78
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
30 changes: 21 additions & 9 deletions http.js
Expand Up @@ -13,8 +13,20 @@ var defaults = {
timeout: 60000,
// retries: 1, // needed for `got` only, not supported by `request`
headers: {
'User-Agent': defaultAgent
}
'User-Agent': defaultAgent,

// Override default "gzip, deflate" header that is auto-inserted when
// request has gzip option turned on.
//
// It's done so because gzip may have large block size, and we only need
// to know a first few bytes to extract image size.
//
'Accept-Encoding': 'identity'
},

// turn gzip decompression on in case there are misconfigured servers
// that always return gzip encoded content even if not requested
gzip: true
};

var P;
Expand All @@ -25,20 +37,20 @@ module.exports = function probeHttp(src, options) {
P = P || require('any-promise');

return new P(function (resolve, reject) {
var req, length, finalUrl;
var stream, length, finalUrl;

try {
req = request(merge.all([ { url: src }, defaults, options ]));
stream = request(merge.all([ { url: src }, defaults, options ]));
} catch (err) {
reject(err);
return;
}

req.on('response', function (res) {
stream.on('response', function (res) {
if (res.statusCode !== 200) {
var err = new ProbeError('bad status code: ' + res.statusCode, null, res.statusCode);

req.abort();
stream.abort();
reject(err);

return;
Expand All @@ -49,7 +61,7 @@ module.exports = function probeHttp(src, options) {
if (len && len.match(/^\d+$/)) length = +len;
finalUrl = res.request.uri.href;

probeStream(res)
probeStream(stream)
.then(function (result) {
if (length) result.length = length;

Expand All @@ -58,10 +70,10 @@ module.exports = function probeHttp(src, options) {
resolve(result);
})
.catch(reject)
.then(function () { req.abort(); });
.then(function () { stream.abort(); });
});

req.on('error', function (err) {
stream.on('error', function (err) {
/* This check needed for `got` only, because it returns 404 as error.
if (err.statusCode) {
reject(new ProbeError('bad status code: ' + err.statusCode, null, err.statusCode));
Expand Down
3 changes: 2 additions & 1 deletion stream.js
Expand Up @@ -45,7 +45,8 @@ module.exports = function probeStream(stream) {
});

function cleanup() {
stream.unpipe(proxy);
// request stream doesn't have unpipe, https://github.com/request/request/issues/874
if (typeof stream.unpipe === 'function') stream.unpipe(this);
proxy.end();
}

Expand Down
9 changes: 8 additions & 1 deletion test/http.js
Expand Up @@ -39,7 +39,14 @@ describe('probeHttp', function () {
});

it('should process an image (callback)', function (callback) {
var haveResponse = false;

responder = function (req, res) {
req.on('close', function () {
assert.ok(haveResponse);
callback();
});

res.writeHead(200);
res.write(GIF1x1);

Expand All @@ -52,7 +59,7 @@ describe('probeHttp', function () {
assert.equal(size.height, 1);
assert.equal(size.mime, 'image/gif');

callback();
haveResponse = true;
});
});

Expand Down

0 comments on commit 0acaa78

Please sign in to comment.