为什么 crypto.createHash() 在调用 setEncoding() 之前不可写?

Why crypto.createHash() is not writable until setEncoding() is called?

我使用 request package with crypto. It seems that request implements legacy stream protocol and don't write 任何数据到管道目的地,直到它是 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();
      }
    }
  }
...

所以,如果我使用下一个代码:

const crypto = require('crypto'); 
const request = require('request');

var hasher = crypto.createHash('sha256'); 
// Uncomment the line below to fix!
// hasher.setEncoding('hex');
console.log(hasher.writable); 
request('http://ya.ru').pipe(hasher).on('finish', function() {
    console.log('Hash is', hasher.read()); 
});

它产生 sha256('') (即从空值)。但是当我使用 hasher.setEncoding('hex') 代码生成 sha256(<response_body>) 并且 hasher.writable 给出 true.

我不明白这样做的原因是什么?文档中的说明在哪里?

最后,Node.js 中出现了 bug。这是重现它的更小的代码示例:

var hasher = crypto.createHash('sha256'); 
const _ = hasher._writableState;  // we don't even have to call .setEnconding() here
console.log(hasher.writable);

Stream1 实现 required this.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();
      }
    }
  }

  source.on('data', ondata);
  ...

调用 hasher.setEncoding('hex')(或任何其他对 this._writableState 的访问)触发了对流的实际构造函数的调用。在它之前 this.writableundefined.