Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d537b61

Browse filesBrowse files
khaosdoctorRafaelGSS
authored andcommitted
doc: include code examples for webstreams consumers
Add missing examples for webstreams consumers Doc URL: https://nodejs.org/api/webstreams.html#streamconsumerstextstream PR-URL: #44387 Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
1 parent 7d9de70 commit d537b61
Copy full SHA for d537b61

File tree

Expand file treeCollapse file tree

1 file changed

+128
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+128
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/webstreams.md‎

Copy file name to clipboardExpand all lines: doc/api/webstreams.md
+128Lines changed: 128 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,32 @@ added: v16.7.0
14571457
* Returns: {Promise} Fulfills with an `ArrayBuffer` containing the full
14581458
contents of the stream.
14591459
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+
14601486
#### `streamConsumers.blob(stream)`
14611487
14621488
<!-- YAML
@@ -1467,6 +1493,27 @@ added: v16.7.0
14671493
* Returns: {Promise} Fulfills with a {Blob} containing the full contents
14681494
of the stream.
14691495
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+
14701517
#### `streamConsumers.buffer(stream)`
14711518
14721519
<!-- YAML
@@ -1477,6 +1524,31 @@ added: v16.7.0
14771524
* Returns: {Promise} Fulfills with a {Buffer} containing the full
14781525
contents of the stream.
14791526
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+
14801552
#### `streamConsumers.json(stream)`
14811553
14821554
<!-- YAML
@@ -1487,6 +1559,43 @@ added: v16.7.0
14871559
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
14881560
UTF-8 encoded string that is then passed through `JSON.parse()`.
14891561
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+
14901599
#### `streamConsumers.text(stream)`
14911600
14921601
<!-- YAML
@@ -1497,5 +1606,24 @@ added: v16.7.0
14971606
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
14981607
UTF-8 encoded string.
14991608
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+
15001628
[Streams]: stream.md
15011629
[WHATWG Streams Standard]: https://streams.spec.whatwg.org/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.