mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
26 lines
748 B
TypeScript
26 lines
748 B
TypeScript
import type { WebSocket as NodeWebSocketType } from "ws";
|
|
|
|
let NodeWebSocket: typeof NodeWebSocketType = 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);
|
|
});
|
|
}
|
|
|
|
type ComboWebSocket = NodeWebSocketType | WebSocket;
|
|
|
|
function newWebSocket(url: string, headers: { [key: string]: string }): ComboWebSocket {
|
|
if (NodeWebSocket) {
|
|
return new NodeWebSocket(url, { headers });
|
|
} else {
|
|
return new WebSocket(url);
|
|
}
|
|
}
|
|
|
|
export { newWebSocket };
|
|
export type { ComboWebSocket as WebSocket };
|