admin管理员组文章数量:1290959
What are my options to pipe stream to a variable? Based on this documentation the examples of writable stream include:
- http requests, on the client
- http responses, on the server fs write
- streams zlib streams
- crypto streams
- tcp sockets
- child process stdin
- process.stdout, process.stderr
So does it mean I cannot pipe stream to a variable to process it? In fact I dont want to save the stream on my disk, so what is the best way to pipe all of the streams and use that data?
Thanks please let me know if you need more clarification!
What are my options to pipe stream to a variable? Based on this documentation the examples of writable stream include:
- http requests, on the client
- http responses, on the server fs write
- streams zlib streams
- crypto streams
- tcp sockets
- child process stdin
- process.stdout, process.stderr
So does it mean I cannot pipe stream to a variable to process it? In fact I dont want to save the stream on my disk, so what is the best way to pipe all of the streams and use that data?
Thanks please let me know if you need more clarification!
Share Improve this question asked Jan 18, 2015 at 5:44 user3399784user3399784 4-
1
You can pipe a stream to a variable, but pipe is generally used to pipe the stream to some other method that can utilize the stream, if you just want the data in a variable there are events for that, like
on('data')
etc – adeneo Commented Jan 18, 2015 at 5:58 - Thanks man! and is there any difference between on('data') and on('readable')? – user3399784 Commented Jan 18, 2015 at 6:11
-
1
Both events are described in the documentation, but the difference is somewhat cryptic,
readable
fires when the stream is, well, readable, while data apparently fires when there's data. What the specific differences are, I don't know ! – adeneo Commented Jan 18, 2015 at 6:25 - 1 Would you mind adding your answer and I mark it as an answer for future reference; it can be usefull for people who are new to Node.JS/JavaScript like me! And thanks again! – user3399784 Commented Jan 18, 2015 at 6:26
1 Answer
Reset to default 8You probably can pipe a stream to a variable, but pipe
is generally used to pipe the stream to some other method that can utilize the stream, as pipe
pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.
For instance, piping a stream to a file
someReadableStream.pipe(fs.createWriteStream("result.json"));
If you just want the data in a variable, there are events for that, that are probably easier to use, like on('data')
var readable = getReadableStreamSomehow(),
result = '';
readable.on('data', function(chunk) {
result += chunk;
});
readable.on('end', function () {
// do something with "result"
});
本文标签: javascriptPiping stream to a variable or Stack Overflow
版权声明:本文标题:javascript - Piping stream to a variable!? or ....? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504207a2382221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论