@@ -1349,6 +1349,32 @@ added: v16.7.0
13491349* Returns: {Promise} Fulfills with an ` ArrayBuffer ` containing the full
13501350 contents of the stream.
13511351
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+
13521378#### ` streamConsumers .blob (stream)`
13531379
13541380<!-- YAML
@@ -1359,6 +1385,27 @@ added: v16.7.0
13591385* Returns: {Promise} Fulfills with a {Blob} containing the full contents
13601386 of the stream.
13611387
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+
13621409#### ` streamConsumers .buffer (stream)`
13631410
13641411<!-- YAML
@@ -1369,6 +1416,31 @@ added: v16.7.0
13691416* Returns: {Promise} Fulfills with a {Buffer} containing the full
13701417 contents of the stream.
13711418
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+
13721444#### ` streamConsumers .json (stream)`
13731445
13741446<!-- YAML
@@ -1379,6 +1451,43 @@ added: v16.7.0
13791451* Returns: {Promise} Fulfills with the contents of the stream parsed as a
13801452 UTF-8 encoded string that is then passed through ` JSON .parse ()` .
13811453
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+
13821491#### ` streamConsumers .text (stream)`
13831492
13841493<!-- YAML
@@ -1389,5 +1498,24 @@ added: v16.7.0
13891498* Returns: {Promise} Fulfills with the contents of the stream parsed as a
13901499 UTF-8 encoded string.
13911500
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+
13921520[Streams]: stream.md
13931521[WHATWG Streams Standard]: https://streams.spec.whatwg.org/
0 commit comments