bare-stream

2.13.0 · active · verified Sun Apr 19

bare-stream provides a lightweight and performant abstraction for handling streaming data in JavaScript environments, building upon the well-established `streamx` API. It is currently at version 2.13.0, with regular updates indicated by recent minor version bumps, suggesting an active development cadence. The "bare" prefix implies a focus on minimal overhead and core functionality, making it suitable for environments where resource efficiency is critical, such as low-resource servers or specialized applications. Unlike Node.js's built-in streams, bare-stream aims for broader compatibility and a more streamlined API, leveraging the `streamx` pattern. Its primary differentiator is its minimal dependency footprint and its explicit design for efficient data flow, providing a robust foundation for implementing custom streaming logic without the overhead of larger alternatives.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates creating a basic streaming pipeline using `bare-stream`. It shows a `Readable` stream generating data, a `Transform` stream processing it, and a `Writable` stream consuming and logging the final output, including basic error handling.

import { Readable, Writable, Transform } from 'bare-stream';

class MyGenerator extends Readable {
  constructor(limit) {
    super();
    this.index = 0;
    this.limit = limit;
  }

  _read(size) {
    if (this.index < this.limit) {
      const chunk = `Data chunk ${this.index++}\n`;
      this.push(Buffer.from(chunk));
    } else {
      this.push(null); // Signal end of stream
    }
  }
}

class MyProcessor extends Transform {
  _transform(chunk, encoding, callback) {
    // Convert to uppercase and add a prefix
    this.push(Buffer.from(`PROCESSED: ${chunk.toString().toUpperCase()}`));
    callback();
  }
}

class MyConsumer extends Writable {
  _write(chunk, encoding, callback) {
    console.log(chunk.toString().trim());
    callback();
  }
}

const generator = new MyGenerator(3);
const processor = new MyProcessor();
const consumer = new MyConsumer();

generator.pipe(processor).pipe(consumer);

// Error handling is crucial for streams
generator.on('error', (err) => console.error('Generator error:', err));
processor.on('error', (err) => console.error('Processor error:', err));
consumer.on('error', (err) => console.error('Consumer error:', err));
consumer.on('finish', () => console.log('All data processed.'));

view raw JSON →