Skip to content

Chaining pipe for streams - probably stupid question but... #2384

@Ayms

Description

@Ayms

The test code is simply:

const stream=require('stream');
let stream1=new stream.Duplex();
let stream2=new stream.Duplex();
let stream3=new stream.Duplex();
stream1._read=function() {let data=this.read();if (data) {console.log('stream1 read '+data.toString())}};
stream2._read=function() {let data=this.read();if (data) {console.log('stream2 read '+data.toString())}};
stream3._read=function() {let data=this.read();if (data) {console.log('stream3 read '+data.toString())}};
stream1._write=function(data) {console.log('stream1 receive write '+data.toString())};
stream2._write=function(data) {console.log('stream2 receive write '+data.toString())};
stream3._write=function(data) {console.log('stream3 receive write '+data.toString())};
stream3.pipe(stream2).pipe(stream1);
stream3.push(new Buffer('hello','utf8'));

The result is:

stream2 receive write hello
stream3 read hello

Then stream1 receives nothing, there are of course different ways to solve this but probably I am missing something obvious because if we have to implement something like this.push(data) in stream2._write then I don't see the use of chaining pipes (we can do stream3.write(data))

The doc states that we can pipe from a readable and chain writable streams (the result of the pipe here is a duplex stream, the doc does not say how the pipe handles the write from the writable to the read of the readable to send the data forward to the piped stream)

The example is always the same using gzip, after some research I did not find any other examples except duplex pairs which looks strange since we can wonder then why we are using Duplex streams

That's why I am asking

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions