2012-02-27 20:18:10 +01:00
|
|
|
# Stream
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 2 - Unstable
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
A stream is an abstract interface implemented by various objects in
|
|
|
|
Node. For example a request to an HTTP server is a stream, as is
|
|
|
|
stdout. Streams are readable, writable, or both. All streams are
|
|
|
|
instances of [EventEmitter][]
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:18:10 +01:00
|
|
|
You can load up the Stream base class by doing `require('stream')`.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
## Readable Stream
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
<!--type=class-->
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
A `Readable Stream` has the following methods, members, and events.
|
|
|
|
|
|
|
|
### Event: 'data'
|
|
|
|
|
|
|
|
`function (data) { }`
|
|
|
|
|
|
|
|
The `'data'` event emits either a `Buffer` (by default) or a string if
|
|
|
|
`setEncoding()` was used.
|
|
|
|
|
2012-02-05 11:11:54 +01:00
|
|
|
Note that the __data will be lost__ if there is no listener when a
|
|
|
|
`Readable Stream` emits a `'data'` event.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### Event: 'end'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
|
|
|
Emitted when the stream has received an EOF (FIN in TCP terminology).
|
2012-07-07 18:53:33 +02:00
|
|
|
Indicates that no more `'data'` events will happen. If the stream is
|
|
|
|
also writable, it may be possible to continue writing.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### Event: 'error'
|
|
|
|
|
|
|
|
`function (exception) { }`
|
|
|
|
|
|
|
|
Emitted if there was an error receiving data.
|
|
|
|
|
|
|
|
### Event: 'close'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Emitted when the underlying resource (for example, the backing file
|
|
|
|
descriptor) has been closed. Not all streams will emit this.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.readable
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
A boolean that is `true` by default, but turns `false` after an
|
|
|
|
`'error'` occurred, the stream came to an `'end'`, or `destroy()` was
|
|
|
|
called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-05-05 16:10:36 +02:00
|
|
|
### stream.setEncoding([encoding])
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Makes the `'data'` event emit a string instead of a `Buffer`. `encoding`
|
|
|
|
can be `'utf8'`, `'utf16le'` (`'ucs2'`), `'ascii'`, or `'hex'`. Defaults
|
|
|
|
to `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.pause()
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Issues an advisory signal to the underlying communication layer,
|
|
|
|
requesting that no further data be sent until `resume()` is called.
|
2012-04-09 07:46:55 +02:00
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Note that, due to the advisory nature, certain streams will not be
|
|
|
|
paused immediately, and so `'data'` events may be emitted for some
|
|
|
|
indeterminate period of time even after `pause()` is called. You may
|
|
|
|
wish to buffer such `'data'` events.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.resume()
|
|
|
|
|
|
|
|
Resumes the incoming `'data'` events after a `pause()`.
|
|
|
|
|
|
|
|
### stream.destroy()
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Closes the underlying file descriptor. Stream is no longer `writable`
|
|
|
|
nor `readable`. The stream will not emit any more 'data', or 'end'
|
|
|
|
events. Any queued write data will not be sent. The stream should emit
|
|
|
|
'close' event once its resources have been disposed of.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-02 01:41:39 +01:00
|
|
|
### stream.pipe(destination, [options])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 09:41:44 +01:00
|
|
|
This is a `Stream.prototype` method available on all `Stream`s.
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Connects this read stream to `destination` WriteStream. Incoming data on
|
|
|
|
this stream gets written to `destination`. The destination and source
|
2010-11-18 09:41:44 +01:00
|
|
|
streams are kept in sync by pausing and resuming as necessary.
|
|
|
|
|
2011-07-11 07:35:25 +02:00
|
|
|
This function returns the `destination` stream.
|
|
|
|
|
2010-11-18 09:41:44 +01:00
|
|
|
Emulating the Unix `cat` command:
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
process.stdin.resume(); process.stdin.pipe(process.stdout);
|
2010-11-18 09:41:44 +01:00
|
|
|
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
By default `end()` is called on the destination when the source stream
|
|
|
|
emits `end`, so that `destination` is no longer writable. Pass `{ end:
|
|
|
|
false }` as `options` to keep the destination stream open.
|
2010-11-18 09:41:44 +01:00
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
This keeps `process.stdout` open so that "Goodbye" can be written at the
|
|
|
|
end.
|
2010-11-18 09:41:44 +01:00
|
|
|
|
2011-01-02 06:54:46 +01:00
|
|
|
process.stdin.resume();
|
|
|
|
|
|
|
|
process.stdin.pipe(process.stdout, { end: false });
|
|
|
|
|
|
|
|
process.stdin.on("end", function() {
|
2012-07-07 18:53:33 +02:00
|
|
|
process.stdout.write("Goodbye\n"); });
|
2010-11-18 09:41:44 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
## Writable Stream
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
<!--type=class-->
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
A `Writable Stream` has the following methods, members, and events.
|
|
|
|
|
|
|
|
### Event: 'drain'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
After a `write()` method returned `false`, this event is emitted to
|
2010-10-28 14:18:16 +02:00
|
|
|
indicate that it is safe to write again.
|
|
|
|
|
|
|
|
### Event: 'error'
|
|
|
|
|
|
|
|
`function (exception) { }`
|
|
|
|
|
|
|
|
Emitted on error with the exception `exception`.
|
|
|
|
|
|
|
|
### Event: 'close'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
|
|
|
Emitted when the underlying file descriptor has been closed.
|
|
|
|
|
2011-02-10 08:02:51 +01:00
|
|
|
### Event: 'pipe'
|
|
|
|
|
|
|
|
`function (src) { }`
|
|
|
|
|
|
|
|
Emitted when the stream is passed to a readable stream's pipe method.
|
|
|
|
|
2011-01-11 19:18:46 +01:00
|
|
|
### stream.writable
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
A boolean that is `true` by default, but turns `false` after an
|
|
|
|
`'error'` occurred or `end()` / `destroy()` was called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-12-27 09:43:58 +01:00
|
|
|
### stream.write(string, [encoding], [fd])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Writes `string` with the given `encoding` to the stream. Returns `true`
|
|
|
|
if the string has been flushed to the kernel buffer. Returns `false` to
|
|
|
|
indicate that the kernel buffer is full, and the data will be sent out
|
|
|
|
in the future. The `'drain'` event will indicate when the kernel buffer
|
|
|
|
is empty again. The `encoding` defaults to `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
If the optional `fd` parameter is specified, it is interpreted as an
|
|
|
|
integral file descriptor to be sent over the stream. This is only
|
|
|
|
supported for UNIX streams, and is silently ignored otherwise. When
|
|
|
|
writing a file descriptor in this manner, closing the descriptor before
|
|
|
|
the stream drains risks sending an invalid (closed) FD.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.write(buffer)
|
|
|
|
|
|
|
|
Same as the above except with a raw buffer.
|
|
|
|
|
|
|
|
### stream.end()
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Terminates the stream with EOF or FIN. This call will allow queued
|
|
|
|
write data to be sent before closing the stream.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.end(string, encoding)
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Sends `string` with the given `encoding` and terminates the stream with
|
|
|
|
EOF or FIN. This is useful to reduce the number of packets sent.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### stream.end(buffer)
|
|
|
|
|
|
|
|
Same as above but with a `buffer`.
|
|
|
|
|
|
|
|
### stream.destroy()
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
Closes the underlying file descriptor. Stream is no longer `writable`
|
|
|
|
nor `readable`. The stream will not emit any more 'data', or 'end'
|
|
|
|
events. Any queued write data will not be sent. The stream should emit
|
|
|
|
'close' event once its resources have been disposed of.
|
2011-02-19 00:30:15 +01:00
|
|
|
|
|
|
|
### stream.destroySoon()
|
|
|
|
|
2012-07-07 18:53:33 +02:00
|
|
|
After the write queue is drained, close the file descriptor.
|
|
|
|
`destroySoon()` can still destroy straight away, as long as there is no
|
|
|
|
data left in the queue for writes.
|
2012-06-06 21:05:18 +02:00
|
|
|
|
|
|
|
[EventEmitter]: events.html#events_class_events_eventemitter
|