Learning node.js. Trumpet works by piping a stream back to itself, apparently, so the processed data can then be output. This makes no sense to me, since it seems to be like connecting both ends of a stream to itself. How does trumpet distinguish between the pre and post processed data? ie, why doesn't loud.pipe(...).pipe(loud)
result in some form of infinite loop of processing?
学习node.js.小号显然是通过将流管道回自身来工作,因此可以输出处理后的数据。这对我来说毫无意义,因为它似乎就像将流的两端连接到自身一样。小号如何区分前处理数据和后处理数据?即,为什么不loud.pipe(...)。管道(响亮)导致某种形式的无限循环处理?
var trumpet = require('trumpet');
var through = require('through');
var tr = trumpet();
var loud = tr.select('.loud').createStream();
loud.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(loud);
process.stdin.pipe(tr).pipe(process.stdout);
2 个解决方案
#1
18
I had the exact same confusion:
我有同样的困惑:
davecocoa had this to say to me at the nodeschool.io discussions thread on github. Below is an extract from https://github.com/nodeschool/discussions/issues/346
davecocoa有这个在github上的nodeschool.io讨论主题上对我说。以下是https://github.com/nodeschool/discussions/issues/346的摘录
I think you might be confusing the two streams loud
and tr
.
我想你可能会混淆这两个流和tr。
tr
is the main trumpet stream
tr是主要的小号流
- It is a transform stream (has input and output like a pipe)
- 它是一个变换流(具有像管道一样的输入和输出)
- It takes html as input
- 它需要html作为输入
- It outputs html
- 它输出html
- we connect
stdin
to its input, and we connect its output tostdout
- 我们将stdin连接到它的输入,并将它的输出连接到stdout
We created loud
by asking tr
to select html elements with class loud
我们通过要求tr大声选择html元素来大声创建
- It is a duplex stream (has input and output like a telephone)
- 它是双工流(具有像电话一样的输入和输出)
- it outputs or sends html elements
- 它输出或发送html元素
- it also receives html elements
- 它还接收html元素
tr
behaves such that, when html is streamed to it, if there are elements with class loud, they are output from loud
, which sends them to the through stream you built for making the text uppercase, which sends them to back to loud
's input, where they are reinserted into the html tr
originally received and output from tr
.
tr的行为是这样的,当html被传输到它时,如果有类大声的元素,它们是从大声输出的,它将它们发送到你为了使文本大写而构建的直流,它将它们发送回大声的输入,将它们重新插入最初收到的html tr并从tr输出。
I guess an important thing to note is that, although loud
has an important connection with tr
, they are not actually piped together at all.
我想要注意的一件重要的事情是,尽管声音与tr有重要联系,但它们实际上根本没有连接在一起。
#2
3
I was confused too and i would express it again with my words:
我也很困惑,我会用我的话再说一遍:
tr.select('.loud').createStream()
creates a Duplex-Stream which is nothing else than a combined ReadStream
and WriteStream
tr.select('。loud')。createStream()创建一个Duplex-Stream,它只是一个组合的ReadStream和WriteStream。
This Stream recieves all matches in its ReadStream. If you write to the WriteStream, trumpet takes it as match-replace
此Stream接收其ReadStream中的所有匹配项。如果您写入WriteStream,则trumpet将其作为匹配替换
This works for me in the same way:
这对我来说也是这样的:
// create trumpet stream
var tr = trumpet();
// create stream with selected elems
var trumpetSelector = tr.select('.loud');
var upperOut = trumpetSelector.createReadStream();
var upperIn = trumpetSelector.createWriteStream();
upperOut.pipe(new UpperCaseTransformer()).pipe(upperIn);
Please correct me if i'm wrong!
如果我错了请纠正我!
#1
18
I had the exact same confusion:
我有同样的困惑:
davecocoa had this to say to me at the nodeschool.io discussions thread on github. Below is an extract from https://github.com/nodeschool/discussions/issues/346
davecocoa有这个在github上的nodeschool.io讨论主题上对我说。以下是https://github.com/nodeschool/discussions/issues/346的摘录
I think you might be confusing the two streams loud
and tr
.
我想你可能会混淆这两个流和tr。
tr
is the main trumpet stream
tr是主要的小号流
- It is a transform stream (has input and output like a pipe)
- 它是一个变换流(具有像管道一样的输入和输出)
- It takes html as input
- 它需要html作为输入
- It outputs html
- 它输出html
- we connect
stdin
to its input, and we connect its output tostdout
- 我们将stdin连接到它的输入,并将它的输出连接到stdout
We created loud
by asking tr
to select html elements with class loud
我们通过要求tr大声选择html元素来大声创建
- It is a duplex stream (has input and output like a telephone)
- 它是双工流(具有像电话一样的输入和输出)
- it outputs or sends html elements
- 它输出或发送html元素
- it also receives html elements
- 它还接收html元素
tr
behaves such that, when html is streamed to it, if there are elements with class loud, they are output from loud
, which sends them to the through stream you built for making the text uppercase, which sends them to back to loud
's input, where they are reinserted into the html tr
originally received and output from tr
.
tr的行为是这样的,当html被传输到它时,如果有类大声的元素,它们是从大声输出的,它将它们发送到你为了使文本大写而构建的直流,它将它们发送回大声的输入,将它们重新插入最初收到的html tr并从tr输出。
I guess an important thing to note is that, although loud
has an important connection with tr
, they are not actually piped together at all.
我想要注意的一件重要的事情是,尽管声音与tr有重要联系,但它们实际上根本没有连接在一起。
#2
3
I was confused too and i would express it again with my words:
我也很困惑,我会用我的话再说一遍:
tr.select('.loud').createStream()
creates a Duplex-Stream which is nothing else than a combined ReadStream
and WriteStream
tr.select('。loud')。createStream()创建一个Duplex-Stream,它只是一个组合的ReadStream和WriteStream。
This Stream recieves all matches in its ReadStream. If you write to the WriteStream, trumpet takes it as match-replace
此Stream接收其ReadStream中的所有匹配项。如果您写入WriteStream,则trumpet将其作为匹配替换
This works for me in the same way:
这对我来说也是这样的:
// create trumpet stream
var tr = trumpet();
// create stream with selected elems
var trumpetSelector = tr.select('.loud');
var upperOut = trumpetSelector.createReadStream();
var upperIn = trumpetSelector.createWriteStream();
upperOut.pipe(new UpperCaseTransformer()).pipe(upperIn);
Please correct me if i'm wrong!
如果我错了请纠正我!