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

ethers doesn't work in a Worker #3536

Closed
samczsun opened this issue Nov 22, 2022 · 10 comments
Closed

ethers doesn't work in a Worker #3536

samczsun opened this issue Nov 22, 2022 · 10 comments
Assignees
Labels
discussion Questions, feedback and general information.

Comments

@samczsun
Copy link

Ethers Version

5.7.2

Search Terms

No response

Describe the Problem

In a Worker, it seems like fetch behaves differently:

const provider = new ethers.providers.JsonRpcProvider('https://rpc.ankr.com/eth');
provider.on('debug', console.log);
provider.on('error', console.log);
await provider.getNetwork();
Uncaught Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.2)
    at Logger.makeError (ethers-5.7.2.umd.min.js:1:62180)
    at Logger.throwError (ethers-5.7.2.umd.min.js:1:62385)
    at JsonRpcProvider.<anonymous> (ethers-5.7.2.umd.min.js:1:655650)
    at step (ethers-5.7.2.umd.min.js:1:642267)
    at Object.throw (ethers-5.7.2.umd.min.js:1:641527)
    at rejected (ethers-5.7.2.umd.min.js:1:640986)

The cause is the following error: Failed to execute 'fetch' on 'WorkerGlobalScope': Referrer 'client' is not a valid URL., which is set here:

request.referrer = "client"; // no-referrer, *client

It seems this was discussed in #1886 but I'm not sure how to turn it on. Given that this affects vanilla JS and not an esoteric platform, maybe the behavior can be changed to be more flexible?

Code Snippet

No response

Contract ABI

No response

Errors

No response

Environment

Browser (Chrome, Safari, etc)

Environment (Other)

No response

@samczsun samczsun added the investigate Under investigation and may be a bug. label Nov 22, 2022
@ricmoo
Copy link
Member

ricmoo commented Nov 22, 2022

Instead of passing in the URL directly, try passing in an object that looks like: { skipFetchSetup: true, url }.

That should skip the fetch setup that the affects JavaScriptCore-based systems with a custom/non-standard fetch implementation like Cloudflare Workers… :)

@ricmoo
Copy link
Member

ricmoo commented Nov 22, 2022

(in v6, the fetch class is much more flexible ;))

@samczsun
Copy link
Author

I'll give the object a shot! But just to make sure we're on the same page here, I'm referring to a Worker, not a Cloudflare Worker

@ricmoo
Copy link
Member

ricmoo commented Nov 23, 2022

@samczsun Oh, that issue was mainly regarding Cloudflare workers. I’m not sure why a normal Worker would complain about that value for the fetch.

But the object should skip populating it anyways, so may still help you.

@ricmoo
Copy link
Member

ricmoo commented Nov 23, 2022

(also, random question; what browser are you using?)

@samczsun
Copy link
Author

Latest Chrome. I think maybe it's because my worker is built from a blob url?

@ricmoo
Copy link
Member

ricmoo commented Nov 23, 2022

Oh. Possibly. I know Safari used to have weird behaviours (especially with things CORS-ish) for BLOB urls.

Did skipFetchSetup work?

@doigensan
Copy link

doigensan commented Dec 15, 2022

@ricmoo ran into a similar issue this week, provider.getBlockNumber() was returning
missing response (requestBody="{\"method\":\"eth_blockNumber\",\"params\":[],\"id\":42,\"jsonrpc\":\"2.0\"}", requestMethod="POST", serverError={}, url="https://evm-xxx", code=SERVER_ERROR, version=web/5.7.1)

Setting the skipFetchSetup fixed it.
Thank you!

@ricmoo
Copy link
Member

ricmoo commented Jan 18, 2023

Awesome!

I'm going to close this now as fixed. If you have any further issues though, please let me know.

Thanks! :)

@ricmoo ricmoo added discussion Questions, feedback and general information. and removed investigate Under investigation and may be a bug. labels Jan 18, 2023
@ricmoo ricmoo closed this as completed Jan 18, 2023
@makkarpov
Copy link

makkarpov commented Jul 18, 2023

Global options would be useful. There are libraries that don't allow you to customize everything. I.e. https://github.com/synapsecns/sdk which uses fetchJson and passes just URL there. And while technically it's their fault, I'm afraid that it would be a whack-a-mole game to hunt for every library in the world.

joshdchang added a commit to joshdchang/js-sdk that referenced this issue Jul 16, 2024
Workaround from ethers-io/ethers.js#3536 (comment)

Signed-off-by: Josh Chang <joshchang04@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

4 participants