Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81acc6d

Browse files
committed
Break out getting terminal websocket url
To make it easier to move away from this service.
1 parent d4bb247 commit 81acc6d

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

site/src/utils/terminal.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as API from "api/api";
2+
3+
export const terminalWebsocketUrl = async (
4+
baseUrl: string | undefined,
5+
reconnect: string,
6+
agentId: string,
7+
command: string | undefined,
8+
): Promise<string> => {
9+
const query = new URLSearchParams({ reconnect });
10+
if (command) {
11+
query.set("command", command);
12+
}
13+
14+
const url = new URL(baseUrl || `${location.protocol}//${location.host}`);
15+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
16+
if (!url.pathname.endsWith("/")) {
17+
url.pathname + "/";
18+
}
19+
url.pathname += `api/v2/workspaceagents/${agentId}/pty`;
20+
url.search = "?" + query.toString();
21+
22+
// If the URL is just the primary API, we don't need a signed token to
23+
// connect.
24+
if (!baseUrl) {
25+
return url.toString();
26+
}
27+
28+
// Do ticket issuance and set the query parameter.
29+
const tokenRes = await API.issueReconnectingPTYSignedToken({
30+
url: url.toString(),
31+
agentID: agentId,
32+
});
33+
query.set("coder_signed_app_token_23db1dde", tokenRes.signed_token);
34+
url.search = "?" + query.toString();
35+
36+
return url.toString();
37+
};

site/src/xServices/terminal/terminalXService.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assign, createMachine } from "xstate";
22
import * as API from "api/api";
33
import * as TypesGen from "api/typesGenerated";
4+
import { terminalWebsocketUrl } from "utils/terminal";
45
import { getMatchingAgentOrFirst } from "utils/workspace";
56

67
interface ReconnectingPTYRequest {
@@ -213,42 +214,12 @@ export const terminalMachine =
213214
if (!context.reconnection) {
214215
throw new Error("reconnection ID is not set");
215216
}
216-
217-
let baseURL = context.baseURL || "";
218-
if (!baseURL) {
219-
baseURL = `${location.protocol}//${location.host}`;
220-
}
221-
222-
const query = new URLSearchParams({
223-
reconnect: context.reconnection,
224-
});
225-
if (context.command) {
226-
query.set("command", context.command);
227-
}
228-
229-
const url = new URL(baseURL);
230-
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
231-
if (!url.pathname.endsWith("/")) {
232-
url.pathname + "/";
233-
}
234-
url.pathname += `api/v2/workspaceagents/${context.workspaceAgent.id}/pty`;
235-
url.search = "?" + query.toString();
236-
237-
// If the URL is just the primary API, we don't need a signed token to
238-
// connect.
239-
if (!context.baseURL) {
240-
return url.toString();
241-
}
242-
243-
// Do ticket issuance and set the query parameter.
244-
const tokenRes = await API.issueReconnectingPTYSignedToken({
245-
url: url.toString(),
246-
agentID: context.workspaceAgent.id,
247-
});
248-
query.set("coder_signed_app_token_23db1dde", tokenRes.signed_token);
249-
url.search = "?" + query.toString();
250-
251-
return url.toString();
217+
return terminalWebsocketUrl(
218+
context.baseURL,
219+
context.reconnection,
220+
context.workspaceAgent.id,
221+
context.command,
222+
);
252223
},
253224
connect: (context) => (send) => {
254225
return new Promise<WebSocket>((resolve, reject) => {

0 commit comments

Comments
 (0)