Files
plugins/packages/amp-usage/page/boot.js
T
ming_jiang 4b0aef5327 amp-usage: Amp usage page with background collection; day-card 0.3.1 (#1000)
Adds packages/amp-usage (@magpie-community/page-amp-usage 0.1.0). It is a magpie page that reads Amp's local thread files and the gateway's usage.jsonl and shows per-day usage. The service listens on loopback only and requires a per-start service token on every API call. The page that carries the token is served only to a navigation (Sec-Fetch-Mode). The gateway log is read incrementally from a saved offset, whole lines only, and is re-indexed on rotation. The UI words are in ui/words.js (en, zh).

Also bumps packages/day-card to 0.3.1.

Round 3 (13c1f33) adds three things:
- a test that bytes before the gateway-log offset are never re-read;
- ui.test.mjs, which checks that every error code the server can return has words in each language, and that an unknown code falls back;
- words.js, moved out of app.js, served at /words.js and included in the pack.

What ran, at 13c1f33 merged with main 14187408, on mica:
- amp-usage `node --test`: 47/47.
- `bun test packages/amp-usage packages/day-card`: 65 pass, 0 fail.
- `bun scripts/check.mjs`, all packages: ok.
- `npm pack --dry-run`: amp-usage 0.1.0 has 19 files including ui/words.js; day-card 0.3.1 has 17 files.
- Root `bun test` with a temp HOME: 8362 pass, 0 fail (8382 tests, 590 files).
- Render smoke in Chromium and WebKit at 1200 and 420px, en and zh: words.js loads, both languages show their own words, no page errors, no horizontal overflow.

Broken by hand, each run with node and bun:
- collector.mjs:102 made to re-read the whole file every scan: the new offset test goes red.
- `const cut = buf.length`, so a partial line is taken: red.
- Raw error codes shown instead of their words (`w.err[code] || code`): red.
- A zh string deleted: red.

Not tried:
- A real Amp account or the real `amp` CLI.
- Changing the auto-refresh default from 300 to 60 fails no test. The author left it untested and round 2 called it optional.

Rounds 1 and 2 also ran the 1M-line log probe (rss steady at about 363MB, scans of 70 to 114ms after the first), the bad-record and paging probes, and the full render matrix including dark mode.
2026-11-11 21:43:59 +08:00

69 lines
2.5 KiB
JavaScript

(async () => {
const zh = (lang) =>
String(lang || navigator.language || "")
.toLowerCase()
.startsWith("zh");
const words = (lang) =>
zh(lang)
? {
title: "Amp 用量",
connecting: "正在连接本地采集服务…",
stopped: "采集服务没有启动。请查看插件日志,并确认端口没有被占用。",
port: "插件选项中的服务端口无效。",
remote: "请更新 Magpie,才能从远程浏览器打开这个服务。",
retry: "重新连接",
}
: {
title: "Amp usage",
connecting: "Connecting to the local collector…",
stopped:
"The collector did not start. Check the plugin log and whether its port is already in use.",
port: "Invalid service port in plugin options.",
remote: "Update Magpie to open this service from a remote browser.",
retry: "Reconnect",
};
const message = document.getElementById("message");
const retry = document.getElementById("retry");
retry.addEventListener("click", () => location.reload());
let text = words();
const say = (key) => {
document.title = text.title;
document.getElementById("title").textContent = text.title;
retry.textContent = text.retry;
message.textContent = text[key];
};
say("connecting");
try {
const theme = await magpie.ready;
text = words(theme.lang);
say("connecting");
const options = await magpie.options.get();
const port = Number(options?.port ?? 3497);
if (!Number.isInteger(port) || port < 1 || port > 65535)
throw new Error("port");
const target =
magpie.service ||
(["localhost", "127.0.0.1", ""].includes(location.hostname)
? `http://127.0.0.1:${port}/`
: "");
if (!target) throw new Error("remote");
const url = new URL(target);
url.searchParams.set("theme", theme.dark ? "dark" : "light");
url.searchParams.set("lang", zh(theme.lang) ? "zh" : "en");
// The service's page wears the window's own colours.
const tokens = {};
for (const [k, v] of Object.entries(theme.tokens || {}))
if (/^[\x20-\x7e]{1,160}$/.test(String(v))) tokens[k] = String(v);
if (Object.keys(tokens).length)
url.searchParams.set("tokens", btoa(JSON.stringify(tokens)));
setTimeout(() => {
say("stopped");
retry.hidden = false;
}, 6000);
location.replace(url.href);
} catch (e) {
say(text[e.message] ? e.message : "stopped");
retry.hidden = false;
}
})();