Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

ReadableStream.pipeThrough(): transforming a stream

pipeThrough() routes stream data to a supplied pair’s writable side and immediately returns its readable side. It supports chaining transformations with backpressure propagation; it does not return a completion promise.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

source.pipeThrough({ writable, readable }, { preventClose, preventAbort, preventCancel, signal })

ReadableStream.pipeThrough() is an instance method defined by WHATWG Streams, not ECMAScript. It accepts a TransformStream or a { writable, readable } pair: it connects the source to writable and returns exactly readable, not a Promise. For an arbitrary pair, the method itself does not connect the two sides.

Piping locks the source and destination writable for the operation; an existing lock on either causes a synchronous TypeError. A chain using TransformStream propagates backpressure. Consume the result: without a consumer, transformation can stall. In contrast, pipeTo() returns a promise for completion of piping.

Options apply to the source → writable connection. By default, source closure closes the destination, source failure aborts the destination, and destination failure cancels the source. Respectively, preventClose, preventAbort, and preventCancel disable these behaviors; all default to false. signal allows aborting the operation, subject to those options. Observe errors while consuming the result; pipeThrough() does not expose its internal piping promise.

The example doubles two chunks and awaits the final pipeTo(). Check availability of the APIs in your target browser; having Node 24 in a repository does not establish browser support.

Examples

async function main() {
  const source = new ReadableStream({
    start(controller) {
      controller.enqueue(2);
      controller.enqueue(4);
      controller.close();
    }
  });

  const doubled = source.pipeThrough(new TransformStream({
    transform(chunk, controller) {
      controller.enqueue(chunk * 2);
    }
  }));

  const values = [];
  await doubled.pipeTo(new WritableStream({
    write(chunk) {
      values.push(chunk);
    }
  }));
  console.log(JSON.stringify(values));
}

main().catch(console.error);

Result

[4,8]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources