sea: mount bundled assets as a virtual file system · nodejs/node@4876627 · GitHub
Skip to content

Commit 4876627

Browse files
mcollinaaduh95
authored andcommitted
sea: mount bundled assets as a virtual file system
Support "useVfs": true in the SEA configuration: mount the bundled assets as a read-only VFS and run the CommonJS or ESM main script from inside the mount. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #65675 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 46863a7 commit 4876627

28 files changed

Lines changed: 1059 additions & 2 deletions

‎doc/api/single-executable-applications.md‎

Lines changed: 104 additions & 0 deletions

‎doc/api/vfs.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,37 @@ system, the callers are responsible for avoiding removal or
419419
invalidation of modules in the virtual file system while they are
420420
being loaded.
421421

422+
## Use with Single Executable Applications
423+
424+
When running as a [Single Executable Application][] built with
425+
`"useVfs": true` in the SEA configuration, the bundled assets are
426+
automatically mounted as a read-only virtual file system and the injected
427+
main script is executed from the root of the mount. No additional setup is
428+
required. Since the mount point is reserved and chosen at runtime, bundled
429+
code accesses the assets through `__dirname`-relative paths and relative
430+
`require()` calls rather than through a fixed path:
431+
432+
```cjs
433+
// In the SEA main script, __dirname is the root of the mounted assets.
434+
const fs = require('node:fs');
435+
const path = require('node:path');
436+
437+
const config = JSON.parse(
438+
fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
439+
const template = fs.readFileSync(
440+
path.join(__dirname, 'templates/index.html'), 'utf8');
441+
```
442+
443+
ESM entry points (`"mainFormat": "module"`) are supported: the main module
444+
is loaded from inside the mount through the ESM loader, and
445+
`import.meta.dirname` points at the mount root.
446+
447+
`"useVfs"` cannot be used together with `"useSnapshot"` or `"useCodeCache"`.
448+
The SEA configuration parser will error if either combination is detected.
449+
450+
See the [Single Executable Application][] documentation for more information
451+
on creating SEA builds with assets.
452+
422453
## Class: `VirtualProvider`
423454

424455
<!-- YAML
@@ -591,6 +622,7 @@ fields use synthetic but stable values:
591622
[CommonJS resolution algorithm]: modules.md#all-together
592623
[ES modules resolution algorithm]: esm.md#resolution-algorithm
593624
[Explicit Resource Management]: https://github.com/tc39/proposal-explicit-resource-management
625+
[Single Executable Application]: single-executable-applications.md
594626
[`MemoryProvider`]: #class-memoryprovider
595627
[`RealFSProvider`]: #class-realfsprovider
596628
[`VirtualFileSystem`]: #class-virtualfilesystem

‎lib/internal/main/embedding.js‎

Lines changed: 51 additions & 2 deletions

0 commit comments

Comments
 (0)