Convert Figma logo to code with AI

nodejs logoreadable-stream

Node-core streams for userland

1,031
225
1,031
13

Top Related Projects

106,466

Node.js JavaScript runtime ✨🐢🚀✨

EventStream is like functional programming meets IO

Quick Overview

The nodejs/readable-stream repository is a mirror of the Streams2 and Streams3 implementations in Node.js core. It provides a compatibility layer for older Node.js versions and browsers, allowing developers to use the latest stream implementations across different environments.

Pros

  • Ensures consistent stream behavior across different Node.js versions
  • Allows use of modern stream implementations in older environments
  • Improves code portability and compatibility
  • Well-maintained and actively developed

Cons

  • Adds an extra dependency to projects
  • May introduce a slight performance overhead compared to native implementations
  • Requires additional configuration for optimal browser usage
  • Learning curve for developers unfamiliar with the Streams API

Code Examples

  1. Creating a readable stream:
const { Readable } = require('readable-stream');

const myStream = new Readable({
  read(size) {
    this.push('Hello, world!');
    this.push(null); // Signals the end of the stream
  }
});

myStream.on('data', (chunk) => {
  console.log(chunk.toString());
});
  1. Piping streams:
const { Readable, Writable } = require('readable-stream');

const readableStream = new Readable({
  read() {}
});

const writableStream = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});

readableStream.pipe(writableStream);

readableStream.push('Data to be piped');
readableStream.push(null);
  1. Using transform streams:
const { Transform } = require('readable-stream');

const upperCaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

process.stdin.pipe(upperCaseTransform).pipe(process.stdout);

Getting Started

To use readable-stream in your project, follow these steps:

  1. Install the package:

    npm install readable-stream
    
  2. Import the desired stream classes in your code:

    const { Readable, Writable, Transform, Duplex } = require('readable-stream');
    
  3. Use the imported classes to create and manipulate streams as needed:

    const myReadable = new Readable({
      read() {
        // Implementation
      }
    });
    
    const myWritable = new Writable({
      write(chunk, encoding, callback) {
        // Implementation
      }
    });
    
  4. For browser usage, consider using a bundler like webpack or browserify to handle the Node.js-style require statements.

Competitor Comparisons

106,466

Node.js JavaScript runtime ✨🐢🚀✨

Pros of node

  • Comprehensive platform for server-side JavaScript development
  • Includes a wide range of built-in modules and APIs
  • Active development with frequent updates and improvements

Cons of node

  • Larger codebase and more complex architecture
  • Potentially steeper learning curve for beginners
  • May include unnecessary features for projects only needing stream functionality

Code Comparison

node:

const { Readable } = require('stream');

const readable = new Readable({
  read(size) {
    // Implementation
  }
});

readable-stream:

const { Readable } = require('readable-stream');

const readable = new Readable({
  read(size) {
    // Implementation
  }
});

Summary

node is a full-featured JavaScript runtime environment, while readable-stream focuses specifically on providing a streams implementation. node offers a more comprehensive solution for server-side development but may be overkill for projects only requiring stream functionality. readable-stream provides a more lightweight alternative for stream-specific use cases and can be used as a polyfill for older Node.js versions.

Both repositories implement the streams API, with readable-stream aiming to maintain compatibility with node's implementation. The code usage is nearly identical, making it easy to switch between the two depending on project requirements.

EventStream is like functional programming meets IO

Pros of event-stream

  • Simpler API with fewer methods, making it easier to learn and use
  • Lightweight and focused on basic stream operations
  • More flexible with custom stream transformations

Cons of event-stream

  • Less comprehensive than readable-stream, lacking some advanced features
  • Not as actively maintained or updated
  • May have compatibility issues with newer Node.js versions

Code Comparison

event-stream:

var es = require('event-stream')

es.pipeline(
  fs.createReadStream('input.txt'),
  es.split(),
  es.map(function (line, cb) {
    cb(null, line.toUpperCase())
  }),
  fs.createWriteStream('output.txt')
)

readable-stream:

const { pipeline, Transform } = require('readable-stream')
const fs = require('fs')

pipeline(
  fs.createReadStream('input.txt'),
  new Transform({
    transform(chunk, encoding, callback) {
      this.push(chunk.toString().toUpperCase())
      callback()
    }
  }),
  fs.createWriteStream('output.txt'),
  (err) => {
    if (err) console.error('Pipeline failed', err)
    else console.log('Pipeline succeeded')
  }
)

The code examples show that event-stream provides a more concise API for stream operations, while readable-stream offers a more standardized and robust approach with built-in error handling. readable-stream's implementation is closer to the native Node.js streams, ensuring better compatibility and performance in most cases.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

readable-stream

Node.js core streams for userland

npm status node Node.js Build Browsers Build

npm install readable-stream

This package is a mirror of the streams implementations in Node.js 18.19.0.

Full documentation may be found on the Node.js website.

If you want to guarantee a stable streams base, regardless of what version of Node you, or the users of your libraries are using, use readable-stream only and avoid the "stream" module in Node-core, for background see this blogpost.

As of version 2.0.0 readable-stream uses semantic versioning.

Version 4.x.x

v4.x.x of readable-stream is a cut from Node 18. This version supports Node 12, 14, 16 and 18, as well as evergreen browsers. The breaking changes introduced by v4 are composed of the combined breaking changes in:

This also includes many new features.

Version 3.x.x

v3.x.x of readable-stream is a cut from Node 10. This version supports Node 6, 8, and 10, as well as evergreen browsers, IE 11 and latest Safari. The breaking changes introduced by v3 are composed by the combined breaking changes in Node v9 and Node v10, as follows:

  1. Error codes: https://github.com/nodejs/node/pull/13310, https://github.com/nodejs/node/pull/13291, https://github.com/nodejs/node/pull/16589, https://github.com/nodejs/node/pull/15042, https://github.com/nodejs/node/pull/15665, https://github.com/nodejs/readable-stream/pull/344
  2. 'readable' have precedence over flowing https://github.com/nodejs/node/pull/18994
  3. make virtual methods errors consistent https://github.com/nodejs/node/pull/18813
  4. updated streams error handling https://github.com/nodejs/node/pull/18438
  5. writable.end should return this. https://github.com/nodejs/node/pull/18780
  6. readable continues to read when push('') https://github.com/nodejs/node/pull/18211
  7. add custom inspect to BufferList https://github.com/nodejs/node/pull/17907
  8. always defer 'readable' with nextTick https://github.com/nodejs/node/pull/17979

Version 2.x.x

v2.x.x of readable-stream is a cut of the stream module from Node 8 (there have been no semver-major changes from Node 4 to 8). This version supports all Node.js versions from 0.8, as well as evergreen browsers and IE 10 & 11.

Usage

You can swap your require('stream') with require('readable-stream') without any changes, if you are just using one of the main classes and functions.

const {
  Readable,
  Writable,
  Transform,
  Duplex,
  pipeline,
  finished
} = require('readable-stream')

Note that require('stream') will return Stream, while require('readable-stream') will return Readable. We discourage using whatever is exported directly, but rather use one of the properties as shown in the example above.

Usage In Browsers

You will need a bundler like browserify, webpack, parcel or similar. Polyfills are no longer required since version 4.2.0.

Streams Working Group

readable-stream is maintained by the Streams Working Group, which oversees the development and maintenance of the Streams API within Node.js. The responsibilities of the Streams Working Group include:

  • Addressing stream issues on the Node.js issue tracker.
  • Authoring and editing stream documentation within the Node.js project.
  • Reviewing changes to stream subclasses within the Node.js project.
  • Redirecting changes to streams from the Node.js project to this project.
  • Assisting in the implementation of stream providers within Node.js.
  • Recommending versions of readable-stream to be included in Node.js.
  • Messaging about the future of streams to give the community advance notice of changes.

Team Members

NPM DownloadsLast 30 Days