8000 Support request gzip content. · npm/npm-registry-client@93a5580 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 93a5580

Browse files
committed
Support request gzip content.
Current CDN almost support auto gzip text base content mirrors. e.g.: http://registry.qiniudn.com/npm It can save ~650kb through gzip 700kb json content to 38kb.
1 parent effb4bc commit 93a5580

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/request.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = regRequest
22

33
var url = require("url")
4+
, zlib = require("zlib")
45
, fs = require("graceful-fs")
56
, rm = require("rimraf")
67
, asyncMap = require("slide").asyncMap
@@ -128,6 +129,7 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
128129
if (strict === undefined) strict = true
129130
var opts = { url: remote
130131
, method: method
132+
, encoding: null // tell request let body be Buffer instance
131133
, ca: this.conf.get('ca')
132134
, localAddress: this.conf.get('local-address')
133135
, cert: this.conf.get('cert')
@@ -140,6 +142,7 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
140142
}
141143

142144
headers.accept = "application/json"
145+
headers['accept-encoding'] = 'gzip'
143146

144147
headers["user-agent"] = this.conf.get('user-agent') ||
145148
'node/' + process.version
@@ -170,7 +173,7 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
170173
this.log.http(method, remote.href || "/")
171174

172175
var done = requestDone.call(this, method, where, cb)
173-
var req = request(opts, done)
176+
var req = request(opts, decodeResponseBody(done))
174177

175178
req.on("error", cb)
176179
req.on("socket", function (s) {
@@ -182,6 +185,20 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
182185
}
183186
}
184187

188+
function decodeResponseBody(cb) {
189+
return function (er, response, data) {
190+
if (er) return cb(er, response, data)
191+
192+
if (response.headers['content-encoding'] !== 'gzip') return cb(er, response, data)
193+
194+
zlib.gunzip(data, function (er, buf) {
195+
if (er) return cb(er, response, data)
196+
197+
cb(null, response, buf)
198+
})
199+
}
200+
}
201+
185202
// cb(er, parsed, raw, response)
186203
function requestDone (method, where, cb) {
187204
return function (er, response, data) {

test/request-gzip-content.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var zlib = require('zlib')
2+
var tap = require('tap')
3+
var server = require('./fixtures/server.js')
4+
var RC = require('../')
5+
var pkg = {
6+
_id: 'some-package-gzip@1.2.3',
7+
name: 'some-package-gzip',
8+
version: '1.2.3'
9+
}
10+
11+
zlib.gzip(JSON.stringify(pkg), function (err, pkgGzip) {
12+
var client = new RC({
13+
cache: __dirname + '/fixtures/cache'
14+
, 'fetch-retries': 1
15+
, 'fetch-retry-mintimeout': 10
16+
, 'fetch-retry-maxtimeout': 100
17+
, registry: 'http://localhost:' + server.port })
18+
19+
tap.test('request gzip package content', function (t) {
20+
server.expect('GET', '/some-package-gzip/1.2.3', function (req, res) {
21+
res.s D251 tatusCode = 200
22+
res.setHeader('Content-Encoding', 'gzip');
23+
res.setHeader('Content-Type', 'application/json');
24+
res.end(pkgGzip)
25+
})
26+
27+
client.get('/some-package-gzip/1.2.3', function (er, data, raw, res) {
28+
if (er) throw er
29+
t.deepEqual(data, pkg)
30+
t.end()
31+
})
32+
})
33+
34+
tap.test('request wrong gzip package content', function (t) {
35+
server.expect('GET', '/some-package-gzip-error/1.2.3', function (req, res) {
36+
res.statusCode = 200
37+
res.setHeader('Content-Encoding', 'gzip')
38+
res.setHeader('Content-Type', 'application/json')
39+
res.end(new Buffer('wrong gzip content'))
40+
})
41+
42+
client.get('/some-package-gzip-error/1.2.3', function (er, data, raw, res) {
43+
t.ok(er)
44+
t.end()
45+
})
46+
})
47+
});

0 commit comments

Comments
 (0)
0