Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: refactor to avoid unsafe array iteration #36680

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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