管道输出out2

时间:2015-10-25 01:15:51

标签: node.js stream

我第一次尝试通过2,我遇到了一些麻烦。我通过一系列流来处理我的数据,我想要管理我的转换功能。

文档说我应该使用this.push从我的转换函数中获取数据,然后在我完成时调用回调,所以我尝试使用管道(因为这应该做)同样的事情,对吧?),但是我得到了一个writeAfterEnd错误。我的代码和堆栈跟踪在下面,但我不知道该怎么做。有人能指出我正确的方向吗?

代码 -

function run(chunk, encoding, callback) {
    this.on("finish", function(){
        console.log("this finished")
        callback()
    })
    dataStream = new stream.Readable({objectMode: true});
    dataStream._read = function noop(){};
    dataStream.push(chunk);
    dataStream.pipe(sass()).pipe(this)
}

gulp.src("./web-src/scss/add_post.scss").pipe(through.obj(run)).on('data', function(chunk){console.log(chunk)})

Stacktrace -

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: write after end
    at writeAfterEnd (/Volumes/BackRoom/Dropbox/Timpani/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:198:12)
    at DestroyableTransform.Writable.write (/Volumes/BackRoom/Dropbox/Timpani/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:243:5)
    at write (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)
    at DestroyableTransform.pipeOnReadable (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:664:5)
    at DestroyableTransform.emit (events.js:104:17)
    at emitReadable_ (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:448:10)
    at emitReadable (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:444:5)
    at readableAddChunk (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:187:9)
    at DestroyableTransform.Readable.push (/Volumes/BackRoom/Dropbox/Timpani/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:149:10)

1 个答案:

答案 0 :(得分:1)

您是在实施Stream还是仅使用through2?

API docs warn against消费者使用.push()。

错误:如果chunk === null,可能会发生写操作,这是流实现者的" API"解释为EOF。