Skip to content

Commit a940dba

Browse files
committed
vm: throw error when duplicated exportNames in SyntheticModule
Fixes: #32806
1 parent cf888ac commit a940dba

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
@@ -124,12 +124,22 @@ const util = require('util');
124124
// Check to throws invalid exportNames
125125
{
126126
assert.throws(() => new SyntheticModule(undefined, () => {}, {}), {
127-
message: 'The "exportNames" argument must be an Array of strings.' +
128-
' Received undefined',
127+
message: 'The "exportNames" argument must be an ' +
128+
'Array of unique strings.' +
129+
' Received undefined',
129130
name: 'TypeError'
130131
});
131132
}
132133

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

0 commit comments

Comments
 (0)