How i can check if a var is a readable stream in Nodejs?
Example:
function foo(streamobj){
if(streamobj != readablestream){
// Error: no writable stream
}else{
// So something with streamobj
}
}
I tried
if (!(streamobj instanceof stream.Readable)){
But than i get a ReferenceError: stream is not defined
Your problem is definitely that you haven't required stream
. But. instanceof
is not a good method to check if variable is a readable stream. Consider following cases:
stream.Stream
);data
and end
events;Readable
from external module (https://github.com/isaacs/readable-stream);The best way to go is duck typing. Basically, if you are going to pipe
stream, check if it has pipe
method, if you are going to listen to events, check if stream has on
method, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With