Repository navigation
Best way to redirect vfs writes to real fs in when using sea
#301
Replies: 1 comment
|
The good news is you probably don't need one. In SEA mode the VFS ( So writes aren't "trapped in the VFS" — they just target a path that doesn't exist on disk. All you need is the path rewrite itself, which you can install in userland: your code runs after the VFS has patched if (process.pkg && !process.pkg.mount) {
const fs = require('fs');
const path = require('path');
const stdlib = require('@grain/stdlib');
const target = path.join(process.cwd(), 'target');
fs.mkdirSync(target, { recursive: true });
const prefix = stdlib + path.sep;
const esc = prefix.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
const rule = new RegExp(`^${esc}(.+)(?<!\\.gr)$`); // same rule as your mount()
const remap = (p) => {
if (typeof p !== 'string') return p;
const m = rule.exec(p);
return m ? path.join(target, m[1]) : p;
};
const ONE = ['readFileSync','readFile','writeFileSync','writeFile','appendFileSync','appendFile',
'openSync','open','statSync','stat','lstatSync','lstat','readdirSync','readdir','existsSync',
'accessSync','access','mkdirSync','mkdir','rmSync','rm','unlinkSync','unlink','realpathSync',
'realpath','createReadStream','createWriteStream'];
const TWO = ['renameSync','rename','copyFileSync','copyFile'];
for (const api of [fs, fs.promises]) { // require('fs/promises') is the same object as fs.promises
for (const name of ONE) {
const orig = api[name];
if (typeof orig !== 'function') continue;
api[name] = function (p, ...rest) { return orig.call(api, remap(p), ...rest); };
}
for (const name of TWO) {
const orig = api[name];
if (typeof orig !== 'function') continue;
api[name] = function (a, b, ...rest) { return orig.call(api, remap(a), remap(b), ...rest); };
}
}
}Verified in a Two caveats: run this before anything caches a destructured reference ( If a first-class |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am working on switching an app from using the older
pkg builderto the node-sea builder, and previously I had been using:To redirect writes to the vfs to the disk while keeping reads on the vfs, is there anyway for me to keep this behaviour with the newer vfs?.
All reactions