* Utility function to help create a WebSocket connection with Coder's API.
( path: string, params: URLSearchParams = new URLSearchParams(), )
| 4002 | * Utility function to help create a WebSocket connection with Coder's API. |
| 4003 | */ |
| 4004 | function createWebSocket( |
| 4005 | path: string, |
| 4006 | params: URLSearchParams = new URLSearchParams(), |
| 4007 | ) { |
| 4008 | // When running in an embedded context (e.g. VS Code webview), |
| 4009 | // the session token is set via the API header but browsers |
| 4010 | // cannot attach custom headers to WebSocket connections. |
| 4011 | // Pass it as a query parameter instead. |
| 4012 | const token = API.getSessionToken(); |
| 4013 | if (token) { |
| 4014 | params.set(SessionTokenCookie, token); |
| 4015 | } |
| 4016 | const protocol = location.protocol === "https:" ? "wss:" : "ws:"; |
| 4017 | const socket = new WebSocket( |
| 4018 | `${protocol}//${location.host}${path}?${params}`, |
| 4019 | ); |
| 4020 | socket.binaryType = "blob"; |
| 4021 | return socket; |
| 4022 | } |
| 4023 | |
| 4024 | // Other non-API methods defined here to make it a little easier to find them. |
| 4025 | interface ClientApi extends ApiMethods { |
no test coverage detected