from2: Readable Stream Wrapper

2.3.0 · abandoned · verified Tue Apr 21

from2 is a convenience wrapper for Node.js `ReadableStream` (specifically, the `readable-stream` base class), designed to simplify the creation of readable streams while correctly handling backpressure. The current stable version is 2.3.0, published in 2016, indicating the project is likely abandoned or in long-term maintenance with no active development or planned release cadence. It differentiates itself by offering an API inspired by `from` and `through2`, providing `from2.obj` for object mode streams and `from2.ctor` for creating reusable stream constructors, which can improve performance for multiple similar streams. It aims to make stream creation more approachable than direct `ReadableStream` implementation.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a basic readable stream using `from2` that emits chunks of a given string, handling backpressure appropriately, and then pipes it to standard output.

const from = require('from2');

function fromString(string) {
  return from(function(size, next) {
    // if there's no more content
    // left in the string, close the stream.
    if (string.length <= 0) return next(null, null);

    // Pull in a new chunk of text,
    // removing it from the string.
    var chunk = string.slice(0, size);
    string = string.slice(size);

    // Emit "chunk" from the stream.
    next(null, chunk);
  });
}

// pipe "hello world" out
// to stdout.
fromString('hello world').pipe(process.stdout);

view raw JSON →