Node.js Readable Stream to Web ReadableStream Converter

1.0.1 · deprecated · verified Tue Apr 21

The `readable-stream-node-to-web` package offers a utility to convert a Node.js `Readable` stream into a WHATWG `ReadableStream`, commonly known as a web stream. This bridge allows developers to use Node.js stream-producing modules within web-native contexts such as Service Workers or modern browser environments, where web streams are the standard. The current and last published stable version is 1.0.1, released approximately nine years ago. Due to its age and the subsequent introduction of native stream conversion utilities directly within Node.js (e.g., `stream.Readable.toWeb()` since Node.js v17), this package is largely superseded. Its development is effectively halted, and users are encouraged to explore native Node.js APIs or more actively maintained alternatives for stream interoperability. The module was written in ES5 and was compatible with bundlers like Browserify and Webpack without needing transpilation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting a mock Node.js Readable stream to a WHATWG ReadableStream and then consuming its data using a web stream reader.

import { Readable } from 'node:stream';
import nodeToWebStream from 'readable-stream-node-to-web';

// Create a mock Node.js Readable stream
class MockNodeReadable extends Readable {
  constructor(dataArray) {
    super();
    this.data = [...dataArray];
  }

  _read() {
    if (this.data.length) {
      this.push(this.data.shift());
    } else {
      this.push(null);
    }
  }
}

const nodeStream = new MockNodeReadable(['Hello', ' ', 'World', '!']);
const webStream = nodeToWebStream(nodeStream);

// Consume the web stream
async function consumeWebStream() {
  const reader = webStream.getReader();
  let result = '';
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    result += new TextDecoder().decode(value);
  }
  console.log('Consumed Web Stream:', result);
}

consumeWebStream();

view raw JSON →