实现双工流


¥Implementing a duplex stream

Duplex 流是同时实现 ReadableWritable 的流,例如 TCP 套接字连接。

¥A Duplex stream is one that implements both Readable and Writable, such as a TCP socket connection.

因为 JavaScript 不支持多重继承,所以扩展 stream.Duplex 类以实现 Duplex 流(与扩展 stream.Readablestream.Writable 类相反)。

¥Because JavaScript does not have support for multiple inheritance, the stream.Duplex class is extended to implement a Duplex stream (as opposed to extending the stream.Readable and stream.Writable classes).

stream.Duplex 类原型继承自 stream.Readable 并寄生于 stream.Writable,但由于在 stream.Writable 上覆盖了 Symbol.hasInstanceinstanceof 将适用于两个基类。

¥The stream.Duplex class prototypically inherits from stream.Readable and parasitically from stream.Writable, but instanceof will work properly for both base classes due to overriding Symbol.hasInstance on stream.Writable.

自定义 Duplex 流必须调用 new stream.Duplex([options]) 构造函数并实现 readable._read()writable._write() 方法。

¥Custom Duplex streams must call the new stream.Duplex([options]) constructor and implement both the readable._read() and writable._write() methods.