mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
Remove `module.register()` (DEP0205), the `--experimental-loader` and `--loader` flags, and the machinery that supported them: the loader hooks worker thread, the hooks proxy, the async module customization hook chains for resolving and loading, the internal worker class used to run the loader thread, and the asynchronous module job linking. Module customization is now provided only by `module.registerHooks()`, which runs synchronously on the thread that loads the modules. As the loader thread no longer exists, the ES module loader always loads and links modules synchronously, so the code that handled promises and the re-invented `require()` for imported CommonJS customized by the async module customization hooks is removed too. `import()` still defers evaluation of the module to a later microtask, as before. `worker_threads.isInternalThread` is kept for compatibility and is now always `false`; removing it is left for a future semver-major change. The `ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED` and `ERR_LOADER_CHAIN_INCOMPLETE` error codes are no longer used and are documented as removed. Tests that exist only to exercise the async module customization hooks are removed. Assisted-by: Claude Signed-off-by: Geoffrey Booth <webadmin@geoffreybooth.com> PR-URL: https://github.com/nodejs/node/pull/67090 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
119 lines
4.4 KiB
JavaScript
119 lines
4.4 KiB
JavaScript
'use strict';
|
|
// Flags: --expose-internals
|
|
|
|
require('../common');
|
|
|
|
const assert = require('assert');
|
|
const { ModuleLoader } = require('internal/modules/esm/loader');
|
|
const { LoadCache, ResolveCache } = require('internal/modules/esm/module_map');
|
|
const { ModuleJob } = require('internal/modules/esm/module_job');
|
|
const createDynamicModule = require(
|
|
'internal/modules/esm/create_dynamic_module');
|
|
|
|
const jsModuleDataUrl = 'data:text/javascript,export{}';
|
|
const jsonModuleDataUrl = 'data:application/json,""';
|
|
|
|
const stubJsModule = createDynamicModule([], ['default'], jsModuleDataUrl);
|
|
const stubJsonModule = createDynamicModule([], ['default'], jsonModuleDataUrl);
|
|
|
|
const loader = new ModuleLoader();
|
|
const jsModuleJob = new ModuleJob(loader, jsModuleDataUrl, {}, stubJsModule.module);
|
|
const jsonModuleJob = new ModuleJob(loader, jsonModuleDataUrl,
|
|
{ type: 'json' }, stubJsonModule.module);
|
|
|
|
// LoadCache.set and LoadCache.get store and retrieve module jobs for a
|
|
// specified url/type tuple; LoadCache.has correctly reports whether such jobs
|
|
// are stored in the map.
|
|
{
|
|
const moduleMap = new LoadCache();
|
|
|
|
moduleMap.set(jsModuleDataUrl, undefined, jsModuleJob);
|
|
moduleMap.set(jsonModuleDataUrl, 'json', jsonModuleJob);
|
|
|
|
assert.strictEqual(moduleMap.get(jsModuleDataUrl), jsModuleJob);
|
|
assert.strictEqual(moduleMap.get(jsonModuleDataUrl, 'json'), jsonModuleJob);
|
|
|
|
assert.strictEqual(moduleMap.has(jsModuleDataUrl), true);
|
|
assert.strictEqual(moduleMap.has(jsModuleDataUrl, 'javascript'), true);
|
|
assert.strictEqual(moduleMap.has(jsonModuleDataUrl, 'json'), true);
|
|
|
|
assert.strictEqual(moduleMap.has('unknown'), false);
|
|
|
|
// The types must match
|
|
assert.strictEqual(moduleMap.has(jsModuleDataUrl, 'json'), false);
|
|
assert.strictEqual(moduleMap.has(jsonModuleDataUrl, 'javascript'), false);
|
|
assert.strictEqual(moduleMap.has(jsonModuleDataUrl), false);
|
|
assert.strictEqual(moduleMap.has(jsModuleDataUrl, 'unknown'), false);
|
|
assert.strictEqual(moduleMap.has(jsonModuleDataUrl, 'unknown'), false);
|
|
}
|
|
|
|
// LoadCache.get, LoadCache.has and LoadCache.set should only accept string
|
|
// values as url argument.
|
|
{
|
|
const moduleMap = new LoadCache();
|
|
|
|
const errorObj = {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /^The "url" argument must be of type string/,
|
|
};
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
assert.throws(() => moduleMap.get(value), errorObj);
|
|
assert.throws(() => moduleMap.has(value), errorObj);
|
|
assert.throws(() => moduleMap.set(value, undefined, jsModuleJob), errorObj);
|
|
});
|
|
}
|
|
|
|
// LoadCache.get, LoadCache.has and LoadCache.set should only accept string
|
|
// values (or the kAssertType symbol) as type argument.
|
|
{
|
|
const moduleMap = new LoadCache();
|
|
|
|
const errorObj = {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /^The "type" argument must be of type string/,
|
|
};
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
assert.throws(() => moduleMap.get(jsModuleDataUrl, value), errorObj);
|
|
assert.throws(() => moduleMap.has(jsModuleDataUrl, value), errorObj);
|
|
assert.throws(() => moduleMap.set(jsModuleDataUrl, value, jsModuleJob), errorObj);
|
|
});
|
|
}
|
|
|
|
// LoadCache.set should only accept ModuleJob values as job argument.
|
|
{
|
|
const moduleMap = new LoadCache();
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
assert.throws(() => moduleMap.set('', undefined, value), {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /^The "job" argument must be an instance of ModuleJob/,
|
|
});
|
|
});
|
|
}
|
|
|
|
{
|
|
const resolveMap = new ResolveCache();
|
|
|
|
assert.strictEqual(resolveMap.serializeKey('./file', { __proto__: null }), './file::');
|
|
assert.strictEqual(resolveMap.serializeKey('./file', { __proto__: null, type: 'json' }), './file::"type""json"');
|
|
assert.strictEqual(resolveMap.serializeKey('./file::"type""json"', { __proto__: null }), './file::"type""json"::');
|
|
assert.strictEqual(resolveMap.serializeKey('./file', { __proto__: null, c: 'd', a: 'b' }), './file::"a""b","c""d"');
|
|
assert.strictEqual(
|
|
resolveMap.serializeKey('./s', { __proto__: null, c: 'd', a: 'b', b: 'c' }),
|
|
'./s::"a""b","b""c","c""d"',
|
|
);
|
|
|
|
resolveMap.set('key1', 'parent1', 1);
|
|
resolveMap.set('key2', 'parent1', 2);
|
|
resolveMap.set('key2', 'parent2', 3);
|
|
|
|
assert.strictEqual(resolveMap.get('key1', 'parent1'), 1);
|
|
assert.strictEqual(resolveMap.get('key2', 'parent1'), 2);
|
|
assert.strictEqual(resolveMap.get('key2', 'parent2'), 3);
|
|
}
|