Create a Broadcast from an existing source. The source is consumed automatically and pushed to all subscribers.
Node.js module
node:stream/iter
namespace Broadcast
- @param options
Same as
broadcast().
namespace Stream
- readable: ReadableStream
Converts a classic Readable stream (or duck-typed equivalent) into a stream/iter async iterable source that can be passed to
from(),pull(),text(), etc.If the object implements the
toAsyncStreamableprotocol (asstream.Readabledoes), that protocol is used. Otherwise, the function duck-types onread()andon()(EventEmitter) and wraps the stream with a batched async iterator.The result is cached per instance -- calling
fromReadable()twice with the same stream returns the same iterable.For object-mode or encoded Readable streams, chunks are automatically normalized to
Uint8Array.import { Readable } from 'node:stream'; import { fromReadable, text } from 'node:stream/iter'; const readable = new Readable({ read() { this.push('hello world'); this.push(null); }, }); const result = await text(fromReadable(readable)); console.log(result); // 'hello world'@param readableA classic Readable stream or any object with
read()andon()methods.@returnsA stream/iter async iterable source.
- writable: WritableStream,
Creates a stream/iter Writer adapter from a classic Writable stream (or duck-typed equivalent). The adapter can be passed to
pipeTo()as a destination.Since all writes on a classic Writable are fundamentally asynchronous, the synchronous Writer methods (
writeSync,writevSync,endSync) always returnfalseor-1, deferring to the async path. The per-writeoptions.signalparameter from the Writer interface is also ignored.The result is cached per instance -- calling
fromWritable()twice with the same stream returns the same Writer.For duck-typed streams that do not expose
writableHighWaterMark,writableLength, or similar properties, sensible defaults are used. Object-mode writables (if detectable) are rejected since the Writer interface is bytes-only.import { Writable } from 'node:stream'; import { from, fromWritable, pipeTo } from 'node:stream/iter'; const writable = new Writable({ write(chunk, encoding, cb) { console.log(chunk.toString()); cb(); }, }); await pipeTo(from('hello world'), fromWritable(writable, { backpressure: 'block' }));@param writableA classic Writable stream or any object with
write()andon()methods.@returnsA stream/iter Writer adapter.
- ): Promise<number>;): Promise<number>;
- ): number;): number;): number;
Creates a byte-mode
stream.Readablefrom anAsyncIterable<Uint8Array[]>(the native batch format used by the stream/iter API). EachUint8Arrayin a yielded batch is pushed as a separate chunk into the Readable.import { createWriteStream } from 'node:fs'; import { from, pull, toReadable } from 'node:stream/iter'; import { compressGzip } from 'node:zlib/iter'; const source = pull(from('hello world'), compressGzip()); const readable = toReadable(source); readable.pipe(createWriteStream('output.gz'));@param sourceAn
AsyncIterable<Uint8Array[]>source, such as the return value ofpull()orfrom().Creates a byte-mode
stream.Readablefrom a synchronousIterable<Uint8Array[]>. The_read()method pulls from the iterator synchronously, so data is available immediately viareadable.read().import { fromSync, toReadableSync } from 'node:stream/iter'; const source = fromSync('hello world'); const readable = toReadableSync(source); console.log(readable.read().toString()); // 'hello world'@param sourceAn
Iterable<Uint8Array[]>source, such as the return value ofpullSync()orfromSync().Creates a classic
stream.Writablebacked by a stream/iter Writer.Each
_write()/_writev()call attempts the Writer's synchronous method first (writeSync/writevSync), falling back to the async method if the sync path returnsfalseor throws. Similarly,_final()triesendSync()beforeend(). When the sync path succeeds, the callback is deferred viaqueueMicrotaskto preserve the async resolution contract.The Writable's
highWaterMarkis set toNumber.MAX_SAFE_INTEGERto effectively disable its internal buffering, allowing the underlying Writer to manage backpressure directly.import { push, toWritable } from 'node:stream/iter'; const { writer, readable } = push(); const writable = toWritable(writer); writable.write('hello'); writable.end();@param writerA stream/iter Writer. Only the
write()method is required;end(),fail(),writeSync(),writevSync(),endSync(), andwritev()are optional.
Type definitions
interface AsyncStreamable
interface Broadcast
interface Broadcastable
interface BroadcastOptions
interface BroadcastResult
interface BroadcastWriter
interface ConsumeOptions
interface ConsumeSyncOptions
interface Drainable
interface DuplexChannel
interface DuplexDirectionOptions
interface DuplexOptions
interface FromWritableOptions
interface MergeOptions
interface PartialSyncWriter
interface PartialWriter
interface PipeToOptions
interface PipeToSyncOptions
interface PullOptions
interface PushStreamOptions
interface PushWriter
interface StatefulTransform
interface StatelessTransformFn
interface Streamable
interface SyncStatefulTransform
interface SyncStatelessTransformFn
interface SyncWriter
interface TextConsumeOptions
interface TextConsumeSyncOptions
interface ToReadableOptions
interface ToReadableSyncOptions
interface TransformCallbackOptions
interface WriteOptions
interface Writer
- type BackpressurePolicy = 'strict' | 'block' | 'drop-oldest' | 'drop-newest'
- type ByteReadableStream = AsyncIterable<Uint8Array[]>
- type Source = string | ArrayBufferLike | ArrayBufferView | Iterable<SyncSource> | AsyncIterable<Source> | Streamable | AsyncStreamable
- type SyncByteReadableStream = Iterable<Uint8Array[]>
- type SyncSource = string | ArrayBufferLike | ArrayBufferView | Iterable<SyncSource> | Streamable
- type SyncTransformResult = string | ArrayBufferLike | ArrayBufferView | Iterable<SyncTransformResult>
- type TransformResult = string | ArrayBufferLike | ArrayBufferView | Iterable<SyncTransformResult> | AsyncIterable<TransformResult>