@milesroxas/design-hub 0.11.2 → 0.12.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 (57) hide show
  1. package/README.md +4 -2
  2. package/core/prototype.test.ts +52 -0
  3. package/core/prototype.ts +59 -0
  4. package/core/review/model.ts +20 -0
  5. package/core/review/service.test.ts +224 -0
  6. package/core/review/service.ts +276 -52
  7. package/core/review/store.ts +10 -4
  8. package/dist/core/prototype.d.ts +50 -0
  9. package/dist/core/prototype.js +38 -0
  10. package/dist/core/review/model.d.ts +22 -0
  11. package/dist/core/review/model.js +8 -0
  12. package/dist/core/review/service.d.ts +98 -18
  13. package/dist/core/review/service.js +248 -46
  14. package/dist/core/review/store.d.ts +10 -4
  15. package/dist/scripts/client.d.ts +3 -3
  16. package/dist/scripts/client.js +52 -66
  17. package/dist/scripts/lib/dns.d.ts +32 -0
  18. package/dist/scripts/lib/dns.js +69 -1
  19. package/dist/scripts/lib/fake-host.d.ts +34 -9
  20. package/dist/scripts/lib/fake-host.js +7 -4
  21. package/dist/scripts/lib/studios.d.ts +3 -1
  22. package/dist/scripts/lib/studios.js +2 -0
  23. package/dist/scripts/people.d.ts +7 -4
  24. package/dist/scripts/people.js +147 -39
  25. package/dist/scripts/studio.d.ts +3 -3
  26. package/dist/scripts/studio.js +100 -10
  27. package/dist/server/api.d.ts +6 -8
  28. package/dist/server/api.js +129 -42
  29. package/dist/server/hosts.d.ts +3 -3
  30. package/dist/server/hosts.js +10 -10
  31. package/dist/server/session.d.ts +7 -0
  32. package/dist/server/session.js +8 -1
  33. package/dist/vercel/proxy.d.ts +7 -3
  34. package/dist/vercel/proxy.js +215 -49
  35. package/dist/vite/plugin.d.ts +6 -2
  36. package/dist/vite/plugin.js +21 -3
  37. package/package.json +1 -1
  38. package/shell/Index.tsx +29 -0
  39. package/shell/review/People.tsx +79 -49
  40. package/shell/review/Review.tsx +49 -1
  41. package/shell/review/ThreadCard.tsx +13 -1
  42. package/shell/review/client.ts +54 -21
  43. package/shell/review/review.css +1 -0
  44. package/shell/review/unsent.test.ts +63 -0
  45. package/shell/review/unsent.ts +64 -0
  46. package/shell/review/view.ts +22 -6
  47. package/shell/studio/ClientHub.tsx +9 -36
  48. package/shell/studio/StudioHub.tsx +293 -0
  49. package/shell/studio/hub.css +80 -5
  50. package/shell/studio/page.tsx +57 -0
  51. package/shell/studio/studio.html +14 -0
  52. package/shell/studio/studio.tsx +19 -0
  53. package/skills/miles-deploy/SKILL.md +15 -5
  54. package/skills/miles-design-hub/SKILL.md +3 -3
  55. package/skills/miles-review/SKILL.md +1 -1
  56. package/templates/studio/README.md +32 -13
  57. package/templates/studio/package.json +1 -1
@@ -1,9 +1,10 @@
1
1
  import { SLUG } from '../core/feature.js';
2
2
  import { hostName, isProjectSlug } from '../core/prototype.js';
3
- import { createReview, NO_PROJECT, ReviewError } from '../core/review/service.js';
3
+ import { isAdded, STUDIO_SITE, } from '../core/review/model.js';
4
+ import { createReview, NO_PROJECT, ReviewError, } from '../core/review/service.js';
4
5
  import { jevTriager } from '../core/review/triage.js';
5
6
  import { hostOf } from './hosts.js';
6
- import { clearedCookie, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, } from './session.js';
7
+ import { clearedCookie, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, studioGate, } from './session.js';
7
8
  import { storeConfig, upstashKV } from './upstash.js';
8
9
  export const API_PATH = '/api/review';
9
10
  const MAX_BODY_BYTES = 64 * 1024;
@@ -62,25 +63,21 @@ async function query(op, params, { review, actor, scope }) {
62
63
  return undefined;
63
64
  }
64
65
  }
65
- const link = (r) => ({
66
- person: visible(r.person),
67
- link: `/invite/${r.token}`,
68
- });
66
+ /** A link as a client's host answers it: at its own root, where its gate answers it. */
67
+ const link = (r) => ({ person: visible(r.person), link: `/invite/${r.token}` });
69
68
  /** Studio's commands over people, their links and how long they keep access. */
70
- async function peopleCommand(op, b, { review, actor, scope }) {
69
+ async function peopleCommand(op, b, { review, actor, at, link }) {
71
70
  switch (op) {
72
71
  case 'people.add':
73
- return link(await review.addPerson(scope, actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
72
+ return link(await review.addPerson(at, actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
74
73
  case 'people.link':
75
- return link(await review.newLink(scope, actor, str(b.id)));
74
+ return link(await review.newLink(at, actor, str(b.id)));
76
75
  case 'people.signOut':
77
- return link(await review.signOutEverywhere(scope, actor, str(b.id)));
76
+ return link(await review.signOutEverywhere(at, actor, str(b.id)));
78
77
  case 'people.revoke':
79
- return { person: visible(await review.revoke(scope, actor, str(b.id))) };
78
+ return { person: visible(await review.revoke(at, actor, str(b.id))) };
80
79
  case 'people.access':
81
- return { person: visible(await review.setAccess(scope, actor, str(b.id), b.access)) };
82
- case 'access.set':
83
- return { access: await review.setDefaultAccess(scope, actor, b.access) };
80
+ return { person: visible(await review.setAccess(at, actor, str(b.id), b.access)) };
84
81
  default:
85
82
  return undefined;
86
83
  }
@@ -97,9 +94,11 @@ const PROJECT_COMMANDS = [
97
94
  'control.audience',
98
95
  ];
99
96
  async function command(op, b, ctx) {
100
- if (!PROJECT_COMMANDS.includes(op))
101
- return peopleCommand(op, b, ctx);
102
97
  const { review, actor } = ctx;
98
+ if (op === 'access.set')
99
+ return { access: await review.setDefaultAccess(ctx.scope, actor, b.access) };
100
+ if (!PROJECT_COMMANDS.includes(op))
101
+ return peopleCommand(op, b, { review, actor, at: ctx.scope, link });
103
102
  const scope = projectScope(ctx.scope);
104
103
  const at = () => ({ prototype: prototypeOf(b.prototype), id: str(b.id) });
105
104
  switch (op) {
@@ -163,11 +162,11 @@ async function readJson(request) {
163
162
  return null;
164
163
  }
165
164
  }
166
- export async function handleReview(request, ctx) {
165
+ async function answer(request, ops) {
167
166
  try {
168
167
  if (request.method === 'GET') {
169
168
  const params = new URL(request.url).searchParams;
170
- const result = await query(params.get('op') ?? '', params, ctx);
169
+ const result = await ops.query(params.get('op') ?? '', params);
171
170
  return result === undefined ? json(400, { error: 'Unknown query.' }) : json(200, result);
172
171
  }
173
172
  if (request.method === 'POST') {
@@ -177,7 +176,7 @@ export async function handleReview(request, ctx) {
177
176
  const body = await readJson(request);
178
177
  if (!body)
179
178
  return json(400, { error: `Send a JSON object of ${MAX_BODY_BYTES / 1024} KB or less.` });
180
- const result = await command(str(body.op), body, ctx);
179
+ const result = await ops.command(str(body.op), body);
181
180
  return result === undefined ? json(400, { error: 'Unknown command.' }) : json(200, result);
182
181
  }
183
182
  return new Response('Method not allowed', { status: 405, headers: { allow: 'GET, POST' } });
@@ -189,10 +188,89 @@ export async function handleReview(request, ctx) {
189
188
  return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
190
189
  }
191
190
  }
192
- /** The person a request's signed cookie names, while they may still sign in, in `scope`. */
193
- export async function actorFrom(request, review, gate, scope) {
191
+ /** The review API on a client's host, or in dev: every op in the context's scope. */
192
+ const clientOps = (ctx) => ({
193
+ query: (op, params) => query(op, params, ctx),
194
+ command: (op, body) => command(op, body, ctx),
195
+ });
196
+ export const handleReview = (request, ctx) => answer(request, clientOps(ctx));
197
+ /**
198
+ * What the studio hub shows of a client: its name and host, whether it is
199
+ * still being added, and each project with whether it has a build to open.
200
+ */
201
+ const studioListed = ({ client, projects }) => ({
202
+ slug: client.slug,
203
+ name: client.name,
204
+ host: client.host,
205
+ waiting: !isAdded(client),
206
+ projects: projects.map((p) => ({ slug: p.slug, name: p.name, built: p.build !== null })),
207
+ });
208
+ /** The ops on people the studio's own host answers, the review panel's People ops. */
209
+ const PEOPLE_COMMANDS = [
210
+ 'people.add',
211
+ 'people.link',
212
+ 'people.signOut',
213
+ 'people.revoke',
214
+ 'people.access',
215
+ ];
216
+ /**
217
+ * The review API on the studio's own host, for a Studio person signed in
218
+ * there (`host`): who they are, every client with its projects, and the
219
+ * people. A people op names the client it is for with `client`, checked
220
+ * against the store as a handoff checks it (studioClient): an unknown,
221
+ * waiting or archived client answers as an unknown one. Without `client` it
222
+ * is the studio's own people. A link comes back whole, on the host it signs
223
+ * in on: a Client's on their client's host, a Studio person's on this one.
224
+ * No client's host has any of this: there `clients` is an unknown query, and
225
+ * a `client` parameter is never read (S10).
226
+ */
227
+ const studioOps = (review, actor, host) => {
228
+ const where = async (client) => {
229
+ if (client === undefined || client === null)
230
+ return { at: STUDIO_SITE, host };
231
+ const c = await review.studioClient(actor, client);
232
+ return { at: { client: c.slug }, host: c.host };
233
+ };
234
+ const people = async (client) => {
235
+ const w = await where(client);
236
+ return {
237
+ review,
238
+ actor,
239
+ at: w.at,
240
+ link: (r) => ({
241
+ person: visible(r.person),
242
+ link: `https://${r.person.client === null ? host : w.host}/invite/${r.token}`,
243
+ }),
244
+ };
245
+ };
246
+ return {
247
+ async query(op, params) {
248
+ if (op === 'me')
249
+ return { person: visible(actor), client: STUDIO_SITE };
250
+ if (op === 'clients') {
251
+ const all = await review.listClients(actor);
252
+ return { clients: all.filter((c) => c.client.archivedAt === null).map(studioListed) };
253
+ }
254
+ if (op !== 'people' && op !== 'access')
255
+ return undefined;
256
+ const { at } = await where(params.get('client'));
257
+ if (op === 'access')
258
+ return {
259
+ access: at === STUDIO_SITE ? await review.studioAccess(actor) : await review.defaultAccess(at, actor),
260
+ };
261
+ // A client's section lists its own people; the Studio people have a section of their own.
262
+ const listed = await review.listPeople(at, actor);
263
+ return { people: listed.filter((p) => at === STUDIO_SITE || p.client !== null).map(visible) };
264
+ },
265
+ async command(op, body) {
266
+ return PEOPLE_COMMANDS.includes(op) ? peopleCommand(op, body, await people(body.client)) : undefined;
267
+ },
268
+ };
269
+ };
270
+ /** The person a request's signed cookie names, while they may still sign in, where `at` is: a client's scope, or the studio's host. */
271
+ export async function actorFrom(request, review, gate, at) {
194
272
  const claim = await readPersonToken(gate, readCookie(request, PERSON_COOKIE));
195
- return claim ? review.sessionPerson(scope, claim.id, claim.epoch) : null;
273
+ return claim ? review.sessionPerson(at, claim.id, claim.epoch) : null;
196
274
  }
197
275
  /** What a studio site says when it is not set up: every client host sees it, so it names no command, studio or client. */
198
276
  const SITE_NOT_SET_UP = 'This site is not set up yet.';
@@ -218,10 +296,11 @@ export function studioReview(env, checks = null, now) {
218
296
  }),
219
297
  };
220
298
  }
221
- /** The client a studio request's host names, from one host lookup; null for a host no client has. */
222
- async function hostClient(request, review, hosts) {
299
+ /** A studio request's host and what it serves, from one host lookup: a client, the studio's own host, or null. */
300
+ async function hostSite(request, review, hosts) {
223
301
  const host = hostName(hostOf(request));
224
- return host ? await hosts(host, (h) => review.clientOfHost(h)) : null;
302
+ const site = host ? await hosts(host, (h) => review.siteOfHost(h)) : null;
303
+ return host && site ? { host, site } : null;
225
304
  }
226
305
  /**
227
306
  * The scope a signed-in studio request runs in: the host's client, and the
@@ -241,34 +320,42 @@ async function studioScope(request, review, client) {
241
320
  }
242
321
  /** The gate's answer to no session, in JSON: it names no client (S10), and clears a cookie that does not hold. */
243
322
  const signedOut = (request) => json(401, { error: 'Sign in with the link the studio sent you.' }, readCookie(request, PERSON_COOKIE) === undefined ? {} : { 'set-cookie': clearedCookie(PERSON_COOKIE) });
323
+ /**
324
+ * The ops a request on a known host may run, or the answer that ends it: on
325
+ * the studio's own host the studio hub's alone (studioOps), for a Studio
326
+ * person signed in there; on a client's host every op in the client's scope.
327
+ * The session is read before any project is looked up (S5), so a caller
328
+ * signed out, or signed in for another client, gets one answer whatever
329
+ * project the request names.
330
+ */
331
+ async function opsFor(request, { review, keys }, { host, site }) {
332
+ if (site === STUDIO_SITE) {
333
+ const actor = await actorFrom(request, review, studioGate(keys), STUDIO_SITE);
334
+ return actor ? studioOps(review, actor, host) : signedOut(request);
335
+ }
336
+ const client = site.slug;
337
+ const actor = await actorFrom(request, review, { ...keys, client, since: site.createdAt }, { client });
338
+ if (!actor)
339
+ return signedOut(request);
340
+ const scope = await studioScope(request, review, client);
341
+ return scope ? clientOps({ review, actor, scope }) : json(404, { error: 'Not found.' });
342
+ }
244
343
  /**
245
344
  * A studio site's review function: the host's client is the scope, and the
246
- * gate's client, of every op. The session is read before any project is
247
- * looked up (S5), so a caller signed out, or signed in for another client,
248
- * gets one answer whatever project the request names.
345
+ * gate's client, of every op; the studio's own host has the studio hub's.
249
346
  */
250
347
  export async function studioHandler(request, checks, hosts) {
251
348
  const setup = studioReview(process.env, checks);
252
349
  if ('error' in setup)
253
350
  return json(503, { error: setup.error });
254
- let ctx;
351
+ let ops;
255
352
  try {
256
- const found = await hostClient(request, setup.review, hosts);
257
- if (!found)
258
- return json(404, { error: 'Not found.' });
259
- const client = found.slug;
260
- const gate = { ...setup.keys, client, since: found.createdAt };
261
- const actor = await actorFrom(request, setup.review, gate, { client });
262
- if (!actor)
263
- return signedOut(request);
264
- const scope = await studioScope(request, setup.review, client);
265
- if (!scope)
266
- return json(404, { error: 'Not found.' });
267
- ctx = { review: setup.review, actor, scope };
353
+ const found = await hostSite(request, setup.review, hosts);
354
+ ops = found ? await opsFor(request, setup, found) : json(404, { error: 'Not found.' });
268
355
  }
269
356
  catch (error) {
270
357
  console.error('[review]', error);
271
358
  return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
272
359
  }
273
- return handleReview(request, ctx);
360
+ return ops instanceof Response ? ops : answer(request, ops);
274
361
  }
@@ -1,11 +1,11 @@
1
- import { type Client } from '../core/review/model.ts';
2
- export type HostLookup = (host: string, load: (host: string) => Promise<Client | null>) => Promise<Client | null>;
1
+ import { type Site } from '../core/review/model.ts';
2
+ export type HostLookup = (host: string, load: (host: string) => Promise<Site>) => Promise<Site>;
3
3
  /** One instance's cache of host answers, the unknown ones included. `now` is its clock, so tests need not wait. */
4
4
  export declare function hostCache(now?: () => Date): HostLookup;
5
5
  /**
6
6
  * The host a request was sent to, as the platform hands it on: the forwarded
7
7
  * host, else Host, else the URL's (a function's URL can carry the deployment's
8
8
  * own host). A client may send any of these; the cookie it holds is signed to
9
- * one client, so naming another host opens nothing.
9
+ * one client, or to the studio's host, so naming another host opens nothing.
10
10
  */
11
11
  export declare const hostOf: (request: Request) => string;
@@ -1,8 +1,8 @@
1
- // Which client a studio site's host serves. The gate and every studio
2
- // function ask on each request, so the answer is kept in memory for a minute
3
- // per instance: archiving a client, or adding one, takes effect within
4
- // HOST_SECONDS. A store that does not answer is never kept, so an outage ends
5
- // when the store is back.
1
+ // What a studio site's host serves: a client, or the studio's own host. The
2
+ // gate and every studio function ask on each request, so the answer is kept
3
+ // in memory for a minute per instance: archiving a client, or adding one,
4
+ // takes effect within HOST_SECONDS. A store that does not answer is never
5
+ // kept, so an outage ends when the store is back.
6
6
  import { HOST_SECONDS } from '../core/review/model.js';
7
7
  /** Hosts kept at once: a wildcard domain sends any made-up name to the gate, so the map is bounded. */
8
8
  const MAX_HOSTS = 500;
@@ -13,19 +13,19 @@ export function hostCache(now = () => new Date()) {
13
13
  const at = now().getTime();
14
14
  const hit = seen.get(host);
15
15
  if (hit && at - hit.at < HOST_SECONDS * 1000)
16
- return hit.client;
17
- const client = await load(host);
16
+ return hit.site;
17
+ const site = await load(host);
18
18
  seen.delete(host);
19
19
  if (seen.size >= MAX_HOSTS)
20
20
  seen.delete(seen.keys().next().value);
21
- seen.set(host, { client, at });
22
- return client;
21
+ seen.set(host, { site, at });
22
+ return site;
23
23
  };
24
24
  }
25
25
  /**
26
26
  * The host a request was sent to, as the platform hands it on: the forwarded
27
27
  * host, else Host, else the URL's (a function's URL can carry the deployment's
28
28
  * own host). A client may send any of these; the cookie it holds is signed to
29
- * one client, so naming another host opens nothing.
29
+ * one client, or to the studio's host, so naming another host opens nothing.
30
30
  */
31
31
  export const hostOf = (request) => request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host;
@@ -21,6 +21,13 @@ export type Gate = {
21
21
  };
22
22
  /** What the studio's env gives every gate: the secret and the store it signs to. The host gives the rest. */
23
23
  export type GateKeys = Pick<Gate, 'secret' | 'site'>;
24
+ /**
25
+ * The gate on the studio's own host (`studio.<apex>`): its tokens are signed
26
+ * to `studio`, which no client's slug is, so none verifies on a client's
27
+ * host and no client's verifies here. The studio's host is never archived
28
+ * and added again, so it has no `since` to tell two apart.
29
+ */
30
+ export declare const studioGate: (keys: GateKeys) => Gate;
24
31
  /** The gate's keys, or null when GATE_SECRET is missing or short (the gate then fails closed). */
25
32
  export declare function readGate(env: Record<string, string | undefined>): GateKeys | null;
26
33
  /**
@@ -14,11 +14,18 @@
14
14
  // Every function that reads the clock takes `at`, so tests need not wait.
15
15
  import { base64url } from '../core/bytes.js';
16
16
  import { SLUG } from '../core/feature.js';
17
- import { PERSON_KINDS } from '../core/review/model.js';
17
+ import { PERSON_KINDS, STUDIO_SITE } from '../core/review/model.js';
18
18
  export const PERSON_COOKIE = '__Host-hub_person';
19
19
  const MAX_SESSION_SECONDS = 60 * 60 * 24 * 400;
20
20
  /** How long the store's last yes is trusted before the gate asks again. */
21
21
  export const CHECK_SECONDS = 5 * 60;
22
+ /**
23
+ * The gate on the studio's own host (`studio.<apex>`): its tokens are signed
24
+ * to `studio`, which no client's slug is, so none verifies on a client's
25
+ * host and no client's verifies here. The studio's host is never archived
26
+ * and added again, so it has no `since` to tell two apart.
27
+ */
28
+ export const studioGate = (keys) => ({ ...keys, client: STUDIO_SITE, since: '' });
22
29
  /** The gate's keys, or null when GATE_SECRET is missing or short (the gate then fails closed). */
23
30
  export function readGate(env) {
24
31
  const secret = env.GATE_SECRET ?? '';
@@ -1,6 +1,6 @@
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';
2
+ import { type Client, type Person, type PersonKind, type Project, type Scope } from '../core/review/model.ts';
3
+ import { type Review } from '../core/review/service.ts';
4
4
  import { type HostLookup } from '../server/hosts.ts';
5
5
  import { type Gate } from '../server/session.ts';
6
6
  export declare const DEFAULT_SITE_NAME = "Prototypes";
@@ -58,7 +58,11 @@ export type Refusal = {
58
58
  refused: 'signed-out';
59
59
  stale: boolean;
60
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. */
61
+ /**
62
+ * The client `pathname` is asked of on the request's host, or why there is
63
+ * none: an unknown host is the plain 404, and so is the studio's own host,
64
+ * which serves no client's anything.
65
+ */
62
66
  export declare function findPlace(request: Request, pathname: string, hosts: HostLookup, at: Date): Promise<Found | Refusal>;
63
67
  /** A request that may go on: who asks, the person to re-issue a cookie for, and the project it is for. */
64
68
  export type Admitted = Found & {