( baseUrl: string | undefined, reconnect: string, agentId: string, command: string | undefined, height: number, width: number, containerName: string | undefined, containerUser: string | undefined, )
| 1 | import { 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 | height: number, |
| 9 | width: number, |
| 10 | containerName: string | undefined, |
| 11 | containerUser: string | undefined, |
| 12 | ): Promise<string> => { |
| 13 | const query = new URLSearchParams({ reconnect }); |
| 14 | if (command) { |
| 15 | query.set("command", command); |
| 16 | } |
| 17 | query.set("height", height.toString()); |
| 18 | query.set("width", width.toString()); |
| 19 | if (containerName) { |
| 20 | query.set("container", containerName); |
| 21 | } |
| 22 | if (containerName && containerUser) { |
| 23 | query.set("container_user", containerUser); |
| 24 | } |
| 25 | |
| 26 | const url = new URL(baseUrl || `${location.protocol}//${location.host}`); |
| 27 | url.protocol = url.protocol === "https:" ? "wss:" : "ws:"; |
| 28 | if (!url.pathname.endsWith("/")) { |
| 29 | `${url.pathname}/`; |
| 30 | } |
| 31 | url.pathname += `api/v2/workspaceagents/${agentId}/pty`; |
| 32 | url.search = `?${query.toString()}`; |
| 33 | |
| 34 | // If the URL is just the primary API, we don't need a signed token to |
| 35 | // connect. |
| 36 | if (!baseUrl) { |
| 37 | return url.toString(); |
| 38 | } |
| 39 | |
| 40 | // Do ticket issuance and set the query parameter. |
| 41 | const tokenRes = await API.issueReconnectingPTYSignedToken({ |
| 42 | url: url.toString(), |
| 43 | agentID: agentId, |
| 44 | }); |
| 45 | query.set("coder_signed_app_token_23db1dde", tokenRes.signed_token); |
| 46 | url.search = `?${query.toString()}`; |
| 47 | |
| 48 | return url.toString(); |
| 49 | }; |
no test coverage detected