Node.js read and write stream to the same file at the same time

TL;DR

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?

Background

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

  • read
  • transform
  • write

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

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…

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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:

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. 🙂

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply