```
//1) 自定義可寫流
let {Readable} = require('stream');
// 想實現什么流 就繼承這個流
// Readable 里面有一個 read() 方法,默認調_read();
// Readable中提供了一個push方法,你調用push方法就會觸發data事件
// let index = 9;
// class MyRead extends Readable{
// _read(){
// // 可讀流什么時候停止呢? 當push null的時候停止
// if(index-->0)return this.push('123');
// this.push(null);
// }
// }
// let mr = new MyRead;
// mr.on('data',function(data){
// console.log(data);
// });
// --- --- ---
//2) 自定義可寫流
let {Writable} = require('stream');
// 可寫流實現_write方法
// 源碼中默認調用的是Writable中的write方法
// class MyWrite extends Writable{
// _write(chunk,encoding,callback){
// console.log(chunk.toString());
// callback(); // clearBuffer()
// }
// }
// let mw = new MyWrite();
// mw.write('珠峰','utf8',()=>{
// console.log(1);
// })
// mw.write('珠峰','utf8',()=>{
// console.log(1);
// })
// --- --- ---
//3) Duplex雙工流
let {Duplex} = require('stream');
// 雙工流 又能讀 又能寫,而且讀寫可以沒關系
// class D extends Duplex{
// _read(){
// }
// _write(){
// }
// }
// let d = Duplex({
// read(){ //等同于上面的_read
// this.push('hello');
// this.push(null);
// }
// ,write(chunk,encoding,callback){
// console.log(chunk);
// callback();
// }
// })
// d.on('data',function(data){
// console.log(data); //<Buffer 68 65 6c 6c 6f>
// })
// d.write('world'); // <Buffer 77 6f 72 6c 64>
//可以只實現一個 讀或則寫
// --- --- ---
//4) transform流 他就是Duplex 但它不需要實現read和write,它實現的是transform
let {Transform} = require('stream');
// 他的參數和可寫流一樣
let transform1 = Transform({
transform(chunk,encoding,callback){
// console.log(chunk); //<Buffer 31 0d 0a> // 0d \r 0a \n
// callback(); //必須調 不然卡死 //類似于next
this.push(chunk.toString().toUpperCase()); // 將輸入的內容放入到可讀流中
callback(); //比填
}
});
let transform2 = Transform({
transform(chunk,encoding,callback){
console.log(chunk.toString());
callback();
}
});
// 等待你輸入
// rs.pipe(ws);
process.stdin.pipe(transform1).pipe(transform2)
// 可讀流里只能放 buffer 或則 字符串 對象流里可以放對象
```