Stream in Nodejs

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:

  1. native api:
  • stream
  • fs.createReadStream
  • http
  • socket
  • process.stdin(out)
  • zlib.createGzip: one of the most common transform stream
  1. 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • https://nodejs.org/api/documentation.html 工具模塊 Assert 測(cè)試 ...
    KeKeMars閱讀 6,603評(píng)論 0 6
  • 什么是流? 流是一種用來處理文件的字節(jié)傳輸手段 它不關(guān)心文件的整體內(nèi)容,只關(guān)注是否從文件中讀取到了數(shù)據(jù),以及讀到數(shù)...
    seventhMa閱讀 1,027評(píng)論 0 0
  • 簡(jiǎn)單介紹流 一般處理數(shù)據(jù)有兩種模式, buffer模式和stream模式, buffer模式就是取完數(shù)據(jù)一次性操作...
    褲褲他爸閱讀 444評(píng)論 0 0
  • 姓名:劉強(qiáng) 公司:寧波大發(fā)化纖有限公司 六項(xiàng)精進(jìn)第277期利他四組學(xué)員 【日精進(jìn)打卡第81天】,共計(jì)81天。 【知...
    三分廠劉強(qiáng)閱讀 129評(píng)論 0 0
  • 稚嫩和滄桑用我不理解的方式集中在他的面孔,不愿去猜測(cè)他的年齡,或者說不敢,這位目不轉(zhuǎn)睛盯著小孩子手中即將喝空飲料瓶...
    貪杯的靈魂閱讀 593評(píng)論 0 0

友情鏈接更多精彩內(nèi)容