Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/io/browser/browser-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions src/io/react-native/react-native-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -176,13 +176,16 @@ async function makeIo(logBackend: LogBackend): Promise<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
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') {
Expand Down
9 changes: 7 additions & 2 deletions src/util/nym.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export async function initMixFetch(log: EdgeLog): Promise<IMixFetch> {
*/
export async function queueMixFetch(
uri: string,
opts: RequestInit & { mode?: string }
opts: RequestInit & { mode?: string },
log: EdgeLog
): Promise<Response> {
const hostKey = getHostKey(uri)

Expand All @@ -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) {
Expand Down