如何压缩通过管道 readStream 传递的内容

发布于 2024-11-02 23:56:08 字数 1947 浏览 0 评论 0原文

我目前正在开发一个项目,该项目要求在将内容发送回浏览器之前对其进行 gZip 压缩。

我目前使用一个简单的读取流并将数据传输到请求的响应,但我不确定在不阻塞请求的情况下 gZip 内容的最佳方法

发送数据的行是:

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

请参阅以下类是静态处理程序对象:

(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();
})();

可以任何人都可以帮助我解决这个问题,我不知道如何告诉管道用 gZip 压缩。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2024-11-09 23:56:08

事实上,我有一篇关于这件事的博客文章。 http://dhruvbird.blogspot.com/2011/03/node -and-proxydecorator-pattern.html

您需要:

npm install compress -g

在使用它之前。

基本思想围绕使用管道来添加功能。

然而,对于您的用例,您最好将node.js放在nginx后面来完成所有gzip'ing,因为node.js是单个进程(实际上不是),并且gzip例程会占用您进程的CPU 。

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.

2024-11-09 23:56:08

您可以通过压缩流将其传输:

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

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

假设文件尚未压缩并且您已经设置了响应的所有标头。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文