Name: js-handler/node_modules/restify/lib/plugins/gzip.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
 
3:
var zlib = require('zlib');
4:
 
5:
var assert = require('assert-plus');
6:
 
7:
 
8:
function _writeHead(originalFunction) {
9:
        this.removeHeader('Content-Length');
10:
        originalFunction.apply(this, Array.prototype.slice.call(arguments, 1));
11:
}
12:
 
13:
///--- API
14:
 
15:
function gzipResponse(opts) {
16:
        assert.optionalObject(opts, 'options');
17:
 
18:
        function gzip(req, res, next) {
19:
                if (!req.acceptsEncoding('gzip')) {
20:
                        next();
21:
                        return;
22:
                }
23:
 
24:
                var gz = zlib.createGzip(opts);
25:
 
26:
                gz.on('data', res.write.bind(res));
27:
                gz.once('end', res.end.bind(res));
28:
                gz.on('drain', res.emit.bind(res, 'drain'));
29:
 
30:
                var origWrite = res.write;
31:
                var origEnd = res.end;
32:
                var origWriteHead = res.writeHead;
33:
                res.handledGzip = function _handledGzip() {
34:
                        res.write = origWrite;
35:
                        res.end = origEnd;
36:
                        res.writeHead = origWriteHead;
37:
                };
38:
 
39:
                res.write = gz.write.bind(gz);
40:
                res.end = gz.end.bind(gz);
41:
 
42:
                res.writeHead = _writeHead.bind(res, res.writeHead);
43:
                res.setHeader('Content-Encoding', 'gzip');
44:
                next();
45:
        }
46:
 
47:
        return (gzip);
48:
}
49:
 
50:
 
51:
 
52:
///--- Exports
53:
 
54:
module.exports = gzipResponse;