isStream
Test if an object is a Stream
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.
Usage:
var isStream = require('isstream')
var Stream = require('stream')
isStream(new Stream()) // true
isStream({}) // false
isStream(new Stream.Readable()) // true
isStream(new Stream.Writable()) // true
isStream(new Stream.Duplex()) // true
isStream(new Stream.Transform()) // true
isStream(new Stream.PassThrough()) // true
But wait! There's more!
You can also test for isReadable(obj), isWritable(obj) and isDuplex(obj) to test for implementations of Streams2 (and Streams3) base classes.
var isReadable = require('isstream').isReadable
var isWritable = require('isstream').isWritable
var isDuplex = require('isstream').isDuplex
var Stream = require('stream')
isReadable(new Stream()) // false
isWritable(new Stream()) // false
isDuplex(new Stream()) // false
isReadable(new Stream.Readable()) // true
isReadable(new Stream.Writable()) // false
isReadable(new Stream.Duplex()) // true
isReadable(new Stream.Transform()) // true
isReadable(new Stream.PassThrough()) // true
isWritable(new Stream.Readable()) // false
isWritable(new Stream.Writable()) // true
isWritable(new Stream.Duplex()) // true
isWritable(new Stream.Transform()) // true
isWritable(new Stream.PassThrough()) // true
isDuplex(new Stream.Readable()) // false
isDuplex(new Stream.Writable()) // false
isDuplex(new Stream.Duplex()) // true
isDuplex(new Stream.Transform()) // true
isDuplex(new Stream.PassThrough()) // true
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).
License
isStream is Copyright (c) 2015 Rod Vagg @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.
Source
| 1 | # isStream |
| 2 | |
| 3 | [](http://travis-ci.org/rvagg/isstream) |
| 4 | |
| 5 | **Test if an object is a `Stream`** |
| 6 | |
| 7 | [](https://nodei.co/npm/isstream/) |
| 8 | |
| 9 | 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)**. |
| 10 | |
| 11 | ## Usage: |
| 12 | |
| 13 | ```js |
| 14 | var isStream = require('isstream') |
| 15 | var Stream = require('stream') |
| 16 | |
| 17 | isStream(new Stream()) // true |
| 18 | |
| 19 | isStream({}) // false |
| 20 | |
| 21 | isStream(new Stream.Readable()) // true |
| 22 | isStream(new Stream.Writable()) // true |
| 23 | isStream(new Stream.Duplex()) // true |
| 24 | isStream(new Stream.Transform()) // true |
| 25 | isStream(new Stream.PassThrough()) // true |
| 26 | ``` |
| 27 | |
| 28 | ## But wait! There's more! |
| 29 | |
| 30 | You can also test for `isReadable(obj)`, `isWritable(obj)` and `isDuplex(obj)` to test for implementations of Streams2 (and Streams3) base classes. |
| 31 | |
| 32 | ```js |
| 33 | var isReadable = require('isstream').isReadable |
| 34 | var isWritable = require('isstream').isWritable |
| 35 | var isDuplex = require('isstream').isDuplex |
| 36 | var Stream = require('stream') |
| 37 | |
| 38 | isReadable(new Stream()) // false |
| 39 | isWritable(new Stream()) // false |
| 40 | isDuplex(new Stream()) // false |
| 41 | |
| 42 | isReadable(new Stream.Readable()) // true |
| 43 | isReadable(new Stream.Writable()) // false |
| 44 | isReadable(new Stream.Duplex()) // true |
| 45 | isReadable(new Stream.Transform()) // true |
| 46 | isReadable(new Stream.PassThrough()) // true |
| 47 | |
| 48 | isWritable(new Stream.Readable()) // false |
| 49 | isWritable(new Stream.Writable()) // true |
| 50 | isWritable(new Stream.Duplex()) // true |
| 51 | isWritable(new Stream.Transform()) // true |
| 52 | isWritable(new Stream.PassThrough()) // true |
| 53 | |
| 54 | isDuplex(new Stream.Readable()) // false |
| 55 | isDuplex(new Stream.Writable()) // false |
| 56 | isDuplex(new Stream.Duplex()) // true |
| 57 | isDuplex(new Stream.Transform()) // true |
| 58 | isDuplex(new Stream.PassThrough()) // true |
| 59 | ``` |
| 60 | |
| 61 | *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).* |
| 62 | |
| 63 | |
| 64 | ## License |
| 65 | |
| 66 | **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. |
