Node.jsstream对象模式

我试图理解对象stream的概念,尤其是两者的结合。 我正在寻找的用法是pipe道字节stream连同对象stream如:

// StringifyStream reads Buffers and emits String Objects // Mapper is really just a classical map // BytifyStream reads String Objects and emits buffers. process.stdin.pipe( StringifyStream() ).pipe( Mapper(function(s) { return s.toUpperCase(); }).pipe( BytifyStream() ).pipe(process.stdout); // This code should read from stdin, convert all incoming buffers to strings, // map those strings to upper case and finally convert them back to buffers // and write them to stdout. 

现在,文档说:

“设置objectMode中stream是不安全的。”

我真的不明白这是什么意思。 混合字节/对象stream不安全? 我真的想使用这种模式,但如果它不安全,这可能是一个坏主意。

对象stream是那些发出Buffer或String以外的数据types的stream。

处于对象模式的stream可以发出缓冲区和string以外的通用JavaScript值。

你的例子是安全的,它只是做转换缓冲区 – >string – >上部string – >缓冲区。

这只是我的意见,但你可以简化链,只使用一个转换stream。

 var util = require ("util"); var stream = require ("stream"); var UpperStream = function (){ stream.Transform.call (this); }; util.inherits (UpperStream, stream.Transform); UpperStream.prototype._transform = function (chunk, encoding, cb){ this.push ((chunk + "").toUpperCase ()); cb (); }; process.stdin.pipe (new UpperStream ()).pipe (process.stdout);