mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-06 19:18:22 +01:00
27 lines
795 B
TypeScript
27 lines
795 B
TypeScript
import type { WebSocket as NodeWebSocketType } from "ws";
|
|
|
|
let NodeWebSocket: typeof NodeWebSocketType = null;
|
|
|
|
if (typeof window === "undefined") {
|
|
try {
|
|
// Necessary to avoid issues with Rollup: https://github.com/websockets/ws/issues/2057
|
|
process.env.WS_NO_BUFFER_UTIL = "1";
|
|
import("ws").then((ws) => (NodeWebSocket = ws.default));
|
|
} catch (error) {
|
|
console.log("Error importing 'ws':", error);
|
|
}
|
|
}
|
|
|
|
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 };
|