最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does piping a stream back to itself work with Trumpet? - Stack Overflow

programmeradmin2浏览0评论

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?

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);

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?

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);
Share Improve this question asked Jun 8, 2014 at 7:34 gdbjgdbj 17.5k6 gold badges37 silver badges52 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 20

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


I think you might be confusing the two streams loud and tr.

tr is the main trumpet stream

  • It is a transform stream (has input and output like a pipe)
  • It takes html as input
  • It outputs html
  • we connect stdin to its input, and we connect its output to stdout

We created loud by asking tr to select html elements with class loud

  • It is a duplex stream (has input and output like a telephone)
  • it outputs or sends html elements
  • it also receives html elements

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.

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.


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

This Stream recieves all matches in its ReadStream. If you write to the WriteStream, trumpet takes it as match-replace

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!

发布评论

评论列表(0)

  1. 暂无评论