jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.44k stars 776 forks source link

Feature: openWriteStream #945

Closed Nokel81 closed 2 years ago

Nokel81 commented 2 years ago

I just ran into this problem with the fs and was wondering if this package should have a good solution to this.

I have just discovered that in some cases createWriteStream doesn't immediately throw errors if you pass in flags "wx" for instance.

I found that it possible to get around this by calling await open(path, ...) and then wrapping the file handle in a stream.Writable.

I am wondering if some a function would be useful here?

RyanZim commented 2 years ago

How does https://nodejs.org/api/fs.html#fspromisesopenpath-flags-mode work here?

Nokel81 commented 2 years ago

It works quite well because (I assume) it uses the syscalls immediately, which is why it is async.

RyanZim commented 2 years ago

If that works, why not just use that?

Nokel81 commented 2 years ago

I am, I was wondering if something like it should be lifted up into this package or easier reuse by others.

The reason why it is more than just a call to open is because there also needs the following (or something like it if this is wrong):

new Writable({
  write(chunk, encoding, cb) {
    fileHandle.write(chunk)
      .then(() => cb())
      .catch(cb);
  },
});
RyanZim commented 2 years ago

No, just use https://nodejs.org/api/fs.html#filehandlecreatewritestreamoptions

Nokel81 commented 2 years ago

Ah I see, that was added in node 16 and I am on node 14. Sorry for the noise.