From 89523b23b2d9b41f27907efa5906854f6e130018 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Fri, 6 Apr 2018 13:43:54 +0200 Subject: [PATCH] stream: add docs --- doc/api/errors.md | 2 +- doc/api/stream.md | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 821fd6fd61691d..e6251f6f682a60 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1426,7 +1426,7 @@ An attempt was made to call [`stream.write()`][] with a `null` chunk. ### ERR_STREAM_PREMATURE_CLOSE -An error returned by `stream.onEnd` and `stream.pipeline`, when a stream +An error returned by `stream.finished()` and `stream.pipeline()`, when a stream or a pipeline ends non gracefully with no explicit error. diff --git a/doc/api/stream.md b/doc/api/stream.md index 86286906b7f297..723b25f7517ee2 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -46,6 +46,9 @@ There are four fundamental stream types within Node.js: * [Transform][] - Duplex streams that can modify or transform the data as it is written and read (for example [`zlib.createDeflate()`][]). +Additionally this module includes the utility functions [pipeline][] and +[finished][]. + ### Object Mode All streams created by Node.js APIs operate exclusively on strings and `Buffer` @@ -1283,6 +1286,107 @@ implementors should not override this method, but instead implement [`readable._destroy`][readable-_destroy]. The default implementation of `_destroy` for `Transform` also emit `'close'`. +### stream.finished(stream, callback) + + +* `stream` {Stream} A readable and/or writable stream. +* `callback` {Function} A callback function that takes an optional error + argument. + +A function to get notified when a stream is no longer readable, writable +or has experienced an error or a premature close event. + +```js +const { finished } = require('stream'); + +const rs = fs.createReadStream('archive.tar'); + +finished(rs, (err) => { + if (err) { + console.error('Stream failed', err); + } else { + console.log('Stream is done reading'); + } +}); + +rs.resume(); // drain the stream +``` + +Especially useful in error handling scenarios where a stream is destroyed +prematurely (like an aborted HTTP request), and will not emit `'end'` +or `'finish'`. + +The `finished` API is promisify'able as well; + +```js +const finished = util.promisify(stream.finished); + +const rs = fs.createReadStream('archive.tar'); + +async function run() { + await finished(rs); + console.log('Stream is done reading'); +} + +run().catch(console.error); +rs.resume(); // drain the stream +``` + +### stream.pipeline(...streams[, callback]) + + +* `...streams` {Stream} Two or more streams to pipe between. +* `callback` {Function} A callback function that takes an optional error + argument. + +A module method to pipe between streams forwarding errors and properly cleaning +up and provide a callback when the pipeline is complete. + +```js +const { pipeline } = require('stream'); +const fs = require('fs'); +const zlib = require('zlib'); + +// Use the pipeline API to easily pipe a series of streams +// together and get notified when the pipeline is fully done. + +// A pipeline to gzip a potentially huge tar file efficiently: + +pipeline( + fs.createReadStream('archive.tar'), + zlib.createGzip(), + fs.createWriteStream('archive.tar.gz'), + (err) => { + if (err) { + console.error('Pipeline failed', err); + } else { + console.log('Pipeline succeeded'); + } + } +); +``` + +The `pipeline` API is promisify'able as well: + +```js +const pipeline = util.promisify(stream.pipeline); + +async function run() { + await pipeline( + fs.createReadStream('archive.tar'), + zlib.createGzip(), + fs.createWriteStream('archive.tar.gz') + ); + console.log('Pipeline succeeded'); +} + +run().catch(console.error); +``` + ## API for Stream Implementers @@ -2395,6 +2499,8 @@ contain multi-byte characters. [http-incoming-message]: http.html#http_class_http_incomingmessage [zlib]: zlib.html [hwm-gotcha]: #stream_highwatermark_discrepancy_after_calling_readable_setencoding +[pipeline]: #stream_stream_pipeline_streams_callback +[finished]: #stream_stream_finished_stream_callback [stream-_flush]: #stream_transform_flush_callback [stream-_read]: #stream_readable_read_size_1 [stream-_transform]: #stream_transform_transform_chunk_encoding_callback