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

相关文章

  • [读] NodeJS stream 一:Buffer

    NodeJS stream 一:Buffer

  • nodejs stream

    参考 nodejs stream 手册英文原版stream-handbook Streams come to us...

  • Stream in Nodejs

    reference: https://nodesource.com/blog/understanding-stre...

  • Nodejs Stream

    前言 前端工程领域中使用Nodejs处处掣肘,原因无非是Nodejs中困难部分:文件和网络。而文件和网络都依赖一个...

  • nodejs stream

    为什么需要stream 不用stream的情况:太耗内存 优化:用pipe连接读文件流和http可写流:image...

  • improve scheme

    Nodejs Stream: https://nodesource.com/blog/understanding-...

  • 2.nodejs通过stream方式加载页面 / 通过fs模块加

    nodejs通过stream方式加载页面(stream方式加载优化性能) 配置index.js文件,创建服务,设置...

  • nodejs--stream

    Stream是一个抽象接口,非常重要,应用广泛,通过流接口可以实现一些磁盘文件读写,套字节,http请求交互等。 ...

  • Nodejs Stream 手册

    编译地址:https://github.com/substack/stream-handbook译者:jabez1...

  • Nodejs Stream 初识

    linux文件 为了区别不同文件类型,会有一个type来进行区别普通文件:包含任意数据目录: 相关一组文件的索引套...

网友评论

    本文标题:Stream in Nodejs

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