An overview of the
There are four common types of custom streams: Readable, Writable, Duplex, and Transform. Common custom streams are used for HTTP requests, responses, Crypto encryption, and process STDIN communication.
Stream Module Introduction
In NodeJS, a custom stream is implemented by importing the stream module directly, without downloading it. All types of streams are implemented by inheriting the corresponding classes provided by the module.
Implement a custom Readable stream, Readable
1, create a custom readable stream class MyRead
Implementing a custom Readable stream requires creating a class called MyRead, inheriting the Readable class from stream, and overriding the _read method, which is a set point for all custom streams.
const { Readable } = require("stream"); // Create a custom Readable stream class MyRead extends Readable {constructor() { super(); this.index = 0; } // Override the _read method of a custom readable stream_read() {
this.index++;
this.push(this.index + "");
if(this.index === 3) { this.push(null); }}}Copy the code
The _read method we wrote will first look up and execute, and then use the push method to read out the data until the value of push is null. Otherwise, the _read will be considered incomplete and will continue to be called.
2. Validate custom readable streams
let myRead = new MyRead();
myRead.on("data", data => {
console.log(data);
});
myRead.on("end".function() {
console.log("Read completed"); }); // <Buffer 31> // <Buffer 32> // <Buffer 33> // Read completeCopy the code
Implement a custom Writable stream Writable
1, create a custom writable stream class MyWrite
Create a class named MyWrite that inherits the Writable class from stream and overwrites the _write method.
const { Writable } = require("stream"); Class MyWrite extends Writable {MyWrite extends Writable {MyWrite extends Writable {MyWrite (chunk, encoding, callback)) {callback(); // Write cache to file}}Copy the code
By default, the first write is directly written to the file and subsequent writes are written to the cache. If you do not call callback, only the first write is written to the file by default. Calling callback clears the cache and writes to the file.
2. Validate custom writable streams
let myWrite = new MyWrite();
myWrite.write("hello"."utf8", () => {
console.log("hello ok");
});
myWrite.write("world"."utf8", () => {
console.log("world ok");
});
// hello ok
// world okCopy the code
Implement a custom Duplex stream Duplex
Create class MyDuplex for custom duplex streams
Create a class named MyDuplex and inherit the Duplex class from stream. Since Duplex streams are both readable and writable, override the _read and _write methods.
const { Duplex } = require("stream"); Class MyDuplex extends Duplex {// Override the _read method for custom Duplex streams_read() {
this.push("123"); this.push(null); } // Override the _write method for a custom duplex stream _write(chunk, encoding, callback) {callback(); }}Copy the code
A duplex stream has Readable and Writable capabilities respectively, but reads and writes are independent of each other.
2. Verify the custom duplex flow
let myDuplex = new MyDuplex();
myDuplex.on("readable", () => {
console.log(myDuplex.read(1), "--");
});
setTimeout(() => {
myDuplex.on("data", data => {
console.log(data, "xxxx");
});
}, 3000);
// <Buffer 31> ----
// <Buffer 32> xxxx
// <Buffer 32> ----
// <Buffer 33> xxxxCopy the code
If both readable and Data use the default data event first, select one and do not use both. Readable streams typically consume data and then lose it (the cache is empty). If you must use both, you can add a timer.
Implement a custom Transform stream Transform
Create MyTransform class for custom convertable flows
Create a class named MyTransform, inherit the Transform class from stream, and override the _transform method, which takes the same arguments as _write.
const { Transform } = require('stream'); Class MyTransform extends Transform {// Override the _transform method of a custom Transform stream _transform(chunk, encoding, callback)) { console.log(chunck.toString.toUpperCase()); callback(); this.push('123'); }}Copy the code
In the _transform method of a custom transform flow, both the push method that reads data and the callback that writes data can be used.
Transform
A type can convert a readable stream into a writable stream or convert a writable stream into a readable stream. Its main purpose is not to be responsible for reading and writing data like other types of streams, but to be both a readable stream and a writable stream, that is, to achieve special processing of data, such aszib
The compression stream implemented by the module,cropo
The encryption flow realized by the module is essentially a transformation flow, which is used as a writable flow to store the writable flow of the file contentpipe
Method writes to the transform stream and passes the transform stream as a readable streampipe
Method responds to the browser with processed data.
2. Verify the custom transformation flow
letmyTransForm = new MyTransform(); // Use the standard input process.stdin.pipe(myTransForm).pipe(process.stdin);Copy the code
The conversion stream is first written to the standard input as a writable stream, while stdin is used to read the stream, i.e. read the user’s input. After reading, the transform stream invokes the PIPE as a readable stream, writing the user-input information to the command line window via standard output, where stdout is used to write the stream.
conclusion
The most common types of custom flows have been covered above, but they are not really used in development. If you need to write a custom flow, it should be much more complex than the above. The main purpose of this article is to understand what a custom flow is, and understand the basic routines of writing a custom flow.