实现双工流


双工流同时实现了可读流可写流,例如 TCP socket 连接。 因为 JavaScript 不支持多重继承,所以使用 stream.Duplex 类实现双工流(而不是使用 stream.Readable 类和 stream.Writable 类)。

stream.Duplex 类的原型继承自 stream.Readable 和寄生自 stream.Writable,但是 instanceof 对这两个基础类都可用,因为重写了 stream.WritableSymbol.hasInstance

自定义的双工流必须调用 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.