Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PATCH v2] Add support for gzip content decoding
Support decoding of the gzip Content-Encoding in responses using the zlib module to decode the response before piping it through the request object (and therefore before user-connected pipes or body parsing). Add the boolean option `gzip` to allow users to explicitly request decoding of supported response content and inclusion of appropriate content negotiation headers, if unspecified. This commit favors backwards-compatibility over the increased performance that transparent compression could provide, although it is hoped that a future backwards-incompatible version can make transparent compression the default. Some major tradeoffs of transparent compression are: - It may trigger changes in server behavior and performance (for better or worse) that are unexpected, either due to buggy servers or intermediate network hardware. - The compression is not fully transparent as users who attach to the `data` event of the response (rather than the `data` event of `request`) will get gzipped data rather than uncompressed data. + It is likely a big win for most users (both current and future) who would otherwise be unaware or unable to spend the time to implement content negotiation and decoding. Especially given the prevalence of highly-compressible text content (e.g. JSON and XML) and widespread server support for gzip. Changes since v1: - Rename `decodeContent` option to `gzip` to match option name in 3.0 Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
- Loading branch information
Showing
3 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
var request = require('../index') | ||
, http = require('http') | ||
, assert = require('assert') | ||
, zlib = require('zlib') | ||
|
||
var testContent = 'Compressible response content.\n' | ||
, testContentGzip | ||
|
||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/plain') | ||
|
||
if (/\bgzip\b/i.test(req.headers['accept-encoding'])) { | ||
zlib.gzip(testContent, function (err, data) { | ||
assert.ifError(err) | ||
testContentGzip = data | ||
res.setHeader('Content-Encoding', 'gzip') | ||
res.end(data) | ||
}) | ||
} else { | ||
res.end(testContent) | ||
} | ||
}) | ||
|
||
server.listen(6767, function (err) { | ||
assert.ifError(err) | ||
|
||
var headers, options | ||
|
||
// Transparently supports gzip decoding to callbacks | ||
options = { url: 'http://localhost:6767/foo', gzip: true } | ||
request.get(options, function (err, res, body) { | ||
assert.ifError(err) | ||
assert.strictEqual(res.headers['content-encoding'], 'gzip') | ||
assert.strictEqual(body, testContent) | ||
}) | ||
|
||
|
||
// Transparently supports gzip decoding to pipes | ||
options = { url: 'http://localhost:6767/foo', gzip: true } | ||
var chunks = [] | ||
request.get(options) | ||
.on('data', function (chunk) { chunks.push(chunk) }) | ||
.on('end', function () { | ||
assert.strictEqual(Buffer.concat(chunks).toString(), testContent) | ||
}) | ||
.on('error', function (err) { assert.ifError(err) }) | ||
|
||
|
||
// Does not request gzip if user specifies Accepted-Encodings | ||
headers = { 'Accept-Encoding': null } | ||
options = { | ||
url: 'http://localhost:6767/foo', | ||
headers: headers, | ||
gzip: true | ||
} | ||
request.get(options, function (err, res, body) { | ||
assert.ifError(err) | ||
assert.strictEqual(res.headers['content-encoding'], undefined) | ||
assert.strictEqual(body, testContent) | ||
}) | ||
|
||
|
||
// Does not decode user-requested encoding by default | ||
headers = { 'Accept-Encoding': 'gzip' } | ||
options = { url: 'http://localhost:6767/foo', headers: headers } | ||
request.get(options, function (err, res, body) { | ||
assert.ifError(err) | ||
assert.strictEqual(res.headers['content-encoding'], 'gzip') | ||
assert.strictEqual(body, testContentGzip.toString()) | ||
}) | ||
|
||
|
||
// Supports character encoding with gzip encoding | ||
headers = { 'Accept-Encoding': 'gzip' } | ||
options = { | ||
url: 'http://localhost:6767/foo', | ||
headers: headers, | ||
gzip: true, | ||
encoding: "utf8" | ||
} | ||
var strings = [] | ||
request.get(options) | ||
.on('data', function (string) { | ||
assert.strictEqual(typeof string, "string") | ||
strings.push(string) | ||
}) | ||
.on('end', function () { | ||
assert.strictEqual(strings.join(""), testContent) | ||
|
||
// Shutdown server after last test | ||
server.close() | ||
}) | ||
.on('error', function (err) { assert.ifError(err) }) | ||
}) |