@@ -1457,6 +1457,32 @@ added: v16.7.0
1457
1457
* Returns: {Promise} Fulfills with an ` ArrayBuffer ` containing the full
1458
1458
contents of the stream.
1459
1459
1460
+ ` ` ` mjs
1461
+ import { buffer as arrayBuffer } from ' node:stream/consumers' ;
1462
+ import { Readable } from ' node:stream' ;
1463
+ import { TextEncoder } from ' node:util' ;
1464
+
1465
+ const encoder = new TextEncoder ();
1466
+ const dataArray = encoder .encode (' hello world from consumers!' );
1467
+
1468
+ const readable = Readable .from (dataArray);
1469
+ const data = await arrayBuffer (readable);
1470
+ console .log (` from readable: ${ data .byteLength } ` );
1471
+ ` ` `
1472
+
1473
+ ` ` ` cjs
1474
+ const { arrayBuffer } = require (' node:stream/consumers' );
1475
+ const { Readable } = require (' stream' );
1476
+ const { TextEncoder } = require (' util' );
1477
+
1478
+ const encoder = new TextEncoder ();
1479
+ const dataArray = encoder .encode ([' hello world from consumers!' ]);
1480
+ const readable = Readable .from (dataArray);
1481
+ arrayBuffer (readable).then ((data ) => {
1482
+ console .log (` from readable: ${ data .byteLength } ` );
1483
+ });
1484
+ ` ` `
1485
+
1460
1486
#### ` streamConsumers .blob (stream)`
1461
1487
1462
1488
<!-- YAML
@@ -1467,6 +1493,27 @@ added: v16.7.0
1467
1493
* Returns: {Promise} Fulfills with a {Blob} containing the full contents
1468
1494
of the stream.
1469
1495
1496
+ ` ` ` mjs
1497
+ import { blob } from ' node:stream/consumers' ;
1498
+
1499
+ const dataBlob = new Blob ([' hello world from consumers!' ]);
1500
+
1501
+ const readable = dataBlob .stream ();
1502
+ const data = await blob (readable);
1503
+ console .log (` from readable: ${ data .size } ` );
1504
+ ` ` `
1505
+
1506
+ ` ` ` cjs
1507
+ const { blob } = require (' node:stream/consumers' );
1508
+
1509
+ const dataBlob = new Blob ([' hello world from consumers!' ]);
1510
+
1511
+ const readable = dataBlob .stream ();
1512
+ blob (readable).then ((data ) => {
1513
+ console .log (` from readable: ${ data .size } ` );
1514
+ });
1515
+ ` ` `
1516
+
1470
1517
#### ` streamConsumers .buffer (stream)`
1471
1518
1472
1519
<!-- YAML
@@ -1477,6 +1524,31 @@ added: v16.7.0
1477
1524
* Returns: {Promise} Fulfills with a {Buffer} containing the full
1478
1525
contents of the stream.
1479
1526
1527
+ ` ` ` mjs
1528
+ import { buffer } from ' node:stream/consumers' ;
1529
+ import { Readable } from ' node:stream' ;
1530
+ import { Buffer } from ' node:buffer' ;
1531
+
1532
+ const dataBuffer = Buffer .from (' hello world from consumers!' );
1533
+
1534
+ const readable = Readable .from (dataBuffer);
1535
+ const data = await buffer (readable);
1536
+ console .log (` from readable: ${ data .length } ` );
1537
+ ` ` `
1538
+
1539
+ ` ` ` cjs
1540
+ const { buffer } = require (' node:stream/consumers' );
1541
+ const { Readable } = require (' node:stream' );
1542
+ const { Buffer } = require (' node:buffer' );
1543
+
1544
+ const dataBuffer = Buffer .from (' hello world from consumers!' );
1545
+
1546
+ const readable = Readable .from (dataBuffer);
1547
+ buffer (readable).then ((data ) => {
1548
+ console .log (` from readable: ${ data .length } ` );
1549
+ });
1550
+ ` ` `
1551
+
1480
1552
#### ` streamConsumers .json (stream)`
1481
1553
1482
1554
<!-- YAML
@@ -1487,6 +1559,43 @@ added: v16.7.0
1487
1559
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
1488
1560
UTF-8 encoded string that is then passed through ` JSON .parse ()` .
1489
1561
1562
+ ` ` ` mjs
1563
+ import { json } from ' node:stream/consumers' ;
1564
+ import { Readable } from ' node:stream' ;
1565
+
1566
+ const items = Array .from (
1567
+ {
1568
+ length: 100
1569
+ },
1570
+ () => ({
1571
+ message: ' hello world from consumers!'
1572
+ })
1573
+ );
1574
+
1575
+ const readable = Readable .from (JSON .stringify (items));
1576
+ const data = await json (readable);
1577
+ console .log (` from readable: ${ data .length } ` );
1578
+ ` ` `
1579
+
1580
+ ` ` ` cjs
1581
+ const { json } = require (' node:stream/consumers' );
1582
+ const { Readable } = require (' node:stream' );
1583
+
1584
+ const items = Array .from (
1585
+ {
1586
+ length: 100
1587
+ },
1588
+ () => ({
1589
+ message: ' hello world from consumers!'
1590
+ })
1591
+ );
1592
+
1593
+ const readable = Readable .from (JSON .stringify (items));
1594
+ json (readable).then ((data ) => {
1595
+ console .log (` from readable: ${ data .length } ` );
1596
+ });
1597
+ ` ` `
1598
+
1490
1599
#### ` streamConsumers .text (stream)`
1491
1600
1492
1601
<!-- YAML
@@ -1497,5 +1606,24 @@ added: v16.7.0
1497
1606
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
1498
1607
UTF-8 encoded string.
1499
1608
1609
+ ` ` ` mjs
1610
+ import { json , text , blob , buffer } from ' node:stream/consumers' ;
1611
+ import { Readable } from ' node:stream' ;
1612
+
1613
+ const readable = Readable .from (' Hello world from consumers!' );
1614
+ const data = await text (readable);
1615
+ console .log (` from readable: ${ data .length } ` );
1616
+ ` ` `
1617
+
1618
+ ` ` ` cjs
1619
+ const { text } = require (' node:stream/consumers' );
1620
+ const { Readable } = require (' node:stream' );
1621
+
1622
+ const readable = Readable .from (' Hello world from consumers!' );
1623
+ text (readable).then ((data ) => {
1624
+ console .log (` from readable: ${ data .length } ` );
1625
+ });
1626
+ ` ` `
1627
+
1500
1628
[Streams]: stream.md
1501
1629
[WHATWG Streams Standard]: https://streams.spec.whatwg.org/
0 commit comments