| 1 |
# end-of-stream |
| 2 |
|
| 3 |
A node module that calls a callback when a readable/writable/duplex stream has completed or failed. |
| 4 |
|
| 5 |
npm install end-of-stream |
| 6 |
|
| 7 |
[](https://travis-ci.org/mafintosh/end-of-stream) |
| 8 |
|
| 9 |
## Usage |
| 10 |
|
| 11 |
Simply pass a stream and a callback to the `eos`. |
| 12 |
Both legacy streams, streams2 and stream3 are supported. |
| 13 |
|
| 14 |
``` js |
| 15 |
var eos = require('end-of-stream'); |
| 16 |
|
| 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); |
| 21 |
}); |
| 22 |
|
| 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); |
| 26 |
}); |
| 27 |
|
| 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); |
| 31 |
}); |
| 32 |
|
| 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'); |
| 36 |
}); |
| 37 |
|
| 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'); |
| 41 |
}); |
| 42 |
|
| 43 |
eos(readableStream, {error:false}, function(err) { |
| 44 |
// do not treat emit('error', err) as a end-of-stream |
| 45 |
}); |
| 46 |
``` |
| 47 |
|
| 48 |
## License |
| 49 |
|
| 50 |
MIT |
| 51 |
|
| 52 |
## Related |
| 53 |
|
| 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. |