简体   繁体   English

在node.js中将一个流传输到两个流的最佳方法是什么

[英]What is optimal way to pipe one stream to two streams in node.js

I'm using a ReadableStream from request.js to pipe() a resource from http to a file system WritableStream. 我正在使用来自request.js的ReadableStream来将管道资源从http传递到文件系统WritableStream。

How do I modify this to be able to process the content in memory? 如何修改它以便能够处理内存中的内容? (for streamed parsing or whatever) (用于流解析或其他方法)

Ideally a full 'dupe' into two real streams and not just a callback 'tap' (but I'll take any advice). 理想情况下,将完整的“ dupe”分为两个真实流,而不仅仅是回调“ tap”(但我会接受任何建议)。

Ha, I had some lucky timing: the answer to my problem was announced today: 哈,我有一些幸运的时机:今天宣布了我的问题的答案:

highlandjs, new stream util library by @caolan (the guy from async ). highlandjs,@ caolan(来自async的家伙)开发的新流实用程序库。

This is what I needed: http://highlandjs.org/#fork 这就是我需要的: http : //highlandjs.org/#fork

It has the backpressure feature, lazy evaluation etc. 它具有反压功能,延迟评估等。

I would like to firstly pipe the content from http.req into memory (Buffer) through a module named memory-stream . 我想首先通过一个名为memory-stream的模块将内容从http.req传递到内存(缓冲区)中。 Then I create a readable from Buffer and pipe into file system. 然后,我从Buffer创建一个可读文件,然后将其传送到文件系统。 At the same time you can create multiple readable from this Buffer and pipe it to any writable. 同时,您可以从此Buffer创建多个可读文件,并将其通过管道传递到任何可写文件。

In one of my projects I need to process thumbnail files in many sizes once I received an image uploaded from browser and I was using the approach above to implement, save the original image into Buffer and create readable steams for each size, pipe with imagemagick and then pipe file system writable stream to save in parallel. 在我的一个项目中,一旦收到从浏览器上传的图像,并且使用上述方法实施,将原始图像保存到Buffer中并为每种尺寸创建可读的流,使用imagemagick和然后通过管道将文件系统可写流并行保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM