@eloxiv/daemon 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/Acp.d.ts +8 -0
- package/dist/Acp.d.ts.map +1 -0
- package/dist/Acp.js +268 -0
- package/dist/Acp.js.map +1 -0
- package/dist/Canonical.d.ts +3 -0
- package/dist/Canonical.d.ts.map +1 -0
- package/dist/Canonical.js +12 -0
- package/dist/Canonical.js.map +1 -0
- package/dist/Client.d.ts +100 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +474 -0
- package/dist/Client.js.map +1 -0
- package/dist/Commands.d.ts +85 -0
- package/dist/Commands.d.ts.map +1 -0
- package/dist/Commands.js +370 -0
- package/dist/Commands.js.map +1 -0
- package/dist/Config.d.ts +74 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +126 -0
- package/dist/Config.js.map +1 -0
- package/dist/Daemon.d.ts +243 -0
- package/dist/Daemon.d.ts.map +1 -0
- package/dist/Daemon.js +965 -0
- package/dist/Daemon.js.map +1 -0
- package/dist/Harness.d.ts +133 -0
- package/dist/Harness.d.ts.map +1 -0
- package/dist/Harness.js +734 -0
- package/dist/Harness.js.map +1 -0
- package/dist/Http.d.ts +15 -0
- package/dist/Http.d.ts.map +1 -0
- package/dist/Http.js +548 -0
- package/dist/Http.js.map +1 -0
- package/dist/KernelLink.d.ts +145 -0
- package/dist/KernelLink.d.ts.map +1 -0
- package/dist/KernelLink.js +109 -0
- package/dist/KernelLink.js.map +1 -0
- package/dist/LocalAuth.d.ts +99 -0
- package/dist/LocalAuth.d.ts.map +1 -0
- package/dist/LocalAuth.js +225 -0
- package/dist/LocalAuth.js.map +1 -0
- package/dist/Main.d.ts +51 -0
- package/dist/Main.d.ts.map +1 -0
- package/dist/Main.js +219 -0
- package/dist/Main.js.map +1 -0
- package/dist/Secrets.d.ts +21 -0
- package/dist/Secrets.d.ts.map +1 -0
- package/dist/Secrets.js +204 -0
- package/dist/Secrets.js.map +1 -0
- package/dist/SecureUrl.d.ts +11 -0
- package/dist/SecureUrl.d.ts.map +1 -0
- package/dist/SecureUrl.js +23 -0
- package/dist/SecureUrl.js.map +1 -0
- package/dist/State.d.ts +77 -0
- package/dist/State.d.ts.map +1 -0
- package/dist/State.js +38 -0
- package/dist/State.js.map +1 -0
- package/dist/Terminals.d.ts +49 -0
- package/dist/Terminals.d.ts.map +1 -0
- package/dist/Terminals.js +203 -0
- package/dist/Terminals.js.map +1 -0
- package/dist/bin-node.d.ts +2 -0
- package/dist/bin-node.d.ts.map +1 -0
- package/dist/bin-node.js +4 -0
- package/dist/bin-node.js.map +1 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/bun.d.ts +11 -0
- package/dist/runtime/bun.d.ts.map +1 -0
- package/dist/runtime/bun.js +20 -0
- package/dist/runtime/bun.js.map +1 -0
- package/dist/runtime/node.d.ts +11 -0
- package/dist/runtime/node.d.ts.map +1 -0
- package/dist/runtime/node.js +21 -0
- package/dist/runtime/node.js.map +1 -0
- package/dist/testing.d.ts +25 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/fixtures/claude-code-env-vars.txt +409 -0
- package/fixtures/fake-acp-agent.mjs +222 -0
- package/fixtures/fake-harness.mjs +299 -0
- package/fixtures/recorded/claude-code.jsonl +7 -0
- package/fixtures/recorded/codex.jsonl +6 -0
- package/package.json +64 -3
- package/scripts/build-sidecar.ts +86 -0
- package/src/Acp.ts +292 -0
- package/src/Canonical.ts +9 -0
- package/src/Client.ts +585 -0
- package/src/Commands.ts +427 -0
- package/src/Config.ts +179 -0
- package/src/Daemon.ts +1092 -0
- package/src/Harness.ts +839 -0
- package/src/Http.ts +563 -0
- package/src/KernelLink.ts +197 -0
- package/src/LocalAuth.ts +301 -0
- package/src/Main.ts +281 -0
- package/src/Secrets.ts +226 -0
- package/src/SecureUrl.ts +21 -0
- package/src/State.ts +109 -0
- package/src/Terminals.ts +227 -0
- package/src/bin-node.ts +4 -0
- package/src/bin.ts +4 -0
- package/src/index.ts +16 -0
- package/src/runtime/bun.ts +29 -0
- package/src/runtime/node.ts +30 -0
- package/src/testing.ts +43 -0
package/dist/Secrets.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custody of the device credential: a small secret store over the operating system's secure storage, with a
|
|
3
|
+
* file fallback.
|
|
4
|
+
*
|
|
5
|
+
* - `libsecret` (Linux: GNOME Keyring, KWallet through the Secret Service) via `secret-tool`;
|
|
6
|
+
* - `keychain` (macOS) via `security`;
|
|
7
|
+
* - `wincred` (Windows Credential Manager) via PowerShell and `CredRead`/`CredWrite`;
|
|
8
|
+
* - `file`: `<state dir>/secrets.json`, mode 0600 in a 0700 directory, written atomically. Anyone who can read
|
|
9
|
+
* the user's files can read it; use it only where no secure storage exists (headless servers, CI).
|
|
10
|
+
*
|
|
11
|
+
* `auto` picks the platform's store when its tool works (probed with a throwaway secret) and falls back to the
|
|
12
|
+
* file store, saying so in the status. Secrets never go in argv where the tool allows otherwise (`secret-tool`
|
|
13
|
+
* reads stdin; `security` and PowerShell get the value through stdin or the environment).
|
|
14
|
+
*/
|
|
15
|
+
import { execFile, spawn } from "node:child_process";
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
const SERVICE = "eloxiv-daemon";
|
|
20
|
+
const run = (command, args, options = {}) => new Promise((resolve) => {
|
|
21
|
+
const child = spawn(command, [...args], { stdio: ["pipe", "pipe", "pipe"], env: options.env ?? process.env });
|
|
22
|
+
let stdout = "";
|
|
23
|
+
let stderr = "";
|
|
24
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), 15_000);
|
|
25
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => (stdout += chunk));
|
|
26
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => (stderr += chunk));
|
|
27
|
+
child.on("error", (error) => {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
resolve({ code: -1, stdout, stderr: String(error) });
|
|
30
|
+
});
|
|
31
|
+
child.on("close", (code) => {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
resolve({ code: code ?? -1, stdout, stderr });
|
|
34
|
+
});
|
|
35
|
+
child.stdin.end(options.input ?? "");
|
|
36
|
+
});
|
|
37
|
+
const onPath = (command) => new Promise((resolve) => {
|
|
38
|
+
execFile(process.platform === "win32" ? "where" : "which", [command], (error) => resolve(error === null));
|
|
39
|
+
});
|
|
40
|
+
/** The file store: `<dir>/secrets.json`, 0600, written by rename so a crash never leaves half a file. */
|
|
41
|
+
export const fileSecretStore = (dir) => {
|
|
42
|
+
const path = join(dir, "secrets.json");
|
|
43
|
+
const read = () => {
|
|
44
|
+
if (!existsSync(path))
|
|
45
|
+
return {};
|
|
46
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
47
|
+
};
|
|
48
|
+
const write = (secrets) => {
|
|
49
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
50
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
51
|
+
writeFileSync(temporary, `${JSON.stringify(secrets)}\n`, { mode: 0o600 });
|
|
52
|
+
chmodSync(temporary, 0o600);
|
|
53
|
+
renameSync(temporary, path);
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
kind: "file",
|
|
57
|
+
get: async (key) => read()[key],
|
|
58
|
+
set: async (key, value) => write({ ...read(), [key]: value }),
|
|
59
|
+
delete: async (key) => {
|
|
60
|
+
const secrets = read();
|
|
61
|
+
if (!(key in secrets))
|
|
62
|
+
return;
|
|
63
|
+
delete secrets[key];
|
|
64
|
+
write(secrets);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export const libsecretStore = () => ({
|
|
69
|
+
kind: "libsecret",
|
|
70
|
+
get: async (key) => {
|
|
71
|
+
const result = await run("secret-tool", ["lookup", "service", SERVICE, "account", key]);
|
|
72
|
+
return result.code === 0 && result.stdout.length > 0 ? result.stdout : undefined;
|
|
73
|
+
},
|
|
74
|
+
set: async (key, value) => {
|
|
75
|
+
const result = await run("secret-tool", ["store", `--label=Eloxiv daemon (${key})`, "service", SERVICE, "account", key], {
|
|
76
|
+
input: value,
|
|
77
|
+
});
|
|
78
|
+
if (result.code !== 0)
|
|
79
|
+
throw new Error(`secret-tool store failed: ${result.stderr.trim()}`);
|
|
80
|
+
},
|
|
81
|
+
delete: async (key) => {
|
|
82
|
+
await run("secret-tool", ["clear", "service", SERVICE, "account", key]);
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
export const keychainStore = () => ({
|
|
86
|
+
kind: "keychain",
|
|
87
|
+
get: async (key) => {
|
|
88
|
+
const result = await run("security", ["find-generic-password", "-s", SERVICE, "-a", key, "-w"]);
|
|
89
|
+
return result.code === 0 ? result.stdout.replace(/\n$/u, "") : undefined;
|
|
90
|
+
},
|
|
91
|
+
set: async (key, value) => {
|
|
92
|
+
// `-w` as the last option with no value makes `security` read the password from stdin (twice: entry and
|
|
93
|
+
// confirmation), so it never appears in the process list.
|
|
94
|
+
const result = await run("security", ["add-generic-password", "-U", "-s", SERVICE, "-a", key, "-w"], {
|
|
95
|
+
input: `${value}\n${value}\n`,
|
|
96
|
+
});
|
|
97
|
+
if (result.code !== 0)
|
|
98
|
+
throw new Error(`security add-generic-password failed: ${result.stderr.trim()}`);
|
|
99
|
+
},
|
|
100
|
+
delete: async (key) => {
|
|
101
|
+
await run("security", ["delete-generic-password", "-s", SERVICE, "-a", key]);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
/** CredRead/CredWrite/CredDelete through PowerShell; the value travels in an environment variable. */
|
|
105
|
+
const WINCRED_SCRIPT = `
|
|
106
|
+
$ErrorActionPreference = 'Stop'
|
|
107
|
+
Add-Type -TypeDefinition @"
|
|
108
|
+
using System; using System.Runtime.InteropServices; using System.Text;
|
|
109
|
+
public static class EloxivCred {
|
|
110
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
111
|
+
public struct CREDENTIAL { public int Flags; public int Type; public string TargetName; public string Comment;
|
|
112
|
+
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten; public int CredentialBlobSize;
|
|
113
|
+
public IntPtr CredentialBlob; public int Persist; public int AttributeCount; public IntPtr Attributes;
|
|
114
|
+
public string TargetAlias; public string UserName; }
|
|
115
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
116
|
+
public static extern bool CredRead(string target, int type, int flags, out IntPtr cred);
|
|
117
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
118
|
+
public static extern bool CredWrite(ref CREDENTIAL cred, int flags);
|
|
119
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
120
|
+
public static extern bool CredDelete(string target, int type, int flags);
|
|
121
|
+
[DllImport("advapi32.dll")] public static extern void CredFree(IntPtr cred);
|
|
122
|
+
public static string Read(string target) {
|
|
123
|
+
IntPtr p; if (!CredRead(target, 1, 0, out p)) return null;
|
|
124
|
+
var c = (CREDENTIAL)Marshal.PtrToStructure(p, typeof(CREDENTIAL));
|
|
125
|
+
var s = Marshal.PtrToStringUni(c.CredentialBlob, c.CredentialBlobSize / 2); CredFree(p); return s; }
|
|
126
|
+
public static void Write(string target, string value) {
|
|
127
|
+
var bytes = Encoding.Unicode.GetBytes(value); var c = new CREDENTIAL(); c.Type = 1; c.TargetName = target;
|
|
128
|
+
c.CredentialBlobSize = bytes.Length; c.CredentialBlob = Marshal.AllocHGlobal(bytes.Length);
|
|
129
|
+
Marshal.Copy(bytes, 0, c.CredentialBlob, bytes.Length); c.Persist = 2; c.UserName = "eloxiv";
|
|
130
|
+
try { if (!CredWrite(ref c, 0)) throw new Exception("CredWrite " + Marshal.GetLastWin32Error()); }
|
|
131
|
+
finally { Marshal.FreeHGlobal(c.CredentialBlob); } }
|
|
132
|
+
}
|
|
133
|
+
"@
|
|
134
|
+
switch ($env:ELOXIV_CRED_OP) {
|
|
135
|
+
'get' { $v = [EloxivCred]::Read($env:ELOXIV_CRED_TARGET); if ($v -ne $null) { [Console]::Out.Write($v) } else { exit 2 } }
|
|
136
|
+
'set' { [EloxivCred]::Write($env:ELOXIV_CRED_TARGET, $env:ELOXIV_CRED_VALUE) }
|
|
137
|
+
'delete' { [void][EloxivCred]::CredDelete($env:ELOXIV_CRED_TARGET, 1, 0) }
|
|
138
|
+
}
|
|
139
|
+
`;
|
|
140
|
+
export const wincredStore = () => {
|
|
141
|
+
const call = (op, key, value = "") => run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", "-"], {
|
|
142
|
+
input: WINCRED_SCRIPT,
|
|
143
|
+
env: { ...process.env, ELOXIV_CRED_OP: op, ELOXIV_CRED_TARGET: `${SERVICE}:${key}`, ELOXIV_CRED_VALUE: value },
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
kind: "wincred",
|
|
147
|
+
get: async (key) => {
|
|
148
|
+
const result = await call("get", key);
|
|
149
|
+
return result.code === 0 ? result.stdout : undefined;
|
|
150
|
+
},
|
|
151
|
+
set: async (key, value) => {
|
|
152
|
+
const result = await call("set", key, value);
|
|
153
|
+
if (result.code !== 0)
|
|
154
|
+
throw new Error(`CredWrite failed: ${result.stderr.trim()}`);
|
|
155
|
+
},
|
|
156
|
+
delete: async (key) => {
|
|
157
|
+
await call("delete", key);
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
/** Does `store` work here? Store, read back and delete a throwaway secret. */
|
|
162
|
+
const works = async (store) => {
|
|
163
|
+
const key = `probe-${process.pid}-${Date.now()}`;
|
|
164
|
+
try {
|
|
165
|
+
await store.set(key, "probe");
|
|
166
|
+
const value = await store.get(key);
|
|
167
|
+
await store.delete(key);
|
|
168
|
+
return value === "probe";
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* The store for `kind`. `auto` tries the platform's secure storage and falls back to the file store; an
|
|
176
|
+
* explicit kind is used as asked (and fails loudly if it does not work).
|
|
177
|
+
*/
|
|
178
|
+
export const selectSecretStore = async (kind, stateDir) => {
|
|
179
|
+
switch (kind) {
|
|
180
|
+
case "file":
|
|
181
|
+
return fileSecretStore(stateDir);
|
|
182
|
+
case "libsecret":
|
|
183
|
+
return libsecretStore();
|
|
184
|
+
case "keychain":
|
|
185
|
+
return keychainStore();
|
|
186
|
+
case "wincred":
|
|
187
|
+
return wincredStore();
|
|
188
|
+
case "auto": {
|
|
189
|
+
const candidate = process.platform === "linux" && process.env["DBUS_SESSION_BUS_ADDRESS"] && (await onPath("secret-tool"))
|
|
190
|
+
? libsecretStore()
|
|
191
|
+
: process.platform === "darwin" && (await onPath("security"))
|
|
192
|
+
? keychainStore()
|
|
193
|
+
: process.platform === "win32"
|
|
194
|
+
? wincredStore()
|
|
195
|
+
: undefined;
|
|
196
|
+
if (candidate !== undefined && (await works(candidate)))
|
|
197
|
+
return candidate;
|
|
198
|
+
return fileSecretStore(stateDir);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
/** The key the device credential is stored under: one per state directory, so daemons on one machine never share. */
|
|
203
|
+
export const credentialKey = (stateDir) => `device-credential-${createHash("sha256").update(stateDir).digest("hex").slice(0, 16)}`;
|
|
204
|
+
//# sourceMappingURL=Secrets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Secrets.js","sour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ient's
|
|
3
|
+
* kernel and zero-cache): https, or http only on loopback. Anything else would put the credential on the wire in
|
|
4
|
+
* clear.
|
|
5
|
+
*/
|
|
6
|
+
export declare class InsecureUrl extends Error {
|
|
7
|
+
readonly name = "InsecureUrl";
|
|
8
|
+
}
|
|
9
|
+
/** `raw` without a trailing slash when it is https or loopback http; otherwise `InsecureUrl`. */
|
|
10
|
+
export declare const secureUrl: (raw: string, what?: string) => string;
|
|
11
|
+
//# sourceMappingURL=SecureUrl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SecureUrl.d.ts","sourceRoot":"","sources":["../src/SecureUrl.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACpC,SAAkB,IAAI,iBAAgB;CACvC;AAED,iGAAiG;AACjG,eAAO,MAAM,SAAS,QAAS,MAAM,oBAAwB,MAU5D,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transport rule for every URL the daemon sends a credential to (the kernel it pairs with, LocalClient's
|
|
3
|
+
* kernel and zero-cache): https, or http only on loopback. Anything else would put the credential on the wire in
|
|
4
|
+
* clear.
|
|
5
|
+
*/
|
|
6
|
+
export class InsecureUrl extends Error {
|
|
7
|
+
name = "InsecureUrl";
|
|
8
|
+
}
|
|
9
|
+
/** `raw` without a trailing slash when it is https or loopback http; otherwise `InsecureUrl`. */
|
|
10
|
+
export const secureUrl = (raw, what = "The kernel") => {
|
|
11
|
+
let url;
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(raw);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
throw new InsecureUrl(`Not a URL: ${raw}`);
|
|
17
|
+
}
|
|
18
|
+
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
19
|
+
if (url.protocol === "https:" || (url.protocol === "http:" && loopback))
|
|
20
|
+
return url.toString().replace(/\/+$/u, "");
|
|
21
|
+
throw new InsecureUrl(`${what} must be https (or http on loopback): ${raw}`);
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=SecureUrl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SecureUrl.js","sourceRoot":"","sources":["../src/SecureUrl.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IAClB,IAAI,GAAG,aAAa,CAAA;CACvC;AAED,iGAAiG;AACjG,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAW,EAAE,IAAI,GAAG,YAAY,EAAU,EAAE;IACpE,IAAI,GAAQ,CAAA;IACZ,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAA;IACpB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,WAAW,CAAC,cAAc,GAAG,EAAE,CAAC,CAAA;IAC5C,CAAC;IACD,MAAM,QAAQ,GAAG,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC3E,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,GAAG,CAAC,QAAQ,KAAK,OAAO,IAAI,QAAQ,CAAC;QAAE,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IACnH,MAAM,IAAI,WAAW,CAAC,GAAG,IAAI,yCAAyC,GAAG,EAAE,CAAC,CAAA;AAC9E,CAAC,CAAA"}
|
package/dist/State.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's persistent state: `<state dir>/state.json` (0600 in a 0700 directory, written atomically).
|
|
3
|
+
* It holds the kernel pairing (never the credential, which is in the secret store), the local clients paired
|
|
4
|
+
* with the daemon (their tokens only as SHA-256 hashes) and the turn a runner is working on.
|
|
5
|
+
*/
|
|
6
|
+
import type { HarnessKind } from "@eloxiv/contracts";
|
|
7
|
+
export interface KernelPairing {
|
|
8
|
+
readonly url: string;
|
|
9
|
+
readonly deviceId: string;
|
|
10
|
+
readonly orgId: string;
|
|
11
|
+
readonly subject: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly kind: "desktop" | "runner";
|
|
14
|
+
readonly pairedAt: string;
|
|
15
|
+
/** Set when the kernel refused the credential (the device was revoked, or its person left). */
|
|
16
|
+
readonly revokedAt?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
export interface LocalClient {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly kind: "shell" | "browser-extension" | "cli" | "computer-use" | "desktop-app";
|
|
22
|
+
readonly tokenHash: string;
|
|
23
|
+
readonly pairedAt: string;
|
|
24
|
+
}
|
|
25
|
+
export interface DaemonState {
|
|
26
|
+
readonly version: 1;
|
|
27
|
+
readonly kernel?: KernelPairing | undefined;
|
|
28
|
+
readonly localClients: ReadonlyArray<LocalClient>;
|
|
29
|
+
/** The turn a runner claimed and has not settled (diagnostics; the kernel's lease is authoritative). */
|
|
30
|
+
readonly inflight?: {
|
|
31
|
+
readonly turnId: string;
|
|
32
|
+
readonly attempt: number;
|
|
33
|
+
readonly since: string;
|
|
34
|
+
} | undefined;
|
|
35
|
+
/** A finished turn's outcome the kernel has not taken yet: reported before anything else, also after a restart. */
|
|
36
|
+
readonly pendingCompletion?: PendingCompletion | undefined;
|
|
37
|
+
/** The harness sessions resumable turns kept: the only ones a follow-up may resume (newest last). */
|
|
38
|
+
readonly harnessSessions?: ReadonlyArray<HarnessSessionRecord> | undefined;
|
|
39
|
+
}
|
|
40
|
+
/** A harness session a resumable turn kept, and the working directory it belongs to. */
|
|
41
|
+
export interface HarnessSessionRecord {
|
|
42
|
+
/** The harness's own session (Claude Code) or thread (Codex) id. */
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly harness: HarnessKind;
|
|
45
|
+
readonly cwd: string;
|
|
46
|
+
/** The daemon created `cwd` for the session (removed when the session is forgotten). */
|
|
47
|
+
readonly owned: boolean;
|
|
48
|
+
readonly turnId: string;
|
|
49
|
+
readonly lastUsedAt: string;
|
|
50
|
+
}
|
|
51
|
+
export interface PendingCompletion {
|
|
52
|
+
readonly turnId: string;
|
|
53
|
+
readonly lease: string;
|
|
54
|
+
readonly outcome: "succeeded" | "failed";
|
|
55
|
+
readonly result: string | null;
|
|
56
|
+
readonly usage: {
|
|
57
|
+
readonly inputTokens: number;
|
|
58
|
+
readonly outputTokens: number;
|
|
59
|
+
readonly cachedInputTokens: number | null;
|
|
60
|
+
readonly costUsd: number | null;
|
|
61
|
+
} | null;
|
|
62
|
+
readonly error: string | null;
|
|
63
|
+
readonly sessionId?: string | null;
|
|
64
|
+
}
|
|
65
|
+
export declare const emptyState: DaemonState;
|
|
66
|
+
export declare const sha256Hex: (value: string) => string;
|
|
67
|
+
/** The default state directory: per user, per platform. */
|
|
68
|
+
export declare const defaultStateDir: (env?: Readonly<Record<string, string | undefined>>) => string;
|
|
69
|
+
export declare class StateFile {
|
|
70
|
+
#private;
|
|
71
|
+
readonly path: string;
|
|
72
|
+
readonly dir: string;
|
|
73
|
+
constructor(dir: string);
|
|
74
|
+
get current(): DaemonState;
|
|
75
|
+
update(change: (state: DaemonState) => DaemonState): DaemonState;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=State.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"State.d.ts","sourceRoot":"","sources":["../src/State.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAMpD,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CA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IAAI,WAAW,CAEzB;IAED,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,WAAW,GAAG,WAAW,CAQ/D;CACF"}
|
package/dist/State.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
export const emptyState = { version: 1, localClients: [] };
|
|
6
|
+
export const sha256Hex = (value) => createHash("sha256").update(value, "utf8").digest("hex");
|
|
7
|
+
/** The default state directory: per user, per platform. */
|
|
8
|
+
export const defaultStateDir = (env = process.env) => {
|
|
9
|
+
if (process.platform === "win32")
|
|
10
|
+
return join(env["APPDATA"] ?? join(homedir(), "AppData", "Roaming"), "Eloxiv", "daemon");
|
|
11
|
+
if (process.platform === "darwin")
|
|
12
|
+
return join(homedir(), "Library", "Application Support", "Eloxiv", "daemon");
|
|
13
|
+
return join(env["XDG_STATE_HOME"] ?? join(homedir(), ".local", "state"), "eloxiv", "daemon");
|
|
14
|
+
};
|
|
15
|
+
export class StateFile {
|
|
16
|
+
path;
|
|
17
|
+
#state;
|
|
18
|
+
dir;
|
|
19
|
+
constructor(dir) {
|
|
20
|
+
this.dir = dir;
|
|
21
|
+
this.path = join(dir, "state.json");
|
|
22
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
23
|
+
this.#state = existsSync(this.path) ? JSON.parse(readFileSync(this.path, "utf8")) : emptyState;
|
|
24
|
+
}
|
|
25
|
+
get current() {
|
|
26
|
+
return this.#state;
|
|
27
|
+
}
|
|
28
|
+
update(change) {
|
|
29
|
+
const next = change(this.#state);
|
|
30
|
+
const temporary = `${this.path}.${process.pid}.tmp`;
|
|
31
|
+
writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
32
|
+
chmodSync(temporary, 0o600);
|
|
33
|
+
renameSync(temporary, this.path);
|
|
34
|
+
this.#state = next;
|
|
35
|
+
return next;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=State.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"State.js","sourceRoot":"","sources":["../src/State.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACnG,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AA6DhC,MAAM,CAAC,MAAM,UAAU,GAAgB,EAAE,OAAO,EAAE,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAA;AAEvE,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAEpG,2DAA2D;AAC3D,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,GAAG,GAAiD,OAAO,CAAC,GAAG,EAAE,EAAE;IACjG,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO;QAC9B,OAAO,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA;IAC1F,IAAI,OAAO,CAAC,QAAQ,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,qBAAqB,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA;IAC/G,OAAO,IAAI,CAAC,GAAG,CAAC,gBAA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 -0,0 +1,49 @@
|
|
|
1
|
+
/** Terminals open at once, per daemon. */
|
|
2
|
+
export declare const MAX_TERMINALS = 8;
|
|
3
|
+
/** A terminal no client has watched for this long is closed (ms). */
|
|
4
|
+
export declare const TERMINAL_IDLE_MS: number;
|
|
5
|
+
type Listener = (event: {
|
|
6
|
+
readonly type: "output";
|
|
7
|
+
readonly data: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly type: "exit";
|
|
10
|
+
readonly code: number | null;
|
|
11
|
+
}) => void;
|
|
12
|
+
export declare class TerminalError extends Error {
|
|
13
|
+
readonly status: 403 | 404 | 429;
|
|
14
|
+
constructor(status: 403 | 404 | 429, message: string);
|
|
15
|
+
}
|
|
16
|
+
/** Whether terminals here get a pseudo-terminal (Bun) or pipes (Node). */
|
|
17
|
+
export declare const ptyAvailable: () => boolean;
|
|
18
|
+
export declare class Terminals {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(onChange: (terminal: {
|
|
21
|
+
id: string;
|
|
22
|
+
startedAt: string;
|
|
23
|
+
endedAt: string | null;
|
|
24
|
+
pty: boolean;
|
|
25
|
+
}) => void);
|
|
26
|
+
/** Open a shell in `cwd` for the client `owner`. */
|
|
27
|
+
open(owner: string, input: {
|
|
28
|
+
readonly cwd: string;
|
|
29
|
+
readonly cols: number;
|
|
30
|
+
readonly rows: number;
|
|
31
|
+
}): {
|
|
32
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
33
|
+
pty: boolean;
|
|
34
|
+
};
|
|
35
|
+
list(owner: string): {
|
|
36
|
+
id: string;
|
|
37
|
+
pty: boolean;
|
|
38
|
+
startedAt: string;
|
|
39
|
+
}[];
|
|
40
|
+
write(owner: string, id: string, data: string): void;
|
|
41
|
+
resize(owner: string, id: string, cols: number, rows: number): void;
|
|
42
|
+
/** Watch a terminal: its backlog first, then what follows; the returned function stops watching. */
|
|
43
|
+
watch(owner: string, id: string, listener: Listener): () => void;
|
|
44
|
+
close(owner: string, id: string): void;
|
|
45
|
+
/** Close every terminal (the daemon stops). */
|
|
46
|
+
closeAll(): void;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
|
49
|
+
//# sourceMappingURL=Terminals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Terminals.d.ts","sourceRoot":"","sources":["../src/Terminals.ts"],"names":[],"mappings":"AAeA,0CAA0C;AAC1C,eAAO,MAAM,aAAa,IAAI,CAAA;AAG9B,qEAAqE;AACrE,eAAO,MAAM,gBAAgB,QAAc,CAAA;AAE3C,KAAK,QAAQ,GAAG,CACd,KAAK,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,KAChH,IAAI,CAAA;AAsBT,qBAAa,aAAc,SAAQ,KAAK;IACtC,QAAQ,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,CAAA;IAChC,YAAY,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM,EAGnD;CACF;AAID,0EAA0E;AAC1E,eAAO,MAAM,YAAY,eAAiD,CAAA;AAM1E,qBAAa,SAAS;;IAKpB,YAAY,QAAQ,EAAE,CAAC,QAAQ,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,GAAG,EAAE,OAAO,CAAA;KAAE,KAAK,IAAI,EAgBhH;IAED,oDAAoD;IACpD,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE;;;MAgFhG;IASD,IAAI,CAAC,KAAK,EAAE,MAAM;;;;QAIjB;IAED,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAE5C;IAED,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAE3D;IAED,oGAAoG;IACpG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,MAAM,IAAI,CAQ/D;IAED,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAE9B;IAiBD,+CAA+C;IAC/C,QAAQ,SAIP;CACF"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal sessions for the person's local clients (DeviceControl): an interactive shell in the daemon's workspace,
|
|
3
|
+
* its output streamed to the client that opened it and its input written by that client only. On Bun the shell
|
|
4
|
+
* runs in a pseudo-terminal (`Bun.spawn`'s `terminal`); on Node, which has none built in, it runs with pipes
|
|
5
|
+
* (`pty: false`: no line editing or full-screen programs).
|
|
6
|
+
*
|
|
7
|
+
* Off unless the daemon serves terminal commands (`--allow-commands terminal`, with a workspace). The shell runs as
|
|
8
|
+
* the daemon's OS user with the harnesses' environment allow-list (no credentials), in its own process group, which
|
|
9
|
+
* is killed when the terminal is closed, when its shell exits, when no client has watched it for ten minutes, and
|
|
10
|
+
* when the daemon stops.
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { harnessEnv } from "./Harness.js";
|
|
15
|
+
/** Terminals open at once, per daemon. */
|
|
16
|
+
export const MAX_TERMINALS = 8;
|
|
17
|
+
/** Output kept for a client that (re)attaches (characters). */
|
|
18
|
+
const BACKLOG = 64 * 1024;
|
|
19
|
+
/** A terminal no client has watched for this long is closed (ms). */
|
|
20
|
+
export const TERMINAL_IDLE_MS = 10 * 60_000;
|
|
21
|
+
export class TerminalError extends Error {
|
|
22
|
+
status;
|
|
23
|
+
constructor(status, message) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.status = status;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const bunSpawn = globalThis.Bun;
|
|
29
|
+
/** Whether terminals here get a pseudo-terminal (Bun) or pipes (Node). */
|
|
30
|
+
export const ptyAvailable = () => typeof bunSpawn?.Terminal === "function";
|
|
31
|
+
/** Every daemon's terminals in this process, closed when the process exits (their shells are process groups). */
|
|
32
|
+
const everyTerminals = new Set();
|
|
33
|
+
let exitHook = false;
|
|
34
|
+
export class Terminals {
|
|
35
|
+
#open = new Map();
|
|
36
|
+
#sweep;
|
|
37
|
+
#onChange;
|
|
38
|
+
constructor(onChange) {
|
|
39
|
+
this.#onChange = onChange;
|
|
40
|
+
this.#sweep = setInterval(() => {
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
for (const terminal of this.#open.values()) {
|
|
43
|
+
if (terminal.listeners.size === 0 && now - terminal.idleSince > TERMINAL_IDLE_MS)
|
|
44
|
+
this.#end(terminal);
|
|
45
|
+
}
|
|
46
|
+
}, 60_000);
|
|
47
|
+
this.#sweep.unref();
|
|
48
|
+
everyTerminals.add(this);
|
|
49
|
+
if (!exitHook) {
|
|
50
|
+
exitHook = true;
|
|
51
|
+
process.once("exit", () => {
|
|
52
|
+
for (const terminals of everyTerminals)
|
|
53
|
+
terminals.closeAll();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Open a shell in `cwd` for the client `owner`. */
|
|
58
|
+
open(owner, input) {
|
|
59
|
+
if (this.#open.size >= MAX_TERMINALS)
|
|
60
|
+
throw new TerminalError(429, `At most ${MAX_TERMINALS} terminals are open at once`);
|
|
61
|
+
const id = randomUUID();
|
|
62
|
+
const windows = process.platform === "win32";
|
|
63
|
+
const shell = windows ? "cmd.exe" : "/bin/sh";
|
|
64
|
+
const env = harnessEnv({ TERM: "xterm-256color" });
|
|
65
|
+
const terminal = {
|
|
66
|
+
id,
|
|
67
|
+
owner,
|
|
68
|
+
pty: ptyAvailable(),
|
|
69
|
+
startedAt: new Date().toISOString(),
|
|
70
|
+
write: () => { },
|
|
71
|
+
resize: () => { },
|
|
72
|
+
kill: () => { },
|
|
73
|
+
backlog: "",
|
|
74
|
+
exited: undefined,
|
|
75
|
+
listeners: new Set(),
|
|
76
|
+
idleSince: Date.now(),
|
|
77
|
+
};
|
|
78
|
+
const emit = (data) => {
|
|
79
|
+
terminal.backlog = (terminal.backlog + data).slice(-BACKLOG);
|
|
80
|
+
for (const listener of terminal.listeners)
|
|
81
|
+
listener({ type: "output", data });
|
|
82
|
+
};
|
|
83
|
+
const exited = (code) => {
|
|
84
|
+
if (terminal.exited !== undefined)
|
|
85
|
+
return;
|
|
86
|
+
terminal.exited = { code };
|
|
87
|
+
for (const listener of terminal.listeners)
|
|
88
|
+
listener({ type: "exit", code });
|
|
89
|
+
this.#end(terminal);
|
|
90
|
+
};
|
|
91
|
+
if (terminal.pty) {
|
|
92
|
+
const decoder = new TextDecoder();
|
|
93
|
+
const proc = bunSpawn.spawn([shell], {
|
|
94
|
+
cwd: input.cwd,
|
|
95
|
+
env,
|
|
96
|
+
terminal: {
|
|
97
|
+
cols: input.cols,
|
|
98
|
+
rows: input.rows,
|
|
99
|
+
data: (_t, data) => emit(decoder.decode(data, { stream: true })),
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
Object.assign(terminal, {
|
|
103
|
+
write: (data) => proc.terminal.write(data),
|
|
104
|
+
resize: (cols, rows) => proc.terminal.resize(cols, rows),
|
|
105
|
+
kill: () => {
|
|
106
|
+
try {
|
|
107
|
+
process.kill(-proc.pid, "SIGKILL");
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
proc.kill("SIGKILL");
|
|
111
|
+
}
|
|
112
|
+
proc.terminal.close();
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
void proc.exited.then((code) => exited(code));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const child = spawn(shell, windows ? [] : ["-i"], {
|
|
119
|
+
cwd: input.cwd,
|
|
120
|
+
env,
|
|
121
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
122
|
+
detached: !windows,
|
|
123
|
+
});
|
|
124
|
+
child.stdout.setEncoding("utf8").on("data", emit);
|
|
125
|
+
child.stderr.setEncoding("utf8").on("data", emit);
|
|
126
|
+
child.on("error", () => exited(null));
|
|
127
|
+
child.on("close", (code) => exited(code));
|
|
128
|
+
Object.assign(terminal, {
|
|
129
|
+
write: (data) => child.stdin.write(data),
|
|
130
|
+
kill: () => {
|
|
131
|
+
try {
|
|
132
|
+
if (!windows && child.pid !== undefined)
|
|
133
|
+
process.kill(-child.pid, "SIGKILL");
|
|
134
|
+
else
|
|
135
|
+
child.kill("SIGKILL");
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Already gone.
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
this.#open.set(id, terminal);
|
|
144
|
+
this.#onChange({ id, startedAt: terminal.startedAt, endedAt: null, pty: terminal.pty });
|
|
145
|
+
return { id, pty: terminal.pty };
|
|
146
|
+
}
|
|
147
|
+
/** The terminal `id` if `owner` opened it (anyone else: not found). */
|
|
148
|
+
#owned(owner, id) {
|
|
149
|
+
const terminal = this.#open.get(id);
|
|
150
|
+
if (terminal === undefined || terminal.owner !== owner)
|
|
151
|
+
throw new TerminalError(404, "No such terminal");
|
|
152
|
+
return terminal;
|
|
153
|
+
}
|
|
154
|
+
list(owner) {
|
|
155
|
+
return [...this.#open.values()]
|
|
156
|
+
.filter((terminal) => terminal.owner === owner)
|
|
157
|
+
.map(({ id, pty, startedAt }) => ({ id, pty, startedAt }));
|
|
158
|
+
}
|
|
159
|
+
write(owner, id, data) {
|
|
160
|
+
this.#owned(owner, id).write(data);
|
|
161
|
+
}
|
|
162
|
+
resize(owner, id, cols, rows) {
|
|
163
|
+
this.#owned(owner, id).resize(cols, rows);
|
|
164
|
+
}
|
|
165
|
+
/** Watch a terminal: its backlog first, then what follows; the returned function stops watching. */
|
|
166
|
+
watch(owner, id, listener) {
|
|
167
|
+
const terminal = this.#owned(owner, id);
|
|
168
|
+
if (terminal.backlog.length > 0)
|
|
169
|
+
listener({ type: "output", data: terminal.backlog });
|
|
170
|
+
terminal.listeners.add(listener);
|
|
171
|
+
return () => {
|
|
172
|
+
terminal.listeners.delete(listener);
|
|
173
|
+
terminal.idleSince = Date.now();
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
close(owner, id) {
|
|
177
|
+
this.#end(this.#owned(owner, id));
|
|
178
|
+
}
|
|
179
|
+
#end(terminal) {
|
|
180
|
+
if (!this.#open.delete(terminal.id))
|
|
181
|
+
return;
|
|
182
|
+
terminal.kill();
|
|
183
|
+
if (terminal.exited === undefined) {
|
|
184
|
+
terminal.exited = { code: null };
|
|
185
|
+
for (const listener of terminal.listeners)
|
|
186
|
+
listener({ type: "exit", code: null });
|
|
187
|
+
}
|
|
188
|
+
this.#onChange({
|
|
189
|
+
id: terminal.id,
|
|
190
|
+
startedAt: terminal.startedAt,
|
|
191
|
+
endedAt: new Date().toISOString(),
|
|
192
|
+
pty: terminal.pty,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/** Close every terminal (the daemon stops). */
|
|
196
|
+
closeAll() {
|
|
197
|
+
clearInterval(this.#sweep);
|
|
198
|
+
everyTerminals.delete(this);
|
|
199
|
+
for (const terminal of Array.from(this.#open.values()))
|
|
200
|
+
this.#end(terminal);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=Terminals.js.map
|