2015-09-04 67 views
0

我有一個函數會寫/消耗一個流如此。計算一個流的sha1散列,但仍然使用流?

consume(stream, function(e,d){}); 

但我想在調用此函數之前計算流的SHA1哈希。我知道你可以像這樣得到散列:

var crypto = require('crypto'); 
var hash = crypto.createHash('sha1'); 

stream.on('data', function (data) { 
    hash.update(data, 'utf8') 
}) 

stream.on('end', function() { 
    hash.digest('hex');c 
}) 

但是每次我嘗試調用消耗函數時,流都是空的。我怎樣才能做到這一點?

回答

2

,你可以管streamsha1Calcconsume

stream.pipe(sha1Calc); 
stream.pipe(consume); 
+0

但不會在第一管消耗/空流?這是如何運作的? –

+0

不,當您管道到多個流時,它會寫入所有流。 – mscdex