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.
npm install end-of-streamconst eos = require('end-of-stream');For ESM:
import eos from 'end-of-stream';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');
});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:
readable (boolean): Whether to monitor the readable side. Defaults to true for readable and duplex streamswritable (boolean): Whether to monitor the writable side. Defaults to true for writable and duplex streamserror (boolean): Whether to treat error events as end-of-stream. Defaults to true (set to false to ignore errors)function(error)this is bound to the stream instancenull on successful completion, or an Error object on failureReturn Value:
Returns a cleanup function that removes all event listeners and cancels monitoring when called.
Error Types:
error: false is specified)Stream Support:
_writableState/_readableStateUsage 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