node.js - Why crypto.createHash() is not writable until setEncoding() is called? -
i use request
package crypto
. seems request
implements legacy stream
protocol , don't write data piped destination until it's writable
.
stream.prototype.pipe = function(dest, options) { var source = this; function ondata(chunk) { if (dest.writable) { if (false === dest.write(chunk) && source.pause) { source.pause(); } } } ...
so, if use next code:
const crypto = require('crypto'); const request = require('request'); var hasher = crypto.createhash('sha256'); // uncomment line below fix! // hasher.setencoding('hex'); console.log(hasher.writable); request('http://ya.ru').pipe(hasher).on('finish', function() { console.log('hash is', hasher.read()); });
it produces sha256('')
(i.e. empty value). when use hasher.setencoding('hex')
code produces sha256(<response_body>)
, hasher.writable
gives true
.
i can't understand reason of acting way? , stated in documentation?
finally, there bug in node. here smaller code sample reproduce it:
var hasher = crypto.createhash('sha256'); const _ = hasher._writablestate; // don't have call .setenconding() here console.log(hasher.writable);
stream1 implementation required this.writable
true in destination stream.
stream.prototype.pipe = function(dest, options) { var source = this; function ondata(chunk) { if (dest.writable) { if (false === dest.write(chunk) && source.pause) { source.pause(); } } } source.on('data', ondata); ...
calling hasher.setencoding('hex')
(or other access this._writablestate
) triggered call actual constructor of stream. before this.writable
undefined
.
Comments
Post a Comment