Détail du package

node-readable-to-web-readable-stream

Borewit1.3kMIT0.4.2

Convert Node Readable to Web API ReadableStream

Readable, Stream, Node.js, Node

readme

CI NPM version NPM downloads

node-readable-to-web-readable-stream

node-readable-to-web-readable-stream is a utility that converts a Node.js stream.Readable stream into a Web API ReadableStream. This is particularly useful for integrating Node.js streams with web-native streaming APIs.

To convert in the opposite direction, see may use readable-web-to-node-stream instead.

Installation

Install the package using npm:

npm install node-readable-to-web-readable-stream

Or with yarn:

yarn add node-readable-to-web-readable-stream

Usage

You can either convert to a WHATWG / Web API ReadableStream byte mode, or default mode.

Here's how you can use this utility to convert a Node.js stream.Readable stream into a byte WHATWG / Web API ReadableStream: If you want to use a ReadableStreamBYOBReader you should use this method.

import {makeByteReadableStreamFromNodeReadable} from 'node-readable-to-web-readable-stream';
import {createReadStream} from 'fs';

// Create a Node.js Readable stream
const nodeReadable = fs.createReadStream('example.txt');

// Convert to a web ReadableStream
const webReadable = makeByteReadableStreamFromNodeReadable(nodeReadable);

// Now you can use webReadable as a WHATWG ReadableStream in byte mode

If you want to use a ReadableStreamDefaultReader you should use this method.

import {makeDefaultReadableStreamFromNodeReadable} from 'node-readable-to-web-readable-stream';
import {createReadStream} from 'fs';

// Create a Node.js Readable stream
const nodeReadable = fs.createReadStream('example.txt');

// Convert to a web ReadableStream
const webReadable = makeDefaultReadableStreamFromNodeReadable(nodeReadable);

// Now you can use webReadable as a WHATWG default ReadableStream

Compatibility

This is an ECMAScript Module (ESM). Cross-platform compliant:

  • Node.js ≥ 18
  • Bum ≥ 1.2
  • Modern web browsers

You can load the project with require in Node.js ≥ 22

Features

  • Supports stream backpressure
  • BYOB (Bring Your Own Buffer) compliant

API

toWebReadableStream(nodeReadable, options)

License

This project is licensed under the MIT License. See the LICENSE file for details.