如何在Node.js中一次将一个可读流管道化为两个可写流



目标是:

  1. 创建文件读流
  2. 管道到gzip (zlib.createGzip())
  3. 然后将zlib输出的读流管道到:

    1) HTTP response对象

    2) 可写文件流保存压缩后的输出

现在我可以减少到3.1:

var gzip = zlib.createGzip(),
    sourceFileStream = fs.createReadStream(sourceFilePath),
    targetFileStream = fs.createWriteStream(targetFilePath);
response.setHeader('Content-Encoding', 'gzip');
sourceFileStream.pipe(gzip).pipe(response);

…这工作得很好,但我也需要保存gzip压缩的数据到一个文件,这样我就不需要每次重新压缩,并能够直接流gzip压缩的数据作为响应。

那么我如何在Node中一次将一个可读流管道到两个可写流?

sourceFileStream.pipe(gzip).pipe(response).pipe(targetFileStream);在Node 0.8.x中工作吗?

管道链接/分割不像你在这里试图做的那样,发送第一个到两个不同的后续步骤:

sourceFileStream.pipe(gzip).pipe(response);

但是,您可以将相同的可读流管道化为两个可写流,例如:

var fs = require('fs');
var source = fs.createReadStream('source.txt');
var dest1 = fs.createWriteStream('dest1.txt');
var dest2 = fs.createWriteStream('dest2.txt');
source.pipe(dest1);
source.pipe(dest2);

我发现zlib返回一个可读流,稍后可以将其管道传输到多个其他流中。所以我做了以下的事情来解决上面的问题:

var sourceFileStream = fs.createReadStream(sourceFile);
// Even though we could chain like
// sourceFileStream.pipe(zlib.createGzip()).pipe(response);
// we need a stream with a gzipped data to pipe to two
// other streams.
var gzip = sourceFileStream.pipe(zlib.createGzip());
// This will pipe the gzipped data to response object
// and automatically close the response object.
gzip.pipe(response);
// Then I can pipe the gzipped data to a file.
gzip.pipe(fs.createWriteStream(targetFilePath));

你可以使用"readable-stream-clone"包

const fs = require("fs");
const ReadableStreamClone = require("readable-stream-clone");
const readStream = fs.createReadStream('text.txt');
const readStream1 = new ReadableStreamClone(readStream);
const readStream2 = new ReadableStreamClone(readStream);
const writeStream1 = fs.createWriteStream('sample1.txt');
const writeStream2 = fs.createWriteStream('sample2.txt');
readStream1.pipe(writeStream1)
readStream2.pipe(writeStream2)

相关内容

  • 没有找到相关文章

最新更新