-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathwsutil.ts
30 lines (24 loc) · 952 Bytes
/
wsutil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import type { WebSocket as NodeWebSocketType } from "ws";
let NodeWebSocket: any = null;
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;
function newWebSocket(url: string, headers: { [key: string]: string }): ComboWebSocket {
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);
}
}
export { newWebSocket };
export type { ComboWebSocket as WebSocket };