Skip to content

Commit 9545013

Browse files
committed
vm: throw error when duplicated exportNames in SyntheticModule
Fixes: #32806 PR-URL: #32810 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent f64c640 commit 9545013

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/internal/vm/module.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
} = require('internal/util');
2323
const {
2424
ERR_INVALID_ARG_TYPE,
25+
ERR_INVALID_ARG_VALUE,
2526
ERR_VM_MODULE_ALREADY_LINKED,
2627
ERR_VM_MODULE_DIFFERENT_CONTEXT,
2728
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA,
@@ -379,8 +380,17 @@ class SyntheticModule extends Module {
379380
constructor(exportNames, evaluateCallback, options = {}) {
380381
if (!ArrayIsArray(exportNames) ||
381382
exportNames.some((e) => typeof e !== 'string')) {
382-
throw new ERR_INVALID_ARG_TYPE('exportNames', 'Array of strings',
383+
throw new ERR_INVALID_ARG_TYPE('exportNames',
384+
'Array of unique strings',
383385
exportNames);
386+
} else {
387+
exportNames.forEach((name, i) => {
388+
if (exportNames.indexOf(name, i + 1) !== -1) {
389+
throw new ERR_INVALID_ARG_VALUE(`exportNames.${name}`,
390+
name,
391+
'is duplicated');
392+
}
393+
});
384394
}
385395
if (typeof evaluateCallback !== 'function') {
386396
throw new ERR_INVALID_ARG_TYPE('evaluateCallback', 'function',

test/parallel/test-vm-module-basic.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,22 @@ const util = require('util');
125125
// Check to throws invalid exportNames
126126
{
127127
assert.throws(() => new SyntheticModule(undefined, () => {}, {}), {
128-
message: 'The "exportNames" argument must be an Array of strings.' +
129-
' Received undefined',
128+
message: 'The "exportNames" argument must be an ' +
129+
'Array of unique strings.' +
130+
' Received undefined',
130131
name: 'TypeError'
131132
});
132133
}
133134

135+
// Check to throws duplicated exportNames
136+
// https://github.com/nodejs/node/issues/32806
137+
{
138+
assert.throws(() => new SyntheticModule(['x', 'x'], () => {}, {}), {
139+
message: 'The argument \'exportNames.x\' is duplicated. Received \'x\'',
140+
name: 'TypeError',
141+
});
142+
}
143+
134144
// Check to throws invalid evaluateCallback
135145
{
136146
assert.throws(() => new SyntheticModule([], undefined, {}), {

0 commit comments

Comments
 (0)