An iteration of the Node.js core streams with a series of improvements
—
Base class for all streamx streams providing core functionality including lifecycle management, error handling, and event integration.
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);
}
});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);
}
}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);
}
}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();Boolean indicating whether the stream has been destroyed.
/**
* Whether the stream has been destroyed
*/
get destroyed(): boolean;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 trueOverride 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 streamsreadable events enable readable event emissiondrain events enable drain event emission for writable streamsThe Stream class extends EventEmitter and provides special handling for stream-specific events.
close - Emitted when the stream is fully closederror - Emitted when an error occursUsage 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