Node.js同时将流读取和写入同一文件 [英] Node.js read and write stream to the same file at the same time

查看:313
本文介绍了Node.js同时将流读取和写入同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

我正在浏览npmgithub上的许多解决方案,寻找可以让我同时在两个不同位置的readwrite到同一文件的某些解决方案.到目前为止,我很难找到这样的东西.是否存在某种允许使用该模块的模块?

I'm browsing through a number of solutions on npm and github looking for something that would allow me to read and write to the same file in two different places at the same time. So far I'm having trouble actually finding anything like this. Is there a module of some sort that will allow that?

背景

本质上,我的要求是,在大文件中,我需要按以下顺序进行操作:

In essence my requirement is that in a large file I need to, in the following order:

  • 阅读
  • 转换

理想情况下,用法如下:

Ideally the usage would be something like:

const fd = fs.open(file, "r+");
const read = createReadStreamSomehowFrom(fd);
const write = createWriteStreamSomehowFrom(fd);

read
   .pipe(new Transform(transform() {...}))
   .pipe(write);

我可以使用标准的fs.create[Read/Write]Stream来做到这一点,但是无法控制两个流的流量,如果我的写位置超出了读位置,那么我正在读我刚刚写的东西...

I could do that with standard fs.create[Read/Write]Stream but there's no way to control the flow of both streams and if my write position goes beyond read position then I'm reading something I just wrote...

用例与perl -p -i -e相同,异步读取和写入相同的文件(表示相同的inode),并替换内容而不将所有内容加载到内存中.

The use case is the same as perl -p -i -e, read and write to the same file (meaning the same inode) asynchronously and replace the contents without loading everything into memory.

我希望这是一个真实的用例,但是我发现的所有实现实际上都是将整个文件加载到内存中然后保存.我是在这里错过了一个已知的模块,还是需要实际编写类似的东西?

I would expect this a real world use case, yet all implementations I found actually load the whole file into memory and then save it. Am I missing a known module here or is there a need to actually write something like this?

推荐答案

嗯...看来很难. :)

Hmm... a tough one it seems. :)

因此,这里有记录-我没有找到这样的模块,实际上是和负责一个好的文件内替换模块的一些人讨论过的.看不到解决办法,我决定从头开始编写,这里是:

So here's for the record - I found no such module and actually discussed this with some people responsible for a nice in-file replacing module. Seeing no way to solve this I decided to write it from scratch and here it is:

  • signicode/rw-stream repo on github
  • rw-stream at npm

该模块以一种简单的原理工作,即在可读流中将其消耗掉之前,无法写入任何字节,而且其底层非常简单(结合fs.read/write ops并着眼于读写点).

The module works on a simple principle that no byte can be written until it has been consumed in the readable stream and it's fairly simple underneath (couple fs.read/write ops with keeping eye on the point of read and write).

如果您觉得这很有用,那么我很高兴. :)

If you find this useful then I'm happy. :)

这篇关于Node.js同时将流读取和写入同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆