3
3
const {
4
4
ArrayPrototypePush,
5
5
ArrayPrototypePushApply,
6
- FunctionPrototypeCall,
7
6
Int32Array,
8
7
ObjectAssign,
9
8
ObjectDefineProperty,
10
9
ObjectSetPrototypeOf,
11
10
Promise,
12
11
SafeSet,
13
12
StringPrototypeSlice,
14
- StringPrototypeStartsWith,
15
13
StringPrototypeToUpperCase,
16
14
globalThis,
17
15
} = primordials ;
@@ -33,7 +31,6 @@ const {
33
31
ERR_INVALID_RETURN_VALUE ,
34
32
ERR_LOADER_CHAIN_INCOMPLETE ,
35
33
ERR_METHOD_NOT_IMPLEMENTED ,
36
- ERR_UNKNOWN_BUILTIN_MODULE ,
37
34
ERR_WORKER_UNSERIALIZABLE_ERROR ,
38
35
} = require ( 'internal/errors' ) . codes ;
39
36
const { exitCodes : { kUnfinishedTopLevelAwait } } = internalBinding ( 'errors' ) ;
@@ -49,7 +46,6 @@ const {
49
46
validateString,
50
47
} = require ( 'internal/validators' ) ;
51
48
const {
52
- emitExperimentalWarning,
53
49
kEmptyObject,
54
50
} = require ( 'internal/util' ) ;
55
51
@@ -73,8 +69,6 @@ let importMetaInitializer;
73
69
74
70
/**
75
71
* @typedef {object } ExportedHooks
76
- * @property {Function } initialize Customizations setup hook.
77
- * @property {Function } globalPreload Global preload hook.
78
72
* @property {Function } resolve Resolve hook.
79
73
* @property {Function } load Load hook.
80
74
*/
@@ -89,13 +83,6 @@ let importMetaInitializer;
89
83
90
84
class Hooks {
91
85
#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
-
99
86
/**
100
87
* Phase 1 of 2 in ESM loading.
101
88
* The output of the `resolve` chain of hooks is passed into the `load` chain of hooks.
@@ -146,7 +133,6 @@ class Hooks {
146
133
147
134
/**
148
135
* Collect custom/user-defined module loader hook(s).
149
- * After all hooks have been collected, the global preload hook(s) must be initialized.
150
136
* @param {string } url Custom loader specifier
151
137
* @param {Record<string, unknown> } exports
152
138
* @param {any } [data] Arbitrary data to be passed from the custom loader (user-land)
@@ -155,18 +141,11 @@ class Hooks {
155
141
*/
156
142
addCustomLoader ( url , exports , data ) {
157
143
const {
158
- globalPreload,
159
144
initialize,
160
145
resolve,
161
146
load,
162
147
} = pluckHooks ( exports ) ;
163
148
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
- }
170
149
if ( resolve ) {
171
150
const next = this . #chains. resolve [ this . #chains. resolve . length - 1 ] ;
172
151
ArrayPrototypePush ( this . #chains. resolve , { __proto__ : null , fn : resolve , url, next } ) ;
@@ -178,49 +157,6 @@ class Hooks {
178
157
return initialize ?. ( data ) ;
179
158
}
180
159
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
-
224
160
/**
225
161
* Resolve the location of the module.
226
162
*
@@ -559,8 +495,9 @@ class HooksProxy {
559
495
AtomicsWait ( this . #lock, WORKER_TO_MAIN_THREAD_NOTIFICATION , 0 ) ;
560
496
const response = this . #worker. receiveMessageSync ( ) ;
561
497
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 ) ;
564
501
}
565
502
566
503
this . #isReady = true ;
@@ -677,66 +614,12 @@ class HooksProxy {
677
614
importMetaInitialize ( meta , context , loader ) {
678
615
this . #importMetaInitializer( meta , context , loader ) ;
679
616
}
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
- }
737
617
}
738
618
ObjectSetPrototypeOf ( HooksProxy . prototype , null ) ;
739
619
620
+ // TODO(JakobJingleheimer): Remove this when loaders go "stable".
621
+ let globalPreloadWarningWasEmitted = false ;
622
+
740
623
/**
741
624
* A utility function to pluck the hooks from a user-defined loader.
742
625
* @param {import('./loader.js).ModuleExports } exports
@@ -750,9 +633,6 @@ function pluckHooks({
750
633
} ) {
751
634
const acceptedHooks = { __proto__ : null } ;
752
635
753
- if ( globalPreload ) {
754
- acceptedHooks . globalPreload = globalPreload ;
755
- }
756
636
if ( resolve ) {
757
637
acceptedHooks . resolve = resolve ;
758
638
}
@@ -762,6 +642,12 @@ function pluckHooks({
762
642
763
643
if ( initialize ) {
764
644
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 ;
765
651
}
766
652
767
653
return acceptedHooks ;
0 commit comments