@x-tech-laboratories/x-app 0.128.0 → 0.130.1
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/README.md +28 -4
- package/dist/agent/claude_stream.d.ts +1 -22
- package/dist/agent/claude_stream.js +2 -85
- package/dist/agent/client.d.ts +1 -35
- package/dist/agent/client.js +2 -169
- package/dist/agent/commands.d.ts +1 -22
- package/dist/agent/commands.js +2 -80
- package/dist/agent/desktop_notify.d.ts +1 -54
- package/dist/agent/desktop_notify.js +2 -229
- package/dist/agent/firefox_bookmarks.d.ts +1 -29
- package/dist/agent/firefox_bookmarks.js +2 -147
- package/dist/agent/git.d.ts +1 -37
- package/dist/agent/git.js +2 -121
- package/dist/agent/github.d.ts +1 -81
- package/dist/agent/github.js +2 -185
- package/dist/agent/inhibit.d.ts +1 -32
- package/dist/agent/inhibit.js +2 -141
- package/dist/agent/keys.d.ts +1 -26
- package/dist/agent/keys.js +2 -161
- package/dist/agent/local_socket.d.ts +1 -18
- package/dist/agent/local_socket.js +2 -106
- package/dist/agent/long_runs.d.ts +1 -45
- package/dist/agent/long_runs.js +2 -419
- package/dist/agent/programs.d.ts +1 -60
- package/dist/agent/programs.js +2 -237
- package/dist/agent/protocol.d.ts +1 -172
- package/dist/agent/protocol.js +2 -249
- package/dist/agent/scheduled_notices.d.ts +1 -21
- package/dist/agent/scheduled_notices.js +2 -38
- package/dist/agent/session.d.ts +1 -61
- package/dist/agent/session.js +2 -335
- package/dist/agent/ssh_config.d.ts +1 -12
- package/dist/agent/ssh_config.js +2 -72
- package/dist/agent/stalls.d.ts +1 -44
- package/dist/agent/stalls.js +2 -176
- package/dist/agent/tasks.d.ts +1 -17
- package/dist/agent/tasks.js +2 -15
- package/dist/agent/wake.d.ts +1 -14
- package/dist/agent/wake.js +2 -88
- package/dist/agent/worktree_files.d.ts +1 -20
- package/dist/agent/worktree_files.js +2 -69
- package/dist/app/define.d.ts +10 -1
- package/dist/app/define.js +1 -0
- package/dist/app/desktop.js +29 -9
- package/dist/app/outside_fetch.d.ts +40 -0
- package/dist/app/outside_fetch.js +94 -0
- package/dist/app/outside_route.d.ts +3 -0
- package/dist/app/outside_route.js +51 -0
- package/dist/app/preload.js +2 -0
- package/dist/app/protocol.d.ts +1 -0
- package/dist/app/server.js +3 -0
- package/dist/app/web/app_page.js +33 -4
- package/dist/app/web/hosts.d.ts +2 -2
- package/dist/app/web/hosts.js +12 -1
- package/dist/app/web/settings_page.d.ts +1 -0
- package/dist/config/capacitor.js +1 -1
- package/dist/daemon_link.d.ts +2 -1
- package/dist/daemon_link.js +4 -2
- package/dist/durations.d.ts +1 -15
- package/dist/durations.js +2 -52
- package/dist/site/build.d.ts +1 -1
- package/dist/site/build.js +28 -8
- package/dist/site/content.js +6 -8
- package/dist/site/links.d.ts +11 -0
- package/dist/site/links.js +55 -0
- package/dist/site/markdown.d.ts +5 -2
- package/dist/site/markdown.js +14 -11
- package/dist/site/model.d.ts +2 -1
- package/dist/site/model.js +12 -3
- package/dist/web/react/phone.js +2 -1
- package/package.json +5 -3
package/dist/agent/github.js
CHANGED
|
@@ -1,185 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// branch's pull request and its checks, and a repository's webhook. Every call runs gh with its arguments and no shell,
|
|
4
|
-
// and anything secret, such as a body or a hook's secret, goes on gh's standard input, never its command line.
|
|
5
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
6
|
-
import { not_installed_exit_code, run_program } from "./programs.js";
|
|
7
|
-
export const check_buckets = ["pass", "fail", "pending", "skipping", "cancel"];
|
|
8
|
-
// ── Titles ───────────────────────────────────────────────────────────────────
|
|
9
|
-
function words_of(title) {
|
|
10
|
-
return new Set(title.toLowerCase().split(/[^a-z0-9]+/).filter((word) => word.length > 2));
|
|
11
|
-
}
|
|
12
|
-
// How alike two titles are, from 0 to 1, by the words of three letters or more they share.
|
|
13
|
-
export function title_likeness(first, second) {
|
|
14
|
-
const first_words = words_of(first);
|
|
15
|
-
const second_words = words_of(second);
|
|
16
|
-
if (!first_words.size || !second_words.size)
|
|
17
|
-
return 0;
|
|
18
|
-
const shared = [...first_words].filter((word) => second_words.has(word)).length;
|
|
19
|
-
return shared / new Set([...first_words, ...second_words]).size;
|
|
20
|
-
}
|
|
21
|
-
// How alike an open issue's title must be to be taken as the same work.
|
|
22
|
-
export const same_issue_likeness = 0.6;
|
|
23
|
-
// The issue whose title is closest to the one given, if close enough to be the same work.
|
|
24
|
-
export function best_title_match(title, issues, least = same_issue_likeness) {
|
|
25
|
-
let best = null;
|
|
26
|
-
for (const issue of issues) {
|
|
27
|
-
const likeness = title_likeness(title, issue.title);
|
|
28
|
-
if (likeness >= least && (!best || likeness > best.likeness))
|
|
29
|
-
best = { number: issue.number, likeness };
|
|
30
|
-
}
|
|
31
|
-
return best?.number ?? null;
|
|
32
|
-
}
|
|
33
|
-
// ── Client ───────────────────────────────────────────────────────────────────
|
|
34
|
-
const issues_searched = 20;
|
|
35
|
-
const labels_listed = 200;
|
|
36
|
-
const device_page = "https://github.com/login/device";
|
|
37
|
-
// gh pr checks ends with 8 while checks run and 1 when one failed, still printing them.
|
|
38
|
-
const checks_exit_codes = [1, 8];
|
|
39
|
-
function as_found(read) {
|
|
40
|
-
const state = read.state === "MERGED" ? "merged" : read.state === "CLOSED" ? "closed" : "open";
|
|
41
|
-
return { number: read.number, url: read.url, state, draft: read.isDraft === true };
|
|
42
|
-
}
|
|
43
|
-
const pull_request_fields = "number,url,state,isDraft";
|
|
44
|
-
export function github_cli({ program = "gh", host = "github.com", code_wait_in_milliseconds = 20_000 } = {}) {
|
|
45
|
-
const missing = `${program} is not installed or not on the PATH`;
|
|
46
|
-
// Exit codes besides 0 whose output is still the answer, such as gh pr checks while checks run.
|
|
47
|
-
async function gh(args, stdin = null, also_fine = []) {
|
|
48
|
-
const result = await run_program([program, ...args], { stdin, stdout: "keep", stderr: "keep" });
|
|
49
|
-
if (result.exit_code === not_installed_exit_code)
|
|
50
|
-
throw new Error(`${missing}; GitHub is reached through it`);
|
|
51
|
-
if (result.exit_code !== 0 && !(also_fine.includes(result.exit_code) && result.stdout.trim()))
|
|
52
|
-
throw new Error(`gh ${args[0]} ${args[1]} failed: ${result.stderr.trim().split("\n").at(-1) || `exit ${result.exit_code}`}`);
|
|
53
|
-
return result.stdout.trim();
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
account: async () => {
|
|
57
|
-
const result = await run_program([program, "auth", "status", "--hostname", host], { stdout: "keep", stderr: "keep" });
|
|
58
|
-
if (result.exit_code === not_installed_exit_code)
|
|
59
|
-
return { signed_in: false, account: "", problem: missing };
|
|
60
|
-
const account = /Logged in to \S+ (?:account|as) (\S+)/.exec(`${result.stdout}\n${result.stderr}`)?.[1] ?? "";
|
|
61
|
-
return result.exit_code === 0 && account ? { signed_in: true, account, problem: "" } : { signed_in: false, account: "", problem: "" };
|
|
62
|
-
},
|
|
63
|
-
start_sign_in: (on_done) => new Promise((resolve, reject) => {
|
|
64
|
-
let seen = "";
|
|
65
|
-
let answered = false;
|
|
66
|
-
const watch = {
|
|
67
|
-
write: (chunk) => {
|
|
68
|
-
seen += chunk.toString();
|
|
69
|
-
const code = /\b([A-Z0-9]{4}-[A-Z0-9]{4})\b/.exec(seen)?.[1];
|
|
70
|
-
if (code && !answered) {
|
|
71
|
-
answered = true;
|
|
72
|
-
clearTimeout(timer);
|
|
73
|
-
resolve({ code, url: /https:\/\/\S+\/login\/device/.exec(seen)?.[0] ?? device_page });
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
const last_words = () => seen.trim().split("\n").at(-1) ?? "";
|
|
78
|
-
const timer = setTimeout(() => {
|
|
79
|
-
if (answered)
|
|
80
|
-
return;
|
|
81
|
-
answered = true;
|
|
82
|
-
reject(new Error(`gh gave no sign-in code: ${last_words() || "no answer"}`));
|
|
83
|
-
}, code_wait_in_milliseconds);
|
|
84
|
-
// The Enter answers gh's prompt to open a browser; the page offers its own link instead.
|
|
85
|
-
void run_program([program, "auth", "login", "--hostname", host, "--web", "--git-protocol", "ssh", "--skip-ssh-key"], { stdin: "\n", stdout: "show", stderr: "show", out: watch, err: watch }).then((result) => {
|
|
86
|
-
if (!answered) {
|
|
87
|
-
answered = true;
|
|
88
|
-
clearTimeout(timer);
|
|
89
|
-
reject(new Error(result.exit_code === not_installed_exit_code ? missing : `gh could not start signing in: ${last_words() || `exit ${result.exit_code}`}`));
|
|
90
|
-
}
|
|
91
|
-
on_done(result.exit_code === 0);
|
|
92
|
-
});
|
|
93
|
-
}),
|
|
94
|
-
sign_out: async (account) => {
|
|
95
|
-
await gh(["auth", "logout", "--hostname", host, "--user", account]);
|
|
96
|
-
},
|
|
97
|
-
find_or_open_issue: async ({ repository, title, body }) => {
|
|
98
|
-
const listed = await gh(["issue", "list", "--repo", repository, "--state", "open", "--search", `${title} in:title`, "--json", "number,title", "--limit", String(issues_searched)]);
|
|
99
|
-
const found = best_title_match(title, JSON.parse(listed || "[]"));
|
|
100
|
-
if (found !== null)
|
|
101
|
-
return { number: found, opened: false };
|
|
102
|
-
const url = await gh(["issue", "create", "--repo", repository, "--title", title, "--body-file", "-"], body || title);
|
|
103
|
-
const number = Number(/\/issues\/(\d+)\s*$/.exec(url)?.[1]);
|
|
104
|
-
if (!Number.isInteger(number))
|
|
105
|
-
throw new Error(`gh opened an issue but gave no number: ${url}`);
|
|
106
|
-
return { number, opened: true };
|
|
107
|
-
},
|
|
108
|
-
view_issue: async (repository, number) => {
|
|
109
|
-
const read = JSON.parse(await gh(["issue", "view", String(number), "--repo", repository, "--json", "state,labels,body"]));
|
|
110
|
-
return { open: read.state === "OPEN", labels: read.labels.map((label) => label.name), body: read.body ?? "" };
|
|
111
|
-
},
|
|
112
|
-
ensure_labels: async (repository, names, style = {}) => {
|
|
113
|
-
const search = style.search ? ["--search", style.search] : [];
|
|
114
|
-
const listed = JSON.parse((await gh(["label", "list", "--repo", repository, ...search, "--json", "name", "--limit", String(labels_listed)])) || "[]");
|
|
115
|
-
const known = new Set(listed.map((label) => label.name.toLowerCase()));
|
|
116
|
-
const looks = [...(style.colour ? ["--color", style.colour] : []), ...(style.description ? ["--description", style.description] : [])];
|
|
117
|
-
for (const name of names.filter((each) => !known.has(each.toLowerCase()))) {
|
|
118
|
-
// Another program may have made it in the meantime, which is as good.
|
|
119
|
-
await gh(["label", "create", name, "--repo", repository, ...looks]).catch((error) => {
|
|
120
|
-
if (!String(error).includes("already exists"))
|
|
121
|
-
throw error;
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
edit_issue: async (repository, number, edit) => {
|
|
126
|
-
const args = ["issue", "edit", String(number), "--repo", repository];
|
|
127
|
-
if (edit.add_labels?.length)
|
|
128
|
-
args.push("--add-label", edit.add_labels.join(","));
|
|
129
|
-
if (edit.remove_labels?.length)
|
|
130
|
-
args.push("--remove-label", edit.remove_labels.join(","));
|
|
131
|
-
if (edit.body !== undefined)
|
|
132
|
-
args.push("--body-file", "-");
|
|
133
|
-
await gh(args, edit.body ?? null);
|
|
134
|
-
},
|
|
135
|
-
post_comment: async (repository, number, body) => {
|
|
136
|
-
const url = await gh(["issue", "comment", String(number), "--repo", repository, "--body-file", "-"], body);
|
|
137
|
-
const id = Number(/#issuecomment-(\d+)\s*$/.exec(url)?.[1]);
|
|
138
|
-
if (!Number.isInteger(id))
|
|
139
|
-
throw new Error(`gh posted a comment but gave no id: ${url}`);
|
|
140
|
-
return id;
|
|
141
|
-
},
|
|
142
|
-
update_comment: async (repository, id, body) => {
|
|
143
|
-
try {
|
|
144
|
-
await gh(["api", "--method", "PATCH", `repos/${repository}/issues/comments/${id}`, "--input", "-"], JSON.stringify({ body }));
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
if (/Not Found|404/.test(String(error)))
|
|
149
|
-
return false;
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
close_issue: async (repository, number) => {
|
|
154
|
-
await gh(["issue", "close", String(number), "--repo", repository]);
|
|
155
|
-
},
|
|
156
|
-
find_pull_request: async (repository, branch) => {
|
|
157
|
-
const listed = JSON.parse((await gh(["pr", "list", "--repo", repository, "--head", branch, "--state", "all", "--json", pull_request_fields, "--limit", "1"])) || "[]");
|
|
158
|
-
return listed[0] ? as_found(listed[0]) : null;
|
|
159
|
-
},
|
|
160
|
-
view_pull_request: async (repository, number) => as_found(JSON.parse(await gh(["pr", "view", String(number), "--repo", repository, "--json", pull_request_fields]))),
|
|
161
|
-
read_checks: async (repository, number) => {
|
|
162
|
-
let text;
|
|
163
|
-
try {
|
|
164
|
-
text = await gh(["pr", "checks", String(number), "--repo", repository, "--json", "name,bucket,link,workflow"], null, checks_exit_codes);
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
if (String(error).includes("no checks reported"))
|
|
168
|
-
return [];
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
const read = JSON.parse(text || "[]");
|
|
172
|
-
return read.map((check) => ({ name: check.name, workflow: check.workflow ?? "", bucket: check_buckets.includes(check.bucket) ? check.bucket : "pending", link: check.link ?? "" }));
|
|
173
|
-
},
|
|
174
|
-
register_hook: async (repository, hook) => {
|
|
175
|
-
const listed = JSON.parse((await gh(["api", "--method", "GET", `repos/${repository}/hooks`])) || "[]");
|
|
176
|
-
const existing = listed.find((each) => each.config?.url === hook.url);
|
|
177
|
-
const body = JSON.stringify({ ...(existing ? {} : { name: "web" }), active: true, events: hook.events, config: { url: hook.url, content_type: "json", secret: hook.secret, insecure_ssl: "0" } });
|
|
178
|
-
if (existing)
|
|
179
|
-
await gh(["api", "--method", "PATCH", `repos/${repository}/hooks/${existing.id}`, "--input", "-"], body);
|
|
180
|
-
else
|
|
181
|
-
await gh(["api", "--method", "POST", `repos/${repository}/hooks`, "--input", "-"], body);
|
|
182
|
-
return existing ? "updated" : "added";
|
|
183
|
-
},
|
|
184
|
-
};
|
|
185
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/github";
|
package/dist/agent/inhibit.d.ts
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare function which(name: string): string | null;
|
|
3
|
-
export interface IdleInhibitorOptions {
|
|
4
|
-
who: string;
|
|
5
|
-
why: string;
|
|
6
|
-
linger_seconds?: number;
|
|
7
|
-
find?: (name: string) => string | null;
|
|
8
|
-
on_warning?: (message: string) => void;
|
|
9
|
-
}
|
|
10
|
-
export declare class IdleInhibitor {
|
|
11
|
-
private readonly linger;
|
|
12
|
-
private readonly find;
|
|
13
|
-
private readonly who;
|
|
14
|
-
private readonly why;
|
|
15
|
-
private readonly on_warning;
|
|
16
|
-
private holders_count;
|
|
17
|
-
private process;
|
|
18
|
-
private release_timer;
|
|
19
|
-
private warned;
|
|
20
|
-
private switching;
|
|
21
|
-
constructor(options: IdleInhibitorOptions);
|
|
22
|
-
get holders(): number;
|
|
23
|
-
get is_locked(): boolean;
|
|
24
|
-
private serially;
|
|
25
|
-
acquire(): Promise<void>;
|
|
26
|
-
release(): void;
|
|
27
|
-
held<Value>(work: () => Promise<Value>): Promise<Value>;
|
|
28
|
-
close(): Promise<void>;
|
|
29
|
-
private lock;
|
|
30
|
-
private unlock;
|
|
31
|
-
private warn_once;
|
|
32
|
-
}
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/inhibit";
|
package/dist/agent/inhibit.js
CHANGED
|
@@ -1,141 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// as anything wants the host awake. It blocks the automatic idle suspend only, so a suspend the user asks for still
|
|
4
|
-
// goes ahead.
|
|
5
|
-
//
|
|
6
|
-
// The lock is a systemd-inhibit child reading the agent's pipe. When the agent dies, however it dies, the pipe closes
|
|
7
|
-
// and the lock goes with it, so a restart can never leave the host held awake.
|
|
8
|
-
import { spawn } from "node:child_process";
|
|
9
|
-
import { accessSync, constants } from "node:fs";
|
|
10
|
-
import { delimiter, join } from "node:path";
|
|
11
|
-
// ── Settings ─────────────────────────────────────────────────────────────────
|
|
12
|
-
// How long the lock outlives its last holder, so a burst of short commands shares one lock.
|
|
13
|
-
export const LINGER_SECONDS = 5.0;
|
|
14
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
15
|
-
// The program's full path on PATH, or null, as shutil.which finds it.
|
|
16
|
-
export function which(name) {
|
|
17
|
-
for (const folder of (process.env.PATH ?? "").split(delimiter)) {
|
|
18
|
-
if (!folder)
|
|
19
|
-
continue;
|
|
20
|
-
const candidate = join(folder, name);
|
|
21
|
-
try {
|
|
22
|
-
accessSync(candidate, constants.X_OK);
|
|
23
|
-
return candidate;
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
// ── Inhibitor ────────────────────────────────────────────────────────────────
|
|
32
|
-
// A counted idle inhibitor: taken on the first hold, released a moment after the last.
|
|
33
|
-
export class IdleInhibitor {
|
|
34
|
-
linger;
|
|
35
|
-
find;
|
|
36
|
-
who;
|
|
37
|
-
why;
|
|
38
|
-
on_warning;
|
|
39
|
-
holders_count = 0;
|
|
40
|
-
process = null;
|
|
41
|
-
release_timer = null;
|
|
42
|
-
warned = false;
|
|
43
|
-
// Taking or dropping the lock starts or stops a process, so concurrent holds wait here rather than each starting one.
|
|
44
|
-
switching = Promise.resolve();
|
|
45
|
-
constructor(options) {
|
|
46
|
-
this.linger = options.linger_seconds ?? LINGER_SECONDS;
|
|
47
|
-
this.find = options.find ?? which;
|
|
48
|
-
this.who = options.who;
|
|
49
|
-
this.why = options.why;
|
|
50
|
-
this.on_warning = options.on_warning ?? (() => { });
|
|
51
|
-
}
|
|
52
|
-
get holders() {
|
|
53
|
-
return this.holders_count;
|
|
54
|
-
}
|
|
55
|
-
get is_locked() {
|
|
56
|
-
return this.process !== null && this.process.exitCode === null && this.process.signalCode === null;
|
|
57
|
-
}
|
|
58
|
-
serially(work) {
|
|
59
|
-
this.switching = this.switching.then(work, work);
|
|
60
|
-
return this.switching;
|
|
61
|
-
}
|
|
62
|
-
// Adds a holder, taking the lock if this is the first one.
|
|
63
|
-
async acquire() {
|
|
64
|
-
this.holders_count += 1;
|
|
65
|
-
if (this.release_timer !== null) {
|
|
66
|
-
clearTimeout(this.release_timer);
|
|
67
|
-
this.release_timer = null;
|
|
68
|
-
}
|
|
69
|
-
await this.serially(async () => {
|
|
70
|
-
if (!this.is_locked && this.holders_count > 0)
|
|
71
|
-
this.lock();
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
// Drops a holder; the lock goes once none is left for the linger period.
|
|
75
|
-
release() {
|
|
76
|
-
if (this.holders_count === 0)
|
|
77
|
-
return;
|
|
78
|
-
this.holders_count -= 1;
|
|
79
|
-
if (this.holders_count === 0 && this.release_timer === null) {
|
|
80
|
-
this.release_timer = setTimeout(() => {
|
|
81
|
-
this.release_timer = null;
|
|
82
|
-
void this.serially(async () => {
|
|
83
|
-
if (this.holders_count === 0)
|
|
84
|
-
await this.unlock();
|
|
85
|
-
});
|
|
86
|
-
}, this.linger * 1000);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
// Holds the host awake while the work runs.
|
|
90
|
-
async held(work) {
|
|
91
|
-
await this.acquire();
|
|
92
|
-
try {
|
|
93
|
-
return await work();
|
|
94
|
-
}
|
|
95
|
-
finally {
|
|
96
|
-
this.release();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
// Releases the lock now, whatever still holds it.
|
|
100
|
-
async close() {
|
|
101
|
-
if (this.release_timer !== null) {
|
|
102
|
-
clearTimeout(this.release_timer);
|
|
103
|
-
this.release_timer = null;
|
|
104
|
-
}
|
|
105
|
-
this.holders_count = 0;
|
|
106
|
-
await this.serially(() => this.unlock());
|
|
107
|
-
}
|
|
108
|
-
lock() {
|
|
109
|
-
const binary = this.find("systemd-inhibit");
|
|
110
|
-
if (binary === null) {
|
|
111
|
-
this.warn_once("systemd-inhibit is not on PATH, so tasks cannot keep this host awake");
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
const child = spawn(binary, ["--what=idle", "--mode=block", `--who=${this.who}`, `--why=${this.why}`, "cat"], { stdio: ["pipe", "ignore", "ignore"] });
|
|
115
|
-
child.on("error", (error) => {
|
|
116
|
-
if (this.process === child)
|
|
117
|
-
this.process = null;
|
|
118
|
-
this.warn_once(`could not start systemd-inhibit: ${error.message}`);
|
|
119
|
-
});
|
|
120
|
-
child.stdin.on("error", () => undefined);
|
|
121
|
-
this.process = child;
|
|
122
|
-
}
|
|
123
|
-
async unlock() {
|
|
124
|
-
const child = this.process;
|
|
125
|
-
this.process = null;
|
|
126
|
-
if (child === null || child.exitCode !== null || child.signalCode !== null)
|
|
127
|
-
return;
|
|
128
|
-
const exited = new Promise((resolve) => child.once("exit", () => resolve()));
|
|
129
|
-
// Closing the pipe ends the child's `cat`, which is what releases the lock.
|
|
130
|
-
child.stdin?.end();
|
|
131
|
-
const timer = setTimeout(() => child.kill("SIGKILL"), 5000);
|
|
132
|
-
await exited;
|
|
133
|
-
clearTimeout(timer);
|
|
134
|
-
}
|
|
135
|
-
warn_once(message) {
|
|
136
|
-
if (this.warned)
|
|
137
|
-
return;
|
|
138
|
-
this.on_warning(message);
|
|
139
|
-
this.warned = true;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/inhibit";
|
package/dist/agent/keys.d.ts
CHANGED
|
@@ -1,26 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare class AgentKeyError extends Error {
|
|
3
|
-
constructor(message: string);
|
|
4
|
-
}
|
|
5
|
-
export declare function challenge_text(app: string, host: string, nonce: string): Buffer;
|
|
6
|
-
export declare function sign_challenge(private_key: KeyObject, app: string, host: string, nonce: string): string;
|
|
7
|
-
export declare function verify_challenge(public_key: KeyObject, app: string, host: string, nonce: string, signature: string): boolean;
|
|
8
|
-
export declare function signed_bytes(parts: readonly string[]): Buffer;
|
|
9
|
-
export declare function server_challenge_text(app: string, host: string, server_nonce: string, server_key_fingerprint: string): Buffer;
|
|
10
|
-
export declare function agent_hello_text(app: string, host: string, server_nonce: string, agent_nonce: string, server_key_fingerprint: string): Buffer;
|
|
11
|
-
export declare function server_message_text(app: string, host: string, server_nonce: string, agent_nonce: string, counter: number, message_text: string): Buffer;
|
|
12
|
-
export declare function sign_text(private_key: KeyObject, text: Buffer): string;
|
|
13
|
-
export declare function verify_text(public_key: KeyObject, text: Buffer, signature: unknown): boolean;
|
|
14
|
-
export declare function new_nonce(): string;
|
|
15
|
-
export declare function is_nonce(value: unknown): value is string;
|
|
16
|
-
export declare function parse_server_keys(lines: readonly string[]): Map<string, KeyObject>;
|
|
17
|
-
export declare function public_key_line(key: KeyObject, comment?: string): string;
|
|
18
|
-
export declare function public_line_of_private_text(text: string, comment?: string): string;
|
|
19
|
-
export declare function fingerprint_of(key: KeyObject): string;
|
|
20
|
-
export declare function parse_public_key(line: string): KeyObject;
|
|
21
|
-
export declare function key_fingerprint(line: string): string;
|
|
22
|
-
export declare function parse_private_key(text: string): KeyObject;
|
|
23
|
-
export declare function generate_agent_key(comment: string): {
|
|
24
|
-
private_text: string;
|
|
25
|
-
public_line: string;
|
|
26
|
-
};
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/keys";
|
package/dist/agent/keys.js
CHANGED
|
@@ -1,161 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// server may hold an ed25519 key of its own in the same format, whose public line each agent lists to check the server.
|
|
4
|
-
// sshpk reads and writes the OpenSSH formats, which Node's crypto does not, and Node's crypto signs.
|
|
5
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
6
|
-
import { createHash, createPrivateKey, createPublicKey, randomBytes, sign, verify } from "node:crypto";
|
|
7
|
-
import sshpk from "sshpk";
|
|
8
|
-
// ── Settings ─────────────────────────────────────────────────────────────────
|
|
9
|
-
export class AgentKeyError extends Error {
|
|
10
|
-
constructor(message) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.name = "AgentKeyError";
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
// ── Challenge ────────────────────────────────────────────────────────────────
|
|
16
|
-
// What the agent signs: the app, the protocol, the host it logs in as and the server's nonce, so a signature fits one
|
|
17
|
-
// login to one app only; `app` is the app's name, such as "x-nix".
|
|
18
|
-
export function challenge_text(app, host, nonce) {
|
|
19
|
-
return Buffer.from(`${app}-agent-v1|${host}|${nonce}`, "utf8");
|
|
20
|
-
}
|
|
21
|
-
export function sign_challenge(private_key, app, host, nonce) {
|
|
22
|
-
return sign(null, challenge_text(app, host, nonce), private_key).toString("base64");
|
|
23
|
-
}
|
|
24
|
-
export function verify_challenge(public_key, app, host, nonce, signature) {
|
|
25
|
-
try {
|
|
26
|
-
return verify(null, challenge_text(app, host, nonce), public_key, Buffer.from(signature, "base64"));
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
// ── Server signatures ────────────────────────────────────────────────────────
|
|
33
|
-
// What a signature covers: each part as a netstring, its UTF-8 length in bytes in decimal, a colon, its bytes and a comma, so no two lists of parts give the same bytes.
|
|
34
|
-
export function signed_bytes(parts) {
|
|
35
|
-
return Buffer.concat(parts.map((part) => {
|
|
36
|
-
const bytes = Buffer.from(part, "utf8");
|
|
37
|
-
return Buffer.concat([Buffer.from(`${bytes.length}:`, "utf8"), bytes, Buffer.from(",", "utf8")]);
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
// What the server signs in its challenge: the app, the host logging in, the server's nonce and its key's fingerprint.
|
|
41
|
-
export function server_challenge_text(app, host, server_nonce, server_key_fingerprint) {
|
|
42
|
-
return signed_bytes(["agent-server-challenge-v1", app, host, server_nonce, server_key_fingerprint]);
|
|
43
|
-
}
|
|
44
|
-
// What an agent that checks the server signs in its hello: the app, its host, both nonces and the server key's fingerprint, so a relayed hello fits no other connection or server.
|
|
45
|
-
export function agent_hello_text(app, host, server_nonce, agent_nonce, server_key_fingerprint) {
|
|
46
|
-
return signed_bytes(["agent-hello-v2", app, host, server_nonce, agent_nonce, server_key_fingerprint]);
|
|
47
|
-
}
|
|
48
|
-
// What the server signs for each message after the hello: the app, the host, both nonces of this connection, the message's counter and the exact text of the message.
|
|
49
|
-
export function server_message_text(app, host, server_nonce, agent_nonce, counter, message_text) {
|
|
50
|
-
return signed_bytes(["agent-server-message-v1", app, host, server_nonce, agent_nonce, String(counter), message_text]);
|
|
51
|
-
}
|
|
52
|
-
export function sign_text(private_key, text) {
|
|
53
|
-
return sign(null, text, private_key).toString("base64");
|
|
54
|
-
}
|
|
55
|
-
export function verify_text(public_key, text, signature) {
|
|
56
|
-
if (typeof signature !== "string")
|
|
57
|
-
return false;
|
|
58
|
-
try {
|
|
59
|
-
return verify(null, text, public_key, Buffer.from(signature, "base64"));
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// A new random nonce of 32 bytes as base64url text.
|
|
66
|
-
export function new_nonce() {
|
|
67
|
-
return randomBytes(32).toString("base64url");
|
|
68
|
-
}
|
|
69
|
-
// Whether a text has the shape of a nonce one side made, so the other side keeps no unbounded text.
|
|
70
|
-
export function is_nonce(value) {
|
|
71
|
-
return typeof value === "string" && /^[A-Za-z0-9_-]{16,128}$/.test(value);
|
|
72
|
-
}
|
|
73
|
-
// The accepted server keys from their OpenSSH public lines, by fingerprint; a line that is not an ed25519 key throws an AgentKeyError.
|
|
74
|
-
export function parse_server_keys(lines) {
|
|
75
|
-
const keys = new Map();
|
|
76
|
-
for (const line of lines) {
|
|
77
|
-
const key = parse_public_key(line);
|
|
78
|
-
keys.set(fingerprint_of(key), key);
|
|
79
|
-
}
|
|
80
|
-
return keys;
|
|
81
|
-
}
|
|
82
|
-
// ── Public key ───────────────────────────────────────────────────────────────
|
|
83
|
-
// The 32 bytes of an ed25519 key's public half, from the public key or its private key.
|
|
84
|
-
function raw_public_key(key) {
|
|
85
|
-
const public_key = key.type === "private" ? createPublicKey(key) : key;
|
|
86
|
-
if (public_key.asymmetricKeyType !== "ed25519")
|
|
87
|
-
throw new AgentKeyError(`must be an ed25519 key, not ${public_key.asymmetricKeyType ?? public_key.type}.`);
|
|
88
|
-
return Buffer.from(public_key.export({ format: "jwk" }).x, "base64url");
|
|
89
|
-
}
|
|
90
|
-
// The key in the SSH wire format an OpenSSH public line carries as base64.
|
|
91
|
-
function ssh_public_blob(key) {
|
|
92
|
-
const field = (bytes) => {
|
|
93
|
-
const length = Buffer.alloc(4);
|
|
94
|
-
length.writeUInt32BE(bytes.length);
|
|
95
|
-
return Buffer.concat([length, bytes]);
|
|
96
|
-
};
|
|
97
|
-
return Buffer.concat([field(Buffer.from("ssh-ed25519", "utf8")), field(raw_public_key(key))]);
|
|
98
|
-
}
|
|
99
|
-
// The OpenSSH public line of an ed25519 key, from the public key or its private key, as ssh-keygen -y prints it.
|
|
100
|
-
export function public_key_line(key, comment = "") {
|
|
101
|
-
const line = `ssh-ed25519 ${ssh_public_blob(key).toString("base64")}`;
|
|
102
|
-
return comment === "" ? line : `${line} ${comment}`;
|
|
103
|
-
}
|
|
104
|
-
// The OpenSSH public line of an unencrypted OpenSSH ed25519 private key's text, for an operator to put in each agent's config.
|
|
105
|
-
export function public_line_of_private_text(text, comment = "") {
|
|
106
|
-
return public_key_line(parse_private_key(text), comment);
|
|
107
|
-
}
|
|
108
|
-
// The SHA256 fingerprint of an ed25519 key, from the public key or its private key, as ssh-keygen -l prints it.
|
|
109
|
-
export function fingerprint_of(key) {
|
|
110
|
-
return `SHA256:${createHash("sha256").update(ssh_public_blob(key)).digest("base64").replace(/=+$/, "")}`;
|
|
111
|
-
}
|
|
112
|
-
// Reads an OpenSSH public key line; the messages never quote the key itself.
|
|
113
|
-
export function parse_public_key(line) {
|
|
114
|
-
const parts = line.trim().split(/\s+/);
|
|
115
|
-
if (parts.length < 2 || parts[0] === "")
|
|
116
|
-
throw new AgentKeyError('must be an OpenSSH public key line such as "ssh-ed25519 AAAA... comment".');
|
|
117
|
-
if (parts[0] !== "ssh-ed25519")
|
|
118
|
-
throw new AgentKeyError(`must be an ssh-ed25519 key, not ${parts[0]}; make one with ssh-keygen -t ed25519.`);
|
|
119
|
-
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(parts[1]))
|
|
120
|
-
throw new AgentKeyError("holds a key that is not base64.");
|
|
121
|
-
let key;
|
|
122
|
-
try {
|
|
123
|
-
key = sshpk.parseKey(line.trim(), "ssh");
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
throw new AgentKeyError("is cut short or damaged.");
|
|
127
|
-
}
|
|
128
|
-
if (key.type !== "ed25519")
|
|
129
|
-
throw new AgentKeyError(`says ssh-ed25519 but holds a ${key.type} key.`);
|
|
130
|
-
return createPublicKey(key.toString("pkcs8"));
|
|
131
|
-
}
|
|
132
|
-
// The key's SHA256 fingerprint as ssh-keygen -l prints it.
|
|
133
|
-
export function key_fingerprint(line) {
|
|
134
|
-
return sshpk.parseKey(line.trim(), "ssh").fingerprint("sha256").toString();
|
|
135
|
-
}
|
|
136
|
-
// ── Private key ──────────────────────────────────────────────────────────────
|
|
137
|
-
// Reads an unencrypted OpenSSH ed25519 private key; the messages never quote the key itself.
|
|
138
|
-
export function parse_private_key(text) {
|
|
139
|
-
if (!/-{5}BEGIN OPENSSH PRIVATE KEY-{5}/.test(text))
|
|
140
|
-
throw new AgentKeyError('is not an OpenSSH private key; make one with ssh-keygen -t ed25519 -N "".');
|
|
141
|
-
let key;
|
|
142
|
-
try {
|
|
143
|
-
key = sshpk.parsePrivateKey(text, "ssh");
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
if (error.name === "KeyEncryptedError") {
|
|
147
|
-
throw new AgentKeyError('is encrypted with a passphrase, which the agent cannot type; make it with -N "" and keep it in a secret store instead.');
|
|
148
|
-
}
|
|
149
|
-
throw new AgentKeyError("is cut short or damaged.");
|
|
150
|
-
}
|
|
151
|
-
if (key.type !== "ed25519")
|
|
152
|
-
throw new AgentKeyError(`must be an ssh-ed25519 key, not ${key.type}; make one with ssh-keygen -t ed25519.`);
|
|
153
|
-
return createPrivateKey(key.toString("pkcs8"));
|
|
154
|
-
}
|
|
155
|
-
// ── New keys ─────────────────────────────────────────────────────────────────
|
|
156
|
-
// A new ed25519 key in the files ssh-keygen would write: the unencrypted OpenSSH private key and its public line.
|
|
157
|
-
export function generate_agent_key(comment) {
|
|
158
|
-
const key = sshpk.generatePrivateKey("ed25519");
|
|
159
|
-
key.comment = comment;
|
|
160
|
-
return { private_text: key.toString("ssh"), public_line: key.toPublic().toString("ssh") };
|
|
161
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/keys";
|
|
@@ -1,18 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const RETRY_SECONDS = 5;
|
|
3
|
-
export declare class LineServer {
|
|
4
|
-
readonly path: string;
|
|
5
|
-
readonly sockets: Set<Socket>;
|
|
6
|
-
private readonly mode;
|
|
7
|
-
private server;
|
|
8
|
-
constructor(path: string, mode?: number);
|
|
9
|
-
start(): Promise<void>;
|
|
10
|
-
send(message: unknown): Promise<void>;
|
|
11
|
-
close(): void;
|
|
12
|
-
}
|
|
13
|
-
export interface FollowOptions {
|
|
14
|
-
accept?: (message: Record<string, unknown>) => boolean;
|
|
15
|
-
retry_seconds?: number;
|
|
16
|
-
on_disconnect?: (reason: string) => void;
|
|
17
|
-
}
|
|
18
|
-
export declare function follow_lines(path: string, handle: (message: Record<string, unknown>) => unknown, signal: AbortSignal, options?: FollowOptions): Promise<void>;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/local_socket";
|