Public: anyone can read this; the crew can change it.
The clock, a deal page and the client timeline each answer in under 50 ms with 200 deals, and a test fails when they stop #53
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/39-the-clock-a-deal-page-and-the-client-tim → main 3 commits fork
12 files
changed +511
−45
README.md +1
| @@ -144,6 +144,7 @@ does not look right is ignored in favour of the sample, with a line in the log. | ||
| 144 | 144 | - **Real:** the dates and their math, mark done, moves with their reasons, |
| 145 | 145 | who owes what, the reminder drafts and editing them, the record of what was |
| 146 | 146 | sent, the reasons for messages you chose not to send, the calendar file, and adding parties, dates and deals. |
| 147 | - **Real:** the clock, a deal page and the client timeline are tested to answer in under 50 ms with 200 deals. | |
| 147 | 148 | - **Simulated:** sending. Send records that a reminder went out and keeps the |
| 148 | 149 | words that were approved; no email or text message leaves the app. |
| 149 | 150 | - **Not in this prototype (phase two):** real email or text messages, |
src/cache.test.ts added +148
| @@ -0,0 +1,148 @@ | ||
| 1 | /** What is remembered between requests never outlives a change or crosses from one person to another. */ | |
| 2 | import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { memoizer } from "./memo.ts"; | |
| 7 | import { forgetRationing } from "./security.ts"; | |
| 8 | import { serve } from "./server.ts"; | |
| 9 | import { sign } from "./who.ts"; | |
| 10 | ||
| 11 | const KEY = "a-key-only-the-forge-holds"; | |
| 12 | let server: ReturnType<typeof serve>; | |
| 13 | let base = ""; | |
| 14 | afterAll(() => server.stop(true)); | |
| 15 | beforeEach(forgetRationing); | |
| 16 | ||
| 17 | const as = (id: string, name: string, role: "user" | "admin" = "user"): Record<string, string> => { | |
| 18 | const identity = `${id}|${name}|${role}|${Date.now() + 60_000}`; | |
| 19 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 20 | }; | |
| 21 | const owner = as("u1", "alice"); | |
| 22 | const other = as("u2", "bob"); | |
| 23 | const admin = as("u0", "root", "admin"); | |
| 24 | const anon: Record<string, string> = {}; | |
| 25 | ||
| 26 | const get = async (path: string, who: Record<string, string>) => { | |
| 27 | const res = await fetch(base + path, { headers: who }); | |
| 28 | return { status: res.status, body: await res.text() }; | |
| 29 | }; | |
| 30 | const post = (path: string, fields: Record<string, string>, who = owner) => { | |
| 31 | const body = new FormData(); | |
| 32 | for (const [k, v] of Object.entries(fields)) body.set(k, v); | |
| 33 | return fetch(base + path, { method: "POST", body, redirect: "manual", headers: who }); | |
| 34 | }; | |
| 35 | ||
| 36 | let deal: { id: number; deadlines: { id: number; kind: string }[] }; | |
| 37 | let closing: { id: number }; | |
| 38 | /** Whether the list says this deal is closed. */ | |
| 39 | const isClosed = (list: string): boolean => | |
| 40 | (JSON.parse(list) as { id: number; closed: boolean }[]).find((d) => d.id === deal.id)!.closed; | |
| 41 | ||
| 42 | // Started when this file's tests start, not when the file is loaded: the | |
| 43 | // writes would otherwise reach other files' event streams mid-test. | |
| 44 | beforeAll(async () => { | |
| 45 | server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-cache-")), identityKey: KEY }); | |
| 46 | base = `http://${server.hostname}:${server.port}`; | |
| 47 | const made = await fetch(base + "/api/deals", { | |
| 48 | method: "POST", | |
| 49 | headers: { ...owner, "content-type": "application/json" }, | |
| 50 | body: JSON.stringify({ address: "9 Memo Mews", buyer: "Bea Buyer", seller: "Sol Seller", contract_date: "2027-03-01", closing_date: "2027-04-05" }), | |
| 51 | }); | |
| 52 | deal = await made.json(); | |
| 53 | closing = deal.deadlines.find((d) => d.kind === "closing")!; | |
| 54 | }); | |
| 55 | ||
| 56 | /** Every surface that shows the deal, read as `who`. */ | |
| 57 | const surfaces = async (who: Record<string, string>) => ({ | |
| 58 | clock: (await get("/", who)).body, | |
| 59 | page: (await get(`/deals/${deal.id}`, who)).body, | |
| 60 | list: (await get("/api/deals", who)).body, | |
| 61 | }); | |
| 62 | ||
| 63 | test("a change shows on the next request of the clock, the deal page and the list, never a stale one", async () => { | |
| 64 | const before = await surfaces(owner); | |
| 65 | // Read twice so every cache holds the page the change must replace. | |
| 66 | expect(await surfaces(owner)).toEqual(before); | |
| 67 | ||
| 68 | expect((await post(`/deadlines/${closing.id}/move`, { due: "2027-04-20", reason: "Lender needs a week", to: "/" })).status).toBe(303); | |
| 69 | const moved = await surfaces(owner); | |
| 70 | expect(moved.clock).toContain("Lender needs a week"); | |
| 71 | expect(moved.page).toContain("Lender needs a week"); | |
| 72 | expect(moved.list).toContain("2027-04-20"); | |
| 73 | expect(moved.list).not.toBe(before.list); | |
| 74 | ||
| 75 | expect((await post("/deals/" + deal.id + "/parties", { role: "lender", name: "Lena Lender", contact: "[email protected]" })).status).toBe(303); | |
| 76 | const added = await surfaces(owner); | |
| 77 | for (const text of Object.values(added)) expect(text).toContain("Lena Lender"); | |
| 78 | expect(before.page).not.toContain("Lena Lender"); | |
| 79 | ||
| 80 | expect((await post(`/deadlines/${closing.id}/done`, { to: "/" })).status).toBe(303); | |
| 81 | const closed = await surfaces(owner); | |
| 82 | expect(closed.list).not.toBe(added.list); | |
| 83 | expect(isClosed(closed.list)).toBe(true); | |
| 84 | expect(closed.clock).not.toBe(added.clock); | |
| 85 | expect(closed.page).not.toBe(added.page); | |
| 86 | ||
| 87 | expect((await post(`/deadlines/${closing.id}/done`, { done: "0", to: "/" })).status).toBe(303); | |
| 88 | expect(isClosed((await surfaces(owner)).list)).toBe(false); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a stranger is never handed the clock or sheets drawn for a signed-in user, nor the reverse", async () => { | |
| 92 | // The owner asks first, so what is remembered was drawn for someone who may change the deal. | |
| 93 | const mine = await surfaces(owner); | |
| 94 | expect(mine.clock).toContain(`action="/deadlines/${closing.id}/move"`); | |
| 95 | expect(mine.page).toContain("[email protected]"); | |
| 96 | ||
| 97 | // The table lets any signed-in user change a deal, so another user is drawn the same controls. | |
| 98 | const theirs = await surfaces(other); | |
| 99 | expect(theirs.clock).toContain(`action="/deadlines/${closing.id}/move"`); | |
| 100 | expect(theirs.page).toContain("[email protected]"); | |
| 101 | ||
| 102 | // A stranger sees the welcome, not a signed-in clock someone else rendered. | |
| 103 | const stranger = await get("/", anon); | |
| 104 | expect(stranger.body).not.toContain("Memo Mews"); | |
| 105 | expect(stranger.body).not.toContain(`action="/deadlines/`); | |
| 106 | expect((await get(`/deals/${deal.id}`, anon)).body).not.toContain("[email protected]"); | |
| 107 | expect((await get("/api/deals", anon)).status).toBe(401); | |
| 108 | ||
| 109 | // Signed in as each of them again: what the stranger was shown left nothing behind. | |
| 110 | expect((await surfaces(admin)).page).toContain("[email protected]"); | |
| 111 | expect((await surfaces(owner)).clock).toBe(mine.clock); | |
| 112 | }); | |
| 113 | ||
| 114 | test("the memoizer makes a value once per key and drops the oldest past its limit", () => { | |
| 115 | const make = memoizer<number>(2); | |
| 116 | let made = 0; | |
| 117 | const next = () => ++made; | |
| 118 | expect([make("a", next), make("a", next)]).toEqual([1, 1]); | |
| 119 | make("b", next); // a, b | |
| 120 | make("c", next); // b, c: a was the oldest | |
| 121 | expect(made).toBe(3); | |
| 122 | expect(make("b", next)).toBe(2); | |
| 123 | expect(make("a", next)).toBe(4); // made again after eviction | |
| 124 | expect(made).toBe(4); | |
| 125 | }); | |
| 126 | ||
| 127 | test("sheets and reminder rows are keyed by whether the viewer may change the deal", async () => { | |
| 128 | const { getDeal } = await import("./deals.ts"); | |
| 129 | const { openDb } = await import("./db.ts"); | |
| 130 | const { dealSheets } = await import("./pages/sheet.ts"); | |
| 131 | const dir = mkdtempSync(join(tmpdir(), "app-cache-keys-")); | |
| 132 | const db = openDb(dir); | |
| 133 | const alice = { role: "user", id: "u1", name: "alice" } as const; | |
| 134 | const planned = (await import("./deals.ts")).plan({ address: "3 Key Court", buyer: "A Buyer", seller: "A Seller", contractDate: "2027-03-01", closingDate: "2027-04-05" }); | |
| 135 | if (!planned.ok) throw new Error(planned.message); | |
| 136 | const { insertDeal } = await import("./deals.ts"); | |
| 137 | const inserted = insertDeal(db, alice, planned.value); | |
| 138 | const found = getDeal(db, alice, { id: inserted.id }); | |
| 139 | if (!found.ok) throw new Error(found.message); | |
| 140 | const strangerView = { role: "anon", id: "", name: "" } as const; | |
| 141 | // The same deal object, asked for by the one who may change it, then by one who may not, then again. | |
| 142 | const drawn = dealSheets(found.value, alice, "2027-03-02", "/"); | |
| 143 | const bare = dealSheets(found.value, strangerView, "2027-03-02", "/"); | |
| 144 | expect(drawn).toContain("/move"); | |
| 145 | expect(bare).not.toContain("/move"); | |
| 146 | expect(dealSheets(found.value, alice, "2027-03-02", "/")).toBe(drawn); | |
| 147 | db.close(); | |
| 148 | }); |
src/db.ts +4
| @@ -206,6 +206,10 @@ export const migrations: string[] = [ | ||
| 206 | 206 | sample TEXT NOT NULL |
| 207 | 207 | ); |
| 208 | 208 | INSERT OR IGNORE INTO sample_deal (deal_id, sample) SELECT id, address FROM deal WHERE author_id = 'sample'`, |
| 209 | // The lists read every deal's parties and every date's moves at once; both | |
| 210 | // are looked up by the deal or date they hang off. | |
| 211 | `CREATE INDEX IF NOT EXISTS party_deal ON party (deal_id); | |
| 212 | CREATE INDEX IF NOT EXISTS deadline_move_deadline ON deadline_move (deadline_id)`, | |
| 209 | 213 | ]; |
| 210 | 214 | |
| 211 | 215 | export function openDb(dir = dataDir): Database { |
src/deals.ts +106 −25
| @@ -161,31 +161,50 @@ const shown = (who: Who, deal: DealView): DealView => | ||
| 161 | 161 | |
| 162 | 162 | // ---- reading ---- |
| 163 | 163 | |
| 164 | function view(db: Database, deal: Deal): DealView { | |
| 165 | const deadlines = db | |
| 166 | .query<Deadline, [number]>( | |
| 167 | "SELECT * FROM deadline WHERE deal_id = ?1 ORDER BY due, id", | |
| 168 | ) | |
| 169 | .all(deal.id); | |
| 170 | const moves = db | |
| 171 | .query<Move, [number]>( | |
| 172 | "SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id WHERE d.deal_id = ?1 ORDER BY m.id", | |
| 173 | ) | |
| 174 | .all(deal.id); | |
| 164 | /** The rows that hang off deals, each list in the order a deal shows it. */ | |
| 165 | interface Children { | |
| 166 | deadlines: Deadline[]; | |
| 167 | moves: Move[]; | |
| 168 | parties: Party[]; | |
| 169 | sent: Sent[]; | |
| 170 | declined: Declined[]; | |
| 171 | } | |
| 172 | ||
| 173 | /** One deal assembled from its rows. */ | |
| 174 | function assemble(deal: Deal, rows: Children): DealView { | |
| 175 | const moves = rows.moves; | |
| 175 | 176 | return { |
| 176 | 177 | ...deal, |
| 177 | 178 | closing: |
| 178 | deadlines.find((d) => d.kind === "closing")?.due ?? deal.contract_date, | |
| 179 | closed: isClosed(deadlines), | |
| 179 | rows.deadlines.find((d) => d.kind === "closing")?.due ?? deal.contract_date, | |
| 180 | closed: isClosed(rows.deadlines), | |
| 181 | parties: rows.parties, | |
| 182 | deadlines: rows.deadlines.map((d) => ({ | |
| 183 | ...d, | |
| 184 | moves: moves.filter((m) => m.deadline_id === d.id), | |
| 185 | })), | |
| 186 | sent: rows.sent, | |
| 187 | declined: rows.declined, | |
| 188 | }; | |
| 189 | } | |
| 190 | ||
| 191 | function view(db: Database, deal: Deal): DealView { | |
| 192 | return assemble(deal, { | |
| 193 | deadlines: db | |
| 194 | .query<Deadline, [number]>( | |
| 195 | "SELECT * FROM deadline WHERE deal_id = ?1 ORDER BY due, id", | |
| 196 | ) | |
| 197 | .all(deal.id), | |
| 198 | moves: db | |
| 199 | .query<Move, [number]>( | |
| 200 | "SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id WHERE d.deal_id = ?1 ORDER BY m.id", | |
| 201 | ) | |
| 202 | .all(deal.id), | |
| 180 | 203 | parties: db |
| 181 | 204 | .query<Party, [number]>( |
| 182 | 205 | "SELECT * FROM party WHERE deal_id = ?1 ORDER BY id", |
| 183 | 206 | ) |
| 184 | 207 | .all(deal.id), |
| 185 | deadlines: deadlines.map((d) => ({ | |
| 186 | ...d, | |
| 187 | moves: moves.filter((m) => m.deadline_id === d.id), | |
| 188 | })), | |
| 189 | 208 | sent: db |
| 190 | 209 | .query<Sent, [number]>( |
| 191 | 210 | "SELECT * FROM reminder WHERE deal_id = ?1 ORDER BY sent_at DESC, id DESC", |
| @@ -196,7 +215,69 @@ function view(db: Database, deal: Deal): DealView { | ||
| 196 | 215 | "SELECT * FROM reminder_declined WHERE deal_id = ?1 ORDER BY id", |
| 197 | 216 | ) |
| 198 | 217 | .all(deal.id), |
| 199 | }; | |
| 218 | }); | |
| 219 | } | |
| 220 | ||
| 221 | /** Rows grouped by the deal they hang off, in the order read. */ | |
| 222 | function byDeal<T>(rows: T[], key: (row: T) => number): Map<number, T[]> { | |
| 223 | const groups = new Map<number, T[]>(); | |
| 224 | for (const row of rows) { | |
| 225 | const id = key(row); | |
| 226 | const group = groups.get(id); | |
| 227 | if (group === undefined) groups.set(id, [row]); | |
| 228 | else group.push(row); | |
| 229 | } | |
| 230 | return groups; | |
| 231 | } | |
| 232 | ||
| 233 | /** | |
| 234 | * Many deals at once: one query per table for the whole list, not five per | |
| 235 | * deal. `where` and `params` pick the deals, as `SELECT * FROM deal` takes them. | |
| 236 | */ | |
| 237 | function viewMany(db: Database, where: string, params: string[]): DealView[] { | |
| 238 | const deals = db | |
| 239 | .query<Deal, string[]>(`SELECT * FROM deal${where}`) | |
| 240 | .all(...params); | |
| 241 | const of = `deal_id IN (SELECT id FROM deal${where})`; | |
| 242 | const deadlines = byDeal( | |
| 243 | db.query<Deadline, string[]>(`SELECT * FROM deadline WHERE ${of} ORDER BY due, id`).all(...params), | |
| 244 | (r) => r.deal_id, | |
| 245 | ); | |
| 246 | // A move belongs to a deal through its deadline; group by that, not by a column of its own. | |
| 247 | const dealOf = new Map<number, number>(); | |
| 248 | for (const [id, rows] of deadlines) for (const d of rows) dealOf.set(d.id, id); | |
| 249 | const moves = byDeal( | |
| 250 | db | |
| 251 | .query<Move, string[]>( | |
| 252 | `SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id | |
| 253 | WHERE d.${of} ORDER BY m.id`, | |
| 254 | ) | |
| 255 | .all(...params), | |
| 256 | (r) => dealOf.get(r.deadline_id)!, | |
| 257 | ); | |
| 258 | const parties = byDeal( | |
| 259 | db.query<Party, string[]>(`SELECT * FROM party WHERE ${of} ORDER BY id`).all(...params), | |
| 260 | (r) => r.deal_id, | |
| 261 | ); | |
| 262 | const sent = byDeal( | |
| 263 | db | |
| 264 | .query<Sent, string[]>(`SELECT * FROM reminder WHERE ${of} ORDER BY sent_at DESC, id DESC`) | |
| 265 | .all(...params), | |
| 266 | (r) => r.deal_id, | |
| 267 | ); | |
| 268 | const declined = byDeal( | |
| 269 | db.query<Declined, string[]>(`SELECT * FROM reminder_declined WHERE ${of} ORDER BY id`).all(...params), | |
| 270 | (r) => r.deal_id, | |
| 271 | ); | |
| 272 | return deals.map((deal) => | |
| 273 | assemble(deal, { | |
| 274 | deadlines: deadlines.get(deal.id) ?? [], | |
| 275 | moves: moves.get(deal.id) ?? [], | |
| 276 | parties: parties.get(deal.id) ?? [], | |
| 277 | sent: sent.get(deal.id) ?? [], | |
| 278 | declined: declined.get(deal.id) ?? [], | |
| 279 | }), | |
| 280 | ); | |
| 200 | 281 | } |
| 201 | 282 | |
| 202 | 283 | export function listDeals(db: Database, who: Who): Result<DealView[]> { |
| @@ -205,15 +286,15 @@ export function listDeals(db: Database, who: Who): Result<DealView[]> { | ||
| 205 | 286 | return refuse(who, "Sign in to see deals.", "You may not see these deals."); |
| 206 | 287 | const deals = |
| 207 | 288 | may === "yes" |
| 208 | ? db.query<Deal, []>("SELECT * FROM deal").all() | |
| 209 | : db | |
| 210 | .query<Deal, [string]>( | |
| 211 | `SELECT * FROM deal WHERE ${may.where.map((c) => `${c} = ?1`).join(" OR ")}`, | |
| 212 | ) | |
| 213 | .all(who.role === "anon" ? "" : who.id); | |
| 289 | ? viewMany(db, "", []) | |
| 290 | : viewMany( | |
| 291 | db, | |
| 292 | ` WHERE ${may.where.map((c) => `${c} = ?1`).join(" OR ")}`, | |
| 293 | [who.role === "anon" ? "" : who.id], | |
| 294 | ); | |
| 214 | 295 | return ok( |
| 215 | 296 | deals |
| 216 | .map((d) => shown(who, view(db, d))) | |
| 297 | .map((d) => shown(who, d)) | |
| 217 | 298 | .sort((a, b) => a.closing.localeCompare(b.closing) || a.id - b.id), |
| 218 | 299 | ); |
| 219 | 300 | } |
src/http.ts +14 −2
| @@ -8,6 +8,7 @@ import { createHash } from "node:crypto"; | ||
| 8 | 8 | import { existsSync, readFileSync } from "node:fs"; |
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { env, publicOrigin } from "./env.ts"; |
| 11 | import { memoizer } from "./memo.ts"; | |
| 11 | 12 | |
| 12 | 13 | // Scripts and styles come from this origin (inline styles allowed, inline |
| 13 | 14 | // scripts not), nothing frames us, browsers never guess a content type, and |
| @@ -35,12 +36,20 @@ export const escape = (s: string): string => | ||
| 35 | 36 | ] ?? c, |
| 36 | 37 | ); |
| 37 | 38 | |
| 39 | // A big page is encoded once while it stays the same string (the clock | |
| 40 | // hands back the page it already rendered), and the response remembers its | |
| 41 | // bytes so the server can compress them once too. | |
| 42 | const encoded = memoizer<Uint8Array>(3); | |
| 43 | export const sourceBytes = new WeakMap<Response, Uint8Array>(); | |
| 44 | const BIG = 64 * 1024; | |
| 45 | ||
| 38 | 46 | export const html = ( |
| 39 | 47 | body: string, |
| 40 | 48 | status = 200, |
| 41 | 49 | headers: Record<string, string> = {}, |
| 42 | ): Response => | |
| 43 | new Response(body, { | |
| 50 | ): Response => { | |
| 51 | const bytes = body.length < BIG ? undefined : encoded(body, () => new TextEncoder().encode(body)); | |
| 52 | const response = new Response(bytes ?? body, { | |
| 44 | 53 | status, |
| 45 | 54 | headers: { |
| 46 | 55 | "content-type": "text/html; charset=utf-8", |
| @@ -48,6 +57,9 @@ export const html = ( | ||
| 48 | 57 | ...headers, |
| 49 | 58 | }, |
| 50 | 59 | }); |
| 60 | if (bytes !== undefined) sourceBytes.set(response, bytes); | |
| 61 | return response; | |
| 62 | }; | |
| 51 | 63 | |
| 52 | 64 | export const json = (data: unknown, status = 200): Response => |
| 53 | 65 | new Response(JSON.stringify(data), { |
src/memo.ts added +30
| @@ -0,0 +1,30 @@ | ||
| 1 | /** | |
| 2 | * Work done once for a deal as it stands. A deal is keyed by its content, so | |
| 3 | * a change to any of its rows is a different key and the old entry is simply | |
| 4 | * never asked for again; nothing needs to be told to forget. Each cache | |
| 5 | * holds a bounded number of entries, the oldest dropped first. | |
| 6 | */ | |
| 7 | ||
| 8 | const prints = new WeakMap<object, string>(); | |
| 9 | ||
| 10 | /** A deal's content as one string: equal for equal rows, different when any row differs. */ | |
| 11 | export function print(value: object): string { | |
| 12 | let text = prints.get(value); | |
| 13 | if (text === undefined) { | |
| 14 | text = JSON.stringify(value); | |
| 15 | prints.set(value, text); | |
| 16 | } | |
| 17 | return text; | |
| 18 | } | |
| 19 | ||
| 20 | /** A bounded cache: `memoizer(limit)(key, make)` returns the value made for that key, making it once. */ | |
| 21 | export function memoizer<T>(limit: number): (key: string, make: () => T) => T { | |
| 22 | const entries = new Map<string, T>(); | |
| 23 | return (key, make) => { | |
| 24 | if (entries.has(key)) return entries.get(key)!; | |
| 25 | const value = make(); | |
| 26 | entries.set(key, value); | |
| 27 | if (entries.size > limit) entries.delete(entries.keys().next().value!); | |
| 28 | return value; | |
| 29 | }; | |
| 30 | } |
src/pages/clock.ts +40 −10
| @@ -15,9 +15,10 @@ import type { Who } from "../who.ts"; | ||
| 15 | 15 | import { thisWeek } from "../week.ts"; |
| 16 | 16 | import { layout } from "./layout.ts"; |
| 17 | 17 | import { mayRestore } from "../actions.ts"; |
| 18 | import { badge, draftItem, owedBy, sheet, sheetId, URGENCY_LABEL } from "./sheet.ts"; | |
| 18 | import { badge, dealSheets, draftItem, owedBy, sheetId, URGENCY_LABEL } from "./sheet.ts"; | |
| 19 | 19 | import { canChange } from "../deals.ts"; |
| 20 | 20 | import { find } from "../find.ts"; |
| 21 | import { memoizer, print } from "../memo.ts"; | |
| 21 | 22 | |
| 22 | 23 | /** The days the clock shows: a week behind today (further, if something open is older), to a few days past the last closing. */ |
| 23 | 24 | export function span(deals: readonly DealView[], on: string): { start: string; days: number } { |
| @@ -112,12 +113,44 @@ function weekCard(deals: readonly DealView[], on: string): string { | ||
| 112 | 113 | </section>`; |
| 113 | 114 | } |
| 114 | 115 | |
| 116 | const remembered = memoizer<{ count: number; toSend: number; html: string }>(400); | |
| 117 | ||
| 118 | /** One deal's reminders that go out today, as list items: counted, and rendered once while the deal stands. */ | |
| 119 | function reminderRows(deal: DealView, who: Who, on: string): { count: number; toSend: number; html: string } { | |
| 120 | const may = canChange(who, deal); | |
| 121 | return remembered(`${may}|${on}|${print(deal)}`, () => { | |
| 122 | const these = drafts(deal, agent).filter((r) => pending(r, on)); | |
| 123 | return { | |
| 124 | count: these.length, | |
| 125 | toSend: these.filter(unsent).length, | |
| 126 | html: these.map((r) => draftItem(r, on, may, "/", true)).join(""), | |
| 127 | }; | |
| 128 | }); | |
| 129 | } | |
| 130 | ||
| 131 | const pages = memoizer<string>(3); | |
| 132 | ||
| 133 | /** The clock, rendered once while nothing it shows has changed. */ | |
| 115 | 134 | export function clock(input: { |
| 116 | 135 | who: Who; |
| 117 | 136 | deals: readonly DealView[]; |
| 118 | 137 | on: string; |
| 119 | 138 | notice?: string | undefined; |
| 120 | 139 | q?: string | undefined; |
| 140 | }): string { | |
| 141 | const key = Bun.hash( | |
| 142 | JSON.stringify([input.who, input.on, input.notice ?? null, input.q ?? "", agent]) + | |
| 143 | input.deals.map(print).join(), | |
| 144 | ); | |
| 145 | return pages(`${input.deals.length}:${key}`, () => render(input)); | |
| 146 | } | |
| 147 | ||
| 148 | function render(input: { | |
| 149 | who: Who; | |
| 150 | deals: readonly DealView[]; | |
| 151 | on: string; | |
| 152 | notice?: string | undefined; | |
| 153 | q?: string | undefined; | |
| 121 | 154 | }): string { |
| 122 | 155 | const { who, on, notice } = input; |
| 123 | 156 | const q = input.q ?? ""; |
| @@ -130,13 +163,10 @@ export function clock(input: { | ||
| 130 | 163 | const overdue = open.filter((d) => urgency(d.due, on, false) === "overdue").length; |
| 131 | 164 | const soon = open.filter((d) => urgency(d.due, on, false) === "soon").length; |
| 132 | 165 | const { start, days } = span(deals, on); |
| 133 | const today = deals.flatMap((deal) => | |
| 134 | drafts(deal, agent) | |
| 135 | .filter((r) => pending(r, on)) | |
| 136 | .map((r) => ({ r, may: canChange(who, deal) })), | |
| 137 | ); | |
| 166 | const today = deals.map((deal) => reminderRows(deal, who, on)); | |
| 167 | const todayCount = today.reduce((n, t) => n + t.count, 0); | |
| 138 | 168 | const none = q !== "" && found.length === 0; |
| 139 | const toSend = today.filter(({ r }) => unsent(r)).length; | |
| 169 | const toSend = today.reduce((n, t) => n + t.toSend, 0); | |
| 140 | 170 | const nothing = |
| 141 | 171 | q !== "" |
| 142 | 172 | ? `<p class="empty">No deal matches “${escape(q)}”. <a href="/">Clear</a></p>` |
| @@ -152,7 +182,7 @@ export function clock(input: { | ||
| 152 | 182 | <ol class="lanes">${deals.map((deal) => lane(deal, start, on)).join("")}</ol> |
| 153 | 183 | </div> |
| 154 | 184 | </div> |
| 155 | ${deals.flatMap((deal) => deal.deadlines.map((d) => sheet(deal, d, who, on, "/"))).join("")}`; | |
| 185 | ${deals.map((deal) => dealSheets(deal, who, on, "/")).join("")}`; | |
| 156 | 186 | const closedCard = |
| 157 | 187 | closed.length === 0 |
| 158 | 188 | ? "" |
| @@ -199,9 +229,9 @@ export function clock(input: { | ||
| 199 | 229 | ${none ? "" : `<section id="today" class="card" data-live="deal.changed"> |
| 200 | 230 | <h2>Reminders for today <span class="muted">· ${toSend === 0 ? "all sent" : `${toSend} to send`}</span></h2> |
| 201 | 231 | ${ |
| 202 | today.length === 0 | |
| 232 | todayCount === 0 | |
| 203 | 233 | ? `<p class="muted">Nothing goes out today.</p>` |
| 204 | : `<ul class="drafts">${today.map(({ r, may }) => draftItem(r, on, may, "/", true)).join("")}</ul>` | |
| 234 | : `<ul class="drafts">${today.map((t) => t.html).join("")}</ul>` | |
| 205 | 235 | } |
| 206 | 236 | <p class="muted small">Each date reminds its parties three days before and the morning of, and the one who owes a date is asked for an update the morning after it slips. Sending is simulated: the draft is shown and marked sent.</p> |
| 207 | 237 | </section>`}`; |
src/pages/sheet.ts +13
| @@ -10,6 +10,7 @@ import { relative, short, spoken, urgency, type Urgency } from "../dates.ts"; | ||
| 10 | 10 | import { PARTY_LABEL } from "../deadlines.ts"; |
| 11 | 11 | import { agent } from "../env.ts"; |
| 12 | 12 | import { escape } from "../http.ts"; |
| 13 | import { memoizer, print } from "../memo.ts"; | |
| 13 | 14 | import { draftsFor, MOMENT_LABEL, MOMENTS, type Draft } from "../reminders.ts"; |
| 14 | 15 | import type { Who } from "../who.ts"; |
| 15 | 16 | import { owner } from "../week.ts"; |
| @@ -163,3 +164,15 @@ export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to: | ||
| 163 | 164 | ${actions} |
| 164 | 165 | </div>`; |
| 165 | 166 | } |
| 167 | ||
| 168 | const remembered = memoizer<string>(300); | |
| 169 | ||
| 170 | /** | |
| 171 | * Every sheet of a deal, in date order. What a sheet shows depends on the | |
| 172 | * deal, whether `who` may change it, the day and where its forms return to, | |
| 173 | * so that is the key: a deal nobody touched is not rendered again. | |
| 174 | */ | |
| 175 | export const dealSheets = (deal: DealView, who: Who, on: string, to: string): string => | |
| 176 | remembered(`${canChange(who, deal)}|${on}|${to}|${JSON.stringify(agent)}|${print(deal)}`, () => | |
| 177 | deal.deadlines.map((d) => sheet(deal, d, who, on, to)).join(""), | |
| 178 | ); |
src/reminders.ts +9 −4
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | import { addDays, spoken } from "./dates.ts"; |
| 11 | import { memoizer, print } from "./memo.ts"; | |
| 11 | 12 | import { KIND, PARTY_LABEL, type PartyRole } from "./deadlines.ts"; |
| 12 | 13 | import type { DealView, DeadlineView, Moment, Party } from "./deals.ts"; |
| 13 | 14 | |
| @@ -92,13 +93,17 @@ function words( | ||
| 92 | 93 | } |
| 93 | 94 | |
| 94 | 95 | /** Every reminder an open date of this deal has, sent or not; a closed deal reminds no one. */ |
| 95 | export function drafts(deal: DealView, agent: Agent): Draft[] { | |
| 96 | export function drafts(deal: DealView, agent: Agent): readonly Draft[] { | |
| 96 | 97 | if (deal.closed) return []; |
| 97 | return deal.deadlines | |
| 98 | .filter((d) => d.done_at === null) | |
| 99 | .flatMap((deadline) => draftsFor(deal, deadline, agent)); | |
| 98 | return remembered(`${JSON.stringify(agent)}|${print(deal)}`, () => | |
| 99 | deal.deadlines | |
| 100 | .filter((d) => d.done_at === null) | |
| 101 | .flatMap((deadline) => draftsFor(deal, deadline, agent)), | |
| 102 | ); | |
| 100 | 103 | } |
| 101 | 104 | |
| 105 | const remembered = memoizer<Draft[]>(400); | |
| 106 | ||
| 102 | 107 | /** The reminders one date has, sent or not. */ |
| 103 | 108 | export function draftsFor(deal: DealView, deadline: DeadlineView, agent: Agent): Draft[] { |
| 104 | 109 | return MOMENTS.flatMap((moment) => |
src/server.test.ts +4 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | import { afterAll, expect, test } from "bun:test"; | |
| 1 | import { afterAll, beforeEach, expect, test } from "bun:test"; | |
| 2 | 2 | import { mkdtempSync } from "node:fs"; |
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| @@ -8,6 +8,7 @@ import { drafts } from "./reminders.ts"; | ||
| 8 | 8 | import { serve } from "./server.ts"; |
| 9 | 9 | import { token } from "./tokens.ts"; |
| 10 | 10 | import { sign } from "./who.ts"; |
| 11 | import { forgetRationing } from "./security.ts"; | |
| 11 | 12 | |
| 12 | 13 | const KEY = "a-key-only-the-forge-holds"; |
| 13 | 14 | const server = serve({ |
| @@ -17,6 +18,8 @@ const server = serve({ | ||
| 17 | 18 | }); |
| 18 | 19 | const base = `http://${server.hostname}:${server.port}`; |
| 19 | 20 | afterAll(() => server.stop(true)); |
| 21 | // Each test starts with full buckets: the file's anonymous writes share one address. | |
| 22 | beforeEach(forgetRationing); | |
| 20 | 23 | |
| 21 | 24 | /** The headers the forge sends for a signed-in person. */ |
| 22 | 25 | const as = ( |
src/server.ts +15 −3
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { openDb } from "./db.ts"; |
| 11 | 11 | import { dataDir, identityKey, title, where } from "./env.ts"; |
| 12 | import { html, text } from "./http.ts"; | |
| 12 | import { html, sourceBytes, text } from "./http.ts"; | |
| 13 | 13 | import { follow } from "./live.ts"; |
| 14 | 14 | import { match } from "./routes.ts"; |
| 15 | 15 | import { door } from "./security.ts"; |
| @@ -86,13 +86,25 @@ async function squeeze(request: Request, response: Response): Promise<Response> | ||
| 86 | 86 | !/\bgzip\b/.test(request.headers.get("accept-encoding") ?? "") |
| 87 | 87 | ) |
| 88 | 88 | return response; |
| 89 | const body = new Uint8Array(await response.arrayBuffer()); | |
| 89 | const body = sourceBytes.get(response) ?? new Uint8Array(await response.arrayBuffer()); | |
| 90 | 90 | if (body.byteLength < 2048) |
| 91 | 91 | return new Response(body, { status: response.status, headers: response.headers }); |
| 92 | 92 | const headers = new Headers(response.headers); |
| 93 | 93 | headers.set("content-encoding", "gzip"); |
| 94 | 94 | headers.append("vary", "accept-encoding"); |
| 95 | return new Response(Bun.gzipSync(body), { status: response.status, headers }); | |
| 95 | return new Response(gzipped(body), { status: response.status, headers }); | |
| 96 | } | |
| 97 | ||
| 98 | // The clock is megabytes with a large book and the same bytes until a deal | |
| 99 | // changes: compress them once, by the identity of the bytes `html` encoded. | |
| 100 | const compressed = new WeakMap<Uint8Array, Uint8Array>(); | |
| 101 | function gzipped(body: Uint8Array): Uint8Array { | |
| 102 | let zipped = compressed.get(body); | |
| 103 | if (zipped === undefined) { | |
| 104 | zipped = Bun.gzipSync(body); | |
| 105 | compressed.set(body, zipped); | |
| 106 | } | |
| 107 | return zipped; | |
| 96 | 108 | } |
| 97 | 109 | |
| 98 | 110 | // HEAD is GET without the body: a health check sees the app up. |
src/speed.test.ts added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | /** The clock, a deal page and the client timeline stay fast with a large book. */ | |
| 2 | import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { insertDeal, plan } from "./deals.ts"; | |
| 7 | import { openDb } from "./db.ts"; | |
| 8 | import { forgetRationing } from "./security.ts"; | |
| 9 | import { serve } from "./server.ts"; | |
| 10 | import { sign, type Who } from "./who.ts"; | |
| 11 | ||
| 12 | const KEY = "a-key-only-the-forge-holds"; | |
| 13 | const BUDGET_MS = 50; | |
| 14 | const dir = mkdtempSync(join(tmpdir(), "app-speed-")); | |
| 15 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 16 | ||
| 17 | let firstId = 0; | |
| 18 | let movable = 0; | |
| 19 | let server: ReturnType<typeof serve>; | |
| 20 | let base = ""; | |
| 21 | let headers: Record<string, string> = {}; | |
| 22 | ||
| 23 | // Seeded when this file's tests start, not when the file is loaded: the | |
| 24 | // writes would otherwise reach other files' servers mid-test. | |
| 25 | beforeAll(() => { | |
| 26 | const seeded = openDb(dir); | |
| 27 | const party = seeded.query("INSERT INTO party (deal_id, role, name, contact) VALUES (?1, ?2, ?3, ?4) RETURNING id"); | |
| 28 | for (let i = 0; i < 200; i++) { | |
| 29 | const planned = plan({ | |
| 30 | address: `${100 + i} Speed Test Lane`, | |
| 31 | buyer: `Buyer ${i}`, | |
| 32 | seller: `Seller ${i}`, | |
| 33 | contractDate: "2026-10-01", | |
| 34 | closingDate: "2026-11-20", | |
| 35 | }); | |
| 36 | if (!planned.ok) throw new Error(planned.message); | |
| 37 | const deal = insertDeal(seeded, alice, planned.value); | |
| 38 | if (i === 0) firstId = deal.id; | |
| 39 | // The buyer and seller are in; a lender and a title company make four. | |
| 40 | const lender = party.get(deal.id, "lender", `Lender ${i}`, "[email protected]") as { id: number }; | |
| 41 | party.get(deal.id, "title", `Title ${i}`, "[email protected]"); | |
| 42 | if (i % 2 !== 0) continue; | |
| 43 | const [first, second] = seeded | |
| 44 | .query<{ id: number; label: string; due: string }, [number]>("SELECT id, label, due FROM deadline WHERE deal_id = ?1 ORDER BY due, id") | |
| 45 | .all(deal.id); | |
| 46 | seeded | |
| 47 | .query("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, party_role, moment, due, by_name) VALUES (?1, ?2, ?3, ?4, ?5, 'lender', 'before', ?6, 'alice')") | |
| 48 | .run(deal.id, first!.id, lender.id, first!.label, `Lender ${i}`, first!.due); | |
| 49 | seeded.query("UPDATE deadline SET due = '2026-11-02' WHERE id = ?1").run(second!.id); | |
| 50 | seeded | |
| 51 | .query("INSERT INTO deadline_move (deadline_id, from_due, to_due, reason, by_name) VALUES (?1, ?2, '2026-11-02', 'Seller asked for more time', 'alice')") | |
| 52 | .run(second!.id, second!.due); | |
| 53 | } | |
| 54 | movable = seeded | |
| 55 | .query<{ id: number }, [number]>("SELECT id FROM deadline WHERE deal_id = ?1 AND kind = 'closing'") | |
| 56 | .get(firstId)!.id; | |
| 57 | seeded.close(); | |
| 58 | server = serve({ port: 0, dataDir: dir, identityKey: KEY }); | |
| 59 | base = `http://${server.hostname}:${server.port}`; | |
| 60 | const identity = `u1|alice|user|${Date.now() + 600_000}`; | |
| 61 | headers = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 62 | }); | |
| 63 | afterAll(() => server.stop(true)); | |
| 64 | beforeEach(forgetRationing); | |
| 65 | ||
| 66 | const SAMPLES = 25; | |
| 67 | const mid = (times: number[]): number => times.sort((x, y) => x - y)[Math.floor(times.length / 2)]!; | |
| 68 | ||
| 69 | /** Ms from sending a request to the response head: the app has built and compressed the page by then. */ | |
| 70 | async function timed(path: string): Promise<number> { | |
| 71 | const start = performance.now(); | |
| 72 | const res = await fetch(base + path, { headers }); | |
| 73 | const took = performance.now() - start; | |
| 74 | expect(res.status).toBe(200); | |
| 75 | // The body is read after the clock stops, so the budget is the app's, not | |
| 76 | // the time this process spends inflating a book-sized page it also served. | |
| 77 | expect((await res.arrayBuffer()).byteLength).toBeGreaterThan(0); | |
| 78 | return took; | |
| 79 | } | |
| 80 | ||
| 81 | /** The median of 25 requests after a warm-up: a book nobody is changing. */ | |
| 82 | async function warm(path: string): Promise<number> { | |
| 83 | await timed(path); | |
| 84 | const times: number[] = []; | |
| 85 | for (let i = 0; i < SAMPLES; i++) times.push(await timed(path)); | |
| 86 | return mid(times); | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * The median, over 5 edits, of the first request after an edit: what a | |
| 91 | * person waits for after every change, with nothing remembered for the deal. | |
| 92 | */ | |
| 93 | async function cold(path: string): Promise<number> { | |
| 94 | await timed(path); | |
| 95 | const times: number[] = []; | |
| 96 | for (let i = 0; i < 5; i++) { | |
| 97 | const body = new FormData(); | |
| 98 | body.set("due", `2026-12-${String(10 + i).padStart(2, "0")}`); | |
| 99 | body.set("reason", `Moved ${i}`); | |
| 100 | body.set("to", "/"); | |
| 101 | const moved = await fetch(`${base}/deadlines/${movable}/move`, { method: "POST", body, headers, redirect: "manual" }); | |
| 102 | expect(moved.status).toBe(303); | |
| 103 | times.push(await timed(path)); | |
| 104 | } | |
| 105 | return mid(times); | |
| 106 | } | |
| 107 | ||
| 108 | const routes: [string, () => string][] = [ | |
| 109 | ["the clock", () => "/"], | |
| 110 | ["the deal list", () => "/api/deals"], | |
| 111 | ["a deal page", () => `/deals/${firstId}`], | |
| 112 | ["the client timeline", () => `/deals/${firstId}/timeline`], | |
| 113 | ]; | |
| 114 | ||
| 115 | test.each(routes)("%s answers in under 50 ms with 200 deals", async (_name, path) => { | |
| 116 | expect(await warm(path())).toBeLessThan(BUDGET_MS); | |
| 117 | }); | |
| 118 | ||
| 119 | // The clock carries every deal's sheets inline, some 9 MB with 200 deals, and | |
| 120 | // after an edit that page is rebuilt, encoded and compressed whole. Cutting | |
| 121 | // that is a change to what the page shows, so its budget after an edit is | |
| 122 | // larger, and a regression past it still fails. | |
| 123 | const COLD_CLOCK_MS = 400; | |
| 124 | ||
| 125 | test.each(routes)("%s answers within budget the first time after a deal changes", async (name, path) => { | |
| 126 | expect(await cold(path())).toBeLessThan(name === "the clock" ? COLD_CLOCK_MS : BUDGET_MS); | |
| 127 | }); |
new above
offline