lightning-fs icon indicating copy to clipboard operation
lightning-fs copied to clipboard

Missing fs.existsSync

Open Pomax opened this issue 1 year ago • 4 comments

It looks like the fairly critical fs.existsSync is missing from the set of available functions, which makes otherwise "simple if check" code a "try/catch with dangling let variables" mess. Could .existsSync be added?

Pomax avatar Oct 11 '24 16:10 Pomax

fs.exists is deprecated in Node.js, so I don't see a reason for adding this.

jcubic avatar Oct 11 '24 16:10 jcubic

You can create this function very easy if you need it:

async function exists(path) {
  try {
    await fs.stat(path);
    return true;
  } catch(e) {
    return false;
  }
}

or

function exists(path) {
  return fs.stat(path)
    .then(() => true)
    .catch(() => false);
}

no need for dangling let.

jcubic avatar Oct 11 '24 16:10 jcubic

Sure, but existsSync is not (not sure why I wrote exists without that, all my code uses the sync version... updated the title and initial comment).

I know it's easy enough to shim, but that shouldn't be necessary.

Pomax avatar Oct 11 '24 22:10 Pomax

I don't think that you can implement existsSync with lightingFS that use promise based API.

But if you want to contribute and implement it (even that I think it's not possible) go ahead.

jcubic avatar Oct 11 '24 22:10 jcubic

But if you want to contribute and implement it (even that I think it's not possible) go ahead.

I think it would have to be done as part of the hybrid system. E.g. other operations would need to synchronously track if a path exists, then existsSync checks that synchronous mapping.

Not that I want/need it or am going to implement it, just putting it out there.

jeff-hykin avatar Apr 16 '25 14:04 jeff-hykin

My suggestion is to Close as not planned (won't fix) since this feature does not seem to be feasible. As described in the README, the implementation is tightly integrated with IndexedDB's asynchronous API and, as a consequence, so is the initialization code. Even if we added existsSync to the in-memory layer (CacheFS.js?) the caller would still need asynchronous code to wait for the underlying FS and backend to be ready.

@Pomax, we're here to support you if you'd like to pursue this further, discuss the feasibility, or implementation options. I think you'd find tracing the rename method through the call stack enough to realize this would be a tough one.

seanpoulter avatar Apr 28 '25 01:04 seanpoulter

I have nowhere near the time to work on this myself, feel free to close.

Pomax avatar Apr 28 '25 14:04 Pomax