From 5f2bb885f8027e2789a1fa8679d242162b4a8633 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 29 Dec 2020 20:07:57 +0100 Subject: [PATCH] module: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/36680 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/modules/esm/get_format.js | 4 ++-- lib/internal/modules/esm/get_source.js | 2 +- lib/internal/modules/esm/module_job.js | 5 +++-- lib/internal/modules/esm/module_map.js | 1 + lib/internal/modules/esm/translators.js | 10 ++++++---- lib/internal/modules/package_json_reader.js | 2 +- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 51b207ea75d131..b48741c422c47f 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -42,10 +42,10 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { } const parsed = new URL(url); if (parsed.protocol === 'data:') { - const [ , mime ] = RegExpPrototypeExec( + const { 1: mime } = RegExpPrototypeExec( /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, parsed.pathname, - ) || [ null, null, null ]; + ) || [ , null ]; const format = ({ '__proto__': null, 'text/javascript': 'module', diff --git a/lib/internal/modules/esm/get_source.js b/lib/internal/modules/esm/get_source.js index b2cf0c3bd28aa2..155a019802f0ba 100644 --- a/lib/internal/modules/esm/get_source.js +++ b/lib/internal/modules/esm/get_source.js @@ -31,7 +31,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) { if (!match) { throw new ERR_INVALID_URL(url); } - const [ , base64, body ] = match; + const { 1: base64, 2: body } = match; source = Buffer.from(body, base64 ? 'base64' : 'utf8'); } else { throw new ERR_INVALID_URL_SCHEME(['file', 'data']); diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 5043da896622be..6cf968553bae6d 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -110,8 +110,9 @@ class ModuleJob { ' does not provide an export named')) { const splitStack = StringPrototypeSplit(e.stack, '\n'); const parentFileUrl = splitStack[0]; - const [, childSpecifier, name] = StringPrototypeMatch(e.message, - /module '(.*)' does not provide an export named '(.+)'/); + const { 1: childSpecifier, 2: name } = StringPrototypeMatch( + e.message, + /module '(.*)' does not provide an export named '(.+)'/); const childFileURL = await this.loader.resolve(childSpecifier, parentFileUrl); const format = await this.loader.getFormat(childFileURL); diff --git a/lib/internal/modules/esm/module_map.js b/lib/internal/modules/esm/module_map.js index 9c51c7c48779f3..9e1116a5647f5f 100644 --- a/lib/internal/modules/esm/module_map.js +++ b/lib/internal/modules/esm/module_map.js @@ -12,6 +12,7 @@ const { validateString } = require('internal/validators'); // Tracks the state of the loader-level module cache class ModuleMap extends SafeMap { + constructor(i) { super(i); } // eslint-disable-line no-useless-constructor get(url) { validateString(url, 'url'); return super.get(url); diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 66636d9ce092b5..b685989345b962 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -3,6 +3,7 @@ /* global WebAssembly */ const { + ArrayPrototypeForEach, ArrayPrototypeMap, Boolean, JSONParse, @@ -12,6 +13,7 @@ const { PromisePrototypeCatch, PromiseReject, RegExpPrototypeTest, + SafeArrayIterator, SafeMap, SafeSet, StringPrototypeReplace, @@ -246,7 +248,7 @@ function cjsPreparseModuleExports(filename) { reexports = []; } - const exportNames = new SafeSet(exports); + const exportNames = new SafeSet(new SafeArrayIterator(exports)); // Set first for cycles. cjsParseCache.set(module, { source, exportNames, loaded }); @@ -255,12 +257,12 @@ function cjsPreparseModuleExports(filename) { module.filename = filename; module.paths = CJSModule._nodeModulePaths(module.path); } - for (const reexport of reexports) { + ArrayPrototypeForEach(reexports, (reexport) => { let resolved; try { resolved = CJSModule._resolveFilename(reexport, module); } catch { - continue; + return; } const ext = extname(resolved); if ((ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) && @@ -269,7 +271,7 @@ function cjsPreparseModuleExports(filename) { for (const name of reexportNames) exportNames.add(name); } - } + }); return { module, exportNames }; } diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index 4a2b0e6ddb3ed8..09eb12bd1533bf 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -18,7 +18,7 @@ function read(jsonPath) { return cache.get(jsonPath); } - const [string, containsKeys] = internalModuleReadJSON( + const { 0: string, 1: containsKeys } = internalModuleReadJSON( toNamespacedPath(jsonPath) ); const result = { string, containsKeys };