Skip to content

fix: add WebSocket polyfill for Electron main process #2101

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions frontend/util/wsutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

import type { WebSocket as NodeWebSocketType } from "ws";

let NodeWebSocket: typeof NodeWebSocketType = null;
let NodeWebSocket: any = null;

if (typeof window === "undefined") {
// Necessary to avoid issues with Rollup: https://github.com/websockets/ws/issues/2057
import("ws")
.then((ws) => (NodeWebSocket = ws.default))
.catch((e) => {
console.log("Error importing 'ws':", e);
});
if (typeof window === "undefined" || (typeof process !== "undefined" && process.type === "browser")) {
try {
NodeWebSocket = require("ws").WebSocket;
} catch (e) {
import("ws").then((ws) => (NodeWebSocket = ws.default)).catch((e) => console.log("ws import error:", e));
}
}

type ComboWebSocket = NodeWebSocketType | WebSocket;
Expand All @@ -20,6 +19,9 @@ function newWebSocket(url: string, headers: { [key: string]: string }): ComboWeb
if (NodeWebSocket) {
return new NodeWebSocket(url, { headers });
} else {
if (typeof WebSocket === "undefined") {
throw new Error("WebSocket not available in this environment");
}
return new WebSocket(url);
}
}
Expand Down