Stream in Nodejs

作者: 一只重拾梦想的小水 | 来源:发表于2020-03-17 19:01 被阅读0次

    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:转换。从可写转成可读,链式调用,做数据处理

    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: 写不进去,wait the drain(下水道) event to continue

    Streams Cheat Sheet:

    fundamental
    practice
    flowing
    readable & transform
    writable & compound

    相关文章

      网友评论

        本文标题:Stream in Nodejs

        本文链接:https://www.haomeiwen.com/subject/csgzcctx.html