Skip to content

Commit 21654a0

Browse files
committed
vfs: address review feedback for SEA docs and getSeaVfs
- Separate readFileSync calls in SEA VFS example for clarity - Make getSeaVfs() throw ERR_INVALID_ARG_VALUE if called with a different prefix than the first call, preventing subtle bugs PR-URL: #61478
1 parent 36ec459 commit 21654a0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

doc/api/single-executable-applications.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ are accessible through this virtual path.
188188
const fs = require('node:fs');
189189

190190
// Assets are automatically available at /sea when running as SEA
191-
const config = JSON.parse(fs.readFileSync('/sea/config.json', 'utf8'));
191+
const rawConfig = fs.readFileSync('/sea/config.json', 'utf8');
192192
const data = fs.readFileSync('/sea/data/file.txt');
193193

194+
const config = JSON.parse(rawConfig);
195+
194196
// Directory operations work too
195197
const files = fs.readdirSync('/sea/assets');
196198

lib/internal/vfs/sea.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
const { isSea, getAssetKeys } = internalBinding('sea');
44
const { kEmptyObject, getLazy } = require('internal/util');
5+
const {
6+
codes: {
7+
ERR_INVALID_ARG_VALUE,
8+
},
9+
} = require('internal/errors');
510

611
// Lazy-loaded VFS
712
let cachedSeaVfs = null;
13+
let cachedSeaVfsPrefix = null;
814

915
// Lazy-load VirtualFileSystem and SEAProvider to avoid loading VFS code if not needed
1016
const lazyVirtualFileSystem = getLazy(
@@ -53,10 +59,20 @@ function createSeaVfs(options = kEmptyObject) {
5359
* This is a singleton that is lazily created on first access.
5460
* @param {object} [options] Configuration options (only used on first call)
5561
* @returns {VirtualFileSystem|null} The VFS instance, or null if not running as SEA
62+
* @throws {ERR_INVALID_ARG_VALUE} If called with a different prefix than the first call
5663
*/
5764
function getSeaVfs(options) {
65+
const prefix = options?.prefix ?? '/sea';
66+
5867
if (cachedSeaVfs === null) {
5968
cachedSeaVfs = createSeaVfs(options);
69+
cachedSeaVfsPrefix = prefix;
70+
} else if (prefix !== cachedSeaVfsPrefix) {
71+
throw new ERR_INVALID_ARG_VALUE(
72+
'options.prefix',
73+
prefix,
74+
`SEA VFS is already initialized with prefix '${cachedSeaVfsPrefix}'`,
75+
);
6076
}
6177
return cachedSeaVfs;
6278
}

0 commit comments

Comments
 (0)