cursedbelt-server 4.78.0 → 4.79.2

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.
Files changed (65) hide show
  1. package/dist/server/analytics/injectedScripts.d.ts +1 -1
  2. package/dist/server/analytics/injectedScripts.js +1 -1
  3. package/dist/server/analytics/webAnalytics.d.ts +2 -2
  4. package/dist/server/analytics/webAnalytics.js +2 -2
  5. package/dist/server/clientIssues/d1.d.ts +1 -1
  6. package/dist/server/engagement/visits.d.ts +1 -1
  7. package/dist/server/incidents/channels.d.ts +2 -2
  8. package/dist/server/incidents/channels.js +2 -2
  9. package/dist/server/incidents/index.d.ts +2 -2
  10. package/dist/server/incidents/index.js +2 -2
  11. package/dist/server/incidents/notifier.d.ts +6 -6
  12. package/dist/server/incidents/notifier.js +6 -6
  13. package/dist/server/incidents/reporter.d.ts +1 -1
  14. package/dist/server/incidents/types.d.ts +5 -5
  15. package/dist/server/incidents/types.js +2 -2
  16. package/dist/server/maps-budget/mapsBudget.d.ts +37 -0
  17. package/dist/server/maps-budget/mapsBudget.js +52 -0
  18. package/dist/server/maps-static/staticMap.d.ts +30 -3
  19. package/dist/server/maps-static/staticMap.js +35 -3
  20. package/dist/server/master-lock/accountsPage.js +2 -2
  21. package/dist/server/master-lock/attempts.js +1 -1
  22. package/dist/server/master-lock/masterLock.d.ts +2 -2
  23. package/dist/server/master-lock/masterLock.js +3 -3
  24. package/dist/server/master-lock/principals.js +1 -1
  25. package/dist/server/presence/index.d.ts +3 -2
  26. package/dist/server/presence/index.js +35 -2
  27. package/dist/server/requestScope.d.ts +8 -0
  28. package/dist/server/requestScope.js +33 -0
  29. package/dist/server/resume/store.d.ts +1 -1
  30. package/dist/server/storage/binaryStoreFake.d.ts +1 -1
  31. package/dist/server/sync/status.d.ts +1 -1
  32. package/dist/server/youtube/ytdlp.d.ts +1 -1
  33. package/package.json +3 -3
  34. package/src/cursedopsRange.spec.ts +5 -3
  35. package/src/noTimerDialsAPeer.spec.ts +3 -3
  36. package/src/server/analytics/injectedScripts.spec.ts +1 -1
  37. package/src/server/analytics/injectedScripts.ts +1 -1
  38. package/src/server/analytics/webAnalytics.ts +2 -2
  39. package/src/server/clientIssues/d1.ts +1 -1
  40. package/src/server/engagement/engagement.spec.ts +1 -1
  41. package/src/server/engagement/visits.ts +1 -1
  42. package/src/server/incidents/channels.ts +2 -2
  43. package/src/server/incidents/index.ts +2 -2
  44. package/src/server/incidents/notifier.ts +9 -9
  45. package/src/server/incidents/reporter.ts +1 -1
  46. package/src/server/incidents/types.ts +5 -5
  47. package/src/server/maps-budget/mapsBudget.ts +73 -0
  48. package/src/server/maps-static/renderStaticMap.spec.ts +79 -0
  49. package/src/server/maps-static/staticMap.ts +47 -3
  50. package/src/server/master-lock/accounts.spec.ts +4 -4
  51. package/src/server/master-lock/accountsPage.spec.ts +1 -1
  52. package/src/server/master-lock/accountsPage.ts +2 -2
  53. package/src/server/master-lock/attempts.ts +1 -1
  54. package/src/server/master-lock/beaconNeverReachesTheWall.spec.ts +3 -3
  55. package/src/server/master-lock/guard.spec.ts +1 -1
  56. package/src/server/master-lock/masterLock.ts +3 -3
  57. package/src/server/master-lock/principals.spec.ts +2 -2
  58. package/src/server/master-lock/principals.ts +1 -1
  59. package/src/server/presence/index.ts +3 -2
  60. package/src/server/requestScope.spec.ts +15 -1
  61. package/src/server/requestScope.ts +35 -0
  62. package/src/server/resume/store.ts +1 -1
  63. package/src/server/storage/binaryStoreFake.ts +1 -1
  64. package/src/server/sync/status.ts +1 -1
  65. package/src/server/youtube/ytdlp.ts +1 -1
@@ -7,14 +7,17 @@
7
7
  * request a query string may ask for (clamped, so the cache key space stays tiny), the URL with
8
8
  * the fleet's map styling, the cache key, and an LRU for the bytes.
9
9
  *
10
- * 🔴 The FETCH is not here, on purpose. Each app keeps its own ONE DOOR to Google — the place
11
- * its `cursedbelt-server/maps-budget` ceiling is consumed immediately before the request — so
12
- * the ceiling and the call can never be separated. Build the URL here; spend it there.
10
+ * 🔴 The FETCH is here only inside {@link renderStaticMap}, THE door: a key Google refused, then the
11
+ * app's `cursedbelt-server/maps-budget` ceiling consumed immediately before the one request, so the
12
+ * ceiling and the call can never be separated. photos and trivia each held a copy of it until forge
13
+ * task 5764; a fix to the refusal or the timeout now reaches every app that draws a map.
13
14
  *
14
15
  * 🔴 The key NEVER reaches a browser: `GOOGLE_MAPS_API_KEY` is restricted by API and not by
15
16
  * HTTP referrer, so the imagery is proxied and the frame's overlay is our own SVG.
16
17
  */
17
18
 
19
+ import type { AsyncMapsBudget, KeyRefusalMemory } from '../maps-budget/mapsBudget.js';
20
+
18
21
  /** The one Static Maps endpoint. An app's door checks a URL starts with it. */
19
22
  export const STATIC_MAP_HOST = "https://maps.googleapis.com/maps/api/staticmap";
20
23
 
@@ -195,3 +198,44 @@ export function createImageCache(max = 160) {
195
198
  };
196
199
  }
197
200
 
201
+
202
+ /** The app's seam to Google: its key and its fetch (a spec passes a fake). */
203
+ export interface StaticMapSeam {
204
+ apiKey: string;
205
+ fetch: (url: string, init?: RequestInit) => Promise<Response>;
206
+ }
207
+
208
+ export type RenderedStaticMap = { ok: true; bytes: Uint8Array } | { ok: false; reason: string };
209
+
210
+ /** How long the door waits on Google before it gives up and the frame draws its graticule. */
211
+ export const STATIC_MAP_TIMEOUT_MS = 8000;
212
+
213
+ /**
214
+ * THE door to Google Static Maps: a refused key and the ceiling first, then the one request, with a
215
+ * timeout. A refusal touches no network, and a 401/403 is remembered so the next viewport does not ask.
216
+ */
217
+ export async function renderStaticMap(input: {
218
+ request: StaticMapRequest;
219
+ maps: StaticMapSeam;
220
+ budget: Pick<AsyncMapsBudget<'staticmap'>, 'consume'>;
221
+ refusals: KeyRefusalMemory;
222
+ timeoutMs?: number;
223
+ }): Promise<RenderedStaticMap> {
224
+ const held = await input.refusals.held('staticmap');
225
+ if (held) return { ok: false, reason: held };
226
+ const verdict = await input.budget.consume('staticmap');
227
+ if (!verdict.allowed) return { ok: false, reason: verdict.reason };
228
+ const controller = new AbortController();
229
+ const timer = setTimeout(() => controller.abort(), input.timeoutMs ?? STATIC_MAP_TIMEOUT_MS);
230
+ try {
231
+ const response = await input.maps.fetch(staticMapUrl(input.request, input.maps.apiKey), { signal: controller.signal });
232
+ const refused = await input.refusals.record('staticmap', response);
233
+ if (refused) return { ok: false, reason: refused };
234
+ if (!response.ok) return { ok: false, reason: `Google staticmap answered ${response.status}` };
235
+ return { ok: true, bytes: new Uint8Array(await response.arrayBuffer()) };
236
+ } catch (error) {
237
+ return { ok: false, reason: `Google staticmap did not answer (${error instanceof Error ? error.message : 'unknown'})` };
238
+ } finally {
239
+ clearTimeout(timer);
240
+ }
241
+ }
@@ -67,7 +67,7 @@ async function open(lock: MasterLock, verifier: string) {
67
67
  return { ...result, req: req(result.token) };
68
68
  }
69
69
 
70
- describe("🔴 claim 1 — the owner's existing password keeps opening everything he has", () => {
70
+ describe("🔴 claim 1 — the owner's existing password keeps opening everything they have", () => {
71
71
  test("a legacy single-password record becomes acct-1, and that password still works", async () => {
72
72
  // THE migration guarantee. `apps/collections` stamps its 279 existing rows with this
73
73
  // id, so an id that moved — or a record that failed to parse and armed nothing —
@@ -152,7 +152,7 @@ describe("🔴 claim 2 — a second password is a second tenant, and never the f
152
152
 
153
153
  test("creating a tenant does NOT enter it", async () => {
154
154
  // Switching means typing that tenant's password. If creating one dropped you into
155
- // it, the owner would add an account and watch his library apparently vanish.
155
+ // it, the owner would add an account and watch their library apparently vanish.
156
156
  const { lock } = build(legacyJson);
157
157
  const first = await open(lock, firstVerifier);
158
158
  await lock.addAccount(first.req, {
@@ -165,8 +165,8 @@ describe("🔴 claim 2 — a second password is a second tenant, and never the f
165
165
 
166
166
  test("🔴 a password that already opens something is refused", async () => {
167
167
  // Two accounts under one password would make `unlock` serve whichever the loop
168
- // reached first — so the owner would type the password he has always typed and be
169
- // handed an empty library, with his real one apparently gone.
168
+ // reached first — so the owner would type the password they have always typed and be
169
+ // handed an empty library, with the owner's real one apparently gone.
170
170
  const { lock } = build(legacyJson);
171
171
  const first = await open(lock, firstVerifier);
172
172
  expect(
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * `cursedbelt@2.5.1`'s React dialog minted a FRESH `kdf` salt on every rotation. Every
5
5
  * account on an app derives under one shared descriptor, so honoring that salt stores a
6
- * verifier hash nothing can match — the owner sets a new password, the rotation logs him
6
+ * verifier hash nothing can match — the owner sets a new password, the rotation logs them
7
7
  * out, and neither the old nor the new one opens the app. The server refuses that shape with
8
8
  * `409 stale-client`; this page is what replaces the client that sent it.
9
9
  *
@@ -1,11 +1,11 @@
1
1
  /**
2
- * The ACCOUNTS page — where the owner changes his master password.
2
+ * The ACCOUNTS page — where the owner changes their master password.
3
3
  *
4
4
  * ── Why it is here and not in the app ───────────────────────────────────────
5
5
  * `cursedbelt@2.5.1`'s React `MasterLockMenuItems` dialog minted a FRESH `kdf` salt on every
6
6
  * rotation, which is what the wire looked like before accounts existed. Every account on an
7
7
  * app now derives under ONE shared descriptor, so honoring that salt would store a verifier
8
- * hash nothing could ever match: the owner sets a new password, the rotation logs him out,
8
+ * hash nothing could ever match: the owner sets a new password, the rotation logs them out,
9
9
  * and neither the old nor the new one opens the app. `guard.ts` therefore answers that shape
10
10
  * `409 {"error":"stale-client"}`. That is the safe failure; this page is the fix.
11
11
  *
@@ -8,7 +8,7 @@
8
8
  * six before the first wait. On `collections`' Worker it was worse: the count lived in a JSON
9
9
  * blob snapshotted per request, so the twelve each wrote `failures + 1` over one another and
10
10
  * the burst was RECORDED as one guess. The same blob holds the open unlocks, so a failed guess
11
- * settling after the owner's unlock could also drop his fresh session.
11
+ * settling after the owner's unlock could also drop their fresh session.
12
12
  *
13
13
  * `charge` is the one operation that closes it: check the window AND, when it is open, charge
14
14
  * a failure in the same step, before anything slow happens. A correct verifier then `clear`s
@@ -29,9 +29,9 @@ import { createMemoryMasterLockStore } from "./store.js";
29
29
  * the one page whose entire job is to take a master password with nothing else on it. The
30
30
  * wall's {@link MASTER_LOCK_CSP} refused the tag, correctly, and the refusal printed in the
31
31
  * console. The owner read that console during an outage on 2026-09-17 and it was the first
32
- * line he saw.
32
+ * line the owner saw.
33
33
  *
34
- * Three ways out were put to him. He took none of them as written (2026-09-18):
34
+ * Three ways out were put to the owner. The owner took none of them as written (2026-09-18):
35
35
  *
36
36
  * > *"do your recommendation with option c but not on the wall. If we can stop the console
37
37
  * > error that would be good since we are purposely blocking on that page and it makes it look
@@ -54,7 +54,7 @@ import { createMemoryMasterLockStore } from "./store.js";
54
54
  *
55
55
  * ## What would turn this red
56
56
  *
57
- * · somebody adds `static.cloudflareinsights.com` to {@link MASTER_LOCK_CSP} — the thing he
57
+ * · somebody adds `static.cloudflareinsights.com` to {@link MASTER_LOCK_CSP} — the thing the owner
58
58
  * refused;
59
59
  * · somebody pastes the analytics tag into `lockPage.ts` or `accountsPage.ts`;
60
60
  * · the wall starts serving any cross-origin script its own policy would refuse, beacon or
@@ -159,7 +159,7 @@ describe("while locked", () => {
159
159
  test("🔴 a rotation carrying a STALE kdf is refused, not silently honored", async () => {
160
160
  // The pre-accounts wire minted a fresh salt on every rotation. Every account now
161
161
  // derives under one shared descriptor, so honoring that would store a verifier hash
162
- // nothing can ever match — the owner sets a new password, the rotation logs him out,
162
+ // nothing can ever match — the owner sets a new password, the rotation logs them out,
163
163
  // and neither the old nor the new one opens the app. `409 stale-client` is the one
164
164
  // refusal here that is deliberately distinguishable from a wrong password.
165
165
  const { guard } = build();
@@ -261,7 +261,7 @@ export class MasterLock {
261
261
  * App-wide, one unlock was right: one person, one site, and the owner's *"entering it
262
262
  * in one site will only apply it to that site"*. Per person it is wrong twice over.
263
263
  * The owner asked to *"put in prompts for the orch and monitor it from our phones and
264
- * other devices"* — so the desk and the phone are both his, both open at once, and a
264
+ * other devices"* — so the desk and the phone are both the owner's, both open at once, and a
265
265
  * single-token model would have each unlock silently evict the other. And the guard
266
266
  * now demands a PRESENTED token rather than reading `unlocked` (see the guard's own
267
267
  * note), which is only possible if every device that typed the password holds one.
@@ -639,7 +639,7 @@ export class MasterLock {
639
639
  /**
640
640
  * Add ANOTHER master password, and with it another tenant.
641
641
  *
642
- * The owner's model, in his words: *"The master passwords represent their own tenants
642
+ * The owner's model, in their words: *"The master passwords represent their own tenants
643
643
  * or you can think of it as a fully separate account even though I am the user in both
644
644
  * cases … If I login with a master password I only see the files I added or removed
645
645
  * with that login."*
@@ -1054,7 +1054,7 @@ function readAccount(value: unknown): MasterLockAccount | null {
1054
1054
  }
1055
1055
 
1056
1056
  /** `acct-1`, `acct-2`, … — the lowest number no live account already holds. Sequential
1057
- * because the owner reads these ids out of his own vault and types them into a terminal;
1057
+ * because the owner reads these ids out of their own vault and types them into a terminal;
1058
1058
  * a random id would be one more thing to write down. */
1059
1059
  function nextAccountId(accounts: readonly MasterLockAccount[]): string {
1060
1060
  const taken = new Set(accounts.map((account) => account.id));
@@ -143,8 +143,8 @@ describe("🔴 claim 1b — the unlock is a TOKEN, never ambient state", () => {
143
143
  /**
144
144
  * Measured while building this, and the reason the guard no longer reads `unlocked` in
145
145
  * per-person mode: with `lock.unlocked ||` in front of the token check, the owner
146
- * enrolling in one browser opened the app for every OTHER browser signed in as him —
147
- * his phone rode his desk's unlock, and an invented `x-master-lock` value was accepted
146
+ * enrolling in one browser opened the app for every OTHER browser signed in as the owner —
147
+ * the owner's phone rode their desk's unlock, and an invented `x-master-lock` value was accepted
148
148
  * because nothing ever looked at it. Both tests below fail if that `||` comes back.
149
149
  */
150
150
  test("a second device signed in as the SAME person still has to type it", async () => {
@@ -11,7 +11,7 @@
11
11
  * *"I would pick one that only I know and use and she could pick one for only she knows
12
12
  * just like we did for the vault app."* Two people cannot share one record — the first to
13
13
  * rotate it locks the second out of an app they were granted — and they must not share one
14
- * unlock either, or her phone opens his desk.
14
+ * unlock either, or her phone opens the owner's desk.
15
15
  *
16
16
  * So the unit here is the PERSON. Each principal gets its own {@link MasterLock}: its own
17
17
  * KDF salt, its own `argon2id(verifier)`, its own idle clock, its own guess throttle and
@@ -30,6 +30,7 @@
30
30
  *
31
31
  * 🔴 Worker-safe: nothing here imports `bun:*`.
32
32
  */
33
+ import { IDLE_AFTER_MS } from "cursedops/idle-window";
33
34
  import type { D1LikeDatabase } from "../d1/types.js";
34
35
 
35
36
  /** One row per name: a presence mark (`reader:<tenant>`) or a tick's last run (`tick:<name>`). */
@@ -38,8 +39,8 @@ export const PRESENCE_DDL = ["CREATE TABLE IF NOT EXISTS presence_marks (name TE
38
39
  /** How often one isolate writes the same presence mark. Presence is an hours-scale question. */
39
40
  export const PRESENCE_THROTTLE_MS = 5 * 60_000;
40
41
 
41
- /** The default "nobody is here" line: no use for this long and scheduled work stands down. */
42
- export const IDLE_AFTER_MS = 3 * 60 * 60_000;
42
+ /** The default "nobody is here" line: no use for this long and scheduled work stands down. cursedops' one home (forge task 5853). */
43
+ export { IDLE_AFTER_MS };
43
44
 
44
45
  const TICK_PREFIX = "tick:";
45
46
 
@@ -1,6 +1,6 @@
1
1
  /** `cursedbelt-server/request-scope` — the request scope a once-per-isolate route tier reads (forge tasks 5632, photos per-request floor). */
2
2
  import { describe, expect, it } from "bun:test";
3
- import { createRequestScope } from "./requestScope";
3
+ import { cacheByBindings, createRequestScope, isolateKey } from "./requestScope";
4
4
 
5
5
  describe("🔴 a request scope never hands one request another's value", () => {
6
6
  interface Scope {
@@ -36,3 +36,17 @@ describe("🔴 a request scope never hands one request another's value", () => {
36
36
  expect(() => store.whose()).toThrow(/outside a request/);
37
37
  });
38
38
  });
39
+
40
+ describe("isolateKey / cacheByBindings — build once per env", () => {
41
+ it("the same env builds once; a changed string or a swapped binding builds afresh", () => {
42
+ const d1 = { prepare() {} };
43
+ let builds = 0;
44
+ const graph = cacheByBindings((env: { DB: object; SECRET: string }) => ({ n: ++builds, env }));
45
+ const env = { DB: d1, SECRET: "a" };
46
+ expect(graph(env).n).toBe(1);
47
+ expect(graph({ SECRET: "a", DB: d1 }).n).toBe(1);
48
+ expect(graph({ DB: d1, SECRET: "b" }).n).toBe(2);
49
+ expect(graph({ DB: { prepare() {} }, SECRET: "b" }).n).toBe(3);
50
+ expect(isolateKey({ DB: d1 })).toBe(isolateKey({ DB: d1 }));
51
+ });
52
+ });
@@ -72,3 +72,38 @@ export function createRequestScope<S extends object>(name: string): RequestScope
72
72
  lateOf((scope) => scope[key] as (NonNullable<S[K]> & object) | undefined, String(key)) as NonNullable<S[K]>;
73
73
  return { run: (scope, work) => storage.run(scope, work), current, late, lateOf };
74
74
  }
75
+
76
+ /** A stable id per binding object, so a key changes when a binding is swapped, not only when a string is. */
77
+ const bindingIds = new WeakMap<object, number>();
78
+ let lastBindingId = 0;
79
+ function bindingId(value: object): number {
80
+ let id = bindingIds.get(value);
81
+ if (id === undefined) {
82
+ lastBindingId += 1;
83
+ id = lastBindingId;
84
+ bindingIds.set(value, id);
85
+ }
86
+ return id;
87
+ }
88
+
89
+ /**
90
+ * What an isolate-built graph is keyed by: every binding by name — a string by value, an object (a D1, a
91
+ * service binding) by identity. A changed secret or a swapped binding builds afresh; the same env never does.
92
+ * photos and collections each held a copy of this until forge task 5830.
93
+ */
94
+ export function isolateKey(env: object): string {
95
+ const entries = Object.entries(env).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
96
+ return JSON.stringify(entries.map(([name, value]) => [name, value !== null && (typeof value === "object" || typeof value === "function") ? bindingId(value) : value]));
97
+ }
98
+
99
+ /** `build(env)` once per {@link isolateKey}: the same env answers the same value, a changed one builds afresh. */
100
+ export function cacheByBindings<E extends object, T>(build: (env: E) => T): (env: E) => T {
101
+ let built: { key: string; value: T } | null = null;
102
+ return (env) => {
103
+ const key = isolateKey(env);
104
+ if (built?.key === key) return built.value;
105
+ const value = build(env);
106
+ built = { key, value };
107
+ return value;
108
+ };
109
+ }
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * ── Why server-side at all ──────────────────────────────────────────────────
6
6
  * `localStorage` would be one line and would answer the wrong question. The
7
- * owner's report is about coming BACK to an app, and he does that from another
7
+ * owner's report is about coming BACK to an app, and the owner does that from another
8
8
  * device — the 2026-08-13 music sign-in from a second laptop is the same shape.
9
9
  * A place remembered only in the browser that saw it is not remembered.
10
10
  *
@@ -12,7 +12,7 @@
12
12
  * `verify:scoped` only typechecks the workspaces its own diff touches, which is
13
13
  * exactly the property that makes it fast.
14
14
  *
15
- * The owner's rule, in his words: *"a change to roms for example cannot block
15
+ * The owner's rule, in their words: *"a change to roms for example cannot block
16
16
  * family tree app from passing its tests."* This is that rule for the one
17
17
  * interface twenty-two suites all fake by hand.
18
18
  *
@@ -58,7 +58,7 @@ export interface SyncStatusReporter {
58
58
  * genuinely not attempted anything, and a restored `consecutiveFailures: 3` would be
59
59
  * a claim about failures that did not happen. But "a peer authenticated here" is
60
60
  * something that happened to the database, and forgetting it produced the owner's
61
- * 2026-08-16 report — prod saying *"Awaiting first sync"* over a vault he had used
61
+ * 2026-08-16 report — prod saying *"Awaiting first sync"* over a vault the owner had used
62
62
  * for weeks. The badge's own words are "your Mac has not dialed since this server
63
63
  * last restarted", which is true and which nobody reads as anything but "this has
64
64
  * never synced". A receiver restarts on every deploy, and the dialer runs every 5
@@ -120,7 +120,7 @@ export interface YtDownload {
120
120
  dispose(): Promise<void>;
121
121
  }
122
122
 
123
- /** What one call may add to the profile: a site that needs the owner's sign-in gets his cookies. */
123
+ /** What one call may add to the profile: a site that needs the owner's sign-in gets their cookies. */
124
124
  interface YtDlpCallOptions {
125
125
  /** A Netscape-format cookie jar yt-dlp reads (`--cookies`). The caller owns and removes it. */
126
126
  cookiesFile?: string;