@bom0792/herdgate 0.9.39 → 0.9.41
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 +1 -1
- package/plugin/server/git-safe.mjs +21 -2
- package/plugin/server/index.mjs +243 -4
- package/plugin/server/pi-wake.mjs +1 -1
- package/plugin/server/spawn-registry.d.mts +23 -0
- package/plugin/server/spawn-registry.mjs +216 -0
- package/scripts/build-release.sh +1 -1
- package/src/cloud/gateway.ts +63 -10
- package/src/cloud/push.ts +97 -2
- package/src/cloud/stores.ts +9 -5
- package/src/web/app.css +89 -0
- package/src/web/app.js +641 -11
- package/src/web/fuzzy.js +63 -0
- package/src/web/push-decision.js +70 -0
- package/src/web/sw.js +45 -8
package/src/web/fuzzy.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* Fuzzy file matcher — the herdr-file-viewer scoring (fuzzy.rs) in web form:
|
|
2
|
+
* case-insensitive subsequence over the whole path, with a basename bonus (a
|
|
3
|
+
* match landing wholly inside the file's own name outranks one that needs
|
|
4
|
+
* directory characters) and shorter-path tie-breaks. Pure, no I/O; loaded by
|
|
5
|
+
* app.js as a module and by Node tests through the globalThis assignment. */
|
|
6
|
+
(function (root) {
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
/** score(path, lowercased query chars) → number | null (null = no match).
|
|
10
|
+
* Lower is better. Basename-only matches beat anything needing directories;
|
|
11
|
+
* then fewer gap jumps; then the shorter path. */
|
|
12
|
+
function score(queryChars, path) {
|
|
13
|
+
const lower = path.toLowerCase();
|
|
14
|
+
const slash = lower.lastIndexOf("/");
|
|
15
|
+
const baseAt = slash + 1; // where the basename starts
|
|
16
|
+
let qi = 0;
|
|
17
|
+
let gaps = 0;
|
|
18
|
+
let prev = -1;
|
|
19
|
+
let inBase = true; // every matched char so far landed inside the basename
|
|
20
|
+
for (let i = 0; i < lower.length && qi < queryChars.length; i++) {
|
|
21
|
+
if (lower[i] !== queryChars[qi]) continue;
|
|
22
|
+
if (i < baseAt) inBase = false; // this hit needed a directory character
|
|
23
|
+
if (prev >= 0 && i > prev + 1) gaps += i - prev - 1; // tighter runs win
|
|
24
|
+
prev = i;
|
|
25
|
+
qi++;
|
|
26
|
+
}
|
|
27
|
+
if (qi < queryChars.length) return null; // ran out of path before query
|
|
28
|
+
// A basename-hit bonus so large it always outranks directory-dependent runs.
|
|
29
|
+
return (inBase ? 0 : 1_000_000) + gaps * 10 + lower.length;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Indices of `candidates` matching `query`, best-first (stable on ties). */
|
|
33
|
+
function matchAndRank(query, candidates) {
|
|
34
|
+
if (!query) return [];
|
|
35
|
+
const qc = query.toLowerCase().split("");
|
|
36
|
+
const scored = [];
|
|
37
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
38
|
+
const s = score(qc, candidates[i]);
|
|
39
|
+
if (s !== null) scored.push([i, s]);
|
|
40
|
+
}
|
|
41
|
+
scored.sort((a, b) => a[1] - b[1] || a[0] - b[0]); // stable: original order on ties
|
|
42
|
+
return scored.map((x) => x[0]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Smartcase in-file search (search.rs): literal substring, never a regex;
|
|
46
|
+
* a query with ANY uppercase matches case-sensitively, all-lowercase matches
|
|
47
|
+
* anywhere. Returns match [start, end) offsets per line, in order. */
|
|
48
|
+
function searchLine(line, query) {
|
|
49
|
+
if (!query) return [];
|
|
50
|
+
const hay = /[A-Z]/.test(query) ? line : line.toLowerCase();
|
|
51
|
+
const needle = /[A-Z]/.test(query) ? query : query.toLowerCase();
|
|
52
|
+
const hits = [];
|
|
53
|
+
let at = 0;
|
|
54
|
+
for (;;) {
|
|
55
|
+
const i = hay.indexOf(needle, at);
|
|
56
|
+
if (i === -1) return hits;
|
|
57
|
+
hits.push([i, i + query.length]);
|
|
58
|
+
at = i + query.length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
root.hgFuzzy = { matchAndRank, searchLine };
|
|
63
|
+
})(typeof self !== "undefined" ? self : globalThis);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/* Pure decision logic for the service worker's push handler, in its own file so it
|
|
2
|
+
* is testable without service-worker globals (sw.js is a classic script and pulls
|
|
3
|
+
* this in with importScripts; Node tests import the file and read globalThis).
|
|
4
|
+
*
|
|
5
|
+
* The gate sends one small encrypted payload per push (RFC 8291; the push provider
|
|
6
|
+
* only ever sees ciphertext):
|
|
7
|
+
* { v: 1, kind: "settle" | "died" | "needs-you" | "activity", agent?, room }
|
|
8
|
+
* and a retraction when the news resolves before the phone looks:
|
|
9
|
+
* { type: "clear", tag }
|
|
10
|
+
*
|
|
11
|
+
* Everything DECIDED here — suppress vs show vs clear, the notification slot tag,
|
|
12
|
+
* the Vietnamese copy — is plain data in, plain data out. The SW keeps only glue:
|
|
13
|
+
* parse the event, ask clients for visibility, perform what this returns. */
|
|
14
|
+
(function (root) {
|
|
15
|
+
"use strict";
|
|
16
|
+
|
|
17
|
+
var KINDS = { settle: true, died: true, "needs-you": true, activity: true };
|
|
18
|
+
|
|
19
|
+
function isPayload(m) {
|
|
20
|
+
return !!(m && typeof m === "object" && m.v === 1 && KINDS[m.kind] &&
|
|
21
|
+
typeof m.room === "string" && m.room.length <= 64);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** One notification slot per room so a busy fleet coalesces instead of stacking. */
|
|
25
|
+
function tagFor(room) { return "hg:" + room; }
|
|
26
|
+
|
|
27
|
+
/** The copy shown for a payload. Kept here (not in the SW) so it is testable. */
|
|
28
|
+
function copyFor(p) {
|
|
29
|
+
var who = typeof p.agent === "string" && p.agent ? p.agent : "Agent";
|
|
30
|
+
switch (p.kind) {
|
|
31
|
+
case "settle": return { title: "Agent đã xong lượt", body: who + " đã settle — chạm để xem kết quả." };
|
|
32
|
+
case "died": return { title: "Agent đã chết", body: who + " không sống sót — chạm để xem." };
|
|
33
|
+
case "needs-you": return { title: "Agent cần bạn", body: who + " đang chờ quyết định của bạn." };
|
|
34
|
+
default: return { title: "Có hoạt động mới", body: "Máy của bạn có thay đổi — chạm để mở." };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decide what the SW should do with a push event.
|
|
40
|
+
* data — event.data ? event.data.json() : null (null = legacy silent ping)
|
|
41
|
+
* hasVisibleClient — a PWA window is open and visible (in-app UI already shows it)
|
|
42
|
+
* Returns one of:
|
|
43
|
+
* { kind: "legacy" } silent ping: wake windows or notify generically
|
|
44
|
+
* { kind: "clear", tag } retraction: close the slot even if a client is visible
|
|
45
|
+
* { kind: "suppress" } visible client already surfaces the news
|
|
46
|
+
* { kind: "show", title, body, tag, room, agent, renotify, urgent }
|
|
47
|
+
*/
|
|
48
|
+
function decidePush(data, hasVisibleClient) {
|
|
49
|
+
if (data && data.type === "clear" && typeof data.tag === "string" && data.tag.length <= 80) {
|
|
50
|
+
return { kind: "clear", tag: data.tag };
|
|
51
|
+
}
|
|
52
|
+
if (!isPayload(data)) return { kind: "legacy" };
|
|
53
|
+
if (hasVisibleClient) return { kind: "suppress" };
|
|
54
|
+
var copy = copyFor(data);
|
|
55
|
+
return {
|
|
56
|
+
kind: "show",
|
|
57
|
+
title: copy.title,
|
|
58
|
+
body: copy.body,
|
|
59
|
+
tag: data.tag || tagFor(data.room),
|
|
60
|
+
room: data.room,
|
|
61
|
+
agent: typeof data.agent === "string" ? data.agent : undefined,
|
|
62
|
+
renotify: true,
|
|
63
|
+
urgent: data.kind === "needs-you" || data.kind === "died",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
root.hgDecidePush = decidePush;
|
|
68
|
+
root.hgTagFor = tagFor;
|
|
69
|
+
root.hgCopyFor = copyFor;
|
|
70
|
+
})(typeof self !== "undefined" ? self : globalThis);
|
package/src/web/sw.js
CHANGED
|
@@ -3,8 +3,12 @@
|
|
|
3
3
|
//
|
|
4
4
|
// Bump VERSION when a shell file changes; old caches are dropped on activate.
|
|
5
5
|
|
|
6
|
-
const VERSION = "herdgate-
|
|
7
|
-
const SHELL = ["/", "/app.css", "/e2e.js", "/app.js", "/app.webmanifest", "/icons/icon.svg"];
|
|
6
|
+
const VERSION = "herdgate-v18";
|
|
7
|
+
const SHELL = ["/", "/app.css", "/e2e.js", "/app.js", "/app.webmanifest", "/icons/icon.svg", "/push-decision.js"];
|
|
8
|
+
|
|
9
|
+
// The push decision table lives in its own plain file so Node tests can load it too
|
|
10
|
+
// (importScripts works in a classic worker; the file assigns onto `self`).
|
|
11
|
+
importScripts("/push-decision.js");
|
|
8
12
|
|
|
9
13
|
self.addEventListener("install", (event) => {
|
|
10
14
|
event.waitUntil(
|
|
@@ -27,17 +31,42 @@ self.addEventListener("activate", (event) => {
|
|
|
27
31
|
);
|
|
28
32
|
});
|
|
29
33
|
|
|
30
|
-
// Push wake: the
|
|
31
|
-
//
|
|
32
|
-
//
|
|
34
|
+
// Push wake: the gate sends either a silent payload-less ping (legacy / keyless
|
|
35
|
+
// subscriptions) or an encrypted payload (RFC 8291) saying WHAT changed — settle,
|
|
36
|
+
// died, needs-you or plain activity. The decision table is in push-decision.js:
|
|
37
|
+
// a visible client suppresses (the in-app UI already surfaces it), a retraction
|
|
38
|
+
// closes the slot regardless, otherwise one notification per room (tag coalesce).
|
|
33
39
|
self.addEventListener("push", (event) => {
|
|
34
40
|
event.waitUntil(
|
|
35
41
|
(async () => {
|
|
42
|
+
let data = null;
|
|
43
|
+
try {
|
|
44
|
+
data = event.data ? event.data.json() : null;
|
|
45
|
+
} catch {
|
|
46
|
+
data = null; // not JSON — treat as a legacy silent ping
|
|
47
|
+
}
|
|
36
48
|
const all = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
37
|
-
|
|
49
|
+
const hasVisibleClient = all.some((c) => c.visibilityState === "visible");
|
|
50
|
+
const d = self.hgDecidePush(data, hasVisibleClient);
|
|
51
|
+
if (d.kind === "clear") {
|
|
52
|
+
for (const n of await self.registration.getNotifications({ tag: d.tag })) n.close();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (d.kind === "suppress" || d.kind === "legacy") {
|
|
38
56
|
for (const client of all) client.postMessage({ t: "wake" });
|
|
57
|
+
if (d.kind === "suppress" || all.length > 0) return;
|
|
58
|
+
// legacy with no window at all → the generic notification below
|
|
59
|
+
}
|
|
60
|
+
if (d.kind === "show") {
|
|
61
|
+
await self.registration.showNotification(d.title, {
|
|
62
|
+
body: d.body,
|
|
63
|
+
tag: d.tag,
|
|
64
|
+
renotify: d.renotify,
|
|
65
|
+
data: { room: d.room, agent: d.agent },
|
|
66
|
+
});
|
|
39
67
|
return;
|
|
40
68
|
}
|
|
69
|
+
// Legacy silent ping with no window: generic notification (old behaviour).
|
|
41
70
|
await self.registration.showNotification("herdgate", {
|
|
42
71
|
body: "Có hoạt động mới trên máy của bạn — chạm để mở.",
|
|
43
72
|
tag: "herdgate-wake",
|
|
@@ -51,9 +80,17 @@ self.addEventListener("notificationclick", (event) => {
|
|
|
51
80
|
event.notification.close();
|
|
52
81
|
event.waitUntil(
|
|
53
82
|
(async () => {
|
|
83
|
+
const room = event.notification.data && event.notification.data.room;
|
|
54
84
|
const all = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
55
|
-
|
|
56
|
-
|
|
85
|
+
// An open window navigates in place (no reload — pairing and drafts survive);
|
|
86
|
+
// with none open, land on the deep link `/#room=<id>` and let the app route it.
|
|
87
|
+
if (all.length > 0) {
|
|
88
|
+
const win = all.find((c) => "focus" in c) ?? all[0];
|
|
89
|
+
await win.focus();
|
|
90
|
+
if (room) win.postMessage({ t: "nav", room });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
return self.clients.openWindow(room ? "/#room=" + encodeURIComponent(room) : "/");
|
|
57
94
|
})(),
|
|
58
95
|
);
|
|
59
96
|
});
|