Skip to content

Commit 302be57

Browse files
aduh95danielleadams
authored andcommitted
module: refactor to avoid unsafe array iteration
PR-URL: #36680 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent dfc962f commit 302be57

File tree

6 files changed

+14
-10
lines changed

6 files changed

+14
-10
lines changed

lib/internal/modules/esm/get_format.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) {
4242
}
4343
const parsed = new URL(url);
4444
if (parsed.protocol === 'data:') {
45-
const [ , mime ] = RegExpPrototypeExec(
45+
const { 1: mime } = RegExpPrototypeExec(
4646
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
4747
parsed.pathname,
48-
) || [ null, null, null ];
48+
) || [ , null ];
4949
const format = ({
5050
'__proto__': null,
5151
'text/javascript': 'module',

lib/internal/modules/esm/get_source.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
3131
if (!match) {
3232
throw new ERR_INVALID_URL(url);
3333
}
34-
const [ , base64, body ] = match;
34+
const { 1: base64, 2: body } = match;
3535
source = Buffer.from(body, base64 ? 'base64' : 'utf8');
3636
} else {
3737
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);

lib/internal/modules/esm/module_job.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ class ModuleJob {
110110
' does not provide an export named')) {
111111
const splitStack = StringPrototypeSplit(e.stack, '\n');
112112
const parentFileUrl = splitStack[0];
113-
const [, childSpecifier, name] = StringPrototypeMatch(e.message,
114-
/module '(.*)' does not provide an export named '(.+)'/);
113+
const { 1: childSpecifier, 2: name } = StringPrototypeMatch(
114+
e.message,
115+
/module '(.*)' does not provide an export named '(.+)'/);
115116
const childFileURL =
116117
await this.loader.resolve(childSpecifier, parentFileUrl);
117118
const format = await this.loader.getFormat(childFileURL);

lib/internal/modules/esm/module_map.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { validateString } = require('internal/validators');
1212

1313
// Tracks the state of the loader-level module cache
1414
class ModuleMap extends SafeMap {
15+
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
1516
get(url) {
1617
validateString(url, 'url');
1718
return super.get(url);

lib/internal/modules/esm/translators.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* global WebAssembly */
44

55
const {
6+
ArrayPrototypeForEach,
67
ArrayPrototypeMap,
78
Boolean,
89
JSONParse,
@@ -12,6 +13,7 @@ const {
1213
PromisePrototypeCatch,
1314
PromiseReject,
1415
RegExpPrototypeTest,
16+
SafeArrayIterator,
1517
SafeMap,
1618
SafeSet,
1719
StringPrototypeReplace,
@@ -246,7 +248,7 @@ function cjsPreparseModuleExports(filename) {
246248
reexports = [];
247249
}
248250

249-
const exportNames = new SafeSet(exports);
251+
const exportNames = new SafeSet(new SafeArrayIterator(exports));
250252

251253
// Set first for cycles.
252254
cjsParseCache.set(module, { source, exportNames, loaded });
@@ -255,12 +257,12 @@ function cjsPreparseModuleExports(filename) {
255257
module.filename = filename;
256258
module.paths = CJSModule._nodeModulePaths(module.path);
257259
}
258-
for (const reexport of reexports) {
260+
ArrayPrototypeForEach(reexports, (reexport) => {
259261
let resolved;
260262
try {
261263
resolved = CJSModule._resolveFilename(reexport, module);
262264
} catch {
263-
continue;
265+
return;
264266
}
265267
const ext = extname(resolved);
266268
if ((ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) &&
@@ -269,7 +271,7 @@ function cjsPreparseModuleExports(filename) {
269271
for (const name of reexportNames)
270272
exportNames.add(name);
271273
}
272-
}
274+
});
273275

274276
return { module, exportNames };
275277
}

lib/internal/modules/package_json_reader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function read(jsonPath) {
1818
return cache.get(jsonPath);
1919
}
2020

21-
const [string, containsKeys] = internalModuleReadJSON(
21+
const { 0: string, 1: containsKeys } = internalModuleReadJSON(
2222
toNamespacedPath(jsonPath)
2323
);
2424
const result = { string, containsKeys };

0 commit comments

Comments
 (0)