[英]Node.js dynamically extend piped stream
I want to benchmark node.js systems. 我想对node.js系统进行基准测试。
Therefore I created a little programm which can encrypt and compress files (i want the app to be CPU intense). 因此,我创建了一个小程序,可以加密和压缩文件(我希望该应用程序占用CPU资源)。 I thought it would make sense to repeate the encryption process multiple times.
我认为重复多次加密过程是有意义的。 But whenever I dynamically pipe the stream, the program exits before finishing.
但是,每当我动态传输流时,程序都会在完成之前退出。 Is there a way how I can wait till the stream finished?
有没有办法等到流完成?
My code looks like this: 我的代码如下所示:
var readStream = fstream.Reader(sourcePath);
var writeStream = fs.createWriteStream(sourcePath + '.tar.gz');
var stream = readStream.on('error', function (e) {
handleError(e, errorMessageReadFile);
})
.pipe(tar.Pack()).on('error', function (e) {
handleError(e, errorMessageTarPack);
});
for (i = 0; i < algorithmCount; i++) {
stream = stream.pipe(encryptStream).on('error', function (e) {
handleError(e, errorMessageEncrypt);
});
console.log("enc");
}
stream.pipe(gzip).on('error', function (e) {
handleError(e, errorMessageCompress);
})
.pipe(writeStream).on('error', function (e) {
handleError(e, errorMessageWriteFile);
})
.on('finish', function () {
console.log('done');
});
This is likely a problem: 这可能是一个问题:
for (i = 0; i < algorithmCount; i++) {
stream = stream.pipe(encryptStream);
}
Remember that .pipe
returns the stream you're piping into . 请记住,
.pipe
返回您正在输送到的流 。 In other words: stream.pipe(encryptStream)
returns encryptStream
. 换句话说:
stream.pipe(encryptStream)
返回encryptStream
。 The first iteration is fine: stream
is initially the tar.Pack()
stream, and so that gets piped into encryptStream
. 第一次迭代就很好了:
stream
最初是tar.Pack()
流,因此可以通过管道encryptStream
到encryptStream
。 However, the second iteration will pipe stream
which now equals encryptStream
into itself . 然而,第二迭代将管
stream
现在等于encryptStream
到自身 。
You probably want to create a new intermediate encrypt stream for every iteration, eg something like: 您可能想为每次迭代创建一个新的中间加密流,例如:
for (i = 0; i < algorithmCount; i++) {
stream = stream.pipe(createEncryptStream());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.