CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-end-of-stream

Call a callback when a readable/writable/duplex stream has completed or failed.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

End of Stream

End of Stream is a Node.js utility that provides a reliable way to detect when readable, writable, or duplex streams have completed or failed. It works across legacy streams, streams2, and streams3 implementations, providing a unified API for stream completion detection.

Package Information

  • Package Name: end-of-stream
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install end-of-stream

Core Imports

const eos = require('end-of-stream');

For ESM:

import eos from 'end-of-stream';

Basic Usage

const eos = require('end-of-stream');
const fs = require('fs');

// Monitor a readable stream
const readStream = fs.createReadStream('input.txt');
eos(readStream, function(err) {
  if (err) return console.log('stream had an error or closed early');
  console.log('stream has ended', this === readStream); // true
});

// Monitor a writable stream
const writeStream = fs.createWriteStream('output.txt');
eos(writeStream, function(err) {
  if (err) return console.log('stream had an error or closed early');
  console.log('stream has finished', this === writeStream); // true
});

// Monitor both sides of a duplex stream with options
const duplexStream = new require('stream').Transform();
eos(duplexStream, {readable: false}, function(err) {
  if (err) return console.log('stream had an error or closed early');
  console.log('stream has finished but might still be readable');
});

Capabilities

Stream Completion Detection

Detects when readable, writable, or duplex streams have completed or failed.

/**
 * Monitor a stream for completion or failure
 * @param {Stream} stream - The stream to monitor
 * @param {Object} [options] - Configuration options
 * @param {boolean} [options.readable] - Monitor readable side (default: true for readable/duplex streams)
 * @param {boolean} [options.writable] - Monitor writable side (default: true for writable/duplex streams)
 * @param {boolean} [options.error] - Treat error events as end-of-stream (default: true)
 * @param {Function} callback - Called when stream completes or fails
 * @returns {Function} Cleanup function to remove listeners and cancel monitoring
 */
function eos(stream, options, callback);
function eos(stream, callback);

Parameters:

  • stream (Stream, required): The stream to monitor. Supports Node.js readable, writable, duplex streams, HTTP requests, and child processes
  • options (Object, optional): Configuration options:
    • readable (boolean): Whether to monitor the readable side. Defaults to true for readable and duplex streams
    • writable (boolean): Whether to monitor the writable side. Defaults to true for writable and duplex streams
    • error (boolean): Whether to treat error events as end-of-stream. Defaults to true (set to false to ignore errors)
  • callback (Function, required): Called when stream completes or fails
    • Signature: function(error)
    • Context: this is bound to the stream instance
    • Called with null on successful completion, or an Error object on failure

Return Value:

Returns a cleanup function that removes all event listeners and cancels monitoring when called.

Error Types:

  • Stream errors are passed directly to the callback (unless error: false is specified)
  • Premature close events generate Error objects with message "premature close"
  • Child process non-zero exit codes generate Error objects with message "exited with error code: X"

Stream Support:

  • Legacy streams: Streams without proper _writableState/_readableState
  • Streams2/Streams3: Modern Node.js streams with internal state tracking
  • HTTP requests: Special handling for request/response objects
  • Child processes: Monitors exit codes and stdio streams
  • Duplex streams: Can monitor readable and/or writable sides independently

Usage Examples:

const eos = require('end-of-stream');
const fs = require('fs');
const http = require('http');
const { spawn } = require('child_process');

// Basic readable stream monitoring
eos(fs.createReadStream('file.txt'), function(err) {
  if (err) console.log('Stream error:', err.message);
  else console.log('Stream ended successfully');
});

// Writable stream with options
eos(fs.createWriteStream('output.txt'), {error: false}, function(err) {
  console.log('Stream finished (ignoring errors)');
});

// HTTP request monitoring
const req = http.get('http://example.com', function(res) {
  eos(res, function(err) {
    if (err) console.log('Response stream error');
    else console.log('Response completed');
  });
});

// Child process monitoring
const child = spawn('echo', ['hello world']);
eos(child, function(err) {
  if (err) console.log('Process failed:', err.message);
  else console.log('Process completed successfully');
});

// Cancellable monitoring
const cleanup = eos(someStream, function(err) {
  console.log('This will not be called');
});
cleanup(); // Cancel monitoring

Install with Tessl CLI

npx tessl i tessl/npm-end-of-stream
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/end-of-stream@1.4.x
Publish Source
CLI
Badge
tessl/npm-end-of-stream badge