@@ -3,6 +3,7 @@ import * as vscode from "vscode"
33import { WebSocket } from "ws"
44import { errToStr } from "./api-helper"
55import { Storage } from "./storage"
6+ import { ProxyAgent } from "proxy-agent"
67
78type 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 } )
0 commit comments