23

I have a function which receives an object that could be a string, Buffer or Stream.

I can easily test if the object is a Buffer like so: if (x instanceof Buffer)

What's the best way to test if an object is a Stream? There doesn't appear to be a Stream base class in node - is there?

What should I look for?

1
  • Another way to test if an object is a Buffer is Buffer.isBuffer(x) (see this question)
    – Wyck
    Aug 19, 2022 at 21:35

3 Answers 3

13

For Readable you can do:

var stream = require('stream');

function isReadableStream(obj) {
  return obj instanceof stream.Stream &&
    typeof (obj._read === 'function') &&
    typeof (obj._readableState === 'object');
}

console.log(isReadableStream(fs.createReadStream('car.jpg'))); // true
console.log(isReadableStream({}))); // false
console.log(isReadableStream(''))); // false
1
10

Not all streams are implemented using stream.Readable and stream.Writable.

process.stdout instanceof require("stream").Writable; // false
process.stdout instanceof require("readable-stream").Writable; // false

The better method is to individually check for the read(), write(), end() functions.

var EventEmitter = require("events");

function isReadableStream(test) {
    return test instanceof EventEmitter && typeof test.read === 'function'
}

function isWritableStream(test) {
    return test instanceof EventEmitter && typeof test.write === 'function' && typeof test.end === 'function'
}

You can always reffer to: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/node.d.ts#L252

6
  • is it safe to assume that this code ( process.stdout instanceof require("stream").Writable; ) is the best way to assume that a stream is a process.stdout? Jan 7, 2017 at 12:54
  • @hisabimbola No simply use instanceof require("events").EventEmitter and check for the write and end properties. Anything else will fail at one point or another.
    – Walter
    Jan 7, 2017 at 15:10
  • What I am trying to do is test that a writable stream is exactly process.stdout not test if it's a writable stream. More info about this here Jan 7, 2017 at 15:28
  • @hisabimbola You'll have to manually check if the stream is a terminal (instanceof require("tty").WriteStream === true). Then check its handle to make sure its either stdin or stdout (test._handle.fd < 0).
    – Walter
    Jan 7, 2017 at 16:05
  • According to the documentation here, this (node -p -e "Boolean(process.stdout.isTTY)" | cat) outputs false, because it's not TTY though it's process.stdout Jan 7, 2017 at 20:05
8

The prototype you are looking for is the stream.Readable stream for readable streams, and stream.Writable for writable streams. They work in the same way as when you check for Buffer.

2
  • should this be lower case stream.readable? it does not seem to have the property with upper case Apr 29, 2015 at 3:20
  • Thanks Sebastien, please add some code next time. I am a noob and can't parse how to compare this answer to the next one.
    – luv2learn
    Aug 30, 2020 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.