├── package.json ├── index.js └── test.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "http-duplex-gzip-client", 3 | "version": "0.1.1", 4 | "description": "HTTP duplex client that accepts and decompresses gzip.", 5 | "main": "index.js", 6 | "dependencies": { 7 | "http-duplex-client": "~0.4.0" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "node test.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/mikeal/http-duplex-gzip-client" 16 | }, 17 | "keywords": [ 18 | "http", 19 | "duplex", 20 | "gzip" 21 | ], 22 | "author": "Mikeal Rogers ", 23 | "license": "BSD", 24 | "bugs": { 25 | "url": "https://github.com/mikeal/http-duplex-gzip-client/issues" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var HTTPDuplex = require('http-duplex-client') 2 | , zlib = require('zlib') 3 | , util = require('util') 4 | ; 5 | 6 | module.exports = HTTPGzipDuplex 7 | 8 | function HTTPGzipDuplex (req, options) { 9 | var self = this 10 | if (! (self instanceof HTTPGzipDuplex)) return new HTTPGzipDuplex(req, options) 11 | HTTPDuplex.apply(this, arguments) 12 | } 13 | util.inherits(HTTPGzipDuplex, HTTPDuplex) 14 | HTTPGzipDuplex.prototype.makeRequest = function (req) { 15 | var self = this 16 | if (!req.headers) req.headers = {} 17 | req.headers['accept-encoding'] = 'gzip,deflate' 18 | self.req = self.http.request(req) 19 | self.req.on('response', function (resp) { 20 | self._output = resp 21 | self.emit('response', resp) 22 | 23 | var encoding = resp.headers['content-encoding'] || 'identity' 24 | , decompress 25 | , output 26 | ; 27 | 28 | if (encoding.match(/\bdeflate\b/)) { 29 | decompress = zlib.createInflate() 30 | } else if (encoding.match(/\bgzip\b/)) { 31 | decompress = zlib.createGunzip() 32 | } 33 | 34 | if (decompress) { 35 | resp.pipe(decompress) 36 | output = decompress 37 | } else { 38 | output = resp 39 | } 40 | 41 | self._output = output 42 | 43 | output.on('data', function (c) { 44 | if (!self.push(c)) output.pause() 45 | }) 46 | output.on('end', function() { 47 | self.push(null) 48 | }) 49 | }) 50 | } 51 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var zlib = require('zlib'); 2 | var http = require('http'); 3 | var fs = require('fs'); 4 | var client = require('./') 5 | var stream = require('stream') 6 | var assert = require('assert') 7 | var util = require('util') 8 | var test = fs.readFileSync(__dirname+'/test.js').toString() 9 | var passes = 0 10 | 11 | util.inherits(TestStream, stream.PassThrough) 12 | function TestStream () { 13 | stream.PassThrough.apply(this, arguments) 14 | var str = '' 15 | var self = this 16 | self.on('data', function (chunk) { 17 | str += chunk 18 | }) 19 | self.on('end', function () { 20 | assert.equal(str, test) 21 | passes += 1 22 | if (passes === 3) { 23 | console.log('ok') 24 | process.exit() 25 | } 26 | }) 27 | } 28 | 29 | http.createServer(function(request, response) { 30 | var raw = fs.createReadStream(__dirname+'/test.js'); 31 | var acceptEncoding = request.headers['accept-encoding']; 32 | if (!acceptEncoding) { 33 | acceptEncoding = ''; 34 | } 35 | if (request.url === '/deflate') { 36 | response.writeHead(200, { 'content-encoding': 'deflate' }); 37 | raw.pipe(zlib.createDeflate()).pipe(response); 38 | } else if (request.url === '/gzip') { 39 | response.writeHead(200, { 'content-encoding': 'gzip' }); 40 | raw.pipe(zlib.createGzip()).pipe(response); 41 | } else { 42 | response.writeHead(200, {}); 43 | raw.pipe(response); 44 | } 45 | }).listen(8080, function () { 46 | 47 | ['gzip', 'deflate', ''].forEach(function (path) { 48 | var c = client({method:"GET", port:8080, path:'/'+path}) 49 | c.pipe(new TestStream()) 50 | c.on('response', function (resp) { 51 | if (path) assert.equal(resp.headers['content-encoding'], path) 52 | }) 53 | c.end() 54 | }) 55 | 56 | }) --------------------------------------------------------------------------------