Skip to content

Commit 2de7f12

Browse files
chore: take inspiration from existing websocket
1 parent f73eaee commit 2de7f12

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function needToken(): boolean {
2828
/**
2929
* Create a new agent based off the current settings.
3030
*/
31-
async function createHttpAgent(): Promise<ProxyAgent> {
31+
export async function createHttpAgent(): Promise<ProxyAgent> {
3232
const cfg = vscode.workspace.getConfiguration()
3333
const insecure = Boolean(cfg.get("coder.insecure"))
3434
const certFile = expandPath(String(cfg.get("coder.tlsCertFile") ?? "").trim())

src/inbox.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as vscode from "vscode"
33
import { WebSocket } from "ws"
44
import { errToStr } from "./api-helper"
55
import { Storage } from "./storage"
6+
import { ProxyAgent } from "proxy-agent"
67

78
type InboxMessage = {
89
unread_count: number
@@ -29,31 +30,33 @@ export class Inbox implements vscode.Disposable {
2930
private socket: WebSocket
3031

3132
constructor(
32-
private readonly restClient: Api,
33+
httpAgent: ProxyAgent,
34+
restClient: Api,
3335
private readonly storage: Storage,
3436
) {
35-
// const url = this.restClient.getAxiosInstance().defaults.baseURL
36-
const token = this.restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as
37-
| string
38-
| undefined
39-
// const inboxUrl = new URL(`${url}/api/v2/notifications/watch`);
40-
const inboxUrl = new URL(`ws://localhost:8080`)
41-
42-
this.storage.writeToCoderOutputChannel("Listening to Coder Inbox")
43-
44-
// We're gonna connect over WebSocket so replace the scheme.
45-
if (inboxUrl.protocol === "https") {
46-
inboxUrl.protocol = "wss"
47-
} else if (inboxUrl.protocol === "http") {
48-
inboxUrl.protocol = "ws"
37+
const baseUrlRaw = restClient.getAxiosInstance().defaults.baseURL
38+
if (!baseUrlRaw) {
39+
throw new Error("No base URL set on REST client")
4940
}
5041

51-
this.socket = new WebSocket(inboxUrl, {
42+
const baseUrl = new URL(baseUrlRaw)
43+
const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:"
44+
const socketUrlRaw = `${socketProto}//${baseUrl.host}/api/v2/notifications/watch`
45+
46+
this.socket = new WebSocket(new URL(socketUrlRaw), {
47+
followRedirects: true,
48+
agent: httpAgent,
5249
headers: {
53-
"Coder-Session-Token": token,
50+
"Coder-Session-Token": restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as
51+
| string
52+
| undefined,
5453
},
5554
})
5655

56+
this.socket.on("open", () => {
57+
this.storage.writeToCoderOutputChannel("Listening to Coder Inbox")
58+
})
59+
5760
this.socket.on("error", (error) => {
5861
this.notifyError(error)
5962
})

src/remote.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as path from "path"
99
import prettyBytes from "pretty-bytes"
1010
import * as semver from "semver"
1111
import * as vscode from "vscode"
12-
import { makeCoderSdk, needToken, startWorkspaceIfStoppedOrFailed, waitForBuild } from "./api"
12+
import { createHttpAgent, makeCoderSdk, needToken, startWorkspaceIfStoppedOrFailed, waitForBuild } from "./api"
1313
import { extractAgents } from "./api-helper"
1414
import * as cli from "./cliManager"
1515
import { Commands } from "./commands"
@@ -405,7 +405,8 @@ export class Remote {
405405
disposables.push(monitor.onChange.event((w) => (this.commands.workspace = w)))
406406

407407
// Watch coder inbox for messages
408-
const inbox = new Inbox(workspaceRestClient, this.storage)
408+
const httpAgent = await createHttpAgent();
409+
const inbox = new Inbox(httpAgent, workspaceRestClient, this.storage)
409410
disposables.push(inbox)
410411

411412
// Wait for the agent to connect.

0 commit comments

Comments
 (0)