@meshconnect/uwc-injected-connector 0.21.0 → 0.22.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 (35) hide show
  1. package/dist/injected-connector.d.ts +18 -1
  2. package/dist/injected-connector.d.ts.map +1 -1
  3. package/dist/injected-connector.js +45 -4
  4. package/dist/injected-connector.js.map +1 -1
  5. package/dist/services/connection-manager.d.ts +67 -1
  6. package/dist/services/connection-manager.d.ts.map +1 -1
  7. package/dist/services/connection-manager.js +130 -18
  8. package/dist/services/connection-manager.js.map +1 -1
  9. package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
  10. package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
  11. package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
  12. package/dist/services/ethereum/ethereum-wallet-service.d.ts +10 -3
  13. package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
  14. package/dist/services/ethereum/ethereum-wallet-service.js +15 -7
  15. package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
  16. package/dist/utils/step-reporter.d.ts +33 -0
  17. package/dist/utils/step-reporter.d.ts.map +1 -0
  18. package/dist/utils/step-reporter.js +54 -0
  19. package/dist/utils/step-reporter.js.map +1 -0
  20. package/dist/utils/with-deadline.d.ts +30 -0
  21. package/dist/utils/with-deadline.d.ts.map +1 -0
  22. package/dist/utils/with-deadline.js +67 -0
  23. package/dist/utils/with-deadline.js.map +1 -0
  24. package/package.json +5 -5
  25. package/src/injected-connector.test.ts +97 -0
  26. package/src/injected-connector.ts +68 -5
  27. package/src/services/connection-manager.test.ts +234 -0
  28. package/src/services/connection-manager.ts +187 -28
  29. package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
  30. package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
  31. package/src/services/ethereum/ethereum-wallet-service.ts +28 -8
  32. package/src/utils/step-reporter.test.ts +56 -0
  33. package/src/utils/step-reporter.ts +83 -0
  34. package/src/utils/with-deadline.test.ts +80 -0
  35. package/src/utils/with-deadline.ts +67 -0
@@ -0,0 +1,80 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
2
+ import { withDeadline } from './with-deadline'
3
+
4
+ describe('withDeadline', () => {
5
+ beforeEach(() => {
6
+ vi.useFakeTimers()
7
+ })
8
+ afterEach(() => {
9
+ vi.useRealTimers()
10
+ })
11
+
12
+ it('returns the value when the operation settles before the deadline', async () => {
13
+ const onTimeout = vi.fn()
14
+ const result = await withDeadline(
15
+ () => Promise.resolve('ok'),
16
+ 1000,
17
+ onTimeout
18
+ )
19
+ expect(result).toBe('ok')
20
+ expect(onTimeout).not.toHaveBeenCalled()
21
+ })
22
+
23
+ it('resolves undefined and reports once when the deadline passes', async () => {
24
+ const onTimeout = vi.fn()
25
+ // Never settles — the hang this exists to bound.
26
+ const pending = withDeadline(() => new Promise(() => {}), 1000, onTimeout)
27
+ await vi.advanceTimersByTimeAsync(1000)
28
+ await expect(pending).resolves.toBeUndefined()
29
+ expect(onTimeout).toHaveBeenCalledTimes(1)
30
+ })
31
+
32
+ it('propagates a rejection rather than swallowing it', async () => {
33
+ const onTimeout = vi.fn()
34
+ await expect(
35
+ withDeadline(() => Promise.reject(new Error('boom')), 1000, onTimeout)
36
+ ).rejects.toThrow('boom')
37
+ expect(onTimeout).not.toHaveBeenCalled()
38
+ })
39
+
40
+ it('does not fire onTimeout after a fast operation already settled', async () => {
41
+ const onTimeout = vi.fn()
42
+ await withDeadline(() => Promise.resolve('fast'), 1000, onTimeout)
43
+ // Push well past the deadline — the timer must already be cleared.
44
+ await vi.advanceTimersByTimeAsync(5000)
45
+ expect(onTimeout).not.toHaveBeenCalled()
46
+ })
47
+
48
+ it('leaves the operation unbounded when the timeout is 0 (opt-out)', async () => {
49
+ const onTimeout = vi.fn()
50
+ let settle: (v: string) => void = () => {}
51
+ const pending = withDeadline(
52
+ () => new Promise<string>(resolve => (settle = resolve)),
53
+ 0,
54
+ onTimeout
55
+ )
56
+ await vi.advanceTimersByTimeAsync(60_000)
57
+ settle('late')
58
+ await expect(pending).resolves.toBe('late')
59
+ expect(onTimeout).not.toHaveBeenCalled()
60
+ })
61
+ })
62
+
63
+ describe('withDeadline — onTimeout robustness (ONC-183 review)', () => {
64
+ beforeEach(() => vi.useFakeTimers())
65
+ afterEach(() => vi.useRealTimers())
66
+
67
+ it('still settles when onTimeout throws (must not re-hang)', async () => {
68
+ // A throwing reporter previously left the race pending forever, which is
69
+ // the exact unbounded hang this util exists to prevent.
70
+ const pending = withDeadline(
71
+ () => new Promise(() => {}),
72
+ 20,
73
+ () => {
74
+ throw new Error('sink exploded')
75
+ }
76
+ )
77
+ await vi.advanceTimersByTimeAsync(300)
78
+ await expect(pending).resolves.toBeUndefined()
79
+ })
80
+ })
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Bound a BEST-EFFORT async step so a wallet that never answers can't hold the
3
+ * whole connect open (ONC-183).
4
+ *
5
+ * Resolves rather than rejects on expiry — deliberately. Every call site here
6
+ * is a step whose existing failure mode is already "swallow and continue
7
+ * without this namespace's address"; rejecting would turn a soft degradation
8
+ * into a hard connect failure and change behaviour far beyond the hang fix.
9
+ *
10
+ * The underlying promise is NOT cancelled — a wallet prompt can't be recalled
11
+ * from the dapp side (same contract as core's `raceAbort`). We stop AWAITING
12
+ * it. A late completion still runs its own `.then` work; call sites must
13
+ * therefore tolerate a late write. For the address gathers that is safe: they
14
+ * push into an array core has already copied by then (`ConnectionService`
15
+ * filters `result.availableAddresses` into a fresh array before it reaches the
16
+ * session), so a late push lands on a detached array and cannot corrupt state.
17
+ *
18
+ * `onTimeout` fires exactly once, at the deadline, and only when the step was
19
+ * still pending.
20
+ *
21
+ * Rejection handling is deadline-scoped: a rejection that lands BEFORE the
22
+ * deadline propagates to the caller untouched. Once the deadline has won the
23
+ * race the result is already settled, so a LATER rejection can no longer reach
24
+ * the caller — it is still observed by the `Promise.race` above (so it never
25
+ * surfaces as an unhandled rejection), just no longer reported. That is the
26
+ * intended consequence of abandoning the step, and the reason every call site
27
+ * treats a `timedOut` outcome as terminal for its own bookkeeping.
28
+ */
29
+ export async function withDeadline<T>(
30
+ operation: () => Promise<T>,
31
+ timeoutMs: number,
32
+ onTimeout?: () => void
33
+ ): Promise<T | undefined> {
34
+ // Non-positive disables the bound entirely (the pre-ONC-183 behaviour), so a
35
+ // consumer can opt back out of the deadline without a code change.
36
+ if (!(timeoutMs > 0)) return await operation()
37
+
38
+ let timer: ReturnType<typeof setTimeout> | undefined
39
+ try {
40
+ return await Promise.race([
41
+ operation(),
42
+ new Promise<undefined>(resolve => {
43
+ timer = setTimeout(() => {
44
+ // SWALLOW, don't just `finally`. A throwing `onTimeout` must not
45
+ // leave the race pending forever — that would reintroduce the exact
46
+ // unbounded hang this util exists to prevent — but a bare
47
+ // try/finally would still let the throw escape the timer callback as
48
+ // an uncaught exception. Neither is acceptable from a reporting
49
+ // callback, so the throw dies here and the deadline still settles.
50
+ // Both current call sites pass an already-swallowing reporter; this
51
+ // is defence in depth for the next one.
52
+ try {
53
+ onTimeout?.()
54
+ } catch {
55
+ // A reporter must never affect the deadline's outcome.
56
+ }
57
+ resolve(undefined)
58
+ }, timeoutMs)
59
+ })
60
+ ])
61
+ } finally {
62
+ // Clear on EVERY exit — resolve, reject, or the timeout branch itself — so a
63
+ // fast step never leaves a pending timer that would fire `onTimeout` after
64
+ // the step already settled.
65
+ if (timer) clearTimeout(timer)
66
+ }
67
+ }