Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall through .watch() and /watchFile() #89

Open
bytesnz opened this issue Apr 23, 2018 · 6 comments
Open

Fall through .watch() and /watchFile() #89

bytesnz opened this issue Apr 23, 2018 · 6 comments

Comments

@bytesnz
Copy link

bytesnz commented Apr 23, 2018

Possibly related to streamich/memfs#116

Not sure if this is a bug or a feature request. Currently, (at least) fs.watch() calls do not fall through to the other file systems that are joint in the unionfs. Was going to say it would be good if on detection of a ENOENT error on the first file system, if unionfs tried the other filesystems in the union for the file, but that will cause issues when you then create the folder/file on the top unionfs file system.

const patchFs = require('fs-monkey').patchFs;
const Volume = require('memfs').Volume;
const ufs = require('unionfs').ufs;
const fs = require('fs');
const path = require('path');
 
const vol = new Volume();
const fs2 = Object.assign({}, fs);
 
ufs.use(fs2).use(vol);
 
patchFs(ufs);
 
var dir = ufs.readdirSync('./');
console.log(dir);

fs.watch('./', { recurse: true }, (eventType, filename) => console.log('watch event', eventType, filename));
@streamich
Copy link
Owner

What are the options?

  1. Fall through to the last file system
  2. Watch the first file system
  3. Watch all file systems
  4. ...

What is the best behavior?

@bytesnz
Copy link
Author

bytesnz commented Apr 23, 2018

The best I could come up with was having a watch manager in union fs that when watch is called, it attaches the initial watch to the file on the filesystem that has that the file and then monitors filesystems above for changes as well and then adds a watch to that filesystem as well.

That would mean that you would be able handle things like:

  • have a memfs on top of the normal fs
  • create a watch on folderx that is only on the normal fs
  • create a file inside folderx (which will be created on the memfs)
  • watch callback should get an event saying a new file was created

An interesting case would be what happens when a folder is created on a filesystem above a filesystem that already has that folder - do you then union that folder as well - do a readdir and get the contents of both the folder on the memfs and the folder on the normal filesystem.

I would think this functionality could be helpful in testing. I am not currently using it though, so it could just be a won't fix for now.

FYI I am just using ufs, memfs and monkey-patch at the moment. Testing a file parser and a file watcher on files that are on memfs.
https://gitlab.com/bytesnz/marss/blob/initial-developement/src/lib/contentHandlers/content.tests.ts
https://gitlab.com/bytesnz/marss/blob/initial-developement/src/tests/lib/unionfs.ts

@LukeSheard
Copy link
Contributor

I’ve fixed this in 4.0.0 where you watch all available file systems for changes.

@iamogbz
Copy link

iamogbz commented Apr 10, 2020

@LukeSheard is this closed then?

@natew
Copy link

natew commented Jan 17, 2021

I'm getting weird behavior:

const ufs = new Union()
const infs = ufs
      // @ts-expect-error
      .use(this.memoryFS)
      .use({ ...fs })

    patchFs(ufs)

console.log(ufs.watch('').on)

It logs undefined, using memfs 3.2.0 unionfs 4.4.0 and fs-monkey 1.0.1.

@wood1986
Copy link

wood1986 commented Oct 2, 2021

See streamich/memfs#859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants