Skip to content

Commit 389a24b

Browse files
legendecastargos
authored andcommitted
module: allow overriding linked requests for a ModuleWrap
This allows overriding linked requests for a `ModuleWrap`. The `statusOverride` in `vm.SourceTextModule` could call `moduleWrap.link` a second time when `statusOverride` of `linking` is set to undefined. Overriding of linked requests should be no harm but better to be avoided. However, this will require a follow-up fix on `statusOverride` in `vm.SourceTextModule`. PR-URL: #59527 Fixes: #59480 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent e2b6bdc commit 389a24b

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

src/module_wrap.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -604,23 +604,13 @@ void ModuleWrap::GetModuleRequests(const FunctionCallbackInfo<Value>& args) {
604604

605605
// moduleWrap.link(moduleWraps)
606606
void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
607-
Realm* realm = Realm::GetCurrent(args);
608607
Isolate* isolate = args.GetIsolate();
609608

610609
ModuleWrap* dependent;
611610
ASSIGN_OR_RETURN_UNWRAP(&dependent, args.This());
612611

613612
CHECK_EQ(args.Length(), 1);
614613

615-
Local<Data> linked_requests =
616-
args.This()->GetInternalField(kLinkedRequestsSlot);
617-
if (linked_requests->IsValue() &&
618-
!linked_requests.As<Value>()->IsUndefined()) {
619-
// If the module is already linked, we should not link it again.
620-
THROW_ERR_VM_MODULE_LINK_FAILURE(realm->env(), "module is already linked");
621-
return;
622-
}
623-
624614
Local<FixedArray> requests =
625615
dependent->module_.Get(isolate)->GetModuleRequests();
626616
Local<Array> modules = args[0].As<Array>();
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Flags: --experimental-vm-modules
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const vm = require('vm');
7+
const assert = require('assert');
8+
9+
// This test verifies that a module can be returned multiple
10+
// times in the linker function in `module.link(linker)`.
11+
// `module.link(linker)` should handle the race condition of
12+
// `module.status` when the linker function is asynchronous.
13+
14+
// Regression of https://github.com/nodejs/node/issues/59480
15+
16+
const sources = {
17+
'./index.js': `
18+
import foo from "./foo.js";
19+
import shared from "./shared.js";
20+
export default {
21+
foo,
22+
shared
23+
};
24+
`,
25+
'./foo.js': `
26+
import shared from "./shared.js";
27+
export default {
28+
name: "foo"
29+
};
30+
`,
31+
'./shared.js': `
32+
export default {
33+
name: "shared",
34+
};
35+
`,
36+
};
37+
38+
const moduleCache = new Map();
39+
40+
function getModuleInstance(identifier) {
41+
let module = moduleCache.get(identifier);
42+
43+
if (!module) {
44+
module = new vm.SourceTextModule(sources[identifier], {
45+
identifier,
46+
});
47+
moduleCache.set(identifier, module);
48+
}
49+
50+
return module;
51+
}
52+
53+
async function esmImport(identifier) {
54+
const module = getModuleInstance(identifier);
55+
const requests = [];
56+
57+
await module.link(async (specifier, referrer) => {
58+
requests.push([specifier, referrer.identifier]);
59+
// Use `Promise.resolve` to defer a tick to create a race condition on
60+
// `module.status` when a module is being imported several times.
61+
return Promise.resolve(getModuleInstance(specifier));
62+
});
63+
64+
await module.evaluate();
65+
return [module.namespace, requests];
66+
}
67+
68+
async function test() {
69+
const { 0: mod, 1: requests } = await esmImport('./index.js');
70+
assert.strictEqual(mod.default.foo.name, 'foo');
71+
assert.strictEqual(mod.default.shared.name, 'shared');
72+
73+
// Assert that there is no duplicated requests.
74+
assert.deepStrictEqual(requests, [
75+
// [specifier, referrer]
76+
['./foo.js', './index.js'],
77+
['./shared.js', './index.js'],
78+
['./shared.js', './foo.js'],
79+
]);
80+
}
81+
82+
test().then(common.mustCall());

0 commit comments

Comments
 (0)