我目前正在通过运行发出'line'
事件的转换流逐行处理文件流。我希望能够在找到当前行匹配某些条件后,暂停输入文件流,开始处理新流,并在完成后,逐行继续处理原始流。我将其浓缩为下面的一个最小例子:
test.coffee:
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
inStream.on 'line', (line) ->
process.stdout.write "-->"
if line.match /line\.match/g
process.stdout.write line
console.error "PAUSE"
inStream.pause()
fs.createReadStream("./test.coffee").pipe(process.stdout).on 'end', ->
console.error "UNPAUSE"
inStream.resume()
else
process.stdout.write line
test-transform.coffee:
Transform = require('stream').Transform
module.exports =
class TestTransform extends Transform
constructor: ->
Transform.call @, readableObjectMode: true
@buffer = ""
pushLines: ->
newlineIndex = @buffer.indexOf "\n"
while newlineIndex isnt -1
@push @buffer.substr(0, newlineIndex + 1)
@emit 'line', @buffer.substr(0, newlineIndex + 1)
@buffer = @buffer.substr(newlineIndex + 1)
newlineIndex = @buffer.indexOf "\n"
_transform: (chunk, enc, cb) ->
@buffer = @buffer + chunk.toString()
@pushLines()
cb?()
_flush: (cb) ->
@pushLines()
@buffer += "\n" # ending newline
@push @buffer
@emit 'line', @buffer # push last line
@buffer = ""
cb?()
(不要过多担心变换流,它只是一个例子。)无论如何,coffee test.coffee
的输出看起来像:
-->fs = require 'fs'
-->
-->TestTransform = require './test-transform'
-->
-->inStream = new TestTransform
-->
-->fs.createReadStream("./test.coffee").pipe(inStream)
-->
-->inStream.on 'line', (line) ->
--> process.stdout.write "-->"
--> if line.match /line\.match/g
PAUSE
--> process.stdout.write line
--> console.error "PAUSE"
--> inStream.pause()
--> fs.createReadStream("./test.coffee").pipe(process.stdout).on 'end', ->
--> console.error "UNPAUSE"
--> inStream.unpause()
--> else
--> process.stdout.write line
-->
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
inStream.on 'line', (line) ->
process.stdout.write "-->"
if line.match /line\.match/g
process.stdout.write line
console.error "PAUSE"
inStream.pause()
fs.createReadStream("./test.coffee").pipe(process.stdout).on 'end', ->
console.error "UNPAUSE"
inStream.unpause()
else
process.stdout.write line
很明显,管道没有暂停,它只是一直持续到完成(即使PAUSE
正在按预期运行),并且"UNPAUSE"
是永远不会被写出来,'end'
回调永远不会被解雇。将流切换为从变换流中暂停/取消暂停到readStream似乎也不起作用。我假设从这种行为中,节点流不知何故不尊重事件回调中的暂停/取消暂停。
可能还有另一种方法可以在不调用暂停/取消暂停的情况下完成此操作;如果有某种方式可以等待流的结束并暂停当前的执行线程,那么这将有效地做我想做的事情。
答案 0 :(得分:2)
如果我正确理解了这个问题,这里有一个使用Dust.js的简单Node应用程序来解决问题。
Dust是一个模板引擎,但其最好的功能之一是它对Node Streams的原生理解。此示例使用Dust 2.7.0。
我使用node-byline
作为您的转换流的替代品,但它做同样的事情 - 逐行读取。
var fs = require('fs'),
byline = require('byline'),
dust = require('dustjs-linkedin');
var stream = byline(fs.createReadStream('./test.txt', { encoding: 'utf8' }));
var template = dust.loadSource(dust.compile('{#byline}--> {.|s}{~n}{match}{/byline}'));
dust.stream(template, {
byline: stream,
match: function(chunk, context) {
var currentLine = context.current();
if(currentLine.match(/line\.match/g)) {
return fs.createReadStream('./test.txt', 'utf8');
}
return chunk;
}
}).pipe(process.stdout);
这是我程序的输出:
$ node index.js
--> fs = require 'fs'
--> TestTransform = require './test-transform'
--> inStream = new TestTransform
--> fs.createReadStream("./test.coffee").pipe(inStream)
--> inStream.on 'line', (line) ->
--> process.stdout.write "-->"
--> if line.match /line\.match/g
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
inStream.on 'line', (line) ->
process.stdout.write "-->"
if line.match /line\.match/g
process.stdout.write line
console.error "PAUSE"
inStream.pause()
fs.createReadStream("./test.coffee").pipe(process.stdout).on 'end', ->
console.error "UNPAUSE"
inStream.resume()
else
process.stdout.write line
--> process.stdout.write line
--> console.error "PAUSE"
--> inStream.pause()
--> fs.createReadStream("./test.coffee").pipe(process.stdout).on 'end', ->
--> console.error "UNPAUSE"
--> inStream.resume()
--> else
--> process.stdout.write line
如您所见,它正确地交错了输出。如果我可以进一步详细说明尘埃部分是如何工作的,请告诉我。
编辑:以下是专门针对Dust模板的解释。
{#byline} {! look for the context variable named `byline` !}
{! okay, it's a stream. For each `data` event, output this stuff once !}
-->
{.|s} {! output the current `data`. Use |s to turn off HTML escaping !}
{~n} {! a newline !}
{match} {! look up the variable called `match` !}
{! okay, it's a function. Run it and insert the result !}
{! if the result is a stream, stream it in. !}
{/byline} {! done looping !}
答案 1 :(得分:1)
我实际上也找到了一个单独的答案;不是很漂亮,但也有效。
基本上,pause()
只会暂停来自管道流的输出(在"流动"模式下);因为我正在听'line'
事件,所以它不流动,所以pause
当然没有做任何事情。因此,第一个解决方案是使用removeListener
而不是pause
,这有效地阻止了流式传输。该文件现在看起来像:
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
c = (line) ->
process.stdout.write "-->"
if line.match /line\.match/g
process.stdout.write line
console.error "PAUSE"
inStream.removeListener 'line', c
f = fs.createReadStream("./test.coffee")
f.on 'end', ->
console.error "UNPAUSE"
inStream.on 'line', c
f.pipe(process.stdout)
else
process.stdout.write line
inStream.on 'line', c
这会产生几乎的输出:
-->fs = require 'fs'
-->TestTransform = require './test-transform'
-->inStream = new TestTransform
-->fs.createReadStream("./test.coffee").pipe(inStream)
-->c = (line) ->
--> process.stdout.write "-->"
--> if line.match /line\.match/g
PAUSE
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
c = (line) ->
process.stdout.write "-->"
if line.match /line\.match/g
process.stdout.write line
console.error "PAUSE"
inStream.removeListener 'line', c
f = fs.createReadStream("./test.coffee")
f.on 'end', ->
console.error "UNPAUSE"
inStream.on 'line', c
f.pipe(process.stdout)
else
process.stdout.write line
inStream.on 'line', c
UNPAUSE
然而,当我删除侦听器时,看起来原始可读流刚刚停止;这使得一些扭曲的意义(我猜节点垃圾收集其所有侦听器被删除后的可读流)。所以我找到的最终工作解决方案依赖于管道。由于上面显示的转换流也将其输出逐行推送到任何'data'
侦听器,因此pause()
可以有效地用于其原始目标,而不仅仅是杀死流。最终输出:
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
inStream.on 'data', (chunk) ->
line = chunk.toString()
process.stdout.write "-->#{line}"
if line.match /line\.match/g
inStream.pause()
f = fs.createReadStream("./test.coffee")
f.on 'end', ->
inStream.resume()
f.pipe(process.stdout)
带输出:
-->fs = require 'fs'
-->TestTransform = require './test-transform'
-->inStream = new TestTransform
-->fs.createReadStream("./test.coffee").pipe(inStream)
-->inStream.on 'data', (chunk) ->
--> line = chunk.toString()
--> process.stdout.write "-->#{line}"
--> if line.match /line\.match/g
fs = require 'fs'
TestTransform = require './test-transform'
inStream = new TestTransform
fs.createReadStream("./test.coffee").pipe(inStream)
inStream.on 'data', (chunk) ->
line = chunk.toString()
process.stdout.write "-->#{line}"
if line.match /line\.match/g
inStream.pause()
f = fs.createReadStream("./test.coffee")
f.on 'end', ->
inStream.resume()
f.pipe(process.stdout)
--> inStream.pause()
--> f = fs.createReadStream("./test.coffee")
--> f.on 'end', ->
--> inStream.resume()
--> f.pipe(process.stdout)
-->
这是预期的结果。