RobertPitt
RobertPitt

Reputation: 57268

How to gZip content that's passed through a piped readStream

Im currently working on a project that requires the content to be gZip-ed before it's sent back to the browser.

Im currently using a simple read stream and piping the data to the response of a request, but im not sure the best way to gZip content without blocking requests

The line that send the data is:

require('fs').createReadStream(self.staticPath + Request.url).pipe(Response);

See the following class is the static handler object:

(function(){

    var StaticFeeder = function()
    {
        this.staticPath = process.cwd() + '/application/static';
        this.contentTypes = require('./contenttypes')
    }

    StaticFeeder.prototype.handle = function(Request,Response,callback)
    {
        var self = this;
        if(Request.url == '/')
        {
            return false;
        }

        if(Request.url.indexOf('../') > -1)
        {
            return false;
        }

        require('path').exists(this.staticPath + Request.url,function(isthere){

            /*
             * If no file exists, pass back to the main handler and return
             * */
            if(isthere === false)
            {
                callback(false);
                return;
            }

            /*
             * Get the extention if possible
             * */
            var ext = require('path').extname(Request.url).replace('.','')

            /*
             * Get the Content-Type
             * */
            var ctype = self.contentTypes[ext] !== undefined ? self.contentTypes[ext] : 'application/octet-stream';

            /*
             * Send the Content-Type
             * */
            Response.setHeader('Content-Type',ctype);

            /*
             * Create a readable stream and send the file
             * */
            require('fs').createReadStream(self.staticPath + Request.url).pipe(Response);

            /*
             * Tell the main handler we have delt with the response
             * */
            callback(true);
        })
    }

    module.exports = new StaticFeeder();
})();

Can anyone help me get around this problem, i haven't a clue on how to tell the piping to compress with gZip.

Thanks

Upvotes: 1

Views: 2186

Answers (2)

Jonathan Ong
Jonathan Ong

Reputation: 20315

You can just pipe it through a compression stream:

var fs = require('fs')
var zlib = require('zlib')

fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(Response)

assumes the file is not compressed already and you have already set all the headers for the response.

Upvotes: 1

dhruvbird
dhruvbird

Reputation: 6189

Actually, I have a blog post on just this thing. http://dhruvbird.blogspot.com/2011/03/node-and-proxydecorator-pattern.html

You will need to:

npm install compress -g

Before using it though.

The basic idea revolves around using pipes to add functionality.

However, for your use-case, you would be better off putting node.js behind nginx to do all the gzip'ing since node.js is a single process (actually not), and the gzip routines would take up your process' CPU.

Upvotes: 1

Related Questions