preface

A lot of the NODEJS apis are related to streams, and I’ve seen some of the company’s great Node code. Implementing one interface is simply a matter of pipe another Java interface. Simple one-line code is confusing. As a small white of their own face meng but did not dare to ask, because they do not know where to ask. Now finally through learning, can also say a convection 123, hope to communicate with everyone.

Flow profile

Streams are divided into buffer mode and object mode. The buffer mode can only handle buffers or strings, and the object mode can handle JS objects. There are four types of streams: readable, writable, duplex, and converted streams. The latter two are really applications of readable and writable streams. So I want to talk about readable streams and writable streams first.

A readable stream

Readable streams come in two modes and can be converted at any time, and we can manipulate the readable stream by listening for events.

Two modes (as described on Node’s Chinese website) : flow mode: A readable stream automatically reads data and feeds it to applications as quickly as possible through events on the EventEmitter interface. Pause mode: the stream.read() method must be explicitly called to read a piece of data from the stream.

The apis used to switch from pause mode to flow mode are: 1) listen for “data” events; 2) call stream.resume(); 3) call stream.pipe() to send data to a writable stream

If there is no pipe target, call stream.unpipe() and cancel listening for the ‘data’ event. If there is no pipe target, call stream.unpipe()

Readable stream events: ‘data’,’readable’,’error’,’close’,’end’

Streams can be written

A writable stream is relatively simple, and we can manipulate it by listening for its events.

Writable stream events: the ‘close’, ‘drain’ tapping, ‘error’ and ‘finish’, ‘pipe’, ‘unpipe’

Take a chestnut

Let me describe the most common scenario for a flow with a simple example to understand the process. Here’s an example: I want to read a file and write its contents to another file (using the “stream” API, of course, rather than the “FS” module API).
















So what does this map correspond to in the code:

let fs = require('fs'); // Create a readable and writable streamlet rs = fs.createReadStream('./1.txt');
let ws = fs.createWriteStream('./2.txt'); // Listen for 'data' events and enable flow mode rs.on('data'.function(data) {// corresponds to the writable stream in the diagramtrueandfalse
    let flag = ws.write(data);
    if(! Flag){// return if the stream is writablefalse, we should stop reading to avoid consuming too much memory rs.pause(); }}); // Drain event ws.on('drain'.function() {// Restart the flow mode rs.resume(); }); // Use pause mode for readable streamsfunction read() {
    let data = rs.read()
    let flag = ws.write(data);
    if(flag){
       read()
    }
}
rs.on('readable'.function() {read()
})

ws.on('drain'.function () {
    read()});Copy the code

At the end

There are many more uses and apis for both readable and writable streams, but this is just a brief overview of the basic process, and if there are any inaccuracies in the description, please comment.

The resources

  • https://nodejs.org/dist/latest-v8.x/docs/api/stream.html
  • http://nodejs.cn/api/