stream.Duplex

Class: stream.Duplex

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

Note that stream.Duplex is an abstract class designed to be extended with an underlying implementation of the stream._read(size) and stream._write(chunk, encoding, callback) methods as you would with a Readable or Writable stream class.

Since JavaScript doesn't have multiple prototypal inheritance, this class prototypally inherits from Readable, and then parasitically from Writable. It is thus up to the user to implement both the low-level stream._read(n) method as well as the low-level stream._write(chunk, encoding, callback) method on extension duplex classes.

new stream.Duplex(options)

  • options <Object> Passed to both Writable and Readable constructors. Also has the following fields:
    • allowHalfOpen <Boolean> Default = true. If set to false, then the stream will automatically end the readable side when the writable side ends and vice versa.
    • readableObjectMode <Boolean> Default = false. Sets objectMode for readable side of the stream. Has no effect if objectMode is true.
    • writableObjectMode <Boolean> Default = false. Sets objectMode for writable side of the stream. Has no effect if objectMode is true.

In classes that extend the Duplex class, make sure to call the constructor so that the buffering settings can be properly initialized.

doc_Nodejs
2016-04-30 04:42:24
Comments
Leave a Comment

Please login to continue.