reference: https://nodesource.com/blog/understanding-streams-in-nodejs/
Advantage:
- In a http example, do not need to put all file size into memories anymore
- prevent the low response speed or even crash caused by memory's over-limited
- process one chunk data each time
Principle:
Event Emitter of Node.js
Category:
- readable:可讀
- writable:可寫
- duplex:雙工。可讀也可寫,互不干擾。
- transform:轉(zhuǎn)換。從可寫轉(zhuǎn)成可讀,鏈?zhǔn)秸{(diào)用,做數(shù)據(jù)處理
Implement:
- native api:
- stream
- fs.createReadStream
- http
- socket
- process.stdin(out)
- zlib.createGzip: one of the most common transform stream
- inherit
to diy your own specific stream:
- inherit class (like Readable)
- redefine _read function
API:
- pipe/unpipe
- pipeline: multiple process in one line
- resume/pause
- push
- event:
readable, data,
error, finish, close, drain
Concept :
- mode: flowing & pause and several related events
- back pressure:背壓,eg: 寫不進(jìn)去,wait the drain(下水道) event to continue
Streams Cheat Sheet:

fundamental

practice

flowing

readable & transform

writable & compound