-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (43 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var HTTPDuplex = require('http-duplex-client')
, zlib = require('zlib')
, util = require('util')
;
module.exports = HTTPGzipDuplex
function HTTPGzipDuplex (req, options) {
var self = this
if (! (self instanceof HTTPGzipDuplex)) return new HTTPGzipDuplex(req, options)
HTTPDuplex.apply(this, arguments)
}
util.inherits(HTTPGzipDuplex, HTTPDuplex)
HTTPGzipDuplex.prototype.makeRequest = function (req) {
var self = this
if (!req.headers) req.headers = {}
req.headers['accept-encoding'] = 'gzip,deflate'
self.req = self.http.request(req)
self.req.on('response', function (resp) {
self._output = resp
self.emit('response', resp)
var encoding = resp.headers['content-encoding'] || 'identity'
, decompress
, output
;
if (encoding.match(/\bdeflate\b/)) {
decompress = zlib.createInflate()
} else if (encoding.match(/\bgzip\b/)) {
decompress = zlib.createGunzip()
}
if (decompress) {
resp.pipe(decompress)
output = decompress
} else {
output = resp
}
self._output = output
output.on('data', function (c) {
if (!self.push(c)) output.pause()
})
output.on('end', function() {
self.push(null)
})
})
}