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