@hallohuman/hightea 0.1.17 → 0.1.31

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.
@@ -1,91 +0,0 @@
1
- import {
2
- TRUSTED_KEY_NAME,
3
- trustedKeyId,
4
- readTrustedKeys,
5
- localHost2
6
- } from "./chunk-h5s3wwdz.js";
7
-
8
- // ../ahp-host/src/trust.ts
9
- import { writeJsonAtomic } from "@ahpd/sdk";
10
- var TRUST_MEANS = "A trusted browser key lets posts made in that browser ask this machine's agents; a trusted host key " + "lets that machine's agents ask them too.";
11
- async function hostTrust(input, now = () => new Date) {
12
- const found = await localHost2(input.host);
13
- if ("error" in found) {
14
- console.error(found.error.replace(/^SERVICE_/u, "TRUST_"));
15
- return 1;
16
- }
17
- let current;
18
- try {
19
- current = readTrustedKeys(found.configDir);
20
- } catch (error) {
21
- console.error(`${error.message}; it was left unchanged`);
22
- return 1;
23
- }
24
- const keys = current.state === "read" ? current.keys : [];
25
- const where = `host ${found.hostId} (${current.file})`;
26
- if (input.action === "list") {
27
- if (input.json === true) {
28
- console.log(JSON.stringify({ hostId: found.hostId, file: current.file, keys }));
29
- return 0;
30
- }
31
- if (keys.length === 0) {
32
- console.log(`No trusted keys for ${where}: room asks to this machine's agents are refused until you add one with` + " `hightea host trust add <name> <public key>`.");
33
- return 0;
34
- }
35
- console.log(`Trusted keys for ${where}:`);
36
- for (const key of keys)
37
- console.log(` ${key.name} key ${key.keyId} added ${key.addedAt}`);
38
- console.log(TRUST_MEANS);
39
- return 0;
40
- }
41
- if (!TRUSTED_KEY_NAME.test(input.name)) {
42
- console.error("TRUST_NAME_INVALID: a name is 1 to 64 letters, digits, '.', '_' or '-', starting with a letter or digit; nothing was changed");
43
- return 1;
44
- }
45
- const named = keys.find((key) => key.name === input.name);
46
- if (input.action === "remove") {
47
- if (named === undefined) {
48
- console.error(`TRUST_NAME_UNKNOWN: ${where} trusts no key named ${input.name} (trusted: ${keys.map((key) => key.name).join(", ") || "none"}); nothing was changed`);
49
- return 1;
50
- }
51
- if (!write(current.file, keys.filter((key) => key !== named)))
52
- return 1;
53
- console.log(`Removed ${input.name} (key ${named.keyId}) from ${where}. Asks signed with it are refused from now on.`);
54
- return 0;
55
- }
56
- const id = trustedKeyId(input.publicKey);
57
- if ("error" in id) {
58
- console.error(`${id.error}; nothing was changed`);
59
- return 1;
60
- }
61
- if (named !== undefined) {
62
- if (named.publicKey === input.publicKey) {
63
- console.log(`${input.name} (key ${id.keyId}) is already trusted by ${where}; it was left unchanged.`);
64
- return 0;
65
- }
66
- console.error(`TRUST_NAME_TAKEN: ${where} already trusts another key as ${input.name}; remove it first or pick another name. Nothing was changed`);
67
- return 1;
68
- }
69
- const holder = keys.find((key) => key.keyId === id.keyId);
70
- if (holder !== undefined) {
71
- console.error(`TRUST_KEY_TAKEN: ${where} already trusts this key as ${holder.name}; nothing was changed`);
72
- return 1;
73
- }
74
- const added = { name: input.name, keyId: id.keyId, publicKey: input.publicKey, addedAt: now().toISOString() };
75
- if (!write(current.file, [...keys, added]))
76
- return 1;
77
- console.log(`Trusted ${input.name} (key ${id.keyId}) on ${where}. ${TRUST_MEANS}`);
78
- return 0;
79
- }
80
- function write(file, keys) {
81
- try {
82
- writeJsonAtomic(file, { v: 1, keys }, { mode: 384 });
83
- } catch (error) {
84
- console.error(`TRUST_FILE_UNWRITABLE: ${file}: ${error.message}`);
85
- return false;
86
- }
87
- return true;
88
- }
89
- export {
90
- hostTrust
91
- };
@@ -1,78 +0,0 @@
1
- import"./chunk-0ab6h39c.js";
2
- import {
3
- ownerIo,
4
- sendOwner
5
- } from "./chunk-sbqmqp8e.js";
6
-
7
- // src/host.ts
8
- var USAGE = `hightea host list --origin https://your-deployment --workspace SLUG [--session-file FILE] [--json]
9
- ` + "hightea host remove --origin https://your-deployment --workspace SLUG --host HOST [--session-file FILE] [--json]";
10
- async function hostRemove(target, io = ownerIo()) {
11
- if (target.workspace.trim().length === 0 || target.host.trim().length === 0) {
12
- io.err(`HOST_REMOVE_INPUT: --workspace and --host are required.
13
- ${USAGE}`);
14
- return 1;
15
- }
16
- const hostId = target.host.trim();
17
- const sent = await sendOwner(target, io, {
18
- method: "DELETE",
19
- path: `/hosts/${encodeURIComponent(hostId)}`,
20
- what: "remove",
21
- label: "HOST_REMOVE"
22
- });
23
- if ("error" in sent) {
24
- io.err(sent.error);
25
- return sent.exit;
26
- }
27
- if (target.json) {
28
- io.out(JSON.stringify(sent.answer));
29
- return 0;
30
- }
31
- const host = sent.answer.host ?? {};
32
- const ended = typeof host.endedDeliveries === "number" ? host.endedDeliveries : 0;
33
- const name = String(host.hostId ?? hostId);
34
- if (host.alreadyRevoked === true) {
35
- io.out(`host ${name} was already removed; nothing was left running`);
36
- return 0;
37
- }
38
- io.out(`removed host ${name}: revoked, ${ended} ${ended === 1 ? "delivery" : "deliveries"} ended`);
39
- io.out("Its agents have no host until they join again with a new join code.");
40
- return 0;
41
- }
42
- function hostLine(host) {
43
- const field = (name) => host[name] === null || host[name] === undefined ? "none" : String(host[name]);
44
- const agents = Array.isArray(host.agentMemberIds) && host.agentMemberIds.length > 0 ? host.agentMemberIds.join(",") : "none";
45
- const state = field("status") === "revoked" ? `revoked ${field("revokedAt")}` : `last seen ${field("lastSeenAt")}`;
46
- return `${field("hostId")} ${field("status")} ${host.connected === true ? "connected" : "not connected"} ` + `plugin ${field("pluginVersion")} agents ${agents} owner ${field("ownerAccountId")} ${state}`;
47
- }
48
- async function hostList(target, io = ownerIo()) {
49
- if (target.workspace.trim().length === 0) {
50
- io.err(`HOST_LIST_INPUT: --workspace is required.
51
- ${USAGE}`);
52
- return 1;
53
- }
54
- const sent = await sendOwner(target, io, { method: "GET", path: "/hosts", what: "list", label: "HOST_LIST" });
55
- if ("error" in sent) {
56
- io.err(sent.error);
57
- return sent.exit;
58
- }
59
- if (target.json) {
60
- io.out(JSON.stringify(sent.answer));
61
- return 0;
62
- }
63
- const hosts = Array.isArray(sent.answer.hosts) ? sent.answer.hosts : [];
64
- if (hosts.length === 0) {
65
- io.out(`no hosts in workspace ${target.workspace.trim()} that your account can see (a workspace owner sees every host)`);
66
- return 0;
67
- }
68
- io.out(hosts.map(hostLine).join(`
69
- `));
70
- if (hosts.some((host) => host.status === "active")) {
71
- io.out("A host you no longer use: `hightea host remove --workspace " + target.workspace.trim() + " --host <id>`.");
72
- }
73
- return 0;
74
- }
75
- export {
76
- hostList,
77
- hostRemove
78
- };
@@ -1,123 +0,0 @@
1
- import {
2
- checkedOrigin2,
3
- ensureCredential2
4
- } from "./chunk-0ab6h39c.js";
5
-
6
- // src/owner-session.ts
7
- import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
8
- import { homedir } from "node:os";
9
- import { join } from "node:path";
10
- var NOT_OWNER = new Set(["FORBIDDEN", "NOT_A_MEMBER", "UNAUTHENTICATED"]);
11
- function ownerIo() {
12
- return {
13
- fetch: (url, init) => globalThis.fetch(url, init),
14
- readFile: (path) => readFile(path, "utf8"),
15
- stat: (path) => stat(path),
16
- writeFile: (path, data, options) => writeFile(path, data, options),
17
- mkdir: async (path, options) => {
18
- await mkdir(path, options);
19
- },
20
- chmod: (path, mode) => chmod(path, mode),
21
- rm: (path, options) => rm(path, options),
22
- env: process.env,
23
- home: homedir(),
24
- nowMs: () => Date.now(),
25
- out: (line) => {
26
- process.stdout.write(`${line}
27
- `);
28
- },
29
- err: (line) => {
30
- process.stderr.write(`${line}
31
- `);
32
- }
33
- };
34
- }
35
- async function readSession(target, io, label, origin) {
36
- const explicit = target.sessionFile?.trim() || io.env.HIGHTEA_SESSION_FILE?.trim();
37
- if (explicit === undefined || explicit === "") {
38
- const stored = await ensureCredential2(origin, io);
39
- if ("credential" in stored)
40
- return { cookie: stored.credential.sessionToken };
41
- if (stored.error.startsWith("AUTH_NOT_SIGNED_IN")) {
42
- const legacy = join(io.env.XDG_CONFIG_HOME?.trim() || join(io.home, ".config"), "hightea", "session");
43
- const copied = await readSessionFile(legacy, io, label);
44
- if (!("error" in copied))
45
- return copied;
46
- return { error: `${label}_SESSION_MISSING: ${stored.error}` };
47
- }
48
- if (stored.error.startsWith("AUTH_CREDENTIALS_EXPOSED"))
49
- return { error: `${label}_SESSION_EXPOSED: ${stored.error}` };
50
- if (stored.error.startsWith("AUTH_SESSION_EXPIRED") || stored.error.startsWith("AUTH_REFRESH")) {
51
- return { error: `${label}_SESSION_EXPIRED: ${stored.error}` };
52
- }
53
- return { error: `${label}_SESSION_INVALID: ${stored.error}` };
54
- }
55
- return await readSessionFile(explicit, io, label);
56
- }
57
- async function readSessionFile(file, io, label) {
58
- let raw;
59
- let mode;
60
- try {
61
- raw = await io.readFile(file);
62
- mode = (await io.stat(file)).mode;
63
- } catch (error) {
64
- return { error: `${label}_SESSION_MISSING: no session in ${file} (${error.message}). Sign in to the deployment in a browser and write its ht_session cookie to that file, or pass --session-file.` };
65
- }
66
- if ((mode & 63) !== 0) {
67
- return { error: `${label}_SESSION_EXPOSED: ${file} is mode 0${(mode & 511).toString(8).padStart(3, "0")}; run chmod 600 ${file}.` };
68
- }
69
- const lines = raw.split(`
70
- `).map((line) => line.trim()).filter((line) => line.length > 0);
71
- if (lines.length !== 1) {
72
- return { error: `${label}_SESSION_INVALID: ${file} must hold exactly one non-empty line (the ht_session cookie value); it holds ${lines.length}.` };
73
- }
74
- return { cookie: lines[0] };
75
- }
76
- async function sendOwner(target, io, request) {
77
- const where = checkedOrigin2(target.origin, request.label);
78
- if ("error" in where)
79
- return { exit: 1, error: where.error };
80
- const session = await readSession(target, io, request.label, where.origin);
81
- if ("error" in session)
82
- return { exit: 1, error: session.error };
83
- const url = `${where.origin}/api/ws/${encodeURIComponent(target.workspace)}${request.path}`;
84
- let response;
85
- try {
86
- response = await io.fetch(url, {
87
- method: request.method,
88
- headers: {
89
- cookie: `ht_session=${session.cookie}`,
90
- origin: where.origin,
91
- accept: "application/json",
92
- "cache-control": "no-store",
93
- ...request.body === undefined ? {} : { "content-type": "application/json" }
94
- },
95
- ...request.body === undefined ? {} : { body: JSON.stringify(request.body) }
96
- });
97
- } catch (error) {
98
- return { exit: 1, error: `${request.label}_UNREACHABLE: ${request.what} ${request.method} ${url} did not answer: ${error.message}` };
99
- }
100
- const text = await response.text();
101
- let answer = {};
102
- if (text.length > 0) {
103
- try {
104
- const parsed = JSON.parse(text);
105
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
106
- throw new SyntaxError("not a JSON object");
107
- answer = parsed;
108
- } catch (error) {
109
- if (!(error instanceof SyntaxError))
110
- throw error;
111
- return { exit: 1, error: `${request.label}_ANSWER_INVALID: ${request.what} ${request.method} ${url} answered HTTP ${response.status} with non-JSON: ${text.slice(0, 300)}` };
112
- }
113
- }
114
- if (response.ok)
115
- return { answer };
116
- const refusal = answer.error;
117
- const code = typeof refusal?.code === "string" ? refusal.code : `HTTP_${response.status}`;
118
- const message = typeof refusal?.message === "string" ? refusal.message : text.slice(0, 300);
119
- const exit = NOT_OWNER.has(code) ? 3 : request.extraExits?.[code] ?? 1;
120
- return { exit, error: `${code}: ${message}`, body: answer };
121
- }
122
-
123
- export { ownerIo, sendOwner };