@hallohuman/hightea 0.1.2 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hallohuman/hightea",
3
- "version": "0.1.2",
3
+ "version": "0.1.8",
4
4
  "description": "The Hightea command line: join this machine to a Hightea workspace as a host.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -9,7 +9,9 @@
9
9
  "bin": {
10
10
  "hightea": "./dist/bin.js"
11
11
  },
12
- "files": ["dist"],
12
+ "files": [
13
+ "dist"
14
+ ],
13
15
  "engines": {
14
16
  "node": ">=26.13.1"
15
17
  },
@@ -18,12 +20,14 @@
18
20
  "prepack": "bun run build"
19
21
  },
20
22
  "dependencies": {
21
- "@ahpd/sdk": "https://github.com/beorn/ahpd/releases/download/hh-37326-9dff8dc1/ahpd-sdk-0.15.0-rc.4-beorn.9dff8dc1.tgz",
22
- "@ahpd/server": "https://github.com/beorn/ahpd/releases/download/hh-37326-9dff8dc1/ahpd-server-0.15.0-rc.4-beorn.9dff8dc1.tgz",
23
+ "@ahpd/sdk": "https://github.com/beorn/ahpd/releases/download/hh-38446-12d61c7b/ahpd-sdk-0.15.0-rc.4-beorn.12d61c7b.tgz",
24
+ "@ahpd/server": "https://github.com/beorn/ahpd/releases/download/hh-38446-12d61c7b/ahpd-server-0.15.0-rc.4-beorn.12d61c7b.tgz",
25
+ "@ahpd/services": "https://github.com/beorn/ahpd/releases/download/hh-38446-12d61c7b/ahpd-services-0.15.0-rc.4-beorn.12d61c7b.tgz",
23
26
  "@silvery/commander": "0.29.3",
24
27
  "ws": "8.21.0"
25
28
  },
26
29
  "devDependencies": {
27
- "@hallohuman/hightea-ahp-host": "0.0.3"
30
+ "@hallohuman/hightea-ahp-host": "0.0.3",
31
+ "@hallohuman/hightea-client": "0.0.1"
28
32
  }
29
33
  }
@@ -1,149 +0,0 @@
1
- // src/source.ts
2
- import { readFile, stat } from "node:fs/promises";
3
- import { homedir } from "node:os";
4
- import { join } from "node:path";
5
- var USAGE = `hightea source bind --origin https://your-deployment --workspace SLUG --source SOURCE --host HOST [--session-file FILE] [--json]
6
- ` + "hightea source show --origin https://your-deployment --workspace SLUG --source SOURCE [--session-file FILE] [--json]";
7
- var NOT_OWNER = new Set(["FORBIDDEN", "NOT_A_MEMBER", "UNAUTHENTICATED"]);
8
- var BOUND_ELSEWHERE = "SOURCE_BINDING_CONFLICT";
9
- function defaults() {
10
- return {
11
- fetch: (url, init) => globalThis.fetch(url, init),
12
- readFile: (path) => readFile(path, "utf8"),
13
- stat: (path) => stat(path),
14
- env: process.env,
15
- home: homedir(),
16
- out: (line) => {
17
- process.stdout.write(`${line}
18
- `);
19
- },
20
- err: (line) => {
21
- process.stderr.write(`${line}
22
- `);
23
- }
24
- };
25
- }
26
- function checkedOrigin(origin) {
27
- try {
28
- const url = new URL(origin);
29
- return url.protocol === "https:" && url.pathname === "/" && url.search === "" && url.hash === "" ? url.origin : null;
30
- } catch {
31
- return null;
32
- }
33
- }
34
- async function readSession(target, io) {
35
- const file = target.sessionFile?.trim() || io.env.HIGHTEA_SESSION_FILE?.trim() || join(io.env.XDG_CONFIG_HOME?.trim() || join(io.home, ".config"), "hightea", "session");
36
- let raw;
37
- let mode;
38
- try {
39
- raw = await io.readFile(file);
40
- mode = (await io.stat(file)).mode;
41
- } catch (error) {
42
- return { error: `SOURCE_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.` };
43
- }
44
- if ((mode & 63) !== 0) {
45
- return { error: `SOURCE_SESSION_EXPOSED: ${file} is mode 0${(mode & 511).toString(8).padStart(3, "0")}; run chmod 600 ${file}.` };
46
- }
47
- const lines = raw.split(`
48
- `).map((line) => line.trim()).filter((line) => line.length > 0);
49
- if (lines.length !== 1) {
50
- return { error: `SOURCE_SESSION_INVALID: ${file} must hold exactly one non-empty line (the ht_session cookie value); it holds ${lines.length}.` };
51
- }
52
- return { cookie: lines[0] };
53
- }
54
- async function send(target, io, request) {
55
- const origin = checkedOrigin(target.origin);
56
- if (origin === null) {
57
- return { exit: 1, error: `SOURCE_ORIGIN_INVALID: --origin must be the https deployment origin, e.g. https://hightea.dev (got ${JSON.stringify(target.origin)}).` };
58
- }
59
- if (target.workspace.trim().length === 0 || target.source.trim().length === 0) {
60
- return { exit: 1, error: `SOURCE_INPUT: --workspace and --source are required.
61
- ${USAGE}` };
62
- }
63
- const session = await readSession(target, io);
64
- if ("error" in session)
65
- return { exit: 1, error: session.error };
66
- const url = `${origin}/api/ws/${encodeURIComponent(target.workspace)}/sources/${encodeURIComponent(target.source)}${request.path}`;
67
- let response;
68
- try {
69
- response = await io.fetch(url, {
70
- method: request.method,
71
- headers: {
72
- cookie: `ht_session=${session.cookie}`,
73
- origin,
74
- accept: "application/json",
75
- "cache-control": "no-store",
76
- ...request.body === undefined ? {} : { "content-type": "application/json" }
77
- },
78
- ...request.body === undefined ? {} : { body: JSON.stringify(request.body) }
79
- });
80
- } catch (error) {
81
- return { exit: 1, error: `SOURCE_UNREACHABLE: ${request.what} ${request.method} ${url} did not answer: ${error.message}` };
82
- }
83
- const text = await response.text();
84
- let answer = {};
85
- if (text.length > 0) {
86
- try {
87
- const parsed = JSON.parse(text);
88
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
89
- throw new SyntaxError("not a JSON object");
90
- answer = parsed;
91
- } catch (error) {
92
- if (!(error instanceof SyntaxError))
93
- throw error;
94
- return { exit: 1, error: `SOURCE_ANSWER_INVALID: ${request.what} ${request.method} ${url} answered HTTP ${response.status} with non-JSON: ${text.slice(0, 300)}` };
95
- }
96
- }
97
- if (response.ok)
98
- return { answer };
99
- const refusal = answer.error;
100
- const code = typeof refusal?.code === "string" ? refusal.code : `HTTP_${response.status}`;
101
- const message = typeof refusal?.message === "string" ? refusal.message : text.slice(0, 300);
102
- const exit = NOT_OWNER.has(code) ? 3 : code === BOUND_ELSEWHERE ? 4 : 1;
103
- return { exit, error: `${code}: ${message}` };
104
- }
105
- function datasetLine(source) {
106
- const field = (name) => source[name] === null || source[name] === undefined ? "none" : String(source[name]);
107
- const listing = source.listing === true ? ", first list running" : source.listing === false ? ", no list running" : "";
108
- return `${field("status")} on ${field("hostId")} as dataset ${field("dataset")} (freshness ${field("freshness")}${listing})`;
109
- }
110
- async function sourceBind(target, io = defaults()) {
111
- if (target.host.trim().length === 0) {
112
- io.err(`SOURCE_INPUT: --host is required (the host id that serves the source).
113
- ${USAGE}`);
114
- return 1;
115
- }
116
- const sent = await send(target, io, { method: "POST", path: "/bind", body: { hostId: target.host.trim() }, what: "bind" });
117
- if ("error" in sent) {
118
- io.err(sent.error);
119
- return sent.exit;
120
- }
121
- if (target.json) {
122
- io.out(JSON.stringify(sent.answer));
123
- return 0;
124
- }
125
- const source = sent.answer.source ?? {};
126
- io.out(`bound ${String(source.sourceId ?? target.source)} to ${String(source.hostId ?? target.host)}: ${datasetLine(source)}`);
127
- io.out("The source's first list fills its dataset; run `hightea source show` to watch its freshness.");
128
- return 0;
129
- }
130
- async function sourceShow(target, io = defaults()) {
131
- const sent = await send(target, io, { method: "GET", path: "", what: "read" });
132
- if ("error" in sent) {
133
- io.err(sent.error);
134
- return sent.exit;
135
- }
136
- if (target.json) {
137
- io.out(JSON.stringify(sent.answer));
138
- return 0;
139
- }
140
- const source = sent.answer.source ?? {};
141
- const position = source.position;
142
- io.out(`${String(source.sourceId ?? target.source)}: ${datasetLine(source)}`);
143
- io.out(`position ${position === null || position === undefined ? "none" : `${String(position.epoch)}/${String(position.ordinal)}`}, ` + `last error ${source.lastError === null || source.lastError === undefined ? "none" : String(source.lastError)}, ` + `dropped ${String(source.dropped ?? 0)}, duplicates ${String(source.duplicates ?? 0)}`);
144
- return 0;
145
- }
146
- export {
147
- sourceBind,
148
- sourceShow
149
- };