cursedauth 0.12.1 → 0.14.0
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/README.md +1 -0
- package/dist/d1Stores.d.ts +21 -0
- package/dist/d1Stores.d.ts.map +1 -1
- package/dist/d1Stores.js +48 -4
- package/dist/d1Stores.js.map +1 -1
- package/dist/fleetCall.d.ts +16 -0
- package/dist/fleetCall.d.ts.map +1 -0
- package/dist/fleetCall.js +46 -0
- package/dist/fleetCall.js.map +1 -0
- package/package.json +7 -1
- package/src/d1Stores.ts +68 -1
- package/src/fleetCall.ts +54 -0
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ logins and nothing else. The provider is a separate service reached over HTTPS a
|
|
|
48
48
|
| `cursedauth/sso` | both | the grant: its wire shape, its claims, its verification |
|
|
49
49
|
| `cursedauth/session-claims` | server | the bearer-token half: `SESSION_CLAIMS_KEY`, `SessionClaims`, `createRequireSession`. **Worker-safe** — reaches no `bun:sqlite` |
|
|
50
50
|
| `cursedauth/constant-time` | both | `constantTimeEqual` — compare two secrets without leaking where they differ |
|
|
51
|
+
| `cursedauth/fleet-call` | both | `parseFleetUser`, `fleetBearerMatches`, `fleetCallHeaders` — one fleet app asking another over a Service Binding as the signed-in account (`x-fleet-user`) |
|
|
51
52
|
| `cursedauth/secure-request` | both | `isSecureRequest` — whether a request arrived over HTTPS (reading `x-forwarded-proto` behind the tunnel), the one question the cookie `Secure` attribute turns on. Published 0.9.3 because `patterns` and `auth` each carried a copy (task 2106) |
|
|
52
53
|
| `cursedauth/offline-grant` | both | the bounded offline window: how long a device with no network may keep using cached content |
|
|
53
54
|
| `cursedauth/relay-door` | server | a relay app's door on its Worker (`cursedops/relay`): the SSO consumer over D1, the account signed into `<app>_as` for the Mac, the browser's session cookies stripped, the door's schema and the cutover's session import. Lifted from `apps/roms` in 0.10.0. **Worker-safe** |
|
package/dist/d1Stores.d.ts
CHANGED
|
@@ -135,11 +135,32 @@ export declare class D1SnapshotTooLargeError extends RangeError {
|
|
|
135
135
|
readonly table: string;
|
|
136
136
|
constructor(table: string, max: number);
|
|
137
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* How long a snapshot read may go unanswered before a second, identical one is started.
|
|
140
|
+
*
|
|
141
|
+
* A warm read answers in tens of milliseconds. The first D1 read from a cold isolate has
|
|
142
|
+
* stalled 9.5–26 s on a few milliseconds of CPU while the database itself answered every
|
|
143
|
+
* query it received in under 30 ms (forge tasks 3744, 3745, auth at colo DFW) — so the wait
|
|
144
|
+
* is in reaching D1, not in the query, and a second attempt may take a path that is not stuck.
|
|
145
|
+
*/
|
|
146
|
+
export declare const SNAPSHOT_HEDGE_AFTER_MS = 1500;
|
|
147
|
+
/** What {@link D1StoreOptions.onHedge} is told once a hedged snapshot has answered. */
|
|
148
|
+
export interface SnapshotHedge {
|
|
149
|
+
table: string;
|
|
150
|
+
/** Which read answered: the original, or the one started at `hedgeAfterMs`. */
|
|
151
|
+
answeredBy: "first" | "hedge";
|
|
152
|
+
/** From the first read's start to the answer. */
|
|
153
|
+
ms: number;
|
|
154
|
+
}
|
|
138
155
|
export interface D1StoreOptions {
|
|
139
156
|
/** The invocation's clock, in ms. One value for the whole request. */
|
|
140
157
|
now: number;
|
|
141
158
|
/** Override {@link SNAPSHOT_MAX_ROWS} — lower it in a test that proves the refusal. */
|
|
142
159
|
maxRows?: number;
|
|
160
|
+
/** Override {@link SNAPSHOT_HEDGE_AFTER_MS}; `Infinity` never hedges. */
|
|
161
|
+
hedgeAfterMs?: number;
|
|
162
|
+
/** Told which read answered, whenever a snapshot was hedged — what names a stall in the logs. */
|
|
163
|
+
onHedge?: (hedge: SnapshotHedge) => void;
|
|
143
164
|
}
|
|
144
165
|
/**
|
|
145
166
|
* The `sessions` table for one invocation. Hand the result to
|
package/dist/d1Stores.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"d1Stores.d.ts","sourceRoot":"","sources":["../src/d1Stores.ts"],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;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,iBAAiB,OAAQ,CAAC;AAEvC,qBAAa,uBAAwB,SAAQ,UAAU;IACtD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;gBACX,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;CAUtC;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,uBAAuB,OAAQ,CAAC;AAE7C,uFAAuF;AACvF,MAAM,WAAW,aAAa;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,+EAA+E;IAC/E,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC;IAC9B,iDAAiD;IACjD,EAAE,EAAE,MAAM,CAAC;CACX;AAoFD,MAAM,WAAW,cAAc;IAC9B,sEAAsE;IACtE,GAAG,EAAE,MAAM,CAAC;IACZ,uFAAuF;IACvF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iGAAiG;IACjG,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;CACzC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,mBAAmB,CACxC,EAAE,EAAE,QAAQ,EACZ,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,cAAc,GACrB,OAAO,CAAC,WAAW,CAAC,CAmFtB;AAKD;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CACzC,EAAE,EAAE,QAAQ,EACZ,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,cAAc,GACrB,OAAO,CAAC,YAAY,CAAC,CAsHvB"}
|
package/dist/d1Stores.js
CHANGED
|
@@ -104,11 +104,55 @@ export class D1SnapshotTooLargeError extends RangeError {
|
|
|
104
104
|
this.table = table;
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* How long a snapshot read may go unanswered before a second, identical one is started.
|
|
109
|
+
*
|
|
110
|
+
* A warm read answers in tens of milliseconds. The first D1 read from a cold isolate has
|
|
111
|
+
* stalled 9.5–26 s on a few milliseconds of CPU while the database itself answered every
|
|
112
|
+
* query it received in under 30 ms (forge tasks 3744, 3745, auth at colo DFW) — so the wait
|
|
113
|
+
* is in reaching D1, not in the query, and a second attempt may take a path that is not stuck.
|
|
114
|
+
*/
|
|
115
|
+
export const SNAPSHOT_HEDGE_AFTER_MS = 1_500;
|
|
116
|
+
/**
|
|
117
|
+
* Run `read`, and if it has not settled after `afterMs`, run it again and take whichever
|
|
118
|
+
* SUCCEEDS first. Safe only because a snapshot is a pure read — never hedge a write.
|
|
119
|
+
*
|
|
120
|
+
* A first read that FAILS before the hedge fires fails at once: a missing table or a
|
|
121
|
+
* refused snapshot is an answer, not a stall, and must surface by name rather than twice.
|
|
122
|
+
* Once both are running, one failing waits for the other; both failing throws the first's.
|
|
123
|
+
*/
|
|
124
|
+
async function hedged(read, afterMs, onHedge) {
|
|
125
|
+
const started = Date.now();
|
|
126
|
+
const first = read();
|
|
127
|
+
if (!Number.isFinite(afterMs))
|
|
128
|
+
return first;
|
|
129
|
+
let timer;
|
|
130
|
+
const fired = new Promise((resolve) => {
|
|
131
|
+
timer = setTimeout(() => resolve("fired"), afterMs);
|
|
132
|
+
});
|
|
133
|
+
try {
|
|
134
|
+
if ((await Promise.race([first.then(() => "first"), fired])) === "first")
|
|
135
|
+
return await first;
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
}
|
|
140
|
+
const tagged = (promise, by) => promise.then((value) => {
|
|
141
|
+
onHedge?.(by, Date.now() - started);
|
|
142
|
+
return value;
|
|
143
|
+
});
|
|
144
|
+
try {
|
|
145
|
+
return await Promise.any([tagged(first, "first"), tagged(read(), "hedge")]);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
throw error instanceof AggregateError ? error.errors[0] : error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
107
151
|
/** One read, bounded, that refuses rather than truncates. */
|
|
108
|
-
async function snapshot(db, table, sql, binds, max) {
|
|
152
|
+
async function snapshot(db, table, sql, binds, max, options) {
|
|
109
153
|
// `max + 1` is what turns "we got exactly the limit" from ambiguous into decisive: a
|
|
110
154
|
// full page could mean a full table or a cut one, and one extra row says which.
|
|
111
|
-
const rows = (await db.prepare(`${sql} LIMIT ?`).bind(...binds, max + 1).all()).results;
|
|
155
|
+
const rows = await hedged(async () => (await db.prepare(`${sql} LIMIT ?`).bind(...binds, max + 1).all()).results, options.hedgeAfterMs ?? SNAPSHOT_HEDGE_AFTER_MS, options.onHedge && ((answeredBy, ms) => options.onHedge?.({ table, answeredBy, ms })));
|
|
112
156
|
if (rows.length > max)
|
|
113
157
|
throw new D1SnapshotTooLargeError(table, max);
|
|
114
158
|
return rows;
|
|
@@ -144,7 +188,7 @@ const normalizeSessionRow = (row) => ({
|
|
|
144
188
|
*/
|
|
145
189
|
export async function createD1SessionRows(db, writes, options) {
|
|
146
190
|
const max = options.maxRows ?? SNAPSHOT_MAX_ROWS;
|
|
147
|
-
const rows = await snapshot(db, "sessions", `SELECT ${SESSION_COLUMNS} FROM sessions WHERE expires_at > ?`, [options.now], max);
|
|
191
|
+
const rows = await snapshot(db, "sessions", `SELECT ${SESSION_COLUMNS} FROM sessions WHERE expires_at > ?`, [options.now], max, options);
|
|
148
192
|
const live = new Map();
|
|
149
193
|
for (const row of rows)
|
|
150
194
|
live.set(row.id, normalizeSessionRow(row));
|
|
@@ -215,7 +259,7 @@ const SSO_USER_COLUMNS = "id, email, name, is_admin, is_owner, is_granted, last_
|
|
|
215
259
|
*/
|
|
216
260
|
export async function createD1SsoUserStore(db, writes, options) {
|
|
217
261
|
const max = options.maxRows ?? SNAPSHOT_MAX_ROWS;
|
|
218
|
-
const rows = await snapshot(db, "sso_users", `SELECT ${SSO_USER_COLUMNS} FROM sso_users`, [], max);
|
|
262
|
+
const rows = await snapshot(db, "sso_users", `SELECT ${SSO_USER_COLUMNS} FROM sso_users`, [], max, options);
|
|
219
263
|
const users = new Map();
|
|
220
264
|
for (const row of rows)
|
|
221
265
|
users.set(row.id, normalizeSsoUserRow(row));
|
package/dist/d1Stores.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"d1Stores.js","sourceRoot":"","sources":["../src/d1Stores.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,OAAO,EACN,mBAAmB,GAInB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAoD9C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB;IAClC,MAAM,OAAO,GAAuB,EAAE,CAAC;IACvC,OAAO;QACN,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;QAC5C,KAAK,CAAC,MAAM;YACX,4EAA4E;YAC5E,iFAAiF;YACjF,6BAA6B;YAC7B,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC5B,CAAC;KACD,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAEvC,MAAM,OAAO,uBAAwB,SAAQ,UAAU;IAC7C,KAAK,CAAS;IACvB,YAAY,KAAa,EAAE,GAAW;QACrC,KAAK,CACJ,mBAAmB,KAAK,0BAA0B,GAAG,+BAA+B;YACnF,qFAAqF;YACrF,sFAAsF;YACtF,2CAA2C,CAC5C,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD;AAED,6DAA6D;AAC7D,KAAK,UAAU,QAAQ,CACtB,EAAY,EACZ,KAAa,EACb,G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AC,GAAG,EAAE,GAAG,CAAC,CAAC;gBACpB,CAAC;YACF,CAAC;YACD,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;YACnE,KAAK,MAAM,EAAE,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;gBAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;oBAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YAC7E,MAAM,CAAC,GAAG,CACT,EAAE;iBACA,OAAO,CACP;;;;;;;;;QASE,CACF;iBACA,GAAG,EAAE,CACP,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The asking account, JSON — `{ id, email, isOwner?, isAdmin? }`. */
|
|
2
|
+
export declare const FLEET_USER_HEADER = "x-fleet-user";
|
|
3
|
+
/** The account a fleet call names. `email` is lowercased; the flags are `true` or `false`, never absent. */
|
|
4
|
+
export interface FleetUser {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
isOwner: boolean;
|
|
8
|
+
isAdmin: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** The `x-fleet-user` value as an account, or null for anything that is not one — never throws. */
|
|
11
|
+
export declare function parseFleetUser(header: string | null | undefined): FleetUser | null;
|
|
12
|
+
/** Whether the request's `authorization: Bearer …` is `token` — false when either is empty. Constant time. */
|
|
13
|
+
export declare function fleetBearerMatches(authorization: string | null | undefined, token: string | null | undefined): boolean;
|
|
14
|
+
/** The caller's headers: the bearer, the asking account, and JSON wanted back. */
|
|
15
|
+
export declare function fleetCallHeaders(token: string, user: FleetUser): Record<string, string>;
|
|
16
|
+
//# sourceMappingURL=fleetCall.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fleetCall.d.ts","sourceRoot":"","sources":["../src/fleetCall.ts"],"names":[],"mappings":"AAqBA,sEAAsE;AACtE,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,4GAA4G;AAC5G,MAAM,WAAW,SAAS;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,mGAAmG;AACnG,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,SAAS,GAAG,IAAI,CASlF;AAED,8GAA8G;AAC9G,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAGtH;AAED,kFAAkF;AAClF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEvF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One fleet app asking another, over a Service Binding, AS the signed-in account —
|
|
3
|
+
* `cursedauth/fleet-call`.
|
|
4
|
+
*
|
|
5
|
+
* The caller proves itself with a bearer the two apps share (a secret per route, put from each
|
|
6
|
+
* app's own secrets file) and names whose eyes the read uses in `x-fleet-user`, JSON
|
|
7
|
+
* `{ id, email, isOwner?, isAdmin? }`. The callee checks the bearer in constant time, parses the
|
|
8
|
+
* account, and answers through its OWN visibility rules for that account — a relative who cannot
|
|
9
|
+
* see a private branch in one app cannot find it from the other.
|
|
10
|
+
*
|
|
11
|
+
* ## Why it is here (2026-10-27, forge task cursedforge-tasks-todo)
|
|
12
|
+
*
|
|
13
|
+
* family↔photos is the first pair to call each other both ways (photos' tree picker reads
|
|
14
|
+
* family's people; family's dossier reads photos' pictures of a person), and the second direction
|
|
15
|
+
* had begun as a copy of the first's header name and parser — `tools/check-copies.ts` caught the
|
|
16
|
+
* constant. The parser is the part that matters: it decides who the reader is, so two copies
|
|
17
|
+
* that drift is a read made with the wrong eyes. Pure functions, no node builtin: a Worker and a
|
|
18
|
+
* Mac host both import it.
|
|
19
|
+
*/
|
|
20
|
+
import { constantTimeEqual } from "./constantTime.js";
|
|
21
|
+
/** The asking account, JSON — `{ id, email, isOwner?, isAdmin? }`. */
|
|
22
|
+
export const FLEET_USER_HEADER = "x-fleet-user";
|
|
23
|
+
/** The `x-fleet-user` value as an account, or null for anything that is not one — never throws. */
|
|
24
|
+
export function parseFleetUser(header) {
|
|
25
|
+
if (!header)
|
|
26
|
+
return null;
|
|
27
|
+
try {
|
|
28
|
+
const v = JSON.parse(header);
|
|
29
|
+
if (!v || typeof v.id !== "string" || !v.id.trim() || typeof v.email !== "string")
|
|
30
|
+
return null;
|
|
31
|
+
return { id: v.id.trim(), email: v.email.trim().toLowerCase(), isOwner: v.isOwner === true, isAdmin: v.isAdmin === true };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Whether the request's `authorization: Bearer …` is `token` — false when either is empty. Constant time. */
|
|
38
|
+
export function fleetBearerMatches(authorization, token) {
|
|
39
|
+
const presented = (authorization ?? "").replace(/^Bearer\s+/i, "").trim();
|
|
40
|
+
return Boolean(presented && token) && constantTimeEqual(presented, token);
|
|
41
|
+
}
|
|
42
|
+
/** The caller's headers: the bearer, the asking account, and JSON wanted back. */
|
|
43
|
+
export function fleetCallHeaders(token, user) {
|
|
44
|
+
return { authorization: `Bearer ${token}`, [FLEET_USER_HEADER]: JSON.stringify(user), accept: "application/json" };
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=fleetCall.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fleetCall.js","sourceRoot":"","sources":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AE,IAAe;IAC9D,OAAO,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;AACpH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedauth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "The app side of fleet SSO — sign in through the accounts service, keep a local snapshot of who signed in, and gate the app on it. Bun + Hono + React, one SQLite file per app.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -101,6 +101,12 @@
|
|
|
101
101
|
"source": "./src/constantTime.ts",
|
|
102
102
|
"import": "./dist/constantTime.js"
|
|
103
103
|
},
|
|
104
|
+
"./fleet-call": {
|
|
105
|
+
"types": "./dist/fleetCall.d.ts",
|
|
106
|
+
"bun": "./src/fleetCall.ts",
|
|
107
|
+
"source": "./src/fleetCall.ts",
|
|
108
|
+
"import": "./dist/fleetCall.js"
|
|
109
|
+
},
|
|
104
110
|
"./secure-request": {
|
|
105
111
|
"types": "./dist/secureRequest.d.ts",
|
|
106
112
|
"bun": "./src/secureRequest.ts",
|
package/src/d1Stores.ts
CHANGED
|
@@ -167,6 +167,62 @@ export class D1SnapshotTooLargeError extends RangeError {
|
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
/**
|
|
171
|
+
* How long a snapshot read may go unanswered before a second, identical one is started.
|
|
172
|
+
*
|
|
173
|
+
* A warm read answers in tens of milliseconds. The first D1 read from a cold isolate has
|
|
174
|
+
* stalled 9.5–26 s on a few milliseconds of CPU while the database itself answered every
|
|
175
|
+
* query it received in under 30 ms (forge tasks 3744, 3745, auth at colo DFW) — so the wait
|
|
176
|
+
* is in reaching D1, not in the query, and a second attempt may take a path that is not stuck.
|
|
177
|
+
*/
|
|
178
|
+
export const SNAPSHOT_HEDGE_AFTER_MS = 1_500;
|
|
179
|
+
|
|
180
|
+
/** What {@link D1StoreOptions.onHedge} is told once a hedged snapshot has answered. */
|
|
181
|
+
export interface SnapshotHedge {
|
|
182
|
+
table: string;
|
|
183
|
+
/** Which read answered: the original, or the one started at `hedgeAfterMs`. */
|
|
184
|
+
answeredBy: "first" | "hedge";
|
|
185
|
+
/** From the first read's start to the answer. */
|
|
186
|
+
ms: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Run `read`, and if it has not settled after `afterMs`, run it again and take whichever
|
|
191
|
+
* SUCCEEDS first. Safe only because a snapshot is a pure read — never hedge a write.
|
|
192
|
+
*
|
|
193
|
+
* A first read that FAILS before the hedge fires fails at once: a missing table or a
|
|
194
|
+
* refused snapshot is an answer, not a stall, and must surface by name rather than twice.
|
|
195
|
+
* Once both are running, one failing waits for the other; both failing throws the first's.
|
|
196
|
+
*/
|
|
197
|
+
async function hedged<T>(
|
|
198
|
+
read: () => Promise<T>,
|
|
199
|
+
afterMs: number,
|
|
200
|
+
onHedge: ((answeredBy: "first" | "hedge", ms: number) => void) | undefined,
|
|
201
|
+
): Promise<T> {
|
|
202
|
+
const started = Date.now();
|
|
203
|
+
const first = read();
|
|
204
|
+
if (!Number.isFinite(afterMs)) return first;
|
|
205
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
206
|
+
const fired = new Promise<"fired">((resolve) => {
|
|
207
|
+
timer = setTimeout(() => resolve("fired"), afterMs);
|
|
208
|
+
});
|
|
209
|
+
try {
|
|
210
|
+
if ((await Promise.race([first.then(() => "first" as const), fired])) === "first") return await first;
|
|
211
|
+
} finally {
|
|
212
|
+
clearTimeout(timer);
|
|
213
|
+
}
|
|
214
|
+
const tagged = (promise: Promise<T>, by: "first" | "hedge") =>
|
|
215
|
+
promise.then((value) => {
|
|
216
|
+
onHedge?.(by, Date.now() - started);
|
|
217
|
+
return value;
|
|
218
|
+
});
|
|
219
|
+
try {
|
|
220
|
+
return await Promise.any([tagged(first, "first"), tagged(read(), "hedge")]);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
throw error instanceof AggregateError ? error.errors[0] : error;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
170
226
|
/** One read, bounded, that refuses rather than truncates. */
|
|
171
227
|
async function snapshot<T>(
|
|
172
228
|
db: D1Handle,
|
|
@@ -174,10 +230,15 @@ async function snapshot<T>(
|
|
|
174
230
|
sql: string,
|
|
175
231
|
binds: (string | number | null)[],
|
|
176
232
|
max: number,
|
|
233
|
+
options: D1StoreOptions,
|
|
177
234
|
): Promise<T[]> {
|
|
178
235
|
// `max + 1` is what turns "we got exactly the limit" from ambiguous into decisive: a
|
|
179
236
|
// full page could mean a full table or a cut one, and one extra row says which.
|
|
180
|
-
const rows =
|
|
237
|
+
const rows = await hedged(
|
|
238
|
+
async () => (await db.prepare(`${sql} LIMIT ?`).bind(...binds, max + 1).all<T>()).results,
|
|
239
|
+
options.hedgeAfterMs ?? SNAPSHOT_HEDGE_AFTER_MS,
|
|
240
|
+
options.onHedge && ((answeredBy, ms) => options.onHedge?.({ table, answeredBy, ms })),
|
|
241
|
+
);
|
|
181
242
|
if (rows.length > max) throw new D1SnapshotTooLargeError(table, max);
|
|
182
243
|
return rows;
|
|
183
244
|
}
|
|
@@ -212,6 +273,10 @@ export interface D1StoreOptions {
|
|
|
212
273
|
now: number;
|
|
213
274
|
/** Override {@link SNAPSHOT_MAX_ROWS} — lower it in a test that proves the refusal. */
|
|
214
275
|
maxRows?: number;
|
|
276
|
+
/** Override {@link SNAPSHOT_HEDGE_AFTER_MS}; `Infinity` never hedges. */
|
|
277
|
+
hedgeAfterMs?: number;
|
|
278
|
+
/** Told which read answered, whenever a snapshot was hedged — what names a stall in the logs. */
|
|
279
|
+
onHedge?: (hedge: SnapshotHedge) => void;
|
|
215
280
|
}
|
|
216
281
|
|
|
217
282
|
/**
|
|
@@ -243,6 +308,7 @@ export async function createD1SessionRows(
|
|
|
243
308
|
`SELECT ${SESSION_COLUMNS} FROM sessions WHERE expires_at > ?`,
|
|
244
309
|
[options.now],
|
|
245
310
|
max,
|
|
311
|
+
options,
|
|
246
312
|
);
|
|
247
313
|
const live = new Map<string, SessionRow>();
|
|
248
314
|
for (const row of rows) live.set(row.id, normalizeSessionRow(row));
|
|
@@ -343,6 +409,7 @@ export async function createD1SsoUserStore(
|
|
|
343
409
|
`SELECT ${SSO_USER_COLUMNS} FROM sso_users`,
|
|
344
410
|
[],
|
|
345
411
|
max,
|
|
412
|
+
options,
|
|
346
413
|
);
|
|
347
414
|
const users = new Map<string, SsoUserRow>();
|
|
348
415
|
for (const row of rows) users.set(row.id, normalizeSsoUserRow(row));
|
package/src/fleetCall.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One fleet app asking another, over a Service Binding, AS the signed-in account —
|
|
3
|
+
* `cursedauth/fleet-call`.
|
|
4
|
+
*
|
|
5
|
+
* The caller proves itself with a bearer the two apps share (a secret per route, put from each
|
|
6
|
+
* app's own secrets file) and names whose eyes the read uses in `x-fleet-user`, JSON
|
|
7
|
+
* `{ id, email, isOwner?, isAdmin? }`. The callee checks the bearer in constant time, parses the
|
|
8
|
+
* account, and answers through its OWN visibility rules for that account — a relative who cannot
|
|
9
|
+
* see a private branch in one app cannot find it from the other.
|
|
10
|
+
*
|
|
11
|
+
* ## Why it is here (2026-10-27, forge task cursedforge-tasks-todo)
|
|
12
|
+
*
|
|
13
|
+
* family↔photos is the first pair to call each other both ways (photos' tree picker reads
|
|
14
|
+
* family's people; family's dossier reads photos' pictures of a person), and the second direction
|
|
15
|
+
* had begun as a copy of the first's header name and parser — `tools/check-copies.ts` caught the
|
|
16
|
+
* constant. The parser is the part that matters: it decides who the reader is, so two copies
|
|
17
|
+
* that drift is a read made with the wrong eyes. Pure functions, no node builtin: a Worker and a
|
|
18
|
+
* Mac host both import it.
|
|
19
|
+
*/
|
|
20
|
+
import { constantTimeEqual } from "./constantTime.js";
|
|
21
|
+
|
|
22
|
+
/** The asking account, JSON — `{ id, email, isOwner?, isAdmin? }`. */
|
|
23
|
+
export const FLEET_USER_HEADER = "x-fleet-user";
|
|
24
|
+
|
|
25
|
+
/** The account a fleet call names. `email` is lowercased; the flags are `true` or `false`, never absent. */
|
|
26
|
+
export interface FleetUser {
|
|
27
|
+
id: string;
|
|
28
|
+
email: string;
|
|
29
|
+
isOwner: boolean;
|
|
30
|
+
isAdmin: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The `x-fleet-user` value as an account, or null for anything that is not one — never throws. */
|
|
34
|
+
export function parseFleetUser(header: string | null | undefined): FleetUser | null {
|
|
35
|
+
if (!header) return null;
|
|
36
|
+
try {
|
|
37
|
+
const v = JSON.parse(header) as Partial<FleetUser> | null;
|
|
38
|
+
if (!v || typeof v.id !== "string" || !v.id.trim() || typeof v.email !== "string") return null;
|
|
39
|
+
return { id: v.id.trim(), email: v.email.trim().toLowerCase(), isOwner: v.isOwner === true, isAdmin: v.isAdmin === true };
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether the request's `authorization: Bearer …` is `token` — false when either is empty. Constant time. */
|
|
46
|
+
export function fleetBearerMatches(authorization: string | null | undefined, token: string | null | undefined): boolean {
|
|
47
|
+
const presented = (authorization ?? "").replace(/^Bearer\s+/i, "").trim();
|
|
48
|
+
return Boolean(presented && token) && constantTimeEqual(presented, token as string);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The caller's headers: the bearer, the asking account, and JSON wanted back. */
|
|
52
|
+
export function fleetCallHeaders(token: string, user: FleetUser): Record<string, string> {
|
|
53
|
+
return { authorization: `Bearer ${token}`, [FLEET_USER_HEADER]: JSON.stringify(user), accept: "application/json" };
|
|
54
|
+
}
|