实现双工流


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

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

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

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

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

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).

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.

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