unionfs
unionfs copied to clipboard
Fall through `.watch()` and `/watchFile()`
Possibly related to https://github.com/streamich/memfs/issues/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));
What are the options?
- Fall through to the last file system
- Watch the first file system
- Watch all file systems
- ...
What is the best behavior?
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
folderxthat 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
I’ve fixed this in 4.0.0 where you watch all available file systems for changes.
@LukeSheard is this closed then?
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.
See streamich/memfs#859