Why can I push into a Readable stream?

时间:2016-07-11 21:54:42

标签: node.js stream

I was using a piece of code similar to this until I discovered the 'setEncoding' method. While refactoring the code, this question came to me.

readable.push(chunk, 'base64')
readable.push(null)
readable.pipe(res)

Isn't pushing a form of writing data? Shouldn't I be unable to write any data into a Readable stream? I know that every data needs to be written (otherwise it would be null) and read (or it becomes useless) at some point, but I thought Node would abstract that from us.

Could someone clear this up for me? Thanks

1 个答案:

答案 0 :(得分:2)

This comes into play when you're creating a new Readable Stream from scratch. A Readable Stream needs to begin somewhere and there needs to be a way to get data into that new Stream. When doing a push() like in your question, you're buffering data until it is read at which point it is then flushed from the stream and then piped/read into the destination for consumption. The consumer will continue reading the data until 'null' is hit which indicates the stream is done outputting data.

var stream = require('stream');
var readable = new stream.Readable(); // new empty stream.Readable
readable.push('some data');
readable.push(null); // Done writing data

To get the fullest picture on how streams work under the hood and the various types of streams, you should read the Stream Handbook by Substack. That should answer any lingering questions you might have.