I am going to do this, but I probably made a mistake. Never recording Streams1 (old streams) or Streams2, I'm probably not the kind of guy who could answer this question on his own, but here everything goes. There seems to be an API Streams1 which is preserved to some extent. Streams2 has two stream modes (inherited) and non-streaming. In short, a gasket that supports the current mode is leaving. This was a message that led to the patch now called Streams3 ,
Same API as streams2, but removes the confusing modality of the stream / old mode switch.
- Each time
read() is called and returns some data, a data event occurs. resume() will make it re-call read (). Otherwise no change.pause() will cause read() called again.pipe(dest) and on('data', fn) will automatically call resume() .- No switches in old mode. There only flowing and stopped. Streams begin with a pause.
Unfortunately, to understand any description that describes Streams3 very well, you need to understand Streams1 first, and legacy streams
Background
First, let's see what Node v0.10.25 says about the two modes,
Read streams have two “modes”: stream mode and non-stream mode. When in streaming mode, data is read from the base system and delivered to your program as quickly as possible. In non-flowing mode, you must explicitly call stream.read () to get pieces of data. - Node v0.10.25 Documents
Isaac Z. Schlüter said in November the slides I dug up :
streams2
- "suck streams"
- Instead of emitting data events, call the read () function to retrieve data from the source.
- Solves all problems (of which we know)
So it seems that in streams1 you should create an object and call .on('data', cb) for that object. This would set the event as a trigger, and then you were in the grip of the flow. In Streams2 streams, internal streams have buffers and you request data from these streams explicitly (using `.read). Isaac continues to point out how backward compatibility works in Streams2 to support Streams1 (old-stream) modules
stream of old mode streams1
- New threads can switch to the old mode, where they display "data"
- If you add a data event handler or call pause () or resume (), then toggle
- Making minimal changes to existing tests to keep us informed.
So, in Streams2, calling .pause() or .resume() starts laying out. And it must be right? In Streams2, you have control when to .read() , and you won’t catch the things that throw you. This caused an outdated mode that acted independently of Streams2.
Take an example from the Isaac slide,
createServer(function(q,s) {
- In threads 1,
q starts reading and emitting immediately (probably data loss) until q.pause q to stop data extraction, but not due to emitting events to clear what it has already read. - In Streams2,
q begins to pause until .pause() called, which means emulating the old mode. - In Streams3,
q starts as paused, never reads from the file descriptor, making q.pause() noop, and when q.on('data', cb) is q.resume , it calls q.resume until there is more data in the buffer, AND then call q.resume again, doing the same.