question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cloudflare worker fails with some parameters that ethers.js harcode

See original GitHub issue

cloudflare worker mimic the behavior of a service worker, as such it is like a limited browser environment.

unfortunately it does not implement all parameters of the fetch api making it fails with ethers.js due to these lines:

https://github.com/ethers-io/ethers.js/blob/ce8f1e4015c0f27bf178238770b1325136e3351a/packages/web/src.ts/browser-geturl.ts#L17-L21

While the redirect field works, the other do not and I get a respective error for each:

The 'credentials' field on 'RequestInitializerDict' is not implemented.

The 'cache' field on 'RequestInitializerDict' is not implemented.

The 'mode' field on 'RequestInitializerDict' is not implemented.

The 'referrer' field on 'RequestInitializerDict' is not implemented.

Would be great if we could override the default behavior here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

16reactions
ricmoocommented, Mar 10, 2022

This is now available in v5.6. I’ve tried it out and am loving Cloudflare Workers.

To use it, connect to your provider setting the skipFetchSetup flag:

const provider = new StaticJsonRpcProvider({
 url: URL,
 skipFetchSetup: true
});

Let me know if you have any issues!

4reactions
DanielAGWcommented, Nov 24, 2021

@wighawag Wow, thank you! I am using webpack and this is my webpack config, based on your suggestion:

module.exports = {
    target: "webworker",
    entry: "./src/index.js",
    mode: "production",
    module: {
        rules: [
            {
                test: /\.(mjs|js|jsx)$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                options: {
                    presets: [
                        "@babel/preset-env",
                        {
                            plugins: [
                                "@babel/plugin-proposal-class-properties"
                            ]
                        }
                    ]
                },
            },
            {
                test: /\.js$/,
                loader: 'string-replace-loader',
                options: {
                    multiple: [
                        { search: 'request.mode = "cors";', replace: '/* request.mode = "cors"; */' },
                        { search: 'request.cache = "no-cache";', replace: '/* request.cache = "no-cache"; */' },
                        { search: 'request.credentials = "same-origin";', replace: '/* request.credentials = "same-origin"; */' },
                        { search: 'request.referrer = "client";', replace: '/* request.referrer = "client"; */' }
                    ]
                }
            }
        ],
    }
};

I needed to install string-replace-loader webpack loader and everything worked out of the box, amazing.

Thank you @wighawag and thank you @ricmoo for the amazing package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

error: could not detect network (event="nonetwork", code ...
cloudflare worker fails with some parameters that ethers.js harcode ... Github.com. Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, ...
Read more >
JS Error in Cloudflare Workers - Getting Started
Hi, I'm new to Cloudflare Workers. How do I add the below JavaScript into Cloudflare Workers? It shows me an error.
Read more >
RxJS in Action.pdf
Error handling with RxJS 182. PART 3 MASTERING RXJS 209. 8 □. Heating up observables 211. 9 □. Toward testable, reactive programs 245....
Read more >
A Government-Backed Cryptocurrency for Universal Basic ...
In this project an Ether-backed UBI token has been created to solve this problem. The system has been built using smart contracts (see...
Read more >
Highlights ethers.js: March 2022 - RicMoo
The latest minor version has just been released with some exciting new ... out the problem with ethers in the Cloudflare Worker environment, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found