8000 feat: add support for coder inbox by DanielleMaywood · Pull Request #444 · coder/vscode-coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add support for coder inbox #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: take inspiration from existing websocket
  • Loading branch information
DanielleMaywood committed Mar 3, 2025
commit 2de7f12ae0ac6aaa5456f62415a79cce2f0f8343
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function needToken(): boolean {
/**
* Create a new agent based off the current settings.
*/
async function createHttpAgent(): Promise<ProxyAgent> {
export async function createHttpAgent(): Promise<ProxyAgent> {
const cfg = vscode.workspace.getConfiguration()
const insecure = Boolean(cfg.get("coder.insecure"))
const certFile = expandPath(String(cfg.get("coder.tlsCertFile") ?? "").trim())
Expand Down
37 changes: 20 additions & 17 deletions src/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { WebSocket } from "ws"
import { errToStr } from "./api-helper"
import { Storage } from "./storage"
import { ProxyAgent } from "proxy-agent"

Check failure on line 6 in src/inbox.ts

View workflow job for this annotation

GitHub Actions / lint

`proxy-agent` import should occur before import of `vscode`

type InboxMessage = {
unread_count: number
Expand All @@ -29,31 +30,33 @@
private socket: WebSocket

constructor(
private readonly restClient: Api,
httpAgent: ProxyAgent,
restClient: Api,
private readonly storage: Storage,
) {
// const url = this.restClient.getAxiosInstance().defaults.baseURL
const token = this.restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as
| string
| undefined
// const inboxUrl = new URL(`${url}/api/v2/notifications/watch`);
const inboxUrl = new URL(`ws://localhost:8080`)

this.storage.writeToCoderOutputChannel("Listening to Coder Inbox")

// We're gonna connect over WebSocket so replace the scheme.
if (inboxUrl.protocol === "https") {
inboxUrl.protocol = "wss"
} else if (inboxUrl.protocol === "http") {
inboxUrl.protocol = "ws"
const baseUrlRaw = restClient.getAxiosInstance().defaults.baseURL
if (!baseUrlRaw) {
throw new Error("No base URL set on REST client")
}

this.socket = new WebSocket(inboxUrl, {
const baseUrl = new URL(baseUrlRaw)
const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:"
const socketUrlRaw = `${socketProto}//${baseUrl.host}/api/v2/notifications/watch`

this.socket = new WebSocket(new URL(socketUrlRaw), {
followRedirects: true,
agent: httpAgent,
headers: {
"Coder-Session-Token": token,
"Coder-Session-Token": restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as
| string
| undefined,
8000
},
})

this.socket.on("open", () => {
this.storage.writeToCoderOutputChannel("Listening to Coder Inbox")
})

this.socket.on("error", (error) => {
this.notifyError(error)
})
Expand Down
5 changes: 3 additions & 2 deletions src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import prettyBytes from "pretty-bytes"
import * as semver from "semver"
import * as vscode from "vscode"
import { makeCoderSdk, needToken, startWorkspaceIfStoppedOrFailed, waitForBuild } from "./api"
import { createHttpAgent, makeCoderSdk, needToken, startWorkspaceIfStoppedOrFailed, waitForBuild } from "./api"
import { extractA 7EA3 gents } from "./api-helper"
import * as cli from "./cliManager"
import { Commands } from "./commands"
Expand Down Expand Up @@ -405,7 +405,8 @@
disposables.push(monitor.onChange.event((w) => (this.commands.workspace = w)))

// Watch coder inbox for messages
const inbox = new Inbox(workspaceRestClient, this.storage)
const httpAgent = await createHttpAgent();

Check failure on line 408 in src/remote.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
const inbox = new Inbox(httpAgent, workspaceRestClient, this.storage)
disposables.push(inbox)

// Wait for the agent to connect.
Expand Down
Loading
0