Registry / serialization / node-readable-to-web-readable-stream

node-readable-to-web-readable-stream

JSON →
library0.4.2jsnpmunverified

node-readable-to-web-readable-stream is a focused utility library designed to bridge the gap between Node.js `stream.Readable` and the Web API `ReadableStream`. It provides functions to convert Node.js readable streams into either byte-mode (BYOB-compliant) or default-mode WHATWG ReadableStreams, enabling seamless integration with web-native streaming APIs. The current stable version is `0.4.2`, with releases occurring relatively frequently to address bug fixes and introduce enhancements, such as improved backpressure mechanisms and default stream support. Key differentiators include its explicit support for BYOB readers, robust backpressure handling, and cross-platform compatibility across Node.js (>=18), Bun (>=1.2), and modern web browsers. It is distributed as an ECMAScript Module (ESM).

npm install node-readable-to-web-readable-stream
INSTALL
IMPORT
SIG · NODE-READABLE-TO-W
N
node-readable-to-web-readable-stream
serializationjavascriptv0.4.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

makeByteReadableStreamFromNodeReadable
import { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';
const makeByteReadableStreamFromNodeReadable = require('node-readable-to-web-readable-stream').makeByteReadableStreamFromNodeReadable;
This package is primarily an ESM. CommonJS `require()` is only officially supported for Node.js >= 22.
makeDefaultReadableStreamFromNodeReadable
import { makeDefaultReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';
const { makeDefaultReadableStreamFromNodeReadable } = require('node-readable-to-web-readable-stream');
Added in v0.4.0. For converting to a default-mode Web ReadableStream, often used with ReadableStreamDefaultReader.
toWebReadableStream
import { toWebReadableStream } from 'node-readable-to-web-readable-stream';
const toWebReadableStream = require('node-readable-to-web-readable-stream').toWebReadableStream;
A general conversion function documented in the API. The `makeByteReadableStreamFromNodeReadable` and `makeDefaultReadableStreamFromNodeReadable` functions are generally preferred for explicit stream type conversion.
NodeJS.ReadableStream
import type { Readable } from 'node:stream';
When providing a Node.js Readable stream, import its type from `node:stream` for type safety.

Demonstrates converting a Node.js `fs.createReadStream` into a Web API `ReadableStream` (byte mode) and consuming it with a BYOB reader.

import { createReadStream } from 'node:fs'; import { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream'; // Imagine 'large-file.bin' is a large binary file // Create a Node.js Readable stream const nodeReadable = createReadStream('large-file.bin'); // Convert to a web ReadableStream, specifically a byte stream for BYOB support const webReadable = makeByteReadableStreamFromNodeReadable(nodeReadable); // Example: Consume the webReadable using a BYOB reader async function readStream(stream: ReadableStream<Uint8Array>) { const reader = stream.getReader({ mode: 'byob' }); try { while (true) { const { value, done } = await reader.read(new Uint8Array(1024)); // Read into a pre-allocated buffer if (done) { console.log('Stream finished.'); break; } if (value) { console.log(`Read ${value.byteLength} bytes.`); // Process the chunk here } } } catch (error) { console.error('Error reading stream:', error); } finally { reader.releaseLock(); } } // In a real application, you'd ensure 'large-file.bin' exists or create it. // For demonstration, we'll create a dummy file if it doesn't exist. import { promises as fsPromises } from 'node:fs'; async function setupAndRun() { try { await fsPromises.access('large-file.bin'); } catch (error) { await fsPromises.writeFile('large-file.bin', Buffer.alloc(1024 * 1024, 'A')); // 1MB dummy file console.log('Created dummy large-file.bin'); } readStream(webReadable); } setupAndRun();
Debug
Known issues
gotchaThis package is an ECMAScript Module (ESM). While it can be loaded with `require()` in Node.js, this functionality is officially supported only for Node.js versions 22 and higher. For older Node.js versions or mixed CJS/ESM projects, direct `import` statements are required.
fix
Use `import` statements: `import { ... } from 'node-readable-to-web-readable-stream';` or ensure your project's `package.json` specifies `"type": "module"` and uses Node.js >= 22 for `require()` interoperability.
affects: <=21.x
gotchaVersions prior to `0.4.1` contained a bug that could cause the converted byte `ReadableStream` to hang, especially when reading the remainder of the stream or after cancellation.
fix
Upgrade to version `0.4.1` or newer to resolve stream hanging issues. The fix was applied in v0.4.1.
affects: <0.4.1
gotchaThe documentation for the general `toWebReadableStream` function exists, but the primary usage examples and recent enhancements (like `makeDefaultReadableStreamFromNodeReadable` in v0.4.0) heavily emphasize the more explicit `makeByteReadableStreamFromNodeReadable` and `makeDefaultReadableStreamFromNodeReadable` functions. Relying on the `make*` functions provides clearer intent.
fix
Prefer `makeByteReadableStreamFromNodeReadable` for BYOB-compatible byte streams or `makeDefaultReadableStreamFromNodeReadable` for default-mode streams, as these are the idiomatic and most actively developed conversion paths.
affects: >=0.1.1
Errors
Common errors & fixes
TypeError: makeByteReadableStreamFromNodeReadable is not a function
Attempting to `require()` the module in a Node.js version older than 22, or in a CommonJS context where ESM imports are not correctly transpiled or handled.
fix
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';`. If using Node.js < 22, you cannot reliably use `require()` for this package.
Web ReadableStream stops emitting data unexpectedly or seems to freeze.
This is often a symptom of the stream hanging bug fixed in earlier versions, where the converted stream would not properly signal completion or handle cancellation.
fix
Upgrade the package to version `0.4.1` or newer. This version specifically addressed issues causing converted byte ReadableStreams to hang.
Upgrade
Version history
0.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
node-readable-to-web-readable-stream — npm install node-readable-to-web-readable-stream · libregistry