mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
lib: enforce use of Promise from primordials
PR-URL: https://github.com/nodejs/node/pull/30936 Refs: https://github.com/nodejs/node/issues/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>
This commit is contained in:
@@ -23,6 +23,8 @@ rules:
|
||||
message: "Use `const { Number } = primordials;` instead of the global."
|
||||
- name: Object
|
||||
message: "Use `const { Object } = primordials;` instead of the global."
|
||||
- name: Promise
|
||||
message: "Use `const { Promise } = primordials;` instead of the global."
|
||||
- name: Reflect
|
||||
message: "Use `const { Reflect } = primordials;` instead of the global."
|
||||
- name: Symbol
|
||||
|
||||
@@ -27,6 +27,7 @@ const {
|
||||
ObjectAssign,
|
||||
ObjectDefineProperty,
|
||||
ObjectPrototypeHasOwnProperty,
|
||||
Promise,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
|
||||
@@ -30,6 +30,7 @@ const {
|
||||
ObjectDefineProperty,
|
||||
ObjectGetPrototypeOf,
|
||||
ObjectKeys,
|
||||
Promise,
|
||||
ReflectApply,
|
||||
ReflectOwnKeys,
|
||||
Symbol,
|
||||
|
||||
@@ -30,6 +30,7 @@ const {
|
||||
ObjectCreate,
|
||||
ObjectDefineProperties,
|
||||
ObjectDefineProperty,
|
||||
Promise,
|
||||
} = primordials;
|
||||
|
||||
const { fs: constants } = internalBinding('constants');
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const {
|
||||
ObjectCreate,
|
||||
ObjectDefineProperty,
|
||||
Promise,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
// - Bring your own custom fs module is not currently supported.
|
||||
// - Some basic code cleanup.
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
Promise,
|
||||
} = primordials;
|
||||
|
||||
const { Buffer } = require('buffer');
|
||||
const {
|
||||
chmod,
|
||||
|
||||
@@ -10,6 +10,7 @@ const {
|
||||
ObjectCreate,
|
||||
ObjectDefineProperty,
|
||||
ObjectPrototypeHasOwnProperty,
|
||||
Promise,
|
||||
ReflectGetPrototypeOf,
|
||||
Symbol,
|
||||
} = primordials;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const {
|
||||
ObjectSetPrototypeOf,
|
||||
PromiseAll,
|
||||
SafeSet,
|
||||
SafePromise,
|
||||
} = primordials;
|
||||
@@ -79,7 +80,7 @@ class ModuleJob {
|
||||
}
|
||||
jobsInGraph.add(moduleJob);
|
||||
const dependencyJobs = await moduleJob.linked;
|
||||
return Promise.all(dependencyJobs.map(addJobsToDependencyGraph));
|
||||
return PromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
|
||||
};
|
||||
await addJobsToDependencyGraph(this);
|
||||
try {
|
||||
|
||||
@@ -47,6 +47,22 @@ function copyPropsRenamed(src, dest, prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
function copyPropsRenamedBound(src, dest, prefix) {
|
||||
for (const key of Reflect.ownKeys(src)) {
|
||||
if (typeof key === 'string') {
|
||||
const desc = Reflect.getOwnPropertyDescriptor(src, key);
|
||||
if (typeof desc.value === 'function') {
|
||||
desc.value = desc.value.bind(src);
|
||||
}
|
||||
Reflect.defineProperty(
|
||||
dest,
|
||||
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
|
||||
desc
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function copyPrototype(src, dest, prefix) {
|
||||
for (const key of Reflect.ownKeys(src)) {
|
||||
if (typeof key === 'string') {
|
||||
@@ -135,5 +151,17 @@ primordials.SafePromise = makeSafe(
|
||||
copyPrototype(original.prototype, primordials, `${name}Prototype`);
|
||||
});
|
||||
|
||||
// Create copies of intrinsic objects that require a valid `this` to call
|
||||
// static methods.
|
||||
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
|
||||
[
|
||||
'Promise',
|
||||
].forEach((name) => {
|
||||
const original = global[name];
|
||||
primordials[name] = original;
|
||||
copyPropsRenamedBound(original, primordials, name);
|
||||
copyPrototype(original.prototype, primordials, `${name}Prototype`);
|
||||
});
|
||||
|
||||
Object.setPrototypeOf(primordials, null);
|
||||
Object.freeze(primordials);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const {
|
||||
JSONStringify,
|
||||
PromiseResolve,
|
||||
} = primordials;
|
||||
|
||||
const path = require('path');
|
||||
@@ -41,7 +42,7 @@ function evalModule(source, print) {
|
||||
const { log, error } = require('internal/console/global');
|
||||
const { decorateErrorStack } = require('internal/util');
|
||||
const asyncESM = require('internal/process/esm_loader');
|
||||
Promise.resolve(asyncESM.ESMLoader).then(async (loader) => {
|
||||
PromiseResolve(asyncESM.ESMLoader).then(async (loader) => {
|
||||
const { result } = await loader.eval(source);
|
||||
if (print) {
|
||||
log(result);
|
||||
|
||||
@@ -4,6 +4,9 @@ const {
|
||||
ObjectCreate,
|
||||
ObjectGetPrototypeOf,
|
||||
ObjectSetPrototypeOf,
|
||||
Promise,
|
||||
PromiseReject,
|
||||
PromiseResolve,
|
||||
Symbol,
|
||||
} = primordials;
|
||||
|
||||
@@ -67,11 +70,11 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
|
||||
// reject straight away.
|
||||
const error = this[kError];
|
||||
if (error !== null) {
|
||||
return Promise.reject(error);
|
||||
return PromiseReject(error);
|
||||
}
|
||||
|
||||
if (this[kEnded]) {
|
||||
return Promise.resolve(createIterResult(undefined, true));
|
||||
return PromiseResolve(createIterResult(undefined, true));
|
||||
}
|
||||
|
||||
if (this[kStream].destroyed) {
|
||||
@@ -103,7 +106,7 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
|
||||
// without triggering the next() queue.
|
||||
const data = this[kStream].read();
|
||||
if (data !== null) {
|
||||
return Promise.resolve(createIterResult(data, false));
|
||||
return PromiseResolve(createIterResult(data, false));
|
||||
}
|
||||
|
||||
promise = new Promise(this[kHandlePromise]);
|
||||
|
||||
@@ -10,6 +10,7 @@ const {
|
||||
ObjectGetOwnPropertyDescriptors,
|
||||
ObjectGetPrototypeOf,
|
||||
ObjectSetPrototypeOf,
|
||||
Promise,
|
||||
ReflectConstruct,
|
||||
Symbol,
|
||||
SymbolFor,
|
||||
|
||||
@@ -7,6 +7,8 @@ const {
|
||||
MathMax,
|
||||
ObjectCreate,
|
||||
ObjectEntries,
|
||||
Promise,
|
||||
PromiseResolve,
|
||||
Symbol,
|
||||
SymbolFor,
|
||||
} = primordials;
|
||||
@@ -259,11 +261,11 @@ class Worker extends EventEmitter {
|
||||
'Passing a callback to worker.terminate() is deprecated. ' +
|
||||
'It returns a Promise instead.',
|
||||
'DeprecationWarning', 'DEP0132');
|
||||
if (this[kHandle] === null) return Promise.resolve();
|
||||
if (this[kHandle] === null) return PromiseResolve();
|
||||
this.once('exit', (exitCode) => callback(null, exitCode));
|
||||
}
|
||||
|
||||
if (this[kHandle] === null) return Promise.resolve();
|
||||
if (this[kHandle] === null) return PromiseResolve();
|
||||
|
||||
this[kHandle].stopThread();
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ const {
|
||||
ObjectGetPrototypeOf,
|
||||
ObjectKeys,
|
||||
ObjectSetPrototypeOf,
|
||||
Promise,
|
||||
PromiseRace,
|
||||
Symbol,
|
||||
} = primordials;
|
||||
|
||||
@@ -460,7 +462,7 @@ function REPLServer(prompt,
|
||||
};
|
||||
prioritizedSigintQueue.add(sigintListener);
|
||||
});
|
||||
promise = Promise.race([promise, interrupt]);
|
||||
promise = PromiseRace([promise, interrupt]);
|
||||
}
|
||||
|
||||
promise.then((result) => {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
const {
|
||||
MathTrunc,
|
||||
Promise,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
|
||||
Reference in New Issue
Block a user