@hallohuman/hightea 0.1.30 → 0.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +5 -0
  2. package/dist/bin.js +30 -362
  3. package/dist/chunk-3emak1d3.js +2 -0
  4. package/dist/chunk-55a0ndzw.js +5 -0
  5. package/dist/chunk-5hvk5pvs.js +9 -0
  6. package/dist/chunk-749gsvce.js +139 -0
  7. package/dist/chunk-7mgx0krk.js +5 -0
  8. package/dist/chunk-ah99zytf.js +58 -0
  9. package/dist/chunk-c7atktnb.js +5 -0
  10. package/dist/chunk-dvsftn8z.js +5 -0
  11. package/dist/chunk-dzr0pvfa.js +1 -0
  12. package/dist/chunk-gbwfgq6g.js +1 -0
  13. package/dist/chunk-h9f45p57.js +4 -0
  14. package/dist/chunk-hrtxeg8j.js +1 -0
  15. package/dist/chunk-nfmmht3j.js +2 -0
  16. package/dist/chunk-pdnrwmme.js +1 -0
  17. package/dist/chunk-pexgyagn.js +2 -0
  18. package/dist/chunk-v221hgba.js +2 -0
  19. package/dist/chunk-vr4rsfaf.js +13 -0
  20. package/dist/chunk-w2f84aab.js +1 -0
  21. package/dist/chunk-wyzpk81n.js +13 -0
  22. package/dist/plugin/plugin.json +4 -1
  23. package/dist/plugin/plugin.mjs +51 -15364
  24. package/package.json +2 -1
  25. package/dist/chunk-0ab6h39c.js +0 -402
  26. package/dist/chunk-0r70ekxd.js +0 -8
  27. package/dist/chunk-13tpn1g6.js +0 -465
  28. package/dist/chunk-3rgfnt23.js +0 -16
  29. package/dist/chunk-5mezgvms.js +0 -139
  30. package/dist/chunk-9e325ntw.js +0 -89
  31. package/dist/chunk-cx3yvvkz.js +0 -7295
  32. package/dist/chunk-d5h204vj.js +0 -14
  33. package/dist/chunk-dvnzt54t.js +0 -7168
  34. package/dist/chunk-dvt4hb8z.js +0 -1491
  35. package/dist/chunk-gk8tyqac.js +0 -184
  36. package/dist/chunk-h8pdqn31.js +0 -157
  37. package/dist/chunk-ph04jess.js +0 -17
  38. package/dist/chunk-sbqmqp8e.js +0 -123
  39. package/dist/chunk-w0ebwtch.js +0 -161
  40. package/dist/chunk-y5r2yxx1.js +0 -347
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@hallohuman/hightea",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "description": "The Hightea command line: join this machine to a Hightea workspace as a host.",
5
+ "license": "UNLICENSED",
5
6
  "publishConfig": {
6
7
  "access": "public"
7
8
  },
@@ -1,402 +0,0 @@
1
- // src/auth.ts
2
- import { chmod, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
3
- import { spawn } from "node:child_process";
4
- import { randomUUID } from "node:crypto";
5
- import { homedir } from "node:os";
6
- import { join } from "node:path";
7
- var AUTH_USAGE = `hightea auth login --origin https://your-deployment [--no-browser] [--json]
8
- ` + `hightea auth status --origin https://your-deployment [--json]
9
- ` + "hightea auth logout --origin https://your-deployment [--json]";
10
- function redactCredentialText(text) {
11
- return text.replace(/\brt_[A-Za-z0-9_-]+/g, "[redacted-refresh-token]").replace(/\bht_session=[^;\s]+/g, "ht_session=[redacted]").replace(/\b[A-Za-z0-9_-]{40,}\b/g, "[redacted-token]");
12
- }
13
- function authIo() {
14
- return {
15
- readFile: (path) => readFile(path, "utf8"),
16
- stat: (path) => stat(path),
17
- writeFile: (path, data, options) => writeFile(path, data, options),
18
- mkdir: async (path, options) => {
19
- await mkdir(path, options);
20
- },
21
- chmod: (path, mode) => chmod(path, mode),
22
- rename: (from, to) => rename(from, to),
23
- rm: (path, options) => rm(path, options),
24
- env: process.env,
25
- home: homedir(),
26
- nowMs: () => Date.now(),
27
- fetch: (url, init) => globalThis.fetch(url, init),
28
- out: (line) => {
29
- process.stdout.write(`${line}
30
- `);
31
- },
32
- err: (line) => {
33
- process.stderr.write(`${line}
34
- `);
35
- },
36
- sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
37
- openBrowser: async (url) => {
38
- const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open";
39
- const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
40
- try {
41
- spawn(command, args, { stdio: "ignore", detached: true }).unref();
42
- } catch {}
43
- }
44
- };
45
- }
46
- function checkedOrigin2(origin, label) {
47
- let url;
48
- try {
49
- url = new URL(origin);
50
- } catch {
51
- return { error: `${label}_ORIGIN_INVALID: --origin must be the https deployment origin, e.g. https://hightea.dev (got ${JSON.stringify(origin)}).` };
52
- }
53
- const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]" || url.hostname === "::1";
54
- if (url.protocol !== "https:" && !(url.protocol === "http:" && loopback) || url.pathname !== "/" || url.search !== "" || url.hash !== "") {
55
- return { error: `${label}_ORIGIN_INVALID: --origin must be the https deployment origin (http is allowed only on localhost), e.g. https://hightea.dev (got ${JSON.stringify(origin)}).` };
56
- }
57
- return { origin: url.origin };
58
- }
59
- function configDirectory(io) {
60
- return io.env.XDG_CONFIG_HOME?.trim() || join(io.home, ".config");
61
- }
62
- function credentialsPath(_origin, io) {
63
- return join(configDirectory(io), "hightea", "credentials.json");
64
- }
65
- function signInHint(origin) {
66
- return `run \`hightea auth login --origin ${origin}\``;
67
- }
68
- function modeText(mode) {
69
- return `0${(mode & 511).toString(8).padStart(3, "0")}`;
70
- }
71
- async function writeCredentialFile(path, file, io) {
72
- const temp = `${path}.${process.pid}.${randomUUID()}.tmp`;
73
- try {
74
- await io.writeFile(temp, `${JSON.stringify(file, null, 2)}
75
- `, { mode: 384, flag: "wx" });
76
- await io.chmod(temp, 384);
77
- await io.rename(temp, path);
78
- } catch (error) {
79
- try {
80
- await io.rm?.(temp, { force: true });
81
- } catch {}
82
- throw error;
83
- }
84
- }
85
- async function readCredentialFile(path, io) {
86
- let raw;
87
- let mode;
88
- try {
89
- raw = await io.readFile(path);
90
- mode = (await io.stat(path)).mode;
91
- } catch (error) {
92
- if (error.code === "ENOENT")
93
- return { error: "AUTH_NOT_SIGNED_IN" };
94
- return { error: `AUTH_CREDENTIALS_UNREADABLE: ${path} (${error.message})` };
95
- }
96
- if ((mode & 63) !== 0) {
97
- return { error: `AUTH_CREDENTIALS_EXPOSED: ${path} is mode ${modeText(mode)}; run chmod 600 ${path}.` };
98
- }
99
- try {
100
- const parsed = JSON.parse(raw);
101
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
102
- throw new SyntaxError("not an object");
103
- const candidate = parsed;
104
- if (candidate.version !== 1 || typeof candidate.origins !== "object" || candidate.origins === null || Array.isArray(candidate.origins)) {
105
- throw new SyntaxError("not a version 1 credential file");
106
- }
107
- return { file: candidate };
108
- } catch (error) {
109
- return { error: `AUTH_CREDENTIALS_INVALID: ${path} (${error.message})` };
110
- }
111
- }
112
- function validCredential(value) {
113
- const candidate = value;
114
- return candidate !== null && typeof candidate === "object" && typeof candidate.sessionToken === "string" && candidate.sessionToken.length > 0 && typeof candidate.sessionExpiresAt === "string" && Number.isFinite(Date.parse(candidate.sessionExpiresAt)) && (candidate.refreshToken === undefined || typeof candidate.refreshToken === "string") && (candidate.account === undefined || typeof candidate.account === "object" && candidate.account !== null);
115
- }
116
- async function loadCredential(origin, io) {
117
- const path = credentialsPath(origin, io);
118
- const read = await readCredentialFile(path, io);
119
- if ("error" in read) {
120
- if (read.error === "AUTH_NOT_SIGNED_IN")
121
- return { error: `${read.error}: no sign-in for ${origin}; ${signInHint(origin)}.` };
122
- if (read.error.startsWith("AUTH_CREDENTIALS_INVALID"))
123
- return { error: `${read.error}; ${signInHint(origin)}.` };
124
- return { error: read.error };
125
- }
126
- const credential = read.file.origins[origin];
127
- if (!validCredential(credential)) {
128
- return { error: `AUTH_NOT_SIGNED_IN: no sign-in for ${origin}; ${signInHint(origin)}.` };
129
- }
130
- return { credential };
131
- }
132
- async function saveCredential(origin, credential, io) {
133
- if (io.writeFile === undefined || io.mkdir === undefined || io.chmod === undefined || io.rename === undefined) {
134
- return { error: "AUTH_FILESYSTEM_UNAVAILABLE: this command had no filesystem to store the credential." };
135
- }
136
- const path = credentialsPath(origin, io);
137
- const directory = join(configDirectory(io), "hightea");
138
- let origins = {};
139
- try {
140
- const existing = JSON.parse(await io.readFile(path));
141
- if (typeof existing.origins === "object" && existing.origins !== null && !Array.isArray(existing.origins)) {
142
- origins = existing.origins;
143
- }
144
- } catch {}
145
- try {
146
- await io.mkdir(directory, { recursive: true, mode: 448 });
147
- const file = { version: 1, origins: { ...origins, [origin]: credential } };
148
- await writeCredentialFile(path, file, io);
149
- return { ok: true };
150
- } catch (error) {
151
- return { error: `AUTH_CREDENTIALS_UNWRITABLE: ${path} (${error.message})` };
152
- }
153
- }
154
- async function removeCredential(origin, io) {
155
- if (io.writeFile === undefined || io.chmod === undefined || io.rename === undefined || io.rm === undefined) {
156
- return { error: "AUTH_FILESYSTEM_UNAVAILABLE: this command had no filesystem to forget the credential." };
157
- }
158
- const path = credentialsPath(origin, io);
159
- let file;
160
- try {
161
- file = JSON.parse(await io.readFile(path));
162
- } catch (error) {
163
- if (error.code === "ENOENT")
164
- return { ok: true };
165
- return { error: `AUTH_CREDENTIALS_INVALID: ${path} (${error.message}).` };
166
- }
167
- const origins = { ...file.origins };
168
- delete origins[origin];
169
- try {
170
- if (Object.keys(origins).length === 0) {
171
- await io.rm(path, { force: true });
172
- } else {
173
- await writeCredentialFile(path, { version: 1, origins }, io);
174
- }
175
- return { ok: true };
176
- } catch (error) {
177
- return { error: `AUTH_CREDENTIALS_UNWRITABLE: ${path} (${error.message})` };
178
- }
179
- }
180
- async function postJson(io, url, origin, body) {
181
- let response;
182
- try {
183
- response = await io.fetch(url, {
184
- method: "POST",
185
- headers: { origin, "content-type": "application/json", accept: "application/json", "cache-control": "no-store" },
186
- body: JSON.stringify(body)
187
- });
188
- } catch (error) {
189
- return { error: `AUTH_UNREACHABLE: ${url} did not answer: ${error.message}` };
190
- }
191
- const text = await response.text();
192
- let answer = {};
193
- if (text.length > 0) {
194
- try {
195
- const parsed = JSON.parse(text);
196
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
197
- throw new SyntaxError("not an object");
198
- answer = parsed;
199
- } catch {
200
- return { error: `AUTH_ANSWER_INVALID: ${url} answered HTTP ${response.status} with non-JSON: ${redactCredentialText(text.slice(0, 200))}` };
201
- }
202
- }
203
- return { status: response.status, answer };
204
- }
205
- function refusal(answer) {
206
- const error = answer.error;
207
- return {
208
- code: typeof error?.code === "string" ? error.code : "HTTP_ERROR",
209
- message: redactCredentialText(typeof error?.message === "string" ? error.message : "the deployment refused the request")
210
- };
211
- }
212
- function credentialFrom(answer) {
213
- const sessionToken = answer.sessionToken;
214
- const sessionExpiresAt = answer.sessionExpiresAt;
215
- const refreshToken = answer.refreshToken;
216
- const account = answer.account;
217
- if (typeof sessionToken !== "string" || sessionToken.length === 0 || typeof sessionExpiresAt !== "string" || !Number.isFinite(Date.parse(sessionExpiresAt)) || typeof refreshToken !== "string" || refreshToken.length === 0)
218
- return null;
219
- return { sessionToken, sessionExpiresAt, refreshToken, ...account === undefined ? {} : { account } };
220
- }
221
- async function refreshCredential(origin, credential, io, retried = false) {
222
- if (credential.refreshToken === undefined || credential.refreshToken.length === 0) {
223
- return { error: `AUTH_SESSION_EXPIRED: no refresh token for ${origin}; ${signInHint(origin)}.` };
224
- }
225
- const result = await postJson(io, `${origin}/auth/cli/refresh`, origin, { refreshToken: credential.refreshToken, sessionToken: credential.sessionToken });
226
- if ("error" in result)
227
- return { error: result.error };
228
- if (result.status !== 200) {
229
- const named = refusal(result.answer);
230
- if (!retried && (result.status === 401 || named.code === "REFRESH_INVALID")) {
231
- const stored = await loadCredential(origin, io);
232
- if ("credential" in stored && stored.credential.refreshToken !== credential.refreshToken) {
233
- const remaining = Date.parse(stored.credential.sessionExpiresAt) - (io.nowMs?.() ?? Date.now());
234
- if (remaining > 60000)
235
- return stored;
236
- return await refreshCredential(origin, stored.credential, io, true);
237
- }
238
- }
239
- return { error: `AUTH_${named.code}: ${named.message} ${signInHint(origin)}` };
240
- }
241
- const next = credentialFrom(result.answer);
242
- if (next === null)
243
- return { error: "AUTH_ANSWER_INVALID: the refresh answer lacked a session or refresh token." };
244
- const saved = await saveCredential(origin, { ...next, ...credential.account === undefined ? {} : { account: credential.account } }, io);
245
- if ("error" in saved)
246
- return saved;
247
- return { credential: next };
248
- }
249
- async function ensureCredential2(origin, io) {
250
- const loaded = await loadCredential(origin, io);
251
- if ("error" in loaded)
252
- return loaded;
253
- const remaining = Date.parse(loaded.credential.sessionExpiresAt) - (io.nowMs?.() ?? Date.now());
254
- if (remaining > 60000)
255
- return loaded;
256
- return await refreshCredential(origin, loaded.credential, io);
257
- }
258
- async function authLogin2(target, io = authIo()) {
259
- const where = checkedOrigin2(target.origin, "AUTH");
260
- if ("error" in where) {
261
- io.err(where.error);
262
- return 1;
263
- }
264
- const origin = where.origin;
265
- const device = await postJson(io, `${origin}/auth/cli/device`, origin, {});
266
- if ("error" in device) {
267
- io.err(device.error);
268
- return 1;
269
- }
270
- if (device.status !== 200) {
271
- const named = refusal(device.answer);
272
- io.err(`AUTH_${named.code}: ${named.message}`);
273
- return 1;
274
- }
275
- const deviceCode = device.answer.deviceCode;
276
- const userCode = device.answer.userCode;
277
- const verificationUri = device.answer.verificationUri;
278
- const completeUri = device.answer.verificationUriComplete;
279
- const expiresIn = device.answer.expiresIn;
280
- const interval = device.answer.interval;
281
- if (typeof deviceCode !== "string" || typeof userCode !== "string" || typeof verificationUri !== "string" || typeof completeUri !== "string" || typeof expiresIn !== "number" || typeof interval !== "number") {
282
- io.err("AUTH_ANSWER_INVALID: the device authorization answer was malformed.");
283
- return 1;
284
- }
285
- const progress = target.json ? io.err : io.out;
286
- progress(`To sign in to ${origin}, open ${verificationUri}`);
287
- progress(`and confirm the code ${userCode}.`);
288
- if (target.noBrowser !== true) {
289
- progress(`Opening ${completeUri}`);
290
- await io.openBrowser(completeUri);
291
- }
292
- let waitMs = interval * 1000;
293
- const deadline = (io.nowMs?.() ?? Date.now()) + expiresIn * 1000;
294
- const maxPolls = Math.max(1, Math.ceil(expiresIn / interval) + 1);
295
- for (let poll = 0;poll < maxPolls; poll += 1) {
296
- if ((io.nowMs?.() ?? Date.now()) >= deadline)
297
- break;
298
- await io.sleep(waitMs);
299
- const token = await postJson(io, `${origin}/auth/cli/token`, origin, { deviceCode });
300
- if ("error" in token) {
301
- io.err(token.error);
302
- return 1;
303
- }
304
- if (token.status === 200) {
305
- const credential = credentialFrom(token.answer);
306
- if (credential === null) {
307
- io.err("AUTH_ANSWER_INVALID: the sign-in answer lacked a session or refresh token.");
308
- return 1;
309
- }
310
- const saved = await saveCredential(origin, credential, io);
311
- if ("error" in saved) {
312
- io.err(saved.error);
313
- return 1;
314
- }
315
- if (target.json) {
316
- io.out(JSON.stringify({ origin, sessionExpiresAt: credential.sessionExpiresAt, account: credential.account ?? null }));
317
- return 0;
318
- }
319
- const email = credential.account?.email;
320
- io.out(`Signed in to ${origin}${typeof email === "string" && email.length > 0 ? ` as ${email}` : ""}.`);
321
- io.out(`Credential stored at ${credentialsPath(origin, io)} (mode 0600).`);
322
- return 0;
323
- }
324
- const named = refusal(token.answer);
325
- if (named.code === "AUTHORIZATION_PENDING")
326
- continue;
327
- if (named.code === "SLOW_DOWN") {
328
- waitMs += 5000;
329
- continue;
330
- }
331
- io.err(`AUTH_${named.code}: ${named.message}`);
332
- return 1;
333
- }
334
- io.err(`AUTH_EXPIRED_TOKEN: the sign-in code expired; ${signInHint(origin)}.`);
335
- return 1;
336
- }
337
- async function authStatus2(target, io = authIo()) {
338
- const where = checkedOrigin2(target.origin, "AUTH");
339
- if ("error" in where) {
340
- io.err(where.error);
341
- return 1;
342
- }
343
- const loaded = await loadCredential(where.origin, io);
344
- if ("error" in loaded) {
345
- io.err(loaded.error);
346
- return 1;
347
- }
348
- const credential = loaded.credential;
349
- const expired = Date.parse(credential.sessionExpiresAt) <= (io.nowMs?.() ?? Date.now());
350
- const account = credential.account;
351
- if (target.json) {
352
- io.out(JSON.stringify({ origin: where.origin, account: account ?? null, sessionExpiresAt: credential.sessionExpiresAt, expired }));
353
- return 0;
354
- }
355
- const email = account?.email;
356
- io.out(`Signed in to ${where.origin}${typeof email === "string" && email.length > 0 ? ` as ${email}` : ""}.`);
357
- io.out(`Session ${expired ? "expired" : "expires"} ${credential.sessionExpiresAt}${expired ? `; ${signInHint(where.origin)}.` : "."}`);
358
- return 0;
359
- }
360
- async function authLogout2(target, io = authIo()) {
361
- const where = checkedOrigin2(target.origin, "AUTH");
362
- if ("error" in where) {
363
- io.err(where.error);
364
- return 1;
365
- }
366
- const origin = where.origin;
367
- const loaded = await loadCredential(origin, io);
368
- if ("error" in loaded) {
369
- if (loaded.error.startsWith("AUTH_NOT_SIGNED_IN")) {
370
- io.out(`Not signed in to ${origin}; nothing to do.`);
371
- return 0;
372
- }
373
- io.err(loaded.error);
374
- return 1;
375
- }
376
- let revoked = true;
377
- try {
378
- const response = await io.fetch(`${origin}/auth/logout`, {
379
- method: "POST",
380
- headers: { origin, cookie: `ht_session=${loaded.credential.sessionToken}`, accept: "application/json", "cache-control": "no-store" }
381
- });
382
- revoked = response.ok || response.status === 302;
383
- } catch {
384
- revoked = false;
385
- }
386
- if (!revoked) {
387
- io.err(`AUTH_LOGOUT_FAILED: ${origin} could not be told to revoke the session; the local credential was kept. Try again.`);
388
- return 1;
389
- }
390
- const removed = await removeCredential(origin, io);
391
- if ("error" in removed) {
392
- io.err(removed.error);
393
- return 1;
394
- }
395
- if (target.json)
396
- io.out(JSON.stringify({ origin, signedOut: true, revoked }));
397
- else
398
- io.out(`Signed out of ${origin}.`);
399
- return 0;
400
- }
401
-
402
- export { checkedOrigin2, ensureCredential2, authLogin2, authStatus2, authLogout2 };
@@ -1,8 +0,0 @@
1
- import {
2
- hostTrust2
3
- } from "./chunk-w0ebwtch.js";
4
- import"./chunk-dvt4hb8z.js";
5
- import"./chunk-dvnzt54t.js";
6
- export {
7
- hostTrust2 as hostTrust
8
- };