Karl.S
Karl.S

Reputation: 2402

How to create a temporary write stream?

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

Upvotes: 8

Views: 2589

Answers (2)

Karl.S
Karl.S

Reputation: 2402

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

So the simplest and "native" solution is :

var ws = new stream.PassThrough();

Upvotes: 5

krl
krl

Reputation: 5296

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

Upvotes: 3

Related Questions