You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7 KiB

4 years ago
  1. # end-of-stream
  2. A node module that calls a callback when a readable/writable/duplex stream has completed or failed.
  3. npm install end-of-stream
  4. [![Build status](https://travis-ci.org/mafintosh/end-of-stream.svg?branch=master)](https://travis-ci.org/mafintosh/end-of-stream)
  5. ## Usage
  6. Simply pass a stream and a callback to the `eos`.
  7. Both legacy streams, streams2 and stream3 are supported.
  8. ``` js
  9. var eos = require('end-of-stream');
  10. eos(readableStream, function(err) {
  11. // this will be set to the stream instance
  12. if (err) return console.log('stream had an error or closed early');
  13. console.log('stream has ended', this === readableStream);
  14. });
  15. eos(writableStream, function(err) {
  16. if (err) return console.log('stream had an error or closed early');
  17. console.log('stream has finished', this === writableStream);
  18. });
  19. eos(duplexStream, function(err) {
  20. if (err) return console.log('stream had an error or closed early');
  21. console.log('stream has ended and finished', this === duplexStream);
  22. });
  23. eos(duplexStream, {readable:false}, function(err) {
  24. if (err) return console.log('stream had an error or closed early');
  25. console.log('stream has finished but might still be readable');
  26. });
  27. eos(duplexStream, {writable:false}, function(err) {
  28. if (err) return console.log('stream had an error or closed early');
  29. console.log('stream has ended but might still be writable');
  30. });
  31. eos(readableStream, {error:false}, function(err) {
  32. // do not treat emit('error', err) as a end-of-stream
  33. });
  34. ```
  35. ## License
  36. MIT
  37. ## Related
  38. `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.