@@ -452,9 +452,10 @@ writer closes immediately. |
452 | 452 | * Returns: {number} Total bytes written, or `-1` if ending cannot complete |
453 | 453 | synchronously. |
454 | 454 |
|
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: |
458 | 459 |
|
459 | 460 | ```cjs |
460 | 461 | const result = writer.endSync(); |
@@ -517,8 +518,11 @@ Synchronous batch write. |
517 | 518 |
|
518 | 519 | ## The `stream/iter` module |
519 | 520 |
|
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. |
522 | 526 |
|
523 | 527 | ```mjs |
524 | 528 | // Named exports |
@@ -892,12 +896,13 @@ const serving = (async () => { |
892 | 896 | for await (const chunks of server.readable) { |
893 | 897 | await server.writer.writev(chunks); |
894 | 898 | } |
| 899 | + await server.writer.end(); |
895 | 900 | })(); |
896 | 901 |
|
897 | 902 | await client.writer.write('hello'); |
898 | 903 | await client.writer.end(); |
899 | 904 |
|
900 | | -console.log(await text(server.readable)); // handled by echo |
| 905 | +console.log(await text(client.readable)); // 'hello' |
901 | 906 | await serving; |
902 | 907 | ``` |
903 | 908 |
|
@@ -912,12 +917,13 @@ async function run() { |
912 | 917 | for await (const chunks of server.readable) { |
913 | 918 | await server.writer.writev(chunks); |
914 | 919 | } |
| 920 | + await server.writer.end(); |
915 | 921 | })(); |
916 | 922 |
|
917 | 923 | await client.writer.write('hello'); |
918 | 924 | await client.writer.end(); |
919 | 925 |
|
920 | | - console.log(await text(server.readable)); // handled by echo |
| 926 | + console.log(await text(client.readable)); // 'hello' |
921 | 927 | await serving; |
922 | 928 | } |
923 | 929 |
|
@@ -1180,7 +1186,8 @@ run().catch(console.error); |
1180 | 1186 | added: v25.9.0 |
1181 | 1187 | --> |
1182 | 1188 |
|
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. |
1184 | 1191 | * Returns: {Function} A stateless transform. |
1185 | 1192 |
|
1186 | 1193 | Create a pass-through transform that observes batches without modifying them. |
@@ -1191,7 +1198,9 @@ import { from, pull, text, tap } from 'node:stream/iter'; |
1191 | 1198 |
|
1192 | 1199 | const result = pull( |
1193 | 1200 | 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 | + }), |
1195 | 1204 | ); |
1196 | 1205 | console.log(await text(result)); |
1197 | 1206 | ``` |
@@ -1202,7 +1211,9 @@ const { from, pull, text, tap } = require('node:stream/iter'); |
1202 | 1211 | async function run() { |
1203 | 1212 | const result = pull( |
1204 | 1213 | 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 | + }), |
1206 | 1217 | ); |
1207 | 1218 | console.log(await text(result)); |
1208 | 1219 | } |
@@ -1314,9 +1325,8 @@ The number of active consumers. |
1314 | 1325 | * `signal` {AbortSignal} |
1315 | 1326 | * Returns: {AsyncIterable} whose chunks fulfill with {Uint8Array\[]} |
1316 | 1327 |
|
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. |
1320 | 1330 |
|
1321 | 1331 | #### `broadcast[Symbol.dispose]()` |
1322 | 1332 |
|
@@ -1475,12 +1485,6 @@ added: v25.9.0 |
1475 | 1485 | * `options` {Object} |
1476 | 1486 | * Returns: {SyncShare} |
1477 | 1487 |
|
1478 | | -#### `share.bufferSize` |
1479 | | - |
1480 | | -* {number} |
1481 | | - |
1482 | | -The number of chunks currently buffered. |
1483 | | - |
1484 | 1488 | #### `share.cancel([reason])` |
1485 | 1489 |
|
1486 | 1490 | * `reason` {any} |
@@ -1494,11 +1498,9 @@ reason. If it is omitted, consumers complete normally. |
1494 | 1498 |
|
1495 | 1499 | The number of active consumers. |
1496 | 1500 |
|
1497 | | -#### `share.pull([...transforms][, options])` |
| 1501 | +#### `share.pull([...transforms])` |
1498 | 1502 |
|
1499 | 1503 | * `...transforms` {Function|Object} |
1500 | | -* `options` {Object} |
1501 | | - * `signal` {AbortSignal} |
1502 | 1504 | * Returns: {Iterable} whose chunks return {Uint8Array\[]} |
1503 | 1505 |
|
1504 | 1506 | Create a new consumer of the shared source. |
@@ -1803,7 +1805,11 @@ to the {BroadcastChannel} interface. The implementation is fully custom -- it ca |
1803 | 1805 | manage consumers, buffering, and backpressure however it wants. |
1804 | 1806 |
|
1805 | 1807 | ```mjs |
1806 | | -import { Broadcast, text } from 'node:stream/iter'; |
| 1808 | +import { |
| 1809 | + broadcast as createBroadcast, |
| 1810 | + Broadcast, |
| 1811 | + text, |
| 1812 | +} from 'node:stream/iter'; |
1807 | 1813 |
|
1808 | 1814 | // This example defers to the built-in Broadcast, but a custom |
1809 | 1815 | // implementation could use any mechanism. |
@@ -1812,7 +1818,7 @@ class MessageBus { |
1812 | 1818 | #writer; |
1813 | 1819 |
|
1814 | 1820 | constructor() { |
1815 | | - const { writer, broadcast } = Broadcast(); |
| 1821 | + const { writer, broadcast } = createBroadcast(); |
1816 | 1822 | this.#writer = writer; |
1817 | 1823 | this.#broadcast = broadcast; |
1818 | 1824 | } |
@@ -1839,7 +1845,11 @@ console.log(await text(consumer)); // 'hello' |
1839 | 1845 | ``` |
1840 | 1846 |
|
1841 | 1847 | ```cjs |
1842 | | -const { Broadcast, text } = require('node:stream/iter'); |
| 1848 | +const { |
| 1849 | + broadcast: createBroadcast, |
| 1850 | + Broadcast, |
| 1851 | + text, |
| 1852 | +} = require('node:stream/iter'); |
1843 | 1853 |
|
1844 | 1854 | // This example defers to the built-in Broadcast, but a custom |
1845 | 1855 | // implementation could use any mechanism. |
@@ -1848,7 +1858,7 @@ class MessageBus { |
1848 | 1858 | #writer; |
1849 | 1859 |
|
1850 | 1860 | constructor() { |
1851 | | - const { writer, broadcast } = Broadcast(); |
| 1861 | + const { writer, broadcast } = createBroadcast(); |
1852 | 1862 | this.#writer = writer; |
1853 | 1863 | this.#broadcast = broadcast; |
1854 | 1864 | } |
@@ -2084,11 +2094,9 @@ console.log(textSync(consumer)); // 'hello' |
2084 | 2094 | * Value: `Symbol.for('Stream.toAsyncStreamable')` |
2085 | 2095 |
|
2086 | 2096 | 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. |
2092 | 2100 |
|
2093 | 2101 | ```mjs |
2094 | 2102 | import { from, text } from 'node:stream/iter'; |
@@ -2133,10 +2141,9 @@ text(stream).then(console.log); // 'hello world' |
2133 | 2141 | * Value: `Symbol.for('Stream.toStreamable')` |
2134 | 2142 |
|
2135 | 2143 | 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`. |
2140 | 2147 |
|
2141 | 2148 | ```mjs |
2142 | 2149 | import { fromSync, textSync } from 'node:stream/iter'; |
|