@milesroxas/design-hub 0.6.1 → 0.7.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.
Files changed (55) hide show
  1. package/README.md +17 -7
  2. package/core/review/model.ts +90 -10
  3. package/core/review/policy.test.ts +62 -0
  4. package/core/review/policy.ts +13 -9
  5. package/core/review/service.test.ts +464 -54
  6. package/core/review/service.ts +168 -36
  7. package/core/review/store.ts +31 -7
  8. package/dist/core/review/model.d.ts +39 -7
  9. package/dist/core/review/model.js +73 -7
  10. package/dist/core/review/policy.d.ts +4 -1
  11. package/dist/core/review/policy.js +4 -4
  12. package/dist/core/review/service.d.ts +24 -6
  13. package/dist/core/review/service.js +155 -36
  14. package/dist/core/review/store.d.ts +15 -5
  15. package/dist/core/review/store.js +27 -6
  16. package/dist/scripts/bootstrap.d.ts +16 -14
  17. package/dist/scripts/bootstrap.js +56 -39
  18. package/dist/scripts/lib/deploy.d.ts +12 -4
  19. package/dist/scripts/lib/deploy.js +47 -13
  20. package/dist/scripts/lib/review.js +3 -3
  21. package/dist/scripts/lib/vercel.d.ts +8 -2
  22. package/dist/scripts/lib/vercel.js +23 -9
  23. package/dist/scripts/people.d.ts +16 -29
  24. package/dist/scripts/people.js +72 -39
  25. package/dist/scripts/preflight.js +2 -2
  26. package/dist/scripts/share.d.ts +2 -3
  27. package/dist/scripts/share.js +18 -10
  28. package/dist/server/api.d.ts +6 -2
  29. package/dist/server/api.js +53 -22
  30. package/dist/server/dev.js +3 -3
  31. package/dist/server/fake-upstash.js +3 -2
  32. package/dist/server/local.d.ts +2 -2
  33. package/dist/server/local.js +6 -4
  34. package/dist/server/session.d.ts +25 -10
  35. package/dist/server/session.js +69 -52
  36. package/dist/server/upstash.js +4 -3
  37. package/dist/vercel/proxy.d.ts +5 -3
  38. package/dist/vercel/proxy.js +183 -157
  39. package/dist/vite/plugin.js +5 -5
  40. package/kit/ui/Menu.tsx +14 -4
  41. package/kit/ui/ui.css +16 -3
  42. package/package.json +2 -2
  43. package/shell/review/Bar.tsx +5 -4
  44. package/shell/review/People.tsx +194 -55
  45. package/shell/review/Review.tsx +9 -8
  46. package/shell/review/ThreadCard.tsx +24 -21
  47. package/shell/review/client.ts +37 -18
  48. package/shell/review/review.css +9 -1
  49. package/skills/miles-deploy/SKILL.md +44 -16
  50. package/skills/miles-design-hub/SKILL.md +5 -5
  51. package/skills/miles-new-feature/SKILL.md +1 -1
  52. package/skills/miles-review/SKILL.md +7 -5
  53. package/skills/miles-worktrees/SKILL.md +1 -1
  54. package/templates/conductor/settings.toml +1 -1
  55. package/templates/guide.md +1 -1
@@ -5,15 +5,16 @@ import { PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin } from
5
5
  import { storeConfig, upstashKV } from './upstash.js';
6
6
  export const API_PATH = '/api/review';
7
7
  const MAX_BODY_BYTES = 64 * 1024;
8
+ const STORE_DOWN = 'The review store did not answer. Try again.';
8
9
  const STATUS = {
9
10
  unauthorized: 401,
10
11
  forbidden: 403,
11
12
  not_found: 404,
12
13
  invalid: 422,
13
14
  };
14
- const json = (status, body) => new Response(JSON.stringify(body), {
15
+ const json = (status, body, extra = {}) => new Response(JSON.stringify(body), {
15
16
  status,
16
- headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
17
+ headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store', ...extra },
17
18
  });
18
19
  /** What the browser may see of a person: everything but the session epoch. */
19
20
  const visible = ({ epoch: _epoch, ...p }) => p;
@@ -32,16 +33,38 @@ async function query(op, params, { review, actor }) {
32
33
  return { comments: await review.listComments(actor, prototypeOf(params.get('prototype'))) };
33
34
  case 'people':
34
35
  return { people: (await review.listPeople(actor)).map(visible) };
36
+ case 'access':
37
+ return { access: await review.defaultAccess(actor) };
35
38
  default:
36
39
  return undefined;
37
40
  }
38
41
  }
39
- async function command(op, b, { review, actor }) {
42
+ const link = (r) => ({
43
+ person: visible(r.person),
44
+ link: `/invite/${r.token}`,
45
+ });
46
+ /** Studio's commands over people, their links and how long they keep access. */
47
+ async function peopleCommand(op, b, { review, actor }) {
48
+ switch (op) {
49
+ case 'people.add':
50
+ return link(await review.addPerson(actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
51
+ case 'people.link':
52
+ return link(await review.newLink(actor, str(b.id)));
53
+ case 'people.signOut':
54
+ return link(await review.signOutEverywhere(actor, str(b.id)));
55
+ case 'people.revoke':
56
+ return { person: visible(await review.revoke(actor, str(b.id))) };
57
+ case 'people.access':
58
+ return { person: visible(await review.setAccess(actor, str(b.id), b.access)) };
59
+ case 'access.set':
60
+ return { access: await review.setDefaultAccess(actor, b.access) };
61
+ default:
62
+ return undefined;
63
+ }
64
+ }
65
+ async function command(op, b, ctx) {
66
+ const { review, actor } = ctx;
40
67
  const at = () => ({ prototype: prototypeOf(b.prototype), id: str(b.id) });
41
- const link = (r) => ({
42
- person: visible(r.person),
43
- link: `/invite/${r.token}`,
44
- });
45
68
  switch (op) {
46
69
  case 'comment.add':
47
70
  return { comment: await review.add(actor, { body: b.body, anchor: b.anchor, states: b.states }) };
@@ -61,14 +84,8 @@ async function command(op, b, { review, actor }) {
61
84
  return { comment: await review.resolve(actor, { ...at(), resolved: b.resolved === true }) };
62
85
  case 'comment.triage':
63
86
  return { comment: await review.retriage(actor, { ...at(), triage: b.triage, states: b.states }) };
64
- case 'people.invite':
65
- return link(await review.invite(actor, { name: b.name, team: b.team, role: b.role }));
66
- case 'people.reissue':
67
- return link(await review.reissue(actor, str(b.id)));
68
- case 'people.revoke':
69
- return { person: visible(await review.revoke(actor, str(b.id))) };
70
87
  default:
71
- return undefined;
88
+ return peopleCommand(op, b, ctx);
72
89
  }
73
90
  }
74
91
  async function readJson(request) {
@@ -110,7 +127,7 @@ export async function handleReview(request, ctx) {
110
127
  if (error instanceof ReviewError)
111
128
  return json(STATUS[error.code], { error: error.message, problems: error.problems });
112
129
  console.error('[review]', error);
113
- return json(500, { error: 'The review store did not answer. Try again.' });
130
+ return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
114
131
  }
115
132
  }
116
133
  /** The person a request's signed cookie names, while they may still sign in. */
@@ -118,8 +135,12 @@ export async function actorFrom(request, review, gate) {
118
135
  const claim = await readPersonToken(gate, readCookie(request, PERSON_COOKIE));
119
136
  return claim ? review.sessionPerson(claim.id, claim.epoch) : null;
120
137
  }
121
- /** The review service as deployed: Upstash for the store, Jev for triage when its key is set and `checks` is given. */
122
- export function deployedReview(env, checks = null) {
138
+ /**
139
+ * The review service as deployed: Upstash for the store, Jev for triage when
140
+ * its key is set and `checks` is given. `now` is the clock, so the gate and
141
+ * the service it asks run on one (the gate's tests move it).
142
+ */
143
+ export function deployedReview(env, checks = null, now) {
123
144
  const gate = readGate(env);
124
145
  if (!gate)
125
146
  return { error: 'The prototype gate is not configured.' };
@@ -128,7 +149,10 @@ export function deployedReview(env, checks = null) {
128
149
  return { error: 'Review is not set up on this site: pnpm bootstrap --vercel <project> --review.' };
129
150
  return {
130
151
  gate,
131
- review: createReview(upstashKV(store), { triage: jevTriager(env.TYPESAFE_API_KEY ?? '', checks) }),
152
+ review: createReview(upstashKV(store), {
153
+ triage: jevTriager(env.TYPESAFE_API_KEY ?? '', checks),
154
+ ...(now ? { now } : {}),
155
+ }),
132
156
  };
133
157
  }
134
158
  /** The Vercel function's whole body; `checks` is the project's checks.json, bundled by its api/review.ts. */
@@ -136,8 +160,15 @@ export async function deployedHandler(request, checks) {
136
160
  const setup = deployedReview(process.env, checks);
137
161
  if ('error' in setup)
138
162
  return json(503, { error: setup.error });
139
- return handleReview(request, {
140
- review: setup.review,
141
- actor: await actorFrom(request, setup.review, setup.gate),
142
- });
163
+ let actor;
164
+ try {
165
+ actor = await actorFrom(request, setup.review, setup.gate);
166
+ }
167
+ catch (error) {
168
+ // The store is down: say so in JSON, as every other answer here is, so the panel can retry.
169
+ console.error('[review]', error);
170
+ // Retry-After tells the panel this passes, unlike a 503 for a site with no store.
171
+ return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
172
+ }
173
+ return handleReview(request, { review: setup.review, actor });
143
174
  }
@@ -5,7 +5,7 @@ import { API_PATH, handleReview } from './api.js';
5
5
  import { checks } from './checks.js';
6
6
  import { readEnv } from './env.js';
7
7
  import { fileKV } from './file-kv.js';
8
- import { LOCAL_STORE, localEditor } from './local.js';
8
+ import { LOCAL_STORE, localStudio } from './local.js';
9
9
  import { ROOT } from './paths.js';
10
10
  // Connect strips the mount path from req.url; originalUrl keeps the query the handler reads.
11
11
  async function toRequest(req) {
@@ -40,8 +40,8 @@ export function devReview() {
40
40
  const review = createReview(kv, {
41
41
  triage: jevTriager(readEnv(ROOT).TYPESAFE_API_KEY ?? '', checks),
42
42
  });
43
- const actor = localEditor();
44
- // The local editor is in the people list like anyone else, so the people panel shows them.
43
+ const actor = localStudio();
44
+ // The local Studio person is in the people list like anyone else, so the people panel shows them.
45
45
  void kv.hset(KEY.people, actor.id, JSON.stringify(actor));
46
46
  server.middlewares.use(API_PATH, (req, res, nextMiddleware) => {
47
47
  toRequest(req)
@@ -12,8 +12,9 @@ export function fakeUpstash() {
12
12
  await kv.hset(key, a, b);
13
13
  return 1;
14
14
  case 'HDEL':
15
- await kv.hdel(key, a);
16
- return 1;
15
+ return kv.hdel(key, a);
16
+ case 'HINCRBY':
17
+ return kv.hincrby(key, a, Number(b));
17
18
  case 'HGETALL':
18
19
  return Object.entries(await kv.hgetall(key)).flat();
19
20
  case 'SADD':
@@ -1,4 +1,4 @@
1
1
  import type { Person } from '../core/review/model.ts';
2
2
  export declare const LOCAL_STORE: string;
3
- /** Whoever runs this machine's dev server or CLI: an editor, because they hold the store itself. */
4
- export declare const localEditor: (id?: string, team?: string) => Person;
3
+ /** Whoever runs this machine's dev server or CLI: Studio, because they hold the store itself. */
4
+ export declare const localStudio: (id?: string, team?: string) => Person;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  // The review store and identity on this machine, shared by the dev server
3
- // and the CLI: .design-hub/review.json, and a local editor named from git.
3
+ // and the CLI: .design-hub/review.json, and a local Studio person named from git.
4
4
  import { execFileSync } from 'node:child_process';
5
5
  import { join } from 'node:path';
6
6
  import { STATE_DIR } from './paths.js';
@@ -13,12 +13,14 @@ function gitName() {
13
13
  return '';
14
14
  }
15
15
  }
16
- /** Whoever runs this machine's dev server or CLI: an editor, because they hold the store itself. */
17
- export const localEditor = (id = 'local', team = 'Local') => ({
16
+ /** Whoever runs this machine's dev server or CLI: Studio, because they hold the store itself. */
17
+ export const localStudio = (id = 'local', team = 'Local') => ({
18
18
  id,
19
19
  name: gitName() || 'You',
20
20
  team,
21
- role: 'editor',
21
+ kind: 'studio',
22
+ email: null,
23
+ accessEndsAt: null,
22
24
  createdAt: new Date(0).toISOString(),
23
25
  revokedAt: null,
24
26
  epoch: 0,
@@ -1,22 +1,37 @@
1
- export declare const GATE_COOKIE = "__Host-hub_gate";
2
1
  export declare const PERSON_COOKIE = "__Host-hub_person";
2
+ /** How long the store's last yes is trusted before the gate asks again. */
3
+ export declare const CHECK_SECONDS: number;
4
+ /**
5
+ * `site` is the review store's URL. It is signed into every token, so two
6
+ * projects that share one GATE_SECRET (a copied folder keeps its .env) never
7
+ * accept each other's cookies. Empty without a store, when nobody can sign in anyway.
8
+ */
3
9
  export type Gate = {
4
- password: string;
5
10
  secret: string;
11
+ site: string;
6
12
  };
7
- /** The gate's settings, or null when either is missing (the gate then fails closed). */
13
+ /** The gate's settings, or null when GATE_SECRET is missing or short (the gate then fails closed). */
8
14
  export declare function readGate(env: Record<string, string | undefined>): Gate | null;
9
- export declare function passwordMatches(gate: Gate, submitted: string): Promise<boolean>;
10
- export declare function issueGateToken(gate: Gate): Promise<string>;
11
- export declare function gateTokenIsValid(gate: Gate, token: string | undefined): Promise<boolean>;
15
+ /**
16
+ * How long a session started at `at` lasts, in seconds: to the access end,
17
+ * capped at 400 days, and 0 for an access end that has passed or does not parse.
18
+ */
19
+ export declare function sessionSeconds(endsAt: string | null, at?: Date): number;
12
20
  export type PersonClaim = {
13
21
  id: string;
14
22
  epoch: number;
15
23
  };
16
- export declare function issuePersonToken(gate: Gate, claim: PersonClaim): Promise<string>;
17
- /** The person a token names when its signature and expiry hold; the caller still checks the store. */
18
- export declare function readPersonToken(gate: Gate, token: string | undefined): Promise<PersonClaim | null>;
24
+ export type SessionClaim = PersonClaim & {
25
+ checkedAt: number;
26
+ };
27
+ /** A token for someone the store said yes to at `at`, lasting to their access end. */
28
+ export declare function issuePersonToken(gate: Gate, claim: PersonClaim, endsAt: string | null, at?: Date): Promise<string>;
29
+ /** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
30
+ export declare function readPersonToken(gate: Gate, token: string | undefined, at?: Date): Promise<SessionClaim | null>;
31
+ /** Whether the store's yes in this claim is recent enough to trust without asking again. */
32
+ export declare function isFresh(claim: SessionClaim, at?: Date): boolean;
19
33
  export declare function readCookie(request: Request, name: string): string | undefined;
20
- export declare const sessionCookie: (name: string, value: string) => string;
34
+ /** A session cookie that lasts `seconds`: sessionSeconds of the same access end as its token. */
35
+ export declare const sessionCookie: (name: string, value: string, seconds: number) => string;
21
36
  export declare const clearedCookie: (name: string) => string;
22
37
  export declare function sameOrigin(request: Request): boolean;
@@ -1,23 +1,26 @@
1
- // Signed cookies for the deployed prototypes, on Web Crypto only, so the
2
- // gate (proxy.ts), the review function (api/review.ts) and their tests share
3
- // one implementation. Two sessions:
1
+ // The signed session cookie for the deployed prototypes, on Web Crypto only,
2
+ // so the gate (proxy.ts), the review function (api/review.ts) and their tests
3
+ // share one implementation. There is one session: a person who used their
4
+ // one-time link. It names the person and their epoch, which a sign out
5
+ // everywhere or a revoke bumps, so either ends every session they hold. It
6
+ // lasts until their access ends, at most 400 days (the longest a browser
7
+ // keeps a cookie). HMAC-SHA256 with GATE_SECRET.
4
8
  //
5
- // gate the shared password was typed: view everything, comment on nothing.
6
- // The signature covers the password, so rotating it signs everyone out.
7
- // person an invite link was opened: view everything, and comment as that
8
- // person. The signature covers the person's epoch, which a reissue
9
- // or revoke bumps, so either ends every session they hold.
10
- //
11
- // Both are HMAC-SHA256 with GATE_SECRET and last 30 days.
12
- import { base64url, sha256 } from '../core/bytes.js';
13
- export const GATE_COOKIE = '__Host-hub_gate';
9
+ // It also carries when the store last said yes (checkedAt). For CHECK_SECONDS
10
+ // after that the gate trusts the signature; then it asks the store again and
11
+ // issues a fresh cookie, so a revoke, a sign out everywhere or a changed
12
+ // access end reaches every request within CHECK_SECONDS, whatever it claims to be.
13
+ // Every function that reads the clock takes `at`, so tests need not wait.
14
+ import { base64url } from '../core/bytes.js';
14
15
  export const PERSON_COOKIE = '__Host-hub_person';
15
- const SESSION_SECONDS = 60 * 60 * 24 * 30;
16
- /** The gate's settings, or null when either is missing (the gate then fails closed). */
16
+ const MAX_SESSION_SECONDS = 60 * 60 * 24 * 400;
17
+ /** How long the store's last yes is trusted before the gate asks again. */
18
+ export const CHECK_SECONDS = 5 * 60;
19
+ /** The gate's settings, or null when GATE_SECRET is missing or short (the gate then fails closed). */
17
20
  export function readGate(env) {
18
- const password = env.GATE_PASSWORD ?? '';
19
21
  const secret = env.GATE_SECRET ?? '';
20
- return password.length > 0 && secret.length >= 32 ? { password, secret } : null;
22
+ const site = (env.KV_REST_API_URL ?? env.UPSTASH_REDIS_REST_URL ?? '').replace(/\/$/, '');
23
+ return secret.length >= 32 ? { secret, site } : null;
21
24
  }
22
25
  const encoder = new TextEncoder();
23
26
  async function hmac(secret, message) {
@@ -33,44 +36,54 @@ function timingSafeEqual(a, b) {
33
36
  return diff === 0;
34
37
  }
35
38
  const sameText = (a, b) => timingSafeEqual(encoder.encode(a), encoder.encode(b));
36
- const nowSeconds = () => Math.floor(Date.now() / 1000);
37
- const live = (expires) => Number.isInteger(expires) && expires > nowSeconds();
38
- export async function passwordMatches(gate, submitted) {
39
- const [a, b] = await Promise.all([sha256(submitted), sha256(gate.password)]);
40
- return timingSafeEqual(a, b);
41
- }
42
- // A gate session token: <expires>.<sig>
43
- async function gateSignature(gate, expires) {
44
- return hmac(gate.secret, `v1.${expires}.${base64url(await sha256(gate.password))}`);
39
+ const seconds = (at) => Math.floor(at.getTime() / 1000);
40
+ /**
41
+ * How long a session started at `at` lasts, in seconds: to the access end,
42
+ * capped at 400 days, and 0 for an access end that has passed or does not parse.
43
+ */
44
+ export function sessionSeconds(endsAt, at = new Date()) {
45
+ if (endsAt === null)
46
+ return MAX_SESSION_SECONDS;
47
+ const left = Math.floor(Date.parse(endsAt) / 1000) - seconds(at);
48
+ return Number.isFinite(left) ? Math.max(0, Math.min(MAX_SESSION_SECONDS, left)) : 0;
45
49
  }
46
- export async function issueGateToken(gate) {
47
- const expires = nowSeconds() + SESSION_SECONDS;
48
- return `${expires}.${await gateSignature(gate, expires)}`;
49
- }
50
- export async function gateTokenIsValid(gate, token) {
51
- const [expires, sig, extra] = (token ?? '').split('.');
52
- if (!sig || extra !== undefined || !live(Number(expires)))
53
- return false;
54
- return sameText(sig, await gateSignature(gate, Number(expires)));
50
+ const VERSION = 'p3';
51
+ // The site is signed, never written into the token; a newline cannot occur in a URL, so the parts cannot run together.
52
+ const personMessage = (site, id, epoch, expires, checkedAt) => `${VERSION}\n${site}\n${id}.${epoch}.${expires}.${checkedAt}`;
53
+ const tokenText = (id, epoch, expires, checkedAt) => `${VERSION}.${id}.${epoch}.${expires}.${checkedAt}`;
54
+ /** A token for someone the store said yes to at `at`, lasting to their access end. */
55
+ export async function issuePersonToken(gate, claim, endsAt, at = new Date()) {
56
+ const checkedAt = seconds(at);
57
+ const expires = checkedAt + sessionSeconds(endsAt, at);
58
+ const message = personMessage(gate.site, claim.id, claim.epoch, expires, checkedAt);
59
+ return `${tokenText(claim.id, claim.epoch, expires, checkedAt)}.${await hmac(gate.secret, message)}`;
55
60
  }
56
- const personMessage = (id, epoch, expires) => `p1.${id}.${epoch}.${expires}`;
57
- export async function issuePersonToken(gate, claim) {
58
- const expires = nowSeconds() + SESSION_SECONDS;
59
- return `${claim.id}.${claim.epoch}.${expires}.${await hmac(gate.secret, personMessage(claim.id, claim.epoch, expires))}`;
60
- }
61
- /** The person a token names when its signature and expiry hold; the caller still checks the store. */
62
- export async function readPersonToken(gate, token) {
63
- const [id, epochText, expiresText, sig, extra] = (token ?? '').split('.');
64
- const epoch = Number(epochText);
65
- const expires = Number(expiresText);
66
- if (!id ||
67
- !sig ||
68
- extra !== undefined ||
69
- !/^[a-z0-9]+$/.test(id) ||
70
- !Number.isInteger(epoch) ||
71
- !live(expires))
61
+ const isCount = (n) => Number.isInteger(n) && n >= 0;
62
+ const wellFormed = (parts) => {
63
+ const [version, id, epoch, expires, checkedAt, sig, extra] = parts;
64
+ return (version === VERSION &&
65
+ /^[a-z0-9]+$/.test(id ?? '') &&
66
+ [epoch, expires, checkedAt].every((n) => isCount(Number(n))) &&
67
+ !!sig &&
68
+ extra === undefined);
69
+ };
70
+ /** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
71
+ export async function readPersonToken(gate, token, at = new Date()) {
72
+ const parts = (token ?? '').split('.');
73
+ if (!wellFormed(parts))
74
+ return null;
75
+ const [, id, epoch, expires, checkedAt, sig] = parts;
76
+ if (Number(expires) <= seconds(at))
72
77
  return null;
73
- return sameText(sig, await hmac(gate.secret, personMessage(id, epoch, expires))) ? { id, epoch } : null;
78
+ const message = personMessage(gate.site, id, Number(epoch), Number(expires), Number(checkedAt));
79
+ return sameText(sig, await hmac(gate.secret, message))
80
+ ? { id, epoch: Number(epoch), checkedAt: Number(checkedAt) }
81
+ : null;
82
+ }
83
+ /** Whether the store's yes in this claim is recent enough to trust without asking again. */
84
+ export function isFresh(claim, at = new Date()) {
85
+ const age = seconds(at) - claim.checkedAt;
86
+ return age >= 0 && age < CHECK_SECONDS;
74
87
  }
75
88
  export function readCookie(request, name) {
76
89
  const header = request.headers.get('cookie');
@@ -83,13 +96,17 @@ export function readCookie(request, name) {
83
96
  }
84
97
  return undefined;
85
98
  }
86
- export const sessionCookie = (name, value) => `${name}=${value}; Path=/; Max-Age=${SESSION_SECONDS}; HttpOnly; Secure; SameSite=Lax`;
99
+ /** A session cookie that lasts `seconds`: sessionSeconds of the same access end as its token. */
100
+ export const sessionCookie = (name, value, seconds) => `${name}=${value}; Path=/; Max-Age=${seconds}; HttpOnly; Secure; SameSite=Lax`;
87
101
  export const clearedCookie = (name) => `${name}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=Lax`;
88
102
  // Same-origin check for a state-changing request. Sec-Fetch-Site is the
89
103
  // browser's own verdict; Origin backs it up for browsers without it. Host
90
104
  // comes from the forwarded header, since request.url carries the deployment
91
105
  // host behind a custom domain. A browser sends "Origin: null" on a navigation
92
106
  // from a no-referrer document, so null is accepted only with a same-origin verdict.
107
+ // A request with neither header is not from a browser that could be tricked
108
+ // into it (every current browser sends Origin on a POST), so it passes: it
109
+ // holds whatever it sends, and a forged request needs a victim's browser.
93
110
  export function sameOrigin(request) {
94
111
  const site = request.headers.get('sec-fetch-site');
95
112
  if (site && site !== 'same-origin')
@@ -23,9 +23,10 @@ export function upstashKV({ url, token }, fetcher = fetch) {
23
23
  hset: async (key, field, value) => {
24
24
  await command('HSET', key, field, value);
25
25
  },
26
- hdel: async (key, field) => {
27
- await command('HDEL', key, field);
28
- },
26
+ // HDEL answers how many fields it removed, which is what lets a one-time link be claimed once.
27
+ hdel: (key, field) => command('HDEL', key, field),
28
+ // Atomic in Redis, so two sign-outs everywhere at once raise the epoch twice.
29
+ hincrby: (key, field, by) => command('HINCRBY', key, field, String(by)),
29
30
  hgetall: async (key) => {
30
31
  const flat = (await command('HGETALL', key)) ?? [];
31
32
  const out = {};
@@ -1,11 +1,13 @@
1
1
  export declare const GATE_FONT = "/_gate/geist.woff2";
2
- export declare function gatePreview(failed: boolean, siteName?: string): Promise<{
2
+ export declare function gatePreview(page: string | null, siteName?: string): Promise<{
3
3
  html: string;
4
4
  csp: string;
5
5
  }>;
6
6
  export type ProxyOptions = {
7
- /** The gate page's title: the project's name as reviewers know it. */
7
+ /** The gate pages' title: the project's name as reviewers know it. */
8
8
  siteName?: string;
9
+ /** The gate's clock; tests move it instead of waiting. */
10
+ now?: () => Date;
9
11
  };
10
12
  /** The middleware a project's proxy.ts default-exports. */
11
- export declare function createProxy({ siteName }?: ProxyOptions): (request: Request) => Promise<Response>;
13
+ export declare function createProxy({ siteName, now }?: ProxyOptions): (request: Request) => Promise<Response>;