@milesroxas/design-hub 0.10.0 → 0.11.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 +13 -11
- package/core/files.test.ts +76 -0
- package/core/files.ts +131 -0
- package/core/prototype.test.ts +124 -7
- package/core/prototype.ts +133 -24
- package/core/review/model.ts +119 -2
- package/core/review/policy.test.ts +79 -22
- package/core/review/policy.ts +36 -8
- package/core/review/service.test.ts +1464 -340
- package/core/review/service.ts +777 -191
- package/core/review/store.test.ts +91 -0
- package/core/review/store.ts +143 -33
- package/dist/core/files.d.ts +39 -0
- package/dist/core/files.js +107 -0
- package/dist/core/prototype.d.ts +61 -12
- package/dist/core/prototype.js +105 -26
- package/dist/core/review/model.d.ts +76 -2
- package/dist/core/review/model.js +76 -1
- package/dist/core/review/policy.d.ts +11 -4
- package/dist/core/review/policy.js +14 -2
- package/dist/core/review/service.d.ts +197 -45
- package/dist/core/review/service.js +702 -192
- package/dist/core/review/store.d.ts +84 -31
- package/dist/core/review/store.js +66 -33
- package/dist/scripts/bootstrap.d.ts +4 -34
- package/dist/scripts/bootstrap.js +9 -244
- package/dist/scripts/cli.js +6 -0
- package/dist/scripts/client.d.ts +45 -0
- package/dist/scripts/client.js +320 -0
- package/dist/scripts/commands.js +6 -0
- package/dist/scripts/comments.d.ts +8 -26
- package/dist/scripts/comments.js +27 -14
- package/dist/scripts/lib/builds.d.ts +40 -0
- package/dist/scripts/lib/builds.js +198 -0
- package/dist/scripts/lib/cli.js +3 -1
- package/dist/scripts/lib/deploy.d.ts +6 -90
- package/dist/scripts/lib/deploy.js +24 -137
- package/dist/scripts/lib/dns.d.ts +49 -0
- package/dist/scripts/lib/dns.js +141 -0
- package/dist/scripts/lib/fake-host.d.ts +168 -0
- package/dist/scripts/lib/fake-host.js +244 -0
- package/dist/scripts/lib/host.d.ts +87 -0
- package/dist/scripts/lib/host.js +362 -0
- package/dist/scripts/lib/linked.d.ts +51 -0
- package/dist/scripts/lib/linked.js +124 -0
- package/dist/scripts/lib/names.d.ts +35 -0
- package/dist/scripts/lib/names.js +77 -0
- package/dist/scripts/lib/review.d.ts +23 -2
- package/dist/scripts/lib/review.js +32 -15
- package/dist/scripts/lib/studio-deps.d.ts +35 -0
- package/dist/scripts/lib/studio-deps.js +79 -0
- package/dist/scripts/lib/studios.d.ts +70 -0
- package/dist/scripts/lib/studios.js +187 -0
- package/dist/scripts/lib/update.d.ts +33 -0
- package/dist/scripts/lib/update.js +40 -0
- package/dist/scripts/lib/vercel.d.ts +1 -23
- package/dist/scripts/lib/vercel.js +6 -81
- package/dist/scripts/people.d.ts +7 -11
- package/dist/scripts/people.js +51 -46
- package/dist/scripts/preflight.d.ts +8 -1
- package/dist/scripts/preflight.js +36 -19
- package/dist/scripts/project.d.ts +86 -0
- package/dist/scripts/project.js +141 -0
- package/dist/scripts/share.d.ts +105 -30
- package/dist/scripts/share.js +82 -154
- package/dist/scripts/skills.d.ts +18 -2
- package/dist/scripts/skills.js +22 -3
- package/dist/scripts/studio.d.ts +75 -0
- package/dist/scripts/studio.js +401 -0
- package/dist/scripts/upgrade.d.ts +10 -2
- package/dist/scripts/upgrade.js +44 -14
- package/dist/server/api.d.ts +22 -11
- package/dist/server/api.js +114 -46
- package/dist/server/blob.d.ts +10 -0
- package/dist/server/blob.js +53 -0
- package/dist/server/checks.js +2 -2
- package/dist/server/dev.js +4 -4
- package/dist/server/env.d.ts +2 -0
- package/dist/server/env.js +9 -0
- package/dist/server/fake-upstash.js +52 -3
- package/dist/server/hosts.d.ts +11 -0
- package/dist/server/hosts.js +31 -0
- package/dist/server/local.d.ts +7 -1
- package/dist/server/local.js +10 -1
- package/dist/server/mark.d.ts +41 -0
- package/dist/server/mark.js +111 -0
- package/dist/server/session.d.ts +17 -9
- package/dist/server/session.js +22 -45
- package/dist/server/upstash.js +20 -0
- package/dist/vercel/files.d.ts +11 -0
- package/dist/vercel/files.js +68 -0
- package/dist/vercel/index.d.ts +4 -3
- package/dist/vercel/index.js +12 -8
- package/dist/vercel/proxy.d.ts +69 -4
- package/dist/vercel/proxy.js +171 -107
- package/dist/vite/plugin.d.ts +24 -1
- package/dist/vite/plugin.js +46 -2
- package/kit/dials.tsx +2 -2
- package/package.json +2 -1
- package/shell/Guide.tsx +11 -5
- package/shell/Index.tsx +32 -10
- package/shell/Notice.tsx +1 -1
- package/shell/controls.ts +1 -1
- package/shell/hub.ts +5 -1
- package/shell/main.tsx +18 -3
- package/shell/review/Review.tsx +2 -2
- package/shell/review/ThreadCard.tsx +5 -5
- package/shell/review/client.ts +28 -11
- package/shell/review/view.test.ts +19 -0
- package/shell/review/view.ts +25 -0
- package/shell/shell.css +4 -0
- package/shell/studio/ClientHub.tsx +98 -0
- package/shell/studio/hub.css +48 -0
- package/shell/studio/index.html +14 -0
- package/shell/studio/main.tsx +17 -0
- package/skills/miles-deploy/SKILL.md +120 -65
- package/skills/miles-design-hub/SKILL.md +11 -9
- package/skills/miles-review/SKILL.md +5 -7
- package/skills/miles-worktrees/SKILL.md +3 -3
- package/templates/conductor/settings.toml +1 -1
- package/templates/studio/.env.example +9 -0
- package/templates/studio/.vercelignore +5 -0
- package/templates/studio/README.md +84 -0
- package/templates/studio/_gitignore +7 -0
- package/templates/studio/api/files.ts +4 -0
- package/templates/studio/api/review.ts +4 -0
- package/templates/studio/build.mjs +4 -0
- package/templates/studio/package.json +16 -0
- package/templates/studio/proxy.ts +4 -0
- package/templates/studio/studio.json +9 -0
- package/templates/studio/vercel.json +27 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// Which studio a review store serves. A database serves one studio only (S1,
|
|
3
|
+
// S8), and no store key names a studio, so two studios on one database would
|
|
4
|
+
// be one set of clients, people and links, each studio's commands acting on
|
|
5
|
+
// the other's. So the database says whose it is: `studio create` marks it
|
|
6
|
+
// with the studio's id (studio.json `id`), once and atomically, before it
|
|
7
|
+
// writes anything else to it, and only when it holds no studio's data; and
|
|
8
|
+
// every other way into a studio's store from its folder checks the mark
|
|
9
|
+
// before it acts: the studio, client and project commands, and share,
|
|
10
|
+
// people and comments in a linked project (openReview in
|
|
11
|
+
// scripts/lib/studio-deps.ts).
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { keyOf } from '../core/review/store.js';
|
|
14
|
+
const MARK = keyOf('studio', 'mark');
|
|
15
|
+
const FIELD = 'id';
|
|
16
|
+
/** A client's records outside its own keys: any of them means a client was added at some time. */
|
|
17
|
+
const CLIENT_RECORDS = ['clients', 'hosts', 'claims', 'held'];
|
|
18
|
+
/** The studio's records beside its people. */
|
|
19
|
+
const STUDIO_RECORDS = ['revoked', 'epochs', 'links', 'settings'];
|
|
20
|
+
/**
|
|
21
|
+
* A refusal: why, and its detail (`code`, `studio`, `fix`), which the CLI
|
|
22
|
+
* prints as it prints a CliError's (scripts/lib/cli.ts, which this layer
|
|
23
|
+
* cannot import).
|
|
24
|
+
*/
|
|
25
|
+
export class MarkError extends Error {
|
|
26
|
+
detail;
|
|
27
|
+
constructor(message, detail) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.detail = detail;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** What every refusal says: whose database this must be, and how to give the studio one of its own. */
|
|
33
|
+
function ownDatabase({ dir, config }) {
|
|
34
|
+
return `A database serves one studio only; never connect one another studio uses. On team ${config.vercel.team}'s Storage page on Vercel, disconnect this one from project ${config.vercel.project}, remove KV_REST_API_URL and KV_REST_API_TOKEN from ${join(dir, '.env')}, then run design-hub studio create again in ${dir} with the same flags, without --database (a new free one) or with --database <a database no studio uses>.`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether a database marked as no studio's is free for this one: it holds no
|
|
38
|
+
* studio's data at all; or, for a studio made before studios had ids, it holds
|
|
39
|
+
* only this folder's own: its owner is the only Studio person and no client
|
|
40
|
+
* was ever added. Any other studio data is another studio's, and is never
|
|
41
|
+
* taken over.
|
|
42
|
+
*
|
|
43
|
+
* Only the studio-level keys are read, on purpose. An unmarked database that
|
|
44
|
+
* holds only records under the fixed `site` client, or an older published
|
|
45
|
+
* hub's keys, holds no studio record, so it reads as free and is claimed. That is meant:
|
|
46
|
+
* studios start clean, with no migration (S11); and `site` is a reserved
|
|
47
|
+
* client slug (LOCAL_CLIENT), so no studio's client ever reads those records
|
|
48
|
+
* as its own.
|
|
49
|
+
*/
|
|
50
|
+
async function freeFor(kv, owner) {
|
|
51
|
+
const fields = async (name) => Object.keys(await kv.hgetall(keyOf('studio', name)));
|
|
52
|
+
if ((await Promise.all(CLIENT_RECORDS.map(fields))).some((f) => f.length))
|
|
53
|
+
return false;
|
|
54
|
+
const people = await fields('people');
|
|
55
|
+
if (!people.length)
|
|
56
|
+
return (await Promise.all(STUDIO_RECORDS.map(fields))).every((f) => !f.length);
|
|
57
|
+
return owner !== null && people.length === 1 && people[0] === owner;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Marks the database as this studio's, for `studio create`, before anything
|
|
61
|
+
* else is written to it: 'claimed' the first time, 'kept' when it is already
|
|
62
|
+
* this studio's. One marked for another studio, or holding data it is not
|
|
63
|
+
* free for, is refused with nothing written. The mark is set by HSETNX, so of
|
|
64
|
+
* two creates at once on one empty database only one wins.
|
|
65
|
+
*/
|
|
66
|
+
export async function claimDatabase(kv, studio) {
|
|
67
|
+
const { id, slug, owner } = studio.config;
|
|
68
|
+
if (!id)
|
|
69
|
+
throw new Error('A studio is given its id before its database is claimed.');
|
|
70
|
+
const mark = await kv.hget(MARK, FIELD);
|
|
71
|
+
if (mark === id)
|
|
72
|
+
return 'kept';
|
|
73
|
+
if (mark === null && (await freeFor(kv, owner)) && (await kv.hsetnx(MARK, FIELD, id)))
|
|
74
|
+
return 'claimed';
|
|
75
|
+
const now = await kv.hget(MARK, FIELD);
|
|
76
|
+
// The same studio's create, run twice at once: the other run marked it.
|
|
77
|
+
if (now === id)
|
|
78
|
+
return 'kept';
|
|
79
|
+
throw new MarkError(now === null
|
|
80
|
+
? `The database connected to studio ${slug} already holds studio data and no mark says whose; nothing was written to it.`
|
|
81
|
+
: `The database connected to studio ${slug} is another studio's; nothing was written to it.`, { code: 'database_not_this_studios', studio: slug, fix: ownDatabase(studio) });
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Stops a command before it acts on a database that is not marked as this
|
|
85
|
+
* studio's, so no command of one studio ever reads or changes another's
|
|
86
|
+
* clients, people or links.
|
|
87
|
+
*/
|
|
88
|
+
export async function mustBeOwnDatabase(kv, studio) {
|
|
89
|
+
const { id, slug } = studio.config;
|
|
90
|
+
const again = `Run design-hub studio create again in ${studio.dir} with the flags it was made with: it marks the studio's database as its own, or says why it cannot.`;
|
|
91
|
+
if (!id)
|
|
92
|
+
throw new MarkError(`Studio ${slug} has no id in its studio.json; nothing was changed.`, {
|
|
93
|
+
code: 'studio_without_id',
|
|
94
|
+
studio: slug,
|
|
95
|
+
fix: again,
|
|
96
|
+
});
|
|
97
|
+
const mark = await kv.hget(MARK, FIELD);
|
|
98
|
+
if (mark === id)
|
|
99
|
+
return;
|
|
100
|
+
if (mark === null)
|
|
101
|
+
throw new MarkError(`The database in studio ${slug}'s .env is not marked as its own; nothing was changed.`, {
|
|
102
|
+
code: 'database_not_marked',
|
|
103
|
+
studio: slug,
|
|
104
|
+
fix: again,
|
|
105
|
+
});
|
|
106
|
+
throw new MarkError(`The database in studio ${slug}'s .env is another studio's; nothing was changed.`, {
|
|
107
|
+
code: 'database_not_this_studios',
|
|
108
|
+
studio: slug,
|
|
109
|
+
fix: ownDatabase(studio),
|
|
110
|
+
});
|
|
111
|
+
}
|
package/dist/server/session.d.ts
CHANGED
|
@@ -4,15 +4,25 @@ export declare const PERSON_COOKIE = "__Host-hub_person";
|
|
|
4
4
|
export declare const CHECK_SECONDS: number;
|
|
5
5
|
/**
|
|
6
6
|
* `site` is the review store's URL. It is signed into every token, so two
|
|
7
|
-
*
|
|
8
|
-
* accept each other's cookies.
|
|
7
|
+
* studios that share one GATE_SECRET (a copied folder keeps its .env) never
|
|
8
|
+
* accept each other's cookies.
|
|
9
|
+
* `client` is the client of the host the gate answers, or `studio` for the
|
|
10
|
+
* studio's own host: a token is signed to it and verifies only where it is
|
|
11
|
+
* the same. `since` is when that client was added (its record's
|
|
12
|
+
* `createdAt`), signed in beside the client: archiving a client and adding
|
|
13
|
+
* the slug again gives a new `since`, so no token from before verifies,
|
|
14
|
+
* fresh or not.
|
|
9
15
|
*/
|
|
10
16
|
export type Gate = {
|
|
11
17
|
secret: string;
|
|
12
18
|
site: string;
|
|
19
|
+
client: string | 'studio';
|
|
20
|
+
since: string;
|
|
13
21
|
};
|
|
14
|
-
/**
|
|
15
|
-
export
|
|
22
|
+
/** What the studio's env gives every gate: the secret and the store it signs to. The host gives the rest. */
|
|
23
|
+
export type GateKeys = Pick<Gate, 'secret' | 'site'>;
|
|
24
|
+
/** The gate's keys, or null when GATE_SECRET is missing or short (the gate then fails closed). */
|
|
25
|
+
export declare function readGate(env: Record<string, string | undefined>): GateKeys | null;
|
|
16
26
|
/**
|
|
17
27
|
* How long a session started at `at` lasts, in seconds: to the access end,
|
|
18
28
|
* capped at 400 days, and 0 for an access end that has passed or does not parse.
|
|
@@ -23,14 +33,12 @@ export type PersonClaim = {
|
|
|
23
33
|
epoch: number;
|
|
24
34
|
kind: PersonKind;
|
|
25
35
|
};
|
|
26
|
-
|
|
27
|
-
export type SessionClaim = Omit<PersonClaim, 'kind'> & {
|
|
28
|
-
kind: PersonKind | null;
|
|
36
|
+
export type SessionClaim = PersonClaim & {
|
|
29
37
|
checkedAt: number;
|
|
30
38
|
};
|
|
31
|
-
/** A token for someone the store said yes to at `at`, lasting to their access end. */
|
|
39
|
+
/** A token for someone the store said yes to at `at`, signed to the gate's client, lasting to their access end. */
|
|
32
40
|
export declare function issuePersonToken(gate: Gate, claim: PersonClaim, endsAt: string | null, at?: Date): Promise<string>;
|
|
33
|
-
/** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
|
|
41
|
+
/** The person a token names when its version, client, signature and expiry hold; the caller decides whether to ask the store. */
|
|
34
42
|
export declare function readPersonToken(gate: Gate, token: string | undefined, at?: Date): Promise<SessionClaim | null>;
|
|
35
43
|
/** Whether the store's yes in this claim is recent enough to trust without asking again. */
|
|
36
44
|
export declare function isFresh(claim: SessionClaim, at?: Date): boolean;
|
package/dist/server/session.js
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
// The signed session cookie for
|
|
2
|
-
//
|
|
1
|
+
// The signed session cookie for a studio site, on Web Crypto only, so the
|
|
2
|
+
// gate (proxy.ts), the review and files functions (api/) and their tests
|
|
3
3
|
// share one implementation. There is one session: a person who used their
|
|
4
4
|
// one-time link. It names the person and their epoch, which a sign out
|
|
5
5
|
// everywhere or a revoke bumps, so either ends every session they hold. It
|
|
6
6
|
// lasts until their access ends, at most 400 days (the longest a browser
|
|
7
7
|
// keeps a cookie). HMAC-SHA256 with GATE_SECRET.
|
|
8
8
|
//
|
|
9
|
-
// It also carries the person's kind,
|
|
9
|
+
// It also carries the person's kind, the client it was signed to, and when
|
|
10
|
+
// the store last said yes (checkedAt). For CHECK_SECONDS
|
|
10
11
|
// after that the gate trusts the signature; then it asks the store again and
|
|
11
12
|
// issues a fresh cookie, so a revoke, a sign out everywhere or a changed
|
|
12
13
|
// access end reaches every request within CHECK_SECONDS, whatever it claims to be.
|
|
13
14
|
// Every function that reads the clock takes `at`, so tests need not wait.
|
|
14
15
|
import { base64url } from '../core/bytes.js';
|
|
16
|
+
import { SLUG } from '../core/feature.js';
|
|
15
17
|
import { PERSON_KINDS } from '../core/review/model.js';
|
|
16
18
|
export const PERSON_COOKIE = '__Host-hub_person';
|
|
17
19
|
const MAX_SESSION_SECONDS = 60 * 60 * 24 * 400;
|
|
18
20
|
/** How long the store's last yes is trusted before the gate asks again. */
|
|
19
21
|
export const CHECK_SECONDS = 5 * 60;
|
|
20
|
-
/** The gate's
|
|
22
|
+
/** The gate's keys, or null when GATE_SECRET is missing or short (the gate then fails closed). */
|
|
21
23
|
export function readGate(env) {
|
|
22
24
|
const secret = env.GATE_SECRET ?? '';
|
|
23
25
|
const site = (env.KV_REST_API_URL ?? env.UPSTASH_REDIS_REST_URL ?? '').replace(/\/$/, '');
|
|
@@ -48,58 +50,33 @@ export function sessionSeconds(endsAt, at = new Date()) {
|
|
|
48
50
|
const left = Math.floor(Date.parse(endsAt) / 1000) - seconds(at);
|
|
49
51
|
return Number.isFinite(left) ? Math.max(0, Math.min(MAX_SESSION_SECONDS, left)) : 0;
|
|
50
52
|
}
|
|
51
|
-
const VERSION = '
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
const tokenText = (id, epoch, kind, expires, checkedAt) => `${VERSION}.${id}.${epoch}.${kind}.${expires}.${checkedAt}`;
|
|
57
|
-
/** A token for someone the store said yes to at `at`, lasting to their access end. */
|
|
53
|
+
const VERSION = 'p5';
|
|
54
|
+
// A newline cannot occur in a URL or an ISO time, so the parts cannot run together.
|
|
55
|
+
const signed = ({ site, since }, text) => `${VERSION}\n${site}\n${since}\n${text}`;
|
|
56
|
+
const tokenText = (claim, client, expires, checkedAt) => `${claim.id}.${claim.epoch}.${claim.kind}.${client}.${expires}.${checkedAt}`;
|
|
57
|
+
/** A token for someone the store said yes to at `at`, signed to the gate's client, lasting to their access end. */
|
|
58
58
|
export async function issuePersonToken(gate, claim, endsAt, at = new Date()) {
|
|
59
59
|
const checkedAt = seconds(at);
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
return `${tokenText(claim.id, claim.epoch, claim.kind, expires, checkedAt)}.${await hmac(gate.secret, message)}`;
|
|
60
|
+
const text = tokenText(claim, gate.client, checkedAt + sessionSeconds(endsAt, at), checkedAt);
|
|
61
|
+
return `${VERSION}.${text}.${await hmac(gate.secret, signed(gate, text))}`;
|
|
63
62
|
}
|
|
64
63
|
const isCount = (n) => Number.isInteger(n) && n >= 0;
|
|
65
64
|
const isKind = (k) => PERSON_KINDS.includes(k);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
/** The person a token names when its version, client, signature and expiry hold; the caller decides whether to ask the store. */
|
|
66
|
+
export async function readPersonToken(gate, token, at = new Date()) {
|
|
67
|
+
const [version, id, epoch, kind, client, expires, checkedAt, sig, extra] = (token ?? '').split('.');
|
|
68
|
+
if (version !== VERSION || !/^[a-z0-9]+$/.test(id ?? '') || !isKind(kind) || !sig || extra !== undefined)
|
|
69
69
|
return null;
|
|
70
|
-
if (!
|
|
70
|
+
if (!SLUG.test(client ?? '') || client !== gate.client)
|
|
71
71
|
return null;
|
|
72
|
-
|
|
73
|
-
const message = (site) => personMessage(site, id, claim.epoch, kind, Number(expires), claim.checkedAt);
|
|
74
|
-
return { claim, expires: Number(expires), sig, message };
|
|
75
|
-
}
|
|
76
|
-
function parseOld(parts) {
|
|
77
|
-
const [, id, epoch, expires, checkedAt, sig, extra] = parts;
|
|
78
|
-
if (!/^[a-z0-9]+$/.test(id ?? '') || !sig || extra !== undefined)
|
|
72
|
+
if (![epoch, expires, checkedAt].every((n) => isCount(Number(n))) || Number(expires) <= seconds(at))
|
|
79
73
|
return null;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const message = (site) => oldMessage(site, id, claim.epoch, Number(expires), claim.checkedAt);
|
|
84
|
-
return { claim, expires: Number(expires), sig, message };
|
|
85
|
-
}
|
|
86
|
-
const PARSERS = {
|
|
87
|
-
[VERSION]: parseCurrent,
|
|
88
|
-
[OLD_VERSION]: parseOld,
|
|
89
|
-
};
|
|
90
|
-
/** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
|
|
91
|
-
export async function readPersonToken(gate, token, at = new Date()) {
|
|
92
|
-
const parts = (token ?? '').split('.');
|
|
93
|
-
const parse = Object.hasOwn(PARSERS, parts[0]) ? PARSERS[parts[0]] : null;
|
|
94
|
-
const parsed = parse?.(parts);
|
|
95
|
-
if (!parsed || parsed.expires <= seconds(at))
|
|
96
|
-
return null;
|
|
97
|
-
return sameText(parsed.sig, await hmac(gate.secret, parsed.message(gate.site))) ? parsed.claim : null;
|
|
74
|
+
const claim = { id, epoch: Number(epoch), kind, checkedAt: Number(checkedAt) };
|
|
75
|
+
const text = tokenText(claim, client, Number(expires), claim.checkedAt);
|
|
76
|
+
return sameText(sig, await hmac(gate.secret, signed(gate, text))) ? claim : null;
|
|
98
77
|
}
|
|
99
78
|
/** Whether the store's yes in this claim is recent enough to trust without asking again. */
|
|
100
79
|
export function isFresh(claim, at = new Date()) {
|
|
101
|
-
if (claim.kind === null)
|
|
102
|
-
return false;
|
|
103
80
|
const age = seconds(at) - claim.checkedAt;
|
|
104
81
|
return age >= 0 && age < CHECK_SECONDS;
|
|
105
82
|
}
|
package/dist/server/upstash.js
CHANGED
|
@@ -25,6 +25,8 @@ export function upstashKV({ url, token }, fetcher = fetch) {
|
|
|
25
25
|
},
|
|
26
26
|
// HDEL answers how many fields it removed, which is what lets a one-time link be claimed once.
|
|
27
27
|
hdel: (key, field) => command('HDEL', key, field),
|
|
28
|
+
// HSETNX answers 1 only to the one call that set the field, which is what lets a studio claim its database once.
|
|
29
|
+
hsetnx: async (key, field, value) => (await command('HSETNX', key, field, value)) === 1,
|
|
28
30
|
// Atomic in Redis, so two sign-outs everywhere at once raise the epoch twice.
|
|
29
31
|
hincrby: (key, field, by) => command('HINCRBY', key, field, String(by)),
|
|
30
32
|
hgetall: async (key) => {
|
|
@@ -38,5 +40,23 @@ export function upstashKV({ url, token }, fetcher = fetch) {
|
|
|
38
40
|
await command('SADD', key, member);
|
|
39
41
|
},
|
|
40
42
|
smembers: async (key) => (await command('SMEMBERS', key)) ?? [],
|
|
43
|
+
del: async (key) => {
|
|
44
|
+
await command('DEL', key);
|
|
45
|
+
},
|
|
46
|
+
// SCAN walks every key, a page at a time, until its cursor comes back to 0, and may name a key
|
|
47
|
+
// twice. MATCH is a glob, so the prefix's own `*`, `?`, `[`, `]` and `\` are escaped to match only themselves.
|
|
48
|
+
delPrefix: async (prefix) => {
|
|
49
|
+
const match = `${prefix.replace(/[*?[\]\\]/g, '\\$&')}*`;
|
|
50
|
+
const keys = new Set();
|
|
51
|
+
let cursor = '0';
|
|
52
|
+
do {
|
|
53
|
+
const [next, page] = await command('SCAN', cursor, 'MATCH', match, 'COUNT', '1000');
|
|
54
|
+
for (const key of page)
|
|
55
|
+
keys.add(key);
|
|
56
|
+
cursor = String(next);
|
|
57
|
+
} while (cursor !== '0');
|
|
58
|
+
if (keys.size)
|
|
59
|
+
await command('DEL', ...keys);
|
|
60
|
+
},
|
|
41
61
|
};
|
|
42
62
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Files } from '../core/files.ts';
|
|
2
|
+
export type FilesOptions = {
|
|
3
|
+
/** The file store; the studio's private Vercel Blob store when not given. */
|
|
4
|
+
files?: Files;
|
|
5
|
+
/** The function's clock; tests move it instead of waiting. */
|
|
6
|
+
now?: () => Date;
|
|
7
|
+
};
|
|
8
|
+
/** The function a studio site's api/files.ts default-exports. */
|
|
9
|
+
export declare function filesFunction({ files, now }?: FilesOptions): {
|
|
10
|
+
fetch: (request: Request) => Promise<Response>;
|
|
11
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The studio site's files function (api/files.ts): a project's build, read
|
|
2
|
+
// from the file store by the build its record points at. The gate rewrites
|
|
3
|
+
// `/<project>/<file>` here as `/api/files?path=/<project>/<file>`, but this
|
|
4
|
+
// trusts nothing the gate did or set (S5): it finds the host's client, checks
|
|
5
|
+
// the session and the boundary itself with the gate's own code (`admit`), and
|
|
6
|
+
// answers a refused request exactly as the gate would. Every file goes out
|
|
7
|
+
// `private, no-store` with no ETag, so every view reads it fresh.
|
|
8
|
+
import { filePath } from '../core/files.js';
|
|
9
|
+
import { blobFiles } from '../server/blob.js';
|
|
10
|
+
import { hostCache } from '../server/hosts.js';
|
|
11
|
+
import { admit, DEFAULT_SITE_NAME, findPlace, isRead, notFound, plain, refuse, storeDown, } from './proxy.js';
|
|
12
|
+
/** A path's last segment names a file (`app.js`) rather than a page of the hub (`guide`, a prototype). */
|
|
13
|
+
const isFileName = (file) => (file.at(-1) ?? '').includes('.');
|
|
14
|
+
/**
|
|
15
|
+
* The file a request names in the project's current build. A page of the
|
|
16
|
+
* project (no file name) is the build's `index.html`, as the hub's own
|
|
17
|
+
* routes are; a file that is not in this build is not there, whichever
|
|
18
|
+
* build once held it.
|
|
19
|
+
*/
|
|
20
|
+
async function read(files, scope, build, file) {
|
|
21
|
+
const found = file.length ? await files.get(filePath(scope, build, file)) : null;
|
|
22
|
+
if (found || isFileName(file))
|
|
23
|
+
return found;
|
|
24
|
+
return files.get(filePath(scope, build, ['index.html']));
|
|
25
|
+
}
|
|
26
|
+
async function serve(files, hosts, ctx) {
|
|
27
|
+
const { request, at } = ctx;
|
|
28
|
+
if (!isRead(request))
|
|
29
|
+
return plain(request, 405, 'Method not allowed', { allow: 'GET, HEAD' });
|
|
30
|
+
const path = new URL(request.url).searchParams.get('path') ?? '';
|
|
31
|
+
const found = await findPlace(request, path, hosts, at);
|
|
32
|
+
if ('refused' in found)
|
|
33
|
+
return refuse(ctx, found);
|
|
34
|
+
const admitted = await admit(found, request, at);
|
|
35
|
+
if ('refused' in admitted)
|
|
36
|
+
return refuse(ctx, admitted);
|
|
37
|
+
const { route, project } = admitted;
|
|
38
|
+
if (route.kind !== 'file' || !project?.build)
|
|
39
|
+
return notFound(request);
|
|
40
|
+
let file;
|
|
41
|
+
try {
|
|
42
|
+
file = await read(files, { client: project.client, project: project.slug }, project.build, route.file);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error('[files]', error);
|
|
46
|
+
return storeDown(request);
|
|
47
|
+
}
|
|
48
|
+
return file ? sent(request, file) : notFound(request);
|
|
49
|
+
}
|
|
50
|
+
function sent(request, { body, type }) {
|
|
51
|
+
if (request.method === 'HEAD' && body instanceof ReadableStream)
|
|
52
|
+
void body.cancel();
|
|
53
|
+
return new Response(request.method === 'HEAD' ? null : body, {
|
|
54
|
+
headers: {
|
|
55
|
+
'content-type': type,
|
|
56
|
+
'cache-control': 'private, no-store',
|
|
57
|
+
'x-content-type-options': 'nosniff',
|
|
58
|
+
'x-robots-tag': 'noindex, nofollow',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** The function a studio site's api/files.ts default-exports. */
|
|
63
|
+
export function filesFunction({ files = blobFiles(), now = () => new Date() } = {}) {
|
|
64
|
+
const hosts = hostCache(now);
|
|
65
|
+
return {
|
|
66
|
+
fetch: (request) => serve(files, hosts, { siteName: DEFAULT_SITE_NAME, request, at: now() }),
|
|
67
|
+
};
|
|
68
|
+
}
|
package/dist/vercel/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export { filesFunction } from './files.ts';
|
|
2
|
+
export { createStudioProxy, GATE_FONT, gatePreview } from './proxy.ts';
|
|
3
|
+
/** A studio site's /api/review: scoped by the host's client and the project the request names. */
|
|
4
|
+
export declare function studioReviewFunction(checks?: unknown, now?: () => Date): {
|
|
4
5
|
fetch: (request: Request) => Promise<Response>;
|
|
5
6
|
};
|
package/dist/vercel/index.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
// What a
|
|
1
|
+
// What a studio site's entry files import (templates/studio/). Each is one line:
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// proxy.ts export default createStudioProxy()
|
|
4
|
+
// api/review.ts export default studioReviewFunction()
|
|
5
|
+
// api/files.ts export default filesFunction()
|
|
5
6
|
import { loadChecks } from '../core/checks.js';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
7
|
+
import { studioHandler } from '../server/api.js';
|
|
8
|
+
import { hostCache } from '../server/hosts.js';
|
|
9
|
+
export { filesFunction } from './files.js';
|
|
10
|
+
export { createStudioProxy, GATE_FONT, gatePreview } from './proxy.js';
|
|
11
|
+
/** A studio site's /api/review: scoped by the host's client and the project the request names. */
|
|
12
|
+
export function studioReviewFunction(checks = null, now = () => new Date()) {
|
|
10
13
|
const loaded = loadChecks(checks).checks;
|
|
11
|
-
|
|
14
|
+
const hosts = hostCache(now);
|
|
15
|
+
return { fetch: (request) => studioHandler(request, loaded, hosts) };
|
|
12
16
|
}
|
package/dist/vercel/proxy.d.ts
CHANGED
|
@@ -1,13 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
import { GATE_FONT, type Route } from '../core/prototype.ts';
|
|
2
|
+
import type { Client, Person, PersonKind, Project, Scope } from '../core/review/model.ts';
|
|
3
|
+
import type { Review } from '../core/review/service.ts';
|
|
4
|
+
import { type HostLookup } from '../server/hosts.ts';
|
|
5
|
+
import { type Gate } from '../server/session.ts';
|
|
6
|
+
export declare const DEFAULT_SITE_NAME = "Prototypes";
|
|
7
|
+
export { GATE_FONT };
|
|
2
8
|
export declare function gatePreview(page: string | null, siteName?: string): Promise<{
|
|
3
9
|
html: string;
|
|
4
10
|
csp: string;
|
|
5
11
|
}>;
|
|
12
|
+
export declare const isRead: (request: Request) => boolean;
|
|
13
|
+
type Extra = {
|
|
14
|
+
cookie?: string;
|
|
15
|
+
allow?: string;
|
|
16
|
+
referrer?: 'no-referrer' | 'same-origin';
|
|
17
|
+
/** Seconds, on a 503 that passes (a store that did not answer), so the review panel retries instead of switching off. */
|
|
18
|
+
retryAfter?: number;
|
|
19
|
+
};
|
|
20
|
+
export declare const plain: (request: Request, status: number, text: string, extra?: Extra) => Response;
|
|
21
|
+
/** One answer for a prototype a Client may not have, whether it is internal or not there at all. */
|
|
22
|
+
export declare const notFound: (request: Request) => Response;
|
|
23
|
+
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
24
|
+
export declare const storeDown: (request: Request) => Response;
|
|
25
|
+
/** One request at one moment: `at` is the gate's clock, so freshness is tested without waiting. */
|
|
26
|
+
export type Ctx = {
|
|
27
|
+
siteName: string;
|
|
28
|
+
request: Request;
|
|
29
|
+
at: Date;
|
|
30
|
+
};
|
|
6
31
|
export type ProxyOptions = {
|
|
7
|
-
/** The gate pages' title
|
|
32
|
+
/** The gate pages' title, the same on every client's host. */
|
|
8
33
|
siteName?: string;
|
|
9
34
|
/** The gate's clock; tests move it instead of waiting. */
|
|
10
35
|
now?: () => Date;
|
|
11
36
|
};
|
|
12
|
-
/**
|
|
13
|
-
export
|
|
37
|
+
/** Who a request's host is: its client, and the gate and the client's scope built from that one lookup. */
|
|
38
|
+
export type Place = {
|
|
39
|
+
client: Client;
|
|
40
|
+
gate: Gate;
|
|
41
|
+
scope: Scope;
|
|
42
|
+
};
|
|
43
|
+
/** A request on a studio site that names a known client. */
|
|
44
|
+
export type Found = {
|
|
45
|
+
review: Review;
|
|
46
|
+
place: Place;
|
|
47
|
+
route: Route;
|
|
48
|
+
};
|
|
49
|
+
/** Why a studio request ends before it is served; `refuse` answers each one the same wherever it is met. */
|
|
50
|
+
export type Refusal = {
|
|
51
|
+
refused: 'not-found';
|
|
52
|
+
} | {
|
|
53
|
+
refused: 'store-down';
|
|
54
|
+
} | {
|
|
55
|
+
refused: 'unavailable';
|
|
56
|
+
error: string;
|
|
57
|
+
} | {
|
|
58
|
+
refused: 'signed-out';
|
|
59
|
+
stale: boolean;
|
|
60
|
+
};
|
|
61
|
+
/** The client `pathname` is asked of on the request's host, or why there is none: an unknown host is the plain 404. */
|
|
62
|
+
export declare function findPlace(request: Request, pathname: string, hosts: HostLookup, at: Date): Promise<Found | Refusal>;
|
|
63
|
+
/** A request that may go on: who asks, the person to re-issue a cookie for, and the project it is for. */
|
|
64
|
+
export type Admitted = Found & {
|
|
65
|
+
kind: PersonKind;
|
|
66
|
+
current: Person | null;
|
|
67
|
+
project: Project | null;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The checks every studio request passes, in the gate and again in the files
|
|
71
|
+
* function: a session signed to this host's client (asked of the store again
|
|
72
|
+
* once its stamp is stale), then the boundary.
|
|
73
|
+
*/
|
|
74
|
+
export declare function admit(found: Found, request: Request, at: Date): Promise<Admitted | Refusal>;
|
|
75
|
+
/** The answer for a refusal: the same from the gate and from the files function. */
|
|
76
|
+
export declare function refuse(ctx: Ctx, refusal: Refusal): Promise<Response> | Response;
|
|
77
|
+
/** The middleware a studio site's proxy.ts default-exports. Its sign-in page names no client (S10). */
|
|
78
|
+
export declare function createStudioProxy({ siteName, now, }?: ProxyOptions): (request: Request) => Promise<Response>;
|