|
1 | 1 | import { assign, createMachine } from "xstate";
|
2 | 2 | import * as API from "api/api";
|
3 | 3 | import * as TypesGen from "api/typesGenerated";
|
| 4 | +import { terminalWebsocketUrl } from "utils/terminal"; |
4 | 5 | import { getMatchingAgentOrFirst } from "utils/workspace";
|
5 | 6 |
|
6 | 7 | interface ReconnectingPTYRequest {
|
@@ -213,42 +214,12 @@ export const terminalMachine =
|
213 | 214 | if (!context.reconnection) {
|
214 | 215 | throw new Error("reconnection ID is not set");
|
215 | 216 | }
|
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 | + ); |
252 | 223 | },
|
253 | 224 | connect: (context) => (send) => {
|
254 | 225 | return new Promise<WebSocket>((resolve, reject) => {
|
|
0 commit comments