Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Streams and readline

Node.js Readable.toWeb() and Readable.fromWeb()

Reference for converting between Node.js streams and web streams: Readable.toWeb(), fromWeb(), Writable and Duplex adapters, options and caveats.

Node.js has two stream families: the classic stream.Readable/Writable/Duplex classes and the web ReadableStream/WritableStream/TransformStream. A set of static adapter methods converts between them, so you can feed a file stream to fetch() or read a fetch() body with pipeline().

The adapters live on the classic classes: Readable.toWeb(readable) and Readable.fromWeb(webReadable), the same pair on Writable and Duplex. Conversion wires up backpressure, errors and cancellation in both directions, so you do not copy data by hand.

NODE
import { Readable } from 'node:stream';

const web = Readable.toWeb(nodeReadable);      // Node stream -> web stream
const node = Readable.fromWeb(web);            // web stream  -> Node stream

Try it

Node.js Node Readable to web ReadableStream and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Writable and Duplex adapters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
Readable.toWeb(streamReadable, options?): ReadableStream
Readable.fromWeb(readableStream, options?): Readable
Writable.toWeb(streamWritable): WritableStream
Writable.fromWeb(writableStream, options?): Writable
Duplex.toWeb(streamDuplex): { readable, writable }
Duplex.fromWeb({ readable, writable }, options?): Duplex
Name Description
streamReadable, streamWritable, streamDuplex The Node.js stream to adapt.
readableStream, writableStream The web stream to adapt.
options.strategy For toWeb() on a Readable: a queuing strategy (highWaterMark, size).
options.type For Readable.toWeb(): set to 'bytes' to create a byte stream.
options (fromWeb) Normal stream options such as objectMode, highWaterMark, encoding, signal.

Returns: the converted stream, or a { readable, writable } pair for Duplex.toWeb().

Errors: passing a stream that is already locked, disturbed or destroyed throws ERR_INVALID_STATE; the wrong type throws ERR_INVALID_ARG_TYPE.

Usage notes

  • Ownership moves. After toWeb() the original stream belongs to the adapter. Do not read from or write to it directly.
  • Chunk types differ. Node Readables emit Buffers; web byte consumers expect Uint8Array. Buffer is a Uint8Array, so it usually just works, but in fromWeb() set objectMode: true if the web stream carries strings or objects.
  • Errors and destroy propagate. An error on one side errors the other, and cancelling a web stream destroys the Node stream.
  • fetch() bodies. Readable.fromWeb(response.body) turns a response into something pipeline() understands. In the other direction pass Readable.toWeb(stream) as a request body together with duplex: 'half'.
  • Cost. The adapters add a layer of buffering. For hot paths, stay in one family.
  • Stability. These methods are marked experimental in older Node.js versions; check the stability index for your release.