| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 428d03c commit dc484b6
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,19 +2,17 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ArrayPrototypeJoin, | |
| 5 | - ArrayPrototypeMap, | ||
| 6 | 5 | ArrayPrototypePush, | |
| 7 | 6 | ArrayPrototypeSome, | |
| 8 | 7 | FunctionPrototype, | |
| 9 | 8 | ObjectCreate, | |
| 10 | 9 | ObjectSetPrototypeOf, | |
| 11 | - PromiseAll, | ||
| 12 | 10 | PromiseResolve, | |
| 13 | 11 | PromisePrototypeCatch, | |
| 14 | 12 | ReflectApply, | |
| 15 | 13 | RegExpPrototypeExec, | |
| 16 | 14 | RegExpPrototypeSymbolReplace, | |
| 17 | - SafeArrayIterator, | ||
| 15 | + SafePromiseAll, | ||
| 18 | 16 | SafeSet, | |
| 19 | 17 | StringPrototypeIncludes, | |
| 20 | 18 | StringPrototypeSplit, | |
@@ -82,9 +80,9 @@ class ModuleJob { | |||
| 82 | 80 | }); | |
| 83 | 81 | ||
| 84 | 82 | if (promises !== undefined) | |
| 85 | - await PromiseAll(new SafeArrayIterator(promises)); | ||
| 83 | + await SafePromiseAll(promises); | ||
| 86 | 84 | ||
| 87 | - return PromiseAll(new SafeArrayIterator(dependencyJobs)); | ||
| 85 | + return SafePromiseAll(dependencyJobs); | ||
| 88 | 86 | }; | |
| 89 | 87 | // Promise for the list of all dependencyJobs. | |
| 90 | 88 | this.linked = link(); | |
@@ -112,8 +110,7 @@ class ModuleJob { | |||
| 112 | 110 | } | |
| 113 | 111 | jobsInGraph.add(moduleJob); | |
| 114 | 112 | const dependencyJobs = await moduleJob.linked; | |
| 115 | - return PromiseAll(new SafeArrayIterator( | ||
| 116 | - ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph))); | ||
| 113 | + return SafePromiseAll(dependencyJobs, addJobsToDependencyGraph); | ||
| 117 | 114 | }; | |
| 118 | 115 | await addJobsToDependencyGraph(this); | |
| 119 | 116 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -262,6 +262,7 @@ function copyPrototype(src, dest, prefix) { | |||
| 262 | 262 | ||
| 263 | 263 | const { | |
| 264 | 264 | ArrayPrototypeForEach, | |
| 265 | + ArrayPrototypeMap, | ||
| 265 | 266 | FinalizationRegistry, | |
| 266 | 267 | FunctionPrototypeCall, | |
| 267 | 268 | Map, | |
@@ -434,5 +435,63 @@ primordials.AsyncIteratorPrototype = | |||
| 434 | 435 | primordials.ReflectGetPrototypeOf( | |
| 435 | 436 | async function* () {}).prototype); | |
| 436 | 437 | ||
| 438 | + const arrayToSafePromiseIterable = (promises, mapFn) => | ||
| 439 | + new primordials.SafeArrayIterator( | ||
| 440 | + ArrayPrototypeMap( | ||
| 441 | + promises, | ||
| 442 | + (promise, i) => | ||
| 443 | + new SafePromise((a, b) => PromisePrototypeThen(mapFn == null ? promise : mapFn(promise, i), a, b)) | ||
| 444 | + ) | ||
| 445 | + ); | ||
| 446 | + | ||
| 447 | + /** | ||
| 448 | + * @param {Promise<any>[]} promises | ||
| 449 | + * @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn] | ||
| 450 | + * @returns {Promise<any[]>} | ||
| 451 | + */ | ||
| 452 | + primordials.SafePromiseAll = (promises, mapFn) => | ||
| 453 | + // Wrapping on a new Promise is necessary to not expose the SafePromise | ||
| 454 | + // prototype to user-land. | ||
| 455 | + new Promise((a, b) => | ||
| 456 | + SafePromise.all(arrayToSafePromiseIterable(promises, mapFn)).then(a, b) | ||
| 457 | + ); | ||
| 458 | + | ||
| 459 | + /** | ||
| 460 | + * @param {Promise<any>[]} promises | ||
| 461 | + * @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn] | ||
| 462 | + * @returns {Promise<PromiseSettledResult<any>[]>} | ||
| 463 | + */ | ||
| 464 | + primordials.SafePromiseAllSettled = (promises, mapFn) => | ||
| 465 | + // Wrapping on a new Promise is necessary to not expose the SafePromise | ||
| 466 | + // prototype to user-land. | ||
| 467 | + new Promise((a, b) => | ||
| 468 | + SafePromise.allSettled(arrayToSafePromiseIterable(promises, mapFn)).then(a, b) | ||
| 469 | + ); | ||
| 470 | + | ||
| 471 | + /** | ||
| 472 | + * @param {Promise<any>[]} promises | ||
| 473 | + * @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn] | ||
| 474 | + * @returns {Promise<any>} | ||
| 475 | + */ | ||
| 476 | + primordials.SafePromiseAny = (promises, mapFn) => | ||
| 477 | + // Wrapping on a new Promise is necessary to not expose the SafePromise | ||
| 478 | + // prototype to user-land. | ||
| 479 | + new Promise((a, b) => | ||
| 480 | + SafePromise.any(arrayToSafePromiseIterable(promises, mapFn)).then(a, b) | ||
| 481 | + ); | ||
| 482 | + | ||
| 483 | + /** | ||
| 484 | + * @param {Promise<any>[]} promises | ||
| 485 | + * @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn] | ||
| 486 | + * @returns {Promise<any>} | ||
| 487 | + */ | ||
| 488 | + primordials.SafePromiseRace = (promises, mapFn) => | ||
| 489 | + // Wrapping on a new Promise is necessary to not expose the SafePromise | ||
| 490 | + // prototype to user-land. | ||
| 491 | + new Promise((a, b) => | ||
| 492 | + SafePromise.race(arrayToSafePromiseIterable(promises, mapFn)).then(a, b) | ||
| 493 | + ); | ||
| 494 | + | ||
| 495 | + | ||
| 437 | 496 | ObjectSetPrototypeOf(primordials, null); | |
| 438 | 497 | ObjectFreeze(primordials); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,8 +10,8 @@ const { | |||
| 10 | 10 | ObjectDefineProperty, | |
| 11 | 11 | ObjectGetPrototypeOf, | |
| 12 | 12 | ObjectSetPrototypeOf, | |
| 13 | - PromiseAll, | ||
| 14 | 13 | ReflectApply, | |
| 14 | + SafePromiseAll, | ||
| 15 | 15 | SafeWeakMap, | |
| 16 | 16 | Symbol, | |
| 17 | 17 | SymbolToStringTag, | |
@@ -330,7 +330,7 @@ class SourceTextModule extends Module { | |||
| 330 | 330 | ||
| 331 | 331 | try { | |
| 332 | 332 | if (promises !== undefined) { | |
| 333 | - await PromiseAll(promises); | ||
| 333 | + await SafePromiseAll(promises); | ||
| 334 | 334 | } | |
| 335 | 335 | } catch (e) { | |
| 336 | 336 | this.#error = e; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,9 +7,18 @@ const assert = require('assert'); | |||
| 7 | 7 | const { | |
| 8 | 8 | PromisePrototypeCatch, | |
| 9 | 9 | PromisePrototypeThen, | |
| 10 | + SafePromiseAll, | ||
| 11 | + SafePromiseAllSettled, | ||
| 12 | + SafePromiseAny, | ||
| 10 | 13 | SafePromisePrototypeFinally, | |
| 14 | + SafePromiseRace, | ||
| 11 | 15 | } = require('internal/test/binding').primordials; | |
| 12 | 16 | ||
| 17 | + Array.prototype[Symbol.iterator] = common.mustNotCall(); | ||
| 18 | + Promise.all = common.mustNotCall(); | ||
| 19 | + Promise.allSettled = common.mustNotCall(); | ||
| 20 | + Promise.any = common.mustNotCall(); | ||
| 21 | + Promise.race = common.mustNotCall(); | ||
| 13 | 22 | Promise.prototype.catch = common.mustNotCall(); | |
| 14 | 23 | Promise.prototype.finally = common.mustNotCall(); | |
| 15 | 24 | Promise.prototype.then = common.mustNotCall(); | |
@@ -18,6 +27,11 @@ assertIsPromise(PromisePrototypeCatch(Promise.reject(), common.mustCall())); | |||
| 18 | 27 | assertIsPromise(PromisePrototypeThen(test(), common.mustCall())); | |
| 19 | 28 | assertIsPromise(SafePromisePrototypeFinally(test(), common.mustCall())); | |
| 20 | 29 | ||
| 30 | + assertIsPromise(SafePromiseAll([test()])); | ||
| 31 | + assertIsPromise(SafePromiseAllSettled([test()])); | ||
| 32 | + assertIsPromise(SafePromiseAny([test()])); | ||
| 33 | + assertIsPromise(SafePromiseRace([test()])); | ||
| 34 | + | ||
| 21 | 35 | async function test() { | |
| 22 | 36 | const catchFn = common.mustCall(); | |
| 23 | 37 | const finallyFn = common.mustCall(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments