3 A node module that calls a callback when a readable/writable/duplex stream has completed or failed.
5 npm install end-of-stream
7 [![Build status](https://travis-ci.org/mafintosh/end-of-stream.svg?branch=master)](https://travis-ci.org/mafintosh/end-of-stream)
11 Simply pass a stream and a callback to the `eos`.
12 Both legacy streams, streams2 and stream3 are supported.
15 var eos = require('end-of-stream');
17 eos(readableStream, function(err) {
18 // this will be set to the stream instance
19 if (err) return console.log('stream had an error or closed early');
20 console.log('stream has ended', this === readableStream);
23 eos(writableStream, function(err) {
24 if (err) return console.log('stream had an error or closed early');
25 console.log('stream has finished', this === writableStream);
28 eos(duplexStream, function(err) {
29 if (err) return console.log('stream had an error or closed early');
30 console.log('stream has ended and finished', this === duplexStream);
33 eos(duplexStream, {readable:false}, function(err) {
34 if (err) return console.log('stream had an error or closed early');
35 console.log('stream has finished but might still be readable');
38 eos(duplexStream, {writable:false}, function(err) {
39 if (err) return console.log('stream had an error or closed early');
40 console.log('stream has ended but might still be writable');
43 eos(readableStream, {error:false}, function(err) {
44 // do not treat emit('error', err) as a end-of-stream
54 `end-of-stream` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.