Streams can be readable, writable, or both. All streams are instances of
EventEmitter.

The stream module can be accessed using:

const stream = require('stream');

While it is important for all Node.js users to understand how streams work,
the stream module itself is most useful for developers that are creating new
types of stream instances. Developers who are primarily consuming stream
objects will rarely (if ever) have need to use the stream module directly.

This document is divided into two primary sections with a third section for
additional notes. The first section explains the elements of the stream API that
are required to use streams within an application. The second section explains
the elements of the API that are required to implement new types of streams.

All streams created by Node.js APIs operate exclusively on strings and Buffer
(or Uint8Array) objects. It is possible, however, for stream implementations
to work with other types of JavaScript values (with the exception of null,
which serves a special purpose within streams). Such streams are considered to
operate in "object mode".

Stream instances are switched into object mode using the objectMode option
when the stream is created. Attempting to switch an existing stream into
object mode is not safe.

Both Writable and Readable streams will store data in an internal
buffer that can be retrieved using writable._writableState.getBuffer() or
readable._readableState.buffer, respectively.

The amount of data potentially buffered depends on the highWaterMark option
passed into the streams constructor. For normal streams, the highWaterMark
option specifies a total number of bytes. For streams operating
in object mode, the highWaterMark specifies a total number of objects.

Data is buffered in Readable streams when the implementation calls
stream.push(chunk). If the consumer of the Stream does not
call stream.read(), the data will sit in the internal
queue until it is consumed.

Once the total size of the internal read buffer reaches the threshold specified
by highWaterMark, the stream will temporarily stop reading data from the
underlying resource until the data currently buffered can be consumed (that is,
the stream will stop calling the internal readable._read() method that is
used to fill the read buffer).

Data is buffered in Writable streams when the
writable.write(chunk) method is called repeatedly. While the
total size of the internal write buffer is below the threshold set by
highWaterMark, calls to writable.write() will return true. Once
the size of the internal buffer reaches or exceeds the highWaterMark, false
will be returned.

A key goal of the stream API, particularly the stream.pipe() method,
is to limit the buffering of data to acceptable levels such that sources and
destinations of differing speeds will not overwhelm the available memory.

Because Duplex and Transform streams are both Readable and Writable,
each maintain two separate internal buffers used for reading and writing,
allowing each side to operate independently of the other while maintaining an
appropriate and efficient flow of data. For example, net.Socket instances
are Duplex streams whose Readable side allows consumption of data received
from the socket and whose Writable side allows writing data to the socket.
Because data may be written to the socket at a faster or slower rate than data
is received, it is important for each side to operate (and buffer) independently
of the other.

The 'close' event is emitted when the stream and any of its underlying
resources (a file descriptor, for example) have been closed. The event indicates
that no more events will be emitted, and no further computation will occur.

The writable.cork() method forces all written data to be buffered in memory.
The buffered data will be flushed when either the stream.uncork() or
stream.end() methods are called.

The primary intent of writable.cork() is to avoid a situation where writing
many small chunks of data to a stream do not cause a backup in the internal
buffer that would have an adverse impact on performance. In such situations,
implementations that implement the writable._writev() method can perform
buffered writes in a more optimized manner.

chunk<string> | <Buffer> | <Uint8Array> | <any> Optional data to write. For streams
not operating in object mode, chunk must be a string, Buffer or
Uint8Array. For object mode streams, chunk may be any JavaScript value
other than null.

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. If provided, the optional callback function is attached as a listener
for the 'finish' event.

The writable.uncork() method flushes all data buffered since
stream.cork() was called.

When using writable.cork() and writable.uncork() to manage the buffering
of writes to a stream, it is recommended that calls to writable.uncork() be
deferred using process.nextTick(). Doing so allows batching of all
writable.write() calls that occur within a given Node.js event loop phase.

If the writable.cork() method is called multiple times on a stream, the same
number of calls to writable.uncork() must be called to flush the buffered
data.

stream.cork();
stream.write('some ');
stream.cork();
stream.write('data ');
process.nextTick(() => {
stream.uncork();
// The data will not be flushed until uncork() is called a second time.
stream.uncork();
});

Passing null as the chunk parameter will always be considered invalid now, even in object mode.

v0.9.4

Added in: v0.9.4

chunk<string> | <Buffer> | <Uint8Array> | <any> Optional data to write. For streams
not operating in object mode, chunk must be a string, Buffer or
Uint8Array. For object mode streams, chunk may be any JavaScript value
other than null.

Returns: <boolean>false if the stream wishes for the calling code to
wait for the 'drain' event to be emitted before continuing to write
additional data; otherwise true.

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 callbackmay or may not be called with the error as its
first argument. To reliably detect write errors, add a listener for the
'error' event.

The return value is true if the internal buffer is less than the
highWaterMark configured when the stream was created after admitting chunk.
If false is returned, further attempts to write data to the stream should
stop until the 'drain' event is emitted.

While a stream is not draining, calls to write() will buffer chunk, and
return false. Once all currently buffered chunks are drained (accepted for
delivery by the operating system), the 'drain' event will be emitted.
It is recommended that once write() returns false, no more chunks be written
until the 'drain' event is emitted. While calling write() on a stream that
is not draining is allowed, Node.js will buffer all written chunks until
maximum memory usage occurs, at which point it will abort unconditionally.
Even before it aborts, high memory usage will cause poor garbage collector
performance and high RSS (which is not typically released back to the system,
even after the memory is no longer required). Since TCP sockets may never
drain if the remote peer does not read the data, writing a socket that is
not draining may lead to a remotely exploitable vulnerability.

Writing data while the stream is not draining is particularly
problematic for a Transform, because the Transform streams are paused
by default until they are piped or an 'data' or 'readable' event handler
is added.

If the data to be written can be generated or fetched on demand, it is
recommended to encapsulate the logic into a Readable and use
stream.pipe(). However, if calling write() is preferred, it is
possible to respect backpressure and avoid memory issues using the
'drain' event:

The Readable can switch back to paused mode using one of the following:

If there are no pipe destinations, by calling the
stream.pause() method.

If there are pipe destinations, by removing any 'data' event
handlers, and removing all pipe destinations by calling the
stream.unpipe() method.

The important concept to remember is that a Readable will not generate data
until a mechanism for either consuming or ignoring that data is provided. If
the consuming mechanism is disabled or taken away, the Readable will attempt
to stop generating the data.

Note: For backwards compatibility reasons, removing 'data' event
handlers will not automatically pause the stream. Also, if there are piped
destinations, then calling stream.pause() will not guarantee
that the stream will remain paused once those destinations drain and ask for
more data.

Note: If a Readable is switched into flowing mode and there are no
consumers available to handle the data, that data will be lost. This can occur,
for instance, when the readable.resume() method is called without a listener
attached to the 'data' event, or when a 'data' event handler is removed
from the stream.

The "two modes" of operation for a Readable stream are a simplified abstraction
for the more complicated internal state management that is happening within the
Readable stream implementation.

Specifically, at any given point in time, every Readable is in one of three
possible states:

readable._readableState.flowing = null

readable._readableState.flowing = false

readable._readableState.flowing = true

When readable._readableState.flowing is null, no mechanism for consuming the
streams data is provided so the stream will not generate its data. While in this
state, attaching a listener for the 'data' event, calling the readable.pipe()
method, or calling the readable.resume() method will switch
readable._readableState.flowing to true, causing the Readable to begin
actively emitting events as data is generated.

Calling readable.pause(), readable.unpipe(), or receiving "back pressure"
will cause the readable._readableState.flowing to be set as false,
temporarily halting the flowing of events but not halting the generation of
data. While in this state, attaching a listener for the 'data' event
would not cause readable._readableState.flowing to switch to true.

The Readable stream API evolved across multiple Node.js versions and provides
multiple methods of consuming stream data. In general, developers should choose
one of the methods of consuming data and should never use multiple methods
to consume data from a single stream.

Use of the readable.pipe() method is recommended for most users as it has been
implemented to provide the easiest way of consuming stream data. Developers that
require more fine-grained control over the transfer and generation of data can
use the EventEmitter and readable.pause()/readable.resume() APIs.

The 'close' event is emitted when the stream and any of its underlying
resources (a file descriptor, for example) have been closed. The event indicates
that no more events will be emitted, and no further computation will occur.

chunk<Buffer> | <string> | <any> The chunk of data. For streams that are not
operating in object mode, the chunk will be either a string or Buffer.
For streams that are in object mode, the chunk can be any JavaScript value
other than null.

The 'data' event is emitted whenever the stream is relinquishing ownership of
a chunk of data to a consumer. This may occur whenever the stream is switched
in flowing mode by calling readable.pipe(), readable.resume(), or by
attaching a listener callback to the 'data' event. The 'data' event will
also be emitted whenever the readable.read() method is called and a chunk of
data is available to be returned.

Attaching a 'data' event listener to a stream that has not been explicitly
paused will switch the stream into flowing mode. Data will then be passed as
soon as it is available.

The listener callback will be passed the chunk of data as a string if a default
encoding has been specified for the stream using the
readable.setEncoding() method; otherwise the data will be passed as a
Buffer.

The 'end' event is emitted when there is no more data to be consumed from
the stream.

Note: The 'end' event will not be emitted unless the data is
completely consumed. This can be accomplished by switching the stream into
flowing mode, or by calling stream.read() repeatedly until
all data has been consumed.

The 'error' event may be emitted by a Readable implementation at any time.
Typically, this may occur if the underlying stream is unable to generate data
due to an underlying internal failure, or when a stream implementation attempts
to push an invalid chunk of data.

The 'readable' event is emitted when there is data available to be read from
the stream. In some cases, attaching a listener for the 'readable' event will
cause some amount of data to be read into an internal buffer.

The 'readable' event will also be emitted once the end of the stream data
has been reached but before the 'end' event is emitted.

Effectively, the 'readable' event indicates that the stream has new
information: either new data is available or the end of the stream has been
reached. In the former case, stream.read() will return the
available data. In the latter case, stream.read() will return
null. For instance, in the following example, foo.txt is an empty file:

The readable.isPaused() method returns the current operating state of the
Readable. This is used primarily by the mechanism that underlies the
readable.pipe() method. In most typical cases, there will be no reason to
use this method directly.

The readable.pipe() method attaches a Writable stream to the readable,
causing it to switch automatically into flowing mode and push all of its data
to the attached Writable. The flow of data will be automatically managed so
that the destination Writable stream is not overwhelmed by a faster Readable
stream.

The following example pipes all of the data from the readable into a file
named file.txt:

By default, stream.end() is called on the destination Writable
stream when the source Readable stream emits 'end', so that the
destination is no longer writable. To disable this default behavior, the end
option can be passed as false, causing the destination stream to remain open,
as illustrated in the following example:

One important caveat is that if the Readable stream emits an error during
processing, the Writable destination is not closed automatically. If an
error occurs, it will be necessary to manually close each stream in order
to prevent memory leaks.

Note: The process.stderr and process.stdout Writable streams are
never closed until the Node.js process exits, regardless of the specified
options.

The readable.read() method pulls some data out of the internal buffer and
returns it. If no data available to be read, null is returned. By default,
the data will be returned as a Buffer object unless an encoding has been
specified using the readable.setEncoding() method or the stream is operating
in object mode.

The optional size argument specifies a specific number of bytes to read. If
size bytes are not available to be read, null will be returned unless
the stream has ended, in which case all of the data remaining in the internal
buffer will be returned.

If the size argument is not specified, all of the data contained in the
internal buffer will be returned.

The readable.read() method should only be called on Readable streams operating
in paused mode. In flowing mode, readable.read() is called automatically until
the internal buffer is fully drained.

The readable.setEncoding() method sets the character encoding for
data read from the Readable stream.

By default, no encoding is assigned and stream data will be returned as
Buffer objects. Setting an encoding causes the stream data
to be returned as strings of the specified encoding rather than as Buffer
objects. For instance, calling readable.setEncoding('utf8') will cause the
output data to be interpreted as UTF-8 data, and passed as strings. Calling
readable.setEncoding('hex') will cause the data to be encoded in hexadecimal
string format.

The Readable stream will properly handle multi-byte characters delivered through
the stream that would otherwise become improperly decoded if simply pulled from
the stream as Buffer objects.

chunk<Buffer> | <Uint8Array> | <string> | <any> Chunk of data to unshift onto the
read queue. For streams not operating in object mode, chunk must be a
string, Buffer or Uint8Array. For object mode streams, chunk may be
any JavaScript value other than null.

The readable.unshift() method pushes a chunk of data back into the internal
buffer. This is useful in certain situations where a stream is being consumed by
code that needs to "un-consume" some amount of data that it has optimistically
pulled out of the source, so that the data can be passed on to some other party.

Note: The stream.unshift(chunk) method cannot be called after the
'end' event has been emitted or a runtime error will be thrown.

Note: Unlike stream.push(chunk), stream.unshift(chunk)
will not end the reading process by resetting the internal reading state of the
stream. This can cause unexpected results if readable.unshift() is called
during a read (i.e. from within a stream._read()
implementation on a custom stream). Following the call to readable.unshift()
with an immediate stream.push('') will reset the reading state
appropriately, however it is best to simply avoid calling readable.unshift()
while in the process of performing a read.

Versions of Node.js prior to v0.10 had streams that did not implement the
entire stream module API as it is currently defined. (See Compatibility
for more information.)

When using an older Node.js library that emits 'data' events and has a
stream.pause() method that is advisory only, the
readable.wrap() method can be used to create a Readable stream that uses
the old stream as its data source.

It will rarely be necessary to use readable.wrap() but the method has been
provided as a convenience for interacting with older Node.js applications and
libraries.

Destroy the stream, and emit 'error'. After this call, the
transform stream would release any internal resources.
implementors should not override this method, but instead implement
readable._destroy.
The default implementation of _destroy for Transform also emit 'close'.

The stream module API has been designed to make it possible to easily
implement streams using JavaScript's prototypal inheritance model.

First, a stream developer would declare a new JavaScript class that extends one
of the four basic stream classes (stream.Writable, stream.Readable,
stream.Duplex, or stream.Transform), making sure they call the appropriate
parent class constructor:

Note: The implementation code for a stream should never call the "public"
methods of a stream that are intended for use by consumers (as described in
the API for Stream Consumers section). Doing so may lead to adverse
side effects in application code consuming the stream.

For many simple cases, it is possible to construct a stream without relying on
inheritance. This can be accomplished by directly creating instances of the
stream.Writable, stream.Readable, stream.Duplex or stream.Transform
objects and passing appropriate methods as constructor options.

decodeStrings<boolean> Whether or not to decode strings into
Buffers before passing them to stream._write().
Defaults to true

objectMode<boolean> Whether or not the
stream.write(anyObj) is a valid operation. When set,
it becomes possible to write JavaScript values other than string,
Buffer or Uint8Array if supported by the stream implementation.
Defaults to false

Note: This function MUST NOT be called by application code directly. It
should be implemented by child classes, and called by the internal Writable
class methods only.

The callback method must be called to signal either that the write completed
successfully or failed with an error. The first argument passed to the
callback must be the Error object if the call failed or null if the
write succeeded.

It is important to note that all calls to writable.write() that occur between
the time writable._write() is called and the callback is called will cause
the written data to be buffered. Once the callback is invoked, the stream will
emit a 'drain' event. If a stream implementation is capable of processing
multiple chunks of data at once, the writable._writev() method should be
implemented.

If the decodeStrings property is set in the constructor options, then
chunk may be a string rather than a Buffer, and encoding will
indicate the character encoding of the string. This is to support
implementations that have an optimized handling for certain string
data encodings. If the decodeStrings property is explicitly set to false,
the encoding argument can be safely ignored, and chunk will remain the same
object that is passed to .write().

The writable._write() method is prefixed with an underscore because it is
internal to the class that defines it, and should never be called directly by
user programs.

chunks<Array> The chunks to be written. Each chunk has following
format: { chunk: ..., encoding: ... }.

callback<Function> A callback function (optionally with an error
argument) to be invoked when processing is complete for the supplied chunks.

Note: This function MUST NOT be called by application code directly. It
should be implemented by child classes, and called by the internal Writable
class methods only.

The writable._writev() method may be implemented in addition to
writable._write() in stream implementations that are capable of processing
multiple chunks of data at once. If implemented, the method will be called with
all chunks of data currently buffered in the write queue.

The writable._writev() method is prefixed with an underscore because it is
internal to the class that defines it, and should never be called directly by
user programs.

It is recommended that errors occurring during the processing of the
writable._write() and writable._writev() methods are reported by invoking
the callback and passing the error as the first argument. This will cause an
'error' event to be emitted by the Writable. Throwing an Error from within
writable._write() can result in unexpected and inconsistent behavior depending
on how the stream is being used. Using the callback ensures consistent and
predictable handling of errors.

The following illustrates a rather simplistic (and somewhat pointless) custom
Writable stream implementation. While this specific Writable stream instance
is not of any real particular usefulness, the example illustrates each of the
required elements of a custom Writable stream instance:

Note: This function MUST NOT be called by application code directly. It
should be implemented by child classes, and called by the internal Readable
class methods only.

All Readable stream implementations must provide an implementation of the
readable._read() method to fetch data from the underlying resource.

When readable._read() is called, if data is available from the resource, the
implementation should begin pushing that data into the read queue using the
this.push(dataChunk) method. _read() should continue reading
from the resource and pushing data until readable.push() returns false. Only
when _read() is called again after it has stopped should it resume pushing
additional data onto the queue.

Note: Once the readable._read() method has been called, it will not be
called again until the readable.push() method is called.

The size argument is advisory. For implementations where a "read" is a
single operation that returns data can use the size argument to determine how
much data to fetch. Other implementations may ignore this argument and simply
provide data whenever it becomes available. There is no need to "wait" until
size bytes are available before calling stream.push(chunk).

The readable._read() method is prefixed with an underscore because it is
internal to the class that defines it, and should never be called directly by
user programs.

chunk<Buffer> | <Uint8Array> | <string> | <null> | <any> Chunk of data to push into the
read queue. For streams not operating in object mode, chunk must be a
string, Buffer or Uint8Array. For object mode streams, chunk may be
any JavaScript value.

encoding<string> Encoding of string chunks. Must be a valid
Buffer encoding, such as 'utf8' or 'ascii'

Returns <boolean>true if additional chunks of data may continued to be
pushed; false otherwise.

When chunk is a Buffer, Uint8Array or string, the chunk of data will
be added to the internal queue for users of the stream to consume.
Passing chunk as null signals the end of the stream (EOF), after which no
more data can be written.

When the Readable is operating in paused mode, the data added with
readable.push() can be read out by calling the
readable.read() method when the 'readable' event is
emitted.

When the Readable is operating in flowing mode, the data added with
readable.push() will be delivered by emitting a 'data' event.

The readable.push() method is designed to be as flexible as possible. For
example, when wrapping a lower-level source that provides some form of
pause/resume mechanism, and a data callback, the low-level source can be wrapped
by the custom Readable instance as illustrated in the following example:

// source is an object with readStop() and readStart() methods,
// and an `ondata` member that gets called when it has data, and
// an `onend` member that gets called when the data is over.
class SourceWrapper extends Readable {
constructor(options) {
super(options);
this._source = getLowlevelSourceObject();
// Every time there's data, push it into the internal buffer.
this._source.ondata = (chunk) => {
// if push() returns false, then stop reading from source
if (!this.push(chunk))
this._source.readStop();
};
// When the source ends, push the EOF-signaling `null` chunk
this._source.onend = () => {
this.push(null);
};
}
// _read will be called when the stream wants to pull more data in
// the advisory size argument is ignored in this case.
_read(size) {
this._source.readStart();
}
}

Note: The readable.push() method is intended be called only by Readable
Implementers, and only from within the readable._read() method.

It is recommended that errors occurring during the processing of the
readable._read() method are emitted using the 'error' event rather than
being thrown. Throwing an Error from within readable._read() can result in
unexpected and inconsistent behavior depending on whether the stream is
operating in flowing or paused mode. Using the 'error' event ensures
consistent and predictable handling of errors.

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.Readableandstream.Writable classes).

Note: 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.

The following illustrates a simple example of a Duplex stream that wraps a
hypothetical lower-level source object to which data can be written, and
from which data can be read, albeit using an API that is not compatible with
Node.js streams.
The following illustrates a simple example of a Duplex stream that buffers
incoming written data via the Writable interface that is read back out
via the Readable interface.

For Duplex streams, objectMode can be set exclusively for either the Readable
or Writable side using the readableObjectMode and writableObjectMode options
respectively.

In the following example, for instance, a new Transform stream (which is a
type of Duplex stream) is created that has an object mode Writable side
that accepts JavaScript numbers that are converted to hexadecimal strings on
the Readable side.

A Transform stream is a Duplex stream where the output is computed
in some way from the input. Examples include zlib streams or crypto
streams that compress, encrypt, or decrypt data.

Note: There is no requirement that the output be the same size as the input,
the same number of chunks, or arrive at the same time. For example, a
Hash stream will only ever have a single chunk of output which is
provided when the input is ended. A zlib stream will produce output
that is either much smaller or much larger than its input.

The stream.Transform class is extended to implement a Transform stream.

The stream.Transform class prototypically inherits from stream.Duplex and
implements its own versions of the writable._write() and readable._read()
methods. Custom Transform implementations must implement the
transform._transform() method and may also implement
the transform._flush() method.

Note: Care must be taken when using Transform streams in that data written
to the stream can cause the Writable side of the stream to become paused if
the output on the Readable side is not consumed.

The 'finish' and 'end' events are from the stream.Writable
and stream.Readable classes, respectively. The 'finish' event is emitted
after stream.end() is called and all chunks have been processed
by stream._transform(). The 'end' event is emitted
after all data has been output, which occurs after the callback in
transform._flush() has been called.

callback<Function> A callback function (optionally with an error
argument and data) to be called when remaining data has been flushed.

Note: This function MUST NOT be called by application code directly. It
should be implemented by child classes, and called by the internal Readable
class methods only.

In some cases, a transform operation may need to emit an additional bit of
data at the end of the stream. For example, a zlib compression stream will
store an amount of internal state used to optimally compress the output. When
the stream ends, however, that additional data needs to be flushed so that the
compressed data will be complete.

Custom Transform implementations may implement the transform._flush()
method. This will be called when there is no more written data to be consumed,
but before the 'end' event is emitted signaling the end of the
Readable stream.

Within the transform._flush() implementation, the readable.push() method
may be called zero or more times, as appropriate. The callback function must
be called when the flush operation is complete.

The transform._flush() method is prefixed with an underscore because it is
internal to the class that defines it, and should never be called directly by
user programs.

chunk<Buffer> | <string> | <any> The chunk to be transformed. Will always
be a buffer unless the decodeStrings option was set to false
or the stream is operating in object mode.

encoding<string> If the chunk is a string, then this is the
encoding type. If chunk is a buffer, then this is the special
value - 'buffer', ignore it in this case.

callback<Function> A callback function (optionally with an error
argument and data) to be called after the supplied chunk has been
processed.

Note: This function MUST NOT be called by application code directly. It
should be implemented by child classes, and called by the internal Readable
class methods only.

All Transform stream implementations must provide a _transform()
method to accept input and produce output. The transform._transform()
implementation handles the bytes being written, computes an output, then passes
that output off to the readable portion using the readable.push() method.

The transform.push() method may be called zero or more times to generate
output from a single input chunk, depending on how much is to be output
as a result of the chunk.

It is possible that no output is generated from any given chunk of input data.

The callback function must be called only when the current chunk is completely
consumed. The first argument passed to the callback must be an Error object
if an error occurred while processing the input or null otherwise. If a second
argument is passed to the callback, it will be forwarded on to the
readable.push() method. In other words the following are equivalent:

The stream.PassThrough class is a trivial implementation of a Transform
stream that simply passes the input bytes across to the output. Its purpose is
primarily for examples and testing, but there are some use cases where
stream.PassThrough is useful as a building block for novel sorts of streams.

In versions of Node.js prior to v0.10, the Readable stream interface was
simpler, but also less powerful and less useful.

Rather than waiting for calls the stream.read() method,
'data' events would begin emitting immediately. Applications that
would need to perform some amount of work to decide how to handle data
were required to store read data into buffers so the data would not be lost.

The stream.pause() method was advisory, rather than
guaranteed. This meant that it was still necessary to be prepared to receive
'data' events even when the stream was in a paused state.

In Node.js v0.10, the Readable class was added. For backwards compatibility
with older Node.js programs, Readable streams switch into "flowing mode" when a
'data' event handler is added, or when the
stream.resume() method is called. The effect is that, even
when not using the new stream.read() method and
'readable' event, it is no longer necessary to worry about losing
'data' chunks.

While most applications will continue to function normally, this introduces an
edge case in the following conditions:

// WARNING! BROKEN!
net.createServer((socket) => {
// we add an 'end' method, but never consume the data
socket.on('end', () => {
// It will never get here.
socket.end('The message was received but was not processed.\n');
});
}).listen(1337);

In versions of Node.js prior to v0.10, the incoming message data would be
simply discarded. However, in Node.js v0.10 and beyond, the socket remains
paused forever.

The workaround in this situation is to call the
stream.resume() method to begin the flow of data:

There are some cases where it is necessary to trigger a refresh of the
underlying readable stream mechanisms, without actually consuming any
data. In such cases, it is possible to call readable.read(0), which will
always return null.

If the internal read buffer is below the highWaterMark, and the
stream is not currently reading, then calling stream.read(0) will trigger
a low-level stream._read() call.

While most applications will almost never need to do this, there are
situations within Node.js where this is done, particularly in the
Readable stream class internals.

Pushing a zero-byte string, Buffer or Uint8Array to a stream that is not in
object mode has an interesting side effect. Because it is a call to
readable.push(), the call will end the reading process.
However, because the argument is an empty string, no data is added to the
readable buffer so there is nothing for a user to consume.

The use of readable.setEncoding() will change the behavior of how the
highWaterMark operates in non-object mode.

Typically, the size of the current buffer is measured against the
highWaterMark in bytes. However, after setEncoding() is called, the
comparison function will begin to measure the buffer's size in characters.

This is not a problem in common cases with latin1 or ascii. But it is
advised to be mindful about this behavior when working with strings that could
contain multi-byte characters.