streamich / unionfs

Use multiple fs modules at once
https://www.npmjs.com/package/unionfs
The Unlicense
205 stars 24 forks source link

Suggestion: unionFS.promises.use() #751

Open ajvincent opened 1 year ago

ajvincent commented 1 year ago

I don't always use fs, but when I do, I prefer fs/promises. 😄

import fs from "fs";
import { Union } from "unionfs";
import { IFS } from "unionfs/lib/fs";

type FS_Promises = (typeof fs)["promises"];

type UseFSPromises = FS_Promises & {
  use(filesystem: FS_Promises): UseFSPromises
};

export function UnionFSPromises() : UseFSPromises
{
  const unionFS = new Union;
  const promises = unionFS.promises as UseFSPromises;
  promises.use = function(filesystem: FS_Promises) : UseFSPromises {
    unionFS.use({ promises: filesystem } as IFS);
    return this;
  }

  return promises;
}
G-Rath commented 1 year ago

I'm not sure what you're suggesting here - afaik unionfs should already work with fs.promises; could you explain in more detail what you're experiencing, and what you think could be improved?

ajvincent commented 1 year ago

Okay, you're looking for a genuine use case. Yes, unionfs works with fs.promises, at least as far as I can see. (Forgive me for filing right away, as I'm a first-time user of this library.)

Whenever possible, I prefer to use await statements and async functions. I usually import fs/promises instead of fs.

I was reading npmjs's package pages for unionfs and memfs, and thinking, "wow, this is really nice, and simple." Union instances have a .use() method for adding typeof fs instances.

My ideal case would be something like this:

import fs from "fs/promises";
import { Union } from "unionfs/promises";
import { Volume, createFSFromVolume } from "memfs/promises";

export function TemporaryFSPromises() : Union {
  const vol = new Volume;
  const inMemoryFS = createFsFromVolume(vol);

  const unionFS = new Union;
  return unionFS.use(fs).use(inMemoryFS);
}

In other words, like the synchronous filesystem API of Union.prototype.use, I'm looking for an asynchronous filesystem API for Union.prototype.promise.use or UnionPromise.prototype.use (assuming the latter would exist as a spin-off into its own export or package).

As my code sample in the original post suggests, I've already figured out how to emulate this on my own, so it's not a strict need. I just think, for parity with the fs/promises NodeJS package, it would be nice to have.

G-Rath commented 1 year ago

Right, so you're asking for a /promises aliases - I'm not against exploring that, though I don't know if it'll actually be worth it since fs/promises is literally an alias of fs.promises that's exported directly which we already support, and it might require some complexity to maintain two different versions of the Union class? (which probably should actually be the same class 🤔)