Skip to content

Commit 31227e6

Browse files
committed
lib: enforce use of Promise from primordials
PR-URL: #30936 Refs: #30697 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 74f7844 commit 31227e6

15 files changed

+59
-8
lines changed

lib/.eslintrc.yaml

+2
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

lib/child_process.js

+1
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 {

lib/events.js

+1
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;

lib/fs.js

+1
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');

lib/internal/dns/promises.js

+1
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 {

lib/internal/fs/rimraf.js

+5
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,

lib/internal/http2/core.js

+1
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;

lib/internal/modules/esm/module_job.js

+2-1
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 {

lib/internal/per_context/primordials.js

+28
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);

lib/internal/process/execution.js

+2-1
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);

lib/internal/streams/async_iterator.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const {
44
ObjectCreate,
55
ObjectGetPrototypeOf,
66
ObjectSetPrototypeOf,
7+
Promise,
8+
PromiseReject,
9+
PromiseResolve,
710
Symbol,
811
} = primordials;
912

@@ -67,11 +70,11 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
6770
// reject straight away.
6871
const error = this[kError];
6972
if (error !== null) {
70-
return Promise.reject(error);
73+
return PromiseReject(error);
7174
}
7275

7376
if (this[kEnded]) {
74-
return Promise.resolve(createIterResult(undefined, true));
77+
return PromiseResolve(createIterResult(undefined, true));
7578
}
7679

7780
if (this[kStream].destroyed) {
@@ -103,7 +106,7 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
103106
// without triggering the next() queue.
104107
const data = this[kStream].read();
105108
if (data !== null) {
106-
return Promise.resolve(createIterResult(data, false));
109+
return PromiseResolve(createIterResult(data, false));
107110
}
108111

109112
promise = new Promise(this[kHandlePromise]);

lib/internal/util.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ObjectGetOwnPropertyDescriptors,
1111
ObjectGetPrototypeOf,
1212
ObjectSetPrototypeOf,
13+
Promise,
1314
ReflectConstruct,
1415
Symbol,
1516
SymbolFor,

lib/internal/worker.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const {
77
MathMax,
88
ObjectCreate,
99
ObjectEntries,
10+
Promise,
11+
PromiseResolve,
1012
Symbol,
1113
SymbolFor,
1214
} = primordials;
@@ -251,11 +253,11 @@ class Worker extends EventEmitter {
251253
'Passing a callback to worker.terminate() is deprecated. ' +
252254
'It returns a Promise instead.',
253255
'DeprecationWarning', 'DEP0132');
254-
if (this[kHandle] === null) return Promise.resolve();
256+
if (this[kHandle] === null) return PromiseResolve();
255257
this.once('exit', (exitCode) => callback(null, exitCode));
256258
}
257259

258-
if (this[kHandle] === null) return Promise.resolve();
260+
if (this[kHandle] === null) return PromiseResolve();
259261

260262
this[kHandle].stopThread();
261263

lib/repl.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const {
5555
ObjectKeys,
5656
ObjectPrototypeHasOwnProperty,
5757
ObjectSetPrototypeOf,
58+
Promise,
59+
PromiseRace,
5860
Symbol,
5961
} = primordials;
6062

@@ -463,7 +465,7 @@ function REPLServer(prompt,
463465
};
464466
prioritizedSigintQueue.add(sigintListener);
465467
});
466-
promise = Promise.race([promise, interrupt]);
468+
promise = PromiseRace([promise, interrupt]);
467469
}
468470

469471
promise.then((result) => {

lib/timers.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
MathTrunc,
26+
Promise,
2627
} = primordials;
2728

2829
const {

0 commit comments

Comments
 (0)