首页 > 代码库 > Stream

Stream

流这一章非常的重要,官方网店将它分成2个部分讲解:

    第一部分是介绍API,教你怎么用 

    第二部分是教你怎么通过API实现你自己的流

Types of Streams#

 Node.js 给出了最基本的4中流类型:

  • Readable - streams from which data can be read (for example fs.createReadStream()).
  • Writable - streams to which data can be written (for example fs.createWriteStream()).
  • Duplex - streams that are both Readable and Writable (for example net.Socket).
  • Transform - Duplex streams that can modify or transform the data as it is written and read (for example zlib.createDeflate()).

 

Buffering

 Writable and Readable streams  都会在内部存储data,并且通过writable._writableState.getBuffer() or readable._readableState.buffer 来使用它。

当然,data的buffer量 是依靠 highWaterMark 来决定的(highWaterMark 会传入流的构造函数)。对于普通的流,highWaterMark 就是指定的字节总数,对于object mode下的 highWaterMark,highWaterMark 指定了对象的总数。

 

从Readable streams 里来的缓

Stream