Duplex
Duplex streams are streams that implement both the Readable
and Writable
interfaces.
Examples of Duplex
streams include:
TCP sockets
zlib streams
crypto streams
Since
v0.9.4
Inheritors
Properties
If false
then the stream will automatically end the writable side when the readable side ends. Set initially by the allowHalfOpen
constructor option, which defaults to true
.
Returns whether the stream was destroyed or errored before emitting 'end'
.
Returns whether 'data'
has been emitted.
Getter for the property encoding
of a given Readable
stream. The encoding
property can be set using the {@link setEncoding} method.
Becomes true
when 'end'
event is emitted.
This property reflects the current state of a Readable
stream as described in the Three states section.
Returns the value of highWaterMark
passed when creating this Readable
.
This property contains the number of bytes (or objects) in the queue ready to be read. The value provides introspection data regarding the status of the highWaterMark
.
Getter for the property objectMode
of a given Readable
stream.
Is true
if it is safe to call writable.write()
, which means the stream has not been destroyed, errored, or ended.
Number of times writable.uncork()
needs to be called in order to fully uncork the stream.
Is true
after writable.end()
has been called. This property does not indicate whether the data has been flushed, for this use writable.writableFinished
instead.
Is set to true
immediately before the 'finish'
event is emitted.
Return the value of highWaterMark
passed when creating this Writable
.
This property contains the number of bytes (or objects) in the queue ready to be written. The value provides introspection data regarding the status of the highWaterMark
.
Is true
if the stream's buffer has been full and stream will emit 'drain'
.
Getter for the property objectMode
of a given Writable
stream.
Functions
Event emitter The defined events on documents including:
This method returns a new stream with chunks of the underlying stream paired with a counter in the form [index, chunk]
. The first index value is 0
and it increases by 1 for each chunk produced.
Destroy the stream. Optionally emit an 'error'
event, and emit a 'close'
event (unless emitClose
is set to false
). After this call, the readable stream will release any internal resources and subsequent calls to push()
will be ignored.
This method returns a new stream with the first limit chunks dropped from the start.
Calling the writable.end()
method signals that no more data will be written to the Writable
. The optional chunk
and encoding
arguments allow one final additional chunk of data to be written immediately before closing the stream.
Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbol
s.
This method is similar to Array.prototype.every
and calls fn on each chunk in the stream to check if all awaited return values are truthy value for fn. Once an fn call on a chunk await
ed return value is falsy, the stream is destroyed and the promise is fulfilled with false
. If all of the fn calls on the chunks return a truthy value, the promise is fulfilled with true
.
This method allows filtering the stream. For each chunk in the stream the fn function will be called and if it returns a truthy value, the chunk will be passed to the result stream. If the fn function returns a promise - that promise will be await
ed.
This method is similar to Array.prototype.find
and calls fn on each chunk in the stream to find a chunk with a truthy value for fn. Once an fn call's awaited return value is truthy, the stream is destroyed and the promise is fulfilled with value for which fn returned a truthy value. If all of the fn calls on the chunks return a falsy value, the promise is fulfilled with undefined
.
This method returns a new stream by applying the given callback to each chunk of the stream and then flattening the result.
This method allows iterating a stream. For each chunk in the stream the fn function will be called. If the fn function returns a promise - that promise will be await
ed.
Returns the current max listener value for the EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to {@link defaultMaxListeners}.
The iterator created by this method gives users the option to cancel the destruction of the stream if the for await...of
loop is exited by return
, break
, or throw
, or if the iterator should destroy the stream if the stream emitted an error during iteration.
This method allows mapping over the stream. The fn function will be called for every chunk in the stream. If the fn function returns a promise - that promise will be await
ed before being passed to the result stream.
The readable.read()
method reads data out of the internal buffer and returns it. If no data is available to be read, null
is returned. By default, the data is returned as a Buffer
object unless an encoding has been specified using the readable.setEncoding()
method or the stream is operating in object mode.
This method calls fn on each chunk of the stream in order, passing it the result from the calculation on the previous element. It returns a promise for the final value of the reduction.
The writable.setDefaultEncoding()
method sets the default encoding
for a Writable
stream.
The readable.setEncoding()
method sets the character encoding for data read from the Readable
stream.
By default EventEmitter
s will print a warning if more than 10
listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners()
method allows the limit to be modified for this specific EventEmitter
instance. The value can be set to Infinity
(or 0
) to indicate an unlimited number of listeners.
This method is similar to Array.prototype.some
and calls fn on each chunk in the stream until the awaited return value is true
(or any truthy value). Once an fn call on a chunk await
ed return value is truthy, the stream is destroyed and the promise is fulfilled with true
. If none of the fn calls on the chunks return a truthy value, the promise is fulfilled with false
.
This method returns a new stream with the first limit chunks.
This method allows easily obtaining the contents of a stream.
The readable.unpipe()
method detaches a Writable
stream previously attached using the {@link pipe} method.
Passing chunk
as null
signals the end of the stream (EOF) and behaves the same as readable.push(null)
, after which no more data can be written. The EOF signal is put at the end of the buffer and any buffered data will still be flushed.
Prior to Node.js 0.10, streams did not implement the entire node:stream
module API as it is currently defined. (See Compatibility
for more information.)
The writable.write()
method writes some data to the stream, and calls the supplied callback
once the data has been fully handled. If an error occurs, the callback
will be called with the error as its first argument. The callback
is called asynchronously and before 'error'
is emitted.