| | |
| | | /** |
| | | * A stream is an abstract interface for working with streaming data in Node.js. |
| | | * The `node:stream` module provides an API for implementing the stream interface. |
| | | * |
| | | * There are many stream objects provided by Node.js. For instance, a [request to an HTTP server](https://nodejs.org/docs/latest-v25.x/api/http.html#class-httpincomingmessage) |
| | | * and [`process.stdout`](https://nodejs.org/docs/latest-v25.x/api/process.html#processstdout) are both stream instances. |
| | | * |
| | | * Streams can be readable, writable, or both. All streams are instances of [`EventEmitter`](https://nodejs.org/docs/latest-v25.x/api/events.html#class-eventemitter). |
| | | * |
| | | * To access the `node:stream` module: |
| | | * |
| | | * ```js |
| | | * import stream from 'node:stream'; |
| | | * ``` |
| | | * |
| | | * The `node:stream` module is useful for creating new types of stream instances. |
| | | * It is usually not necessary to use the `node:stream` module to consume streams. |
| | | * @see [source](https://github.com/nodejs/node/blob/v25.x/lib/stream.js) |
| | | */ |
| | | declare module "node:stream" { |
| | | import { Blob } from "node:buffer"; |
| | | import { Abortable, EventEmitter } from "node:events"; |
| | |
| | | interface ArrayOptions extends ReadableOperatorOptions {} |
| | | interface ReadableToWebOptions { |
| | | strategy?: web.QueuingStrategy | undefined; |
| | | type?: web.ReadableStreamType | undefined; |
| | | } |
| | | interface ReadableEventMap { |
| | | "close": []; |
| | |
| | | * } |
| | | * } |
| | | * |
| | | * const wordsStream = Readable.from(['this is', 'compose as operator']).compose(splitToWords); |
| | | * const wordsStream = Readable.from(['text passed through', 'composed stream']).compose(splitToWords); |
| | | * const words = await wordsStream.toArray(); |
| | | * |
| | | * console.log(words); // prints ['this', 'is', 'compose', 'as', 'operator'] |
| | | * console.log(words); // prints ['text', 'passed', 'through', 'composed', 'stream'] |
| | | * ``` |
| | | * |
| | | * See [`stream.compose`](https://nodejs.org/docs/latest-v25.x/api/stream.html#streamcomposestreams) for more information. |
| | | * `readable.compose(s)` is equivalent to `stream.compose(readable, s)`. |
| | | * |
| | | * This method also allows for an `AbortSignal` to be provided, which will destroy |
| | | * the composed stream when aborted. |
| | | * |
| | | * See [`stream.compose(...streams)`](https://nodejs.org/docs/latest-v25.x/api/stream.html#streamcomposestreams) for more information. |
| | | * @since v19.1.0, v18.13.0 |
| | | * @returns a stream composed with the stream `stream`. |
| | | */ |
| | |
| | | * @since v0.9.4 |
| | | * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a {string}, {Buffer}, |
| | | * {TypedArray} or {DataView}. For object mode streams, `chunk` may be any JavaScript value other than `null`. |
| | | * @param [encoding='utf8'] The encoding, if `chunk` is a string. |
| | | * @param callback Callback for when this chunk of data is flushed. |
| | | * @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`. |
| | | */ |
| | | write(chunk: any, callback?: (error: Error | null | undefined) => void): boolean; |
| | | /** |
| | | * Writes data to the stream, with an explicit encoding for string data. |
| | | * @see {@link Writable.write} for full details. |
| | | * @since v0.9.4 |
| | | * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a {string}, {Buffer}, |
| | | * {TypedArray} or {DataView}. For object mode streams, `chunk` may be any JavaScript value other than `null`. |
| | | * @param encoding The encoding, if `chunk` is a string. |
| | | * @param callback Callback for when this chunk of data is flushed. |
| | | * @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`. |
| | | */ |
| | | write(chunk: any, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): boolean; |
| | | /** |
| | | * The `writable.setDefaultEncoding()` method sets the default `encoding` for a `Writable` stream. |
| | |
| | | * // Writing more now is not allowed! |
| | | * ``` |
| | | * @since v0.9.4 |
| | | * @param cb Callback for when the stream is finished. |
| | | */ |
| | | end(cb?: () => void): this; |
| | | /** |
| | | * Signals that no more data will be written, with one final chunk of data. |
| | | * @see {@link Writable.end} for full details. |
| | | * @since v0.9.4 |
| | | * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a {string}, {Buffer}, |
| | | * {TypedArray} or {DataView}. For object mode streams, `chunk` may be any JavaScript value other than `null`. |
| | | * @param cb Callback for when the stream is finished. |
| | | */ |
| | | end(chunk: any, cb?: () => void): this; |
| | | /** |
| | | * Signals that no more data will be written, with one final chunk of data. |
| | | * @see {@link Writable.end} for full details. |
| | | * @since v0.9.4 |
| | | * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a {string}, {Buffer}, |
| | | * {TypedArray} or {DataView}. For object mode streams, `chunk` may be any JavaScript value other than `null`. |
| | | * @param encoding The encoding if `chunk` is a string |
| | | * @param callback Callback for when the stream is finished. |
| | | * @param cb Callback for when the stream is finished. |
| | | */ |
| | | end(cb?: () => void): this; |
| | | end(chunk: any, cb?: () => void): this; |
| | | end(chunk: any, encoding: BufferEncoding, cb?: () => void): this; |
| | | /** |
| | | * The `writable.cork()` method forces all written data to be buffered in memory. |
| | |
| | | writableHighWaterMark?: number | undefined; |
| | | writableCorked?: number | undefined; |
| | | } |
| | | interface DuplexToWebOptions { |
| | | type?: web.ReadableStreamType | undefined; |
| | | } |
| | | interface DuplexEventMap extends ReadableEventMap, WritableEventMap {} |
| | | /** |
| | | * Duplex streams are streams that implement both the `Readable` and `Writable` interfaces. |
| | |
| | | * A utility method for creating a web `ReadableStream` and `WritableStream` from a `Duplex`. |
| | | * @since v17.0.0 |
| | | */ |
| | | static toWeb(streamDuplex: NodeJS.ReadWriteStream): web.ReadableWritablePair; |
| | | static toWeb(streamDuplex: NodeJS.ReadWriteStream, options?: DuplexToWebOptions): web.ReadableWritablePair; |
| | | /** |
| | | * A utility method for creating a `Duplex` from a web `ReadableStream` and `WritableStream`. |
| | | * @since v17.0.0 |
| | |
| | | * console.log(res); // prints 'HELLOWORLD' |
| | | * ``` |
| | | * |
| | | * See [`readable.compose(stream)`](https://nodejs.org/docs/latest-v25.x/api/stream.html#readablecomposestream-options) for `stream.compose` as operator. |
| | | * For convenience, the `readable.compose(stream)` method is available on |
| | | * `Readable` and `Duplex` streams as a wrapper for this function. |
| | | * @since v16.9.0 |
| | | * @experimental |
| | | */ |