2017-10-06 18:48:41 +00:00
|
|
|
'use strict';
|
2019-10-11 21:57:13 +00:00
|
|
|
// Flags: --expose-internals
|
2017-10-06 18:48:41 +00:00
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
require('../common');
|
2017-10-06 18:48:41 +00:00
|
|
|
|
2021-08-27 23:47:49 +00:00
|
|
|
const { strictEqual, throws } = require('assert');
|
2023-04-13 07:35:17 +00:00
|
|
|
const { createModuleLoader } = require('internal/modules/esm/loader');
|
2023-07-26 20:34:20 +00:00
|
|
|
const { LoadCache, ResolveCache } = require('internal/modules/esm/module_map');
|
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`
This is based on the the following design aspect of ESM:
- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
also synchronous, and, by the time the module graph is instantiated
(before evaluation starts), this is is already known.
If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:
- Explicitly marked as an ES module with a `"type": "module"` field in
the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).
`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.
```mjs
// point.mjs
export function distance(a, b) {
return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```
```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
// default: [class Point],
// distance: [Function: distance]
// }
console.log(required);
(async () => {
const imported = await import('./point.mjs');
console.log(imported === required); // true
})();
```
If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.
If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.
PR-URL: https://github.com/nodejs/node/pull/51977
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-11 17:50:24 +00:00
|
|
|
const { ModuleJob } = require('internal/modules/esm/module_job');
|
2018-03-23 14:32:23 +00:00
|
|
|
const createDynamicModule = require(
|
|
|
|
'internal/modules/esm/create_dynamic_module');
|
2017-10-06 18:48:41 +00:00
|
|
|
|
2021-08-27 23:47:49 +00:00
|
|
|
const jsModuleDataUrl = 'data:text/javascript,export{}';
|
|
|
|
const jsonModuleDataUrl = 'data:application/json,""';
|
|
|
|
|
|
|
|
const stubJsModule = createDynamicModule([], ['default'], jsModuleDataUrl);
|
|
|
|
const stubJsonModule = createDynamicModule([], ['default'], jsonModuleDataUrl);
|
|
|
|
|
2023-04-13 07:35:17 +00:00
|
|
|
const loader = createModuleLoader(false);
|
2021-08-27 23:47:49 +00:00
|
|
|
const jsModuleJob = new ModuleJob(loader, stubJsModule.module, undefined,
|
|
|
|
() => new Promise(() => {}));
|
|
|
|
const jsonModuleJob = new ModuleJob(loader, stubJsonModule.module,
|
|
|
|
{ type: 'json' },
|
|
|
|
() => new Promise(() => {}));
|
2017-10-06 18:48:41 +00:00
|
|
|
|
2021-08-27 23:47:49 +00:00
|
|
|
|
2023-07-26 20:34:20 +00:00
|
|
|
// LoadCache.set and LoadCache.get store and retrieve module jobs for a
|
|
|
|
// specified url/type tuple; LoadCache.has correctly reports whether such jobs
|
2021-08-27 23:47:49 +00:00
|
|
|
// are stored in the map.
|
|
|
|
{
|
2023-07-26 20:34:20 +00:00
|
|
|
const moduleMap = new LoadCache();
|
2021-08-27 23:47:49 +00:00
|
|
|
|
|
|
|
moduleMap.set(jsModuleDataUrl, undefined, jsModuleJob);
|
|
|
|
moduleMap.set(jsonModuleDataUrl, 'json', jsonModuleJob);
|
|
|
|
|
|
|
|
strictEqual(moduleMap.get(jsModuleDataUrl), jsModuleJob);
|
|
|
|
strictEqual(moduleMap.get(jsonModuleDataUrl, 'json'), jsonModuleJob);
|
|
|
|
|
|
|
|
strictEqual(moduleMap.has(jsModuleDataUrl), true);
|
2021-11-10 07:23:47 +00:00
|
|
|
strictEqual(moduleMap.has(jsModuleDataUrl, 'javascript'), true);
|
2021-08-27 23:47:49 +00:00
|
|
|
strictEqual(moduleMap.has(jsonModuleDataUrl, 'json'), true);
|
|
|
|
|
|
|
|
strictEqual(moduleMap.has('unknown'), false);
|
|
|
|
|
|
|
|
// The types must match
|
|
|
|
strictEqual(moduleMap.has(jsModuleDataUrl, 'json'), false);
|
2021-11-10 07:23:47 +00:00
|
|
|
strictEqual(moduleMap.has(jsonModuleDataUrl, 'javascript'), false);
|
2021-08-27 23:47:49 +00:00
|
|
|
strictEqual(moduleMap.has(jsonModuleDataUrl), false);
|
|
|
|
strictEqual(moduleMap.has(jsModuleDataUrl, 'unknown'), false);
|
|
|
|
strictEqual(moduleMap.has(jsonModuleDataUrl, 'unknown'), false);
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:34:20 +00:00
|
|
|
// LoadCache.get, LoadCache.has and LoadCache.set should only accept string
|
2021-08-27 23:47:49 +00:00
|
|
|
// values as url argument.
|
|
|
|
{
|
2023-07-26 20:34:20 +00:00
|
|
|
const moduleMap = new LoadCache();
|
2021-08-27 23:47:49 +00:00
|
|
|
|
|
|
|
const errorObj = {
|
2017-10-06 18:48:41 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2021-08-27 23:47:49 +00:00
|
|
|
message: /^The "url" argument must be of type string/
|
|
|
|
};
|
|
|
|
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
|
|
throws(() => moduleMap.get(value), errorObj);
|
|
|
|
throws(() => moduleMap.has(value), errorObj);
|
|
|
|
throws(() => moduleMap.set(value, undefined, jsModuleJob), errorObj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:34:20 +00:00
|
|
|
// LoadCache.get, LoadCache.has and LoadCache.set should only accept string
|
2021-08-27 23:47:49 +00:00
|
|
|
// values (or the kAssertType symbol) as type argument.
|
|
|
|
{
|
2023-07-26 20:34:20 +00:00
|
|
|
const moduleMap = new LoadCache();
|
2021-08-27 23:47:49 +00:00
|
|
|
|
|
|
|
const errorObj = {
|
2017-10-06 18:48:41 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2021-08-27 23:47:49 +00:00
|
|
|
message: /^The "type" argument must be of type string/
|
|
|
|
};
|
|
|
|
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
|
|
throws(() => moduleMap.get(jsModuleDataUrl, value), errorObj);
|
|
|
|
throws(() => moduleMap.has(jsModuleDataUrl, value), errorObj);
|
|
|
|
throws(() => moduleMap.set(jsModuleDataUrl, value, jsModuleJob), errorObj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:34:20 +00:00
|
|
|
// LoadCache.set should only accept ModuleJob values as job argument.
|
2021-08-27 23:47:49 +00:00
|
|
|
{
|
2023-07-26 20:34:20 +00:00
|
|
|
const moduleMap = new LoadCache();
|
2021-08-27 23:47:49 +00:00
|
|
|
|
|
|
|
[{}, [], true, 1].forEach((value) => {
|
|
|
|
throws(() => moduleMap.set('', undefined, value), {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
name: 'TypeError',
|
|
|
|
message: /^The "job" argument must be an instance of ModuleJob/
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-07-26 20:34:20 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const resolveMap = new ResolveCache();
|
|
|
|
|
|
|
|
strictEqual(resolveMap.serializeKey('./file', { __proto__: null }), './file::');
|
|
|
|
strictEqual(resolveMap.serializeKey('./file', { __proto__: null, type: 'json' }), './file::"type""json"');
|
|
|
|
strictEqual(resolveMap.serializeKey('./file::"type""json"', { __proto__: null }), './file::"type""json"::');
|
|
|
|
strictEqual(resolveMap.serializeKey('./file', { __proto__: null, c: 'd', a: 'b' }), './file::"a""b","c""d"');
|
|
|
|
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);
|
|
|
|
|
|
|
|
strictEqual(resolveMap.get('key1', 'parent1'), 1);
|
|
|
|
strictEqual(resolveMap.get('key2', 'parent1'), 2);
|
|
|
|
strictEqual(resolveMap.get('key2', 'parent2'), 3);
|
|
|
|
}
|