Skip to content

Commit 27da75a

Browse files
aduh95targos
authored andcommitted
test_runner: do not expose internal loader
PR-URL: #54106Fixes: #54071 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent a7fdc60 commit 27da75a

4 files changed

Lines changed: 25 additions & 25 deletions

File tree

‎lib/internal/modules/esm/hooks.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ class Hooks {
160160
* @param {any} [data] Arbitrary data to be passed from the custom
161161
* loader (user-land) to the worker.
162162
*/
163-
asyncregister(urlOrSpecifier,parentURL,data){
163+
asyncregister(urlOrSpecifier,parentURL,data,isInternal){
164164
constcascadedLoader=require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
165-
constkeyedExports=awaitcascadedLoader.import(
166-
urlOrSpecifier,
167-
parentURL,
168-
kEmptyObject,
169-
);
165+
constkeyedExports=isInternal ?
166+
require(urlOrSpecifier) :
167+
awaitcascadedLoader.import(
168+
urlOrSpecifier,
169+
parentURL,
170+
kEmptyObject,
171+
);
170172
awaitthis.addCustomLoader(urlOrSpecifier,keyedExports,data);
171173
}
172174

‎lib/internal/modules/esm/loader.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,15 @@ class ModuleLoader {
477477
/**
478478
* @see {@link CustomizedModuleLoader.register}
479479
*/
480-
register(specifier,parentURL,data,transferList){
480+
register(specifier,parentURL,data,transferList,isInternal){
481481
if(!this.#customizations){
482482
// `CustomizedModuleLoader` is defined at the bottom of this file and
483483
// available well before this line is ever invoked. This is here in
484484
// order to preserve the git diff instead of moving the class.
485485
// eslint-disable-next-line no-use-before-define
486486
this.setCustomizations(newCustomizedModuleLoader());
487487
}
488-
returnthis.#customizations.register(`${specifier}`,`${parentURL}`,data,transferList);
488+
returnthis.#customizations.register(`${specifier}`,`${parentURL}`,data,transferList,isInternal);
489489
}
490490

491491
/**
@@ -617,10 +617,11 @@ class CustomizedModuleLoader {
617617
* @param {any} [data] Arbitrary data to be passed from the custom loader
618618
* (user-land) to the worker.
619619
* @param {any[]} [transferList] Objects in `data` that are changing ownership
620+
* @param {boolean} [isInternal] For internal loaders that should not be publicly exposed.
620621
* @returns {{ format: string, url: URL['href'] }}
621622
*/
622-
register(originalSpecifier,parentURL,data,transferList){
623-
returnhooksProxy.makeSyncRequest('register',transferList,originalSpecifier,parentURL,data);
623+
register(originalSpecifier,parentURL,data,transferList,isInternal){
624+
returnhooksProxy.makeSyncRequest('register',transferList,originalSpecifier,parentURL,data,isInternal);
624625
}
625626

626627
/**
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ const { createRequire, isBuiltin } = require('module');
2626
const{ defaultGetFormatWithoutErrors }=require('internal/modules/esm/get_format');
2727
const{ defaultResolve }=require('internal/modules/esm/resolve');
2828

29-
// TODO(cjihrig): This file should not be exposed publicly, but register() does
30-
// not handle internal loaders. Before marking this API as stable, one of the
31-
// following issues needs to be implemented:
32-
// https://github.com/nodejs/node/issues/49473
33-
// or https://github.com/nodejs/node/issues/52219
34-
3529
// TODO(cjihrig): The mocks need to be thread aware because the exports are
3630
// evaluated on the thread that creates the mock. Before marking this API as
3731
// stable, one of the following issues needs to be implemented:

‎lib/internal/test_runner/mock/mock.js‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ const {
5353
}=require('internal/validators');
5454
const{ MockTimers }=require('internal/test_runner/mock/mock_timers');
5555
const{ strictEqual, notStrictEqual }=require('assert');
56-
const{isBuiltin,Module, register }=require('module');
56+
const{ Module}=require('internal/modules/cjs/loader');
5757
const{ MessageChannel }=require('worker_threads');
58-
const{ _load, _nodeModulePaths, _resolveFilename }=Module;
58+
const{ _load, _nodeModulePaths, _resolveFilename, isBuiltin}=Module;
5959
functionkDefaultFunction(){}
6060
constenableModuleMocking=getOptionValue('--experimental-test-module-mocks');
6161
constkMockSearchParam='node-test-mock';
@@ -650,19 +650,22 @@ function setupSharedModuleState() {
650650
const{ mock }=require('test');
651651
constmockExports=newSafeMap();
652652
const{ port1, port2 }=newMessageChannel();
653-
654-
register('node:test/mock_loader',{
655-
__proto__: null,
656-
data: {__proto__: null,port: port2},
657-
transferList: [port2],
658-
});
653+
constmoduleLoader=esmLoader.getOrInitializeCascadedLoader();
654+
655+
moduleLoader.register(
656+
'internal/test_runner/mock/loader',
657+
'node:',
658+
{__proto__: null,port: port2},
659+
[port2],
660+
true,
661+
);
659662

660663
sharedModuleState={
661664
__proto__: null,
662665
loaderPort: port1,
663666
mockExports,
664667
mockMap: newSafeMap(),
665-
moduleLoader: esmLoader.getOrInitializeCascadedLoader(),
668+
moduleLoader,
666669
};
667670
mock._mockExports=mockExports;
668671
Module._load=FunctionPrototypeBind(cjsMockModuleLoad,sharedModuleState);

0 commit comments

Comments
 (0)