对象模式


🌐 Object mode

所有由 Node.js API 创建的流仅操作字符串、<Buffer><TypedArray><DataView> 对象:

🌐 All streams created by Node.js APIs operate exclusively on strings, <Buffer>, <TypedArray> and <DataView> objects:

  • StringsBuffers 是与流一起使用的最常见类型。
  • TypedArrayDataView 让你可以使用像 Int32ArrayUint8Array 这样的类型来处理二进制数据。当你将 TypedArray 或 DataView 写入流时,Node.js 会处理原始字节。

然而,流实现也可以与其他类型的 JavaScript 值一起工作(null 除外,因为它在流中有特殊用途)。这样的流被认为是在“对象模式”下运行的。

🌐 It is possible, however, for stream implementations to work with other types of JavaScript values (with the exception of null, which serves a special purpose within streams). Such streams are considered to operate in "object mode".

流实例在创建时使用 objectMode 选项切换到对象模式。尝试将已有流切换到对象模式是不安全的。

🌐 Stream instances are switched into object mode using the objectMode option when the stream is created. Attempting to switch an existing stream into object mode is not safe.