|
1 | 1 | import { |
2 | 2 | createMixFetch, |
3 | | - disconnectMixTunnel, |
4 | | - SetupMixTunnelOpts |
| 3 | + disconnectMixFetch, |
| 4 | + IMixFetch, |
| 5 | + IMixFetchFn, |
| 6 | + SetupMixFetchOps |
5 | 7 | } from '@nymproject/mix-fetch' |
6 | 8 |
|
7 | 9 | import { EdgeLog } from '../types/types' |
8 | 10 |
|
9 | | -/** The fetch-bound function `createMixFetch` resolves to. */ |
10 | | -type MixFetchFn = (url: string, init?: RequestInit) => Promise<Response> |
11 | | - |
12 | 11 | /** |
13 | | - * Configuration options for the NYM mixFetch tunnel. |
| 12 | + * Configuration options for the NYM mixFetch client. |
14 | 13 | */ |
15 | | -export const mixFetchOptions: SetupMixTunnelOpts = { |
| 14 | +export const mixFetchOptions: SetupMixFetchOps = { |
16 | 15 | clientId: 'edge-core-js-2026-03-10', |
| 16 | + preferredGateway: '5rXcNe2a44vXisK3uqLHCzpzvEwcnsijDMU7hg4fcYk8', // with WSS |
| 17 | + preferredNetworkRequester: |
| 18 | + '5x6q9UfVHs5AohKMUqeivj7a556kVVy7QwoKige8xHxh.6CFoB3kJaDbYz6oafPJxNxNjzahpT2NtgtytcSyN9EvF@5rXcNe2a44vXisK3uqLHCzpzvEwcnsijDMU7hg4fcYk8', |
17 | 19 | forceTls: true, // force WSS |
18 | | - // Mixnet round trips are slow, so give the tunnel handshake plenty of time. |
19 | | - // v1 tuned a 5 min `requestTimeoutMs`; v2 exposes no per-request timeout, but |
20 | | - // the tunnel setup is where mixnet latency bites, so restore that 5 min |
21 | | - // budget here to avoid premature failures during the handshake. |
22 | | - connectTimeoutMs: 300000 |
| 20 | + mixFetchOverride: { |
| 21 | + requestTimeoutMs: 300000 |
| 22 | + } |
23 | 23 | } |
24 | 24 |
|
| 25 | +/** |
| 26 | + * Budget for `createMixFetch` itself (client start + gateway handshake). |
| 27 | + * |
| 28 | + * A healthy setup with the pinned gateway completes in under 10s measured. |
| 29 | + * Without a bound here the whole app blocks on the first mixnet request for |
| 30 | + * as long as a dead gateway keeps us waiting, which reads to the user as a |
| 31 | + * freeze. |
| 32 | + */ |
| 33 | +const SETUP_TIMEOUT_MS = 60000 |
| 34 | + |
25 | 35 | // MixFetch initialization state |
26 | | -let mixFetchInitPromise: Promise<MixFetchFn> | null = null |
| 36 | +let mixFetchInitPromise: Promise<IMixFetch> | null = null |
27 | 37 |
|
28 | 38 | /** |
29 | 39 | * Initialize the NYM mixFetch client. Must be called before using mixFetch. |
30 | 40 | * Safe to call multiple times - subsequent calls return the same promise. |
31 | 41 | */ |
32 | | -export async function initMixFetch(log: EdgeLog): Promise<MixFetchFn> { |
| 42 | +export async function initMixFetch(log: EdgeLog): Promise<IMixFetchFn> { |
33 | 43 | if (mixFetchInitPromise == null) { |
34 | 44 | log('Initializing mixFetch...') |
35 | | - mixFetchInitPromise = createMixFetch(mixFetchOptions) |
36 | | - .then(mixFetch => { |
| 45 | + const pending = createMixFetch(mixFetchOptions) |
| 46 | + // The timeout below can abandon this setup while it is still in flight. |
| 47 | + // Deliberately do NOT tear it down on late completion: `createMixFetch` |
| 48 | + // resolves to a healthy global singleton, and disconnecting it (a |
| 49 | + // process-wide operation) would race a newer init that has taken over. |
| 50 | + // A late completion just repopulates `__mixFetchGlobal`, which the next |
| 51 | + // init reuses. Swallow a late rejection so it is not unhandled. |
| 52 | + pending.catch(() => {}) |
| 53 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 54 | + const timeout = new Promise<never>((resolve, reject) => { |
| 55 | + timer = setTimeout(() => { |
| 56 | + reject( |
| 57 | + new Error(`mixFetch setup timed out after ${SETUP_TIMEOUT_MS}ms`) |
| 58 | + ) |
| 59 | + }, SETUP_TIMEOUT_MS) |
| 60 | + }) |
| 61 | + mixFetchInitPromise = Promise.race([pending, timeout]) |
| 62 | + .then(mixFetchModule => { |
37 | 63 | log('mixFetch initialized successfully') |
38 | | - return mixFetch |
| 64 | + return mixFetchModule |
39 | 65 | }) |
40 | 66 | .catch(async error => { |
41 | | - // Tear down any partially-established tunnel left by the failed init |
42 | | - // so the next createMixFetch call starts fresh instead of reusing a |
| 67 | + // Clean up stale global state left by the failed init so the |
| 68 | + // next createMixFetch call starts fresh instead of reusing a |
43 | 69 | // broken singleton. |
44 | 70 | try { |
45 | | - await disconnectMixTunnel() |
| 71 | + await disconnectMixFetch() |
46 | 72 | } catch {} |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete |
| 74 | + delete (window as any).__mixFetchGlobal |
47 | 75 | mixFetchInitPromise = null |
48 | 76 | log.error('mixFetch initialization failed:', error) |
49 | 77 | throw error |
50 | 78 | }) |
| 79 | + .finally(() => { |
| 80 | + clearTimeout(timer) |
| 81 | + }) |
51 | 82 | } |
52 | | - return await mixFetchInitPromise |
| 83 | + const mixFetchModule = await mixFetchInitPromise |
| 84 | + return mixFetchModule.mixFetch |
53 | 85 | } |
0 commit comments