diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 81042a5c9d6f27..8299e79a00eba0 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -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 diff --git a/lib/child_process.js b/lib/child_process.js index 2a0d471c02a74d..0549e5daf7d10a 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -27,6 +27,7 @@ const { ObjectAssign, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, + Promise, } = primordials; const { diff --git a/lib/events.js b/lib/events.js index 13ecbd9ea87433..fc530ff7f3fc15 100644 --- a/lib/events.js +++ b/lib/events.js @@ -30,6 +30,7 @@ const { ObjectDefineProperty, ObjectGetPrototypeOf, ObjectKeys, + Promise, ReflectApply, ReflectOwnKeys, Symbol, diff --git a/lib/fs.js b/lib/fs.js index de37a0c2a0a704..32429a70896101 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -30,6 +30,7 @@ const { ObjectCreate, ObjectDefineProperties, ObjectDefineProperty, + Promise, } = primordials; const { fs: constants } = internalBinding('constants'); diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index c4bbc21ae5528c..ae007fd31934e2 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -3,6 +3,7 @@ const { ObjectCreate, ObjectDefineProperty, + Promise, } = primordials; const { diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index 3402af60a4022f..02cdfdf4b5da3f 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -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, diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index abd9c8f417b81b..0bbf74cdc71ded 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -10,6 +10,7 @@ const { ObjectCreate, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, + Promise, ReflectGetPrototypeOf, Symbol, } = primordials; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index e4a9040217689d..01a574044b4f4a 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -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 { diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 46ce4457d1e814..09bea0fc7fc523 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -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); diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 8b15fbbae46f26..d9567e643d7c85 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -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); diff --git a/lib/internal/streams/async_iterator.js b/lib/internal/streams/async_iterator.js index a6b8bd9d181c38..ced9c77c4f50e2 100644 --- a/lib/internal/streams/async_iterator.js +++ b/lib/internal/streams/async_iterator.js @@ -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]); diff --git a/lib/internal/util.js b/lib/internal/util.js index f80c9212304a03..8d4f66a0be26ff 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -10,6 +10,7 @@ const { ObjectGetOwnPropertyDescriptors, ObjectGetPrototypeOf, ObjectSetPrototypeOf, + Promise, ReflectConstruct, Symbol, SymbolFor, diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 6cdd6cba0b18bc..f078d5329d6017 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -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(); diff --git a/lib/repl.js b/lib/repl.js index 24948477fea65e..181556a2316c2d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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) => { diff --git a/lib/timers.js b/lib/timers.js index 68327e2c3e5947..643fb06d2ea49c 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -23,6 +23,7 @@ const { MathTrunc, + Promise, } = primordials; const {