« index
Coverage for /Users/yunong/workspace/node-restify/lib/plugins/gzip.js : 90%
53 lines |
48 run |
5 missing |
0 partial |
5 blocks |
3 blocks run |
2 blocks missing
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 51 52 53 | // Copyright 2012 Mark Cavage, Inc. All rights reserved. var zlib = require('zlib'); var assert = require('assert-plus'); function _writeHead(originalFunction) { this.removeHeader('Content-Length'); originalFunction.apply(this, Array.prototype.slice.call(arguments, 1)); } ///--- API function gzipResponse(opts) { assert.optionalObject(opts, 'options'); function gzip(req, res, next) { if (!req.acceptsEncoding('gzip')) { next(); return; } var gz = zlib.createGzip(opts); gz.on('data', res.write.bind(res)); gz.once('end', res.end.bind(res)); gz.on('drain', res.emit.bind(res, 'drain')); var origWrite = res.write; var origEnd = res.end; var origWriteHead = res.writeHead; res.handledGzip = function _handledGzip() { res.write = origWrite; res.end = origEnd; res.writeHead = origWriteHead; }; res.write = gz.write.bind(gz); res.end = gz.end.bind(gz); res.writeHead = _writeHead.bind(res, res.writeHead); res.setHeader('Content-Encoding', 'gzip'); next(); } return (gzip); } ///--- Exports module.exports = gzipResponse; |