@solana/promises 6.3.1 → 6.3.2-canary-20260313112147

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/promises",
3
- "version": "6.3.1",
3
+ "version": "6.3.2-canary-20260313112147",
4
4
  "description": "Helpers for using JavaScript promises",
5
5
  "homepage": "https://www.solanakit.com/api#solanapromises",
6
6
  "exports": {
@@ -33,7 +33,8 @@
33
33
  "types": "./dist/types/index.d.ts",
34
34
  "type": "commonjs",
35
35
  "files": [
36
- "./dist/"
36
+ "./dist/",
37
+ "./src/"
37
38
  ],
38
39
  "sideEffects": false,
39
40
  "keywords": [
@@ -0,0 +1,39 @@
1
+ import { safeRace } from './race';
2
+
3
+ /**
4
+ * Returns a new promise that will reject if the abort signal fires before the original promise
5
+ * settles. Resolves or rejects with the value of the original promise otherwise.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const result = await getAbortablePromise(
10
+ * // Resolves or rejects when `fetch` settles.
11
+ * fetch('https://example.com/json').then(r => r.json()),
12
+ * // ...unless it takes longer than 5 seconds, after which the `AbortSignal` is triggered.
13
+ * AbortSignal.timeout(5000),
14
+ * );
15
+ * ```
16
+ */
17
+ export function getAbortablePromise<T>(promise: Promise<T>, abortSignal?: AbortSignal): Promise<T> {
18
+ if (!abortSignal) {
19
+ return promise;
20
+ } else {
21
+ return safeRace([
22
+ // This promise only ever rejects if the signal is aborted. Otherwise it idles forever.
23
+ // It's important that this come before the input promise; in the event of an abort, we
24
+ // want to throw even if the input promise's result is ready
25
+ new Promise<never>((_, reject) => {
26
+ if (abortSignal.aborted) {
27
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
28
+ reject(abortSignal.reason);
29
+ } else {
30
+ abortSignal.addEventListener('abort', function () {
31
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
32
+ reject(this.reason);
33
+ });
34
+ }
35
+ }),
36
+ promise,
37
+ ]);
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * This package contains helpers for using JavaScript promises.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export * from './abortable';
7
+ export * from './race';
package/src/race.ts ADDED
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Forked from https://github.com/digitalloggers/race-as-promised/tree/master
3
+ *
4
+ * Authored by Brian Kim:
5
+ * https://github.com/nodejs/node/issues/17469#issuecomment-685216777
6
+ *
7
+ * Adapted to module structure.
8
+ *
9
+ * This is free and unencumbered software released into the public domain.
10
+ *
11
+ * Anyone is free to copy, modify, publish, use, compile, sell, or
12
+ * distribute this software, either in source code form or as a compiled
13
+ * binary, for any purpose, commercial or non-commercial, and by any
14
+ * means.
15
+ *
16
+ * In jurisdictions that recognize copyright laws, the author or authors
17
+ * of this software dedicate any and all copyright interest in the
18
+ * software to the public domain. We make this dedication for the benefit
19
+ * of the public at large and to the detriment of our heirs and
20
+ * successors. We intend this dedication to be an overt act of
21
+ * relinquishment in perpetuity of all present and future rights to this
22
+ * software under copyright law.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30
+ * OTHER DEALINGS IN THE SOFTWARE.
31
+ *
32
+ * For more information, please refer to <http://unlicense.org/>
33
+ */
34
+
35
+ type Deferred = Readonly<{
36
+ reject: (reason?: unknown) => void;
37
+ resolve: (value: unknown) => void;
38
+ }>;
39
+
40
+ function isObject(value: unknown): value is object {
41
+ return value !== null && (typeof value === 'object' || typeof value === 'function');
42
+ }
43
+
44
+ function addRaceContender(contender: object) {
45
+ const deferreds = new Set<Deferred>();
46
+ const record = { deferreds, settled: false };
47
+
48
+ // This call to `then` happens once for the lifetime of the value.
49
+ Promise.resolve(contender).then(
50
+ value => {
51
+ for (const { resolve } of deferreds) {
52
+ resolve(value);
53
+ }
54
+
55
+ deferreds.clear();
56
+ record.settled = true;
57
+ },
58
+ err => {
59
+ for (const { reject } of deferreds) {
60
+ reject(err);
61
+ }
62
+
63
+ deferreds.clear();
64
+ record.settled = true;
65
+ },
66
+ );
67
+ return record;
68
+ }
69
+
70
+ // Keys are the values passed to race, values are a record of data containing a
71
+ // set of deferreds and whether the value has settled.
72
+ const wm = new WeakMap<object, { deferreds: Set<Deferred>; settled: boolean }>();
73
+ /**
74
+ * An implementation of [`Promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)
75
+ * that causes all of the losing promises to settle. This allows them to be released and garbage
76
+ * collected, preventing memory leaks.
77
+ *
78
+ * Read more here: https://github.com/nodejs/node/issues/17469
79
+ */
80
+ export async function safeRace<T extends readonly unknown[] | []>(contenders: T): Promise<Awaited<T[number]>> {
81
+ let deferred: Deferred;
82
+ const result = new Promise((resolve, reject) => {
83
+ deferred = { reject, resolve };
84
+ for (const contender of contenders) {
85
+ if (!isObject(contender)) {
86
+ // If the contender is a primitive, attempting to use it as a key in the
87
+ // weakmap would throw an error. Luckily, it is safe to call
88
+ // `Promise.resolve(contender).then` on a primitive value multiple times
89
+ // because the promise fulfills immediately.
90
+ Promise.resolve(contender).then(resolve, reject);
91
+ continue;
92
+ }
93
+
94
+ let record = wm.get(contender);
95
+ if (record === undefined) {
96
+ record = addRaceContender(contender);
97
+ record.deferreds.add(deferred);
98
+ wm.set(contender, record);
99
+ } else if (record.settled) {
100
+ // If the value has settled, it is safe to call
101
+ // `Promise.resolve(contender).then` on it.
102
+ Promise.resolve(contender).then(resolve, reject);
103
+ } else {
104
+ record.deferreds.add(deferred);
105
+ }
106
+ }
107
+ });
108
+
109
+ // The finally callback executes when any value settles, preventing any of
110
+ // the unresolved values from retaining a reference to the resolved value.
111
+ return await (result.finally(() => {
112
+ for (const contender of contenders) {
113
+ if (isObject(contender)) {
114
+ const record = wm.get(contender)!;
115
+ record.deferreds.delete(deferred);
116
+ }
117
+ }
118
+ }) as Promise<Awaited<T[number]>>);
119
+ }