Skip to content

Commit e97e687

Browse files
committed
Create CodeApi and separate the API methods
1 parent f78f262 commit e97e687

16 files changed

+512
-530
lines changed

src/agentMetadataHelper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
AgentMetadataEvent,
55
AgentMetadataEventSchemaArray,
66
errToStr,
7-
} from "./api-helper";
8-
import { CoderWebSocketClient } from "./websocket/webSocketClient";
7+
} from "./api/api-helper";
8+
import { CodeApi } from "./api/codeApi";
99

1010
export type AgentMetadataWatcher = {
1111
onChange: vscode.EventEmitter<null>["event"];
@@ -20,9 +20,9 @@ export type AgentMetadataWatcher = {
2020
*/
2121
export function createAgentMetadataWatcher(
2222
agentId: WorkspaceAgent["id"],
23-
webSocketClient: CoderWebSocketClient,
23+
client: CodeApi,
2424
): AgentMetadataWatcher {
25-
const socket = webSocketClient.watchAgentMetadata(agentId);
25+
const socket = client.watchAgentMetadata(agentId);
2626

2727
let disposed = false;
2828
const onChange = new vscode.EventEmitter<null>();

src/api.ts

Lines changed: 0 additions & 285 deletions
This file was deleted.

src/api-helper.ts renamed to src/api/api-helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
import { ErrorEvent } from "eventsource";
88
import { z } from "zod";
99

10+
/**
11+
* Convert various error types to readable strings
12+
*/
1013
export function errToStr(
1114
error: unknown,
1215
def: string = "No error message provided",
@@ -27,6 +30,13 @@ export function errToStr(
2730
return def;
2831
}
2932

33+
/**
34+
* Create workspace owner/name identifier
35+
*/
36+
export function createWorkspaceIdentifier(workspace: Workspace): string {
37+
return `${workspace.owner_name}/${workspace.name}`;
38+
}
39+
3040
export function extractAllAgents(
3141
workspaces: readonly Workspace[],
3242
): WorkspaceAgent[] {

src/api/auth.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import fs from "fs";
2+
import { ProxyAgent } from "proxy-agent";
3+
import { type WorkspaceConfiguration } from "vscode";
4+
import { getProxyForUrl } from "../proxy";
5+
import { expandPath } from "../util";
6+
7+
/**
8+
* Return whether the API will need a token for authorization.
9+
* If mTLS is in use (as specified by the cert or key files being set) then
10+
* token authorization is disabled. Otherwise, it is enabled.
11+
*/
12+
export function needToken(cfg: WorkspaceConfiguration): boolean {
13+
const certFile = expandPath(
14+
String(cfg.get("coder.tlsCertFile") ?? "").trim(),
15+
);
16+
const keyFile = expandPath(String(cfg.get("coder.tlsKeyFile") ?? "").trim());
17+
return !certFile && !keyFile;
18+
}
19+
20+
/**
21+
* Create a new HTTP agent based on the current VS Code settings.
22+
* Configures proxy, TLS certificates, and security options.
23+
*/
24+
export function createHttpAgent(cfg: WorkspaceConfiguration): ProxyAgent {
25+
const insecure = Boolean(cfg.get("coder.insecure"));
26+
const certFile = expandPath(
27+
String(cfg.get("coder.tlsCertFile") ?? "").trim(),
28+
);
29+
const keyFile = expandPath(String(cfg.get("coder.tlsKeyFile") ?? "").trim());
30+
const caFile = expandPath(String(cfg.get("coder.tlsCaFile") ?? "").trim());
31+
const altHost = expandPath(String(cfg.get("coder.tlsAltHost") ?? "").trim());
32+
33+
return new ProxyAgent({
34+
// Called each time a request is made.
35+
getProxyForUrl: (url: string) => {
36+
return getProxyForUrl(
37+
url,
38+
cfg.get("http.proxy"),
39+
cfg.get("coder.proxyBypass"),
40+
);
41+
},
42+
cert: certFile === "" ? undefined : fs.readFileSync(certFile),
43+
key: keyFile === "" ? undefined : fs.readFileSync(keyFile),
44+
ca: caFile === "" ? undefined : fs.readFileSync(caFile),
45+
servername: altHost === "" ? undefined : altHost,
46+
// rejectUnauthorized defaults to true, so we need to explicitly set it to
47+
// false if we want to allow self-signed certificates.
48+
rejectUnauthorized: !insecure,
49+
});
50+
}

0 commit comments

Comments
 (0)