GeezerVison is showing github.com — test_runner: do not expose internal loader · nodejs/node@fe793a6
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 |
|---|
@@ -145,13 +145,15 @@ class Hooks { | 145 | 145 | * @param {any} [data] Arbitrary data to be passed from the custom | 146 | 146 | * loader (user-land) to the worker. | 147 | 147 | */ | 148 | | - async register(urlOrSpecifier, parentURL, data) { | | 148 | + async register(urlOrSpecifier, parentURL, data, isInternal) { | 149 | 149 | const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | 150 | | - const keyedExports = await cascadedLoader.import( | 151 | | - urlOrSpecifier, | 152 | | - parentURL, | 153 | | - kEmptyObject, | 154 | | - ); | | 150 | + const keyedExports = isInternal ? | | 151 | + require(urlOrSpecifier) : | | 152 | + await cascadedLoader.import( | | 153 | + urlOrSpecifier, | | 154 | + parentURL, | | 155 | + kEmptyObject, | | 156 | + ); | 155 | 157 | await this.addCustomLoader(urlOrSpecifier, keyedExports, data); | 156 | 158 | } | 157 | 159 |
| |
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 |
|---|
@@ -491,15 +491,15 @@ class ModuleLoader { | 491 | 491 | /** | 492 | 492 | * @see {@link CustomizedModuleLoader.register} | 493 | 493 | */ | 494 | | - register(specifier, parentURL, data, transferList) { | | 494 | + register(specifier, parentURL, data, transferList, isInternal) { | 495 | 495 | if (!this.#customizations) { | 496 | 496 | // `CustomizedModuleLoader` is defined at the bottom of this file and | 497 | 497 | // available well before this line is ever invoked. This is here in | 498 | 498 | // order to preserve the git diff instead of moving the class. | 499 | 499 | // eslint-disable-next-line no-use-before-define | 500 | 500 | this.setCustomizations(new CustomizedModuleLoader()); | 501 | 501 | } | 502 | | - return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList); | | 502 | + return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList, isInternal); | 503 | 503 | } | 504 | 504 |
| 505 | 505 | /** | @@ -636,10 +636,11 @@ class CustomizedModuleLoader { | 636 | 636 | * @param {any} [data] Arbitrary data to be passed from the custom loader | 637 | 637 | * (user-land) to the worker. | 638 | 638 | * @param {any[]} [transferList] Objects in `data` that are changing ownership | | 639 | + * @param {boolean} [isInternal] For internal loaders that should not be publicly exposed. | 639 | 640 | * @returns {{ format: string, url: URL['href'] }} | 640 | 641 | */ | 641 | | - register(originalSpecifier, parentURL, data, transferList) { | 642 | | - return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data); | | 642 | + register(originalSpecifier, parentURL, data, transferList, isInternal) { | | 643 | + return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data, isInternal); | 643 | 644 | } | 644 | 645 |
| 645 | 646 | /** | |
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); | |
|