Skip to content

Commit 0ee26a1

Browse files
committed
doc: change examples to use the same data type as the result
1 parent 0102e52 commit 0ee26a1

1 file changed

Lines changed: 25 additions & 67 deletions

File tree

doc/api/webstreams.md

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,33 +1458,26 @@ added: v16.7.0
14581458
contents of the stream.
14591459
14601460
```mjs
1461-
import { arrayBuffer } from 'node:stream/consumers';
1461+
import { buffer as arrayBuffer } from 'node:stream/consumers';
14621462
import { Readable } from 'node:stream';
1463+
import { TextEncoder } from 'node:util';
14631464

1464-
const items = Array.from(
1465-
{
1466-
length: 100
1467-
},
1468-
() => 'hello world from consumers!'
1469-
);
1465+
const encoder = new TextEncoder();
1466+
const dataArray = encoder.encode('hello world from consumers!');
14701467

1471-
const readable = Readable.from(items);
1468+
const readable = Readable.from(dataArray);
14721469
const data = await arrayBuffer(readable);
14731470
console.log(`from readable: ${data.byteLength}`);
14741471
```
14751472
14761473
```cjs
14771474
const { arrayBuffer } = require('node:stream/consumers');
1478-
const { Readable } = require('node:stream');
1475+
const { Readable } = require('stream');
1476+
const { TextEncoder } = require('util');
14791477

1480-
const items = Array.from(
1481-
{
1482-
length: 100
1483-
},
1484-
() => 'hello world from consumers!'
1485-
);
1486-
1487-
const readable = Readable.from(items);
1478+
const encoder = new TextEncoder();
1479+
const dataArray = encoder.encode(['hello world from consumers!']);
1480+
const readable = Readable.from(dataArray);
14881481
arrayBuffer(readable).then((data) => {
14891482
console.log(`from readable: ${data.byteLength}`);
14901483
});
@@ -1502,42 +1495,29 @@ added: v16.7.0
15021495
15031496
```mjs
15041497
import { blob } from 'node:stream/consumers';
1505-
import { Readable } from 'node:stream';
15061498

1507-
const items = Array.from(
1508-
{
1509-
length: 100
1510-
},
1511-
() => 'hello world from consumers!'
1512-
);
1499+
const dataBlob = new Blob(['hello world from consumers!']);
15131500

1514-
const readable = Readable.from(items);
1501+
const readable = dataBlob.stream();
15151502
const data = await blob(readable);
15161503
console.log(`from readable: ${data.size}`);
15171504
```
15181505
15191506
```cjs
15201507
const { blob } = require('node:stream/consumers');
1521-
const { Readable } = require('node:stream');
15221508

1523-
const items = Array.from(
1524-
{
1525-
length: 100
1526-
},
1527-
() => 'hello world from consumers!'
1528-
);
1509+
const dataBlob = new Blob(['hello world from consumers!']);
15291510

1530-
const readable = Readable.from(items);
1511+
const readable = dataBlob.stream();
15311512
blob(readable).then((data) => {
15321513
console.log(`from readable: ${data.size}`);
15331514
});
15341515
```
15351516
15361517
#### `streamConsumers.buffer(stream)`
15371518
1538-
<!-- YAML
1539-
added: v16.7.0
1540-
-->
1519+
\<!-added: v16.7.0
1520+
\-->
15411521
15421522
* `stream` {ReadableStream|stream.Readable|AsyncIterator}
15431523
* Returns: {Promise} Fulfills with a {Buffer} containing the full
@@ -1546,31 +1526,23 @@ added: v16.7.0
15461526
```mjs
15471527
import { buffer } from 'node:stream/consumers';
15481528
import { Readable } from 'node:stream';
1529+
import { Buffer } from 'node:buffer';
15491530

1550-
const items = Array.from(
1551-
{
1552-
length: 100
1553-
},
1554-
() => 'hello world from consumers!'
1555-
);
1531+
const dataBuffer = Buffer.from('hello world from consumers!');
15561532

1557-
const readable = Readable.from(items);
1533+
const readable = Readable.from(dataBuffer);
15581534
const data = await buffer(readable);
15591535
console.log(`from readable: ${data.length}`);
15601536
```
15611537
15621538
```cjs
15631539
const { buffer } = require('node:stream/consumers');
15641540
const { Readable } = require('node:stream');
1541+
const { Buffer } = require('node:buffer');
15651542

1566-
const items = Array.from(
1567-
{
1568-
length: 100
1569-
},
1570-
() => 'hello world from consumers!'
1571-
);
1543+
const dataBuffer = Buffer.from('hello world from consumers!');
15721544

1573-
const readable = Readable.from(items);
1545+
const readable = Readable.from(dataBuffer);
15741546
buffer(readable).then((data) => {
15751547
console.log(`from readable: ${data.length}`);
15761548
});
@@ -1617,7 +1589,7 @@ const items = Array.from(
16171589
})
16181590
);
16191591

1620-
const readable = Readable.from(items);
1592+
const readable = Readable.from(JSON.stringify(items));
16211593
json(readable).then((data) => {
16221594
console.log(`from readable: ${data.length}`);
16231595
});
@@ -1637,14 +1609,7 @@ added: v16.7.0
16371609
import { json, text, blob, buffer } from 'node:stream/consumers';
16381610
import { Readable } from 'node:stream';
16391611

1640-
const items = Array.from(
1641-
{
1642-
length: 100
1643-
},
1644-
() => 'hello world from consumers!'
1645-
);
1646-
1647-
const readable = Readable.from(items);
1612+
const readable = Readable.from('Hello world from consumers!');
16481613
const data = await text(readable);
16491614
console.log(`from readable: ${data.length}`);
16501615
```
@@ -1653,14 +1618,7 @@ console.log(`from readable: ${data.length}`);
16531618
const { text } = require('node:stream/consumers');
16541619
const { Readable } = require('node:stream');
16551620

1656-
const items = Array.from(
1657-
{
1658-
length: 100
1659-
},
1660-
() => 'hello world from consumers!'
1661-
);
1662-
1663-
const readable = Readable.from(items);
1621+
const readable = Readable.from('Hello world from consumers!');
16641622
text(readable).then((data) => {
16651623
console.log(`from readable: ${data.length}`);
16661624
});

0 commit comments

Comments
 (0)