CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mafintosh--streamx

An iteration of the Node.js core streams with a series of improvements

Pending
Overview
Eval results
Files

stream.mddocs/

Stream Base Class

Base class for all streamx streams providing core functionality including lifecycle management, error handling, and event integration.

Capabilities

Stream Constructor

Creates a new Stream instance with optional lifecycle hooks.

/**
 * Creates a new Stream instance
 * @param opts - Configuration options including lifecycle hooks
 */
class Stream extends EventEmitter {
  constructor(opts?: StreamOptions);
}

interface StreamOptions {
  /** Function called before first stream operation */
  open?: (cb: (error?: Error) => void) => void;
  /** Function called during stream destruction */
  destroy?: (cb: (error?: Error) => void) => void;
}

Usage Example:

const { Stream } = require("@mafintosh/streamx");

const stream = new Stream({
  open(cb) {
    console.log("Stream opening");
    // Perform initialization
    cb(null);
  },
  destroy(cb) {
    console.log("Stream destroying");
    // Perform cleanup
    cb(null);
  }
});

Lifecycle Methods

_open Method

Called once before the first stream operation. Override to implement initialization logic.

/**
 * Called before first stream operation for initialization
 * @param cb - Callback to call when open is complete
 */
_open(cb: (error?: Error) => void): void;

Usage Example:

class CustomStream extends Stream {
  _open(cb) {
    // Initialize resources
    this.resource = acquireResource();
    cb(null);
  }
}

_destroy Method

Called during stream destruction. Override to implement cleanup logic.

/**
 * Called during stream destruction for cleanup
 * @param cb - Callback to call when destroy is complete
 */
_destroy(cb: (error?: Error) => void): void;

Usage Example:

class CustomStream extends Stream {
  _destroy(cb) {
    // Clean up resources
    if (this.resource) {
      this.resource.close();
    }
    cb(null);
  }
}

Stream Control

destroy Method

Forcefully destroys the stream, calling _destroy after pending operations complete.

/**
 * Forcefully destroy the stream
 * @param error - Optional error indicating reason for destruction
 */
destroy(error?: Error): void;

Usage Example:

const stream = new Stream();

// Destroy with error
stream.destroy(new Error("Something went wrong"));

// Destroy without error
stream.destroy();

Stream State

destroyed Property

Boolean indicating whether the stream has been destroyed.

/**
 * Whether the stream has been destroyed
 */
get destroyed(): boolean;

destroying Property

Boolean indicating whether the stream is currently being destroyed.

/**
 * Whether the stream is currently being destroyed
 */
get destroying(): boolean;

Usage Example:

const stream = new Stream();

console.log(stream.destroyed);  // false
console.log(stream.destroying); // false

stream.destroy();

console.log(stream.destroying); // true
// Eventually destroyed becomes true

on Method

Override of EventEmitter's on method with special stream event handling.

/**
 * Add event listener with special stream-specific behavior
 * @param name - Event name
 * @param fn - Event listener function
 * @returns This stream instance
 */
on(name: string, fn: Function): this;

Special Event Behaviors:

  • data events automatically resume readable streams
  • readable events enable readable event emission
  • drain events enable drain event emission for writable streams

Event Handling

The Stream class extends EventEmitter and provides special handling for stream-specific events.

Common Events

  • close - Emitted when the stream is fully closed
  • error - Emitted when an error occurs

Usage Example:

const stream = new Stream();

stream.on('close', () => {
  console.log('Stream closed');
});

stream.on('error', (err) => {
  console.error('Stream error:', err);
});

stream.destroy();

Install with Tessl CLI

npx tessl i tessl/npm-mafintosh--streamx

docs

duplex.md

index.md

readable.md

stream.md

transform.md

writable.md

tile.json