GeezerVison is showing github.com — test_runner: do not expose internal loader · nodejs/node@27da75a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/hooks.js +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -160,13 +160,15 @@ class Hooks { | 160 | 160 | * @param {any} [data] Arbitrary data to be passed from the custom | 161 | 161 | * loader (user-land) to the worker. | 162 | 162 | */ | 163 | | - async register(urlOrSpecifier, parentURL, data) { | | 163 | + async register(urlOrSpecifier, parentURL, data, isInternal) { | 164 | 164 | const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | 165 | | - const keyedExports = await cascadedLoader.import( | 166 | | - urlOrSpecifier, | 167 | | - parentURL, | 168 | | - kEmptyObject, | 169 | | - ); | | 165 | + const keyedExports = isInternal ? | | 166 | + require(urlOrSpecifier) : | | 167 | + await cascadedLoader.import( | | 168 | + urlOrSpecifier, | | 169 | + parentURL, | | 170 | + kEmptyObject, | | 171 | + ); | 170 | 172 | await this.addCustomLoader(urlOrSpecifier, keyedExports, data); | 171 | 173 | } | 172 | 174 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -477,15 +477,15 @@ class ModuleLoader { | 477 | 477 | /** | 478 | 478 | * @see {@link CustomizedModuleLoader.register} | 479 | 479 | */ | 480 | | - register(specifier, parentURL, data, transferList) { | | 480 | + register(specifier, parentURL, data, transferList, isInternal) { | 481 | 481 | if (!this.#customizations) { | 482 | 482 | // `CustomizedModuleLoader` is defined at the bottom of this file and | 483 | 483 | // available well before this line is ever invoked. This is here in | 484 | 484 | // order to preserve the git diff instead of moving the class. | 485 | 485 | // eslint-disable-next-line no-use-before-define | 486 | 486 | this.setCustomizations(new CustomizedModuleLoader()); | 487 | 487 | } | 488 | | - return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList); | | 488 | + return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList, isInternal); | 489 | 489 | } | 490 | 490 |
| 491 | 491 | /** | @@ -617,10 +617,11 @@ class CustomizedModuleLoader { | 617 | 617 | * @param {any} [data] Arbitrary data to be passed from the custom loader | 618 | 618 | * (user-land) to the worker. | 619 | 619 | * @param {any[]} [transferList] Objects in `data` that are changing ownership | | 620 | + * @param {boolean} [isInternal] For internal loaders that should not be publicly exposed. | 620 | 621 | * @returns {{ format: string, url: URL['href'] }} | 621 | 622 | */ | 622 | | - register(originalSpecifier, parentURL, data, transferList) { | 623 | | - return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data); | | 623 | + register(originalSpecifier, parentURL, data, transferList, isInternal) { | | 624 | + return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data, isInternal); | 624 | 625 | } | 625 | 626 |
| 626 | 627 | /** | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/loader.js -6Lines changed: 0 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,12 +26,6 @@ const { createRequire, isBuiltin } = require('module'); | 26 | 26 | const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format'); | 27 | 27 | const { defaultResolve } = require('internal/modules/esm/resolve'); | 28 | 28 |
| 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 | | - | 35 | 29 | // TODO(cjihrig): The mocks need to be thread aware because the exports are | 36 | 30 | // evaluated on the thread that creates the mock. Before marking this API as | 37 | 31 | // stable, one of the following issues needs to be implemented: | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock.js +12-9Lines changed: 12 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,9 +53,9 @@ const { | 53 | 53 | } = require('internal/validators'); | 54 | 54 | const { MockTimers } = require('internal/test_runner/mock/mock_timers'); | 55 | 55 | const { strictEqual, notStrictEqual } = require('assert'); | 56 | | -const { isBuiltin, Module, register } = require('module'); | | 56 | +const { Module } = require('internal/modules/cjs/loader'); | 57 | 57 | const { MessageChannel } = require('worker_threads'); | 58 | | -const { _load, _nodeModulePaths, _resolveFilename } = Module; | | 58 | +const { _load, _nodeModulePaths, _resolveFilename, isBuiltin } = Module; | 59 | 59 | function kDefaultFunction() {} | 60 | 60 | const enableModuleMocking = getOptionValue('--experimental-test-module-mocks'); | 61 | 61 | const kMockSearchParam = 'node-test-mock'; | @@ -650,19 +650,22 @@ function setupSharedModuleState() { | 650 | 650 | const { mock } = require('test'); | 651 | 651 | const mockExports = new SafeMap(); | 652 | 652 | const { port1, port2 } = new MessageChannel(); | 653 | | - | 654 | | - register('node:test/mock_loader', { | 655 | | - __proto__: null, | 656 | | - data: { __proto__: null, port: port2 }, | 657 | | - transferList: [port2], | 658 | | - }); | | 653 | + const moduleLoader = esmLoader.getOrInitializeCascadedLoader(); | | 654 | + | | 655 | + moduleLoader.register( | | 656 | + 'internal/test_runner/mock/loader', | | 657 | + 'node:', | | 658 | + { __proto__: null, port: port2 }, | | 659 | + [port2], | | 660 | + true, | | 661 | + ); | 659 | 662 |
| 660 | 663 | sharedModuleState = { | 661 | 664 | __proto__: null, | 662 | 665 | loaderPort: port1, | 663 | 666 | mockExports, | 664 | 667 | mockMap: new SafeMap(), | 665 | | - moduleLoader: esmLoader.getOrInitializeCascadedLoader(), | | 668 | + moduleLoader, | 666 | 669 | }; | 667 | 670 | mock._mockExports = mockExports; | 668 | 671 | Module._load = FunctionPrototypeBind(cjsMockModuleLoad, sharedModuleState); | |
|