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

fetch: Add Cloudflare Workers support (cross-fetch v4) #794

Merged
merged 2 commits into from
Jun 26, 2023

Conversation

spencercap
Copy link
Contributor

at the @thencc we're using the js algosdk in cloudflare workers' v8 environment for EXTREME server-side benefits.

by simply updating to cross-fetch v4 (currently in alpha), algosdk can work in chrome v8 in addition to the usual node + browser envs. this was just recently made available as discussed here: lquixada/cross-fetch#163

as a byproduct, this PR removes the mode: "cors" arg in algosdk's fetch calls as it is not supported by the fetch api available within workers. may i ask what is the real use for this arg anyway?

@CLAassistant
Copy link

CLAassistant commented Jun 6, 2023

CLA assistant check
All committers have signed the CLA.

@Eric-Warehime
Copy link
Contributor

I'll take a look at this--I'm not really familiar with how the CORS stuff is currently implemented in the SDK. The CI is failing on unrelated things which will be fixed once we merge #793 I'll rerun once I merge that.

@spencercap
Copy link
Contributor Author

ok thanks ! yes, let me know what y'all think about removing that cors arg + that merge.

Copy link
Contributor

@Eric-Warehime Eric-Warehime left a comment

Choose a reason for hiding this comment

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

Ok, I don't have a problem with this as is. We'll have to keep an eye on cross-fetch to see when we can just swap to v4 stable.

For the cors stuff it looks like it was committed when we swapped from superagent to fetch, but the fetch library doesn't provide support for setting the mode property in requests, so it doesn't do anything.

@Eric-Warehime Eric-Warehime changed the title Add Cloudflare Workers support (cross-fetch v4) fetch: Add Cloudflare Workers support (cross-fetch v4) Jun 13, 2023
@jasonpaulos
Copy link
Contributor

jasonpaulos commented Jun 14, 2023

While the cors option has no effect for node, I'm under the impression that it's necessary for browsers. We need to carefully verify that any changes here do not adversely affect browser usage.

@Eric-Warehime
Copy link
Contributor

@jasonpaulos

We need to carefully verify that any changes here do not adversely affect browser usage.

Agreed!

While the cors option has no effect for node, I'm under the impression that it's necessary for browsers.

The node-fetch library which is being folyfilled by cross-fetch doesn't ever use the mode argument which is passed in. I've taken a look at the polyfill code I could find in the cross-fetch repo and I can't find anywhere that it's being used in there either.

Is there some place you know of that I'm not finding w/r/t the creation of the request object in browsers?

@jasonpaulos
Copy link
Contributor

The node-fetch library which is being folyfilled by cross-fetch doesn't ever use the mode argument which is passed in. I've taken a look at the polyfill code I could find in the cross-fetch repo and I can't find anywhere that it's being used in there either.

Is there some place you know of that I'm not finding w/r/t the creation of the request object in browsers?

The polyfills you mentioned should only ever be used in node. Our expectation is that the browsers we support are modern enough to supply their own native implementation of fetch -- no polyfill should be active for them. That's the environment that I think your analysis is missing.

In this case, the best way to test that environment is to access a webpage from a real host (i.e. not localhost or accessed as a local file) using the SDK, and having code which talks to an algod located in a different host. That's the situation in which CORS would apply.

@Eric-Warehime
Copy link
Contributor

The polyfills you mentioned should only ever be used in node.

I guess that includes react-native as well--I know we don't support it right now.

Good point about the browsers. I'm unsure if our current browser tests will exercise cross origin support or not

@youraerials
Copy link

Hi, there is not really a need to polyfill fetch in 2023. It causes way more problems than it fixes across environments. Just use native fetch calls and the standard API and let us polyfill fetch in user space in the select places that is needed. Have a look at https://www.builder.io/blog/stop-polyfilling-fetch-in-your-npm-package

@youraerials
Copy link

Also, FWIW mode: "cors" is the default in the fetch api, you can safely omit it! https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

@Eric-Warehime
Copy link
Contributor

@spencercap The CI should pass if you rebase?

Also I'm happy to merge this as long as @jasonpaulos doesn't object.

@Eric-Warehime
Copy link
Contributor

It looks like the new CI failures are because we're bumping the golang version everywhere, but we haven't updated sandbox yet. I've got algorand/sandbox#189 open to fix that and will re-run the CI once it's merged.

@Eric-Warehime Eric-Warehime merged commit 28ba159 into algorand:develop Jun 26, 2023
2 checks passed
@spencercap
Copy link
Contributor Author

any estimate when this will be pushed to the @latest release?

nullun added a commit to nullun/js-algorand-sdk that referenced this pull request Sep 24, 2023
joe-p pushed a commit to joe-p/js-algorand-sdk that referenced this pull request Apr 11, 2024
* using new cross-fetch 4 w workers shim
* removed mode: 'cors' for v8 compat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants