2

I would like to know how to check if a value/argument/resource in node.js is a writable stream.

Is there a function or method to do that?

Thanks

1 Answers1

2

You can just check

// include node stream module
const stream = require('stream');

// check if stream is an instance of a Writable Stream
let isWritableStream = yourStream instanceof stream.Writable

Note: in your program, your customized "Writable Stream Classes" should extends the stream.Writable class. In order to extends it you may check How to implement a writable stream (Stack Overflow)

References:

  1. stream.Writable (Node.js v.0.9.4+)
  2. instanceof (MDN)
DDomen
  • 1,808
  • 1
  • 7
  • 17
  • btw.. I didn't even know before today that streams were LITERAL classes of things – The Bomb Squad Feb 09 '21 at 19:13
  • You have a misconception about what a "class" in JS essentially is. JS is not an **Obect Oriented** but a **Prototype Oriented** programming language. Any class is infact a "special" `Function` object with a certain prototype. Checking an `instanceof` is just checking if the prototypes coincide. Node.js just exposes the V8 (engine) underlying `Function` object which is the prototype of all the internal stream implementations. It is not *LITERAL* a class, but a function, indeed you can call it without the `new` keyword: `stream.Writable()`, no errors. – DDomen Feb 09 '21 at 19:35
  • 1
    I never said I didn't know(about prototypes).. it's just that it didn't really connect uk.. it's just.. a moment to take in – The Bomb Squad Feb 09 '21 at 19:38