Mikuso Mikuso -5 years ago 112
Node.js Question

How to test if an object is a Stream in NodeJS

I have a function which receives an object that could be a


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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download