Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions src/io/browser/browser-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ export function makeBrowserIo(logBackend: LogBackend): EdgeIo {

if (privacy === 'nym') {
// Ensure mixFetch is initialized before use
await initMixFetch(log)
const nymMixFetch = 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
})
return await queueMixFetch(
uri,
{
...opts,
mode: 'unsafe-ignore-cors' as RequestMode
},
nymMixFetch
)
}
if (corsBypass === 'always') {
return await fetchCorsProxy(uri, opts)
Expand Down
14 changes: 9 additions & 5 deletions src/io/react-native/react-native-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,16 @@ async function makeIo(logBackend: LogBackend): Promise<EdgeIo> {

if (privacy === 'nym') {
// Ensure mixFetch is initialized before use
await initMixFetch(log)
const nymMixFetch = 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
})
const response = await queueMixFetch(
uri,
{
...opts,
mode: 'unsafe-ignore-cors' as RequestMode
},
nymMixFetch
)
return response
}
if (corsBypass === 'always') {
Expand Down
12 changes: 10 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 },
instance?: IMixFetch

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a parameter for dependency injection, just initialize the instance within the nym.ts module. Have the queue function await the single initialization promise.

): Promise<Response> {
const hostKey = getHostKey(uri)

Expand All @@ -79,7 +80,14 @@ 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 () => {
// Use the provided instance if available (properly initialized context)
// Otherwise fall back to the module-level function
if (instance != null) {
return await instance(uri, opts, mixFetchOptions)
}
return await mixFetch(uri, opts, mixFetchOptions)
})
.finally(() => {
// Clean up if we're still the chain tail
if (hostRequestChains.get(hostKey) === ourWork) {
Expand Down