Sha256: 0e0d6fc65a8baa23967a18d7cd1c4e5eae845a4befc3ce0a5ebcd9e05157d978

Contents?: true

Size: 1.79 KB

Versions: 26

Compression:

Stored size: 1.79 KB

Contents

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

/**
 * Module dependencies.
 */

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

/**
 * Provides gzip compression via the `gzip` executable.
 *
 * @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 = child_process.spawn("gzip", ["-9"]);

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

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

            gzip.stdout.addListener('data', function (chunk) {
                write.call(res, chunk);
            });

            gzip.addListener("exit", 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.8.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.8.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.8.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.7.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.7.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.7.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.6.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.6.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.5.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.4.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.3.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.5 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.4 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.3 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.2.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.1.6 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.1.5 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js
vulcan-0.1.4 server/node_modules/connect-form/support/connect/lib/connect/middleware/gzip-proc.js