@depup/supabase__auth-js 2.99.2-depup.0

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.
Files changed (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/changes.json +5 -0
  4. package/dist/main/AuthAdminApi.d.ts +4 -0
  5. package/dist/main/AuthAdminApi.d.ts.map +1 -0
  6. package/dist/main/AuthAdminApi.js +7 -0
  7. package/dist/main/AuthAdminApi.js.map +1 -0
  8. package/dist/main/AuthClient.d.ts +4 -0
  9. package/dist/main/AuthClient.d.ts.map +1 -0
  10. package/dist/main/AuthClient.js +7 -0
  11. package/dist/main/AuthClient.js.map +1 -0
  12. package/dist/main/GoTrueAdminApi.d.ts +227 -0
  13. package/dist/main/GoTrueAdminApi.d.ts.map +1 -0
  14. package/dist/main/GoTrueAdminApi.js +596 -0
  15. package/dist/main/GoTrueAdminApi.js.map +1 -0
  16. package/dist/main/GoTrueClient.d.ts +783 -0
  17. package/dist/main/GoTrueClient.d.ts.map +1 -0
  18. package/dist/main/GoTrueClient.js +3029 -0
  19. package/dist/main/GoTrueClient.js.map +1 -0
  20. package/dist/main/index.d.ts +9 -0
  21. package/dist/main/index.d.ts.map +1 -0
  22. package/dist/main/index.js +20 -0
  23. package/dist/main/index.js.map +1 -0
  24. package/dist/main/lib/base64url.d.ts +76 -0
  25. package/dist/main/lib/base64url.d.ts.map +1 -0
  26. package/dist/main/lib/base64url.js +269 -0
  27. package/dist/main/lib/base64url.js.map +1 -0
  28. package/dist/main/lib/constants.d.ts +26 -0
  29. package/dist/main/lib/constants.d.ts.map +1 -0
  30. package/dist/main/lib/constants.js +31 -0
  31. package/dist/main/lib/constants.js.map +1 -0
  32. package/dist/main/lib/error-codes.d.ts +7 -0
  33. package/dist/main/lib/error-codes.d.ts.map +1 -0
  34. package/dist/main/lib/error-codes.js +3 -0
  35. package/dist/main/lib/error-codes.js.map +1 -0
  36. package/dist/main/lib/errors.d.ts +243 -0
  37. package/dist/main/lib/errors.d.ts.map +1 -0
  38. package/dist/main/lib/errors.js +289 -0
  39. package/dist/main/lib/errors.js.map +1 -0
  40. package/dist/main/lib/fetch.d.ts +34 -0
  41. package/dist/main/lib/fetch.d.ts.map +1 -0
  42. package/dist/main/lib/fetch.js +184 -0
  43. package/dist/main/lib/fetch.js.map +1 -0
  44. package/dist/main/lib/helpers.d.ts +91 -0
  45. package/dist/main/lib/helpers.d.ts.map +1 -0
  46. package/dist/main/lib/helpers.js +395 -0
  47. package/dist/main/lib/helpers.js.map +1 -0
  48. package/dist/main/lib/local-storage.d.ts +9 -0
  49. package/dist/main/lib/local-storage.d.ts.map +1 -0
  50. package/dist/main/lib/local-storage.js +21 -0
  51. package/dist/main/lib/local-storage.js.map +1 -0
  52. package/dist/main/lib/locks.d.ts +107 -0
  53. package/dist/main/lib/locks.d.ts.map +1 -0
  54. package/dist/main/lib/locks.js +314 -0
  55. package/dist/main/lib/locks.js.map +1 -0
  56. package/dist/main/lib/polyfills.d.ts +5 -0
  57. package/dist/main/lib/polyfills.d.ts.map +1 -0
  58. package/dist/main/lib/polyfills.js +29 -0
  59. package/dist/main/lib/polyfills.js.map +1 -0
  60. package/dist/main/lib/types.d.ts +1861 -0
  61. package/dist/main/lib/types.d.ts.map +1 -0
  62. package/dist/main/lib/types.js +23 -0
  63. package/dist/main/lib/types.js.map +1 -0
  64. package/dist/main/lib/version.d.ts +2 -0
  65. package/dist/main/lib/version.d.ts.map +1 -0
  66. package/dist/main/lib/version.js +11 -0
  67. package/dist/main/lib/version.js.map +1 -0
  68. package/dist/main/lib/web3/ethereum.d.ts +96 -0
  69. package/dist/main/lib/web3/ethereum.d.ts.map +1 -0
  70. package/dist/main/lib/web3/ethereum.js +66 -0
  71. package/dist/main/lib/web3/ethereum.js.map +1 -0
  72. package/dist/main/lib/web3/solana.d.ts +160 -0
  73. package/dist/main/lib/web3/solana.d.ts.map +1 -0
  74. package/dist/main/lib/web3/solana.js +4 -0
  75. package/dist/main/lib/web3/solana.js.map +1 -0
  76. package/dist/main/lib/webauthn.d.ts +276 -0
  77. package/dist/main/lib/webauthn.d.ts.map +1 -0
  78. package/dist/main/lib/webauthn.dom.d.ts +583 -0
  79. package/dist/main/lib/webauthn.dom.d.ts.map +1 -0
  80. package/dist/main/lib/webauthn.dom.js +4 -0
  81. package/dist/main/lib/webauthn.dom.js.map +1 -0
  82. package/dist/main/lib/webauthn.errors.d.ts +80 -0
  83. package/dist/main/lib/webauthn.errors.d.ts.map +1 -0
  84. package/dist/main/lib/webauthn.errors.js +265 -0
  85. package/dist/main/lib/webauthn.errors.js.map +1 -0
  86. package/dist/main/lib/webauthn.js +706 -0
  87. package/dist/main/lib/webauthn.js.map +1 -0
  88. package/dist/module/AuthAdminApi.d.ts +4 -0
  89. package/dist/module/AuthAdminApi.d.ts.map +1 -0
  90. package/dist/module/AuthAdminApi.js +4 -0
  91. package/dist/module/AuthAdminApi.js.map +1 -0
  92. package/dist/module/AuthClient.d.ts +4 -0
  93. package/dist/module/AuthClient.d.ts.map +1 -0
  94. package/dist/module/AuthClient.js +4 -0
  95. package/dist/module/AuthClient.js.map +1 -0
  96. package/dist/module/GoTrueAdminApi.d.ts +227 -0
  97. package/dist/module/GoTrueAdminApi.d.ts.map +1 -0
  98. package/dist/module/GoTrueAdminApi.js +593 -0
  99. package/dist/module/GoTrueAdminApi.js.map +1 -0
  100. package/dist/module/GoTrueClient.d.ts +783 -0
  101. package/dist/module/GoTrueClient.d.ts.map +1 -0
  102. package/dist/module/GoTrueClient.js +3026 -0
  103. package/dist/module/GoTrueClient.js.map +1 -0
  104. package/dist/module/index.d.ts +9 -0
  105. package/dist/module/index.d.ts.map +1 -0
  106. package/dist/module/index.js +9 -0
  107. package/dist/module/index.js.map +1 -0
  108. package/dist/module/lib/base64url.d.ts +76 -0
  109. package/dist/module/lib/base64url.d.ts.map +1 -0
  110. package/dist/module/lib/base64url.js +257 -0
  111. package/dist/module/lib/base64url.js.map +1 -0
  112. package/dist/module/lib/constants.d.ts +26 -0
  113. package/dist/module/lib/constants.d.ts.map +1 -0
  114. package/dist/module/lib/constants.js +28 -0
  115. package/dist/module/lib/constants.js.map +1 -0
  116. package/dist/module/lib/error-codes.d.ts +7 -0
  117. package/dist/module/lib/error-codes.d.ts.map +1 -0
  118. package/dist/module/lib/error-codes.js +2 -0
  119. package/dist/module/lib/error-codes.js.map +1 -0
  120. package/dist/module/lib/errors.d.ts +243 -0
  121. package/dist/module/lib/errors.d.ts.map +1 -0
  122. package/dist/module/lib/errors.js +266 -0
  123. package/dist/module/lib/errors.js.map +1 -0
  124. package/dist/module/lib/fetch.d.ts +34 -0
  125. package/dist/module/lib/fetch.d.ts.map +1 -0
  126. package/dist/module/lib/fetch.js +174 -0
  127. package/dist/module/lib/fetch.js.map +1 -0
  128. package/dist/module/lib/helpers.d.ts +91 -0
  129. package/dist/module/lib/helpers.d.ts.map +1 -0
  130. package/dist/module/lib/helpers.js +368 -0
  131. package/dist/module/lib/helpers.js.map +1 -0
  132. package/dist/module/lib/local-storage.d.ts +9 -0
  133. package/dist/module/lib/local-storage.d.ts.map +1 -0
  134. package/dist/module/lib/local-storage.js +18 -0
  135. package/dist/module/lib/local-storage.js.map +1 -0
  136. package/dist/module/lib/locks.d.ts +107 -0
  137. package/dist/module/lib/locks.d.ts.map +1 -0
  138. package/dist/module/lib/locks.js +306 -0
  139. package/dist/module/lib/locks.js.map +1 -0
  140. package/dist/module/lib/polyfills.d.ts +5 -0
  141. package/dist/module/lib/polyfills.d.ts.map +1 -0
  142. package/dist/module/lib/polyfills.js +26 -0
  143. package/dist/module/lib/polyfills.js.map +1 -0
  144. package/dist/module/lib/types.d.ts +1861 -0
  145. package/dist/module/lib/types.d.ts.map +1 -0
  146. package/dist/module/lib/types.js +20 -0
  147. package/dist/module/lib/types.js.map +1 -0
  148. package/dist/module/lib/version.d.ts +2 -0
  149. package/dist/module/lib/version.d.ts.map +1 -0
  150. package/dist/module/lib/version.js +8 -0
  151. package/dist/module/lib/version.js.map +1 -0
  152. package/dist/module/lib/web3/ethereum.d.ts +96 -0
  153. package/dist/module/lib/web3/ethereum.d.ts.map +1 -0
  154. package/dist/module/lib/web3/ethereum.js +60 -0
  155. package/dist/module/lib/web3/ethereum.js.map +1 -0
  156. package/dist/module/lib/web3/solana.d.ts +160 -0
  157. package/dist/module/lib/web3/solana.d.ts.map +1 -0
  158. package/dist/module/lib/web3/solana.js +3 -0
  159. package/dist/module/lib/web3/solana.js.map +1 -0
  160. package/dist/module/lib/webauthn.d.ts +276 -0
  161. package/dist/module/lib/webauthn.d.ts.map +1 -0
  162. package/dist/module/lib/webauthn.dom.d.ts +583 -0
  163. package/dist/module/lib/webauthn.dom.d.ts.map +1 -0
  164. package/dist/module/lib/webauthn.dom.js +3 -0
  165. package/dist/module/lib/webauthn.dom.js.map +1 -0
  166. package/dist/module/lib/webauthn.errors.d.ts +80 -0
  167. package/dist/module/lib/webauthn.errors.d.ts.map +1 -0
  168. package/dist/module/lib/webauthn.errors.js +257 -0
  169. package/dist/module/lib/webauthn.errors.js.map +1 -0
  170. package/dist/module/lib/webauthn.js +689 -0
  171. package/dist/module/lib/webauthn.js.map +1 -0
  172. package/dist/tsconfig.module.tsbuildinfo +1 -0
  173. package/dist/tsconfig.tsbuildinfo +1 -0
  174. package/package.json +56 -0
  175. package/src/AuthAdminApi.ts +5 -0
  176. package/src/AuthClient.ts +5 -0
  177. package/src/GoTrueAdminApi.ts +723 -0
  178. package/src/GoTrueClient.ts +4078 -0
  179. package/src/index.ts +13 -0
  180. package/src/lib/base64url.ts +308 -0
  181. package/src/lib/constants.ts +34 -0
  182. package/src/lib/error-codes.ts +90 -0
  183. package/src/lib/errors.ts +324 -0
  184. package/src/lib/fetch.ts +283 -0
  185. package/src/lib/helpers.ts +463 -0
  186. package/src/lib/local-storage.ts +21 -0
  187. package/src/lib/locks.ts +375 -0
  188. package/src/lib/polyfills.ts +23 -0
  189. package/src/lib/types.ts +2229 -0
  190. package/src/lib/version.ts +7 -0
  191. package/src/lib/web3/ethereum.ts +184 -0
  192. package/src/lib/web3/solana.ts +186 -0
  193. package/src/lib/webauthn.dom.ts +636 -0
  194. package/src/lib/webauthn.errors.ts +317 -0
  195. package/src/lib/webauthn.ts +946 -0
@@ -0,0 +1,375 @@
1
+ import { supportsLocalStorage } from './helpers'
2
+
3
+ /**
4
+ * @experimental
5
+ */
6
+ export const internals = {
7
+ /**
8
+ * @experimental
9
+ */
10
+ debug: !!(
11
+ globalThis &&
12
+ supportsLocalStorage() &&
13
+ globalThis.localStorage &&
14
+ globalThis.localStorage.getItem('supabase.gotrue-js.locks.debug') === 'true'
15
+ ),
16
+ }
17
+
18
+ /**
19
+ * An error thrown when a lock cannot be acquired after some amount of time.
20
+ *
21
+ * Use the {@link #isAcquireTimeout} property instead of checking with `instanceof`.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { LockAcquireTimeoutError } from '@supabase/auth-js'
26
+ *
27
+ * class CustomLockError extends LockAcquireTimeoutError {
28
+ * constructor() {
29
+ * super('Lock timed out')
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ export abstract class LockAcquireTimeoutError extends Error {
35
+ public readonly isAcquireTimeout = true
36
+
37
+ constructor(message: string) {
38
+ super(message)
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Error thrown when the browser Navigator Lock API fails to acquire a lock.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import { NavigatorLockAcquireTimeoutError } from '@supabase/auth-js'
48
+ *
49
+ * throw new NavigatorLockAcquireTimeoutError('Lock timed out')
50
+ * ```
51
+ */
52
+ export class NavigatorLockAcquireTimeoutError extends LockAcquireTimeoutError {}
53
+ /**
54
+ * Error thrown when the process-level lock helper cannot acquire a lock.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import { ProcessLockAcquireTimeoutError } from '@supabase/auth-js'
59
+ *
60
+ * throw new ProcessLockAcquireTimeoutError('Lock timed out')
61
+ * ```
62
+ */
63
+ export class ProcessLockAcquireTimeoutError extends LockAcquireTimeoutError {}
64
+
65
+ /**
66
+ * Implements a global exclusive lock using the Navigator LockManager API. It
67
+ * is available on all browsers released after 2022-03-15 with Safari being the
68
+ * last one to release support. If the API is not available, this function will
69
+ * throw. Make sure you check availablility before configuring {@link
70
+ * GoTrueClient}.
71
+ *
72
+ * You can turn on debugging by setting the `supabase.gotrue-js.locks.debug`
73
+ * local storage item to `true`.
74
+ *
75
+ * Internals:
76
+ *
77
+ * Since the LockManager API does not preserve stack traces for the async
78
+ * function passed in the `request` method, a trick is used where acquiring the
79
+ * lock releases a previously started promise to run the operation in the `fn`
80
+ * function. The lock waits for that promise to finish (with or without error),
81
+ * while the function will finally wait for the result anyway.
82
+ *
83
+ * @param name Name of the lock to be acquired.
84
+ * @param acquireTimeout If negative, no timeout. If 0 an error is thrown if
85
+ * the lock can't be acquired without waiting. If positive, the lock acquire
86
+ * will time out after so many milliseconds. An error is
87
+ * a timeout if it has `isAcquireTimeout` set to true.
88
+ * @param fn The operation to run once the lock is acquired.
89
+ * @example
90
+ * ```ts
91
+ * await navigatorLock('sync-user', 1000, async () => {
92
+ * await refreshSession()
93
+ * })
94
+ * ```
95
+ */
96
+ export async function navigatorLock<R>(
97
+ name: string,
98
+ acquireTimeout: number,
99
+ fn: () => Promise<R>
100
+ ): Promise<R> {
101
+ if (internals.debug) {
102
+ console.log('@supabase/gotrue-js: navigatorLock: acquire lock', name, acquireTimeout)
103
+ }
104
+
105
+ const abortController = new globalThis.AbortController()
106
+
107
+ if (acquireTimeout > 0) {
108
+ setTimeout(() => {
109
+ abortController.abort()
110
+ if (internals.debug) {
111
+ console.log('@supabase/gotrue-js: navigatorLock acquire timed out', name)
112
+ }
113
+ }, acquireTimeout)
114
+ }
115
+
116
+ // MDN article: https://developer.mozilla.org/en-US/docs/Web/API/LockManager/request
117
+
118
+ // Wrapping navigator.locks.request() with a plain Promise is done as some
119
+ // libraries like zone.js patch the Promise object to track the execution
120
+ // context. However, it appears that most browsers use an internal promise
121
+ // implementation when using the navigator.locks.request() API causing them
122
+ // to lose context and emit confusing log messages or break certain features.
123
+ // This wrapping is believed to help zone.js track the execution context
124
+ // better.
125
+ await Promise.resolve()
126
+
127
+ try {
128
+ return await globalThis.navigator.locks.request(
129
+ name,
130
+ acquireTimeout === 0
131
+ ? {
132
+ mode: 'exclusive',
133
+ ifAvailable: true,
134
+ }
135
+ : {
136
+ mode: 'exclusive',
137
+ signal: abortController.signal,
138
+ },
139
+ async (lock) => {
140
+ if (lock) {
141
+ if (internals.debug) {
142
+ console.log('@supabase/gotrue-js: navigatorLock: acquired', name, lock.name)
143
+ }
144
+
145
+ try {
146
+ return await fn()
147
+ } finally {
148
+ if (internals.debug) {
149
+ console.log('@supabase/gotrue-js: navigatorLock: released', name, lock.name)
150
+ }
151
+ }
152
+ } else {
153
+ if (acquireTimeout === 0) {
154
+ if (internals.debug) {
155
+ console.log('@supabase/gotrue-js: navigatorLock: not immediately available', name)
156
+ }
157
+
158
+ throw new NavigatorLockAcquireTimeoutError(
159
+ `Acquiring an exclusive Navigator LockManager lock "${name}" immediately failed`
160
+ )
161
+ } else {
162
+ if (internals.debug) {
163
+ try {
164
+ const result = await globalThis.navigator.locks.query()
165
+
166
+ console.log(
167
+ '@supabase/gotrue-js: Navigator LockManager state',
168
+ JSON.stringify(result, null, ' ')
169
+ )
170
+ } catch (e: any) {
171
+ console.warn(
172
+ '@supabase/gotrue-js: Error when querying Navigator LockManager state',
173
+ e
174
+ )
175
+ }
176
+ }
177
+
178
+ // Browser is not following the Navigator LockManager spec, it
179
+ // returned a null lock when we didn't use ifAvailable. So we can
180
+ // pretend the lock is acquired in the name of backward compatibility
181
+ // and user experience and just run the function.
182
+ console.warn(
183
+ '@supabase/gotrue-js: Navigator LockManager returned a null lock when using #request without ifAvailable set to true, it appears this browser is not following the LockManager spec https://developer.mozilla.org/en-US/docs/Web/API/LockManager/request'
184
+ )
185
+
186
+ return await fn()
187
+ }
188
+ }
189
+ }
190
+ )
191
+ } catch (e: any) {
192
+ if (e?.name === 'AbortError' && acquireTimeout > 0) {
193
+ // The lock acquisition was aborted because the timeout fired while the
194
+ // request was still pending. This typically means another lock holder is
195
+ // not releasing the lock, possibly due to React Strict Mode's
196
+ // double-mount/unmount behavior or a component unmounting mid-operation,
197
+ // leaving an orphaned lock.
198
+ //
199
+ // Recovery: use { steal: true } to forcefully acquire the lock. Per the
200
+ // Web Locks API spec, this releases any currently held lock with the same
201
+ // name and grants the request immediately, preempting any queued requests.
202
+ // The previous holder's callback continues running to completion but no
203
+ // longer holds the lock for exclusion purposes.
204
+ //
205
+ // See: https://github.com/supabase/supabase/issues/42505
206
+ if (internals.debug) {
207
+ console.log(
208
+ '@supabase/gotrue-js: navigatorLock: acquire timeout, recovering by stealing lock',
209
+ name
210
+ )
211
+ }
212
+
213
+ console.warn(
214
+ `@supabase/gotrue-js: Lock "${name}" was not released within ${acquireTimeout}ms. ` +
215
+ 'This may indicate an orphaned lock from a component unmount (e.g., React Strict Mode). ' +
216
+ 'Forcefully acquiring the lock to recover.'
217
+ )
218
+
219
+ return await Promise.resolve().then(() =>
220
+ globalThis.navigator.locks.request(
221
+ name,
222
+ {
223
+ mode: 'exclusive',
224
+ steal: true,
225
+ },
226
+ async (lock) => {
227
+ if (lock) {
228
+ if (internals.debug) {
229
+ console.log(
230
+ '@supabase/gotrue-js: navigatorLock: recovered (stolen)',
231
+ name,
232
+ lock.name
233
+ )
234
+ }
235
+
236
+ try {
237
+ return await fn()
238
+ } finally {
239
+ if (internals.debug) {
240
+ console.log(
241
+ '@supabase/gotrue-js: navigatorLock: released (stolen)',
242
+ name,
243
+ lock.name
244
+ )
245
+ }
246
+ }
247
+ } else {
248
+ // This should not happen with steal: true, but handle gracefully.
249
+ console.warn(
250
+ '@supabase/gotrue-js: Navigator LockManager returned null lock even with steal: true'
251
+ )
252
+ return await fn()
253
+ }
254
+ }
255
+ )
256
+ )
257
+ }
258
+
259
+ throw e
260
+ }
261
+ }
262
+
263
+ const PROCESS_LOCKS: { [name: string]: Promise<any> } = {}
264
+
265
+ /**
266
+ * Implements a global exclusive lock that works only in the current process.
267
+ * Useful for environments like React Native or other non-browser
268
+ * single-process (i.e. no concept of "tabs") environments.
269
+ *
270
+ * Use {@link #navigatorLock} in browser environments.
271
+ *
272
+ * @param name Name of the lock to be acquired.
273
+ * @param acquireTimeout If negative, no timeout. If 0 an error is thrown if
274
+ * the lock can't be acquired without waiting. If positive, the lock acquire
275
+ * will time out after so many milliseconds. An error is
276
+ * a timeout if it has `isAcquireTimeout` set to true.
277
+ * @param fn The operation to run once the lock is acquired.
278
+ * @example
279
+ * ```ts
280
+ * await processLock('migrate', 5000, async () => {
281
+ * await runMigration()
282
+ * })
283
+ * ```
284
+ */
285
+ export async function processLock<R>(
286
+ name: string,
287
+ acquireTimeout: number,
288
+ fn: () => Promise<R>
289
+ ): Promise<R> {
290
+ const previousOperation = PROCESS_LOCKS[name] ?? Promise.resolve()
291
+
292
+ // Wrap previousOperation to handle errors without using .catch()
293
+ // This avoids Firefox content script security errors
294
+ const previousOperationHandled = (async () => {
295
+ try {
296
+ await previousOperation
297
+ return null
298
+ } catch (e) {
299
+ // ignore error of previous operation that we're waiting to finish
300
+ return null
301
+ }
302
+ })()
303
+
304
+ const currentOperation = (async () => {
305
+ let timeoutId: ReturnType<typeof setTimeout> | null = null
306
+
307
+ try {
308
+ // Wait for either previous operation or timeout
309
+ const timeoutPromise =
310
+ acquireTimeout >= 0
311
+ ? new Promise((_, reject) => {
312
+ timeoutId = setTimeout(() => {
313
+ console.warn(
314
+ `@supabase/gotrue-js: Lock "${name}" acquisition timed out after ${acquireTimeout}ms. ` +
315
+ 'This may be caused by another operation holding the lock. ' +
316
+ 'Consider increasing lockAcquireTimeout or checking for stuck operations.'
317
+ )
318
+
319
+ reject(
320
+ new ProcessLockAcquireTimeoutError(
321
+ `Acquiring process lock with name "${name}" timed out`
322
+ )
323
+ )
324
+ }, acquireTimeout)
325
+ })
326
+ : null
327
+
328
+ await Promise.race([previousOperationHandled, timeoutPromise].filter((x) => x))
329
+
330
+ // If we reach here, previousOperationHandled won the race
331
+ // Clear the timeout to prevent false warnings
332
+ if (timeoutId !== null) {
333
+ clearTimeout(timeoutId)
334
+ }
335
+ } catch (e: any) {
336
+ // Clear the timeout on error path as well
337
+ if (timeoutId !== null) {
338
+ clearTimeout(timeoutId)
339
+ }
340
+
341
+ // Re-throw timeout errors, ignore others
342
+ if (e && e.isAcquireTimeout) {
343
+ throw e
344
+ }
345
+ // Fall through to run fn() - previous operation finished with error
346
+ }
347
+
348
+ // Previous operations finished and we didn't get a race on the acquire
349
+ // timeout, so the current operation can finally start
350
+ return await fn()
351
+ })()
352
+
353
+ PROCESS_LOCKS[name] = (async () => {
354
+ try {
355
+ return await currentOperation
356
+ } catch (e: any) {
357
+ if (e && e.isAcquireTimeout) {
358
+ // if the current operation timed out, it doesn't mean that the previous
359
+ // operation finished, so we need continue waiting for it to finish
360
+ try {
361
+ await previousOperation
362
+ } catch (prevError) {
363
+ // Ignore previous operation errors
364
+ }
365
+ return null
366
+ }
367
+
368
+ throw e
369
+ }
370
+ })()
371
+
372
+ // finally wait for the current operation to finish successfully, with an
373
+ // error or with an acquire timeout error
374
+ return await currentOperation
375
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * https://mathiasbynens.be/notes/globalthis
3
+ */
4
+ export function polyfillGlobalThis() {
5
+ if (typeof globalThis === 'object') return
6
+ try {
7
+ Object.defineProperty(Object.prototype, '__magic__', {
8
+ get: function () {
9
+ return this
10
+ },
11
+ configurable: true,
12
+ })
13
+ // @ts-expect-error 'Allow access to magic'
14
+ __magic__.globalThis = __magic__
15
+ // @ts-expect-error 'Allow access to magic'
16
+ delete Object.prototype.__magic__
17
+ } catch (e) {
18
+ if (typeof self !== 'undefined') {
19
+ // @ts-expect-error 'Allow access to globals'
20
+ self.globalThis = self
21
+ }
22
+ }
23
+ }