tailcat-webui 0.0.0-stage → 0.2.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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +29 -0
  3. package/README.en.md +112 -0
  4. package/README.md +101 -2
  5. package/bin/tailcat-webui.js +12 -0
  6. package/docs/CORE-API.md +343 -0
  7. package/docs/DESIGN.md +398 -0
  8. package/docs/assets/character.jpg +0 -0
  9. package/docs/assets/portrait.jpg +0 -0
  10. package/docs/screenshots/01-overview-light.png +0 -0
  11. package/docs/screenshots/02-overview-dark.png +0 -0
  12. package/docs/screenshots/03-ticket-light.png +0 -0
  13. package/docs/screenshots/04-ticket-qr-light.png +0 -0
  14. package/docs/screenshots/05-settings-light.png +0 -0
  15. package/docs/screenshots/06-diagnostics-light.png +0 -0
  16. package/docs/screenshots/07-mobile-overview.png +0 -0
  17. package/docs/screenshots/08-install-missing.png +0 -0
  18. package/docs/screenshots/09-keeper.png +0 -0
  19. package/package.json +56 -4
  20. package/src/cli.js +238 -0
  21. package/src/config.js +167 -0
  22. package/src/devices.js +163 -0
  23. package/src/errors.js +24 -0
  24. package/src/index.js +19 -0
  25. package/src/installer.js +907 -0
  26. package/src/manager.js +647 -0
  27. package/src/parse.js +207 -0
  28. package/src/paths.js +97 -0
  29. package/src/preferences.js +190 -0
  30. package/src/qr.js +37 -0
  31. package/src/relay.js +76 -0
  32. package/src/runtime/node.js +281 -0
  33. package/src/serve-targets.js +127 -0
  34. package/src/server.js +674 -0
  35. package/src/settings.js +262 -0
  36. package/src/supervisor.js +833 -0
  37. package/vendor/qrcode.mjs +2289 -0
  38. package/web/api.js +114 -0
  39. package/web/assets/portrait.jpg +0 -0
  40. package/web/boot.js +29 -0
  41. package/web/components.js +558 -0
  42. package/web/dict.js +663 -0
  43. package/web/drawer-access.js +318 -0
  44. package/web/drawer-address.js +271 -0
  45. package/web/drawer-automation.js +223 -0
  46. package/web/drawer-relay.js +128 -0
  47. package/web/drawer-tunnel.js +309 -0
  48. package/web/drawer.js +302 -0
  49. package/web/format.js +252 -0
  50. package/web/icon.png +0 -0
  51. package/web/index.html +29 -0
  52. package/web/main.js +471 -0
  53. package/web/room-diagnostics.js +426 -0
  54. package/web/room-overview.js +438 -0
  55. package/web/room-settings.js +71 -0
  56. package/web/store.js +168 -0
  57. package/web/styles.css +2873 -0
  58. package/web/ticket.js +212 -0
package/web/api.js ADDED
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The only place that knows the local HTTP contract.
3
+ *
4
+ * `GET /api/state` returns the snapshot; `POST /api/action` carries one action
5
+ * plus its payload and answers with the next snapshot. Every failure becomes an
6
+ * `ApiError` whose `message` is what the interface shows to the user.
7
+ */
8
+
9
+ /** The header the server requires on every write. */
10
+ const ACTION_HEADER = 'x-tailcat-webui';
11
+ const REQUEST_TIMEOUT_MS = 15_000;
12
+ const ACTION_TIMEOUT_MS = 5 * 60_000;
13
+
14
+ /** A request that failed; `code` mirrors the server error code when present. */
15
+ export class ApiError extends Error {
16
+ /**
17
+ * @param {string} message - operator-facing message.
18
+ * @param {string} code - server error code, or `network`/`http`.
19
+ * @param {number} [status] - HTTP status.
20
+ */
21
+ constructor(message, code = 'http', status = 0) {
22
+ super(message);
23
+ this.name = 'ApiError';
24
+ this.code = code;
25
+ this.status = status;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * @param {Response} response - fetch response.
31
+ * @returns {Promise<object | null>} parsed body, or null.
32
+ */
33
+ async function readJson(response) {
34
+ try {
35
+ return await response.json();
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @param {object | null} data - parsed body.
43
+ * @param {number} status - HTTP status.
44
+ * @returns {ApiError} normalized failure.
45
+ */
46
+ function failure(data, status) {
47
+ const error = data && typeof data === 'object' && data.error && typeof data.error === 'object' ? data.error : null;
48
+ const message = error && typeof error.message === 'string' && error.message !== '' ? error.message : `HTTP ${status}`;
49
+ const code = error && typeof error.code === 'string' && error.code !== '' ? error.code : 'http';
50
+ return new ApiError(message, code, status);
51
+ }
52
+
53
+ /**
54
+ * @param {string} path - request path.
55
+ * @param {RequestInit} options - fetch options.
56
+ * @param {number} timeoutMs - abort bound.
57
+ * @returns {Promise<object>} response body.
58
+ */
59
+ async function requestJson(path, options, timeoutMs) {
60
+ const controller = typeof AbortController === 'function' ? new AbortController() : null;
61
+ const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
62
+ let response;
63
+ try {
64
+ response = await fetch(path, controller ? { ...options, signal: controller.signal } : options);
65
+ } catch {
66
+ throw new ApiError('Could not reach the local service.', 'network');
67
+ } finally {
68
+ if (timer !== null) clearTimeout(timer);
69
+ }
70
+ const data = await readJson(response);
71
+ if (!response.ok || !data || data.ok !== true) throw failure(data, response.status);
72
+ return data;
73
+ }
74
+
75
+ /**
76
+ * @param {{ baseUrl?: string }} [options] - optional absolute base for tests.
77
+ * @returns {{ state: () => Promise<object>, action: (action: string, payload?: object) => Promise<object> }} client.
78
+ */
79
+ export function createApi(options = {}) {
80
+ const base = typeof options.baseUrl === 'string' ? options.baseUrl.replace(/\/$/, '') : '';
81
+ return {
82
+ /**
83
+ * @returns {Promise<object>} the current snapshot (plus `server`).
84
+ */
85
+ state() {
86
+ return requestJson(`${base}/api/state`, {
87
+ method: 'GET',
88
+ headers: { accept: 'application/json' },
89
+ cache: 'no-store',
90
+ credentials: 'same-origin',
91
+ }, REQUEST_TIMEOUT_MS);
92
+ },
93
+
94
+ /**
95
+ * @param {string} action - one of start/stop/restart/rotate/settings/devices/derpMap/install.
96
+ * @param {object} [payload] - action payload; merged next to `action`.
97
+ * @returns {Promise<object>} the snapshot after the action.
98
+ */
99
+ action(action, payload = {}) {
100
+ if (typeof action !== 'string' || action === '') throw new ApiError('Missing action name.', 'invalid-action');
101
+ return requestJson(`${base}/api/action`, {
102
+ method: 'POST',
103
+ headers: {
104
+ accept: 'application/json',
105
+ 'content-type': 'application/json',
106
+ [ACTION_HEADER]: '1',
107
+ },
108
+ cache: 'no-store',
109
+ credentials: 'same-origin',
110
+ body: JSON.stringify({ action, ...payload }),
111
+ }, ACTION_TIMEOUT_MS);
112
+ },
113
+ };
114
+ }
Binary file
package/web/boot.js ADDED
@@ -0,0 +1,29 @@
1
+ /*
2
+ * Runs before the stylesheet so the first paint already has the right theme and
3
+ * language. Kept as a classic script on purpose: module scripts run after the
4
+ * document is parsed, which would flash the wrong colours in dark mode.
5
+ */
6
+ (function () {
7
+ var root = document.documentElement;
8
+ var theme = 'system';
9
+ var lang = null;
10
+ try {
11
+ var storedTheme = window.localStorage.getItem('tailcat-webui.theme');
12
+ if (storedTheme === 'light' || storedTheme === 'dark' || storedTheme === 'system') theme = storedTheme;
13
+ var storedLang = window.localStorage.getItem('tailcat-webui.lang');
14
+ if (storedLang === 'zh' || storedLang === 'en') lang = storedLang;
15
+ } catch (error) {
16
+ /* Storage is optional; the system preference still applies. */
17
+ }
18
+ var dark = theme === 'dark';
19
+ if (theme === 'system' && typeof window.matchMedia === 'function') {
20
+ dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
21
+ }
22
+ root.setAttribute('data-theme', dark ? 'dark' : 'light');
23
+ root.setAttribute('data-theme-mode', theme);
24
+ if (lang === null) {
25
+ var browser = (window.navigator && window.navigator.language) || '';
26
+ lang = /^zh/i.test(browser) ? 'zh' : 'en';
27
+ }
28
+ root.setAttribute('lang', lang);
29
+ }());