mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 20:08:02 +00:00

Create `lib/internal/modules` and restructure the module loaders to make the purpose of those files clearer. Also make it clear in the code that the object exported by `lib/internal/modules/cjs/loader.js` is `CJSModule` instead of the ambiguous `Module`. Before: ``` lib ├── ... ├── internal │ ├── loaders │ │ ├── CreateDynamicModule.js │ │ ├── DefaultResolve.js │ │ ├── Loader.js │ │ ├── ModuleJob.js │ │ ├── ModuleMap.js │ │ └── Translators.js │ └── module.js └── module.js ``` After: ``` lib ├── ... ├── internal │ ├── ... │ └── modules │ ├── cjs │ │ ├── helpers.js │ │ └── loader.js │ └── esm │ ├── CreateDynamicModule.js │ ├── DefaultResolve.js │ ├── Loader.js │ ├── ModuleJob.js │ ├── ModuleMap.js │ └── Translators.js └── module.js # deleted in this commit to work with git file mode ``` PR-URL: https://github.com/nodejs/node/pull/19177 Refs: https://github.com/nodejs/node/pull/19112 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const ModuleMap = require('internal/modules/esm/ModuleMap');
|
|
|
|
// ModuleMap.get, ModuleMap.has and ModuleMap.set should only accept string
|
|
// values as url argument.
|
|
{
|
|
const errorReg = common.expectsError({
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError,
|
|
message: /^The "url" argument must be of type string/
|
|
}, 15);
|
|
|
|
const moduleMap = new ModuleMap();
|
|
|
|
// As long as the assertion of "job" argument is done after the assertion of
|
|
// "url" argument this test suite is ok. Tried to mock the "job" parameter,
|
|
// but I think it's useless, and was not simple to mock...
|
|
const job = undefined;
|
|
|
|
[{}, [], true, 1, () => {}].forEach((value) => {
|
|
assert.throws(() => moduleMap.get(value), errorReg);
|
|
assert.throws(() => moduleMap.has(value), errorReg);
|
|
assert.throws(() => moduleMap.set(value, job), errorReg);
|
|
});
|
|
}
|
|
|
|
// ModuleMap.set, job argument should only accept ModuleJob values.
|
|
{
|
|
const errorReg = common.expectsError({
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError,
|
|
message: /^The "job" argument must be of type ModuleJob/
|
|
}, 5);
|
|
|
|
const moduleMap = new ModuleMap();
|
|
|
|
[{}, [], true, 1, () => {}].forEach((value) => {
|
|
assert.throws(() => moduleMap.set('', value), errorReg);
|
|
});
|
|
}
|