uhop/stream-chain

Unable to create chain with StreamProxy (duplexify)

rhodgkins opened this issue · 6 comments

Hi,

I'm trying to use this library in relation to a streamed Google gRPC response that uses StreamProxy (and duplexify), but there the below check when creating a chain that prevents this...

stream-chain/index.js

Lines 97 to 105 in 49b851f

if (
fn instanceof Duplex ||
fn instanceof Transform ||
(!index && fn instanceof Readable) ||
(index === fns.length - 1 && fn instanceof Writable)
) {
return fn;
}
throw Error('Arguments should be functions, arrays or streams.');

Any ideas on a way around this? (Currently wrapping a pointless PassThrough with the StreamProxy in a NodeJS.pipeline to get around it but this isn't ideal obviously!)

uhop commented

The check in question is to make sure that arguments are functions, arrays, or streams. As far as I can tell StreamProxy (and duplexify) are objects, which do not implement stream protocols and are not based on any standard Node stream.

duplexity uses readable-stream which is a package mirroring the standard Node streams implementing all the stream protocols based on the standard Node stream.

uhop commented

Mirroring is not "the same". Obviously, I cannot include all possible class names of all possible third-party packages in my code for practical reasons. Alternatively, you can ask the developers of the third-party streams to base their classes on Node streams.

If you have another reasonable solution, I would love to hear it.

I'd disagree its third party as its ported from NodeJS, anyway what about matching NodeJS checks for streams?

https://github.com/nodejs/node/blob/a82c1a1da710e83bebd4431a0220e45562a310a3/lib/internal/streams/utils.js#L14-L44

There's no need for the instanceof Transform if its a Duplex stream, so check could just become:

if ( 
   isDuplexNodeStream(fn) || 
   (!index && isReadableNodeStream(fn)) || 
   (index === fns.length - 1 && isWritableNodeStream(fn)) 
 ) { 
   return fn; 
 } 
 throw Error('Arguments should be functions, arrays or streams.'); 
uhop commented

Sounds like a plan. I'll add it to the next release.

uhop commented

Released as 2.2.5.