@x-tech-laboratories/x-app 0.120.0 → 0.122.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.
- package/README.md +27 -6
- package/dist/agent/ssh_config.d.ts +12 -0
- package/dist/agent/ssh_config.js +72 -0
- package/dist/app/daemon.d.ts +7 -1
- package/dist/app/daemon.js +82 -8
- package/dist/app/define.d.ts +38 -1
- package/dist/app/define.js +34 -1
- package/dist/app/desktop.d.ts +13 -0
- package/dist/app/desktop.js +35 -3
- package/dist/app/host_tables.d.ts +21 -0
- package/dist/app/host_tables.js +105 -0
- package/dist/app/preload.js +5 -0
- package/dist/app/protocol.d.ts +31 -1
- package/dist/app/protocol.js +8 -1
- package/dist/app/remote_access.d.ts +17 -0
- package/dist/app/remote_access.js +85 -0
- package/dist/app/server.d.ts +20 -5
- package/dist/app/server.js +101 -22
- package/dist/app/services.d.ts +25 -2
- package/dist/app/web/app_page.d.ts +1 -1
- package/dist/app/web/app_page.js +73 -12
- package/dist/app/web/hosts.d.ts +5 -3
- package/dist/app/web/hosts.js +35 -2
- package/dist/app/web/page.d.ts +3 -0
- package/dist/app/web/page.js +24 -4
- package/dist/app/web/portal.d.ts +14 -0
- package/dist/app/web/portal.js +115 -0
- package/dist/app/web/settings_page.d.ts +13 -2
- package/dist/app/web/settings_page.js +36 -8
- package/dist/change_checks.js +1 -1
- package/dist/github_targets.d.ts +12 -0
- package/dist/github_targets.js +90 -0
- package/dist/remote_access_protocol.d.ts +26 -0
- package/dist/remote_access_protocol.js +33 -0
- package/dist/server/github_webhook.d.ts +2 -12
- package/dist/server/github_webhook.js +2 -87
- package/dist/server/sqlite_backups.d.ts +2 -2
- package/dist/server/sqlite_backups.js +2 -2
- package/dist/server/viewer_scope.d.ts +3 -0
- package/dist/server/viewer_scope.js +14 -0
- package/dist/web/react/remote_access_settings.d.ts +5 -0
- package/dist/web/react/remote_access_settings.js +55 -0
- package/dist/web/react/shell.js +1 -1
- package/dist/web/remote_daemon.d.ts +26 -0
- package/dist/web/remote_daemon.js +105 -0
- package/dist/web/theme.d.ts +8 -0
- package/dist/web/theme.js +23 -0
- package/package.json +1 -1
|
@@ -33,14 +33,14 @@ export function sqlite_backups(options) {
|
|
|
33
33
|
options.after_staging?.();
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
|
-
close();
|
|
36
|
+
await close();
|
|
37
37
|
try {
|
|
38
38
|
rmSync(`${target}-wal`, { force: true });
|
|
39
39
|
rmSync(`${target}-shm`, { force: true });
|
|
40
40
|
copyFileSync(source, target);
|
|
41
41
|
}
|
|
42
42
|
finally {
|
|
43
|
-
open();
|
|
43
|
+
await open();
|
|
44
44
|
}
|
|
45
45
|
options.after_restore?.(options.database());
|
|
46
46
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Who a request a host answers came from, kept across the call's awaits, so work a route hands off, such as an app's
|
|
2
|
+
// host table call, can name who asked in its audit log or refuse what they may not do.
|
|
3
|
+
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
+
// ── Scope ────────────────────────────────────────────────────────────────────
|
|
6
|
+
const storage = new AsyncLocalStorage();
|
|
7
|
+
// Runs the work with the viewer given as the one asking.
|
|
8
|
+
export function with_viewer(viewer, work) {
|
|
9
|
+
return storage.run(viewer ?? null, work);
|
|
10
|
+
}
|
|
11
|
+
// The viewer of the request in hand, or null outside one or when nobody signed in, as on a daemon's own socket.
|
|
12
|
+
export function current_viewer() {
|
|
13
|
+
return storage.getStore() ?? null;
|
|
14
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// "Let other devices connect": whether this computer's daemon listens on the network, where, over https or not, and
|
|
3
|
+
// the admins a phone, a browser or another computer signs in as, for Settings on the desktop.
|
|
4
|
+
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
5
|
+
import { useEffect, useState } from "react";
|
|
6
|
+
import { StatusPill } from "./badges.js";
|
|
7
|
+
import { Switch } from "./controls.js";
|
|
8
|
+
import { confirm_dialog } from "./dialog.js";
|
|
9
|
+
import { SettingRow } from "./layout.js";
|
|
10
|
+
import { attempt_value as attempt, show_error } from "./toasts.js";
|
|
11
|
+
// What other devices can do once signed in, for the switch's hint.
|
|
12
|
+
const default_reach = "Phones, browsers and other computers sign in as an admin below and work on this computer's data with its rights.";
|
|
13
|
+
// ── Section ──────────────────────────────────────────────────────────────────
|
|
14
|
+
export function RemoteAccessSettings({ remote, reach = default_reach }) {
|
|
15
|
+
const [status, set_status] = useState(null);
|
|
16
|
+
const [draft, set_draft] = useState(null);
|
|
17
|
+
const [name, set_name] = useState("");
|
|
18
|
+
const [password, set_password] = useState("");
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
void remote.get_remote_access().then((found) => {
|
|
21
|
+
set_status(found);
|
|
22
|
+
set_draft(found.settings);
|
|
23
|
+
}, show_error);
|
|
24
|
+
}, [remote]);
|
|
25
|
+
if (!status || !draft)
|
|
26
|
+
return null;
|
|
27
|
+
async function save(next) {
|
|
28
|
+
const saved = await attempt(() => remote.set_remote_access(next));
|
|
29
|
+
if (saved) {
|
|
30
|
+
set_status(saved);
|
|
31
|
+
set_draft(saved.settings);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function add_admin() {
|
|
35
|
+
const saved = await attempt(() => remote.set_remote_admin(name, password), `${name.trim()} can sign in from other devices.`);
|
|
36
|
+
if (!saved)
|
|
37
|
+
return;
|
|
38
|
+
set_status(saved);
|
|
39
|
+
set_name("");
|
|
40
|
+
set_password("");
|
|
41
|
+
}
|
|
42
|
+
function remove_admin(admin) {
|
|
43
|
+
confirm_dialog({
|
|
44
|
+
title: `Remove ${admin}?`,
|
|
45
|
+
message: "Devices signed in as them can no longer reach this computer.",
|
|
46
|
+
confirm_label: "Remove",
|
|
47
|
+
danger: true,
|
|
48
|
+
run: async () => void (await attempt(() => remote.remove_remote_admin(admin)).then((saved) => saved && set_status(saved))),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const field = (key, label, hint, placeholder = "") => (_jsx(SettingRow, { label: label, hint: hint, as_label: true, children: _jsx("input", { type: "text", spellCheck: false, placeholder: placeholder, value: draft[key], onChange: (event) => set_draft({ ...draft, [key]: event.currentTarget.value }) }) }));
|
|
52
|
+
const is_changed = JSON.stringify(draft) !== JSON.stringify(status.settings);
|
|
53
|
+
const state = status.listening ? _jsx(StatusPill, { tone: "good", label: `Listening on ${status.listening}` }) : status.problem ? _jsx(StatusPill, { tone: "bad", label: status.problem }) : _jsx(StatusPill, { label: "Off" });
|
|
54
|
+
return (_jsxs(_Fragment, { children: [_jsx(SettingRow, { label: "Let other devices connect", hint: `${reach} Use https, a VPN or a reverse proxy; plain http sends passwords and tokens as they are.`, as_label: true, children: _jsx(Switch, { label: "Let other devices connect", checked: status.settings.enabled, on_change: (on) => void save({ ...draft, enabled: on }) }) }), _jsx(SettingRow, { label: "State", children: state }), field("host", "Address", "Where to listen: 0.0.0.0 for every network, or one address such as a VPN's.", "0.0.0.0"), _jsx(SettingRow, { label: "Port", as_label: true, children: _jsx("input", { type: "number", min: 1, max: 65535, value: draft.port, onChange: (event) => set_draft({ ...draft, port: Number(event.currentTarget.value) }) }) }), field("certificate_file", "Certificate file", "The https certificate, as a PEM file; empty for plain http.", "/path/to/cert.pem"), field("key_file", "Key file", "The certificate's private key, as a PEM file.", "/path/to/key.pem"), _jsx(SettingRow, { label: "Save", hint: "Listens again with these settings.", children: _jsx("button", { type: "button", disabled: !is_changed, onClick: () => void save(draft), children: "Save" }) }), status.admins.map((admin) => (_jsx(SettingRow, { label: admin, hint: "An admin who may sign in from other devices", children: _jsx("button", { type: "button", className: "danger", onClick: () => remove_admin(admin), children: "Remove" }) }, admin))), _jsxs("form", { onSubmit: (event) => { event.preventDefault(); void add_admin(); }, children: [_jsx(SettingRow, { label: "Admin name", hint: "Signing in again with a new password changes it.", as_label: true, children: _jsx("input", { type: "text", autoCapitalize: "none", spellCheck: false, autoComplete: "off", value: name, onChange: (event) => set_name(event.currentTarget.value) }) }), _jsx(SettingRow, { label: "Password", hint: "At least 10 characters.", as_label: true, children: _jsx("input", { type: "password", autoComplete: "new-password", value: password, onChange: (event) => set_password(event.currentTarget.value) }) }), _jsx(SettingRow, { label: "Add an admin", children: _jsx("button", { type: "submit", disabled: name.trim() === "" || password === "", children: "Add Admin" }) })] })] }));
|
|
55
|
+
}
|
package/dist/web/react/shell.js
CHANGED
|
@@ -248,7 +248,7 @@ export function Shell({ brand, brand_icon, brand_logo, sections = [], views, act
|
|
|
248
248
|
window.scrollTo(0, 0);
|
|
249
249
|
main.current?.scrollTo?.(0, 0);
|
|
250
250
|
}, [view_key]);
|
|
251
|
-
return (_jsxs(_Fragment, { children: [_jsx("a", { className: "skip", href: "#main", children: labels.skip_to_content }), _jsxs("header", { className: "topbar", children: [_jsx("button", { ref: menu_button, className: "icon menu-button", "aria-label": labels.open_menu, "aria-expanded": menu_open, "aria-controls": "sidebar", onClick: () => set_menu_open(!menu_open), children: "\u2630" }), !(brand_in_sidebar && !logged_out) && _jsx("a", { href: brand_href, className: "brand", children: _jsx(BrandMark, { brand: brand, icon: brand_icon, logo: brand_logo }) }), !logged_out && tools, pane_open && !narrow && !logged_out && pane_head] }), _jsxs("div", { className: "layout", style: width === undefined ? undefined : { "--sidebar-width": `${width}px` }, children: [_jsxs(Sidebar, { ref: sidebar, id: "sidebar", className: "sidebar", "aria-label": views ? undefined : labels.main_navigation, children: [brand_in_sidebar && _jsx("a", { href: brand_href, className: "sidebar-brand", children: _jsx(BrandMark, { brand: brand, icon: brand_icon, logo: brand_logo }) }), views || has_content || folding ? (_jsxs("div", { className: "sidebar-content", children: [views && (_jsx("nav", { className: "sidebar-nav", "aria-label": labels.main_navigation, children: views.map((view) => (_jsx(RowLink, { href: view.href, current: view.id === active, icon: view.icon && _jsx(Icon, { icon: view.icon }), label: view.label }, view.id))) })), folding && _jsx(SectionTree, { sections: sections, active: active, active_item: active_item, pref_key: sections_pref, label: labels.main_navigation }), sidebar_content] })) : (_jsx("div", { className: "sidebar-links", children: sections.map((section) => (_jsx("a", { href: section.href, "data-section": section.id, className: section.id === active ? "active" : undefined, "aria-current": section.id === active ? "page" : undefined, children: section.label }, section.id))) })), _jsxs("div", { className: "sidebar-foot", children: [sidebar_foot, _jsx(ThemeRow, { choice: theme, labels: labels }), user_name !== undefined && (_jsxs("div", { className: "sidebar-account", children: [user_href
|
|
251
|
+
return (_jsxs(_Fragment, { children: [_jsx("a", { className: "skip", href: "#main", children: labels.skip_to_content }), _jsxs("header", { className: "topbar", children: [_jsx("button", { ref: menu_button, className: "icon menu-button", "aria-label": labels.open_menu, "aria-expanded": menu_open, "aria-controls": "sidebar", onClick: () => set_menu_open(!menu_open), children: "\u2630" }), !(brand_in_sidebar && !logged_out) && _jsx("a", { href: brand_href, className: "brand", children: _jsx(BrandMark, { brand: brand, icon: brand_icon, logo: brand_logo }) }), !logged_out && tools, pane_open && !narrow && !logged_out && pane_head] }), _jsxs("div", { className: "layout", style: width === undefined ? undefined : { "--sidebar-width": `${width}px` }, children: [_jsxs(Sidebar, { ref: sidebar, id: "sidebar", className: "sidebar", "aria-label": views ? undefined : labels.main_navigation, children: [brand_in_sidebar && _jsx("a", { href: brand_href, className: "sidebar-brand", children: _jsx(BrandMark, { brand: brand, icon: brand_icon, logo: brand_logo }) }), views || has_content || folding ? (_jsxs("div", { className: "sidebar-content", children: [views && (_jsx("nav", { className: "sidebar-nav", "aria-label": labels.main_navigation, children: views.map((view) => (_jsx(RowLink, { href: view.href, current: view.id === active, icon: view.icon && _jsx(Icon, { icon: view.icon }), label: view.label }, view.id))) })), folding && _jsx(SectionTree, { sections: sections, active: active, active_item: active_item, pref_key: sections_pref, label: labels.main_navigation }), sidebar_content] })) : (_jsx("div", { className: "sidebar-links", children: sections.map((section) => (_jsx("a", { href: section.href, "data-section": section.id, className: section.id === active ? "active" : undefined, "aria-current": section.id === active ? "page" : undefined, children: section.label }, section.id))) })), _jsxs("div", { className: "sidebar-foot", children: [sidebar_foot, !theme.pinned && _jsx(ThemeRow, { choice: theme, labels: labels }), user_name !== undefined && (_jsxs("div", { className: "sidebar-account", children: [user_href
|
|
252
252
|
? _jsx(RowLink, { href: user_href, current: location.hash === user_href, icon: _jsx(PersonIcon, {}), label: user_name })
|
|
253
253
|
: _jsx("span", { className: "tree-row", children: _jsxs("span", { className: "tree-label", children: [_jsx(PersonIcon, {}), _jsx("span", { className: "user-name", children: user_name })] }) }), on_logout && _jsx("button", { className: "tree-row", onClick: on_logout, children: labels.sign_out })] })), settings && _jsx(RowLink, { href: settings.href, current: settings.active ?? active === "settings", icon: _jsx(CogIcon, {}), label: labels.settings, on_open: settings.on_open })] }), sidebar_width && width !== undefined && (_jsx(ResizeHandle, { side: "left", size: width, min: sidebar_width.min, max: sidebar_width.max, default_size: sidebar_width.default_width, on_resize: (next) => change_width(next, false), on_resize_end: (next) => change_width(next, true), label: labels.resize_sidebar, class_name: "sidebar-resize" }))] }), _jsx("div", { className: "scrim", hidden: !menu_open, onClick: () => set_menu_open(false) }), side_pane ? (_jsxs("main", { ref: main, id: "main", className: [main_class, "with-side-pane"].filter(Boolean).join(" "), tabIndex: -1,
|
|
254
254
|
// Closed, the pane's column takes no room, so main's children keep their place and their state.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type RemoteRemoteClient<Methods> = {
|
|
2
|
+
[Name in keyof Methods]: Methods[Name] extends (...args: infer Args) => infer Result ? (...args: Args) => Promise<Awaited<Result>> : never;
|
|
3
|
+
};
|
|
4
|
+
export type RemoteClient<Methods> = {
|
|
5
|
+
[Name in keyof Methods]: Methods[Name] extends (...args: infer Args) => infer Result ? (...args: Args) => Promise<Awaited<Result>> : never;
|
|
6
|
+
};
|
|
7
|
+
export interface RemoteDaemon {
|
|
8
|
+
address: string;
|
|
9
|
+
token: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RemoteLink {
|
|
12
|
+
daemon: () => RemoteDaemon | null;
|
|
13
|
+
client_id: () => string;
|
|
14
|
+
fetch?: typeof fetch;
|
|
15
|
+
}
|
|
16
|
+
export declare function call_remote(link: RemoteLink, path: string, args: unknown[]): Promise<unknown>;
|
|
17
|
+
export declare function remote_table<Methods extends Record<keyof Methods, (...args: never[]) => unknown>>(link: RemoteLink, prefix: string): RemoteClient<Methods>;
|
|
18
|
+
export interface FollowRemoteOptions {
|
|
19
|
+
retry_ms?: number;
|
|
20
|
+
on_connect?: () => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function parse_remote_event(block: string): {
|
|
23
|
+
event: string;
|
|
24
|
+
data: string;
|
|
25
|
+
} | null;
|
|
26
|
+
export declare function follow_remote(link: RemoteLink, on_event: (event: string, data: string) => void, options?: FollowRemoteOptions): () => void;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// A page's calls to a daemon or server on another computer, made from the page itself with this device's token, as a
|
|
2
|
+
// phone that keeps no data makes them: each call posted under its table's path, and the host's events followed over a
|
|
3
|
+
// stream read by hand, since EventSource sends no token.
|
|
4
|
+
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
5
|
+
import { LoginNeeded } from "./states.js";
|
|
6
|
+
const no_daemon_text = "No daemon is chosen yet. Add one under Settings → Connections.";
|
|
7
|
+
// One call to the daemon, its arguments as a JSON list; a refusal throws the daemon's words.
|
|
8
|
+
export async function call_remote(link, path, args) {
|
|
9
|
+
const daemon = link.daemon();
|
|
10
|
+
if (daemon === null)
|
|
11
|
+
throw new Error(no_daemon_text);
|
|
12
|
+
let response;
|
|
13
|
+
try {
|
|
14
|
+
response = await (link.fetch ?? fetch)(`${daemon.address}${path}`, {
|
|
15
|
+
method: "POST",
|
|
16
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${daemon.token}`, "x-client-id": link.client_id() },
|
|
17
|
+
body: JSON.stringify(args),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new Error(`The daemon at ${daemon.address} did not answer.`);
|
|
22
|
+
}
|
|
23
|
+
const text = await response.text();
|
|
24
|
+
if (response.status === 401)
|
|
25
|
+
throw new LoginNeeded("This device's sign-in was refused; add the daemon again under Settings → Connections.");
|
|
26
|
+
if (!response.ok)
|
|
27
|
+
throw new Error(error_text(text) || `The daemon answered ${response.status}.`);
|
|
28
|
+
return text === "" ? undefined : JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
// The words of a refusal, sent as JSON with an error or message, or as plain text.
|
|
31
|
+
function error_text(text) {
|
|
32
|
+
try {
|
|
33
|
+
const parsed = JSON.parse(text);
|
|
34
|
+
return String(parsed.error ?? parsed.message ?? text);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Every call of a table by name, posted under its path, such as "/api/runner/"; the daemon refuses names it lacks.
|
|
41
|
+
export function remote_table(link, prefix) {
|
|
42
|
+
return new Proxy({}, {
|
|
43
|
+
get: (_target, name) => (typeof name === "string" && name !== "then" ? (...args) => call_remote(link, `${prefix}${name}`, args) : undefined),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// One server-sent event's name and data, or null for a comment or keep-alive.
|
|
47
|
+
export function parse_remote_event(block) {
|
|
48
|
+
let event = "message";
|
|
49
|
+
const data = [];
|
|
50
|
+
for (const line of block.split("\n")) {
|
|
51
|
+
if (line.startsWith("event:"))
|
|
52
|
+
event = line.slice(6).trim();
|
|
53
|
+
else if (line.startsWith("data:"))
|
|
54
|
+
data.push(line.slice(5).replace(/^ /, ""));
|
|
55
|
+
}
|
|
56
|
+
return data.length ? { event, data: data.join("\n") } : null;
|
|
57
|
+
}
|
|
58
|
+
// Follows the daemon's change events with the token, reconnecting whenever the stream ends; returns how to stop.
|
|
59
|
+
export function follow_remote(link, on_event, options = {}) {
|
|
60
|
+
const retry_ms = options.retry_ms ?? 5000;
|
|
61
|
+
let stopped = false;
|
|
62
|
+
let retry;
|
|
63
|
+
let stream = null;
|
|
64
|
+
async function read() {
|
|
65
|
+
const daemon = link.daemon();
|
|
66
|
+
if (daemon === null)
|
|
67
|
+
return;
|
|
68
|
+
stream = new AbortController();
|
|
69
|
+
const response = await (link.fetch ?? fetch)(`${daemon.address}/api/events`, { headers: { accept: "text/event-stream", authorization: `Bearer ${daemon.token}` }, signal: stream.signal });
|
|
70
|
+
if (!response.ok || !response.body)
|
|
71
|
+
return void (await response.body?.cancel());
|
|
72
|
+
options.on_connect?.();
|
|
73
|
+
const reader = response.body.getReader();
|
|
74
|
+
const decoder = new TextDecoder();
|
|
75
|
+
let buffer = "";
|
|
76
|
+
for (;;) {
|
|
77
|
+
const { value, done } = await reader.read();
|
|
78
|
+
if (done)
|
|
79
|
+
return;
|
|
80
|
+
buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n");
|
|
81
|
+
let cut;
|
|
82
|
+
while ((cut = buffer.indexOf("\n\n")) >= 0) {
|
|
83
|
+
const parsed = parse_remote_event(buffer.slice(0, cut));
|
|
84
|
+
buffer = buffer.slice(cut + 2);
|
|
85
|
+
if (parsed)
|
|
86
|
+
on_event(parsed.event, parsed.data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function open() {
|
|
91
|
+
void read()
|
|
92
|
+
.catch(() => undefined)
|
|
93
|
+
.finally(() => {
|
|
94
|
+
stream = null;
|
|
95
|
+
if (!stopped)
|
|
96
|
+
retry = setTimeout(open, retry_ms);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
open();
|
|
100
|
+
return () => {
|
|
101
|
+
stopped = true;
|
|
102
|
+
clearTimeout(retry);
|
|
103
|
+
stream?.abort();
|
|
104
|
+
};
|
|
105
|
+
}
|
package/dist/web/theme.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface ThemeChoice {
|
|
|
4
4
|
set(theme: Theme): void;
|
|
5
5
|
on_change(listener: (theme: Theme) => void): () => void;
|
|
6
6
|
apply(theme?: Theme): void;
|
|
7
|
+
readonly pinned?: boolean;
|
|
7
8
|
}
|
|
8
9
|
export interface ThemeStorage {
|
|
9
10
|
read(): Theme | undefined;
|
|
@@ -13,3 +14,10 @@ export declare function local_theme_storage(storage_key: string): ThemeStorage;
|
|
|
13
14
|
export declare function theme_choice(where: string | ThemeStorage): ThemeChoice;
|
|
14
15
|
export declare function next_theme(theme: Theme): Theme;
|
|
15
16
|
export declare function follow_host_theme(): boolean;
|
|
17
|
+
export interface ServerTheme {
|
|
18
|
+
scheme: Theme | null;
|
|
19
|
+
colors: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
export declare function read_server_theme(value: unknown): ServerTheme;
|
|
22
|
+
export declare function pinned_theme_choice(theme: Theme): ThemeChoice;
|
|
23
|
+
export declare function apply_theme_colors(colors: Readonly<Record<string, string>>, root?: HTMLElement): void;
|
package/dist/web/theme.js
CHANGED
|
@@ -56,3 +56,26 @@ export function follow_host_theme() {
|
|
|
56
56
|
document.documentElement.classList.toggle("follows-host-theme", is_hosted);
|
|
57
57
|
return is_hosted;
|
|
58
58
|
}
|
|
59
|
+
const token_name = /^[a-z][a-z0-9-]*$/;
|
|
60
|
+
// The config's theme block as a server theme, leaving out what is not a scheme, a token name or a colour.
|
|
61
|
+
export function read_server_theme(value) {
|
|
62
|
+
const block = typeof value === "object" && value !== null ? value : {};
|
|
63
|
+
const scheme = block.scheme === "light" || block.scheme === "dark" || block.scheme === "system" ? block.scheme : null;
|
|
64
|
+
const colors = {};
|
|
65
|
+
if (typeof block.colors === "object" && block.colors !== null) {
|
|
66
|
+
for (const [name, color] of Object.entries(block.colors)) {
|
|
67
|
+
if (token_name.test(name) && typeof color === "string" && /^[#a-zA-Z0-9(),.%\s-]+$/.test(color))
|
|
68
|
+
colors[name] = color.trim();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { scheme, colors };
|
|
72
|
+
}
|
|
73
|
+
// The scheme the server pins, which a pick does not change, so the page shows that scheme alone and no theme switch.
|
|
74
|
+
export function pinned_theme_choice(theme) {
|
|
75
|
+
return { ...theme_choice({ read: () => (theme === "system" ? undefined : theme), write: () => undefined }), pinned: true };
|
|
76
|
+
}
|
|
77
|
+
// Sets the server's colours on the page's root, where they win over both schemes' tokens.
|
|
78
|
+
export function apply_theme_colors(colors, root = document.documentElement) {
|
|
79
|
+
for (const [name, color] of Object.entries(colors))
|
|
80
|
+
root.style.setProperty(`--${name}`, color);
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.122.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|