Skip to content

Commit 070ee38

Browse files
committed
doc: add VFS section to single-executable-applications
Document how to use the Virtual File System (VFS) with Single Executable Applications to access bundled assets through standard fs APIs. Includes usage of fs.getSeaVfs(), fs.hasSeaAssets(), supported operations, and the createRequire() requirement for loading bundled modules.
1 parent f9826ef commit 070ee38

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

doc/api/single-executable-applications.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,90 @@ const raw = getRawAsset('a.jpg');
238238
See documentation of the [`sea.getAsset()`][], [`sea.getAssetAsBlob()`][],
239239
[`sea.getRawAsset()`][] and [`sea.getAssetKeys()`][] APIs for more information.
240240
241+
### Virtual File System (VFS) for assets
242+
243+
> Stability: 1 - Experimental
244+
245+
Instead of using the `node:sea` API to access individual assets, you can use
246+
the Virtual File System (VFS) to access bundled assets through standard `fs`
247+
APIs. The VFS automatically populates itself with all assets defined in the
248+
SEA configuration and mounts them at a virtual path (default: `/sea`).
249+
250+
To use the VFS with SEA:
251+
252+
```cjs
253+
const fs = require('node:fs');
254+
255+
// Check if SEA assets are available
256+
if (fs.hasSeaAssets()) {
257+
// Initialize and mount the SEA VFS
258+
const vfs = fs.getSeaVfs();
259+
260+
// Now you can use standard fs APIs to read bundled assets
261+
const config = JSON.parse(fs.readFileSync('/sea/config.json', 'utf8'));
262+
const data = fs.readFileSync('/sea/data/file.txt');
263+
264+
// Directory operations work too
265+
const files = fs.readdirSync('/sea/assets');
266+
267+
// Check if a bundled file exists
268+
if (fs.existsSync('/sea/optional.json')) {
269+
// ...
270+
}
271+
}
272+
```
273+
274+
The VFS supports the following `fs` operations on bundled assets:
275+
276+
* `readFileSync()` / `readFile()` / `promises.readFile()`
277+
* `statSync()` / `stat()` / `promises.stat()`
278+
* `lstatSync()` / `lstat()` / `promises.lstat()`
279+
* `readdirSync()` / `readdir()` / `promises.readdir()`
280+
* `existsSync()`
281+
* `realpathSync()` / `realpath()` / `promises.realpath()`
282+
* `accessSync()` / `access()` / `promises.access()`
283+
* `openSync()` / `open()` - for reading
284+
* `createReadStream()`
285+
286+
#### Loading modules from VFS in SEA
287+
288+
The default `require()` function in a SEA only supports loading Node.js
289+
built-in modules. To load JavaScript modules bundled as assets, you must use
290+
[`module.createRequire()`][]:
291+
292+
```cjs
293+
const fs = require('node:fs');
294+
const { createRequire } = require('node:module');
295+
296+
// Initialize VFS
297+
fs.getSeaVfs();
298+
299+
// Create a require function that works with VFS
300+
const seaRequire = createRequire('/sea/');
301+
302+
// Now you can require bundled modules
303+
const myModule = seaRequire('/sea/lib/mymodule.js');
304+
const utils = seaRequire('/sea/utils/helpers.js');
305+
```
306+
307+
This is necessary because SEA uses a special embedder require that doesn't go
308+
through the standard module resolution hooks that VFS registers.
309+
310+
#### Custom mount prefix
311+
312+
By default, the VFS is mounted at `/sea`. You can specify a custom prefix
313+
when initializing the VFS:
314+
315+
```cjs
316+
const vfs = fs.getSeaVfs({ prefix: '/app' });
317+
318+
// Assets are now accessible under /app
319+
const config = fs.readFileSync('/app/config.json', 'utf8');
320+
```
321+
322+
Note: `fs.getSeaVfs()` returns a singleton. The `prefix` option is only used
323+
on the first call; subsequent calls return the same cached instance.
324+
241325
### Startup snapshot support
242326
243327
The `useSnapshot` field can be used to enable startup snapshot support. In this

0 commit comments

Comments
 (0)