mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 07:19:19 +00:00
test_runner: do not expose internal loader
PR-URL: https://github.com/nodejs/node/pull/54106 Fixes: https://github.com/nodejs/node/issues/54071 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
parent
1d35a066e7
commit
d0f5943363
@ -145,13 +145,15 @@ class Hooks {
|
|||||||
* @param {any} [data] Arbitrary data to be passed from the custom
|
* @param {any} [data] Arbitrary data to be passed from the custom
|
||||||
* loader (user-land) to the worker.
|
* loader (user-land) to the worker.
|
||||||
*/
|
*/
|
||||||
async register(urlOrSpecifier, parentURL, data) {
|
async register(urlOrSpecifier, parentURL, data, isInternal) {
|
||||||
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
|
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
|
||||||
const keyedExports = await cascadedLoader.import(
|
const keyedExports = isInternal ?
|
||||||
urlOrSpecifier,
|
require(urlOrSpecifier) :
|
||||||
parentURL,
|
await cascadedLoader.import(
|
||||||
kEmptyObject,
|
urlOrSpecifier,
|
||||||
);
|
parentURL,
|
||||||
|
kEmptyObject,
|
||||||
|
);
|
||||||
await this.addCustomLoader(urlOrSpecifier, keyedExports, data);
|
await this.addCustomLoader(urlOrSpecifier, keyedExports, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,7 +491,7 @@ class ModuleLoader {
|
|||||||
/**
|
/**
|
||||||
* @see {@link CustomizedModuleLoader.register}
|
* @see {@link CustomizedModuleLoader.register}
|
||||||
*/
|
*/
|
||||||
register(specifier, parentURL, data, transferList) {
|
register(specifier, parentURL, data, transferList, isInternal) {
|
||||||
if (!this.#customizations) {
|
if (!this.#customizations) {
|
||||||
// `CustomizedModuleLoader` is defined at the bottom of this file and
|
// `CustomizedModuleLoader` is defined at the bottom of this file and
|
||||||
// available well before this line is ever invoked. This is here in
|
// available well before this line is ever invoked. This is here in
|
||||||
@ -499,7 +499,7 @@ class ModuleLoader {
|
|||||||
// eslint-disable-next-line no-use-before-define
|
// eslint-disable-next-line no-use-before-define
|
||||||
this.setCustomizations(new CustomizedModuleLoader());
|
this.setCustomizations(new CustomizedModuleLoader());
|
||||||
}
|
}
|
||||||
return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList);
|
return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList, isInternal);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -636,10 +636,11 @@ class CustomizedModuleLoader {
|
|||||||
* @param {any} [data] Arbitrary data to be passed from the custom loader
|
* @param {any} [data] Arbitrary data to be passed from the custom loader
|
||||||
* (user-land) to the worker.
|
* (user-land) to the worker.
|
||||||
* @param {any[]} [transferList] Objects in `data` that are changing ownership
|
* @param {any[]} [transferList] Objects in `data` that are changing ownership
|
||||||
|
* @param {boolean} [isInternal] For internal loaders that should not be publicly exposed.
|
||||||
* @returns {{ format: string, url: URL['href'] }}
|
* @returns {{ format: string, url: URL['href'] }}
|
||||||
*/
|
*/
|
||||||
register(originalSpecifier, parentURL, data, transferList) {
|
register(originalSpecifier, parentURL, data, transferList, isInternal) {
|
||||||
return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data);
|
return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data, isInternal);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,12 +26,6 @@ const { createRequire, isBuiltin } = require('module');
|
|||||||
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
|
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
|
||||||
const { defaultResolve } = require('internal/modules/esm/resolve');
|
const { defaultResolve } = require('internal/modules/esm/resolve');
|
||||||
|
|
||||||
// TODO(cjihrig): This file should not be exposed publicly, but register() does
|
|
||||||
// not handle internal loaders. Before marking this API as stable, one of the
|
|
||||||
// following issues needs to be implemented:
|
|
||||||
// https://github.com/nodejs/node/issues/49473
|
|
||||||
// or https://github.com/nodejs/node/issues/52219
|
|
||||||
|
|
||||||
// TODO(cjihrig): The mocks need to be thread aware because the exports are
|
// TODO(cjihrig): The mocks need to be thread aware because the exports are
|
||||||
// evaluated on the thread that creates the mock. Before marking this API as
|
// evaluated on the thread that creates the mock. Before marking this API as
|
||||||
// stable, one of the following issues needs to be implemented:
|
// stable, one of the following issues needs to be implemented:
|
@ -53,9 +53,9 @@ const {
|
|||||||
} = require('internal/validators');
|
} = require('internal/validators');
|
||||||
const { MockTimers } = require('internal/test_runner/mock/mock_timers');
|
const { MockTimers } = require('internal/test_runner/mock/mock_timers');
|
||||||
const { strictEqual, notStrictEqual } = require('assert');
|
const { strictEqual, notStrictEqual } = require('assert');
|
||||||
const { isBuiltin, Module, register } = require('module');
|
const { Module } = require('internal/modules/cjs/loader');
|
||||||
const { MessageChannel } = require('worker_threads');
|
const { MessageChannel } = require('worker_threads');
|
||||||
const { _load, _nodeModulePaths, _resolveFilename } = Module;
|
const { _load, _nodeModulePaths, _resolveFilename, isBuiltin } = Module;
|
||||||
function kDefaultFunction() {}
|
function kDefaultFunction() {}
|
||||||
const enableModuleMocking = getOptionValue('--experimental-test-module-mocks');
|
const enableModuleMocking = getOptionValue('--experimental-test-module-mocks');
|
||||||
const kMockSearchParam = 'node-test-mock';
|
const kMockSearchParam = 'node-test-mock';
|
||||||
@ -650,19 +650,22 @@ function setupSharedModuleState() {
|
|||||||
const { mock } = require('test');
|
const { mock } = require('test');
|
||||||
const mockExports = new SafeMap();
|
const mockExports = new SafeMap();
|
||||||
const { port1, port2 } = new MessageChannel();
|
const { port1, port2 } = new MessageChannel();
|
||||||
|
const moduleLoader = esmLoader.getOrInitializeCascadedLoader();
|
||||||
|
|
||||||
register('node:test/mock_loader', {
|
moduleLoader.register(
|
||||||
__proto__: null,
|
'internal/test_runner/mock/loader',
|
||||||
data: { __proto__: null, port: port2 },
|
'node:',
|
||||||
transferList: [port2],
|
{ __proto__: null, port: port2 },
|
||||||
});
|
[port2],
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
sharedModuleState = {
|
sharedModuleState = {
|
||||||
__proto__: null,
|
__proto__: null,
|
||||||
loaderPort: port1,
|
loaderPort: port1,
|
||||||
mockExports,
|
mockExports,
|
||||||
mockMap: new SafeMap(),
|
mockMap: new SafeMap(),
|
||||||
moduleLoader: esmLoader.getOrInitializeCascadedLoader(),
|
moduleLoader,
|
||||||
};
|
};
|
||||||
mock._mockExports = mockExports;
|
mock._mockExports = mockExports;
|
||||||
Module._load = FunctionPrototypeBind(cjsMockModuleLoad, sharedModuleState);
|
Module._load = FunctionPrototypeBind(cjsMockModuleLoad, sharedModuleState);
|
||||||
|
Loading…
Reference in New Issue
Block a user