33const {
44 ArrayPrototypePush,
55 ArrayPrototypePushApply,
6- FunctionPrototypeCall,
76 Int32Array,
87 ObjectAssign,
98 ObjectDefineProperty,
109 ObjectSetPrototypeOf,
1110 Promise,
1211 SafeSet,
1312 StringPrototypeSlice,
14- StringPrototypeStartsWith,
1513 StringPrototypeToUpperCase,
1614 globalThis,
1715} = primordials ;
@@ -33,7 +31,6 @@ const {
3331 ERR_INVALID_RETURN_VALUE ,
3432 ERR_LOADER_CHAIN_INCOMPLETE ,
3533 ERR_METHOD_NOT_IMPLEMENTED ,
36- ERR_UNKNOWN_BUILTIN_MODULE ,
3734 ERR_WORKER_UNSERIALIZABLE_ERROR ,
3835} = require ( 'internal/errors' ) . codes ;
3936const { exitCodes : { kUnfinishedTopLevelAwait } } = internalBinding ( 'errors' ) ;
@@ -49,7 +46,6 @@ const {
4946 validateString,
5047} = require ( 'internal/validators' ) ;
5148const {
52- emitExperimentalWarning,
5349 kEmptyObject,
5450} = require ( 'internal/util' ) ;
5551
@@ -73,8 +69,6 @@ let importMetaInitializer;
7369
7470/**
7571 * @typedef {object } ExportedHooks
76- * @property {Function } initialize Customizations setup hook.
77- * @property {Function } globalPreload Global preload hook.
7872 * @property {Function } resolve Resolve hook.
7973 * @property {Function } load Load hook.
8074 */
@@ -89,13 +83,6 @@ let importMetaInitializer;
8983
9084class Hooks {
9185 #chains = {
92- /**
93- * Prior to ESM loading. These are called once before any modules are started.
94- * @private
95- * @property {KeyedHook[] } globalPreload Last-in-first-out list of preload hooks.
96- */
97- globalPreload : [ ] ,
98-
9986 /**
10087 * Phase 1 of 2 in ESM loading.
10188 * The output of the `resolve` chain of hooks is passed into the `load` chain of hooks.
@@ -146,7 +133,6 @@ class Hooks {
146133
147134 /**
148135 * Collect custom/user-defined module loader hook(s).
149- * After all hooks have been collected, the global preload hook(s) must be initialized.
150136 * @param {string } url Custom loader specifier
151137 * @param {Record<string, unknown> } exports
152138 * @param {any } [data] Arbitrary data to be passed from the custom loader (user-land)
@@ -155,18 +141,11 @@ class Hooks {
155141 */
156142 addCustomLoader ( url , exports , data ) {
157143 const {
158- globalPreload,
159144 initialize,
160145 resolve,
161146 load,
162147 } = pluckHooks ( exports ) ;
163148
164- if ( globalPreload && ! initialize ) {
165- emitExperimentalWarning (
166- '`globalPreload` is planned for removal in favor of `initialize`. `globalPreload`' ,
167- ) ;
168- ArrayPrototypePush ( this . #chains. globalPreload , { __proto__ : null , fn : globalPreload , url } ) ;
169- }
170149 if ( resolve ) {
171150 const next = this . #chains. resolve [ this . #chains. resolve . length - 1 ] ;
172151 ArrayPrototypePush ( this . #chains. resolve , { __proto__ : null , fn : resolve , url, next } ) ;
@@ -178,49 +157,6 @@ class Hooks {
178157 return initialize ?. ( data ) ;
179158 }
180159
181- /**
182- * Initialize `globalPreload` hooks.
183- */
184- initializeGlobalPreload ( ) {
185- const preloadScripts = [ ] ;
186- for ( let i = this . #chains. globalPreload . length - 1 ; i >= 0 ; i -- ) {
187- const { MessageChannel } = require ( 'internal/worker/io' ) ;
188- const channel = new MessageChannel ( ) ;
189- const {
190- port1 : insidePreload ,
191- port2 : insideLoader ,
192- } = channel ;
193-
194- insidePreload . unref ( ) ;
195- insideLoader . unref ( ) ;
196-
197- const {
198- fn : preload ,
199- url : specifier ,
200- } = this . #chains. globalPreload [ i ] ;
201-
202- const preloaded = preload ( {
203- port : insideLoader ,
204- } ) ;
205-
206- if ( preloaded == null ) { continue ; }
207-
208- if ( typeof preloaded !== 'string' ) { // [2]
209- throw new ERR_INVALID_RETURN_VALUE (
210- 'a string' ,
211- `${ specifier } globalPreload` ,
212- preload ,
213- ) ;
214- }
215-
216- ArrayPrototypePush ( preloadScripts , {
217- code : preloaded ,
218- port : insidePreload ,
219- } ) ;
220- }
221- return preloadScripts ;
222- }
223-
224160 /**
225161 * Resolve the location of the module.
226162 *
@@ -559,8 +495,9 @@ class HooksProxy {
559495 AtomicsWait ( this . #lock, WORKER_TO_MAIN_THREAD_NOTIFICATION , 0 ) ;
560496 const response = this . #worker. receiveMessageSync ( ) ;
561497 if ( response == null || response . message . status === 'exit' ) { return ; }
562- const { preloadScripts } = this . #unwrapMessage( response ) ;
563- this . #executePreloadScripts( preloadScripts ) ;
498+
499+ // ! This line catches initialization errors in the worker thread.
500+ this . #unwrapMessage( response ) ;
564501 }
565502
566503 this . #isReady = true ;
@@ -677,66 +614,12 @@ class HooksProxy {
677614 importMetaInitialize ( meta , context , loader ) {
678615 this . #importMetaInitializer( meta , context , loader ) ;
679616 }
680-
681- #executePreloadScripts( preloadScripts ) {
682- for ( let i = 0 ; i < preloadScripts . length ; i ++ ) {
683- const { code, port } = preloadScripts [ i ] ;
684- const { compileFunction } = require ( 'vm' ) ;
685- const preloadInit = compileFunction (
686- code ,
687- [ 'getBuiltin' , 'port' , 'setImportMetaCallback' ] ,
688- {
689- filename : '<preload>' ,
690- } ,
691- ) ;
692- let finished = false ;
693- let replacedImportMetaInitializer = false ;
694- let next = this . #importMetaInitializer;
695- const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
696- // Calls the compiled preload source text gotten from the hook
697- // Since the parameters are named we use positional parameters
698- // see compileFunction above to cross reference the names
699- try {
700- FunctionPrototypeCall (
701- preloadInit ,
702- globalThis ,
703- // Param getBuiltin
704- ( builtinName ) => {
705- if ( StringPrototypeStartsWith ( builtinName , 'node:' ) ) {
706- builtinName = StringPrototypeSlice ( builtinName , 5 ) ;
707- } else if ( ! BuiltinModule . canBeRequiredWithoutScheme ( builtinName ) ) {
708- throw new ERR_UNKNOWN_BUILTIN_MODULE ( builtinName ) ;
709- }
710- if ( BuiltinModule . canBeRequiredByUsers ( builtinName ) ) {
711- return require ( builtinName ) ;
712- }
713- throw new ERR_UNKNOWN_BUILTIN_MODULE ( builtinName ) ;
714- } ,
715- // Param port
716- port ,
717- // setImportMetaCallback
718- ( fn ) => {
719- if ( finished || typeof fn !== 'function' ) {
720- throw new ERR_INVALID_ARG_TYPE ( 'fn' , fn ) ;
721- }
722- replacedImportMetaInitializer = true ;
723- const parent = next ;
724- next = ( meta , context ) => {
725- return fn ( meta , context , parent ) ;
726- } ;
727- } ,
728- ) ;
729- } finally {
730- finished = true ;
731- if ( replacedImportMetaInitializer ) {
732- this . #importMetaInitializer = next ;
733- }
734- }
735- }
736- }
737617}
738618ObjectSetPrototypeOf ( HooksProxy . prototype , null ) ;
739619
620+ // TODO(JakobJingleheimer): Remove this when loaders go "stable".
621+ let globalPreloadWarningWasEmitted = false ;
622+
740623/**
741624 * A utility function to pluck the hooks from a user-defined loader.
742625 * @param {import('./loader.js).ModuleExports } exports
@@ -750,9 +633,6 @@ function pluckHooks({
750633} ) {
751634 const acceptedHooks = { __proto__ : null } ;
752635
753- if ( globalPreload ) {
754- acceptedHooks . globalPreload = globalPreload ;
755- }
756636 if ( resolve ) {
757637 acceptedHooks . resolve = resolve ;
758638 }
@@ -762,6 +642,12 @@ function pluckHooks({
762642
763643 if ( initialize ) {
764644 acceptedHooks . initialize = initialize ;
645+ } else if ( globalPreload && ! globalPreloadWarningWasEmitted ) {
646+ process . emitWarning (
647+ '`globalPreload` has been removed; use `initialize` instead.' ,
648+ 'UnsupportedWarning' ,
649+ ) ;
650+ globalPreloadWarningWasEmitted = true ;
765651 }
766652
767653 return acceptedHooks ;
0 commit comments