I want to pipe two streams - both Transform streams.
Stream 1 (line2Arr) - turns a line into an array inside an object and sends it back down the stream
Stream 2 (arr2Json) - takes the object with the array, creates a different object with the info and send it back down the stream.
All of this i want to wrap up in one stream (line2JsonStream)
so that I can then do for example:
pipeline(readStream, line2JsonStream, writeStream)
What does line2JsonStream class need to be in order to be able to do that? Does it need to extend Transform? and if so what would it return exactly?
When I use them seperately
pipeline(readStream, line2Arr, arr2Json, writeStream)
they work, but I want to be able to use it as a single stream, and anything i've done so far didn't work.
When implementing line2Arr and arr2Json as instances of a Transfom like this
const line2Arr = new Transform({
transform( chunk, enc, cb ) {
const transformed = doSomeTransformMagic( chunk );
cb( null, transformed );
}
});
you could do
readStream.pipe( line2Arr ).pipe( arr2Json ).pipe( writeStream )
If I understand your question correctly, you want to squash the two Transform instances into a single Stream so that you might do
readStream.pipe( line2ArrAndArr2Json ).pipe( writeStream )
which I think is not easily possible by using the two created Transform instances line2Arr and arr2Json. The pipe() function requires a Writable as an argument and returns the same object. In our case, that is a Transform wich also implements Readable and its pipe() function. To make the above code work, you'd need to pass the Writable interface of line2Arr to readStream.pipe() but return the Readable interface of arr2Json from readStream.pipe(), which I don't think is possible.