@meshconnect/uwc-injected-connector 0.27.5 → 0.28.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 (30) hide show
  1. package/dist/injected-connector.d.ts.map +1 -1
  2. package/dist/injected-connector.js +62 -47
  3. package/dist/injected-connector.js.map +1 -1
  4. package/dist/services/connection-manager.d.ts +17 -13
  5. package/dist/services/connection-manager.d.ts.map +1 -1
  6. package/dist/services/connection-manager.js +93 -65
  7. package/dist/services/connection-manager.js.map +1 -1
  8. package/dist/services/solana/solana-wallet-service.d.ts +2 -0
  9. package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
  10. package/dist/services/solana/solana-wallet-service.js +1 -0
  11. package/dist/services/solana/solana-wallet-service.js.map +1 -1
  12. package/dist/services/ton/ton-wallet-service.d.ts +7 -1
  13. package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
  14. package/dist/services/ton/ton-wallet-service.js +21 -1
  15. package/dist/services/ton/ton-wallet-service.js.map +1 -1
  16. package/dist/utils/secondary-prompt-gap.d.ts +47 -0
  17. package/dist/utils/secondary-prompt-gap.d.ts.map +1 -0
  18. package/dist/utils/secondary-prompt-gap.js +65 -0
  19. package/dist/utils/secondary-prompt-gap.js.map +1 -0
  20. package/package.json +6 -6
  21. package/src/injected-connector.test.ts +374 -7
  22. package/src/injected-connector.ts +100 -55
  23. package/src/services/connection-manager.test.ts +841 -171
  24. package/src/services/connection-manager.ts +157 -86
  25. package/src/services/solana/solana-wallet-service.check-existing.test.ts +24 -0
  26. package/src/services/solana/solana-wallet-service.ts +6 -1
  27. package/src/services/ton/ton-wallet-service.test.ts +69 -0
  28. package/src/services/ton/ton-wallet-service.ts +22 -1
  29. package/src/utils/secondary-prompt-gap.test.ts +288 -0
  30. package/src/utils/secondary-prompt-gap.ts +95 -0
@@ -0,0 +1,288 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import {
3
+ beforeSecondaryPrompt,
4
+ markPrompted,
5
+ runSecondaryLeg,
6
+ type SecondaryLeg,
7
+ waitBeforeSecondaryPrompt
8
+ } from './secondary-prompt-gap'
9
+
10
+ const legOf = (controller: AbortController): SecondaryLeg => ({
11
+ signal: controller.signal,
12
+ prompted: false
13
+ })
14
+
15
+ describe('waitBeforeSecondaryPrompt', () => {
16
+ beforeEach(() => {
17
+ vi.useFakeTimers()
18
+ })
19
+ afterEach(() => {
20
+ vi.useRealTimers()
21
+ })
22
+
23
+ it.each([0, -1, Number.NaN])('does not wait for a gap of %d', async gap => {
24
+ await expect(waitBeforeSecondaryPrompt(gap)).resolves.toBeUndefined()
25
+ expect(vi.getTimerCount()).toBe(0)
26
+ })
27
+
28
+ it('waits the whole gap, and no longer', async () => {
29
+ const done = vi.fn()
30
+ const pending = waitBeforeSecondaryPrompt(1500).then(done)
31
+
32
+ await vi.advanceTimersByTimeAsync(1499)
33
+ expect(done).not.toHaveBeenCalled()
34
+ await vi.advanceTimersByTimeAsync(1)
35
+ await pending
36
+ expect(done).toHaveBeenCalledTimes(1)
37
+ })
38
+
39
+ it('waits with a live signal and leaves no abort listener behind', async () => {
40
+ const controller = new AbortController()
41
+ const remove = vi.spyOn(controller.signal, 'removeEventListener')
42
+ const pending = waitBeforeSecondaryPrompt(1500, controller.signal)
43
+
44
+ await vi.advanceTimersByTimeAsync(1500)
45
+ await expect(pending).resolves.toBeUndefined()
46
+ expect(remove).toHaveBeenCalledWith('abort', expect.any(Function))
47
+ // An abort after the wait has nothing left to reject or clear.
48
+ expect(() => controller.abort()).not.toThrow()
49
+ })
50
+
51
+ it('rejects at once, without waiting, when the signal is already aborted', async () => {
52
+ const controller = new AbortController()
53
+ const reason = new Error('gather abandoned')
54
+ controller.abort(reason)
55
+
56
+ await expect(
57
+ waitBeforeSecondaryPrompt(1500, controller.signal)
58
+ ).rejects.toBe(reason)
59
+ expect(vi.getTimerCount()).toBe(0)
60
+ })
61
+
62
+ it('rejects an already-aborted signal at a gap of 0 too', async () => {
63
+ const controller = new AbortController()
64
+ controller.abort()
65
+
66
+ await expect(
67
+ waitBeforeSecondaryPrompt(0, controller.signal)
68
+ ).rejects.toMatchObject({ name: 'AbortError' })
69
+ })
70
+
71
+ it('rejects with the reason when aborted during the wait, and clears its timer', async () => {
72
+ const controller = new AbortController()
73
+ const reason = new Error('deadline')
74
+ const pending = waitBeforeSecondaryPrompt(1500, controller.signal)
75
+ const settled = vi.fn()
76
+ pending.then(settled, settled)
77
+
78
+ await vi.advanceTimersByTimeAsync(500)
79
+ expect(vi.getTimerCount()).toBe(1)
80
+ controller.abort(reason)
81
+
82
+ await expect(pending).rejects.toBe(reason)
83
+ expect(vi.getTimerCount()).toBe(0)
84
+ expect(settled).toHaveBeenCalledTimes(1)
85
+ })
86
+ })
87
+
88
+ describe('markPrompted', () => {
89
+ it('marks a live leg as prompted', () => {
90
+ const leg = legOf(new AbortController())
91
+ markPrompted(leg)
92
+ expect(leg.prompted).toBe(true)
93
+ })
94
+
95
+ it('throws for an abandoned leg and leaves it unprompted', () => {
96
+ const controller = new AbortController()
97
+ const reason = new Error('deadline')
98
+ controller.abort(reason)
99
+ const leg = legOf(controller)
100
+
101
+ expect(() => markPrompted(leg)).toThrow(reason)
102
+ expect(leg.prompted).toBe(false)
103
+ })
104
+
105
+ it('is a no-op without a leg', () => {
106
+ expect(() => markPrompted(undefined)).not.toThrow()
107
+ })
108
+ })
109
+
110
+ describe('beforeSecondaryPrompt', () => {
111
+ beforeEach(() => {
112
+ vi.useFakeTimers()
113
+ })
114
+ afterEach(() => {
115
+ vi.useRealTimers()
116
+ })
117
+
118
+ it('marks the leg prompted only once the gap has passed', async () => {
119
+ const leg = legOf(new AbortController())
120
+ const pending = beforeSecondaryPrompt(leg, 1500)
121
+
122
+ await vi.advanceTimersByTimeAsync(1499)
123
+ expect(leg.prompted).toBe(false)
124
+ await vi.advanceTimersByTimeAsync(1)
125
+ await pending
126
+ expect(leg.prompted).toBe(true)
127
+ })
128
+
129
+ it('marks the leg at once for a gap of 0', async () => {
130
+ const leg = legOf(new AbortController())
131
+ await beforeSecondaryPrompt(leg, 0)
132
+ expect(leg.prompted).toBe(true)
133
+ })
134
+
135
+ it('rejects and leaves the leg unprompted when abandoned during the gap', async () => {
136
+ const controller = new AbortController()
137
+ const leg = legOf(controller)
138
+ const pending = beforeSecondaryPrompt(leg, 1500)
139
+
140
+ await vi.advanceTimersByTimeAsync(700)
141
+ controller.abort(new Error('deadline'))
142
+
143
+ await expect(pending).rejects.toThrow('deadline')
144
+ expect(leg.prompted).toBe(false)
145
+ })
146
+
147
+ it('rejects an abandoned leg at a gap of 0 (no request after the deadline)', async () => {
148
+ const controller = new AbortController()
149
+ controller.abort(new Error('deadline'))
150
+ const leg = legOf(controller)
151
+
152
+ await expect(beforeSecondaryPrompt(leg, 0)).rejects.toThrow('deadline')
153
+ expect(leg.prompted).toBe(false)
154
+ })
155
+
156
+ it('still waits without a leg', async () => {
157
+ const done = vi.fn()
158
+ const pending = beforeSecondaryPrompt(undefined, 1000).then(done)
159
+
160
+ await vi.advanceTimersByTimeAsync(999)
161
+ expect(done).not.toHaveBeenCalled()
162
+ await vi.advanceTimersByTimeAsync(1)
163
+ await pending
164
+ expect(done).toHaveBeenCalledTimes(1)
165
+ })
166
+ })
167
+
168
+ describe('runSecondaryLeg', () => {
169
+ beforeEach(() => {
170
+ vi.useFakeTimers()
171
+ })
172
+ afterEach(() => {
173
+ vi.useRealTimers()
174
+ })
175
+
176
+ it('hands the run a live, unprompted leg', async () => {
177
+ let seen: SecondaryLeg | undefined
178
+ await runSecondaryLeg(1000, async leg => {
179
+ seen = leg
180
+ })
181
+ expect(seen?.signal.aborted).toBe(false)
182
+ expect(seen?.prompted).toBe(false)
183
+ })
184
+
185
+ it('returns the value of a run that read an existing authorization', async () => {
186
+ await expect(
187
+ runSecondaryLeg(1000, async () => 'ok' as const)
188
+ ).resolves.toEqual({ status: 'settled', value: 'ok', prompted: false })
189
+ })
190
+
191
+ it('reports a run that sent a prompting request as prompted', async () => {
192
+ await expect(
193
+ runSecondaryLeg(1000, async leg => {
194
+ markPrompted(leg)
195
+ return 'addr'
196
+ })
197
+ ).resolves.toEqual({ status: 'settled', value: 'addr', prompted: true })
198
+ })
199
+
200
+ it('keeps an undefined value as settled, not timed out', async () => {
201
+ await expect(runSecondaryLeg(1000, async () => undefined)).resolves.toEqual(
202
+ { status: 'settled', value: undefined, prompted: false }
203
+ )
204
+ })
205
+
206
+ it('returns failed with the error for a run that rejects before the deadline', async () => {
207
+ const error = new Error('declined')
208
+ await expect(
209
+ runSecondaryLeg(1000, async leg => {
210
+ markPrompted(leg)
211
+ throw error
212
+ })
213
+ ).resolves.toEqual({ status: 'failed', error, prompted: true })
214
+ })
215
+
216
+ it('times out at the deadline, aborts the leg, and keeps what it prompted', async () => {
217
+ let seen: SecondaryLeg | undefined
218
+ const pending = runSecondaryLeg(1000, leg => {
219
+ seen = leg
220
+ markPrompted(leg)
221
+ return new Promise<never>(() => {})
222
+ })
223
+
224
+ await vi.advanceTimersByTimeAsync(999)
225
+ expect(seen?.signal.aborted).toBe(false)
226
+ await vi.advanceTimersByTimeAsync(1)
227
+
228
+ await expect(pending).resolves.toEqual({
229
+ status: 'timedOut',
230
+ prompted: true
231
+ })
232
+ expect(seen?.signal.aborted).toBe(true)
233
+ })
234
+
235
+ it('never sends the request of a leg still waiting out its gap at the deadline', async () => {
236
+ const request = vi.fn().mockResolvedValue('addr')
237
+ const pending = runSecondaryLeg(1000, leg =>
238
+ beforeSecondaryPrompt(leg, 1500).then(request)
239
+ )
240
+
241
+ await vi.advanceTimersByTimeAsync(1000)
242
+ await expect(pending).resolves.toEqual({
243
+ status: 'timedOut',
244
+ prompted: false
245
+ })
246
+ await vi.advanceTimersByTimeAsync(5000)
247
+ expect(request).not.toHaveBeenCalled()
248
+ })
249
+
250
+ it('swallows a rejection that lands after the deadline', async () => {
251
+ let reject: (error: Error) => void = () => {}
252
+ const pending = runSecondaryLeg(
253
+ 1000,
254
+ () =>
255
+ new Promise<never>((_, rej) => {
256
+ reject = rej
257
+ })
258
+ )
259
+
260
+ await vi.advanceTimersByTimeAsync(1000)
261
+ await expect(pending).resolves.toEqual({
262
+ status: 'timedOut',
263
+ prompted: false
264
+ })
265
+ reject(new Error('late'))
266
+ await vi.advanceTimersByTimeAsync(0)
267
+ })
268
+
269
+ it('runs without a deadline for a non-positive timeout', async () => {
270
+ let seen: SecondaryLeg | undefined
271
+ const pending = runSecondaryLeg(
272
+ 0,
273
+ leg =>
274
+ new Promise<string>(resolve => {
275
+ seen = leg
276
+ setTimeout(() => resolve('late'), 60_000)
277
+ })
278
+ )
279
+
280
+ await vi.advanceTimersByTimeAsync(60_000)
281
+ await expect(pending).resolves.toEqual({
282
+ status: 'settled',
283
+ value: 'late',
284
+ prompted: false
285
+ })
286
+ expect(seen?.signal.aborted).toBe(false)
287
+ })
288
+ })
@@ -0,0 +1,95 @@
1
+ import { withDeadline } from './with-deadline'
2
+
3
+ /**
4
+ * Waits `secondaryPromptGapMs` (see `ConnectorConnectOptions`) before a
5
+ * secondary request that can open a wallet prompt. No wait for 0 or less.
6
+ *
7
+ * Rejects with the signal's reason once the gather is abandoned (its deadline
8
+ * passed), before the wait or during it: an abandoned leg must not go on to
9
+ * open a prompt after the connect has moved on.
10
+ */
11
+ export async function waitBeforeSecondaryPrompt(
12
+ gapMs: number,
13
+ signal?: AbortSignal
14
+ ): Promise<void> {
15
+ signal?.throwIfAborted()
16
+ if (!(gapMs > 0)) return
17
+ await new Promise<void>((resolve, reject) => {
18
+ const timer = setTimeout(() => {
19
+ signal?.removeEventListener('abort', onAbort)
20
+ resolve()
21
+ }, gapMs)
22
+ function onAbort(): void {
23
+ clearTimeout(timer)
24
+ reject(signal?.reason)
25
+ }
26
+ signal?.addEventListener('abort', onAbort, { once: true })
27
+ })
28
+ }
29
+
30
+ /**
31
+ * One secondary gather leg. `signal` aborts at the leg's deadline; `prompted`
32
+ * turns true once the leg sends a request that can open the wallet's prompt,
33
+ * as opposed to reading an existing authorization.
34
+ */
35
+ export interface SecondaryLeg {
36
+ readonly signal: AbortSignal
37
+ prompted: boolean
38
+ }
39
+
40
+ /**
41
+ * Right before a leg's request that can open a prompt: throws if the leg was
42
+ * abandoned, then marks it as prompted.
43
+ */
44
+ export function markPrompted(leg: SecondaryLeg | undefined): void {
45
+ leg?.signal.throwIfAborted()
46
+ if (leg) leg.prompted = true
47
+ }
48
+
49
+ /**
50
+ * Right before a secondary request that can open a prompt: waits `gapMs` (see
51
+ * `waitBeforeSecondaryPrompt`), then marks the leg as prompted.
52
+ */
53
+ export async function beforeSecondaryPrompt(
54
+ leg: SecondaryLeg | undefined,
55
+ gapMs: number
56
+ ): Promise<void> {
57
+ await waitBeforeSecondaryPrompt(gapMs, leg?.signal)
58
+ markPrompted(leg)
59
+ }
60
+
61
+ /** How one secondary leg ended, and whether it sent a request that can prompt. */
62
+ export type SecondaryLegResult<T> = { prompted: boolean } & (
63
+ | { status: 'settled'; value: T }
64
+ | { status: 'timedOut' }
65
+ | { status: 'failed'; error: unknown }
66
+ )
67
+
68
+ /**
69
+ * Runs one secondary leg under `withDeadline`. At the deadline the leg's
70
+ * signal aborts, so a leg still waiting out its gap or discovering its provider
71
+ * sends nothing afterwards. Never rejects: a throw comes back as `failed`.
72
+ */
73
+ export async function runSecondaryLeg<T>(
74
+ timeoutMs: number,
75
+ run: (leg: SecondaryLeg) => Promise<T>
76
+ ): Promise<SecondaryLegResult<T>> {
77
+ const abandoned = new AbortController()
78
+ const leg: SecondaryLeg = { signal: abandoned.signal, prompted: false }
79
+ let timedOut = false
80
+ try {
81
+ const value = await withDeadline(
82
+ () => run(leg),
83
+ timeoutMs,
84
+ () => {
85
+ timedOut = true
86
+ abandoned.abort()
87
+ }
88
+ )
89
+ return timedOut
90
+ ? { status: 'timedOut', prompted: leg.prompted }
91
+ : { status: 'settled', value: value as T, prompted: leg.prompted }
92
+ } catch (error) {
93
+ return { status: 'failed', error, prompted: leg.prompted }
94
+ }
95
+ }