Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
working mock test#39240
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
Uh oh!
There was an error while loading. Please reload this page.
working mock test #39240
Changes from all commits
13ab19effdaa30f5e96f3f923cce0e1e1244e4ed138369c4a57ace817416b27d0276c60e11bde172c038b9e13805ad4c1bd61b7d493ba88a1c5996db9f339f05e71c9File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 'use strict'; | ||
bmeck marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const { getOptionValue } = require('internal/options'); | ||
| const experimentalImportMetaResolve = | ||
| getOptionValue('--experimental-import-meta-resolve'); | ||
| const { PromisePrototypeThen, PromiseReject } = primordials; | ||
| const asyncESM = require('internal/process/esm_loader'); | ||
| function createImportMetaResolve(defaultParentUrl) { | ||
| return async function resolve(specifier, parentUrl = defaultParentUrl) { | ||
| return PromisePrototypeThen( | ||
| asyncESM.esmLoader.resolve(specifier, parentUrl), | ||
bmeck marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ({ url }) => url, | ||
| (error) => ( | ||
| error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ? | ||
| error.url : PromiseReject(error)) | ||
| ); | ||
| }; | ||
| } | ||
| function initializeImportMeta(meta, context) { | ||
| const url = context.url; | ||
| // Alphabetical | ||
| if (experimentalImportMetaResolve) | ||
| meta.resolve = createImportMetaResolve(url); | ||
bmeck marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| meta.url = url; | ||
| } | ||
| module.exports = { | ||
| initializeImportMeta | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,6 +18,7 @@ const { | ||
| SafeWeakMap, | ||
| globalThis, | ||
| } = primordials; | ||
| const { MessageChannel } = require('internal/worker/io'); | ||
| const { | ||
| ERR_INVALID_ARG_TYPE, | ||
| @@ -39,6 +40,9 @@ const { | ||
| defaultResolve, | ||
| DEFAULT_CONDITIONS, | ||
| } = require('internal/modules/esm/resolve'); | ||
| const { | ||
| initializeImportMeta | ||
| } = require('internal/modules/esm/initialize_import_meta'); | ||
| const { defaultLoad } = require('internal/modules/esm/load'); | ||
| const { translators } = require( | ||
| 'internal/modules/esm/translators'); | ||
| @@ -76,6 +80,8 @@ class ESMLoader { | ||
| defaultResolve, | ||
| ]; | ||
| #importMetaInitializer = initializeImportMeta; | ||
| /** | ||
| * Map of already-loaded CJS modules to use | ||
| */ | ||
| @@ -359,7 +365,18 @@ class ESMLoader { | ||
| if (!count) return; | ||
| for (let i = 0; i < count; i++) { | ||
| const preload = this.#globalPreloaders[i](); | ||
| const channel = new MessageChannel(); | ||
| const { | ||
| port1: insidePreload, | ||
| port2: insideLoader, | ||
| } = channel; | ||
| insidePreload.unref(); | ||
| insideLoader.unref(); | ||
| const preload = this.#globalPreloaders[i]({ | ||
| port: insideLoader | ||
| }); | ||
| if (preload == null) return; | ||
| @@ -373,22 +390,60 @@ class ESMLoader { | ||
| const { compileFunction } = require('vm'); | ||
| const preloadInit = compileFunction( | ||
| preload, | ||
| ['getBuiltin'], | ||
| ['getBuiltin', 'port', 'setImportMetaCallback'], | ||
| { | ||
| filename: '<preload>', | ||
| } | ||
| ); | ||
| const { NativeModule } = require('internal/bootstrap/loaders'); | ||
| FunctionPrototypeCall(preloadInit, globalThis, (builtinName) => { | ||
| if (NativeModule.canBeRequiredByUsers(builtinName)) { | ||
| return require(builtinName); | ||
| // We only allow replacing the importMetaInitializer during preload, | ||
| // after preload is finished, we disable the ability to replace it | ||
| // | ||
| // This exposes accidentally setting the initializer too late by | ||
| // throwing an error. | ||
| let finished = false; | ||
| let replacedImportMetaInitializer = false; | ||
| let next = this.#importMetaInitializer; | ||
| try { | ||
| // 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 (NativeModule.canBeRequiredByUsers(builtinName)) { | ||
| return require(builtinName); | ||
| } | ||
| throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName); | ||
| }, | ||
| // Param port | ||
| insidePreload, | ||
| // Param 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; | ||
GeoffreyBooth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| importMetaInitialize(meta, context) { | ||
| this.#importMetaInitializer(meta, context); | ||
| } | ||
GeoffreyBooth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Resolve the location of the module. | ||
| * | ||
| 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'; | ||
bmeck marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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' | ||
| })); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.