Skip to content

vfs: add --vfs-load to run modules from a mount - #8

Draft
pipobscure wants to merge 1 commit into
vfsmountfrom
vfsload
Draft

vfs: add --vfs-load to run modules from a mount#8
pipobscure wants to merge 1 commit into
vfsmountfrom
vfsload

Conversation

@pipobscure

Copy link
Copy Markdown
Owner

Builds on --vfs-mount to make a mount a real execution environment, resolving require()/import inside it.

Adds --vfs-load, which runs the entry point (process.argv[1]) and all subsequent require()/import resolution against the last --vfs-mount rather than the real file system. process.argv[1] becomes that mount's root, as if node <mountPoint> had been run, so the mount's own package.json "main"/index.js selects the entry point and positional arguments are the program's own. A self-mounting shebang (#!/usr/bin/env -S node --vfs-load --vfs-mount) therefore makes an archive directly executable. --vfs-load requires at least one --vfs-mount, and workers inherit it alongside the mounts.

Making resolution VFS-aware requires four of the CJS/ESM loader's resolution primitives - package.json reading, nearest-parent/scope lookup, legacy main resolution, and extensionless-file format sniffing - to stop bypassing the public fs module and call it instead, since that bypass is what let them ignore a mount. Each defers to the real native binding unchanged for anything outside an active mount. A lean mount registry (internal/vfs/registry) is extracted from internal/vfs/setup so the resolution fast path can answer "is this path under a mount?" without loading the heavier setup/provider machinery.

Provider selection - and, for a --vfs-load entry, its resolution - is deferred until after -r and --import preload modules have run, so a provider registered from either backs the mount.

Native addons are supported: a directory-backed mount dlopens the real underlying file directly (RealFSProvider.toRealPath); an archive-backed mount extracts the addon to a content-hashed temp file first, scoped per pid so processes don't clobber each other's extractions.

Builds on --vfs-mount to make a mount a real execution environment,
resolving require()/import inside it.

Adds --vfs-load, which runs the entry point (process.argv[1]) and all
subsequent require()/import resolution against the last --vfs-mount
rather than the real file system. process.argv[1] becomes that mount's
root, as if `node <mountPoint>` had been run, so the mount's own
package.json "main"/index.js selects the entry point and positional
arguments are the program's own. A self-mounting shebang
(`#!/usr/bin/env -S node --vfs-load --vfs-mount`) therefore makes an
archive directly executable. --vfs-load requires at least one
--vfs-mount, and workers inherit it alongside the mounts.

Making resolution VFS-aware requires four of the CJS/ESM loader's
resolution primitives - package.json reading, nearest-parent/scope
lookup, legacy main resolution, and extensionless-file format sniffing -
to stop bypassing the public fs module and call it instead, since that
bypass is what let them ignore a mount. Each defers to the real native
binding unchanged for anything outside an active mount. A lean mount
registry (internal/vfs/registry) is extracted from internal/vfs/setup so
the resolution fast path can answer "is this path under a mount?"
without loading the heavier setup/provider machinery.

Provider selection - and, for a --vfs-load entry, its resolution - is
deferred until after -r and --import preload modules have run, so a
provider registered from either backs the mount.

Native addons are supported: a directory-backed mount dlopens the real
underlying file directly (RealFSProvider.toRealPath); an archive-backed
mount extracts the addon to a content-hashed temp file first, scoped
per pid so processes don't clobber each other's extractions.

Signed-off-by: Philipp Dunkel <pip@pipobscure.com>
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

Successfully merging this pull request may close these issues.

1 participant