Skip to content

Commit

Permalink
Add avoidCors API to EdgeFetchFunction
Browse files Browse the repository at this point in the history
This is to be able to to access `nativeFetch` in React Native
environments. This parameter consolidates all fetch modes into one
method.

Setting `auto` (default) will act like old `fetchCors` method.

Setting `always` will act like old `fetch` method.

Setting `never` will forcibly use the CORS-safe fetching implementation
(cors-servers in browser environments and `nativeFetch` in React Native
environments).

In Node environments, `never`, `auto` and `always` are
all idempotent.
  • Loading branch information
samholmes committed Dec 10, 2024
1 parent 7cab5f9 commit e3582dc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Added `fetchCorsForced` for access to nativeFetch in React Native.

## 2.21.0 (2024-12-02)

- added: Added `networkFees` to `EdgeTransaction`.
Expand Down
21 changes: 17 additions & 4 deletions src/io/browser/browser-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ export function makeBrowserIo(): EdgeIo {
}),

// Networking:
fetch(uri: string, opts?: EdgeFetchOptions): Promise<EdgeFetchResponse> {
return window.fetch(uri, opts)
},
async fetchCors(
async fetch(
uri: string,
opts?: EdgeFetchOptions
): Promise<EdgeFetchResponse> {
const { avoidCors = 'auto' } = opts ?? {}

if (avoidCors === 'never') {
return await fetchCorsProxy(uri, opts)
}
if (avoidCors === 'always') {
return await window.fetch(uri, opts)
}

const { hostname } = new URL(uri)
const corsFailureCount = hostnameCorsProxyBlacklist.get(hostname) ?? 0

Expand Down Expand Up @@ -90,6 +96,13 @@ export function makeBrowserIo(): EdgeIo {
// Throw the error from the first fetch instead of the one from
// proxy server.
throw errorToThrow
},

async fetchCors(
uri: string,
opts?: EdgeFetchOptions
): Promise<EdgeFetchResponse> {
return await this.fetch(uri, opts)
}
}
}
21 changes: 15 additions & 6 deletions src/io/react-native/react-native-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ async function makeIo(): Promise<EdgeIo> {
uri: string,
opts?: EdgeFetchOptions
): Promise<EdgeFetchResponse> {
return await window.fetch(uri, opts)
},
const { avoidCors = 'auto' } = opts ?? {}

if (avoidCors === 'never') {
return await nativeFetch(uri, opts)
}
if (avoidCors === 'always') {
return await window.fetch(uri, opts)
}

async fetchCors(
uri: string,
opts: EdgeFetchOptions = {}
): Promise<EdgeFetchResponse> {
const { protocol, host, pathname } = new URL(uri)
const endpoint = `${protocol}//${host}${pathname}`
const state = endpointCorsState.get(endpoint) ?? {
Expand Down Expand Up @@ -208,6 +210,13 @@ async function makeIo(): Promise<EdgeIo> {
const response = await nativeFetch(uri, opts)
state.nativeSuccess = true
return response
},

async fetchCors(
uri: string,
opts: EdgeFetchOptions = {}
): Promise<EdgeFetchResponse> {
return await this.fetch(uri, opts)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface EdgeIo {
/**
* This is like `fetch`, but will try to avoid CORS limitations
* on platforms where that may be a problem.
*
* @deprecated - Use `fetch` instead with `avoidCors: 'always'` option.
*/
readonly fetchCors: EdgeFetchFunction
}
Expand Down

0 comments on commit e3582dc

Please sign in to comment.