|
1 | 1 | import { |
2 | 2 | createMixFetch, |
3 | | - disconnectMixFetch, |
4 | | - IMixFetch, |
5 | | - IMixFetchFn, |
6 | | - SetupMixFetchOps |
| 3 | + disconnectMixTunnel, |
| 4 | + SetupMixTunnelOpts |
7 | 5 | } from '@nymproject/mix-fetch' |
8 | 6 |
|
9 | 7 | import { EdgeLog } from '../types/types' |
10 | 8 |
|
| 9 | +/** The fetch-bound function `createMixFetch` resolves to. */ |
| 10 | +type MixFetchFn = (url: string, init?: RequestInit) => Promise<Response> |
| 11 | + |
11 | 12 | /** |
12 | | - * Configuration options for the NYM mixFetch client. |
| 13 | + * Configuration options for the NYM mixFetch tunnel. |
13 | 14 | */ |
14 | | -export const mixFetchOptions: SetupMixFetchOps = { |
| 15 | +export const mixFetchOptions: SetupMixTunnelOpts = { |
15 | 16 | clientId: 'edge-core-js-2026-03-10', |
16 | | - preferredGateway: '5rXcNe2a44vXisK3uqLHCzpzvEwcnsijDMU7hg4fcYk8', // with WSS |
17 | | - preferredNetworkRequester: |
18 | | - '5x6q9UfVHs5AohKMUqeivj7a556kVVy7QwoKige8xHxh.6CFoB3kJaDbYz6oafPJxNxNjzahpT2NtgtytcSyN9EvF@5rXcNe2a44vXisK3uqLHCzpzvEwcnsijDMU7hg4fcYk8', |
19 | 17 | forceTls: true, // force WSS |
20 | | - mixFetchOverride: { |
21 | | - requestTimeoutMs: 300000 |
22 | | - } |
| 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 |
23 | 23 | } |
24 | 24 |
|
25 | 25 | // MixFetch initialization state |
26 | | -let mixFetchInitPromise: Promise<IMixFetch> | null = null |
| 26 | +let mixFetchInitPromise: Promise<MixFetchFn> | null = null |
27 | 27 |
|
28 | 28 | /** |
29 | 29 | * Initialize the NYM mixFetch client. Must be called before using mixFetch. |
30 | 30 | * Safe to call multiple times - subsequent calls return the same promise. |
31 | 31 | */ |
32 | | -export async function initMixFetch(log: EdgeLog): Promise<IMixFetchFn> { |
| 32 | +export async function initMixFetch(log: EdgeLog): Promise<MixFetchFn> { |
33 | 33 | if (mixFetchInitPromise == null) { |
34 | 34 | log('Initializing mixFetch...') |
35 | 35 | mixFetchInitPromise = createMixFetch(mixFetchOptions) |
36 | | - .then(mixFetchModule => { |
| 36 | + .then(mixFetch => { |
37 | 37 | log('mixFetch initialized successfully') |
38 | | - return mixFetchModule |
| 38 | + return mixFetch |
39 | 39 | }) |
40 | 40 | .catch(async error => { |
41 | | - // Clean up stale global state left by the failed init so the |
42 | | - // next createMixFetch call starts fresh instead of reusing a |
| 41 | + // Tear down any partially-established tunnel left by the failed init |
| 42 | + // so the next createMixFetch call starts fresh instead of reusing a |
43 | 43 | // broken singleton. |
44 | 44 | try { |
45 | | - await disconnectMixFetch() |
| 45 | + await disconnectMixTunnel() |
46 | 46 | } catch {} |
47 | | - // eslint-disable-next-line @typescript-eslint/no-dynamic-delete |
48 | | - delete (window as any).__mixFetchGlobal |
49 | 47 | mixFetchInitPromise = null |
50 | 48 | log.error('mixFetch initialization failed:', error) |
51 | 49 | throw error |
52 | 50 | }) |
53 | 51 | } |
54 | | - const mixFetchModule = await mixFetchInitPromise |
55 | | - return mixFetchModule.mixFetch |
| 52 | + return await mixFetchInitPromise |
56 | 53 | } |
0 commit comments