对象模式
¥Object mode
Node.js API 创建的所有流都专门对字符串、<Buffer>、<TypedArray> 和 <DataView> 对象进行操作:
¥All streams created by Node.js APIs operate exclusively on strings, <Buffer>, <TypedArray> and <DataView> objects:
-
Strings
和Buffers
是流中最常用的类型。¥
Strings
andBuffers
are the most common types used with streams. -
TypedArray
和DataView
允许你处理Int32Array
或Uint8Array
等类型的二进制数据。当你将 TypedArray 或 DataView 写入流时,Node.js 会处理原始字节。¥
TypedArray
andDataView
lets you handle binary data with types likeInt32Array
orUint8Array
. When you write a TypedArray or DataView to a stream, Node.js processes the raw bytes.
但是,流的实现可以使用其他类型的 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.