diff --git a/src/io/browser/browser-io.ts b/src/io/browser/browser-io.ts index 80fa556e8..de399f664 100644 --- a/src/io/browser/browser-io.ts +++ b/src/io/browser/browser-io.ts @@ -3,7 +3,7 @@ import { makeLocalStorageDisklet } from 'disklet' import { LogBackend, makeLog } from '../../core/log/log' import { EdgeFetchOptions, EdgeFetchResponse, EdgeIo } from '../../types/types' import { scrypt } from '../../util/crypto/scrypt' -import { initMixFetch, queueMixFetch } from '../../util/nym' +import { queueMixFetch } from '../../util/nym' import { fetchCorsProxy } from './fetch-cors-proxy' // Only try CORS proxy/bridge techniques up to 5 times @@ -49,13 +49,16 @@ export function makeBrowserIo(logBackend: LogBackend): EdgeIo { const { corsBypass = 'auto', privacy = 'none' } = opts ?? {} if (privacy === 'nym') { - // Ensure mixFetch is initialized before use - await initMixFetch(log) // Use queued fetch to handle mixFetch's one-request-per-host limitation - return await queueMixFetch(uri, { - ...opts, - mode: 'unsafe-ignore-cors' as RequestMode - }) + // initMixFetch is called within queueMixFetch + return await queueMixFetch( + uri, + { + ...opts, + mode: 'unsafe-ignore-cors' as RequestMode + }, + log + ) } if (corsBypass === 'always') { return await fetchCorsProxy(uri, opts) diff --git a/src/io/react-native/react-native-worker.ts b/src/io/react-native/react-native-worker.ts index a71b31a18..86be60095 100644 --- a/src/io/react-native/react-native-worker.ts +++ b/src/io/react-native/react-native-worker.ts @@ -17,7 +17,7 @@ import { EdgeFetchResponse, EdgeIo } from '../../types/types' -import { initMixFetch, queueMixFetch } from '../../util/nym' +import { queueMixFetch } from '../../util/nym' import { hideProperties } from '../hidden-properties' import { makeNativeBridge } from './native-bridge' import { WorkerApi, YAOB_THROTTLE_MS } from './react-native-types' @@ -176,13 +176,16 @@ async function makeIo(logBackend: LogBackend): Promise { const { corsBypass = 'auto', privacy = 'none' } = opts ?? {} if (privacy === 'nym') { - // Ensure mixFetch is initialized before use - await initMixFetch(log) // Use queued fetch to handle mixFetch's one-request-per-host limitation - const response = await queueMixFetch(uri, { - ...opts, - mode: 'unsafe-ignore-cors' as RequestMode - }) + // initMixFetch is called within queueMixFetch + const response = await queueMixFetch( + uri, + { + ...opts, + mode: 'unsafe-ignore-cors' as RequestMode + }, + log + ) return response } if (corsBypass === 'always') { diff --git a/src/util/nym.ts b/src/util/nym.ts index 23faad2d5..8e700ca78 100644 --- a/src/util/nym.ts +++ b/src/util/nym.ts @@ -69,7 +69,8 @@ export async function initMixFetch(log: EdgeLog): Promise { */ export async function queueMixFetch( uri: string, - opts: RequestInit & { mode?: string } + opts: RequestInit & { mode?: string }, + log: EdgeLog ): Promise { const hostKey = getHostKey(uri) @@ -79,7 +80,11 @@ export async function queueMixFetch( // Chain our request after the previous one const ourWork = previousChain .catch(() => {}) // Ignore errors from previous request - .then(async () => await mixFetch(uri, opts, mixFetchOptions)) + .then(async () => { + // Initialize mixFetch on first use (cached for subsequent calls) + const nymMixFetch = await initMixFetch(log) + return await nymMixFetch(uri, opts, mixFetchOptions) + }) .finally(() => { // Clean up if we're still the chain tail if (hostRequestChains.get(hostKey) === ourWork) {