Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add avoidCors API to EdgeFetchFunction #630

Merged
merged 1 commit into from
Dec 11, 2024
Merged

Conversation

samholmes
Copy link
Contributor

@samholmes samholmes commented Dec 10, 2024

This is to be able to to access nativeFetch in React Native
environements. This fetchCorsForced method will also forcibly use the
CORS servers in browser environemnts. It's idompodent in Node
environements.

CHANGELOG

Does this branch warrant an entry to the CHANGELOG?

  • Yes
  • No

Dependencies

none

Description

none

@samholmes samholmes changed the title Add fetchCorsForced Add avoidCors API to EdgeFetchFunction Dec 10, 2024
Copy link
Contributor

@swansontec swansontec left a comment

Choose a reason for hiding this comment

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

In the commit message, you say "idempotent" when you really mean "equivalent". The word "idempotent" means once you've done something once, doing it more times has no effect, like applying the Math.abs function.

Also, please edit the loginFetch function to use avoidCors: 'never'.

Also, in root.ts, find the makeSyncClient call and do fetch: (url, opts) => io.fetch(url, {...opts, avoidCors: 'never'}), to keep the behavior the same.

Comment on lines 48 to 53
if (avoidCors === 'never') {
return await fetchCorsProxy(uri, opts)
}
if (avoidCors === 'always') {
return await window.fetch(uri, opts)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these two conditions are backwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we are "never avoiding cross-origin request sharing" then we are "allowing cross-origin request sharing" which means we are allowing sharing between our origin (null) and the server's, so we should not let CORS policies block.

If we're are "always avoiding cross-origin request sharing" then we are "never cross-origin request sharing" which means if the server doesn't share our origin (null) then it should fail.

This is the logic I understood it as. Which is why it seemed a bit confusing to read and may not be obvious to the user if you're deriving an opposite conclusion from what I did when reading the API.

Copy link
Contributor

Choose a reason for hiding this comment

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

Went with the "bypass" name instead, to make it clear what is meant.

Comment on lines 177 to 182
if (avoidCors === 'never') {
return await nativeFetch(uri, opts)
}
if (avoidCors === 'always') {
return await window.fetch(uri, opts)
}
Copy link
Contributor

@swansontec swansontec Dec 10, 2024

Choose a reason for hiding this comment

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

I think these are backwards too. In order to "avoid" / dodge / bypass / escape the CORS restrictions that are blocking our request, we need to use the native fetch function. Hence, avoidCors: 'always' means "always use the native fetch function, and don't even bother with the web fetch."

@@ -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.
Copy link
Contributor

@swansontec swansontec Dec 10, 2024

Choose a reason for hiding this comment

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

Actually avoidCors: 'auto' would be the correct equivalent. We could simplify the ask to /** @deprecated Use EdgeIo.fetch instead, which now includes CORS avoidance by default */, since the user doesn't need to do anything extra.

@samholmes samholmes force-pushed the sam/fetchCorsForced branch 2 times, most recently from 48f663f to d1c8a97 Compare December 11, 2024 01:02
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 `never` will act like old `fetch` method.

Setting `always` 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.
@samholmes samholmes merged commit 1375ccd into master Dec 11, 2024
2 checks passed
@samholmes samholmes deleted the sam/fetchCorsForced branch December 11, 2024 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants