Sha256: a5a57cc3b25782f261b858d993aefb514b5b51b6ba99048b183b9e2009fd306a

Contents?: true

Size: 2 KB

Versions: 26

Compression:

Stored size: 2 KB

Contents

/*!
 * Ext JS Connect
 * Copyright(c) 2010 Sencha Inc.
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var child_process = require('child_process'),
    sys = require('sys'),
    compress = require('compress');

/**
 * Provides gzip compression via the node-compress library.
 *
 * @return {Function}
 * @api public
 */

module.exports = function gzip(){
    return function gzip(req, res, next) {
        var writeHead = res.writeHead,
            write = res.write,
            end = res.end;

        res.writeHead = function (code, headers) {
            var type = headers["Content-Type"],
                accept = req.headers["accept-encoding"];

            if (!(code === 200 && accept && accept.indexOf('gzip') >= 0
                  && type && (/(text|javascript|json)/).test(type)
                  && headers["Content-Encoding"] === undefined)) {
                res.writeHead = writeHead;
                res.writeHead(code, headers);
                return;
            }

            headers["Content-Encoding"] = "gzip";
            delete headers["Content-Length"];

            var gzip = new compress.GzipStream();

            res.write = function (chunk, encoding) {
                gzip.setInputEncoding(encoding);
                gzip.write(chunk);
            };

            res.end = function (chunk, encoding) {
                if (chunk) {
                    res.write(chunk, encoding);
                }
                gzip.close();
            };

            gzip.addListener('data', function (chunk) {
                write.call(res, chunk);
            });
            gzip.addListener('error', function(err) {
                res.write = write;
                res.end = end;
                next(err);
            });
            gzip.addListener('end', function (code) {
                res.write = write;
                res.end = end;
                res.end();
            });

            res.writeHead = writeHead;
            res.writeHead(code, headers);

        };

        next();
    };
};

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
vulcan-0.1.3 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js
vulcan-0.1.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js
vulcan-0.1.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js
vulcan-0.1.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js
vulcan-0.0.3 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js
vulcan-0.0.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-compress.js