@@ -1349,6 +1349,32 @@ added: v16.7.0
1349
1349
* Returns: {Promise} Fulfills with an ` ArrayBuffer ` containing the full
1350
1350
contents of the stream.
1351
1351
1352
+ ` ` ` mjs
1353
+ import { buffer as arrayBuffer } from ' node:stream/consumers' ;
1354
+ import { Readable } from ' node:stream' ;
1355
+ import { TextEncoder } from ' node:util' ;
1356
+
1357
+ const encoder = new TextEncoder ();
1358
+ const dataArray = encoder .encode (' hello world from consumers!' );
1359
+
1360
+ const readable = Readable .from (dataArray);
1361
+ const data = await arrayBuffer (readable);
1362
+ console .log (` from readable: ${ data .byteLength } ` );
1363
+ ` ` `
1364
+
1365
+ ` ` ` cjs
1366
+ const { arrayBuffer } = require (' node:stream/consumers' );
1367
+ const { Readable } = require (' stream' );
1368
+ const { TextEncoder } = require (' util' );
1369
+
1370
+ const encoder = new TextEncoder ();
1371
+ const dataArray = encoder .encode ([' hello world from consumers!' ]);
1372
+ const readable = Readable .from (dataArray);
1373
+ arrayBuffer (readable).then ((data ) => {
1374
+ console .log (` from readable: ${ data .byteLength } ` );
1375
+ });
1376
+ ` ` `
1377
+
1352
1378
#### ` streamConsumers .blob (stream)`
1353
1379
1354
1380
<!-- YAML
@@ -1359,6 +1385,27 @@ added: v16.7.0
1359
1385
* Returns: {Promise} Fulfills with a {Blob} containing the full contents
1360
1386
of the stream.
1361
1387
1388
+ ` ` ` mjs
1389
+ import { blob } from ' node:stream/consumers' ;
1390
+
1391
+ const dataBlob = new Blob ([' hello world from consumers!' ]);
1392
+
1393
+ const readable = dataBlob .stream ();
1394
+ const data = await blob (readable);
1395
+ console .log (` from readable: ${ data .size } ` );
1396
+ ` ` `
1397
+
1398
+ ` ` ` cjs
1399
+ const { blob } = require (' node:stream/consumers' );
1400
+
1401
+ const dataBlob = new Blob ([' hello world from consumers!' ]);
1402
+
1403
+ const readable = dataBlob .stream ();
1404
+ blob (readable).then ((data ) => {
1405
+ console .log (` from readable: ${ data .size } ` );
1406
+ });
1407
+ ` ` `
1408
+
1362
1409
#### ` streamConsumers .buffer (stream)`
1363
1410
1364
1411
<!-- YAML
@@ -1369,6 +1416,31 @@ added: v16.7.0
1369
1416
* Returns: {Promise} Fulfills with a {Buffer} containing the full
1370
1417
contents of the stream.
1371
1418
1419
+ ` ` ` mjs
1420
+ import { buffer } from ' node:stream/consumers' ;
1421
+ import { Readable } from ' node:stream' ;
1422
+ import { Buffer } from ' node:buffer' ;
1423
+
1424
+ const dataBuffer = Buffer .from (' hello world from consumers!' );
1425
+
1426
+ const readable = Readable .from (dataBuffer);
1427
+ const data = await buffer (readable);
1428
+ console .log (` from readable: ${ data .length } ` );
1429
+ ` ` `
1430
+
1431
+ ` ` ` cjs
1432
+ const { buffer } = require (' node:stream/consumers' );
1433
+ const { Readable } = require (' node:stream' );
1434
+ const { Buffer } = require (' node:buffer' );
1435
+
1436
+ const dataBuffer = Buffer .from (' hello world from consumers!' );
1437
+
1438
+ const readable = Readable .from (dataBuffer);
1439
+ buffer (readable).then ((data ) => {
1440
+ console .log (` from readable: ${ data .length } ` );
1441
+ });
1442
+ ` ` `
1443
+
1372
1444
#### ` streamConsumers .json (stream)`
1373
1445
1374
1446
<!-- YAML
@@ -1379,6 +1451,43 @@ added: v16.7.0
1379
1451
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
1380
1452
UTF-8 encoded string that is then passed through ` JSON .parse ()` .
1381
1453
1454
+ ` ` ` mjs
1455
+ import { json } from ' node:stream/consumers' ;
1456
+ import { Readable } from ' node:stream' ;
1457
+
1458
+ const items = Array .from (
1459
+ {
1460
+ length: 100
1461
+ },
1462
+ () => ({
1463
+ message: ' hello world from consumers!'
1464
+ })
1465
+ );
1466
+
1467
+ const readable = Readable .from (JSON .stringify (items));
1468
+ const data = await json (readable);
1469
+ console .log (` from readable: ${ data .length } ` );
1470
+ ` ` `
1471
+
1472
+ ` ` ` cjs
1473
+ const { json } = require (' node:stream/consumers' );
1474
+ const { Readable } = require (' node:stream' );
1475
+
1476
+ const items = Array .from (
1477
+ {
1478
+ length: 100
1479
+ },
1480
+ () => ({
1481
+ message: ' hello world from consumers!'
1482
+ })
1483
+ );
1484
+
1485
+ const readable = Readable .from (JSON .stringify (items));
1486
+ json (readable).then ((data ) => {
1487
+ console .log (` from readable: ${ data .length } ` );
1488
+ });
1489
+ ` ` `
1490
+
1382
1491
#### ` streamConsumers .text (stream)`
1383
1492
1384
1493
<!-- YAML
@@ -1389,5 +1498,24 @@ added: v16.7.0
1389
1498
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
1390
1499
UTF-8 encoded string.
1391
1500
1501
+ ` ` ` mjs
1502
+ import { json , text , blob , buffer } from ' node:stream/consumers' ;
1503
+ import { Readable } from ' node:stream' ;
1504
+
1505
+ const readable = Readable .from (' Hello world from consumers!' );
1506
+ const data = await text (readable);
1507
+ console .log (` from readable: ${ data .length } ` );
1508
+ ` ` `
1509
+
1510
+ ` ` ` cjs
1511
+ const { text } = require (' node:stream/consumers' );
1512
+ const { Readable } = require (' node:stream' );
1513
+
1514
+ const readable = Readable .from (' Hello world from consumers!' );
1515
+ text (readable).then ((data ) => {
1516
+ console .log (` from readable: ${ data .length } ` );
1517
+ });
1518
+ ` ` `
1519
+
1392
1520
[Streams]: stream.md
1393
1521
[WHATWG Streams Standard]: https://streams.spec.whatwg.org/
0 commit comments