mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-02 18:39:05 +01:00
Fix WebSocket compatibility with Rollup (#398)
This commit is contained in:
parent
43ce3415da
commit
4c3496c5e1
@ -1,9 +1,9 @@
|
|||||||
// Copyright 2024, Command Line Inc.
|
// Copyright 2024, Command Line Inc.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
import { type WebSocket, newWebSocket } from "@/util/wsutil";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
import { sprintf } from "sprintf-js";
|
import { sprintf } from "sprintf-js";
|
||||||
import type { WebSocket as NodeWebSocketType } from "ws";
|
|
||||||
|
|
||||||
const AuthKeyHeader = "X-AuthKey";
|
const AuthKeyHeader = "X-AuthKey";
|
||||||
|
|
||||||
@ -27,12 +27,11 @@ function removeWSReconnectHandler(handler: () => void) {
|
|||||||
type WSEventCallback = (arg0: WSEventType) => void;
|
type WSEventCallback = (arg0: WSEventType) => void;
|
||||||
|
|
||||||
type ElectronOverrideOpts = {
|
type ElectronOverrideOpts = {
|
||||||
wsImpl: typeof NodeWebSocketType;
|
|
||||||
authKey: string;
|
authKey: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WSControl {
|
class WSControl {
|
||||||
wsConn: WebSocket | NodeWebSocketType;
|
wsConn: WebSocket;
|
||||||
open: boolean;
|
open: boolean;
|
||||||
opening: boolean = false;
|
opening: boolean = false;
|
||||||
reconnectTimes: number = 0;
|
reconnectTimes: number = 0;
|
||||||
@ -67,13 +66,9 @@ class WSControl {
|
|||||||
this.lastReconnectTime = Date.now();
|
this.lastReconnectTime = Date.now();
|
||||||
dlog("try reconnect:", desc);
|
dlog("try reconnect:", desc);
|
||||||
this.opening = true;
|
this.opening = true;
|
||||||
if (this.eoOpts) {
|
this.wsConn = newWebSocket(this.baseHostPort + "/ws?windowid=" + this.windowId, {
|
||||||
this.wsConn = new this.eoOpts.wsImpl(this.baseHostPort + "/ws?windowid=" + this.windowId, {
|
[AuthKeyHeader]: this.eoOpts.authKey,
|
||||||
headers: { [AuthKeyHeader]: this.eoOpts.authKey },
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.wsConn = new WebSocket(this.baseHostPort + "/ws?windowid=" + this.windowId);
|
|
||||||
}
|
|
||||||
this.wsConn.onopen = this.onopen.bind(this);
|
this.wsConn.onopen = this.onopen.bind(this);
|
||||||
this.wsConn.onmessage = this.onmessage.bind(this);
|
this.wsConn.onmessage = this.onmessage.bind(this);
|
||||||
this.wsConn.onclose = this.onclose.bind(this);
|
this.wsConn.onclose = this.onclose.bind(this);
|
||||||
@ -209,5 +204,4 @@ class WSControl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { addWSReconnectHandler, removeWSReconnectHandler, WSControl };
|
export { WSControl, addWSReconnectHandler, removeWSReconnectHandler };
|
||||||
export type { ElectronOverrideOpts };
|
|
||||||
|
26
frontend/util/wsutil.ts
Normal file
26
frontend/util/wsutil.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
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 };
|
Loading…
Reference in New Issue
Block a user