mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
ESM resolution and loading is now always synchronous from a non-loader-hook thread. If no asynchrnous loader hooks are registered, the resolution/loading is entirely synchronous. If asynchronous loader hooks are registered, these would be synchronous on the non-loader-hook thread, and asynchronous on the loader hook thread. This avoids several races caused by async/sync loading sharing the same cache. In particular, asynchronous loader hooks now works with `require(esm)` - previously it tends to break due to races. In addition, when an asynchronous loader hook returns a promise that never settles, the main thread no longer silently exits with exit code 13, leaving the code below any module loading calls silently ignored without being executed. Instead, it now throws ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED which can be caught and handled by the main thread. If the module request comes from `import()`, the never-settling promise is now relayed to the result returned by `import()`. Drive-by: when annotating the error about importing undetectable named exports from CommonJS, it now no longer reload the source code of the CommonJS module, and instead reuses format information cached when the module was loaded for linking. PR-URL: https://github.com/nodejs/node/pull/60380 Fixes: https://github.com/nodejs/node/issues/59666 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>
73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
// This tests that the async loader hooks can be invoked for require(esm).
|
|
|
|
require('../common');
|
|
const common = require('../common');
|
|
const { spawnSyncAndAssert } = require('../common/child_process');
|
|
const fixtures = require('../common/fixtures');
|
|
const assert = require('assert');
|
|
|
|
function assertSubGraph(output) {
|
|
// FIXME(node:59666): the async resolve hook invoked for require() in imported CJS only get the URL,
|
|
// not the specifier. This may be fixable if we re-implement the async loader hooks from within
|
|
// the synchronous loader hooks and use the original require() implementation.
|
|
assert.match(output, /resolve .+esm\.mjs from file:.+main\.cjs/);
|
|
assert.match(output, /load file:.+esm\.mjs/);
|
|
|
|
assert.match(output, /resolve \.\/inner\.mjs from file:.+esm\.mjs/);
|
|
assert.match(output, /load file:.+inner\.mjs/);
|
|
|
|
assert.match(output, /resolve \.\/cjs\.cjs from file:.+esm\.mjs/);
|
|
assert.match(output, /load file:.+cjs\.cjs/);
|
|
|
|
// FIXME(node:59666): see above.
|
|
assert.match(output, /resolve .+inner\.cjs from file:.+cjs\.cjs/);
|
|
assert.match(output, /load file:.+inner\.cjs/);
|
|
|
|
assert.match(output, /esmValue in main\.cjs: esm/);
|
|
assert.match(output, /cjsValue in main\.cjs: commonjs/);
|
|
}
|
|
|
|
spawnSyncAndAssert(process.execPath, [
|
|
'--import',
|
|
fixtures.fileURL('module-hooks', 'register-logger-async-hooks.mjs'),
|
|
fixtures.path('module-hooks', 'require-esm', 'main.cjs'),
|
|
], {
|
|
stdout: common.mustCall((output) => {
|
|
// The graph is:
|
|
// main.cjs
|
|
// -> esm.mjs
|
|
// -> inner.mjs
|
|
// -> cjs.cjs
|
|
// -> inner.cjs
|
|
assert.match(output, /resolve .+main\.cjs from undefined/);
|
|
assert.match(output, /load file:.+main\.cjs/);
|
|
|
|
assertSubGraph(output);
|
|
}),
|
|
});
|
|
|
|
spawnSyncAndAssert(process.execPath, [
|
|
'--import',
|
|
fixtures.fileURL('module-hooks', 'register-logger-async-hooks.mjs'),
|
|
fixtures.path('module-hooks', 'require-esm', 'main.mjs'),
|
|
], {
|
|
stdout: common.mustCall((output) => {
|
|
// The graph is:
|
|
// main.mjs
|
|
// -> main.cjs
|
|
// -> esm.mjs
|
|
// -> inner.mjs
|
|
// -> cjs.cjs
|
|
// -> inner.cjs
|
|
assert.match(output, /resolve .+main\.mjs from undefined/);
|
|
assert.match(output, /load file:.+main\.mjs/);
|
|
|
|
assert.match(output, /resolve .+main\.cjs from file:.+main\.mjs/);
|
|
assert.match(output, /load file:.+main\.cjs/);
|
|
|
|
assertSubGraph(output);
|
|
}),
|
|
});
|