@tsplc/agent 0.5.8 → 0.5.10

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 (49) hide show
  1. package/dist/cli.js +2462 -533
  2. package/dist/cli.js.map +30 -17
  3. package/dist/client.js +238 -36
  4. package/dist/client.js.map +4 -4
  5. package/dist/index.js +2162 -416
  6. package/dist/index.js.map +25 -13
  7. package/package.json +5 -5
  8. package/src/access.ts +257 -0
  9. package/src/agent.ts +101 -29
  10. package/src/audit.ts +64 -0
  11. package/src/cli.ts +58 -4
  12. package/src/client.ts +181 -36
  13. package/src/dashboard/index.ts +116 -0
  14. package/src/dashboard/ui/access.ts +192 -0
  15. package/src/dashboard/ui/actions.ts +142 -0
  16. package/src/dashboard/ui/api.ts +147 -0
  17. package/src/dashboard/ui/app.ts +41 -0
  18. package/src/dashboard/ui/approval.ts +132 -0
  19. package/src/dashboard/ui/audit.ts +115 -0
  20. package/src/dashboard/ui/contract.ts +16 -0
  21. package/src/dashboard/ui/dashboard.css +1332 -0
  22. package/src/dashboard/ui/deployment.ts +432 -0
  23. package/src/dashboard/ui/deployments.ts +317 -0
  24. package/src/dashboard/ui/details.ts +240 -0
  25. package/src/dashboard/ui/dom.ts +91 -0
  26. package/src/dashboard/ui/environment.ts +224 -0
  27. package/src/dashboard/ui/format.ts +118 -0
  28. package/src/dashboard/ui/logs.ts +127 -0
  29. package/src/dashboard/ui/main.ts +86 -0
  30. package/src/dashboard/ui/overlay.ts +146 -0
  31. package/src/dashboard/ui/shell.ts +171 -0
  32. package/src/dashboard/ui/source.ts +144 -0
  33. package/src/deploy.ts +138 -144
  34. package/src/deployments/health.ts +80 -0
  35. package/src/deployments/retention.ts +45 -0
  36. package/src/deployments/routes.ts +273 -0
  37. package/src/deployments/runner.ts +877 -0
  38. package/src/deployments/store.ts +233 -0
  39. package/src/environment.ts +205 -0
  40. package/src/index.ts +4 -0
  41. package/src/operations.ts +5 -1
  42. package/src/protocol.ts +218 -2
  43. package/src/releases.ts +73 -1
  44. package/src/routes.ts +52 -0
  45. package/src/secrets.ts +43 -2
  46. package/src/server-process.ts +33 -16
  47. package/src/sessions.ts +58 -0
  48. package/src/state.ts +2 -0
  49. package/src/tailscale.ts +112 -0
@@ -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>;