Skip to content

Commit 530d036

Browse files
jasnelladuh95
authored andcommitted
doc: fixup and cleanup stream/iter docs
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #66079 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent d616a49 commit 530d036

1 file changed

Lines changed: 42 additions & 35 deletions

File tree

‎doc/api/stream_iter.md‎

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,10 @@ writer closes immediately.
452452
* Returns: {number} Total bytes written, or `-1` if ending cannot complete
453453
synchronously.
454454

455-
Synchronous variant of `writer.end()`. A return value of `-1` means closing has
456-
started but requires asynchronous draining. Use the try-fallback pattern to
457-
await completion:
455+
Synchronous variant of `writer.end()`. A return value of `-1` only indicates
456+
that the operation could not complete synchronously; no assumption can be made
457+
about whether closing has started or why it could not complete. Use the
458+
try-fallback pattern to await completion:
458459

459460
```cjs
460461
const result = writer.endSync();
@@ -517,8 +518,11 @@ Synchronous batch write.
517518

518519
## The `stream/iter` module
519520

520-
All functions are available both as named exports and as properties of the
521-
`Stream` namespace object:
521+
Most functions are available both as named exports and as properties of the
522+
`Stream` namespace object. The classic stream adapters (`fromReadable()`,
523+
`fromWritable()`, `toReadable()`, `toReadableSync()`, and `toWritable()`) and
524+
the static helper objects (`Broadcast`, `Share`, and `SyncShare`) are named
525+
exports only.
522526

523527
```mjs
524528
// Named exports
@@ -892,12 +896,13 @@ const serving = (async () => {
892896
for await (const chunks of server.readable) {
893897
await server.writer.writev(chunks);
894898
}
899+
await server.writer.end();
895900
})();
896901

897902
await client.writer.write('hello');
898903
await client.writer.end();
899904

900-
console.log(await text(server.readable)); // handled by echo
905+
console.log(await text(client.readable)); // 'hello'
901906
await serving;
902907
```
903908

@@ -912,12 +917,13 @@ async function run() {
912917
for await (const chunks of server.readable) {
913918
await server.writer.writev(chunks);
914919
}
920+
await server.writer.end();
915921
})();
916922

917923
await client.writer.write('hello');
918924
await client.writer.end();
919925

920-
console.log(await text(server.readable)); // handled by echo
926+
console.log(await text(client.readable)); // 'hello'
921927
await serving;
922928
}
923929

@@ -1180,7 +1186,8 @@ run().catch(console.error);
11801186
added: v25.9.0
11811187
-->
11821188

1183-
* `callback` {Function} `(chunks) => void` Called with each batch.
1189+
* `callback` {Function} `(chunks) => void` Called with each batch and with
1190+
`null` when the source ends.
11841191
* Returns: {Function} A stateless transform.
11851192

11861193
Create a pass-through transform that observes batches without modifying them.
@@ -1191,7 +1198,9 @@ import { from, pull, text, tap } from 'node:stream/iter';
11911198

11921199
const result = pull(
11931200
from('hello'),
1194-
tap((chunks) => console.log('Batch size:', chunks.length)),
1201+
tap((chunks) => {
1202+
if (chunks !== null) console.log('Batch size:', chunks.length);
1203+
}),
11951204
);
11961205
console.log(await text(result));
11971206
```
@@ -1202,7 +1211,9 @@ const { from, pull, text, tap } = require('node:stream/iter');
12021211
async function run() {
12031212
const result = pull(
12041213
from('hello'),
1205-
tap((chunks) => console.log('Batch size:', chunks.length)),
1214+
tap((chunks) => {
1215+
if (chunks !== null) console.log('Batch size:', chunks.length);
1216+
}),
12061217
);
12071218
console.log(await text(result));
12081219
}
@@ -1314,9 +1325,8 @@ The number of active consumers.
13141325
* `signal` {AbortSignal}
13151326
* Returns: {AsyncIterable} whose chunks fulfill with {Uint8Array\[]}
13161327

1317-
Create a new consumer. Each consumer receives all data written to the
1318-
broadcast from the point of subscription onward. Optional transforms are
1319-
applied to this consumer's view of the data.
1328+
Create a new consumer. Optional transforms are applied to this consumer's view
1329+
of the data.
13201330

13211331
#### `broadcast[Symbol.dispose]()`
13221332

@@ -1475,12 +1485,6 @@ added: v25.9.0
14751485
* `options` {Object}
14761486
* Returns: {SyncShare}
14771487

1478-
#### `share.bufferSize`
1479-
1480-
* {number}
1481-
1482-
The number of chunks currently buffered.
1483-
14841488
#### `share.cancel([reason])`
14851489

14861490
* `reason` {any}
@@ -1494,11 +1498,9 @@ reason. If it is omitted, consumers complete normally.
14941498

14951499
The number of active consumers.
14961500

1497-
#### `share.pull([...transforms][, options])`
1501+
#### `share.pull([...transforms])`
14981502

14991503
* `...transforms` {Function|Object}
1500-
* `options` {Object}
1501-
* `signal` {AbortSignal}
15021504
* Returns: {Iterable} whose chunks return {Uint8Array\[]}
15031505

15041506
Create a new consumer of the shared source.
@@ -1803,7 +1805,11 @@ to the {BroadcastChannel} interface. The implementation is fully custom -- it ca
18031805
manage consumers, buffering, and backpressure however it wants.
18041806

18051807
```mjs
1806-
import { Broadcast, text } from 'node:stream/iter';
1808+
import {
1809+
broadcast as createBroadcast,
1810+
Broadcast,
1811+
text,
1812+
} from 'node:stream/iter';
18071813

18081814
// This example defers to the built-in Broadcast, but a custom
18091815
// implementation could use any mechanism.
@@ -1812,7 +1818,7 @@ class MessageBus {
18121818
#writer;
18131819

18141820
constructor() {
1815-
const { writer, broadcast } = Broadcast();
1821+
const { writer, broadcast } = createBroadcast();
18161822
this.#writer = writer;
18171823
this.#broadcast = broadcast;
18181824
}
@@ -1839,7 +1845,11 @@ console.log(await text(consumer)); // 'hello'
18391845
```
18401846

18411847
```cjs
1842-
const { Broadcast, text } = require('node:stream/iter');
1848+
const {
1849+
broadcast: createBroadcast,
1850+
Broadcast,
1851+
text,
1852+
} = require('node:stream/iter');
18431853

18441854
// This example defers to the built-in Broadcast, but a custom
18451855
// implementation could use any mechanism.
@@ -1848,7 +1858,7 @@ class MessageBus {
18481858
#writer;
18491859

18501860
constructor() {
1851-
const { writer, broadcast } = Broadcast();
1861+
const { writer, broadcast } = createBroadcast();
18521862
this.#writer = writer;
18531863
this.#broadcast = broadcast;
18541864
}
@@ -2084,11 +2094,9 @@ console.log(textSync(consumer)); // 'hello'
20842094
* Value: `Symbol.for('Stream.toAsyncStreamable')`
20852095
20862096
The value must be a function that converts the object into a streamable value.
2087-
When the object is encountered anywhere in the streaming pipeline (as a source
2088-
passed to `from()`, or as a value returned from a transform), this method is
2089-
called to produce the actual data. It may return any value that resolves to:
2090-
a string, `Uint8Array`, `AsyncIterable`, `Iterable`, or another streamable
2091-
object.
2097+
When the object is passed to `from()`, this method is called to produce the
2098+
actual data. It may return any value that resolves to a string, `Uint8Array`,
2099+
`AsyncIterable`, `Iterable`, or another streamable object.
20922100
20932101
```mjs
20942102
import { from, text } from 'node:stream/iter';
@@ -2133,10 +2141,9 @@ text(stream).then(console.log); // 'hello world'
21332141
* Value: `Symbol.for('Stream.toStreamable')`
21342142
21352143
The value must be a function that synchronously converts the object into a
2136-
streamable value. When the object is encountered anywhere in the streaming
2137-
pipeline (as a source passed to `fromSync()`, or as a value returned from a
2138-
sync transform), this method is called to produce the actual data. It must
2139-
synchronously return a streamable value: a string, `Uint8Array`, or `Iterable`.
2144+
streamable value. When the object is passed to `fromSync()`, this method is
2145+
called to produce the actual data. It must synchronously return a streamable
2146+
value: a string, `Uint8Array`, or `Iterable`.
21402147
21412148
```mjs
21422149
import { fromSync, textSync } from 'node:stream/iter';

0 commit comments

Comments
 (0)