2012-02-27 20:18:10 +01:00
|
|
|
# Stream
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
A stream is an abstract interface for working with streaming data in Node.js.
|
|
|
|
The `stream` module provides a base API that makes it easy to build objects
|
|
|
|
that implement the stream interface.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
There are many stream objects provided by Node.js. For instance, a
|
|
|
|
[request to an HTTP server][http-incoming-message] and [`process.stdout`][]
|
|
|
|
are both stream instances.
|
2012-02-27 20:18:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Streams can be readable, writable, or both. All streams are instances of
|
|
|
|
[`EventEmitter`][].
|
2015-12-10 22:32:13 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream` module can be accessed using:
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const stream = require('stream');
|
|
|
|
```
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
While it is important for all Node.js users to understand how streams works,
|
|
|
|
the `stream` module itself is most useful for developer's that are creating new
|
|
|
|
types of stream instances. Developer's who are primarily *consuming* stream
|
|
|
|
objects will rarely (if ever) have need to use the `stream` module directly.
|
|
|
|
|
|
|
|
## Organization of this document
|
|
|
|
|
|
|
|
This document is divided into two primary sections and 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.
|
|
|
|
|
|
|
|
## Types of Streams
|
|
|
|
|
|
|
|
There are four fundamental stream types within Node.js:
|
|
|
|
|
|
|
|
* [Readable][] - streams from which data can be read (for example
|
|
|
|
[`fs.createReadStream()`][]).
|
|
|
|
* [Writable][] - streams to which data can be written (for example
|
2016-06-18 23:19:41 +02:00
|
|
|
[`fs.createWriteStream()`][]).
|
2016-05-24 07:30:41 +02:00
|
|
|
* [Duplex][] - streams that are both Readable and Writable (for example
|
|
|
|
[`net.Socket`][]).
|
|
|
|
* [Transform][] - Duplex streams that can modify or transform the data as it
|
|
|
|
is written and read (for example [`zlib.createDeflate()`][]).
|
|
|
|
|
|
|
|
### Object Mode
|
|
|
|
|
|
|
|
All streams created by Node.js APIs operate exclusively on strings and `Buffer`
|
|
|
|
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.
|
|
|
|
|
|
|
|
### Buffering
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2013-07-16 01:56:02 +02:00
|
|
|
<!--type=misc-->
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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)`][stream-push]. If the consumer of the Stream does not
|
|
|
|
call [`stream.read()`][stream-read], the data will sit in the internal
|
|
|
|
queue until it is consumed.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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,
|
2016-06-18 23:19:41 +02:00
|
|
|
the stream will stop calling the internal `readable._read()` method that is
|
2016-05-24 07:30:41 +02:00
|
|
|
used to fill the read buffer).
|
|
|
|
|
|
|
|
Data is buffered in Writable streams when the
|
|
|
|
[`writable.write(chunk)`][stream-write] 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
|
|
|
|
the size of the internal buffer reaches or exceeds the `highWaterMark`, `false`
|
|
|
|
will be returned.
|
|
|
|
|
|
|
|
A key goal of the `stream` API, an in particular 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 each side operate (and buffer) independently of
|
|
|
|
the other.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
## API for Stream Consumers
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
<!--type=misc-->
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Almost all Node.js applications, no matter how simple, use streams in some
|
|
|
|
manner. The following is an example of using streams in a Node.js application
|
|
|
|
that implements an HTTP server:
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-12-15 00:20:25 +01:00
|
|
|
const http = require('http');
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
const server = http.createServer( (req, res) => {
|
2013-07-16 01:56:02 +02:00
|
|
|
// req is an http.IncomingMessage, which is a Readable Stream
|
|
|
|
// res is an http.ServerResponse, which is a Writable Stream
|
|
|
|
|
|
|
|
var body = '';
|
2016-05-24 07:30:41 +02:00
|
|
|
// Get the data as utf8 strings.
|
|
|
|
// If an encoding is not set, Buffer objects will be received.
|
2013-07-16 01:56:02 +02:00
|
|
|
req.setEncoding('utf8');
|
|
|
|
|
|
|
|
// Readable streams emit 'data' events once a listener is added
|
2015-12-15 00:20:25 +01:00
|
|
|
req.on('data', (chunk) => {
|
2013-07-16 01:56:02 +02:00
|
|
|
body += chunk;
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// the end event indicates that the entire body has been received
|
2015-12-15 00:20:25 +01:00
|
|
|
req.on('end', () => {
|
2013-07-16 01:56:02 +02:00
|
|
|
try {
|
2016-05-24 07:30:41 +02:00
|
|
|
const data = JSON.parse(body);
|
2013-07-16 01:56:02 +02:00
|
|
|
} catch (er) {
|
|
|
|
// uh oh! bad json!
|
|
|
|
res.statusCode = 400;
|
2015-12-15 00:20:25 +01:00
|
|
|
return res.end(`error: ${er.message}`);
|
2013-07-16 01:56:02 +02:00
|
|
|
}
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2013-07-16 01:56:02 +02:00
|
|
|
// write back something interesting to the user:
|
|
|
|
res.write(typeof data);
|
|
|
|
res.end();
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
|
|
|
});
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2013-07-16 01:56:02 +02:00
|
|
|
server.listen(1337);
|
|
|
|
|
|
|
|
// $ curl localhost:1337 -d '{}'
|
|
|
|
// object
|
|
|
|
// $ curl localhost:1337 -d '"foo"'
|
|
|
|
// string
|
|
|
|
// $ curl localhost:1337 -d 'not json'
|
|
|
|
// error: Unexpected token o
|
2012-12-13 20:15:49 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
[Writable][] streams (such as `res` in the example) expose methods such as
|
|
|
|
`write()` and `end()` that are used to write data onto the stream.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
[Readable][] streams use the [`EventEmitter`][] API for notifying application
|
|
|
|
code when data is available to be read off the stream. That available data can
|
|
|
|
be read from the stream in multiple ways.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Both [Writable][] and [Readable][] streams use the [`EventEmitter`][] API in
|
|
|
|
various ways to communicate the current state of the stream.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
[Duplex][] and [Transform][] streams are both [Writable][] and [Readable][].
|
|
|
|
|
|
|
|
Applications that are either writing data to or consuming data from a stream
|
|
|
|
are not required to implement the stream interfaces directly and will generally
|
|
|
|
have no reason to call `require('stream')`.
|
|
|
|
|
|
|
|
Developers wishing to implement new types of streams should refer to the
|
|
|
|
section [API for Stream Implemeters][].
|
|
|
|
|
|
|
|
### Writable Streams
|
|
|
|
|
|
|
|
Writable streams are an abstraction for a *destination* to which data is
|
|
|
|
written.
|
|
|
|
|
|
|
|
Examples of [Writable][] streams include:
|
|
|
|
|
|
|
|
* [HTTP requests, on the client][]
|
|
|
|
* [HTTP responses, on the server][]
|
|
|
|
* [fs write streams][]
|
2016-02-02 18:34:29 +01:00
|
|
|
* [zlib streams][zlib]
|
|
|
|
* [crypto streams][crypto]
|
2016-05-24 07:30:41 +02:00
|
|
|
* [TCP sockets][]
|
|
|
|
* [child process stdin][]
|
|
|
|
* [`process.stdout`][], [`process.stderr`][]
|
|
|
|
|
|
|
|
*Note*: Some of these examples are actually [Duplex][] streams that implement
|
|
|
|
the [Writable][] interface.
|
|
|
|
|
|
|
|
All [Writable][] streams implement the interface defined by the
|
|
|
|
`stream.Writable` class.
|
|
|
|
|
|
|
|
While specific instances of [Writable][] streams may differ in various ways,
|
|
|
|
all Writable streams follow the same fundamental usage pattern as illustrated
|
|
|
|
in the example below:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const myStream = getWritableStreamSomehow();
|
|
|
|
myStream.write('some data');
|
|
|
|
myStream.write('some more data');
|
|
|
|
myStream.end('done writing data');
|
|
|
|
```
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Class: stream.Writable
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2013-07-16 01:56:02 +02:00
|
|
|
<!--type=class-->
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'close'
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Not all Writable streams will emit the `'close'` event.
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'drain'
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
If a call to [`stream.write(chunk)`][stream-write] returns `false`, the
|
|
|
|
`'drain'` event will be emitted when it is appropriate to resume writing data
|
|
|
|
to the stream.
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
// Write the data to the supplied writable stream one million times.
|
|
|
|
// Be attentive to back-pressure.
|
|
|
|
function writeOneMillionTimes(writer, data, encoding, callback) {
|
|
|
|
var i = 1000000;
|
|
|
|
write();
|
|
|
|
function write() {
|
|
|
|
var ok = true;
|
|
|
|
do {
|
|
|
|
i--;
|
|
|
|
if (i === 0) {
|
|
|
|
// last time!
|
|
|
|
writer.write(data, encoding, callback);
|
|
|
|
} else {
|
|
|
|
// see if we should continue, or wait
|
|
|
|
// don't pass the callback, because we're not done yet.
|
|
|
|
ok = writer.write(data, encoding);
|
|
|
|
}
|
|
|
|
} while (i > 0 && ok);
|
|
|
|
if (i > 0) {
|
|
|
|
// had to stop early!
|
|
|
|
// write some more once it drains
|
|
|
|
writer.once('drain', write);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'error'
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* {Error}
|
|
|
|
|
|
|
|
The `'error'` event is emitted if an error occurred while writing or piping
|
|
|
|
data. The listener callback is passed a single `Error` argument when called.
|
|
|
|
|
|
|
|
*Note*: The stream is not closed when the `'error'` event is emitted.
|
|
|
|
|
|
|
|
##### Event: 'finish'
|
|
|
|
|
|
|
|
The `'finish'` event is emitted after the [`stream.end()`][stream-end] method
|
|
|
|
has been called, and all data has been flushed to the underlying system.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const writer = getWritableStreamSomehow();
|
|
|
|
for (var i = 0; i < 100; i ++) {
|
|
|
|
writer.write('hello, #${i}!\n');
|
|
|
|
}
|
|
|
|
writer.end('This is the end\n');
|
|
|
|
writer.on('finish', () => {
|
|
|
|
console.error('All writes are now complete.');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Event: 'pipe'
|
|
|
|
|
|
|
|
* `src` {stream.Readable} source stream that is piping to this writable
|
|
|
|
|
|
|
|
The `'pipe'` event is emitted when the [`stream.pipe()`][] method is called on
|
|
|
|
a readable stream, adding this writable to its set of destinations.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const writer = getWritableStreamSomehow();
|
|
|
|
const reader = getReadableStreamSomehow();
|
|
|
|
writer.on('pipe', (src) => {
|
|
|
|
console.error('something is piping into the writer');
|
|
|
|
assert.equal(src, reader);
|
|
|
|
});
|
|
|
|
reader.pipe(writer);
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Event: 'unpipe'
|
|
|
|
|
|
|
|
* `src` {[Readable][] Stream} The source stream that
|
|
|
|
[unpiped][`stream.unpipe()`] this writable
|
|
|
|
|
|
|
|
The `'unpipe'` event is emitted when the [`stream.unpipe()`][] method is called
|
|
|
|
on a [Readable][] stream, removing this [Writable][] from its set of
|
|
|
|
destinations.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const writer = getWritableStreamSomehow();
|
|
|
|
const reader = getReadableStreamSomehow();
|
|
|
|
writer.on('unpipe', (src) => {
|
|
|
|
console.error('Something has stopped piping into the writer.');
|
|
|
|
assert.equal(src, reader);
|
|
|
|
});
|
|
|
|
reader.pipe(writer);
|
|
|
|
reader.unpipe(writer);
|
|
|
|
```
|
|
|
|
|
|
|
|
##### writable.cork()
|
|
|
|
|
|
|
|
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()`][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 an backup in the internal
|
|
|
|
buffer that would have an adverse impact on performance. In such situations,
|
2016-06-18 23:19:41 +02:00
|
|
|
implementations that implement the `writable._writev()` method can perform
|
2016-05-24 07:30:41 +02:00
|
|
|
buffered writes in a more optimized manner.
|
|
|
|
|
|
|
|
##### writable.end([chunk][, encoding][, callback])
|
|
|
|
|
|
|
|
* `chunk` {String|Buffer|any} Optional data to write. For streams not operating
|
|
|
|
in object mode, `chunk` must be a string or a `Buffer`. For object mode
|
|
|
|
streams, `chunk` may be any JavaScript value other than `null`.
|
|
|
|
* `encoding` {String} The encoding, if `chunk` is a String
|
|
|
|
* `callback` {Function} Optional callback for when the stream is finished
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Calling the [`stream.write()`][stream-write] method after calling
|
|
|
|
[`stream.end()`][stream-end] will raise an error.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// write 'hello, ' and then end with 'world!'
|
|
|
|
const file = fs.createWriteStream('example.txt');
|
|
|
|
file.write('hello, ');
|
|
|
|
file.end('world!');
|
|
|
|
// writing more now is not allowed!
|
|
|
|
```
|
|
|
|
|
|
|
|
##### writable.setDefaultEncoding(encoding)
|
|
|
|
|
|
|
|
* `encoding` {String} The new default encoding
|
|
|
|
* Return: `this`
|
|
|
|
|
|
|
|
The `writable.setDefaultEncoding()` method sets the default `encoding` for a
|
|
|
|
[Writable][] stream.
|
|
|
|
|
|
|
|
##### writable.uncork()
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
```js
|
|
|
|
stream.cork();
|
|
|
|
stream.write('some ');
|
|
|
|
stream.write('data ');
|
|
|
|
process.nextTick(() => stream.uncork());
|
|
|
|
```
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
##### writable.write(chunk[, encoding][, callback])
|
|
|
|
|
|
|
|
* `chunk` {String|Buffer} The data to write
|
|
|
|
* `encoding` {String} The encoding, if `chunk` is a String
|
|
|
|
* `callback` {Function} Callback for when this chunk of data is flushed
|
|
|
|
* 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 `callback` *may or may not* be called with the error as its
|
|
|
|
first argument. To reliably detect write errors, add a listener for the
|
|
|
|
`'error'` event.
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The return value indicates whether the written `chunk` was buffered internally
|
|
|
|
and the buffer has exceeded the `highWaterMark` configured when the stream was
|
|
|
|
created. If `false` is returned, further attempts to write data to the stream
|
|
|
|
should be paused until the `'drain'` event is emitted.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
A Writable stream in object mode will always ignore the `encoding` argument.
|
|
|
|
|
|
|
|
### Readable Streams
|
|
|
|
|
|
|
|
Readable streams are an abstraction for a *source* from which data is
|
|
|
|
consumed.
|
|
|
|
|
|
|
|
Examples of Readable streams include:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-02-02 18:34:29 +01:00
|
|
|
* [HTTP responses, on the client][http-incoming-message]
|
|
|
|
* [HTTP requests, on the server][http-incoming-message]
|
2015-11-14 04:21:49 +01:00
|
|
|
* [fs read streams][]
|
2016-02-02 18:34:29 +01:00
|
|
|
* [zlib streams][zlib]
|
|
|
|
* [crypto streams][crypto]
|
|
|
|
* [TCP sockets][]
|
2013-07-16 01:56:02 +02:00
|
|
|
* [child process stdout and stderr][]
|
2015-11-28 00:30:32 +01:00
|
|
|
* [`process.stdin`][]
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
All [Readable][] streams implement the interface defined by the
|
|
|
|
`stream.Readable` class.
|
|
|
|
|
|
|
|
#### Two Modes
|
|
|
|
|
|
|
|
Readable streams effectively operate in one of two modes: flowing and paused.
|
|
|
|
|
|
|
|
When in flowing mode, data is read from the underlying system automatically
|
|
|
|
and provided to an application as quickly as possible using events via the
|
|
|
|
[`EventEmitter`][] interface.
|
|
|
|
|
|
|
|
In paused mode, the [`stream.read()`][stream-read] method must be called
|
|
|
|
explicitly to read chunks of data from the stream.
|
|
|
|
|
|
|
|
All [Readable][] streams begin in paused mode but can be switched to flowing
|
|
|
|
mode in one of the following ways:
|
2015-06-24 05:42:49 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* Adding a [`'data'`][] event handler.
|
|
|
|
* Calling the [`stream.resume()`][stream-resume] method.
|
|
|
|
* Calling the [`stream.pipe()`][] method to send the data to a [Writable][].
|
|
|
|
|
|
|
|
The Readable can switch back to paused mode using one of the following:
|
|
|
|
|
|
|
|
* If there are no pipe destinations, by calling the
|
|
|
|
[`stream.pause()`][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()`][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 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.
|
|
|
|
|
|
|
|
#### Three States
|
|
|
|
|
|
|
|
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.
|
2015-06-24 05:42:49 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2015-06-24 05:42:49 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
While `readable._readableState.flowing` is `false`, data may be accumulating
|
|
|
|
within the streams internal buffer.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Choose One
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
#### Class: stream.Readable
|
|
|
|
|
|
|
|
<!--type=class-->
|
|
|
|
|
|
|
|
##### Event: 'close'
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Not all [Readable][] streams will emit the `'close'` event.
|
|
|
|
|
|
|
|
##### Event: 'data'
|
|
|
|
|
|
|
|
* `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`.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('data', (chunk) => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log(`Received ${chunk.length} bytes of data.`);
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
2012-12-13 20:15:49 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'end'
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `'end'` event is emitted when there is no more data to be consumed from
|
|
|
|
the stream.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*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()`][stream-read] repeatedly until
|
|
|
|
all data has been consumed.
|
2012-02-27 20:09:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('data', (chunk) => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log(`Received ${chunk.length} bytes of data.`);
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('end', () => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log('There will be no more data.');
|
2013-07-16 01:56:02 +02:00
|
|
|
});
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'error'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-04-02 07:41:14 +02:00
|
|
|
* {Error}
|
2014-08-07 20:56:34 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `'error'` event may be emitted by a Readable implementation at any time.
|
|
|
|
Typically, this may occur if the underlying stream in unable to generate data
|
|
|
|
due to an underlying internal failure, or when a stream implementation attempts
|
|
|
|
to push an invalid chunk of data.
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The listener callback will be passed a single `Error` object.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### Event: 'readable'
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2013-07-16 01:56:02 +02:00
|
|
|
```javascript
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('readable', () => {
|
2015-11-05 20:54:10 +01:00
|
|
|
// there is some data to read now
|
2013-07-16 01:56:02 +02:00
|
|
|
});
|
|
|
|
```
|
2016-05-24 07:30:41 +02:00
|
|
|
The `'readable'` event will also be emitted once the end of the stream data
|
|
|
|
has been reached but before the `'end'` event is emitted.
|
2013-03-01 23:26:35 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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()`][stream-read] will return the
|
|
|
|
available data. In the latter case, [`stream.read()`][stream-read] will return
|
|
|
|
`null`. For instance, in the following example, `foo.txt` is an empty file:
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-12-15 00:20:25 +01:00
|
|
|
const fs = require('fs');
|
2016-05-24 07:30:41 +02:00
|
|
|
const rr = fs.createReadStream('foo.txt');
|
2015-12-15 00:20:25 +01:00
|
|
|
rr.on('readable', () => {
|
2015-11-05 20:54:10 +01:00
|
|
|
console.log('readable:', rr.read());
|
|
|
|
});
|
2015-12-15 00:20:25 +01:00
|
|
|
rr.on('end', () => {
|
2015-11-05 20:54:10 +01:00
|
|
|
console.log('end');
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
2013-07-16 01:56:02 +02:00
|
|
|
```
|
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
The output of running this script is:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
```
|
2016-01-17 18:39:07 +01:00
|
|
|
$ node test.js
|
2015-11-05 20:54:10 +01:00
|
|
|
readable: null
|
|
|
|
end
|
|
|
|
```
|
2013-08-28 03:59:58 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: In general, the `readable.pipe()` and `'data'` event mechanisms are
|
|
|
|
preferred over the use of the `'readable'` event.
|
|
|
|
|
|
|
|
##### readable.isPaused()
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* Return: {Boolean}
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = new stream.Readable
|
2015-11-05 20:54:10 +01:00
|
|
|
|
|
|
|
readable.isPaused() // === false
|
|
|
|
readable.pause()
|
|
|
|
readable.isPaused() // === true
|
|
|
|
readable.resume()
|
|
|
|
readable.isPaused() // === false
|
2013-07-16 01:56:02 +02:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.pause()
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2013-08-28 03:59:58 +02:00
|
|
|
* Return: `this`
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.pause()` method will cause a stream in flowing mode to stop
|
|
|
|
emitting [`'data'`][] events, switching out of flowing mode. Any data that
|
|
|
|
becomes available will remain in the internal buffer.
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('data', (chunk) => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log(`Received ${chunk.length} bytes of data.`);
|
2013-07-16 01:56:02 +02:00
|
|
|
readable.pause();
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log('There will be no additional data for 1 second.');
|
2015-12-15 00:20:25 +01:00
|
|
|
setTimeout(() => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log('Now data will start flowing again.');
|
2013-07-16 01:56:02 +02:00
|
|
|
readable.resume();
|
|
|
|
}, 1000);
|
2014-10-03 07:53:15 +02:00
|
|
|
});
|
2013-07-16 01:56:02 +02:00
|
|
|
```
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.pipe(destination[, options])
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* `destination` {stream.Writable} The destination for writing data
|
2013-07-16 01:56:02 +02:00
|
|
|
* `options` {Object} Pipe options
|
2016-05-24 07:30:41 +02:00
|
|
|
* `end` {Boolean} End the writer when the reader ends. Defaults to `true`.
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-01-08 03:07:17 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The following example pipes all of the data from the `readable` into a file
|
|
|
|
named `file.txt`:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
|
|
|
const writable = fs.createWriteStream('file.txt');
|
2013-07-16 01:56:02 +02:00
|
|
|
// All the data from readable goes into 'file.txt'
|
|
|
|
readable.pipe(writable);
|
2013-01-08 03:07:17 +01:00
|
|
|
```
|
2016-05-24 07:30:41 +02:00
|
|
|
It is possible to attach multiple Writable streams to a single Readable stream.
|
2012-12-13 20:15:49 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.pipe()` method returns a reference to the *destination* stream
|
|
|
|
making it possible to set up chains of piped streams:
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const r = fs.createReadStream('file.txt');
|
|
|
|
const z = zlib.createGzip();
|
|
|
|
const w = fs.createWriteStream('file.txt.gz');
|
2013-07-16 01:56:02 +02:00
|
|
|
r.pipe(z).pipe(w);
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
By default, [`stream.end()`][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:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2013-07-16 01:56:02 +02:00
|
|
|
reader.pipe(writer, { end: false });
|
2015-12-15 00:20:25 +01:00
|
|
|
reader.on('end', () => {
|
2013-07-16 01:56:02 +02:00
|
|
|
writer.end('Goodbye\n');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: The [`process.stderr`][] and [`process.stdout`][] Writable streams are
|
|
|
|
never closed until the Node.js process exits, regardless of the specified
|
|
|
|
options.
|
|
|
|
|
|
|
|
##### readable.read([size])
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
* `size` {Number} Optional argument to specify how much data to read.
|
2016-01-19 17:03:15 +01:00
|
|
|
* Return {String|Buffer|Null}
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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 (*even if it exceeds `size` bytes*).
|
stream: Fix unshift() race conditions
Fix #5272
The consumption of a readable stream is a dance with 3 partners.
1. The specific stream Author (A)
2. The Stream Base class (B), and
3. The Consumer of the stream (C)
When B calls the _read() method that A implements, it sets a 'reading'
flag, so that parallel calls to _read() can be avoided. When A calls
stream.push(), B knows that it's safe to start calling _read() again.
If the consumer C is some kind of parser that wants in some cases to
pass the source stream off to some other party, but not before "putting
back" some bit of previously consumed data (as in the case of Node's
websocket http upgrade implementation). So, stream.unshift() will
generally *never* be called by A, but *only* called by C.
Prior to this patch, stream.unshift() *also* unset the state.reading
flag, meaning that C could indicate the end of a read, and B would
dutifully fire off another _read() call to A. This is inappropriate.
In the case of fs streams, and other variably-laggy streams that don't
tolerate overlapped _read() calls, this causes big problems.
Also, calling stream.shift() after the 'end' event did not raise any
kind of error, but would cause very strange behavior indeed. Calling it
after the EOF chunk was seen, but before the 'end' event was fired would
also cause weird behavior, and could lead to data being lost, since it
would not emit another 'readable' event.
This change makes it so that:
1. stream.unshift() does *not* set state.reading = false
2. stream.unshift() is allowed up until the 'end' event.
3. unshifting onto a EOF-encountered and zero-length (but not yet
end-emitted) stream will defer the 'end' event until the new data is
consumed.
4. pushing onto a EOF-encountered stream is now an error.
So, if you read(), you have that single tick to safely unshift() data
back into the stream, even if the null chunk was pushed, and the length
was 0.
2013-04-12 00:01:26 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
If the `size` argument is not specified, all of the data contained in the
|
|
|
|
internal buffer will be returned.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('readable', () => {
|
2015-11-05 20:54:10 +01:00
|
|
|
var chunk;
|
|
|
|
while (null !== (chunk = readable.read())) {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log(`Received ${chunk.length} bytes of data.`);
|
2015-11-05 20:54:10 +01:00
|
|
|
}
|
|
|
|
});
|
2013-07-16 01:56:02 +02:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
In general, it is recommended that developers avoid the use of the `'readable'`
|
|
|
|
event and the `readable.read()` method in favor of using either
|
|
|
|
`readable.pipe()` or the `'data'` event.
|
|
|
|
|
|
|
|
A Readable stream in object mode will always return a single item from
|
|
|
|
a call to [`readable.read(size)`][stream-read], regardless of the value of the
|
|
|
|
`size` argument.
|
|
|
|
|
|
|
|
*Note:* If the `readable.read()` method returns a chunk of data, a `'data'`
|
|
|
|
event will also be emitted.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: Calling [`stream.read([size])`][stream-read] after the [`'end'`][]
|
|
|
|
event has been emitted will return `null`. No runtime error will be raised.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.resume()
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
* Return: `this`
|
2015-06-24 05:42:49 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.resume()` method causes an explicitly paused Readable stream to
|
|
|
|
resume emitting [`'data'`][] events, switching the stream into flowing mode.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.resume()` method can be used to fully consume the data from a
|
|
|
|
stream without actually processing any of that data as illustrated in the
|
|
|
|
following example:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
getReadableStreamSomehow()
|
2016-06-18 23:19:41 +02:00
|
|
|
.resume()
|
2016-05-24 07:30:41 +02:00
|
|
|
.on('end', () => {
|
|
|
|
console.log('Reached the end, but did not read anything.');
|
|
|
|
});
|
2015-11-05 20:54:10 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.setEncoding(encoding)
|
2015-11-05 20:54:10 +01:00
|
|
|
|
|
|
|
* `encoding` {String} The encoding to use.
|
|
|
|
* Return: `this`
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.setEncoding()` method sets the default character encoding for
|
|
|
|
data read from the Readable stream.
|
|
|
|
|
|
|
|
Setting an encoding causes the stream data
|
|
|
|
to be returned as string of the specified encoding rather than as `Buffer`
|
|
|
|
objects. For instance, calling `readable.setEncoding('utf8')` will cause the
|
|
|
|
output data will 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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Encoding can be disabled by calling `readable.setEncoding(null)`. This approach
|
|
|
|
is useful when working with binary data or with large multi-byte strings spread
|
|
|
|
out over multiple chunks.
|
2016-02-09 09:31:07 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
2015-11-05 20:54:10 +01:00
|
|
|
readable.setEncoding('utf8');
|
2015-12-15 00:20:25 +01:00
|
|
|
readable.on('data', (chunk) => {
|
2015-11-05 20:54:10 +01:00
|
|
|
assert.equal(typeof chunk, 'string');
|
|
|
|
console.log('got %d characters of string data', chunk.length);
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.unpipe([destination])
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* `destination` {stream.Writable} Optional specific stream to unpipe
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `readable.unpipe()` method detaches a Writable stream previously attached
|
|
|
|
using the [`stream.pipe()`][] method.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
If the `destination` is not specified, then *all* pipes are detached.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
If the `destination` is specified, but no pipe is set up for it, then
|
|
|
|
the method does nothing.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const readable = getReadableStreamSomehow();
|
|
|
|
const writable = fs.createWriteStream('file.txt');
|
2015-11-05 20:54:10 +01:00
|
|
|
// All the data from readable goes into 'file.txt',
|
|
|
|
// but only for the first second
|
|
|
|
readable.pipe(writable);
|
2015-12-15 00:20:25 +01:00
|
|
|
setTimeout(() => {
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log('Stop writing to file.txt');
|
2015-11-05 20:54:10 +01:00
|
|
|
readable.unpipe(writable);
|
2016-05-24 07:30:41 +02:00
|
|
|
console.log('Manually close the file stream');
|
2015-11-05 20:54:10 +01:00
|
|
|
writable.end();
|
|
|
|
}, 1000);
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.unshift(chunk)
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* `chunk` {Buffer|String} Chunk of data to unshift onto the read queue
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: The `stream.unshift(chunk)` method cannot be called after the
|
|
|
|
[`'end'`][] event has been emitted or a runtime error will be thrown.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Developers using `stream.unshift()` often should consider switching to
|
|
|
|
use of a [Transform][] stream instead. See the [API for Stream Implemeters][]
|
|
|
|
section for more information.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-11-05 20:54:10 +01:00
|
|
|
// Pull off a header delimited by \n\n
|
|
|
|
// use unshift() if we get too much
|
|
|
|
// Call the callback with (error, header, stream)
|
2015-12-15 00:20:25 +01:00
|
|
|
const StringDecoder = require('string_decoder').StringDecoder;
|
2015-11-05 20:54:10 +01:00
|
|
|
function parseHeader(stream, callback) {
|
|
|
|
stream.on('error', callback);
|
2013-07-16 01:56:02 +02:00
|
|
|
stream.on('readable', onReadable);
|
2016-05-24 07:30:41 +02:00
|
|
|
const decoder = new StringDecoder('utf8');
|
2013-07-16 01:56:02 +02:00
|
|
|
var header = '';
|
|
|
|
function onReadable() {
|
|
|
|
var chunk;
|
|
|
|
while (null !== (chunk = stream.read())) {
|
|
|
|
var str = decoder.write(chunk);
|
|
|
|
if (str.match(/\n\n/)) {
|
|
|
|
// found the header boundary
|
|
|
|
var split = str.split(/\n\n/);
|
|
|
|
header += split.shift();
|
2016-05-24 07:30:41 +02:00
|
|
|
const remaining = split.join('\n\n');
|
|
|
|
const buf = Buffer.from(remaining, 'utf8');
|
2013-07-16 01:56:02 +02:00
|
|
|
if (buf.length)
|
|
|
|
stream.unshift(buf);
|
|
|
|
stream.removeListener('error', callback);
|
|
|
|
stream.removeListener('readable', onReadable);
|
|
|
|
// now the body of the message can be read from the stream.
|
|
|
|
callback(null, header, stream);
|
|
|
|
} else {
|
|
|
|
// still reading the header.
|
|
|
|
header += str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2016-01-17 18:39:07 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: Unlike [`stream.push(chunk)`][stream-push], `stream.unshift(chunk)`
|
2016-02-02 18:34:29 +01:00
|
|
|
will not end the reading process by resetting the internal reading state of the
|
2016-05-24 07:30:41 +02:00
|
|
|
stream. This can cause unexpected results if `readable.unshift()` is called
|
|
|
|
during a read (i.e. from within a [`stream._read()`][stream-_read]
|
|
|
|
implementation on a custom stream). Following the call to `readable.unshift()`
|
|
|
|
with an immediate [`stream.push('')`][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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
##### readable.wrap(stream)
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
* `stream` {Stream} An "old style" readable stream
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
Versions of Node.js prior to v0.10 had streams that did not implement the
|
2016-05-24 07:30:41 +02:00
|
|
|
entire `stream` module API as it is currently defined. (See [Compatibility][]
|
|
|
|
for more information.)
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
When using an older Node.js library that emits [`'data'`][] events and has a
|
|
|
|
[`stream.pause()`][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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-12-15 00:20:25 +01:00
|
|
|
const OldReader = require('./old-api-module.js').OldReader;
|
|
|
|
const Readable = require('stream').Readable;
|
|
|
|
const oreader = new OldReader;
|
|
|
|
const myReader = new Readable().wrap(oreader);
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
myReader.on('readable', () => {
|
2013-07-16 01:56:02 +02:00
|
|
|
myReader.read(); // etc.
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### Duplex and Transform Streams
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Class: stream.Duplex
|
|
|
|
|
|
|
|
<!--type=class-->
|
|
|
|
|
|
|
|
Duplex streams are streams that implement both the [Readable][] and
|
2016-01-05 11:49:54 +01:00
|
|
|
[Writable][] interfaces.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Examples of Duplex streams include:
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* [TCP sockets][]
|
2016-02-02 18:34:29 +01:00
|
|
|
* [zlib streams][zlib]
|
|
|
|
* [crypto streams][crypto]
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Class: stream.Transform
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
<!--type=class-->
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Transform streams are [Duplex][] streams where the output is in some way
|
|
|
|
related to the input. Like all [Duplex][] streams, Transform streams
|
|
|
|
implement both the [Readable][] and [Writable][] interfaces.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Examples of Transform streams include:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-02-02 18:34:29 +01:00
|
|
|
* [zlib streams][zlib]
|
|
|
|
* [crypto streams][crypto]
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-01 09:58:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
## API for Stream Implemeters
|
2016-05-01 09:58:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
<!--type=misc-->
|
2016-05-01 09:58:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream` module API has been designed to make it possible to easily
|
|
|
|
implement streams using JavaScript's prototypical inheritance model.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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 the call the appropriate
|
|
|
|
parent class constructor:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
class MyWritable extends Writable {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2013-07-16 01:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The new stream class must then implement one or more specific methods, depending
|
|
|
|
on the type of stream being created, as detailed in the chart below:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
<p>Use-case</p>
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
<p>Class</p>
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
<p>Method(s) to implement</p>
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<p>Reading only</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<p>[Readable](#stream_class_stream_readable_1)</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
2016-02-02 18:34:29 +01:00
|
|
|
<p><code>[_read][stream-_read]</code></p>
|
2013-07-16 01:56:02 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<p>Writing only</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<p>[Writable](#stream_class_stream_writable_1)</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
2016-02-02 18:34:29 +01:00
|
|
|
<p><code>[_write][stream-_write]</code>, <code>[_writev][stream-_writev]</code></p>
|
2013-07-16 01:56:02 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<p>Reading and writing</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<p>[Duplex](#stream_class_stream_duplex_1)</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
2016-02-02 18:34:29 +01:00
|
|
|
<p><code>[_read][stream-_read]</code>, <code>[_write][stream-_write]</code>, <code>[_writev][stream-_writev]</code></p>
|
2013-07-16 01:56:02 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<p>Operate on written data, then read the result</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<p>[Transform](#stream_class_stream_transform_1)</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
2016-02-02 18:34:29 +01:00
|
|
|
<p><code>[_transform][stream-_transform]</code>, <code>[_flush][stream-_flush]</code></p>
|
2013-07-16 01:56:02 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### Simplified Construction
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For example:
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
const myWritable = new Writable({
|
|
|
|
write(chunk, encoding, callback) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### Implementing a Writable Stream
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream.Writable` class is extended to implement a [Writable][] stream.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Custom Writable streams *must* call the `new stream.Writable([options])`
|
2016-06-18 23:19:41 +02:00
|
|
|
constructor and implement the `writable._write()` method. The
|
|
|
|
`writable._writev()` method *may* also be implemented.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Constructor: new stream.Writable([options])
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* `options` {Object}
|
|
|
|
* `highWaterMark` {Number} Buffer level when
|
|
|
|
[`stream.write()`][stream-write] starts returning `false`. Defaults to
|
|
|
|
`16384` (16kb), or `16` for `objectMode` streams.
|
|
|
|
* `decodeStrings` {Boolean} Whether or not to decode strings into
|
|
|
|
Buffers before passing them to [`stream._write()`][stream-_write].
|
|
|
|
Defaults to `true`
|
|
|
|
* `objectMode` {Boolean} Whether or not the
|
|
|
|
[`stream.write(anyObj)`][stream-write] is a valid operation. When set,
|
|
|
|
it becomes possible to write JavaScript values other than string or
|
|
|
|
`Buffer` if supported by the stream implementation. Defaults to `false`
|
|
|
|
* `write` {Function} Implementation for the
|
|
|
|
[`stream._write()`][stream-_write] method.
|
|
|
|
* `writev` {Function} Implementation for the
|
|
|
|
[`stream._writev()`][stream-_writev] method.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For example:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
class MyWritable extends Writable {
|
|
|
|
constructor(options) {
|
|
|
|
// Calls the stream.Writable() constructor
|
|
|
|
super(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or, when using pre-ES6 style constructors:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
function MyWritable(options) {
|
|
|
|
if (!(this instanceof MyWritable))
|
|
|
|
return new MyWritable(options);
|
|
|
|
Writable.call(this, options);
|
|
|
|
}
|
|
|
|
util.inherits(MyWritable, Writable);
|
|
|
|
```
|
|
|
|
|
|
|
|
Or, using the Simplified Constructor approach:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
const myWritable = new Writable({
|
|
|
|
write(chunk, encoding, callback) {
|
|
|
|
// ...
|
|
|
|
},
|
|
|
|
writev(chunks, callback) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
#### writable.\_write(chunk, encoding, callback)
|
|
|
|
|
|
|
|
* `chunk` {Buffer|String} The chunk to be written. Will **always**
|
|
|
|
be a buffer unless the `decodeStrings` option was set to `false`.
|
|
|
|
* `encoding` {String} If the chunk is a string, then `encoding` is the
|
|
|
|
character encoding of that string. If chunk is a `Buffer`, or if the
|
|
|
|
stream is operating in object mode, `encoding` may be ignored.
|
|
|
|
* `callback` {Function} Call this function (optionally with an error
|
|
|
|
argument) when processing is complete for the supplied chunk.
|
|
|
|
|
|
|
|
All Writable stream implementations must provide a
|
|
|
|
[`writable._write()`][stream-_write] method to send data to the underlying
|
|
|
|
resource.
|
|
|
|
|
|
|
|
*Note*: [Transform][] streams provide their own implementation of the
|
2016-06-18 23:19:41 +02:00
|
|
|
[`writable._write()`][stream-_write].
|
2016-05-24 07:30:41 +02:00
|
|
|
|
|
|
|
*Note*: **This function MUST NOT be called by application code directly.** It
|
|
|
|
should be implemented by child classes, and called only 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
|
2016-06-18 23:19:41 +02:00
|
|
|
the time `writable._write()` is called and the `callback` is called will cause
|
2016-05-24 07:30:41 +02:00
|
|
|
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
|
2016-06-18 23:19:41 +02:00
|
|
|
multiple chunks of data at once, the `writable._writev()` method should be
|
2016-05-24 07:30:41 +02:00
|
|
|
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 always be a
|
|
|
|
`Buffer`.
|
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `writable._write()` method is prefixed with an underscore because it is
|
2016-05-24 07:30:41 +02:00
|
|
|
internal to the class that defines it, and should never be called directly by
|
|
|
|
user programs.
|
|
|
|
|
|
|
|
#### writable.\_writev(chunks, callback)
|
|
|
|
|
|
|
|
* `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 only by the internal Writable
|
|
|
|
class methods only.
|
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `writable._writev()` method may be implemented in addition to
|
|
|
|
`writable._write()` in stream implementations that are capable of processing
|
2016-05-24 07:30:41 +02:00
|
|
|
multiple chunks of data at once. If implemented, the method will be called with
|
|
|
|
all chunks of data currently buffered in the write queue.
|
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `writable._writev()` method is prefixed with an underscore because it is
|
2016-05-24 07:30:41 +02:00
|
|
|
internal to the class that defines it, and should never be called directly by
|
|
|
|
user programs.
|
|
|
|
|
|
|
|
#### Errors While Writing
|
|
|
|
|
|
|
|
It is recommended that errors occurring during the processing of the
|
2016-06-18 23:19:41 +02:00
|
|
|
`writable._write()` and `writable._writev()` methods are reported by invoking
|
2016-05-24 07:30:41 +02:00
|
|
|
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
|
2016-06-18 23:19:41 +02:00
|
|
|
`writable._write()` can result in expected and inconsistent behavior depending
|
2016-05-24 07:30:41 +02:00
|
|
|
on how the stream is being used. Using the callback ensures consistent and
|
|
|
|
predictable handling of errors.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
const myWritable = new Writable({
|
|
|
|
write(chunk, encoding, callback) {
|
|
|
|
if (chunk.toString().indexOf('a') >= 0) {
|
|
|
|
callback(new Error('chunk is invalid'))
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
#### An Example Writable Stream
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Writable = require('stream').Writable;
|
|
|
|
|
|
|
|
class MyWritable extends Writable {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
_write(chunk, encoding, callback) {
|
|
|
|
if (chunk.toString().indexOf('a') >= 0) {
|
|
|
|
callback(new Error('chunk is invalid'))
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Implementing a Readable Stream
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream.Readable` class is extended to implement a [Readable][] stream.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Custom Readable streams *must* call the `new stream.Readable([options])`
|
2016-06-18 23:19:41 +02:00
|
|
|
constructor and implement the `readable._read()` method.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
#### new stream.Readable([options])
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-11-05 20:54:10 +01:00
|
|
|
* `options` {Object}
|
|
|
|
* `highWaterMark` {Number} The maximum number of bytes to store in
|
|
|
|
the internal buffer before ceasing to read from the underlying
|
2016-05-24 07:30:41 +02:00
|
|
|
resource. Defaults to `16384` (16kb), or `16` for `objectMode` streams
|
2015-11-05 20:54:10 +01:00
|
|
|
* `encoding` {String} If specified, then buffers will be decoded to
|
2016-05-24 07:30:41 +02:00
|
|
|
strings using the specified encoding. Defaults to `null`
|
2015-11-05 20:54:10 +01:00
|
|
|
* `objectMode` {Boolean} Whether this stream should behave
|
2016-02-02 18:34:29 +01:00
|
|
|
as a stream of objects. Meaning that [`stream.read(n)`][stream-read] returns
|
2016-05-24 07:30:41 +02:00
|
|
|
a single value instead of a Buffer of size n. Defaults to `false`
|
2016-02-02 18:34:29 +01:00
|
|
|
* `read` {Function} Implementation for the [`stream._read()`][stream-_read]
|
|
|
|
method.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Readable = require('stream').Readable;
|
|
|
|
|
|
|
|
class MyReadable extends Readable {
|
|
|
|
constructor(options) {
|
|
|
|
// Calls the stream.Readable(options) constructor
|
|
|
|
super(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or, when using pre-ES6 style constructors:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Readable = require('stream').Readable;
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
function MyReadable(options) {
|
|
|
|
if (!(this instanceof MyReadable))
|
|
|
|
return new MyReadable(options);
|
|
|
|
Readable.call(this, options);
|
|
|
|
}
|
|
|
|
util.inherits(MyReadable, Readable);
|
|
|
|
```
|
|
|
|
|
|
|
|
Or, using the Simplified Constructor approach:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const Readable = require('stream').Readable;
|
|
|
|
|
|
|
|
const myReadable = new Readable({
|
|
|
|
read(size) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
#### readable.\_read(size)
|
2015-11-05 20:54:10 +01:00
|
|
|
|
|
|
|
* `size` {Number} Number of bytes to read asynchronously
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: **This function MUST NOT be called by application code directly.** It
|
|
|
|
should be implemented by child classes, and called only by the internal Readable
|
|
|
|
class methods only.
|
|
|
|
|
|
|
|
All Readable stream implementations must provide an implementation of the
|
2016-06-18 23:19:41 +02:00
|
|
|
`readable._read()` method to fetch data from the underlying resource.
|
2016-05-24 07:30:41 +02:00
|
|
|
|
|
|
|
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)`][stream-push] 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()`][stream-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
|
2016-02-02 18:34:29 +01:00
|
|
|
`size` bytes are available before calling [`stream.push(chunk)`][stream-push].
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `readable._read()` method is prefixed with an underscore because it is
|
2016-05-24 07:30:41 +02:00
|
|
|
internal to the class that defines it, and should never be called directly by
|
|
|
|
user programs.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### readable.push(chunk[, encoding])
|
2016-01-19 17:03:15 +01:00
|
|
|
|
|
|
|
* `chunk` {Buffer|Null|String} Chunk of data to push into the read queue
|
|
|
|
* `encoding` {String} Encoding of String chunks. Must be a valid
|
2015-11-05 20:54:10 +01:00
|
|
|
Buffer encoding, such as `'utf8'` or `'ascii'`
|
2016-05-24 07:30:41 +02:00
|
|
|
* Returns {Boolean} `true` if additional chunks of data may continued to be
|
|
|
|
pushed; `false` otherwise.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
When `chunk` is a `Buffer` 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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
When the Readable is operating in paused mode, the data added with
|
|
|
|
`readable.push()` can be read out by calling the
|
|
|
|
[`readable.read()`][stream-read] method when the [`'readable'`][] event is
|
|
|
|
emitted.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
When the Readable is operating in flowing mode, the data added with
|
|
|
|
`readable.push()` will be delivered by emitting a `'data'` event.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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:
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-11-05 20:54:10 +01:00
|
|
|
// 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.
|
|
|
|
|
2016-05-02 07:03:23 +02:00
|
|
|
class SourceWrapper extends Readable {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-02 07:03:23 +02:00
|
|
|
this._source = getLowlevelSourceObject();
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// Every time there's data, push it into the internal buffer.
|
2016-05-02 07:03:23 +02:00
|
|
|
this._source.ondata = (chunk) => {
|
2016-05-24 07:30:41 +02:00
|
|
|
// if push() returns false, then stop reading from source
|
2016-05-02 07:03:23 +02:00
|
|
|
if (!this.push(chunk))
|
|
|
|
this._source.readStop();
|
|
|
|
};
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// When the source ends, push the EOF-signaling `null` chunk
|
2016-05-02 07:03:23 +02:00
|
|
|
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();
|
|
|
|
}
|
2015-11-05 20:54:10 +01:00
|
|
|
}
|
|
|
|
```
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: The `readable.push()` method is intended be called only by Readable
|
2016-06-18 23:19:41 +02:00
|
|
|
Implemeters, and only from within the `readable._read()` method.
|
2016-05-24 07:30:41 +02:00
|
|
|
|
|
|
|
#### Errors While Reading
|
|
|
|
|
|
|
|
It is recommended that errors occurring during the processing of the
|
2016-06-18 23:19:41 +02:00
|
|
|
`readable._read()` method are emitted using the `'error'` event rather than
|
|
|
|
being thrown. Throwing an Error from within `readable._read()` can result in
|
2016-05-24 07:30:41 +02:00
|
|
|
expected 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.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Readable = require('stream').Readable;
|
|
|
|
|
|
|
|
const myReadable = new Readable({
|
|
|
|
read(size) {
|
|
|
|
if (checkSomeErrorCondition()) {
|
|
|
|
process.nextTick(() => this.emit('error', err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// do some work
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
#### An Example Counting Stream
|
2015-11-05 20:54:10 +01:00
|
|
|
|
|
|
|
<!--type=example-->
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The following is a basic example of a Readable stream that emits the numerals
|
2015-11-05 20:54:10 +01:00
|
|
|
from 1 to 1,000,000 in ascending order, and then ends.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-12-15 00:20:25 +01:00
|
|
|
const Readable = require('stream').Readable;
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-02 07:03:23 +02:00
|
|
|
class Counter extends Readable {
|
|
|
|
constructor(opt) {
|
|
|
|
super(opt);
|
|
|
|
this._max = 1000000;
|
|
|
|
this._index = 1;
|
|
|
|
}
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-02 07:03:23 +02:00
|
|
|
_read() {
|
|
|
|
var i = this._index++;
|
|
|
|
if (i > this._max)
|
|
|
|
this.push(null);
|
|
|
|
else {
|
|
|
|
var str = '' + i;
|
|
|
|
var buf = Buffer.from(str, 'ascii');
|
|
|
|
this.push(buf);
|
|
|
|
}
|
2013-07-16 01:56:02 +02:00
|
|
|
}
|
2016-05-02 07:03:23 +02:00
|
|
|
}
|
2013-07-16 01:56:02 +02:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### Implementing a Duplex Stream
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
A [Duplex][] stream is one that implements both [Readable][] and [Writable][],
|
|
|
|
such as a TCP socket connection.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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).
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: The `stream.Duplex` class prototypically inherits from `stream.Readable`
|
|
|
|
and parasitically from `stream.Writable`.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Custom Duplex streams *must* call the `new stream.Duplex([options])`
|
2016-06-18 23:19:41 +02:00
|
|
|
constructor and implement *both* the `readable._read()` and
|
|
|
|
`writable._write()` methods.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### new stream.Duplex(options)
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* `options` {Object} Passed to both Writable and Readable
|
|
|
|
constructors. Also has the following fields:
|
|
|
|
* `allowHalfOpen` {Boolean} Defaults to `true`. If set to `false`, then
|
|
|
|
the stream will automatically end the readable side when the
|
|
|
|
writable side ends and vice versa.
|
|
|
|
* `readableObjectMode` {Boolean} Defaults to `false`. Sets `objectMode`
|
|
|
|
for readable side of the stream. Has no effect if `objectMode`
|
|
|
|
is `true`.
|
|
|
|
* `writableObjectMode` {Boolean} Defaults to `false`. Sets `objectMode`
|
|
|
|
for writable side of the stream. Has no effect if `objectMode`
|
|
|
|
is `true`.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For example:
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Duplex = require('stream').Duplex;
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
class MyDuplex extends Duplex {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2013-03-01 00:42:55 +01:00
|
|
|
}
|
2016-05-02 07:03:23 +02:00
|
|
|
}
|
2013-03-01 00:42:55 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Or, when using pre-ES6 style constructors:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Duplex = require('stream').Duplex;
|
|
|
|
const util = require('util');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
function MyDuplex(options) {
|
|
|
|
if (!(this instanceof MyDuplex))
|
|
|
|
return new MyDuplex(options);
|
|
|
|
Duplex.call(this, options);
|
|
|
|
}
|
|
|
|
util.inherits(MyDuplex, Duplex);
|
|
|
|
```
|
2013-03-04 04:05:44 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Or, using the Simplified Constructor approach:
|
2014-09-03 15:01:15 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const Duplex = require('stream').Duplex;
|
2014-09-03 15:01:15 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
const myDuplex = new Duplex({
|
|
|
|
read(size) {
|
|
|
|
// ...
|
|
|
|
},
|
|
|
|
write(chunk, encoding, callback) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
});
|
2014-09-03 15:01:15 +02:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### An Example Duplex Stream
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Duplex = require('stream').Duplex;
|
|
|
|
const kSource = Symbol('source');
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
class MyDuplex extends Duplex {
|
|
|
|
constructor(source, options) {
|
2016-05-02 07:03:23 +02:00
|
|
|
super(options);
|
2016-05-24 07:30:41 +02:00
|
|
|
this[kSource] = source;
|
2016-05-02 07:03:23 +02:00
|
|
|
}
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
_write(chunk, encoding, callback) {
|
|
|
|
// The underlying source only deals with strings
|
|
|
|
if (Buffer.isBuffer(chunk))
|
|
|
|
chunk = chunk.toString(encoding);
|
|
|
|
this[kSource].writeSomeData(chunk, encoding);
|
|
|
|
callback();
|
|
|
|
}
|
2013-03-01 00:42:55 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
_read(size) {
|
|
|
|
this[kSource].fetchSomeData(size, (data, encoding) => {
|
|
|
|
this.push(Buffer.from(data, encoding));
|
|
|
|
});
|
2013-03-01 00:42:55 +01:00
|
|
|
}
|
2016-05-02 07:03:23 +02:00
|
|
|
}
|
2013-03-01 00:42:55 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The most important aspect of a Duplex stream is that the Readable and Writable
|
|
|
|
sides operate independently of one another despite co-existing within a single
|
|
|
|
object instance.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Object Mode Duplex Streams
|
2011-02-19 00:30:15 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For Duplex streams, `objectMode` can be set exclusively for either the Readable
|
|
|
|
or Writable side using the `readableObjectMode` and `writableObjectMode` options
|
|
|
|
respectively.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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 hexidecimal strings on
|
|
|
|
the Readable side.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Transform = require('stream').Transform;
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// All Transform streams are also Duplex Streams
|
|
|
|
const myTransform = new Transform({
|
|
|
|
writableObjectMode: true,
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
transform(chunk, encoding, callback) {
|
|
|
|
// Coerce the chunk to a number if necessary
|
|
|
|
chunk |= 0;
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// Transform the chunk into something else.
|
|
|
|
const data = chunk.toString(16);
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
// Push the data onto the readable queue.
|
|
|
|
callback(null, '0'.repeat(data.length % 2) + data);
|
|
|
|
}
|
|
|
|
});
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
myTransform.setEncoding('ascii');
|
|
|
|
myTransform.on('data', (chunk) => console.log(chunk));
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
myTransform.write(1);
|
|
|
|
// Prints: 01
|
|
|
|
myTransform.write(10);
|
|
|
|
// Prints: 0a
|
|
|
|
myTransform.write(100);
|
|
|
|
// Prints: 64
|
|
|
|
```
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### Implementing a Transform Stream
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream.Transform` class is extended to implement a [Transform][] stream.
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The `stream.Transform` class prototypically inherits from `stream.Duplex` and
|
2016-06-18 23:19:41 +02:00
|
|
|
implements its own versions of the `writable._write()` and `readable._read()`
|
2016-05-24 07:30:41 +02:00
|
|
|
methods. Custom Transform implementations *must* implement the
|
2016-06-18 23:19:41 +02:00
|
|
|
[`transform._transform()`][stream-_transform] method and *may* also implement
|
|
|
|
the [`transform._flush()`][stream-_flush] method.
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*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.
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### new stream.Transform([options])
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* `options` {Object} Passed to both Writable and Readable
|
|
|
|
constructors. Also has the following fields:
|
|
|
|
* `transform` {Function} Implementation for the
|
|
|
|
[`stream._transform()`][stream-_transform] method.
|
|
|
|
* `flush` {Function} Implementation for the [`stream._flush()`][stream-_flush]
|
|
|
|
method.
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
For example:
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const Transform = require('stream').Transform;
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
class MyTransform extends Transform {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2015-02-03 02:12:41 +01:00
|
|
|
}
|
2016-05-24 07:30:41 +02:00
|
|
|
}
|
|
|
|
```
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Or, when using pre-ES6 style constructors:
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
const Transform = require('stream').Transform;
|
|
|
|
const util = require('util');
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
function MyTransform(options) {
|
|
|
|
if (!(this instanceof MyTransform))
|
|
|
|
return new MyTransform(options);
|
|
|
|
Transform.call(this, options);
|
|
|
|
}
|
|
|
|
util.inherits(MyTransform, Transform);
|
2015-02-03 02:12:41 +01:00
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Or, using the Simplified Constructor approach:
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
```js
|
2016-05-24 07:30:41 +02:00
|
|
|
const Transform = require('stream').Transform;
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
const myTransform = new Transform({
|
|
|
|
transform(chunk, encoding, callback) {
|
|
|
|
// ...
|
2015-11-05 20:54:10 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Events: 'finish' and 'end'
|
2016-01-17 18:39:07 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
The [`'finish'`][] and [`'end'`][] events are from the `stream.Writable`
|
|
|
|
and `stream.Readable` classes, respectively. The `'finish'` event is emitted
|
|
|
|
after [`stream.end()`][stream-end] is called and all chunks have been processed
|
|
|
|
by [`stream._transform()`][stream-_transform]. The `'end'` event is emitted
|
|
|
|
after all data has been output, which occurs after the callback in
|
|
|
|
[`transform._flush()`][stream-_flush] has been called.
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### transform.\_flush(callback)
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* `callback` {Function} A callback function (optionally with an error
|
2016-06-07 22:54:51 +02:00
|
|
|
argument and data) to be called when remaining data has been flushed.
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: **This function MUST NOT be called by application code directly.** It
|
|
|
|
should be implemented by child classes, and called only by the internal Readable
|
|
|
|
class methods only.
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2015-02-03 02:12:41 +01:00
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
Custom [Transform][] implementations *may* implement the `transform._flush()`
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2016-01-17 18:39:07 +01:00
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
Within the `transform._flush()` implementation, the `readable.push()` method
|
2016-05-24 07:30:41 +02:00
|
|
|
may be called zero or more times, as appropriate. The `callback` function must
|
|
|
|
be called when the flush operation is complete.
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `transform._flush()` method is prefixed with an underscore because it is
|
2016-05-24 07:30:41 +02:00
|
|
|
internal to the class that defines it, and should never be called directly by
|
|
|
|
user programs.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### transform.\_transform(chunk, encoding, callback)
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* `chunk` {Buffer|String} The chunk to be transformed. Will **always**
|
|
|
|
be a buffer unless the `decodeStrings` option was set to `false`.
|
|
|
|
* `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.
|
2015-10-30 11:59:21 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
*Note*: **This function MUST NOT be called by application code directly.** It
|
|
|
|
should be implemented by child classes, and called only by the internal Readable
|
|
|
|
class methods only.
|
|
|
|
|
|
|
|
All Transform stream implementations must provide a `_transform()`
|
2016-06-18 23:19:41 +02:00
|
|
|
method to accept input and produce output. The `transform._transform()`
|
2016-05-24 07:30:41 +02:00
|
|
|
implementation handles the bytes being written, computes an output, then passes
|
|
|
|
that output off to the readable portion using the `readable.push()` method.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
It is possible that no output is generated from any given chunk of input data.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
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:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
```js
|
|
|
|
transform.prototype._transform = function (data, encoding, callback) {
|
|
|
|
this.push(data);
|
|
|
|
callback();
|
|
|
|
};
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
transform.prototype._transform = function (data, encoding, callback) {
|
|
|
|
callback(null, data);
|
|
|
|
};
|
|
|
|
```
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-06-18 23:19:41 +02:00
|
|
|
The `transform._transform()` method is prefixed with an underscore because it
|
2016-05-24 07:30:41 +02:00
|
|
|
is internal to the class that defines it, and should never be called directly by
|
|
|
|
user programs.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
#### Class: stream.PassThrough
|
|
|
|
|
|
|
|
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.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
## Additional Notes
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
<!--type=misc-->
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
### Compatibility with Older Node.js Versions
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
In versions of Node.js prior to v0.10, the Readable stream interface was
|
2013-07-16 01:56:02 +02:00
|
|
|
simpler, but also less powerful and less useful.
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* Rather than waiting for calls the [`stream.read()`][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.
|
2016-02-02 18:34:29 +01:00
|
|
|
* The [`stream.pause()`][stream-pause] method was advisory, rather than
|
2016-05-24 07:30:41 +02:00
|
|
|
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()`][stream-resume] method is called. The effect is that, even
|
|
|
|
when not using the new [`stream.read()`][stream-read] method and
|
|
|
|
[`'readable'`][] event, it is no longer necessary to worry about losing
|
2016-02-02 18:34:29 +01:00
|
|
|
[`'data'`][] chunks.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
While most applications will continue to function normally, this introduces an
|
|
|
|
edge case in the following conditions:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
* No [`'data'`][] event listener is added.
|
2016-02-02 18:34:29 +01:00
|
|
|
* The [`stream.resume()`][stream-resume] method is never called.
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
* The stream is not piped to any writable destination.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
For example, consider the following code:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2013-07-16 01:56:02 +02:00
|
|
|
// WARNING! BROKEN!
|
2015-12-15 00:20:25 +01:00
|
|
|
net.createServer((socket) => {
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
// we add an 'end' method, but never consume the data
|
2015-12-15 00:20:25 +01:00
|
|
|
socket.on('end', () => {
|
2013-07-16 01:56:02 +02:00
|
|
|
// It will never get here.
|
2016-05-24 07:30:41 +02:00
|
|
|
socket.end('The message was received but was not processed.\n');
|
2013-07-16 01:56:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}).listen(1337);
|
|
|
|
```
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
In versions of Node.js prior to v0.10, the incoming message data would be
|
2016-05-24 07:30:41 +02:00
|
|
|
simply discarded. However, in Node.js v0.10 and beyond, the socket remains
|
|
|
|
paused forever.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-02-02 18:34:29 +01:00
|
|
|
The workaround in this situation is to call the
|
2016-05-24 07:30:41 +02:00
|
|
|
[`stream.resume()`][stream-resume] method to begin the flow of data:
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2013-07-16 01:56:02 +02:00
|
|
|
// Workaround
|
2015-12-15 00:20:25 +01:00
|
|
|
net.createServer((socket) => {
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
socket.on('end', () => {
|
2016-05-24 07:30:41 +02:00
|
|
|
socket.end('The message was received but was not processed.\n');
|
2013-07-16 01:56:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// start the flow of data, discarding it.
|
|
|
|
socket.resume();
|
|
|
|
|
|
|
|
}).listen(1337);
|
|
|
|
```
|
|
|
|
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 03:24:02 +02:00
|
|
|
In addition to new Readable streams switching into flowing mode,
|
|
|
|
pre-v0.10 style streams can be wrapped in a Readable class using the
|
2016-06-18 23:19:41 +02:00
|
|
|
[`readable.wrap()`][`stream.wrap()`] method.
|
2013-07-16 01:56:02 +02:00
|
|
|
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### `readable.read(0)`
|
2013-07-16 01:56:02 +02:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
There are some cases where it is necessary to trigger a refresh of the
|
2015-11-05 20:54:10 +01:00
|
|
|
underlying readable stream mechanisms, without actually consuming any
|
2016-05-24 07:30:41 +02:00
|
|
|
data. In such cases, it is possible to call `readable.read(0)`, which will
|
2016-06-07 01:31:49 +02:00
|
|
|
always return `null`.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
|
|
|
If the internal read buffer is below the `highWaterMark`, and the
|
2016-02-02 18:34:29 +01:00
|
|
|
stream is not currently reading, then calling `stream.read(0)` will trigger
|
|
|
|
a low-level [`stream._read()`][stream-_read] call.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
While most applications will almost never need to do this, there are
|
|
|
|
situations within Node.js where this is done, particularly in the
|
2015-11-05 20:54:10 +01:00
|
|
|
Readable stream class internals.
|
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
### `readable.push('')`
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Use of `readable.push('')` is not recommended.
|
2015-11-05 20:54:10 +01:00
|
|
|
|
2016-05-24 07:30:41 +02:00
|
|
|
Pushing a zero-byte string or `Buffer` to a stream that is not in object mode
|
|
|
|
has an interesting side effect. Because it *is* a call to
|
|
|
|
[`readable.push()`][stream-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.
|
2012-06-06 21:05:18 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'data'`]: #stream_event_data
|
2016-02-02 18:34:29 +01:00
|
|
|
[`'drain'`]: #stream_event_drain
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'end'`]: #stream_event_end
|
|
|
|
[`'finish'`]: #stream_event_finish
|
2016-02-02 18:34:29 +01:00
|
|
|
[`'readable'`]: #stream_event_readable
|
|
|
|
[`buf.toString(encoding)`]: buffer.html#buffer_buf_tostring_encoding_start_end
|
2016-01-31 15:44:29 +01:00
|
|
|
[`EventEmitter`]: events.html#events_class_eventemitter
|
2015-11-28 00:30:32 +01:00
|
|
|
[`process.stderr`]: process.html#process_process_stderr
|
|
|
|
[`process.stdin`]: process.html#process_process_stdin
|
|
|
|
[`process.stdout`]: process.html#process_process_stdout
|
2016-02-02 18:34:29 +01:00
|
|
|
[`stream.cork()`]: #stream_writable_cork
|
|
|
|
[`stream.pipe()`]: #stream_readable_pipe_destination_options
|
|
|
|
[`stream.uncork()`]: #stream_writable_uncork
|
|
|
|
[`stream.unpipe()`]: #stream_readable_unpipe_destination
|
|
|
|
[`stream.wrap()`]: #stream_readable_wrap_stream
|
2015-11-28 00:30:32 +01:00
|
|
|
[`tls.CryptoStream`]: tls.html#tls_class_cryptostream
|
2013-07-16 01:56:02 +02:00
|
|
|
[API for Stream Consumers]: #stream_api_for_stream_consumers
|
2016-05-24 07:30:41 +02:00
|
|
|
[API for Stream Implemeters]: #stream_api_for_stream_Implemeters
|
2015-11-28 00:30:32 +01:00
|
|
|
[child process stdin]: child_process.html#child_process_child_stdin
|
|
|
|
[child process stdout and stderr]: child_process.html#child_process_child_stdout
|
2016-01-05 11:49:54 +01:00
|
|
|
[Compatibility]: #stream_compatibility_with_older_node_js_versions
|
2015-11-28 00:30:32 +01:00
|
|
|
[crypto]: crypto.html
|
2013-07-16 01:56:02 +02:00
|
|
|
[Duplex]: #stream_class_stream_duplex
|
2015-11-28 00:30:32 +01:00
|
|
|
[fs read streams]: fs.html#fs_class_fs_readstream
|
|
|
|
[fs write streams]: fs.html#fs_class_fs_writestream
|
2016-05-24 07:30:41 +02:00
|
|
|
[`fs.createReadStream()`]: fs.html#fs_fs_createreadstream_path_options
|
|
|
|
[`fs.createWriteStream()`]: fs.html#fs_fs_createwritestream_path_options
|
|
|
|
[`net.Socket`]: net.html#net_class_net_socket
|
|
|
|
[`zlib.createDeflate()`]: zlib.html#zlib_zlib_createdeflate_options
|
2016-02-02 18:34:29 +01:00
|
|
|
[HTTP requests, on the client]: http.html#http_class_http_clientrequest
|
|
|
|
[HTTP responses, on the server]: http.html#http_class_http_serverresponse
|
|
|
|
[http-incoming-message]: http.html#http_class_http_incomingmessage
|
2015-11-28 00:30:32 +01:00
|
|
|
[Object mode]: #stream_object_mode
|
|
|
|
[Readable]: #stream_class_stream_readable
|
2016-01-05 11:49:54 +01:00
|
|
|
[SimpleProtocol v2]: #stream_example_simpleprotocol_parser_v2
|
2016-02-02 18:34:29 +01:00
|
|
|
[stream-_flush]: #stream_transform_flush_callback
|
|
|
|
[stream-_read]: #stream_readable_read_size_1
|
|
|
|
[stream-_transform]: #stream_transform_transform_chunk_encoding_callback
|
|
|
|
[stream-_write]: #stream_writable_write_chunk_encoding_callback_1
|
|
|
|
[stream-_writev]: #stream_writable_writev_chunks_callback
|
|
|
|
[stream-end]: #stream_writable_end_chunk_encoding_callback
|
|
|
|
[stream-pause]: #stream_readable_pause
|
|
|
|
[stream-push]: #stream_readable_push_chunk_encoding
|
|
|
|
[stream-read]: #stream_readable_read_size
|
|
|
|
[stream-resume]: #stream_readable_resume
|
|
|
|
[stream-write]: #stream_writable_write_chunk_encoding_callback
|
|
|
|
[TCP sockets]: net.html#net_class_net_socket
|
2013-07-16 01:56:02 +02:00
|
|
|
[Transform]: #stream_class_stream_transform
|
2015-11-28 00:30:32 +01:00
|
|
|
[Writable]: #stream_class_stream_writable
|
|
|
|
[zlib]: zlib.html
|