无法使用转换流创建流管道

时间:2016-02-23 23:05:24

标签: node.js

这是我的代码:

stream = require 'stream'
stringer = new stream.Transform objectMode: true
stringer2 = new stream.Transform objectMode: true

stringer.push 'hello'
stringer.push 'world'
stringer.push null


stringer2._transform = (chunk, enc, done) -> 
    console.log chunk.toString()
    done()
    return @

stringer.pipe(stringer2).pipe process.stdout

我能够在stringer2中记录每个块,但我无法将结果传递给stdout。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要调用this.push(chunk)将已转换的块传递到下一个管道。你也可以省略return this,虽然它不会伤害任何东西。

在coffeescript中,它看起来像这样:

stringer2._transform = (chunk, enc, done) ->
    console.log chunk.toString()
    @push chunk
    done()

或者,作为快捷方式,您可以将块传递给done()作为第二个参数(第一个是错误,如果没有错误,则返回null):

stringer2._transform = (chunk, enc, done) ->
    console.log chunk.toString()
    done null, chunk