forked from gitpod-io/xterm-web-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.ts
More file actions
83 lines (71 loc) · 2.83 KB
/
remote.ts
File metadata and controls
83 lines (71 loc) · 2.83 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { output, webSocketSettings } from "../client";
import { IXtermWindow } from "./types";
declare let window: IXtermWindow;
export const resizeRemoteTerminal = async (size: { cols: number; rows: number }, pid: number) => {
if (!pid) {
return;
}
const cols = size.cols;
const rows = size.rows;
const url = `/terminals/${pid}/size?cols=${cols}&rows=${rows}`;
try {
await fetch(url, { method: "POST" });
} catch (e) {
console.error(`Failed to resize the remote shell: ${e}`);
}
};
export const initiateRemoteCommunicationChannelSocket = async (protocol: string) => {
const ReconnectingWebSocket = (await import("reconnecting-websocket")).default;
const socket = new ReconnectingWebSocket(
`${protocol + location.hostname + (location.port ? ":" + location.port : "")}/terminals/remote-communication-channel/`,
[],
webSocketSettings,
);
socket.onopen = () => {
console.debug("External messaging channel socket opened");
};
socket.onmessage = (event) => {
if (!event.data) {
console.warn("Received empty message");
return;
}
const messageData = JSON.parse(event.data);
if (window.handledMessages.includes(messageData.id)) {
console.debug(`Message already handled: ${messageData.id}`);
return;
}
switch (messageData.action) {
case "openUrl": {
const url = messageData.data;
console.debug(`Opening URL: ${url}`);
window.open(url, "_blank");
break;
}
case "notifyAboutUrl": {
const { url, port, name } = messageData.data;
const openUrlButton = document.createElement("button");
openUrlButton.innerText = "Open URL";
openUrlButton.onclick = () => {
window.open(url, "_blank");
};
if (name) {
output(`${name} on port ${port} has been opened`, { formActions: [openUrlButton], reason: "info" });
break;
}
output(`Port ${port} has been opened`, { formActions: [openUrlButton], reason: "info" });
break;
}
case "confirmExit": {
// Ask for confirmation before closing the current terminal session
window.onbeforeunload = (e: BeforeUnloadEvent) => {
e.preventDefault();
e.returnValue = "Are you sure you want to close the terminal?";
};
}
default:
console.debug("Unhandled message", messageData);
}
window.handledMessages.push(messageData.id);
console.debug(`Handled message: ${messageData.id}`);
};
};