{ "source": "doc/api/stream.markdown", "modules": [ { "textRaw": "Stream", "name": "stream", "stability": 2, "stabilityText": "Unstable", "desc": "

A stream is an abstract interface implemented by various objects in\nNode. For example a request to an HTTP server is a stream, as is\nstdout. Streams are readable, writable, or both. All streams are\ninstances of [EventEmitter][]\n\n

\n

You can load up the Stream base class by doing require('stream').\n\n

\n", "classes": [ { "textRaw": "Readable Stream", "name": "Readable Stream", "type": "class", "desc": "

A Readable Stream has the following methods, members, and events.\n\n

\n", "events": [ { "textRaw": "Event: 'data'", "type": "event", "name": "data", "desc": "

function (data) { }\n\n

\n

The 'data' event emits either a Buffer (by default) or a string if\nsetEncoding() was used.\n\n

\n

Note that the data will be lost if there is no listener when a\nReadable Stream emits a 'data' event.\n\n

\n", "params": [] }, { "textRaw": "Event: 'end'", "type": "event", "name": "end", "desc": "

function () { }\n\n

\n

Emitted when the stream has received an EOF (FIN in TCP terminology).\nIndicates that no more 'data' events will happen. If the stream is\nalso writable, it may be possible to continue writing.\n\n

\n", "params": [] }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "desc": "

function (exception) { }\n\n

\n

Emitted if there was an error receiving data.\n\n

\n", "params": [] }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "desc": "

function () { }\n\n

\n

Emitted when the underlying resource (for example, the backing file\ndescriptor) has been closed. Not all streams will emit this.\n\n

\n", "params": [] } ], "properties": [ { "textRaw": "stream.readable", "name": "readable", "desc": "

A boolean that is true by default, but turns false after an\n'error' occurred, the stream came to an 'end', or destroy() was\ncalled.\n\n

\n" } ], "methods": [ { "textRaw": "stream.setEncoding([encoding])", "type": "method", "name": "setEncoding", "desc": "

Makes the 'data' event emit a string instead of a Buffer. encoding\ncan be 'utf8', 'utf16le' ('ucs2'), 'ascii', or 'hex'. Defaults\nto 'utf8'.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "stream.pause()", "type": "method", "name": "pause", "desc": "

Issues an advisory signal to the underlying communication layer,\nrequesting that no further data be sent until resume() is called.\n\n

\n

Note that, due to the advisory nature, certain streams will not be\npaused immediately, and so 'data' events may be emitted for some\nindeterminate period of time even after pause() is called. You may\nwish to buffer such 'data' events.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "stream.resume()", "type": "method", "name": "resume", "desc": "

Resumes the incoming 'data' events after a pause().\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "stream.destroy()", "type": "method", "name": "destroy", "desc": "

Closes the underlying file descriptor. Stream is no longer writable\nnor readable. The stream will not emit any more 'data', or 'end'\nevents. Any queued write data will not be sent. The stream should emit\n'close' event once its resources have been disposed of.\n\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "stream.pipe(destination, [options])", "type": "method", "name": "pipe", "desc": "

This is a Stream.prototype method available on all Streams.\n\n

\n

Connects this read stream to destination WriteStream. Incoming data on\nthis stream gets written to destination. The destination and source\nstreams are kept in sync by pausing and resuming as necessary.\n\n

\n

This function returns the destination stream.\n\n

\n

Emulating the Unix cat command:\n\n

\n
process.stdin.resume(); process.stdin.pipe(process.stdout);
\n

By default end() is called on the destination when the source stream\nemits end, so that destination is no longer writable. Pass { end:\nfalse } as options to keep the destination stream open.\n\n

\n

This keeps process.stdout open so that "Goodbye" can be written at the\nend.\n\n

\n
process.stdin.resume();\n\nprocess.stdin.pipe(process.stdout, { end: false });\n\nprocess.stdin.on("end", function() {\nprocess.stdout.write("Goodbye\\n"); });
\n", "signatures": [ { "params": [ { "name": "destination" }, { "name": "options", "optional": true } ] } ] } ] }, { "textRaw": "Writable Stream", "name": "Writable Stream", "type": "class", "desc": "

A Writable Stream has the following methods, members, and events.\n\n

\n", "events": [ { "textRaw": "Event: 'drain'", "type": "event", "name": "drain", "desc": "

function () { }\n\n

\n

After a write() method returned false, this event is emitted to\nindicate that it is safe to write again.\n\n

\n", "params": [] }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "desc": "

function (exception) { }\n\n

\n

Emitted on error with the exception exception.\n\n

\n", "params": [] }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "desc": "

function () { }\n\n

\n

Emitted when the underlying file descriptor has been closed.\n\n

\n", "params": [] }, { "textRaw": "Event: 'pipe'", "type": "event", "name": "pipe", "desc": "

function (src) { }\n\n

\n

Emitted when the stream is passed to a readable stream's pipe method.\n\n

\n", "params": [] } ], "properties": [ { "textRaw": "stream.writable", "name": "writable", "desc": "

A boolean that is true by default, but turns false after an\n'error' occurred or end() / destroy() was called.\n\n

\n" } ], "methods": [ { "textRaw": "stream.write(string, [encoding], [fd])", "type": "method", "name": "write", "desc": "

Writes string with the given encoding to the stream. Returns true\nif the string has been flushed to the kernel buffer. Returns false to\nindicate that the kernel buffer is full, and the data will be sent out\nin the future. The 'drain' event will indicate when the kernel buffer\nis empty again. The encoding defaults to 'utf8'.\n\n

\n

If the optional fd parameter is specified, it is interpreted as an\nintegral file descriptor to be sent over the stream. This is only\nsupported for UNIX streams, and is silently ignored otherwise. When\nwriting a file descriptor in this manner, closing the descriptor before\nthe stream drains risks sending an invalid (closed) FD.\n\n

\n", "signatures": [ { "params": [ { "name": "string" }, { "name": "encoding", "optional": true }, { "name": "fd", "optional": true } ] } ] }, { "textRaw": "stream.write(buffer)", "type": "method", "name": "write", "desc": "

Same as the above except with a raw buffer.\n\n

\n", "signatures": [ { "params": [ { "name": "buffer" } ] } ] }, { "textRaw": "stream.end()", "type": "method", "name": "end", "desc": "

Terminates the stream with EOF or FIN. This call will allow queued\nwrite data to be sent before closing the stream.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "stream.end(string, encoding)", "type": "method", "name": "end", "desc": "

Sends string with the given encoding and terminates the stream with\nEOF or FIN. This is useful to reduce the number of packets sent.\n\n

\n", "signatures": [ { "params": [ { "name": "string" }, { "name": "encoding" } ] } ] }, { "textRaw": "stream.end(buffer)", "type": "method", "name": "end", "desc": "

Same as above but with a buffer.\n\n

\n", "signatures": [ { "params": [ { "name": "buffer" } ] } ] }, { "textRaw": "stream.destroy()", "type": "method", "name": "destroy", "desc": "

Closes the underlying file descriptor. Stream is no longer writable\nnor readable. The stream will not emit any more 'data', or 'end'\nevents. Any queued write data will not be sent. The stream should emit\n'close' event once its resources have been disposed of.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "stream.destroySoon()", "type": "method", "name": "destroySoon", "desc": "

After the write queue is drained, close the file descriptor.\ndestroySoon() can still destroy straight away, as long as there is no\ndata left in the queue for writes.\n\n

\n", "signatures": [ { "params": [] } ] } ] } ], "type": "module", "displayName": "Stream" } ] }