GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: allow overriding linked requests for a ModuleWrap · nodejs/node@389a24b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • module_wrap.cc
  • test/parallel
    • test-vm-module-link-shared-deps.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/module_wrap.cc‎

Copy file name to clipboardExpand all lines: src/module_wrap.cc
-10Lines 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>();
Collapse file

‎test/parallel/test-vm-module-link-shared-deps.js‎

Copy file name to clipboard
+82Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.