Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pm-stack/console/edge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ runs the fleet's tools can be reached.

## What runs where

| Thing | Where | When the laptop cannot be reached |
| -------------------------------------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The app's page, script and icons | edge | unchanged |
| Checking who is calling | edge, and again on the laptop | the edge still answers the owner; anyone it is unsure of gets nothing |
| State (in progress, next, needs me, prompts, pull…) | edge, from the laptop's report | the board, pull requests, ranked issues and prompts refresh from GitHub and the queue store after one minute of silence; the rest stays as of the last report |
| Notifications: keys, subscriptions, sending | edge | unchanged; and one says the laptop has gone quiet |
| Linked phones and the widget's summary | edge | unchanged |
| Actions safe to do late (prompts, answers, comments, …) | laptop, relayed at once | held at the edge, run once when it is back, dropped after a day and said so |
| Direct actions (reply to or close a prompt, comment, rank an issue, move a task) | edge, once the laptop has been quiet for a minute | run at the edge through the laptop's own classes, and never sent to the laptop, so they are not done twice; a new prompt is filed at the edge and only its queue task is held for the laptop |
| Actions not safe to do late (merge, units, unblock, …) | laptop, relayed at once | refused, saying why |
| The dashboard's own pages, logs, one pull request's page | laptop | a plain notice with a link to the app |
| The signed Android build (`/console/pm-console.apk`) | laptop, and a copy at the edge | the copy is served, and the app says so when the copy is older than the laptop's newest |
| Thing | Where | When the laptop cannot be reached |
| -------------------------------------------------------------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The app's page, script and icons | edge | unchanged |
| Checking who is calling | edge, and again on the laptop | the edge still answers the owner; anyone it is unsure of gets nothing |
| State (in progress, next, needs me, prompts, pull…) | edge, from the laptop's report | the board, pull requests, ranked issues and prompts refresh from GitHub and the queue store after one minute of silence; the rest stays as of the last report |
| Notifications: keys, subscriptions, sending | edge | unchanged; and one says the laptop has gone quiet |
| Linked phones and the widget's summary | edge | unchanged |
| Actions safe to do late (prompts, answers, comments, …) | laptop, relayed at once | held at the edge, run once when it is back, dropped after a day and said so |
| Direct actions (reply to or close a prompt, comment, rank an issue, move a task) | edge, once the laptop has been quiet for a minute | run at the edge through the laptop's own classes, and never sent to the laptop, so they are not done twice; a new prompt is filed at the edge and only its queue task is held for the laptop |
| Actions not safe to do late (merge, units, unblock, …) | laptop, relayed at once | refused, saying why |
| The dashboard's own pages, logs, one pull request's page | laptop | a plain notice with a link to the app |
| The signed Android build (`/console/pm-console.apk`) | GitHub's release first; else laptop, and a copy at the edge | the release is served (checked against its own description of itself); with none published, the copy is, and the app says so when the copy is older than the laptop's newest |

The list of actions that may be done late is `HOLDABLE` in
`../src/edge-proto.ts`.
Expand Down Expand Up @@ -47,6 +47,14 @@ laptop: pm-console (../src/server.ts), reporting through ../src/sync.ts
has. So a mistake at the edge can cost the edge's speed, never the
owner's way in, and a stranger gets from the laptop exactly what a
stranger got before.
- **The app's own files are not downloaded twice.** The platform's file
store tags each file by its content; the Worker passes the tag on and
answers a browser that names it with "still that" (304) and nothing
else. They are still asked for every time (`no-cache`), so a deploy
shows at once. `/console/health` says, to anyone, which release of the
Android app this side knows of (`androidRelease`), beside the copy of
the laptop's build (`androidBuild`): that is how either arriving here
is checked from outside.
- **The laptop reports** to `POST /console/edge/sync`: the sections of
state that changed, how current each unchanged one is, and what became
of anything held for it. The answer says whether anyone is looking, and
Expand Down
19 changes: 14 additions & 5 deletions pm-stack/console/edge/src/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ export class Edge {
return this.released.found;
}

/** What is said of the released build: its name, its code and when it
* was built. Null when none is published, or before the host has said
* which repository this is. */
private async releaseSaid(cfg: EdgeConfig | null): Promise<{ name: string; code: number; built: string } | null> {
const release = cfg ? await this.androidRelease(cfg) : null;
return release && { name: release.name, code: release.code, built: release.built };
}

/** The released build, as a download, or null if it did not arrive as
* its description says (GitHub out of reach, or the description read
* while a new build was being put up). */
Expand Down Expand Up @@ -946,6 +954,11 @@ export class Edge {
// The copy of the Android build kept here: when it was built,
// and whether it is the host's newest. Null if there is none.
androidBuild: await this.apkState(),
// The newest build of the app GitHub has released, as last read
// here (within ten minutes): what a download is, when there is
// one. The release is public; this is how its arrival here is
// checked from outside. Null when none is published.
androidRelease: await this.releaseSaid(cfg),
// Since this object last started: requests it answered as the
// owner's, and requests it left to the host to judge.
answered: this.sure,
Expand Down Expand Up @@ -1047,11 +1060,7 @@ export class Edge {
if (seg.length === 1 && seg[0] === "android") {
// Where a download would come from: the build GitHub released,
// if there is one, and the copy of the laptop's build kept here.
const release = await this.androidRelease(cfg);
return json({
release: release && { name: release.name, code: release.code, built: release.built },
copy: await this.apkState(),
});
return json({ release: await this.releaseSaid(cfg), copy: await this.apkState() });
}
if (seg.length === 1 && seg[0] === "push") return json(await this.push.status());
if (seg.length === 1 && seg[0] === "devices") return json(await this.phones.devices.list());
Expand Down
20 changes: 16 additions & 4 deletions pm-stack/console/edge/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// file of the app, fetch the host's key, or pass the request to the host
// behind the tunnel. If the host cannot be reached, an action that may
// be done late is handed back to the state to hold.
import { SECURITY } from "../../src/http.ts";
import { SECURITY, sameEntity } from "../../src/http.ts";

export interface Io {
/** The object with the state (edge.ts). */
Expand Down Expand Up @@ -134,9 +134,21 @@ export async function route(request: Request, io: Io): Promise<Response> {
if (what === "asset") {
const file = await io.asset(String(detail.path));
if (!file.ok) return new Response("not found", { status: 404, headers: SECURITY });
return new Response(request.method === "HEAD" ? null : file.body, {
headers: { "Content-Type": String(detail.type), "Cache-Control": "no-cache", ...SECURITY },
});
// "no-cache" is "ask before using your copy". The file's tag is what
// the browser asks with; when its copy is still the file, the answer
// is that and nothing else, so a visit does not download the app again.
const headers: Record<string, string> = {
"Content-Type": String(detail.type),
"Cache-Control": "no-cache",
...SECURITY,
};
const etag = file.headers.get("etag");
if (etag) headers.ETag = etag;
if (sameEntity(request.headers.get("if-none-match"), etag)) {
await file.body?.cancel();
return new Response(null, { status: 304, headers });
}
return new Response(request.method === "HEAD" ? null : file.body, { headers });
}

// Everything below is for the host. Away from the routed hostname
Expand Down
19 changes: 18 additions & 1 deletion pm-stack/console/public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
--quiet: #5b6777;
--accent: #0b63ce;
--accent-ink: #ffffff;
/* The status colours above are made for a dark page: on a light one
they are too faint to read (2.2 to 3.4 to 1). These are at least
5 to 1 on every light surface here. */
--good: #18753a;
--warn: #8a5a00;
--bad: #c5222c;
}
}
* {
Expand All @@ -42,8 +48,17 @@ body {
padding: calc(var(--top-h) + env(safe-area-inset-top)) 0 calc(var(--tab-h) + env(safe-area-inset-bottom) + 12px);
-webkit-tap-highlight-color: transparent;
}
/* A link in running text is underlined: its colour alone is too close
to the text's to tell it by. Links drawn as rows or buttons say what
they are by their shape. */
a {
color: var(--accent);
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 2px;
}
a.link,
a.button {
text-decoration: none;
}
[hidden] {
Expand Down Expand Up @@ -133,7 +148,9 @@ main {
min-width: 18px;
padding: 0 5px;
border-radius: 9px;
background: var(--bad);
/* White on the theme's own red is too faint in either theme; this
red carries white at 5.4 to 1. */
background: #cf222e;
color: #fff;
font-size: 0.68rem;
font-weight: 700;
Expand Down
15 changes: 15 additions & 0 deletions pm-stack/console/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,19 @@ function go(to) {
else location.hash = to;
}

/** A card's title is a third-level heading, under a section's
* second-level one. On a screen with no section above it, it comes
* straight after the screen's title: it is then said to be second-level,
* so that someone listening does not hear a level skipped. It looks the
* same. `headings` are the screen's h2 and h3 elements, in order. */
export function levelHeadings(headings) {
let sectioned = false;
for (const el of headings) {
if (el.tagName === "H2") sectioned = true;
else if (!sectioned) el.setAttribute("aria-level", "2");
}
}

function drawTabs() {
const n = needCounts();
const fresh = (docs.core?.asks || []).filter((a) => a.status === "answered").length;
Expand Down Expand Up @@ -1688,13 +1701,15 @@ async function draw() {
const nodes = await render(sub);
if (seq !== drawSeq) return;
view.replaceChildren(...nodes.filter(Boolean));
levelHeadings(view.querySelectorAll("h2, h3"));
if (moved) scrollTo(0, 0);
drawn = route;
} catch (error) {
if (seq !== drawSeq) return;
view.replaceChildren(
card(h("h3", {}, "This section could not load"), h("p", { class: "why" }, error.message), h("div", { class: "row end" }, h("button", { onclick: () => (signedOut ? location.reload() : draw()) }, signedOut ? "Reload" : "Try again"))),
);
levelHeadings(view.querySelectorAll("h2, h3"));
if (moved) scrollTo(0, 0);
drawn = route;
} finally {
Expand Down
1 change: 1 addition & 0 deletions pm-stack/console/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<title>PM</title>
<link rel="manifest" href="/manifest.webmanifest" />
<link rel="icon" href="/console/icon.svg" />
<link rel="apple-touch-icon" href="/icons/apple-touch-icon.png" />
<link rel="stylesheet" href="/console/app.css" />
</head>
<body>
Expand Down
5 changes: 4 additions & 1 deletion pm-stack/console/scripts/edge-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ const io: Io = {
asset: async (path) => {
try {
const name = path.startsWith("/console/") ? path.slice("/console/".length) : "";
return name ? new Response(readFileSync(join(publicDir, name))) : new Response("", { status: 404 });
if (!name) return new Response("", { status: 404 });
// The platform's file store tags each file by its content; so does this.
const bytes = readFileSync(join(publicDir, name));
return new Response(bytes, { headers: { ETag: `"${Bun.hash(bytes).toString(16)}"` } });
} catch {
return new Response("", { status: 404 });
}
Expand Down
26 changes: 26 additions & 0 deletions pm-stack/console/scripts/edge-smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ try {
icon.status === 200 && icon.headers.get("content-type") === "image/png" && bytes[1] === 0x50,
`${bytes.length} bytes`,
);
// The platform's file store tags each file; the Worker has to pass the
// tag on and answer a browser that names it with "still that", or
// every visit downloads the app again. Only this runtime has the store.
const tag = icon.headers.get("etag");
const again = tag ? await get("/icons/icon-192.png", { "If-None-Match": tag }) : null;
const other = await get("/icons/icon-192.png", { "If-None-Match": '"some-other-version"' });
check(
"a file says which version of itself it is, and a browser that has it is sent nothing",
Boolean(tag) &&
again?.status === 304 &&
(await again.arrayBuffer()).byteLength === 0 &&
other.status === 200 &&
(await other.arrayBuffer()).byteLength === bytes.byteLength,
`tag ${tag}, with it ${again?.status}, with another ${other.status}`,
);

await docs.touch(Object.keys(data));
const first = await link.once();
Expand Down Expand Up @@ -204,6 +219,17 @@ try {
);
const still = (await (await get("/console/health")).json()) as Record<string, unknown>;
check("the edge itself is still up", still.ok === true && Number(still.deferred) >= 3, JSON.stringify(still));
// The Android app's release is read from GitHub itself, in this
// runtime, over the real network. There may be none, and GitHub may
// not answer a runner: either is said as null, and neither is a
// failure of this Worker. A release that is there must read as one.
const release = still.androidRelease as { name?: unknown; code?: unknown; built?: unknown } | null | undefined;
check(
"health says which release of the Android app it knows of, or that it knows of none",
release === null ||
(typeof release?.name === "string" && Number.isInteger(release.code) && typeof release.built === "string"),
JSON.stringify(release),
);
} catch (error) {
failed++;
console.log(`FAIL the run stopped: ${(error as Error).message}`);
Expand Down
18 changes: 18 additions & 0 deletions pm-stack/console/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const MANIFEST = {
name: "PM",
short_name: "PM",
description: "What the fleet is doing, what is next, what needs you, and a place to say what you want.",
// What an installed copy is known by. It is the address the app opens
// at, said outright, so that it stays one app if that address moves.
id: "/console/",
start_url: "/console/",
scope: "/",
display: "standalone",
Expand All @@ -34,8 +37,23 @@ export const SECURITY = {
"default-src 'self'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'none'; form-action 'self'",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "same-origin",
// This site is only ever HTTPS: a browser that has been here does not
// try plain HTTP again for a year. (Over plain HTTP, as on the host's
// own loopback port, browsers ignore it.)
"Strict-Transport-Security": "max-age=31536000",
};

/** Whether a browser's copy of a file is still the file: its
* If-None-Match names this entity tag. The comparison is the weak one
* of RFC 9110 (a W/ prefix on either side does not matter), which is
* the one a conditional GET uses. */
export function sameEntity(ifNoneMatch: string | null, etag: string | null): boolean {
if (!ifNoneMatch || !etag) return false;
const bare = (tag: string) => tag.trim().replace(/^W\//, "");
const mine = bare(etag);
return ifNoneMatch.split(",").some((tag) => tag.trim() === "*" || bare(tag) === mine);
}

export function json(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
Expand Down
Loading