Node.js Web Stream Adapters
This package provides utilities to convert Node.js `Readable` and `Writable` streams into Web `ReadableStream` and `WritableStream` objects, and vice-versa. It aims to bridge the gap between Node.js's native stream API and the WHATWG Web Streams API, offering stable conversion functions. This is particularly useful because Node.js's own `Readable.toWeb`, `Writable.toWeb`, `Readable.fromWeb`, and `Writable.fromWeb` methods are marked as experimental as of Node.js version 22. The package is currently at version 0.2.1, indicating an early development stage, though it addresses a stable need. It is likely maintained as needed to keep pace with Node.js and Web Streams API evolutions. Key differentiators include providing a non-experimental, stable API surface for stream conversion.
Common errors
-
ERR_MODULE_NOT_FOUND: Cannot find module 'node-readable-stream' from '/path/to/your/file.js'
cause You are trying to import from 'node-readable-stream' as shown in the README, but you have installed 'node-web-stream-adapters'.fixChange your import statements to `import { ... } from 'node-web-stream-adapters';`. -
TypeError: (0, _nodeWebStreamAdapters.toReadableStream) is not a function
cause This error typically occurs when trying to use CommonJS `require()` syntax (e.g., `const { toReadableStream } = require('node-web-stream-adapters');`) with an ES module-only package, or when incorrectly importing a named export as a default or vice-versa.fixEnsure you are using ES module `import` syntax: `import { toReadableStream } from 'node-web-stream-adapters';` and check that `type: "module"` is set in your `package.json` if running in Node.js, or use a bundler that handles ESM correctly.
Warnings
- breaking The documentation (README) and the provided GitHub URL (`https://github.com/bstefanescu/node-readable-stream`) explicitly refer to `node-readable-stream` as the package name for installation and imports. However, the npm metadata provided specifies `node-web-stream-adapters`. This discrepancy is a critical point of confusion and will lead to `ERR_MODULE_NOT_FOUND` if the README's instructions are followed with the `node-web-stream-adapters` package installed.
- gotcha Node.js core's native `Readable.toWeb`, `Writable.toWeb`, `Readable.fromWeb`, and `Writable.fromWeb` methods are currently experimental (as of Node.js 22). While this library provides stable alternatives, be aware that future Node.js versions might stabilize their native implementations, potentially deprecating or superseding this library's approach. This package aims to offer stability where native Node.js methods are still in flux.
- gotcha As a relatively new package (version 0.2.1), its API surface might undergo breaking changes in minor or patch releases, even though semantic versioning is typically followed. Low version numbers often indicate that the API is still finding its final form.
Install
-
npm install node-web-stream-adapters -
yarn add node-web-stream-adapters -
pnpm add node-web-stream-adapters
Imports
- toReadableStream
const { toReadableStream } = require('node-web-stream-adapters');import { toReadableStream } from 'node-web-stream-adapters'; - createReadableStreamFromReadable
import { createReadableStreamFromReadable } from 'node-web-stream-adapters'; - createReadableStreamFromBuffer
import { createReadableStreamFromBuffer } from 'node-web-stream-adapters';
Quickstart
import fs from "fs/promises";
import { createReadableStreamFromBuffer } from "node-web-stream-adapters";
async function processFileAsStream() {
try {
const filePath = 'some-file.txt';
// Assume 'some-file.txt' exists and has content.
// In a real scenario, you might read a large file or network response.
const buffer = await fs.readFile(filePath);
const webStream = createReadableStreamFromBuffer(buffer);
console.log('Created Web ReadableStream from buffer.');
// Example: Consume the web stream (e.g., pipe to another writable stream or process chunks)
const reader = webStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log('Stream finished.');
break;
}
console.log('Received chunk:', new TextDecoder().decode(value));
}
} catch (error) {
console.error('Error processing stream:', error);
}
}
// Create a dummy file for the example to run
async function setupDummyFile() {
const content = 'Hello, Web Streams!\nThis is a test file.';
await fs.writeFile('some-file.txt', content);
console.log('Dummy file created: some-file.txt');
await processFileAsStream();
}
setupDummyFile();