1. 程式人生 > >node.js中通過stream模組實現自定義流

node.js中通過stream模組實現自定義流

有些時候我們需要自定義一些流,來操作特殊物件,node.js中為我們提供了一些基本流類。

我們新建立的流類需要繼承四個基本流類之一(stream.Writeable,stream.Readable,stream.Duplex,stream.Transform),並確保呼叫了父類建構函式。

 

一、實現自定義的可讀流

實現可讀流需繼承 stream.Readable,並實現 readable._read() 方法。

下面的程式碼我們實現了一個從陣列中讀取資料的流

const {Readable} = require('stream');

//這裡我們自定義了一個用來讀取陣列的流
class ArrRead extends Readable {
    constructor(arr, opt) {
        //注意這裡,需呼叫父類的建構函式
        super(opt);
        this.arr = arr;
        this.index = 0;
    }

    //實現 _read() 方法
    _read(size) {
        //如果當前下標等於陣列長度,說明資料已經讀完
        if (this.index == this.arr.length) {
            this.push(null);
        } else {
            this.arr.slice(this.index, this.index + size).forEach((value) => {
                this.push(value.toString());
            });
            this.index += size;
        }
    }
}

let arr = new ArrRead([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], {
    highWaterMark: 2
});

//這樣當我們監聽 'data' 事件時,流會呼叫我們實現的 _read() 方法往緩衝區中讀取資料
//然後提供給消費者
arr.on('data', function (data) {
    console.log(data.toString());
});

  

二、實現自定義的可寫流

實現可寫流必須繼承 stream.Writeable ,並實現 writeable._write() 方法。writable._writev() 方法是可選的。

const {Writable} = require('stream');

//這裡我們自定義了一個用來寫入陣列的流
class ArrWrite extends Writable {
    constructor(arr, opt) {
        super(opt);
        this.arr = arr;
    }

    //實現 _write() 方法
    _write(chunk, encoding, callback) {
        this.arr.push(chunk.toString());
        callback();
    }
}

let data = [];
let arr = new ArrWrite(data, {
    highWaterMark: 3
});

arr.write('1');
arr.write('2');
arr.write('3');

console.log(data);

  

三、實現自定義的可讀可寫流

可讀可寫流必須繼承 stream.Duplex,並實現 readable._read() 和 writable._write() 方法。

const {Duplex} = require('stream');

//這裡我們自定義了一個用來寫讀可寫陣列的流
class ArrReadWrite extends Duplex {
    constructor(arr, opt) {
        super(opt);
        this.arr = arr;
        this.index = 0;
    }

    //實現 _write() 方法
    _write(chunk, encoding, callback) {
        this.arr.push(chunk.toString());
        callback();
    }

    //實現 _read() 方法
    _read(size) {
        //如果當前下標等於陣列長度,說明資料已經讀完
        if (this.index == this.arr.length) {
            this.push(null);
        } else {
            this.arr.slice(this.index, this.index + size).forEach((value) => {
                this.push(value.toString());
            });
            this.index += size;
        }
    }
}

let data = [];
let arrWR = new ArrReadWrite(data, {
    highWaterMark: 3
});

//往流中寫入資料
arrWR.write('1');
arrWR.write('2');
arrWR.write('3');
console.log(data);

//往流中讀取資料
console.log(arrWR.read(2).toString());
console.log(arrWR.read(2).toString());

  

四、自定義的轉換流

轉換流必須繼承 stream.Transform,需實現 transform._transform() 方法。

const {Transform} = require('stream');

//這裡我們自定義了一個用來轉換陣列的流
class Trans extends Transform {
    constructor(opt) {
        super(opt);
    }

    _transform(chunk, encoding, callback) {
        //將轉換後的資料輸出到可讀流
        this.push(chunk.toString().toUpperCase());
        //引數一是Error物件
        //引數二如果傳入,會被轉發到 readable.push()
        callback();
    }
}

let t = new Trans({
    highWaterMark: 3
});

t.on('data', function (data) {
    console.log(data.toString());
});

t.write('a');
t.write('b');
t.write('c');

轉換流就是將讀取到的資料做些計算然後輸出。轉換流既可以作為可讀流,又可以作為可寫流。

const {Transform} = require('stream');

//這裡我們自定義了一個用來轉換陣列的流
class Trans extends Transform {
    constructor(opt) {
        super(opt);
    }

    _transform(chunk, encoding, callback) {
        //將轉換後的資料輸出到可讀流
        this.push(chunk.toString().toUpperCase());
        //引數一是Error物件
        //引數二如果傳入,會被轉發到 readable.push()
        callback();
    }
}

let t = new Trans({
    highWaterMark: 3
});

t.on('data', function (data) {
    console.log('data', data.toString());
});

//stdin.pipe(t) 表示將我們的標準輸入寫入到我的轉換流 t 中,此時 t 是可寫流。
//pipe(process.stdout) 表示將轉換流 t 中的資料讀取到標準輸出中,此時 t 是可讀流。
process.stdin.pipe(t).pipe(process.stdout);