Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6678897

Browse filesBrowse files
aduh95codebytere
authored andcommitted
esm: refactor to use more primordials
PR-URL: #36019 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 75707f4 commit 6678897
Copy full SHA for 6678897

File tree

Expand file treeCollapse file tree

8 files changed

+50
-26
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+50
-26
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/create_dynamic_module.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/create_dynamic_module.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const {
55
ArrayPrototypeMap,
66
JSONStringify,
77
ObjectCreate,
8-
Set,
8+
SafeSet,
99
} = primordials;
1010

1111
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
@@ -38,7 +38,7 @@ import.meta.done();
3838
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
3939
const m = new ModuleWrap(`${url}`, undefined, source, 0, 0);
4040

41-
const readyfns = new Set();
41+
const readyfns = new SafeSet();
4242
const reflect = {
4343
exports: ObjectCreate(null),
4444
onReady: (cb) => { readyfns.add(cb); },
Collapse file

‎lib/internal/modules/esm/get_format.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
2-
const { StringPrototypeStartsWith } = primordials;
2+
const {
3+
RegExpPrototypeExec,
4+
StringPrototypeStartsWith,
5+
} = primordials;
36
const { extname } = require('path');
47
const { getOptionValue } = require('internal/options');
58

@@ -39,7 +42,10 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) {
3942
}
4043
const parsed = new URL(url);
4144
if (parsed.protocol === 'data:') {
42-
const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ];
45+
const [ , mime ] = RegExpPrototypeExec(
46+
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
47+
parsed.pathname,
48+
) || [ null, null, null ];
4349
const format = ({
4450
'__proto__': null,
4551
'text/javascript': 'module',
Collapse file

‎lib/internal/modules/esm/get_source.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_source.js
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const {
4+
RegExpPrototypeExec,
5+
} = primordials;
36
const { getOptionValue } = require('internal/options');
47
// Do not eagerly grab .manifest, it may be in TDZ
58
const policy = getOptionValue('--experimental-policy') ?
@@ -8,14 +11,13 @@ const policy = getOptionValue('--experimental-policy') ?
811

912
const { Buffer } = require('buffer');
1013

11-
const fs = require('fs');
12-
const { URL } = require('url');
13-
const { promisify } = require('internal/util');
14+
const fs = require('internal/fs/promises').exports;
15+
const { URL } = require('internal/url');
1416
const {
1517
ERR_INVALID_URL,
1618
ERR_INVALID_URL_SCHEME,
1719
} = require('internal/errors').codes;
18-
const readFileAsync = promisify(fs.readFile);
20+
const readFileAsync = fs.readFile;
1921

2022
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
2123

@@ -25,7 +27,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
2527
if (parsed.protocol === 'file:') {
2628
source = await readFileAsync(parsed);
2729
} else if (parsed.protocol === 'data:') {
28-
const match = DATA_URL_PATTERN.exec(parsed.pathname);
30+
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
2931
if (!match) {
3032
throw new ERR_INVALID_URL(url);
3133
}
Collapse file

‎lib/internal/modules/esm/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
FunctionPrototypeBind,
88
ObjectSetPrototypeOf,
99
SafeWeakMap,
10+
StringPrototypeStartsWith,
1011
} = primordials;
1112

1213
const {
@@ -126,8 +127,8 @@ class Loader {
126127
}
127128

128129
if (this._resolve === defaultResolve &&
129-
!url.startsWith('file:') &&
130-
!url.startsWith('data:')
130+
!StringPrototypeStartsWith(url, 'file:') &&
131+
!StringPrototypeStartsWith(url, 'data:')
131132
) {
132133
throw new ERR_INVALID_RETURN_PROPERTY(
133134
'file: or data: url', 'loader resolve', 'url', url
Collapse file

‎lib/internal/modules/esm/module_job.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+15-9Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
const {
44
ArrayPrototypeJoin,
5+
ArrayPrototypeMap,
6+
ArrayPrototypePush,
7+
FunctionPrototype,
58
ObjectSetPrototypeOf,
69
PromiseAll,
10+
PromiseResolve,
11+
PromisePrototypeCatch,
12+
ReflectApply,
713
SafeSet,
8-
SafePromise,
914
StringPrototypeIncludes,
1015
StringPrototypeMatch,
1116
StringPrototypeReplace,
@@ -16,9 +21,9 @@ const { ModuleWrap } = internalBinding('module_wrap');
1621

1722
const { decorateErrorStack } = require('internal/util');
1823
const assert = require('internal/assert');
19-
const resolvedPromise = SafePromise.resolve();
24+
const resolvedPromise = PromiseResolve();
2025

21-
function noop() {}
26+
const noop = FunctionPrototype;
2227

2328
let hasPausedEntry = false;
2429

@@ -35,7 +40,7 @@ class ModuleJob {
3540
this.module = undefined;
3641
// Expose the promise to the ModuleWrap directly for linking below.
3742
// `this.module` is also filled in below.
38-
this.modulePromise = moduleProvider.call(loader, url, isMain);
43+
this.modulePromise = ReflectApply(moduleProvider, loader, [url, isMain]);
3944

4045
// Wait for the ModuleWrap instance being linked with all dependencies.
4146
const link = async () => {
@@ -49,21 +54,21 @@ class ModuleJob {
4954
const dependencyJobs = [];
5055
const promises = this.module.link(async (specifier) => {
5156
const jobPromise = this.loader.getModuleJob(specifier, url);
52-
dependencyJobs.push(jobPromise);
57+
ArrayPrototypePush(dependencyJobs, jobPromise);
5358
const job = await jobPromise;
5459
return job.modulePromise;
5560
});
5661

5762
if (promises !== undefined)
58-
await SafePromise.all(promises);
63+
await PromiseAll(promises);
5964

60-
return SafePromise.all(dependencyJobs);
65+
return PromiseAll(dependencyJobs);
6166
};
6267
// Promise for the list of all dependencyJobs.
6368
this.linked = link();
6469
// This promise is awaited later anyway, so silence
6570
// 'unhandled rejection' warnings.
66-
this.linked.catch(noop);
71+
PromisePrototypeCatch(this.linked, noop);
6772

6873
// instantiated == deep dependency jobs wrappers are instantiated,
6974
// and module wrapper is instantiated.
@@ -85,7 +90,8 @@ class ModuleJob {
8590
}
8691
jobsInGraph.add(moduleJob);
8792
const dependencyJobs = await moduleJob.linked;
88-
return PromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
93+
return PromiseAll(
94+
ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph));
8995
};
9096
await addJobsToDependencyGraph(this);
9197

Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,8 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
880880
[internalFS.realpathCacheKey]: realpathCache
881881
});
882882
const old = url;
883-
url = pathToFileURL(real + (urlPath.endsWith(sep) ? '/' : ''));
883+
url = pathToFileURL(
884+
real + (StringPrototypeEndsWith(urlPath, sep) ? '/' : ''));
884885
url.search = old.search;
885886
url.hash = old.hash;
886887
}
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* global WebAssembly */
44

55
const {
6+
ArrayPrototypeMap,
67
Boolean,
78
JSONParse,
89
ObjectGetPrototypeOf,
@@ -14,6 +15,7 @@ const {
1415
SafeMap,
1516
SafeSet,
1617
StringPrototypeReplace,
18+
StringPrototypeSlice,
1719
StringPrototypeSplit,
1820
StringPrototypeStartsWith,
1921
SyntaxErrorPrototype,
@@ -277,9 +279,9 @@ function cjsPreparseModuleExports(filename) {
277279
translators.set('builtin', async function builtinStrategy(url) {
278280
debug(`Translating BuiltinModule ${url}`);
279281
// Slice 'node:' scheme
280-
const id = url.slice(5);
282+
const id = StringPrototypeSlice(url, 5);
281283
const module = loadNativeModule(id, url, true);
282-
if (!url.startsWith('node:') || !module) {
284+
if (!StringPrototypeStartsWith(url, 'node:') || !module) {
283285
throw new ERR_UNKNOWN_BUILTIN_MODULE(url);
284286
}
285287
debug(`Loading BuiltinModule ${url}`);
@@ -291,7 +293,8 @@ translators.set('json', async function jsonStrategy(url) {
291293
emitExperimentalWarning('Importing JSON modules');
292294
debug(`Translating JSONModule ${url}`);
293295
debug(`Loading JSONModule ${url}`);
294-
const pathname = url.startsWith('file:') ? fileURLToPath(url) : null;
296+
const pathname = StringPrototypeStartsWith(url, 'file:') ?
297+
fileURLToPath(url) : null;
295298
let modulePath;
296299
let module;
297300
if (pathname) {
@@ -365,8 +368,11 @@ translators.set('wasm', async function(url) {
365368
}
366369

367370
const imports =
368-
WebAssembly.Module.imports(compiled).map(({ module }) => module);
369-
const exports = WebAssembly.Module.exports(compiled).map(({ name }) => name);
371+
ArrayPrototypeMap(WebAssembly.Module.imports(compiled),
372+
({ module }) => module);
373+
const exports =
374+
ArrayPrototypeMap(WebAssembly.Module.exports(compiled),
375+
({ name }) => name);
370376

371377
return createDynamicModule(imports, exports, url, (reflect) => {
372378
const { exports } = new WebAssembly.Instance(compiled, reflect.imports);
Collapse file

‎test/parallel/test-bootstrap-modules.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const expectedModules = new Set([
4949
'NativeModule internal/fixed_queue',
5050
'NativeModule internal/fs/dir',
5151
'NativeModule internal/fs/utils',
52+
'NativeModule internal/fs/promises',
53+
'NativeModule internal/fs/rimraf',
5254
'NativeModule internal/idna',
5355
'NativeModule internal/linkedlist',
5456
'NativeModule internal/modules/run_main',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.