javascript - How to create a temporary write stream? - Stack Overflow

I have a file stream that I want to pass in a method named transformRead(), this one accepts a readStre

I have a file stream that I want to pass in a method named transformRead(), this one accepts a readStream and a writeStream, but I don't know how to create the temporary write stream... do I have to use a file ? I just want a kind of pipe() from rs to ws, then ws is gzipped and sent to response.

            // Get file stream
            var rs = store.getReadStream(fileId);
            var ws = ?????; 

            // Execute transformation
            store.transformRead(rs, ws, fileId);

            var accept = req.headers['accept-encoding'] || '';

            // Compress data if supported by the client
            if (accept.match(/\bdeflate\b/)) {
                res.writeHead(200, {
                    'Content-Encoding': 'deflate',
                    'Content-Type': file.type
                });
                ws.pipe(zlib.createDeflate()).pipe(res);

            } else if (accept.match(/\bgzip\b/)) {
                res.writeHead(200, {
                    'Content-Encoding': 'gzip',
                    'Content-Type': file.type
                });
                ws.pipe(zlib.createGzip()).pipe(res);

            } else {
                res.writeHead(200, {});
                ws.pipe(res);
            }

I have a file stream that I want to pass in a method named transformRead(), this one accepts a readStream and a writeStream, but I don't know how to create the temporary write stream... do I have to use a file ? I just want a kind of pipe() from rs to ws, then ws is gzipped and sent to response.

            // Get file stream
            var rs = store.getReadStream(fileId);
            var ws = ?????; 

            // Execute transformation
            store.transformRead(rs, ws, fileId);

            var accept = req.headers['accept-encoding'] || '';

            // Compress data if supported by the client
            if (accept.match(/\bdeflate\b/)) {
                res.writeHead(200, {
                    'Content-Encoding': 'deflate',
                    'Content-Type': file.type
                });
                ws.pipe(zlib.createDeflate()).pipe(res);

            } else if (accept.match(/\bgzip\b/)) {
                res.writeHead(200, {
                    'Content-Encoding': 'gzip',
                    'Content-Type': file.type
                });
                ws.pipe(zlib.createGzip()).pipe(res);

            } else {
                res.writeHead(200, {});
                ws.pipe(res);
            }
Share Improve this question asked Aug 1, 2015 at 7:05 Karl.SKarl.S 2,4021 gold badge29 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Finally, someone told me about using stream.PassThrough();

So the simplest and "native" solution is :

var ws = new stream.PassThrough();

Use through2 to easily create transform (read/write) stream

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744133461a4559944.html

相关推荐

  • javascript - How to create a temporary write stream? - Stack Overflow

    I have a file stream that I want to pass in a method named transformRead(), this one accepts a readStre

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信