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 / WebSocketPair not available #154

Open
1 task done
Ehesp opened this issue Feb 5, 2024 · 6 comments
Open
1 task done

WebSocket / WebSocketPair not available #154

Ehesp opened this issue Feb 5, 2024 · 6 comments
Assignees
Labels
- P2: nice to have Not breaking anything but nice to have (priority) pkg: cloudflare

Comments

@Ehesp
Copy link

Ehesp commented Feb 5, 2024

Astro Info

Astro                    v4.2.6
Node                     v18.17.0
System                   macOS (arm64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/tailwind
                         @astrojs/react

Describe the Bug

I'm running astro with the Cloudflare adapter, and everything is working great apart from the WebSocket and WebSocketPair classes not being available in the server runtime.

export default defineConfig({
  // Mimics the behavior of Wrangler running locally so we don't have to alter the Cloudflare Tunnels
  server: {
    host: true,
    port: 8788,
  },
  // Server output is required for Cloudflare Workers
  output: "server",
  adapter: cloudflare({
    runtime: {
      mode: "local",
      type: "pages",
      bindings: { ... },
   }
  })
})

Local mode says: uses a local runtime powered by miniflare and workerd, which supports Cloudflare’s Bindings.. I might have missed the point here, but I was hoping that those classes would be available since they are on miniflare / workderd?

What's the expected result?

Works just like Cloudflare (as the declarations are available via:

/// <reference types="@cloudflare/workers-types" />

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-8ghpcm?file=src%2Fpages%2Findex.ts

Participation

  • I am willing to submit a pull request for this issue.
@alexanderniebuhr alexanderniebuhr added pkg: cloudflare - P4: important Violate documented behavior or significantly improves performance (priority) labels Feb 6, 2024
@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Feb 6, 2024

Thanks for the issue. This is similar to https://discord.com/channels/830184174198718474/1168702512782659664
We still figure out a way to make this available in astro dev. If you build the project and deploy it, it will work already.

@alexanderniebuhr alexanderniebuhr changed the title [cloudflare]: WebSocket / WebSocketPair not available WebSocket / WebSocketPair not available Feb 6, 2024
@alexanderniebuhr alexanderniebuhr self-assigned this Feb 6, 2024
@alexanderniebuhr alexanderniebuhr added - P2: nice to have Not breaking anything but nice to have (priority) and removed - P4: important Violate documented behavior or significantly improves performance (priority) labels Feb 6, 2024
@Ehesp
Copy link
Author

Ehesp commented Feb 6, 2024

Thanks! Would it be possible to import https://www.npmjs.com/package/@miniflare/web-sockets and provide it as a global via vite config in the adapter?

Probably not that straightforward 😅

@Ehesp
Copy link
Author

Ehesp commented Feb 6, 2024

I need to validate this actually works, but something like this might be the best workaround for now:

let WebSocketImpl: typeof WebSocket;

if (import.meta.env.DEV) {
  const wsModule = await import("ws");
  WebSocketImpl = wsModule.default as unknown as typeof WebSocket;
} else {
  // Fallback to the global `WebSocket` in production
  WebSocketImpl = WebSocket;
}

let WebSocketPairImpl: typeof WebSocketPair;

if (import.meta.env.DEV) {
  // Use @miniflare/web-sockets in development
  const { WebSocketPair: MFWebSocketPair } = await import(
    "@miniflare/web-sockets"
  );
  WebSocketPairImpl = MFWebSocketPair as unknown as typeof WebSocketPair;
} else {
  // Assume WebSocketPair is globally available in production (Cloudflare Workers)
  WebSocketPairImpl = WebSocketPair;
}

export { WebSocketImpl as WebSocket, WebSocketPairImpl as WebSocketPair };

@Ehesp
Copy link
Author

Ehesp commented Feb 8, 2024

@alexanderniebuhr ive just seen https://vitejs.dev/blog/announcing-vite5-1.html

Would this new api allow us to hook directly into workerd? Found via https://twitter.com/pcattori/status/1755632675103080521?t=C0blapvcX2HNq-zTgw3vpw&s=19

@alexanderniebuhr
Copy link
Member

We are looking into this internally already, but that's still experimental.

@Ehesp
Copy link
Author

Ehesp commented Feb 8, 2024

I think this might be moot anyway based on withastro/astro#10057 (comment) :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: nice to have Not breaking anything but nice to have (priority) pkg: cloudflare
Projects
None yet
Development

No branches or pull requests

2 participants