Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
esm: add back globalPreload tests#48779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+475 −31
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,6 +10,7 @@ const{ | ||
| Promise, | ||
| SafeSet, | ||
| StringPrototypeSlice, | ||
| StringPrototypeStartsWith, | ||
| StringPrototypeToUpperCase, | ||
| globalThis, | ||
| } = primordials; | ||
| @@ -30,6 +31,7 @@ const{ | ||
| ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
| ERR_INVALID_RETURN_VALUE, | ||
| ERR_LOADER_CHAIN_INCOMPLETE, | ||
| ERR_UNKNOWN_BUILTIN_MODULE, | ||
| ERR_WORKER_UNSERIALIZABLE_ERROR, | ||
| } = require('internal/errors').codes; | ||
| const{exitCodes:{kUnfinishedTopLevelAwait } } = internalBinding('errors'); | ||
| @@ -521,14 +523,14 @@ class HooksProxy{ | ||
| this.#worker.on('exit', process.exit); | ||
| } | ||
| #waitForWorker(){ | ||
| waitForWorker(){ | ||
| if (!this.#isReady){ | ||
| const{kIsOnline } = require('internal/worker'); | ||
| if (!this.#worker[kIsOnline]){ | ||
| debug('wait for signal from worker'); | ||
| AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 0); | ||
| const response = this.#worker.receiveMessageSync(); | ||
| if (response.message.status === 'exit'){return} | ||
| if (response == null || response.message.status === 'exit'){return} | ||
| const{preloadScripts } = this.#unwrapMessage(response); | ||
| this.#executePreloadScripts(preloadScripts); | ||
| } | ||
| @@ -538,7 +540,7 @@ class HooksProxy{ | ||
| } | ||
| async makeAsyncRequest(method, ...args){ | ||
| this.#waitForWorker(); | ||
| this.waitForWorker(); | ||
| MessageChannel ??= require('internal/worker/io').MessageChannel; | ||
| const asyncCommChannel = new MessageChannel(); | ||
| @@ -578,7 +580,7 @@ class HooksProxy{ | ||
| } | ||
| makeSyncRequest(method, ...args){ | ||
| this.#waitForWorker(); | ||
| this.waitForWorker(); | ||
| // Pass work to the worker. | ||
| debug('post sync message to worker',{method, args }); | ||
| @@ -620,35 +622,66 @@ class HooksProxy{ | ||
| } | ||
| } | ||
| #importMetaInitializer = require('internal/modules/esm/initialize_import_meta').initializeImportMeta; | ||
| importMetaInitialize(meta, context, loader){ | ||
| this.#importMetaInitializer(meta, context, loader); | ||
| } | ||
| #executePreloadScripts(preloadScripts){ | ||
| for (let i = 0; i < preloadScripts.length; i++){ | ||
| const{code, port } = preloadScripts[i]; | ||
| const{compileFunction } = require('vm'); | ||
| const preloadInit = compileFunction( | ||
| code, | ||
| ['getBuiltin', 'port'], | ||
| ['getBuiltin', 'port', 'setImportMetaCallback'], | ||
| { | ||
| filename: '<preload>', | ||
| }, | ||
| ); | ||
| let finished = false; | ||
| let replacedImportMetaInitializer = false; | ||
| let next = this.#importMetaInitializer; | ||
| const{BuiltinModule } = require('internal/bootstrap/realm'); | ||
| // Calls the compiled preload source text gotten from the hook | ||
| // Since the parameters are named we use positional parameters | ||
| // see compileFunction above to cross reference the names | ||
| FunctionPrototypeCall( | ||
| preloadInit, | ||
| globalThis, | ||
| // Param getBuiltin | ||
| (builtinName) =>{ | ||
| if (BuiltinModule.canBeRequiredByUsers(builtinName) && | ||
| BuiltinModule.canBeRequiredWithoutScheme(builtinName)){ | ||
| return require(builtinName); | ||
| } | ||
| throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName); | ||
| }, | ||
| // Param port | ||
| port, | ||
| ); | ||
| try{ | ||
| FunctionPrototypeCall( | ||
| preloadInit, | ||
| globalThis, | ||
| // Param getBuiltin | ||
| (builtinName) =>{ | ||
| if (StringPrototypeStartsWith(builtinName, 'node:')){ | ||
| builtinName = StringPrototypeSlice(builtinName, 5); | ||
| } else if (!BuiltinModule.canBeRequiredWithoutScheme(builtinName)){ | ||
aduh95 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| throw new ERR_UNKNOWN_BUILTIN_MODULE(builtinName); | ||
| } | ||
| if (BuiltinModule.canBeRequiredByUsers(builtinName)){ | ||
| return require(builtinName); | ||
| } | ||
| throw new ERR_UNKNOWN_BUILTIN_MODULE(builtinName); | ||
| }, | ||
| // Param port | ||
| port, | ||
| // setImportMetaCallback | ||
| (fn) =>{ | ||
| if (finished || typeof fn !== 'function'){ | ||
| throw new ERR_INVALID_ARG_TYPE('fn', fn); | ||
| } | ||
| replacedImportMetaInitializer = true; | ||
| const parent = next; | ||
| next = (meta, context) =>{ | ||
| return fn(meta, context, parent); | ||
| }; | ||
| }, | ||
| ); | ||
| } finally{ | ||
| finished = true; | ||
| if (replacedImportMetaInitializer){ | ||
| this.#importMetaInitializer = next; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Flags: --loader ./test/fixtures/es-module-loaders/mock-loader.mjs | ||
| import '../common/index.mjs' | ||
| import assert from 'assert/strict' | ||
| // This is provided by test/fixtures/es-module-loaders/mock-loader.mjs | ||
| import mock from 'node:mock' | ||
| mock('node:events',{ | ||
| EventEmitter: 'This is mocked!' | ||
| }); | ||
| // This resolves to node:events | ||
| // It is intercepted by mock-loader and doesn't return the normal value | ||
| assert.deepStrictEqual(await import('events'), Object.defineProperty({ | ||
| __proto__: null, | ||
| EventEmitter: 'This is mocked!' | ||
| }, Symbol.toStringTag,{ | ||
| enumerable: false, | ||
| value: 'Module' | ||
| })); | ||
| const mutator = mock('node:events',{ | ||
| EventEmitter: 'This is mocked v2!' | ||
| }); | ||
| // It is intercepted by mock-loader and doesn't return the normal value. | ||
| // This is resolved separately from the import above since the specifiers | ||
| // are different. | ||
| const mockedV2 = await import('node:events'); | ||
| assert.deepStrictEqual(mockedV2, Object.defineProperty({ | ||
| __proto__: null, | ||
| EventEmitter: 'This is mocked v2!' | ||
| }, Symbol.toStringTag,{ | ||
| enumerable: false, | ||
| value: 'Module' | ||
| })); | ||
| mutator.EventEmitter = 'This is mocked v3!' | ||
| assert.deepStrictEqual(mockedV2, Object.defineProperty({ | ||
| __proto__: null, | ||
| EventEmitter: 'This is mocked v3!' | ||
| }, Symbol.toStringTag,{ | ||
| enumerable: false, | ||
| value: 'Module' | ||
| })); |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.