Web Streams interop
Local practice: Node.js 22.16.0 on Linux x64; proposed 26.13.1 baseline remains untested. Save each example separately as main.mjs in a disposable directory. No packages or external services required. HTTP examples bind only to loopback and shut down after a finite scenario. Static Node.js code, not verified ByteMind execution.
Web readable to Node readable
import { Readable, Writable, Transform } from 'node:stream';import { pipeline, finished } from 'node:stream/promises';const web = new ReadableStream({start(c){c.enqueue(new Uint8Array([65,66]));c.close();}});const result=[];for await(const chunk of Readable.fromWeb(web)) result.push(...chunk);console.log(result.join(','));Run:
node main.mjsExpected stdout:
65,66Exit status: 0. Expected stderr: empty.
How it works
Web streams use reader locks and read promises, whereas Node streams expose methods and events such as pipe and data. Readable.fromWeb converts a Web readable into a Node readable. Readable.toWeb converts the other way. These adapters are available on the local 22.16.0 runtime, where the documented stability classification is experimental; recheck later releases. Use one consumer at a time and cancel or release an unfinished Web reader appropriately.
Node readable to Web reader
import { Readable, Writable, Transform } from 'node:stream';import { pipeline, finished } from 'node:stream/promises';const web = Readable.toWeb(Readable.from([Buffer.from('ok')],{objectMode:false}));const reader = web.getReader();try { const first=await reader.read(); console.log(new TextDecoder().decode(first.value)); console.log((await reader.read()).done); }finally {reader.releaseLock();}Run:
node main.mjsExpected stdout:
oktrueExit status: 0. Expected stderr: empty.
Common mistake
A Web reader is not a Node EventEmitter. Do not try to call on("data") on it or acquire a second reader while locked.
Practice
Add another byte chunk to the Web source. Confirm that converting to a Node stream preserves the bytes, not necessarily the original chunk boundaries.
Further reading: Authoritative source 1. Internal reference routes remain proposed until repository discovery.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Web Streams interop — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which adapter converts a Web readable into a Node readable?
What does a Web reader lock prevent?
Does an adapter guarantee application record boundaries?