@tsplc/agent 0.5.9 → 0.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/cli.js +2660 -525
  2. package/dist/cli.js.map +32 -18
  3. package/dist/client.js +262 -37
  4. package/dist/client.js.map +4 -4
  5. package/dist/index.js +2325 -415
  6. package/dist/index.js.map +27 -14
  7. package/package.json +6 -5
  8. package/src/access.ts +276 -0
  9. package/src/agent.ts +130 -29
  10. package/src/audit.ts +64 -0
  11. package/src/cli.ts +81 -5
  12. package/src/client.ts +204 -36
  13. package/src/controller.ts +60 -0
  14. package/src/dashboard/index.ts +118 -0
  15. package/src/dashboard/ui/access.ts +192 -0
  16. package/src/dashboard/ui/actions.ts +192 -0
  17. package/src/dashboard/ui/api.ts +158 -0
  18. package/src/dashboard/ui/app.ts +41 -0
  19. package/src/dashboard/ui/approval.ts +132 -0
  20. package/src/dashboard/ui/audit.ts +115 -0
  21. package/src/dashboard/ui/contract.ts +19 -0
  22. package/src/dashboard/ui/dashboard.css +1332 -0
  23. package/src/dashboard/ui/deployment.ts +432 -0
  24. package/src/dashboard/ui/deployments.ts +317 -0
  25. package/src/dashboard/ui/details.ts +240 -0
  26. package/src/dashboard/ui/dom.ts +91 -0
  27. package/src/dashboard/ui/environment.ts +326 -0
  28. package/src/dashboard/ui/format.ts +118 -0
  29. package/src/dashboard/ui/logs.ts +127 -0
  30. package/src/dashboard/ui/main.ts +86 -0
  31. package/src/dashboard/ui/overlay.ts +146 -0
  32. package/src/dashboard/ui/shell.ts +192 -0
  33. package/src/dashboard/ui/source.ts +144 -0
  34. package/src/deploy.ts +174 -144
  35. package/src/deployments/health.ts +80 -0
  36. package/src/deployments/retention.ts +45 -0
  37. package/src/deployments/routes.ts +273 -0
  38. package/src/deployments/runner.ts +877 -0
  39. package/src/deployments/store.ts +233 -0
  40. package/src/environment.ts +284 -0
  41. package/src/index.ts +4 -0
  42. package/src/login.ts +17 -0
  43. package/src/operations.ts +6 -1
  44. package/src/protocol.ts +259 -2
  45. package/src/releases.ts +73 -1
  46. package/src/routes.ts +52 -0
  47. package/src/secrets.ts +74 -4
  48. package/src/server-process.ts +33 -16
  49. package/src/sessions.ts +58 -0
  50. package/src/state.ts +2 -0
  51. package/src/tailscale.ts +112 -0
@@ -0,0 +1,58 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import type { Identity } from "./protocol";
3
+
4
+ /** The cookie a signed-in browser sends: a random session id, never a token. */
5
+ export const sessionCookie = "tsplc_session";
6
+
7
+ /** How long a browser stays signed in. */
8
+ export const sessionLifetimeMs = 12 * 60 * 60 * 1000;
9
+
10
+ /**
11
+ * Browsers signed in with a token, kept in memory: restarting the agent signs every browser out.
12
+ */
13
+ export const createSessions = (now: () => number) => {
14
+ const sessions = new Map<string, { identity: Identity; expires: number }>();
15
+
16
+ const sessionId = (request: Request) =>
17
+ new Bun.CookieMap(request.headers.get("cookie") ?? "").get(sessionCookie);
18
+
19
+ const cookie = (value: string, maxAgeMs: number) =>
20
+ new Bun.Cookie(sessionCookie, value, {
21
+ httpOnly: true,
22
+ sameSite: "strict",
23
+ path: "/",
24
+ maxAge: Math.floor(maxAgeMs / 1000),
25
+ }).serialize();
26
+
27
+ return {
28
+ /** Signs a browser in as `identity`; the answer sets the returned cookie. */
29
+ open: (identity: Identity) => {
30
+ const time = now();
31
+ for (const [id, { expires }] of sessions) {
32
+ if (expires <= time) {
33
+ sessions.delete(id);
34
+ }
35
+ }
36
+ const id = randomBytes(32).toString("base64url");
37
+ sessions.set(id, { identity, expires: time + sessionLifetimeMs });
38
+ return cookie(id, sessionLifetimeMs);
39
+ },
40
+ /** Who the request's cookie signed in, while the session lasts. */
41
+ find: (request: Request) => {
42
+ const id = sessionId(request);
43
+ const session = id === null ? undefined : sessions.get(id);
44
+ if (!session || session.expires <= now()) {
45
+ return null;
46
+ }
47
+ return session.identity;
48
+ },
49
+ /** Ends the request's session; the answer sets the returned cookie, which clears it. */
50
+ close: (request: Request) => {
51
+ const id = sessionId(request);
52
+ if (id !== null) {
53
+ sessions.delete(id);
54
+ }
55
+ return cookie("", 0);
56
+ },
57
+ };
58
+ };
package/src/state.ts CHANGED
@@ -32,6 +32,8 @@ export const openState = async (directory: string) => {
32
32
  release: join(directory, "release.json"),
33
33
  target: join(directory, "target.json"),
34
34
  deployments: join(directory, "deployments.jsonl"),
35
+ /** Each deployment's record and log, under its id. */
36
+ deploymentRecords: join(directory, "deployments"),
35
37
  audit: join(directory, "audit.jsonl"),
36
38
  debug: join(directory, "debug"),
37
39
  /** The running server's process, for the agent that takes it over. */
@@ -0,0 +1,112 @@
1
+ import { BlockList, isIP } from "node:net";
2
+ import { z } from "zod";
3
+ import { errorMessage } from "./errors";
4
+
5
+ const tailnet = new BlockList();
6
+ tailnet.addSubnet("100.64.0.0", 10, "ipv4");
7
+ tailnet.addSubnet("fd7a:115c:a1e0::", 48, "ipv6");
8
+ const loopback = new BlockList();
9
+ loopback.addSubnet("127.0.0.0", 8, "ipv4");
10
+ loopback.addAddress("::1", "ipv6");
11
+
12
+ /** Where a peer address is: this PC, a Tailscale address, or anywhere else. */
13
+ export const peerKind = (address: string) => {
14
+ const version = isIP(address);
15
+ if (version === 0) {
16
+ return "other";
17
+ }
18
+ const family = version === 6 ? "ipv6" : "ipv4";
19
+ if (loopback.check(address, family)) {
20
+ return "loopback";
21
+ }
22
+ return tailnet.check(address, family) ? "tailnet" : "other";
23
+ };
24
+
25
+ /** A Tailscale user, as `tailscale whois` names them. */
26
+ export interface TailnetUser {
27
+ login: string;
28
+ name: string;
29
+ }
30
+
31
+ /** The part of `tailscale whois --json` the agent reads; Tailscale adds fields between versions. */
32
+ const whoisSchema = z.object({
33
+ UserProfile: z.object({
34
+ LoginName: z.string().nonempty(),
35
+ DisplayName: z.string(),
36
+ }),
37
+ });
38
+
39
+ /** Runs a command to completion, with its standard output. */
40
+ export type RunCommand = (
41
+ command: string[],
42
+ ) => Promise<{ exitCode: number; stdout: string }>;
43
+
44
+ export const runCommand: RunCommand = async (command) => {
45
+ const child = Bun.spawn(command, {
46
+ stdout: "pipe",
47
+ stderr: "ignore",
48
+ timeout: 5000,
49
+ });
50
+ const [stdout, exitCode] = await Promise.all([
51
+ new Response(child.stdout).text(),
52
+ child.exited,
53
+ ]);
54
+ return { stdout, exitCode };
55
+ };
56
+
57
+ /** How long a lookup's answer, a user or none, is reused. */
58
+ export const whoisCacheMs = 60_000;
59
+
60
+ /**
61
+ * Who owns a tailnet address, from the local `tailscale` CLI, or null when it can't say: the
62
+ * address isn't a member's, or Tailscale isn't installed or running.
63
+ */
64
+ export const createWhois = (context: {
65
+ run: RunCommand;
66
+ now: () => number;
67
+ log: (message: string) => void;
68
+ }) => {
69
+ const cache = new Map<
70
+ string,
71
+ { expires: number; user: Promise<TailnetUser | null> }
72
+ >();
73
+
74
+ const lookUp = async (address: string) => {
75
+ try {
76
+ const { exitCode, stdout } = await context.run([
77
+ "tailscale",
78
+ "whois",
79
+ "--json",
80
+ address,
81
+ ]);
82
+ if (exitCode !== 0) {
83
+ return null;
84
+ }
85
+ const { UserProfile } = whoisSchema.parse(JSON.parse(stdout));
86
+ return {
87
+ login: UserProfile.LoginName,
88
+ name: UserProfile.DisplayName || UserProfile.LoginName,
89
+ };
90
+ } catch (error) {
91
+ context.log(`tailscale whois ${address} failed: ${errorMessage(error)}`);
92
+ return null;
93
+ }
94
+ };
95
+
96
+ return (address: string): Promise<TailnetUser | null> => {
97
+ const now = context.now();
98
+ for (const [cached, { expires }] of cache) {
99
+ if (expires <= now) {
100
+ cache.delete(cached);
101
+ }
102
+ }
103
+ const known = cache.get(address);
104
+ if (known) {
105
+ return known.user;
106
+ }
107
+ const user = lookUp(address);
108
+ cache.set(address, { expires: now + whoisCacheMs, user });
109
+ return user;
110
+ };
111
+ };
112
+ export type Whois = ReturnType<typeof createWhois>;