@@ -4,13 +4,13 @@ const { isSea, getAssetKeys } = internalBinding('sea');
44const { kEmptyObject, getLazy } = require ( 'internal/util' ) ;
55const {
66 codes : {
7- ERR_INVALID_ARG_VALUE ,
7+ ERR_INVALID_STATE ,
88 } ,
99} = require ( 'internal/errors' ) ;
1010
1111// Lazy-loaded VFS
1212let cachedSeaVfs = null ;
13- let cachedSeaVfsPrefix = null ;
13+ let initialized = false ;
1414
1515// Lazy-load VirtualFileSystem and SEAProvider to avoid loading VFS code if not needed
1616const lazyVirtualFileSystem = getLazy (
@@ -21,8 +21,9 @@ const lazySEAProvider = getLazy(
2121) ;
2222
2323/**
24- * Creates a VirtualFileSystem populated with SEA assets using the new Provider architecture.
24+ * Creates a VirtualFileSystem populated with SEA assets using the Provider architecture.
2525 * Assets are mounted at the specified prefix (default: '/sea').
26+ * This is an internal function - use initSeaVfs() or getSeaVfs() instead.
2627 * @param {object } [options] Configuration options
2728 * @param {string } [options.prefix] Mount point prefix for SEA assets
2829 * @param {boolean } [options.moduleHooks] Whether to enable require/import hooks
@@ -55,29 +56,39 @@ function createSeaVfs(options = kEmptyObject) {
5556}
5657
5758/**
58- * Gets or creates the default SEA VFS instance.
59- * This is a singleton that is lazily created on first access.
60- * @param {object } [options] Configuration options (only used on first call)
61- * @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
59+ * Initializes the SEA VFS with custom options.
60+ * Must be called before getSeaVfs() if custom options are needed.
61+ * @param {object } [options] Configuration options
62+ * @param {string } [options.prefix] Mount point prefix for SEA assets (default: '/sea')
63+ * @param {boolean } [options.moduleHooks] Whether to enable require/import hooks (default: true)
64+ * @returns {VirtualFileSystem|null } The VFS instance, or null if not running as SEA or no assets
65+ * @throws {ERR_INVALID_STATE } If already initialized
6366 */
64- function getSeaVfs ( options ) {
65- const prefix = options ?. prefix ?? '/sea' ;
67+ function initSeaVfs ( options = kEmptyObject ) {
68+ if ( initialized ) {
69+ throw new ERR_INVALID_STATE ( 'SEA VFS is already initialized' ) ;
70+ }
6671
67- if ( cachedSeaVfs === null ) {
68- 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- ) ;
72+ initialized = true ;
73+ cachedSeaVfs = createSeaVfs ( options ) ;
74+ return cachedSeaVfs ;
75+ }
76+
77+ /**
78+ * Gets the SEA VFS instance.
79+ * If not already initialized, auto-initializes with default options.
80+ * @returns {VirtualFileSystem|null } The VFS instance, or null if not running as SEA or no assets
81+ */
82+ function getSeaVfs ( ) {
83+ if ( ! initialized ) {
84+ initialized = true ;
85+ cachedSeaVfs = createSeaVfs ( ) ;
7686 }
7787 return cachedSeaVfs ;
7888}
7989
8090module . exports = {
8191 createSeaVfs,
92+ initSeaVfs,
8293 getSeaVfs,
8394} ;
0 commit comments