How do you understand stream.pipe?

  node.js, question

Are the following two meanings the same?

stream.pipe(...)
 stream.pipe(...)

with

stream.pipe(...).pipe(...)

Is stream.pipe (…) the same thing as stream itself?

But when Object.is(stream,stream.pipe (…) returnsfalse

Is that what they mean?

图片描述

图片描述

readable.pipe(destination[, options])

Only a readable stream can have a pipe method to call. The pipe method receives a writable stream object as the target of pipe
It is to establish a channel between a readable stream and a writable stream, write the data in Readable into destination, and return the stream object specified by destination after pipe is executed.

The return of stream.pipe (…) is not the same object as stream.

stream.pipe(destinationA)
 stream.pipe(destinationB)

The same stream is written to the specified writable stream destinationA/destinationB through pipe.

stream.pipe(destinationA).pipe(destinationB)

Stream stream writes to destinationA ifstream.pipe(destinationA)The returned stream is also readable, then the pipe method can be called in chain to write the stream to destinationB
A prerequisite for pipe method to be able to be called in chain is that the stream returned by pipe is readable.

For example

var r = fs.createReadStream('file.txt');
 var z = zlib.createGzip();
 var w = fs.createWriteStream('file.txt.gz');
 r.pipe(z).pipe(w);

The zlib data stream is both a writable stream and a readable stream.