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 31227e6

Browse filesBrowse files
committed
lib: enforce use of Promise from primordials
PR-URL: #30936 Refs: #30697 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 74f7844 commit 31227e6
Copy full SHA for 31227e6

File tree

Expand file treeCollapse file tree

15 files changed

+59
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

15 files changed

+59
-8
lines changed
Open diff view settings
Collapse file

‎lib/.eslintrc.yaml‎

Copy file name to clipboardExpand all lines: lib/.eslintrc.yaml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ rules:
2323
message: "Use `const { Number } = primordials;` instead of the global."
2424
- name: Object
2525
message: "Use `const { Object } = primordials;` instead of the global."
26+
- name: Promise
27+
message: "Use `const { Promise } = primordials;` instead of the global."
2628
- name: Reflect
2729
message: "Use `const { Reflect } = primordials;` instead of the global."
2830
- name: Symbol
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
ObjectAssign,
2828
ObjectDefineProperty,
2929
ObjectPrototypeHasOwnProperty,
30+
Promise,
3031
} = primordials;
3132

3233
const {
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {
2929
ObjectDefineProperty,
3030
ObjectGetPrototypeOf,
3131
ObjectKeys,
32+
Promise,
3233
ReflectApply,
3334
ReflectOwnKeys,
3435
} = primordials;
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
ObjectCreate,
3131
ObjectDefineProperties,
3232
ObjectDefineProperty,
33+
Promise,
3334
} = primordials;
3435

3536
const { fs: constants } = internalBinding('constants');
Collapse file

‎lib/internal/dns/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/dns/promises.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ObjectCreate,
55
ObjectDefineProperty,
6+
Promise,
67
} = primordials;
78

89
const {
Collapse file

‎lib/internal/fs/rimraf.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/rimraf.js
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// - Bring your own custom fs module is not currently supported.
66
// - Some basic code cleanup.
77
'use strict';
8+
9+
const {
10+
Promise,
11+
} = primordials;
12+
813
const {
914
chmod,
1015
chmodSync,
Collapse file

‎lib/internal/http2/core.js‎

Copy file name to clipboardExpand all lines: lib/internal/http2/core.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ObjectCreate,
1111
ObjectDefineProperty,
1212
ObjectPrototypeHasOwnProperty,
13+
Promise,
1314
ReflectGetPrototypeOf,
1415
Symbol,
1516
} = primordials;
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectSetPrototypeOf,
5+
PromiseAll,
56
SafeSet,
67
SafePromise,
78
} = primordials;
@@ -79,7 +80,7 @@ class ModuleJob {
7980
}
8081
jobsInGraph.add(moduleJob);
8182
const dependencyJobs = await moduleJob.linked;
82-
return Promise.all(dependencyJobs.map(addJobsToDependencyGraph));
83+
return PromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
8384
};
8485
await addJobsToDependencyGraph(this);
8586
try {
Collapse file

‎lib/internal/per_context/primordials.js‎

Copy file name to clipboardExpand all lines: lib/internal/per_context/primordials.js
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ function copyPropsRenamed(src, dest, prefix) {
4747
}
4848
}
4949

50+
function copyPropsRenamedBound(src, dest, prefix) {
51+
for (const key of Reflect.ownKeys(src)) {
52+
if (typeof key === 'string') {
53+
const desc = Reflect.getOwnPropertyDescriptor(src, key);
54+
if (typeof desc.value === 'function') {
55+
desc.value = desc.value.bind(src);
56+
}
57+
Reflect.defineProperty(
58+
dest,
59+
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
60+
desc
61+
);
62+
}
63+
}
64+
}
65+
5066
function copyPrototype(src, dest, prefix) {
5167
for (const key of Reflect.ownKeys(src)) {
5268
if (typeof key === 'string') {
@@ -135,5 +151,17 @@ primordials.SafePromise = makeSafe(
135151
copyPrototype(original.prototype, primordials, `${name}Prototype`);
136152
});
137153

154+
// Create copies of intrinsic objects that require a valid `this` to call
155+
// static methods.
156+
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
157+
[
158+
'Promise',
159+
].forEach((name) => {
160+
const original = global[name];
161+
primordials[name] = original;
162+
copyPropsRenamedBound(original, primordials, name);
163+
copyPrototype(original.prototype, primordials, `${name}Prototype`);
164+
});
165+
138166
Object.setPrototypeOf(primordials, null);
139167
Object.freeze(primordials);
Collapse file

‎lib/internal/process/execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/execution.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
JSONStringify,
5+
PromiseResolve,
56
} = primordials;
67

78
const path = require('path');
@@ -41,7 +42,7 @@ function evalModule(source, print) {
4142
const { log, error } = require('internal/console/global');
4243
const { decorateErrorStack } = require('internal/util');
4344
const asyncESM = require('internal/process/esm_loader');
44-
Promise.resolve(asyncESM.ESMLoader).then(async (loader) => {
45+
PromiseResolve(asyncESM.ESMLoader).then(async (loader) => {
4546
const { result } = await loader.eval(source);
4647
if (print) {
4748
log(result);

0 commit comments

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