Skip to content

Commit

Permalink
module: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36680
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 committed Jan 6, 2021
1 parent ddac3bd commit 5f2bb88
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/esm/module_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* global WebAssembly */

const {
ArrayPrototypeForEach,
ArrayPrototypeMap,
Boolean,
JSONParse,
Expand All @@ -12,6 +13,7 @@ const {
PromisePrototypeCatch,
PromiseReject,
RegExpPrototypeTest,
SafeArrayIterator,
SafeMap,
SafeSet,
StringPrototypeReplace,
Expand Down Expand Up @@ -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 });
Expand All @@ -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]) &&
Expand All @@ -269,7 +271,7 @@ function cjsPreparseModuleExports(filename) {
for (const name of reexportNames)
exportNames.add(name);
}
}
});

return { module, exportNames };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 5f2bb88

Please sign in to comment.