新手的 Stream.concat 函数实现, 合并文件
发布于 6 年前 作者 Luyakus 3092 次浏览 来自 分享

周末逛 V2EX, 在 Node 板块看到一个问题, 把上千个小文件合并到一个大文件, 看到有小伙伴说用自定义可读流实现, 但是这样的话还需要自己建立缓冲区, 自己监听 writstream ‘drain’ 事件, 我考虑后觉得应该充分利用 pipe 给开发者的便利, 写了一版简陋的合并实现, 由于是个新手, 多有不足, 抛砖引玉, 还请大家多多指教

const {Readable, Writable} = require('stream')
const fs = require('fs')

Readable.prototype.cpipe = function(writableStream, opts) {
    let preReadAbleStream = this.preStream
    let curReadAbleStream = this
    this.on('end', () => writableStream.end('\n'))
    while(preReadAbleStream) {
        // (function(cur, pre){
        //     pre.on('end', () => {
        //         writableStream.write('\n----------')
        //         cur.pipe(writableStream, opts)
        //     })
        // })(curReadAbleStream, preReadAbleStream)

        let cur = curReadAbleStream
        let pre = preReadAbleStream
        pre.on('end', () => {
            writableStream.write('\n----------')
            cur.pipe(writableStream, opts)
        })
		// 上面哪种写法好一些呢, 大家说说看法
		
        curReadAbleStream = preReadAbleStream
        preReadAbleStream = curReadAbleStream.preStream
    }
    return curReadAbleStream.pipe(writableStream, opts)
}

Readable.prototype.concat = function(readableStream) {
    readableStream.preStream = this
    return readableStream
}

let colors = fs.createReadStream('./colors.js')
let playground = fs.createReadStream('../playground.js')
let tail = fs.createReadStream('./ntail.js')
let ws = new Writable({
    write(data, encoding, callback) {
        console.log(data.toString())
        callback()
    }
})

colors.concat(playground).concat(tail).cpipe(ws, {end : false}).on('finish', () => {
    console.log('stream end')
})

学习 Node 一段时间啦, 感觉有了顺手工具后, 充分激发了创造能力, 祝 Node 长青

1 回复

挽尊 :)

回到顶部