@@ -46,6 +46,9 @@ There are four fundamental stream types within Node.js:
4646* [ Transform] [ ] - Duplex streams that can modify or transform the data as it
4747 is written and read (for example [ ` zlib.createDeflate() ` ] [ ] ).
4848
49+ Additionally this module includes the utility functions [ pipeline] [ ] and
50+ [ finished] [ ] .
51+
4952### Object Mode
5053
5154All 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
12831286[ ` readable._destroy ` ] [ readable-_destroy ] .
12841287The default implementation of ` _destroy ` for ` Transform ` also emit ` 'close' ` .
12851288
1289+ ### stream.finished(stream, callback)
1290+ <!-- YAML
1291+ added: REPLACEME
1292+ -->
1293+
1294+ * ` stream ` {Stream} A readable and/or writable stream.
1295+ * ` callback ` {Function} A callback function that takes an optional error
1296+ argument.
1297+
1298+ A function to get notified when a stream is no longer readable, writable
1299+ or has experienced an error or a premature close event.
1300+
1301+ ``` js
1302+ const { finished } = require (' stream' );
1303+
1304+ const rs = fs .createReadStream (' archive.tar' );
1305+
1306+ finished (rs, (err ) => {
1307+ if (err) {
1308+ console .error (' Stream failed' , err);
1309+ } else {
1310+ console .log (' Stream is done reading' );
1311+ }
1312+ });
1313+
1314+ rs .resume (); // drain the stream
1315+ ```
1316+
1317+ Especially useful in error handling scenarios where a stream is destroyed
1318+ prematurely (like an aborted HTTP request), and will not emit ` 'end' `
1319+ or ` 'finish' ` .
1320+
1321+ The ` finished ` API is promisify'able as well;
1322+
1323+ ``` js
1324+ const finished = util .promisify (stream .finished );
1325+
1326+ const rs = fs .createReadStream (' archive.tar' );
1327+
1328+ async function run () {
1329+ await finished (rs);
1330+ console .log (' Stream is done reading' );
1331+ }
1332+
1333+ run ().catch (console .error );
1334+ rs .resume (); // drain the stream
1335+ ```
1336+
1337+ ### stream.pipeline(...streams[ , callback] )
1338+ <!-- YAML
1339+ added: REPLACEME
1340+ -->
1341+
1342+ * ` ...streams ` {Stream} Two or more streams to pipe between.
1343+ * ` callback ` {Function} A callback function that takes an optional error
1344+ argument.
1345+
1346+ A module method to pipe between streams forwarding errors and properly cleaning
1347+ up and provide a callback when the pipeline is complete.
1348+
1349+ ``` js
1350+ const { pipeline } = require (' stream' );
1351+ const fs = require (' fs' );
1352+ const zlib = require (' zlib' );
1353+
1354+ // Use the pipeline API to easily pipe a series of streams
1355+ // together and get notified when the pipeline is fully done.
1356+
1357+ // A pipeline to gzip a potentially huge tar file efficiently:
1358+
1359+ pipeline (
1360+ fs .createReadStream (' archive.tar' ),
1361+ zlib .createGzip (),
1362+ fs .createWriteStream (' archive.tar.gz' ),
1363+ (err ) => {
1364+ if (err) {
1365+ console .error (' Pipeline failed' , err);
1366+ } else {
1367+ console .log (' Pipeline succeeded' );
1368+ }
1369+ }
1370+ );
1371+ ```
1372+
1373+ The ` pipeline ` API is promisify'able as well:
1374+
1375+ ``` js
1376+ const pipeline = util .promisify (stream .pipeline );
1377+
1378+ async function run () {
1379+ await pipeline (
1380+ fs .createReadStream (' archive.tar' ),
1381+ zlib .createGzip (),
1382+ fs .createWriteStream (' archive.tar.gz' )
1383+ );
1384+ console .log (' Pipeline succeeded' );
1385+ }
1386+
1387+ run ().catch (console .error );
1388+ ```
1389+
12861390## API for Stream Implementers
12871391
12881392<!-- type=misc-->
@@ -2395,6 +2499,8 @@ contain multi-byte characters.
23952499[ http-incoming-message ] : http.html#http_class_http_incomingmessage
23962500[ zlib ] : zlib.html
23972501[ hwm-gotcha ] : #stream_highwatermark_discrepancy_after_calling_readable_setencoding
2502+ [ pipeline ] : #stream_stream_pipeline_streams_callback
2503+ [ finished ] : #stream_stream_finished_stream_callback
23982504[ stream-_flush ] : #stream_transform_flush_callback
23992505[ stream-_read ] : #stream_readable_read_size_1
24002506[ stream-_transform ] : #stream_transform_transform_chunk_encoding_callback
0 commit comments