## Index

### Namespaces

- finished
- pipeline

### Classes

- Duplex
- PassThrough
- Readable
- Stream
- Transform
- Writable

### Interfaces

- DuplexOptions
- FinishedOptions
- Pipe
- PipelineOptions
- ReadableOptions
- StreamOptions
- TransformOptions
- WritableOptions

### Type aliases

- PipelineCallback
- PipelineDestination
- PipelineDestinationIterableFunction
- PipelineDestinationPromiseFunction
- PipelinePromise
- PipelineSource
- PipelineSourceFunction
- PipelineTransform
- PipelineTransformSource
- TransformCallback

### Variables

- consumers
- promises

### Functions

- addAbortSignal
- finished
- pipeline

## Type aliases

### PipelineCallback

PipelineCallback<S>: S extends PipelineDestinationPromiseFunction<any, infer P> ? (err: ErrnoException | null, value: P) => void : (err: ErrnoException | null) => void

#### Type parameters

- #### S: PipelineDestination<any, any>

### PipelineDestination

PipelineDestination<S, P>: S extends PipelineTransformSource<infer ST> ? WritableStream | PipelineDestinationIterableFunction<ST> | PipelineDestinationPromiseFunction<ST, P> : never

#### Type parameters

- #### S: PipelineTransformSource<any>

- #### P

### PipelineDestinationIterableFunction

PipelineDestinationIterableFunction<T>: (source: AsyncIterable<T>) => AsyncIterable<any>

#### Type parameters

- #### T

#### Type declaration

- - (source: AsyncIterable<T>): AsyncIterable<any>

  - #### Parameters

    - ##### source: AsyncIterable<T>

    #### Returns AsyncIterable<any>

### PipelineDestinationPromiseFunction

PipelineDestinationPromiseFunction<T, P>: (source: AsyncIterable<T>) => Promise<P>

#### Type parameters

- #### T

- #### P

#### Type declaration

- - (source: AsyncIterable<T>): Promise<P>

  - #### Parameters

    - ##### source: AsyncIterable<T>

    #### Returns Promise<P>

### PipelinePromise

PipelinePromise<S>: S extends PipelineDestinationPromiseFunction<any, infer P> ? Promise<P> : Promise<void>

#### Type parameters

- #### S: PipelineDestination<any, any>

### PipelineSource

PipelineSource<T>: Iterable<T> | AsyncIterable<T> | ReadableStream | PipelineSourceFunction<T>

#### Type parameters

- #### T

### PipelineSourceFunction

PipelineSourceFunction<T>: () => Iterable<T> | AsyncIterable<T>

#### Type parameters

- #### T

#### Type declaration

- - (): Iterable<T> | AsyncIterable<T>

  - #### Returns Iterable<T> | AsyncIterable<T>

### PipelineTransform

PipelineTransform<S, U>: ReadWriteStream | ((source: S extends (...args: any[]) => Iterable<infer ST> | AsyncIterable<infer ST> ? AsyncIterable<ST> : S) => AsyncIterable<U>)

#### Type parameters

- #### S: PipelineTransformSource<any>

- #### U

### PipelineTransformSource

PipelineTransformSource<T>: PipelineSource<T> | PipelineTransform<any, T>

#### Type parameters

- #### T

### TransformCallback

TransformCallback: (error?: <internal>.Error | null, data?: any) => void

#### Type declaration

- - (error?: <internal>.Error | null, data?: any): void

  - #### Parameters

    - ##### Optional error: <internal>.Error | null

    - ##### Optional data: any

    #### Returns void

## Variables

### Const consumers

consumers: typeof "node:stream/consumers"

### Const promises

promises: typeof "node:stream/promises"

## Functions

### addAbortSignal

- addAbortSignal<T>(signal: AbortSignal, stream: T): T

- Attaches an AbortSignal to a readable or writeable stream. This lets code control stream destruction using an `AbortController`.

  Calling `abort` on the `AbortController` corresponding to the passed`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`on the stream.

      const fs = require('fs');

      const controller = new AbortController();
      const read = addAbortSignal(
        controller.signal,
        fs.createReadStream(('object.json'))
      );
      // Later, abort the operation closing the stream
      controller.abort();

  Or using an `AbortSignal` with a readable stream as an async iterable:

      const controller = new AbortController();
      setTimeout(() => controller.abort(), 10_000); // set a timeout
      const stream = addAbortSignal(
        controller.signal,
        fs.createReadStream(('object.json'))
      );
      (async () => {
        try {
          for await (const chunk of stream) {
            await process(chunk);
          }
        } catch (e) {
          if (e.name === 'AbortError') {
            // The operation was cancelled
          } else {
            throw e;
          }
        }
      })();

  since

    v15.4.0

  #### Type parameters

  - #### T: <internal>.Stream<T>

  #### Parameters

  - ##### signal: AbortSignal

    A signal representing possible cancellation

  - ##### stream: T

    a stream to attach a signal to

  #### Returns T

### finished

- finished(stream: ReadableStream | WritableStream | ReadWriteStream, options: FinishedOptions, callback: (err?: ErrnoException) => void): () => void
- finished(stream: ReadableStream | WritableStream | ReadWriteStream, callback: (err?: ErrnoException) => void): () => void

- A function to get notified when a stream is no longer readable, writable or has experienced an error or a premature close event.

      const { finished } = require('stream');

      const rs = fs.createReadStream('archive.tar');

      finished(rs, (err) => {
        if (err) {
          console.error('Stream failed.', err);
        } else {
          console.log('Stream is done reading.');
        }
      });

      rs.resume(); // Drain the stream.

  Especially useful in error handling scenarios where a stream is destroyed prematurely (like an aborted HTTP request), and will not emit `'end'`or `'finish'`.

  The `finished` API provides promise version:

      const { finished } = require('stream/promises');

      const rs = fs.createReadStream('archive.tar');

      async function run() {
        await finished(rs);
        console.log('Stream is done reading.');
      }

      run().catch(console.error);
      rs.resume(); // Drain the stream.

  `stream.finished()` leaves dangling event listeners (in particular`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been invoked. The reason for this is so that unexpected `'error'` events (due to incorrect stream implementations) do not cause unexpected crashes. If this is unwanted behavior then the returned cleanup function needs to be invoked in the callback:

      const cleanup = finished(rs, (err) => {
        cleanup();
        // ...
      });

  since

    v10.0.0

  #### Parameters

  - ##### stream: ReadableStream | WritableStream | ReadWriteStream

    A readable and/or writable stream.

  - ##### options: FinishedOptions

  - ##### callback: (err?: ErrnoException) => void

    A callback function that takes an optional error argument.

    - - (err?: ErrnoException): void

      - #### Parameters

        - ##### Optional err: ErrnoException

        #### Returns void

  #### Returns () => void

  A cleanup function which removes all registered listeners.

  - - (): void

    - A function to get notified when a stream is no longer readable, writable or has experienced an error or a premature close event.

          const { finished } = require('stream');

          const rs = fs.createReadStream('archive.tar');

          finished(rs, (err) => {
            if (err) {
              console.error('Stream failed.', err);
            } else {
              console.log('Stream is done reading.');
            }
          });

          rs.resume(); // Drain the stream.

      Especially useful in error handling scenarios where a stream is destroyed prematurely (like an aborted HTTP request), and will not emit `'end'`or `'finish'`.

      The `finished` API provides promise version:

          const { finished } = require('stream/promises');

          const rs = fs.createReadStream('archive.tar');

          async function run() {
            await finished(rs);
            console.log('Stream is done reading.');
          }

          run().catch(console.error);
          rs.resume(); // Drain the stream.

      `stream.finished()` leaves dangling event listeners (in particular`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been invoked. The reason for this is so that unexpected `'error'` events (due to incorrect stream implementations) do not cause unexpected crashes. If this is unwanted behavior then the returned cleanup function needs to be invoked in the callback:

          const cleanup = finished(rs, (err) => {
            cleanup();
            // ...
          });

      since

        v10.0.0

      #### Returns void

      A cleanup function which removes all registered listeners.

- #### Parameters

  - ##### stream: ReadableStream | WritableStream | ReadWriteStream

  - ##### callback: (err?: ErrnoException) => void

    - - (err?: ErrnoException): void

      - #### Parameters

        - ##### Optional err: ErrnoException

        #### Returns void

  #### Returns () => void

  - - (): void

    - #### Returns void

### pipeline

- pipeline<A, B>(source: A, destination: B, callback?: PipelineCallback<B>): B extends WritableStream ? B : WritableStream
- pipeline<A, T1, B>(source: A, transform1: T1, destination: B, callback?: PipelineCallback<B>): B extends WritableStream ? B : WritableStream
- pipeline<A, T1, T2, B>(source: A, transform1: T1, transform2: T2, destination: B, callback?: PipelineCallback<B>): B extends WritableStream ? B : WritableStream
- pipeline<A, T1, T2, T3, B>(source: A, transform1: T1, transform2: T2, transform3: T3, destination: B, callback?: PipelineCallback<B>): B extends WritableStream ? B : WritableStream
- pipeline<A, T1, T2, T3, T4, B>(source: A, transform1: T1, transform2: T2, transform3: T3, transform4: T4, destination: B, callback?: PipelineCallback<B>): B extends WritableStream ? B : WritableStream
- pipeline(streams: readonly (ReadableStream | WritableStream | ReadWriteStream)[], callback?: (err: ErrnoException) => void): WritableStream
- pipeline(stream1: ReadableStream, stream2: WritableStream | ReadWriteStream, ...streams: (WritableStream | ReadWriteStream | ((err: ErrnoException) => void))[]): WritableStream

- A module method to pipe between streams and generators forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.

      const { pipeline } = require('stream');
      const fs = require('fs');
      const zlib = require('zlib');

      // Use the pipeline API to easily pipe a series of streams
      // together and get notified when the pipeline is fully done.

      // A pipeline to gzip a potentially huge tar file efficiently:

      pipeline(
        fs.createReadStream('archive.tar'),
        zlib.createGzip(),
        fs.createWriteStream('archive.tar.gz'),
        (err) => {
          if (err) {
            console.error('Pipeline failed.', err);
          } else {
            console.log('Pipeline succeeded.');
          }
        }
      );

  The `pipeline` API provides a promise version, which can also receive an options argument as the last parameter with a`signal` `AbortSignal` property. When the signal is aborted,`destroy` will be called on the underlying pipeline, with an`AbortError`.

      const { pipeline } = require('stream/promises');

      async function run() {
        await pipeline(
          fs.createReadStream('archive.tar'),
          zlib.createGzip(),
          fs.createWriteStream('archive.tar.gz')
        );
        console.log('Pipeline succeeded.');
      }

      run().catch(console.error);

  To use an `AbortSignal`, pass it inside an options object, as the last argument:

      const { pipeline } = require('stream/promises');

      async function run() {
        const ac = new AbortController();
        const signal = ac.signal;

        setTimeout(() => ac.abort(), 1);
        await pipeline(
          fs.createReadStream('archive.tar'),
          zlib.createGzip(),
          fs.createWriteStream('archive.tar.gz'),
          { signal },
        );
      }

      run().catch(console.error); // AbortError

  The `pipeline` API also supports async generators:

      const { pipeline } = require('stream/promises');
      const fs = require('fs');

      async function run() {
        await pipeline(
          fs.createReadStream('lowercase.txt'),
          async function* (source, signal) {
            source.setEncoding('utf8');  // Work with strings rather than `Buffer`s.
            for await (const chunk of source) {
              yield await processChunk(chunk, { signal });
            }
          },
          fs.createWriteStream('uppercase.txt')
        );
        console.log('Pipeline succeeded.');
      }

      run().catch(console.error);

  Remember to handle the `signal` argument passed into the async generator. Especially in the case where the async generator is the source for the pipeline (i.e. first argument) or the pipeline will never complete.

      const { pipeline } = require('stream/promises');
      const fs = require('fs');

      async function run() {
        await pipeline(
          async function * (signal) {
            await someLongRunningfn({ signal });
            yield 'asd';
          },
          fs.createWriteStream('uppercase.txt')
        );
        console.log('Pipeline succeeded.');
      }

      run().catch(console.error);

  `stream.pipeline()` will call `stream.destroy(err)` on all streams except:

  - `Readable` streams which have emitted `'end'` or `'close'`.
  - `Writable` streams which have emitted `'finish'` or `'close'`.

  `stream.pipeline()` leaves dangling event listeners on the streams after the `callback` has been invoked. In the case of reuse of streams after failure, this can cause event listener leaks and swallowed errors.

  since

    v10.0.0

  #### Type parameters

  - #### A: PipelineSource<any>

  - #### B: WritableStream | PipelineDestinationIterableFunction<any> | PipelineDestinationPromiseFunction<any, any> | PipelineDestinationIterableFunction<string | <internal>.Buffer> | PipelineDestinationPromiseFunction<string | <internal>.Buffer, any>

  #### Parameters

  - ##### source: A

  - ##### destination: B

  - ##### Optional callback: PipelineCallback<B>

    Called when the pipeline is fully done.

  #### Returns B extends WritableStream ? B : WritableStream

- #### Type parameters

  - #### A: PipelineSource<any>

  - #### T1: PipelineTransform<A, any>

  - #### B: WritableStream | PipelineDestinationIterableFunction<any> | PipelineDestinationPromiseFunction<any, any> | PipelineDestinationIterableFunction<string | <internal>.Buffer> | PipelineDestinationPromiseFunction<string | <internal>.Buffer, any>

  #### Parameters

  - ##### source: A

  - ##### transform1: T1

  - ##### destination: B

  - ##### Optional callback: PipelineCallback<B>

  #### Returns B extends WritableStream ? B : WritableStream

- #### Type parameters

  - #### A: PipelineSource<any>

  - #### T1: PipelineTransform<A, any>

  - #### T2: PipelineTransform<T1, any>

  - #### B: WritableStream | PipelineDestinationIterableFunction<any> | PipelineDestinationPromiseFunction<any, any> | PipelineDestinationIterableFunction<string | <internal>.Buffer> | PipelineDestinationPromiseFunction<string | <internal>.Buffer, any>

  #### Parameters

  - ##### source: A

  - ##### transform1: T1

  - ##### transform2: T2

  - ##### destination: B

  - ##### Optional callback: PipelineCallback<B>

  #### Returns B extends WritableStream ? B : WritableStream

- #### Type parameters

  - #### A: PipelineSource<any>

  - #### T1: PipelineTransform<A, any>

  - #### T2: PipelineTransform<T1, any>

  - #### T3: PipelineTransform<T2, any>

  - #### B: WritableStream | PipelineDestinationIterableFunction<any> | PipelineDestinationPromiseFunction<any, any> | PipelineDestinationIterableFunction<string | <internal>.Buffer> | PipelineDestinationPromiseFunction<string | <internal>.Buffer, any>

  #### Parameters

  - ##### source: A

  - ##### transform1: T1

  - ##### transform2: T2

  - ##### transform3: T3

  - ##### destination: B

  - ##### Optional callback: PipelineCallback<B>

  #### Returns B extends WritableStream ? B : WritableStream

- #### Type parameters

  - #### A: PipelineSource<any>

  - #### T1: PipelineTransform<A, any>

  - #### T2: PipelineTransform<T1, any>

  - #### T3: PipelineTransform<T2, any>

  - #### T4: PipelineTransform<T3, any>

  - #### B: WritableStream | PipelineDestinationIterableFunction<any> | PipelineDestinationPromiseFunction<any, any> | PipelineDestinationIterableFunction<string | <internal>.Buffer> | PipelineDestinationPromiseFunction<string | <internal>.Buffer, any>

  #### Parameters

  - ##### source: A

  - ##### transform1: T1

  - ##### transform2: T2

  - ##### transform3: T3

  - ##### transform4: T4

  - ##### destination: B

  - ##### Optional callback: PipelineCallback<B>

  #### Returns B extends WritableStream ? B : WritableStream

- #### Parameters

  - ##### streams: readonly (ReadableStream | WritableStream | ReadWriteStream)[]

  - ##### Optional callback: (err: ErrnoException) => void

    - - (err: ErrnoException): void

      - #### Parameters

        - ##### err: ErrnoException

        #### Returns void

  #### Returns WritableStream

- #### Parameters

  - ##### stream1: ReadableStream

  - ##### stream2: WritableStream | ReadWriteStream

  - ##### Rest ...streams: (WritableStream | ReadWriteStream | ((err: ErrnoException) => void))[]

  #### Returns WritableStream

Generated using TypeDoc