forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebTerminal.js
More file actions
60 lines (55 loc) · 2.05 KB
/
webTerminal.js
File metadata and controls
60 lines (55 loc) · 2.05 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export default async function initWebTerminal() {
const container = document.querySelector('.js-web-terminal');
if (!container) {
return;
}
const Terminal = await loadXterm();
const terminal = new Terminal();
let Addon = null;
if (typeof WebGL2RenderingContext !== 'undefined') {
Addon = await loadWebGLAddon();
} else {
Addon = await loadCanvasAddon();
}
terminal.loadAddon(new Addon());
terminal.open(container);
const socket = new WebSocket(`wss://${window.location.host}/_shell/`);
socket.addEventListener('open', (_) => {
terminal.onData((data) => socket.send(data));
socket.addEventListener('message', (evt) => terminal.write(evt.data));
});
socket.addEventListener('error', (_) => {
terminal.reset();
terminal.writeln('Connection error.');
});
socket.addEventListener('close', (evt) => {
if (evt.wasClean) {
terminal.reset();
terminal.writeln(evt.reason ?? 'Connection closed.');
}
});
}
/** @returns {Promise<typeof import("xterm").Terminal>} */
async function loadXterm() {
// NOTE: String expression used to prevent ESBuild from resolving
// the import on build (xterm is a separate bundle)
const xtermBundlePath = '/js/dist/xterm.min.js';
const xtermModule = await import(`${xtermBundlePath}`);
return xtermModule.default.Terminal;
}
/** @returns {Promise<typeof import("xterm-addon-webgl").WebglAddon>} */
async function loadWebGLAddon() {
// NOTE: String expression used to prevent ESBuild from resolving
// the import on build (xterm-addon-webgl is a separate bundle)
const xtermBundlePath = '/js/dist/xterm-addon-webgl.min.js';
const xtermModule = await import(`${xtermBundlePath}`);
return xtermModule.default.WebglAddon;
}
/** @returns {Promise<typeof import("xterm-addon-canvas").CanvasAddon>} */
async function loadCanvasAddon() {
// NOTE: String expression used to prevent ESBuild from resolving
// the import on build (xterm-addon-canvas is a separate bundle)
const xtermBundlePath = '/js/dist/xterm-addon-canvas.min.js';
const xtermModule = await import(`${xtermBundlePath}`);
return xtermModule.default.CanvasAddon;
}