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

WebSocket providers need special logic to work in Cloudflare Workers #2237

Open
TimTinkers opened this issue Oct 28, 2021 · 0 comments
Open
Labels
investigate Under investigation and may be a bug.

Comments

@TimTinkers
Copy link

Description

Ethers WebSocket providers (such as the one created by ethers.providers.InfuraProvider.getWebSocketProvider(...)) do not work in the Cloudflare Workers environment. They throw a Failed to construct 'WebSocket': the constructor is not implemented error because Cloudflare Workers have their own preferred way of creating a socket connection and do not support the vanilla WebSocket constructor.

Reproduction

Attempt to call either

const socket = new WebSocket('wss://mainnet.infura.io/ws/v3/...');

or

const ethereumNode = ethers.providers.InfuraProvider.getWebSocketProvider(
  'mainnet', {
    projectId: '...'
  }
);

from within any Cloudflare Worker. This is a bit of an annoying one to setup if you've not already got a Cloudflare account and Wrangler installed, but it's an immediate thing to replicate. Either of the scenarios above will throw the aforementioned Failed to construct 'WebSocket': the constructor is not implemented error.

Environment

Workers is Cloudflare's offering for serverless compute, similar to other offerings like AWS Lambda, Azure Functions, GCP Cloud Functions, Vercel Functions, Netlify Functions, or Serverless Cloud. Unlike any of those other environments, Cloudflare Workers do not use a Node runtime. Instead, Cloudflare Workers execute directly on v8 isolates with some custom Cloudflare implementations of standard JavaScript APIs. Normally, this means that any code which works on the browser will work on a Cloudflare Worker.

However, whether due to security, performance, or time constraints, Cloudflare's implementation does not have complete coverage of the JavaScript APIs. In particular the lack of WebSocket API constructor is what makes the WebSocket providers unusable.

Search Terms

This is similar to #1886 as reported by @wighawag in that a Cloudflare difference to browser JavaScript was responsible for both.

Discussion

A special thanks to @mrbbot of Miniflare fame for helping me identify this issue in the Cloudflare Workers Discord.

I believe the most straight-forward solution to this issue is supporting a way to pass an existing WebSocket along to the WebSocketProvider instead of always constructing a new WebSocket. Is this something you would be open to PRs for, @ricmoo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug.
Projects
None yet
Development

No branches or pull requests

1 participant