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.

66 lines
2.4 KiB

  1. # isStream
  2. [![Build Status](https://secure.travis-ci.org/rvagg/isstream.png)](http://travis-ci.org/rvagg/isstream)
  3. **Test if an object is a `Stream`**
  4. [![NPM](https://nodei.co/npm/isstream.svg)](https://nodei.co/npm/isstream/)
  5. The missing `Stream.isStream(obj)`: determine if an object is standard Node.js `Stream`. Works for Node-core `Stream` objects (for 0.8, 0.10, 0.11, and in theory, older and newer versions) and all versions of **[readable-stream](https://github.com/isaacs/readable-stream)**.
  6. ## Usage:
  7. ```js
  8. var isStream = require('isstream')
  9. var Stream = require('stream')
  10. isStream(new Stream()) // true
  11. isStream({}) // false
  12. isStream(new Stream.Readable()) // true
  13. isStream(new Stream.Writable()) // true
  14. isStream(new Stream.Duplex()) // true
  15. isStream(new Stream.Transform()) // true
  16. isStream(new Stream.PassThrough()) // true
  17. ```
  18. ## But wait! There's more!
  19. You can also test for `isReadable(obj)`, `isWritable(obj)` and `isDuplex(obj)` to test for implementations of Streams2 (and Streams3) base classes.
  20. ```js
  21. var isReadable = require('isstream').isReadable
  22. var isWritable = require('isstream').isWritable
  23. var isDuplex = require('isstream').isDuplex
  24. var Stream = require('stream')
  25. isReadable(new Stream()) // false
  26. isWritable(new Stream()) // false
  27. isDuplex(new Stream()) // false
  28. isReadable(new Stream.Readable()) // true
  29. isReadable(new Stream.Writable()) // false
  30. isReadable(new Stream.Duplex()) // true
  31. isReadable(new Stream.Transform()) // true
  32. isReadable(new Stream.PassThrough()) // true
  33. isWritable(new Stream.Readable()) // false
  34. isWritable(new Stream.Writable()) // true
  35. isWritable(new Stream.Duplex()) // true
  36. isWritable(new Stream.Transform()) // true
  37. isWritable(new Stream.PassThrough()) // true
  38. isDuplex(new Stream.Readable()) // false
  39. isDuplex(new Stream.Writable()) // false
  40. isDuplex(new Stream.Duplex()) // true
  41. isDuplex(new Stream.Transform()) // true
  42. isDuplex(new Stream.PassThrough()) // true
  43. ```
  44. *Reminder: when implementing your own streams, please [use **readable-stream** rather than core streams](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html).*
  45. ## License
  46. **isStream** is Copyright (c) 2015 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.