@tsplc/tmp-agent 0.0.0-stage → 0.6.3

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 (65) hide show
  1. package/README.md +26 -2
  2. package/dist/cli.js +4888 -0
  3. package/dist/cli.js.map +53 -0
  4. package/dist/client.js +514 -0
  5. package/dist/client.js.map +12 -0
  6. package/dist/index.js +3680 -0
  7. package/dist/index.js.map +41 -0
  8. package/package.json +61 -4
  9. package/src/access.ts +260 -0
  10. package/src/agent.ts +461 -0
  11. package/src/audit.ts +74 -0
  12. package/src/cli.ts +290 -0
  13. package/src/client.ts +407 -0
  14. package/src/codesys.ts +195 -0
  15. package/src/dashboard/index.ts +159 -0
  16. package/src/dashboard/ui/access.ts +191 -0
  17. package/src/dashboard/ui/actions.ts +207 -0
  18. package/src/dashboard/ui/api.ts +138 -0
  19. package/src/dashboard/ui/app.ts +41 -0
  20. package/src/dashboard/ui/approval.ts +131 -0
  21. package/src/dashboard/ui/audit.ts +115 -0
  22. package/src/dashboard/ui/contract.ts +19 -0
  23. package/src/dashboard/ui/dashboard.css +1332 -0
  24. package/src/dashboard/ui/deployment.ts +431 -0
  25. package/src/dashboard/ui/deployments.ts +319 -0
  26. package/src/dashboard/ui/details.ts +214 -0
  27. package/src/dashboard/ui/dom.ts +87 -0
  28. package/src/dashboard/ui/environment.ts +345 -0
  29. package/src/dashboard/ui/format.ts +124 -0
  30. package/src/dashboard/ui/logs.ts +126 -0
  31. package/src/dashboard/ui/main.ts +88 -0
  32. package/src/dashboard/ui/overlay.ts +144 -0
  33. package/src/dashboard/ui/shell.ts +185 -0
  34. package/src/dashboard/ui/source.ts +144 -0
  35. package/src/debugging.ts +142 -0
  36. package/src/deploy.ts +203 -0
  37. package/src/deployments/health.ts +98 -0
  38. package/src/deployments/retention.ts +44 -0
  39. package/src/deployments/routes.ts +281 -0
  40. package/src/deployments/runner.ts +900 -0
  41. package/src/deployments/store.ts +209 -0
  42. package/src/deployments/submission.ts +78 -0
  43. package/src/discovery.ts +90 -0
  44. package/src/environment.ts +214 -0
  45. package/src/errors.ts +12 -0
  46. package/src/index.ts +36 -0
  47. package/src/instance.ts +51 -0
  48. package/src/login.ts +267 -0
  49. package/src/logs.ts +115 -0
  50. package/src/maintenance.ts +303 -0
  51. package/src/operations.ts +149 -0
  52. package/src/processes.ts +41 -0
  53. package/src/protocol.ts +557 -0
  54. package/src/releases.ts +414 -0
  55. package/src/restarts.ts +96 -0
  56. package/src/routes.ts +128 -0
  57. package/src/secrets.ts +174 -0
  58. package/src/server-process.ts +264 -0
  59. package/src/service.ts +194 -0
  60. package/src/sessions.ts +58 -0
  61. package/src/state.ts +126 -0
  62. package/src/tailscale.ts +112 -0
  63. package/src/terminal.ts +57 -0
  64. package/src/transfer.ts +94 -0
  65. package/src/upgrade.ts +153 -0
@@ -0,0 +1,149 @@
1
+ import { requiresDownload, transferRollback } from "@tsplc/build";
2
+ import { AgentError, errorMessage } from "./errors";
3
+ import type { AgentEvent } from "./protocol";
4
+
5
+ /** What the agent can be busy with, as its messages name it. */
6
+ export type Operation =
7
+ | "a deployment"
8
+ | "a login"
9
+ | "a logout"
10
+ | "a server restart"
11
+ | "a controller restart";
12
+
13
+ /**
14
+ * A failed operation as its clients hear it: whether only a full download can deploy, and what
15
+ * became of the controller after a download failed mid-transfer.
16
+ */
17
+ export const failureReport = (error: unknown) => {
18
+ const rollback = transferRollback(error);
19
+ return {
20
+ message: errorMessage(error),
21
+ downloadRequired: requiresDownload(error),
22
+ ...(rollback && { rollback }),
23
+ };
24
+ };
25
+
26
+ /** The agent runs one deployment, login or logout at a time, and none while it is held for an upgrade. */
27
+ export const createOperations = (log: (message: string) => void) => {
28
+ let busy: Operation | undefined;
29
+ let held = false;
30
+ const waiting = new Set<() => void>();
31
+
32
+ const requireIdle = () => {
33
+ if (held) {
34
+ throw new AgentError(
35
+ "The plant agent is being upgraded; try again once it is back",
36
+ 503,
37
+ );
38
+ }
39
+ if (busy) {
40
+ throw new AgentError(`The agent is busy with ${busy}`, 409);
41
+ }
42
+ };
43
+
44
+ const wakeWaiting = () => {
45
+ for (const wake of waiting) {
46
+ wake();
47
+ }
48
+ waiting.clear();
49
+ };
50
+
51
+ const acquire = (operation: Operation) => {
52
+ requireIdle();
53
+ busy = operation;
54
+ };
55
+
56
+ const free = () => {
57
+ busy = undefined;
58
+ wakeWaiting();
59
+ };
60
+
61
+ /** What keeps the agent from starting an operation, as its messages name it. */
62
+ const blockedBy = () => (held ? "the plant agent's upgrade" : busy);
63
+
64
+ return {
65
+ busy: () => busy,
66
+ blockedBy,
67
+ /** Resolves once nothing blocks the agent, which another caller may take again first. */
68
+ whenIdle: () =>
69
+ blockedBy()
70
+ ? new Promise<void>((resolve) => waiting.add(resolve))
71
+ : Promise.resolve(),
72
+ requireIdle,
73
+ /** Refuses every operation from now on; fails while one runs. */
74
+ hold: () => {
75
+ requireIdle();
76
+ held = true;
77
+ },
78
+ release: () => {
79
+ held = false;
80
+ wakeWaiting();
81
+ },
82
+ held: () => held,
83
+ /** Runs `run` as the agent's one operation. */
84
+ exclusive: async <Result>(
85
+ operation: Operation,
86
+ run: () => Promise<Result>,
87
+ ) => {
88
+ acquire(operation);
89
+ try {
90
+ return await run();
91
+ } finally {
92
+ free();
93
+ }
94
+ },
95
+ /**
96
+ * Streams one exclusive operation's events, then its result, as newline-delimited JSON. A
97
+ * failure names its `rollback` only to a client that `reportsRollback`.
98
+ */
99
+ stream: (
100
+ operation: Operation,
101
+ run: (emit: (event: AgentEvent) => void) => Promise<unknown>,
102
+ { reportsRollback = false }: { reportsRollback?: boolean } = {},
103
+ ) => {
104
+ acquire(operation);
105
+ const encoder = new TextEncoder();
106
+ // A client that disconnects doesn't stop the operation; it just stops hearing about it.
107
+ let listening = true;
108
+ return new Response(
109
+ new ReadableStream({
110
+ start: async (controller) => {
111
+ const write = (text: string) => {
112
+ if (listening) {
113
+ controller.enqueue(encoder.encode(text));
114
+ }
115
+ };
116
+ const emit = (event: AgentEvent) =>
117
+ write(`${JSON.stringify(event)}\n`);
118
+ // CODESYS can stay silent for minutes; blank lines keep proxies from closing the stream.
119
+ const heartbeat = setInterval(() => write("\n"), 15_000);
120
+ try {
121
+ emit({ kind: "done", result: await run(emit) });
122
+ } catch (error) {
123
+ log(
124
+ `${operation} failed: ${error instanceof Error ? error.stack : String(error)}`,
125
+ );
126
+ const { rollback, ...failure } = failureReport(error);
127
+ emit({
128
+ kind: "error",
129
+ ...failure,
130
+ ...(reportsRollback && rollback && { rollback }),
131
+ });
132
+ } finally {
133
+ clearInterval(heartbeat);
134
+ free();
135
+ if (listening) {
136
+ controller.close();
137
+ }
138
+ }
139
+ },
140
+ cancel: () => {
141
+ listening = false;
142
+ },
143
+ }),
144
+ { headers: { "Content-Type": "application/x-ndjson" } },
145
+ );
146
+ },
147
+ };
148
+ };
149
+ export type Operations = ReturnType<typeof createOperations>;
@@ -0,0 +1,41 @@
1
+ import { setTimeout as delay } from "node:timers/promises";
2
+
3
+ const isGone = (error: unknown) =>
4
+ error instanceof Error && "code" in error && error.code === "ESRCH";
5
+
6
+ /** Whether process `pid` exists; the agent can't wait on one an earlier agent started. */
7
+ export const processAlive = (pid: number) => {
8
+ try {
9
+ // Signal 0 checks that the process exists without signalling it.
10
+ process.kill(pid, 0);
11
+ return true;
12
+ } catch (error) {
13
+ return !isGone(error);
14
+ }
15
+ };
16
+
17
+ /** Signals process `pid`, which may have exited since it was last seen. */
18
+ export const signalProcess = (pid: number, name: NodeJS.Signals) => {
19
+ try {
20
+ process.kill(pid, name);
21
+ } catch (error) {
22
+ if (!isGone(error)) {
23
+ throw error;
24
+ }
25
+ }
26
+ };
27
+
28
+ /** Whether `condition` holds within `within` milliseconds, checking it each second. */
29
+ export const waitUntil = async (
30
+ condition: () => Promise<boolean>,
31
+ within: number,
32
+ ) => {
33
+ const deadline = Date.now() + within;
34
+ while (Date.now() < deadline) {
35
+ if (await condition()) {
36
+ return true;
37
+ }
38
+ await delay(1000);
39
+ }
40
+ return false;
41
+ };