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

1

7

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?

Jaundiced answered 28/9, 2018 at 19:38 Comment(0)
J
9

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. :)

Jaundiced answered 5/11, 2018 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.