@milesroxas/design-hub 0.11.1 → 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 (60) 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 +32 -3
  5. package/core/review/service.test.ts +240 -0
  6. package/core/review/service.ts +281 -50
  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 +25 -1
  11. package/dist/core/review/model.js +18 -2
  12. package/dist/core/review/service.d.ts +98 -16
  13. package/dist/core/review/service.js +254 -45
  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/review.d.ts +5 -2
  22. package/dist/scripts/lib/review.js +8 -2
  23. package/dist/scripts/lib/studios.d.ts +3 -1
  24. package/dist/scripts/lib/studios.js +2 -0
  25. package/dist/scripts/people.d.ts +8 -5
  26. package/dist/scripts/people.js +152 -41
  27. package/dist/scripts/share.js +1 -1
  28. package/dist/scripts/studio.d.ts +3 -3
  29. package/dist/scripts/studio.js +100 -10
  30. package/dist/server/api.d.ts +6 -8
  31. package/dist/server/api.js +129 -42
  32. package/dist/server/hosts.d.ts +3 -3
  33. package/dist/server/hosts.js +10 -10
  34. package/dist/server/session.d.ts +7 -0
  35. package/dist/server/session.js +8 -1
  36. package/dist/vercel/proxy.d.ts +7 -3
  37. package/dist/vercel/proxy.js +215 -49
  38. package/dist/vite/plugin.d.ts +6 -2
  39. package/dist/vite/plugin.js +21 -3
  40. package/package.json +1 -1
  41. package/shell/Index.tsx +29 -0
  42. package/shell/review/People.tsx +80 -61
  43. package/shell/review/Review.tsx +49 -1
  44. package/shell/review/ThreadCard.tsx +13 -1
  45. package/shell/review/client.ts +56 -22
  46. package/shell/review/review.css +1 -0
  47. package/shell/review/unsent.test.ts +63 -0
  48. package/shell/review/unsent.ts +64 -0
  49. package/shell/review/view.ts +22 -6
  50. package/shell/studio/ClientHub.tsx +9 -36
  51. package/shell/studio/StudioHub.tsx +293 -0
  52. package/shell/studio/hub.css +80 -5
  53. package/shell/studio/page.tsx +57 -0
  54. package/shell/studio/studio.html +14 -0
  55. package/shell/studio/studio.tsx +19 -0
  56. package/skills/miles-deploy/SKILL.md +16 -6
  57. package/skills/miles-design-hub/SKILL.md +3 -3
  58. package/skills/miles-review/SKILL.md +1 -1
  59. package/templates/studio/README.md +32 -13
  60. package/templates/studio/package.json +1 -1
@@ -35,8 +35,11 @@ import {
35
35
  checkTriage,
36
36
  checkViewAs,
37
37
  DEFAULT_ACCESS,
38
+ HANDOFF_SECONDS,
39
+ type Handoff,
38
40
  HOST_SECONDS,
39
41
  hasAccess,
42
+ isAdded,
40
43
  isProfile,
41
44
  isRoot,
42
45
  isStudioClient,
@@ -51,6 +54,8 @@ import {
51
54
  readComment,
52
55
  readProject,
53
56
  type Scope,
57
+ type Site,
58
+ STUDIO_SITE,
54
59
  type Triage,
55
60
  } from './model.ts';
56
61
  import { type Action, audienceFor, can, inScope, viewOf, visibleTo } from './policy.ts';
@@ -136,6 +141,15 @@ export const NO_PROJECT = 'No such project.';
136
141
  type Home = 'studio' | Scope;
137
142
  const homeOf = ({ client }: Pick<Person, 'client'>): Home => (client === null ? 'studio' : { client });
138
143
 
144
+ /**
145
+ * What a host the gate answers runs in: one client's scope, or the studio's
146
+ * own host (STUDIO_SITE), where only Studio people sign in.
147
+ */
148
+ export type HostScope = Scope | typeof STUDIO_SITE;
149
+ /** Whether a person may be signed in where `at` is: in the client's scope, or Studio on the studio's host. */
150
+ const reaches = (p: Person, at: HostScope) =>
151
+ at === STUDIO_SITE ? p.kind === 'studio' && p.client === null : inScope(p, at);
152
+
139
153
  /** A person and a new one-time link's token for them. */
140
154
  type Issued = { person: Person; token: string };
141
155
 
@@ -289,11 +303,16 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
289
303
 
290
304
  type Found = { home: Home; hash: string; link: Link };
291
305
 
292
- /** The link a token names in the scope's client or the studio, or null for a malformed or unknown token. */
293
- async function findLink(scope: Scope, token: string): Promise<Found | null> {
306
+ /**
307
+ * The link a token names where `at` reaches: a client's scope finds its
308
+ * own client's links and the studio's; the studio's own host only the
309
+ * studio's. Null for a malformed or unknown token.
310
+ */
311
+ async function findLink(at: HostScope, token: string): Promise<Found | null> {
294
312
  if (!LINK_TOKEN.test(token)) return null;
295
313
  const hash = await linkKey(token);
296
- for (const home of [scope, 'studio'] as const) {
314
+ const homes: Home[] = at === STUDIO_SITE ? ['studio'] : [at, 'studio'];
315
+ for (const home of homes) {
297
316
  const link = parse<Link>(await kv.hget(keyOf(home, 'links'), hash));
298
317
  if (link) return { home, hash, link };
299
318
  }
@@ -301,10 +320,10 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
301
320
  }
302
321
 
303
322
  /** The person a current link signs in, while they may sign in here. */
304
- async function linkPerson(scope: Scope, { home, link }: Found): Promise<Person | null> {
323
+ async function linkPerson(at: HostScope, { home, link }: Found): Promise<Person | null> {
305
324
  if (link.expiresAt <= now()) return null;
306
325
  const p = await personAt(home, link.person);
307
- return active(p) && inScope(p, scope) ? p : null;
326
+ return active(p) && reaches(p, at) ? p : null;
308
327
  }
309
328
 
310
329
  async function studioDefault(): Promise<Access> {
@@ -320,10 +339,13 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
320
339
 
321
340
  /** A new Studio person, with no link yet; their records live with the studio. */
322
341
  async function newStudioPerson(input: unknown): Promise<Person> {
323
- const { access, ...fields } = valid(checkPersonInput({ ...(input as object), kind: 'studio' }));
342
+ const { access, team, ...fields } = valid(checkPersonInput({ ...(input as object), kind: 'studio' }));
343
+ // The studio's first person names the studio's team: there is no one to take it from.
344
+ if (!team) throw new ReviewError('invalid', 'Name the team.', ['team: required']);
324
345
  const p: Person = {
325
346
  id: newId(),
326
347
  ...fields,
348
+ team,
327
349
  client: null,
328
350
  email: null,
329
351
  accessEndsAt: accessEnd(access ?? 'open', deps.now()),
@@ -380,16 +402,41 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
380
402
  return scope.client;
381
403
  }
382
404
 
405
+ /**
406
+ * The first check of an op on people, where `at` is: in a client's scope,
407
+ * as `allow` checks every op there; on the studio's own host, Studio, over
408
+ * the studio's own people alone.
409
+ */
410
+ async function allowPeople(at: HostScope, actor: Person | null): Promise<Person> {
411
+ if (at !== STUDIO_SITE) return allow(at, actor, 'people', NO_PERSON);
412
+ allowStudio(actor);
413
+ return actor;
414
+ }
415
+
416
+ /** Someone `at` reaches: a Client of the scope's client or a Studio person; on the studio's host, Studio only. */
417
+ const personHere = (at: HostScope, id: string): Promise<Person | null> =>
418
+ at === STUDIO_SITE ? personAt('studio', id) : person(at, id);
419
+
420
+ /**
421
+ * A client the studio's own host may act for: a studio client's slug, its
422
+ * record there and not archived, added (provenAt), on a host name. Else
423
+ * null, whatever the reason, so each reads as an unknown client.
424
+ */
425
+ async function openable(slug: unknown): Promise<Client | null> {
426
+ const c = isStudioClient(slug) ? await clientAt(slug) : null;
427
+ return c && c.archivedAt === null && isAdded(c) && hostName(c.host) === c.host ? c : null;
428
+ }
429
+
383
430
  /** Someone Studio can still give a link to. */
384
- async function needActive(scope: Scope, id: string): Promise<Person> {
431
+ async function needActive(scope: HostScope, id: string): Promise<Person> {
385
432
  const p = await needPerson(scope, id);
386
433
  if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
387
434
  if (!active(p)) throw new ReviewError('invalid', 'Their access has ended; change their access first.');
388
435
  return p;
389
436
  }
390
437
 
391
- async function needPerson(scope: Scope, id: string): Promise<Person> {
392
- const p = await person(scope, id);
438
+ async function needPerson(scope: HostScope, id: string): Promise<Person> {
439
+ const p = await personHere(scope, id);
393
440
  if (!p) throw new ReviewError('not_found', NO_PERSON);
394
441
  return p;
395
442
  }
@@ -496,6 +543,37 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
496
543
  };
497
544
  const saveProject = (p: Project) =>
498
545
  kv.hset(keyOf({ client: p.client }, 'projects'), p.slug, JSON.stringify(p));
546
+ /** Every well-formed project of a client, by name, built or not. */
547
+ const projectsOf = async (client: string): Promise<Project[]> =>
548
+ Object.values(await kv.hgetall(keyOf({ client }, 'projects')))
549
+ .map(readProject)
550
+ .filter((p): p is Project => p?.client === client)
551
+ .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
552
+
553
+ /**
554
+ * What a host serves: null for a host no client has, an archived client,
555
+ * or records that disagree, so an unknown host is never a scope; the
556
+ * studio's own host when its entry names it (setStudioHost).
557
+ */
558
+ async function siteOfHost(host: string): Promise<Site> {
559
+ const name = hostName(host);
560
+ if (!name) return null;
561
+ const slug = await kv.hget(keyOf('studio', 'hosts'), name);
562
+ if (slug === STUDIO_SITE) return STUDIO_SITE;
563
+ const c = isClientSlug(slug) ? await clientAt(slug) : null;
564
+ return c && c.host === name && c.archivedAt === null ? c : null;
565
+ }
566
+
567
+ /** A stored handoff that is still good, or null for one past its time or not well formed. */
568
+ function readHandoff(json: string | null): Handoff | null {
569
+ const h = safeParse(json) as Partial<Handoff> | null;
570
+ if (!h || typeof h.person !== 'string' || typeof h.expiresAt !== 'string' || h.expiresAt <= now())
571
+ return null;
572
+ const project = h.project ?? null;
573
+ return project === null || isProjectSlug(project)
574
+ ? { person: h.person, project, expiresAt: h.expiresAt }
575
+ : null;
576
+ }
499
577
 
500
578
  return {
501
579
  person,
@@ -506,11 +584,160 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
506
584
  * records that disagree, so an unknown host is never a scope.
507
585
  */
508
586
  async clientOfHost(host: string): Promise<Client | null> {
509
- const name = hostName(host);
510
- if (!name) return null;
511
- const slug = await kv.hget(keyOf('studio', 'hosts'), name);
512
- const c = isClientSlug(slug) ? await clientAt(slug) : null;
513
- return c && c.host === name && c.archivedAt === null ? c : null;
587
+ const site = await siteOfHost(host);
588
+ return site === STUDIO_SITE ? null : site;
589
+ },
590
+
591
+ /**
592
+ * What a host serves, asked by the gate and the studio's functions with
593
+ * no actor: a client (as clientOfHost), the studio's own host, or null.
594
+ */
595
+ siteOfHost,
596
+
597
+ /**
598
+ * Whether the studio's own host has answered its proof (endStudioProof):
599
+ * from then on commands leave it be, and its owner has had a link there.
600
+ * Studio only.
601
+ */
602
+ async studioHostProven(actor: Person | null): Promise<boolean> {
603
+ allowStudio(actor);
604
+ return (await kv.hget(keyOf('studio', 'settings'), 'hostProvenAt')) !== null;
605
+ },
606
+
607
+ /**
608
+ * A new proof for the studio's own host, in place of any before it, as
609
+ * startProof is for a client's: only the gate reading this store answers
610
+ * its ask there. Studio only.
611
+ */
612
+ async startStudioProof(actor: Person | null): Promise<{ ask: string; answer: string }> {
613
+ allowStudio(actor);
614
+ const ask = base64url(deps.random(32));
615
+ const answer = base64url(deps.random(32));
616
+ const key = keyOf('studio', 'proof');
617
+ await kv.del(key);
618
+ await kv.hset(key, await linkKey(ask), answer);
619
+ return { ask, answer };
620
+ },
621
+
622
+ /** Ends the studio host's proof once it answered, marking the host proven; the mark goes on first. Studio only. */
623
+ async endStudioProof(actor: Person | null): Promise<void> {
624
+ allowStudio(actor);
625
+ await kv.hsetnx(keyOf('studio', 'settings'), 'hostProvenAt', now());
626
+ await kv.del(keyOf('studio', 'proof'));
627
+ },
628
+
629
+ /** The answer to the studio host's proof, asked by the gate on that host with no actor: null for any other ask. */
630
+ async studioProofAnswer(ask: string): Promise<string | null> {
631
+ if (typeof ask !== 'string' || !LINK_TOKEN.test(ask)) return null;
632
+ return kv.hget(keyOf('studio', 'proof'), await linkKey(ask));
633
+ },
634
+
635
+ /**
636
+ * Makes `host` the studio's own host (`studio.<apex>`), where Studio
637
+ * signs in and sees every client. Refused for a host a client has, or
638
+ * one that is not a host name. Safe to run again. Studio only.
639
+ */
640
+ async setStudioHost(actor: Person | null, host: string): Promise<'added' | 'kept'> {
641
+ allowStudio(actor);
642
+ if (hostName(host) !== host)
643
+ throw new ReviewError('invalid', 'Some fields are not valid.', ['host: a host name in lower case']);
644
+ const hosts = keyOf('studio', 'hosts');
645
+ if (await kv.hsetnx(hosts, host, STUDIO_SITE)) return 'added';
646
+ if ((await kv.hget(hosts, host)) === STUDIO_SITE) return 'kept';
647
+ throw new ReviewError('invalid', 'That host already serves a client.', ['host: the studio’s own']);
648
+ },
649
+
650
+ /**
651
+ * Every client record the studio has, by name, each with all its
652
+ * projects by name, archived ones too (an archive that stopped part way).
653
+ * The studio's own view, for its hub and `client list`. Studio only.
654
+ */
655
+ async listClients(actor: Person | null): Promise<{ client: Client; projects: Project[] }[]> {
656
+ allowStudio(actor);
657
+ const records = Object.entries(await kv.hgetall(keyOf('studio', 'clients')))
658
+ .map(([slug, json]) => ({ slug, c: readClient(json) }))
659
+ .filter((r): r is { slug: string; c: Client } => r.c?.slug === r.slug)
660
+ .map((r) => r.c)
661
+ .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
662
+ return Promise.all(
663
+ records.map(async (client) => ({ client, projects: await projectsOf(client.slug) })),
664
+ );
665
+ },
666
+
667
+ /**
668
+ * The client an op on the studio's own host names, for its people: known,
669
+ * not archived and added, as a handoff checks it (openable). Anything
670
+ * else is not found, an unknown client's answer. Studio only.
671
+ */
672
+ async studioClient(actor: Person | null, slug: unknown): Promise<Client> {
673
+ allowStudio(actor);
674
+ const c = await openable(slug);
675
+ if (!c) throw new ReviewError('not_found', NO_CLIENT);
676
+ return c;
677
+ },
678
+
679
+ /**
680
+ * A handoff: the actor, a Studio person signed in on the studio's host,
681
+ * opens one client's hub, or one of its projects with a build, on the
682
+ * client's host. Answers the host from the client's record and a new
683
+ * token, which the host claims once within HANDOFF_SECONDS (claimHandoff).
684
+ * A client archived, gone or still being added (no provenAt), a project
685
+ * it does not have or has not built, and a host that is no host name are
686
+ * all not found. The store keeps the token as its hash. Studio only.
687
+ */
688
+ async startHandoff(
689
+ actor: Person | null,
690
+ input: { client: unknown; project: unknown },
691
+ ): Promise<{ host: string; token: string }> {
692
+ allowStudio(actor);
693
+ const c = await openable(input.client);
694
+ if (!c) throw new ReviewError('not_found', NO_CLIENT);
695
+ const project = input.project ?? null;
696
+ if (project !== null) {
697
+ const p = isProjectSlug(project) ? await projectAt({ client: c.slug, project }) : null;
698
+ if (!p?.build) throw new ReviewError('not_found', NO_PROJECT);
699
+ }
700
+ const key = keyOf({ client: c.slug }, 'handoffs');
701
+ // Handoffs nobody claimed go as new ones are made, so the record never grows past a few.
702
+ for (const [hash, json] of Object.entries(await kv.hgetall(key)))
703
+ if (!readHandoff(json)) await kv.hdel(key, hash);
704
+ const token = base64url(deps.random(32));
705
+ const handoff: Handoff = {
706
+ person: actor.id,
707
+ project: project as string | null,
708
+ expiresAt: new Date(deps.now().getTime() + HANDOFF_SECONDS * 1000).toISOString(),
709
+ };
710
+ await kv.hset(key, await linkKey(token), JSON.stringify(handoff));
711
+ return { host: c.host, token };
712
+ },
713
+
714
+ /**
715
+ * Claims a handoff on the scope's client's host, asked by the gate with
716
+ * no actor: the Studio person it was made for and the project to open
717
+ * (null for the client's hub), or null. Only a handoff made for this
718
+ * client is found; one past HANDOFF_SECONDS, one for a client no longer
719
+ * added, or a person who may no longer sign in is null. The delete is
720
+ * the claim, as useLink's, so of two requests with one token only one
721
+ * gets a person; every check comes before it.
722
+ */
723
+ async claimHandoff(
724
+ scope: Scope,
725
+ token: string,
726
+ ): Promise<{ person: Person; project: string | null } | null> {
727
+ if (!isStudioClient(scope.client) || typeof token !== 'string' || !LINK_TOKEN.test(token)) return null;
728
+ const key = keyOf({ client: scope.client }, 'handoffs');
729
+ const hash = await linkKey(token);
730
+ const handoff = readHandoff(await kv.hget(key, hash));
731
+ if (!handoff) return null;
732
+ const { project } = handoff;
733
+ const c = await clientAt(scope.client);
734
+ if (!c || c.archivedAt !== null || !isAdded(c)) return null;
735
+ const p = await personAt('studio', handoff.person);
736
+ if (!active(p)) return null;
737
+ if ((await kv.hdel(key, hash)) !== 1) return null;
738
+ // A revoke or sign out everywhere can land between the check and the claim: read again.
739
+ const after = await personAt('studio', p.id);
740
+ return active(after) && after.epoch === p.epoch ? { person: after, project } : null;
514
741
  },
515
742
 
516
743
  /** One project of the scope's client, asked with no actor: null when the client has none by that name. */
@@ -519,11 +746,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
519
746
  /** Every project of the scope's client that has a build to open, by name. */
520
747
  async listProjects(scope: Scope, actor: Person | null): Promise<Project[]> {
521
748
  await allow(scope, actor, 'read', NO_PROJECT);
522
- const stored = Object.values(await kv.hgetall(keyOf({ client: scope.client }, 'projects')));
523
- return stored
524
- .map(readProject)
525
- .filter((p): p is Project => p?.client === scope.client && p.build !== null)
526
- .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
749
+ return (await projectsOf(scope.client)).filter((p) => p.build !== null);
527
750
  },
528
751
 
529
752
  /**
@@ -781,33 +1004,40 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
781
1004
  if (isBuildId(build) && build !== live) await kv.hdel(keyOf(scope, 'builds'), build);
782
1005
  },
783
1006
 
784
- /** The person a signed session names, if they still may sign in with it, here. */
785
- async sessionPerson(scope: Scope, id: string, epoch: number): Promise<Person | null> {
786
- const p = await person(scope, id);
787
- return active(p) && p.epoch === epoch && inScope(p, scope) ? p : null;
1007
+ /** The person a signed session names, if they still may sign in with it, here: in a client's scope, or Studio on the studio's host. */
1008
+ async sessionPerson(at: HostScope, id: string, epoch: number): Promise<Person | null> {
1009
+ const p = at === STUDIO_SITE ? await personAt('studio', id) : await person(at, id);
1010
+ return active(p) && p.epoch === epoch && reaches(p, at) ? p : null;
788
1011
  },
789
1012
 
790
- /** The scope's Clients and every Studio person. */
791
- async listPeople(scope: Scope, actor: Person | null): Promise<Person[]> {
792
- await allow(scope, actor, 'people', NO_PERSON);
793
- const all = [...(await peopleAt('studio')), ...(await peopleAt(scope))];
1013
+ /** The scope's Clients and every Studio person; on the studio's own host, the Studio people alone. */
1014
+ async listPeople(at: HostScope, actor: Person | null): Promise<Person[]> {
1015
+ await allowPeople(at, actor);
1016
+ const all = [...(await peopleAt('studio')), ...(at === STUDIO_SITE ? [] : await peopleAt(at))];
794
1017
  return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
795
1018
  },
796
1019
 
797
1020
  /**
798
1021
  * A new person and their first link: a Client of the scope's client, or a
799
- * Studio person. Without an access length, a Client gets the default and
800
- * Studio is open-ended.
1022
+ * Studio person; on the studio's own host, a Studio person only. Without
1023
+ * an access length, a Client gets the default and Studio is open-ended.
1024
+ * Without a team, a Studio person gets the studio's, which every Studio
1025
+ * person carries, so the adder's; a Client gets their client's name.
801
1026
  */
802
- async addPerson(scope: Scope, actor: Person | null, input: unknown): Promise<Issued> {
803
- await allow(scope, actor, 'people', NO_PERSON);
804
- const { access, ...fields } = valid(checkPersonInput(input));
1027
+ async addPerson(at: HostScope, actor: Person | null, input: unknown): Promise<Issued> {
1028
+ const by = await allowPeople(at, actor);
1029
+ const { access, team, ...fields } = valid(checkPersonInput(input));
1030
+ // A Client belongs to one client: the studio's own host names none.
1031
+ const scope = fields.kind === 'studio' ? null : at;
1032
+ if (scope === STUDIO_SITE)
1033
+ throw new ReviewError('invalid', 'Some fields are not valid.', ['kind: studio here']);
805
1034
  // S13: the person's own length, else (a Client) the client's default, then the studio's, then 3m.
806
- const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault(scope));
1035
+ const length = access ?? (scope ? await readDefault(scope) : 'open');
807
1036
  const p: Person = {
808
1037
  id: newId(),
809
1038
  ...fields,
810
- client: fields.kind === 'studio' ? null : scope.client,
1039
+ team: team ?? (scope ? ((await clientAt(scope.client))?.name ?? 'Client') : by.team),
1040
+ client: scope ? scope.client : null,
811
1041
  email: null,
812
1042
  accessEndsAt: accessEnd(length, deps.now()),
813
1043
  createdAt: now(),
@@ -819,15 +1049,15 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
819
1049
  },
820
1050
 
821
1051
  /** Another one-time link, for another device. Their sessions and other unused links stay. */
822
- async newLink(scope: Scope, actor: Person | null, id: string): Promise<Issued> {
823
- await allow(scope, actor, 'people', NO_PERSON);
1052
+ async newLink(scope: HostScope, actor: Person | null, id: string): Promise<Issued> {
1053
+ await allowPeople(scope, actor);
824
1054
  const p = await needActive(scope, id);
825
1055
  return { person: p, token: await issueLink(p) };
826
1056
  },
827
1057
 
828
1058
  /** Ends every session they hold and every unused link, and gives one new link. */
829
- async signOutEverywhere(scope: Scope, actor: Person | null, id: string): Promise<Issued> {
830
- await allow(scope, actor, 'people', NO_PERSON);
1059
+ async signOutEverywhere(scope: HostScope, actor: Person | null, id: string): Promise<Issued> {
1060
+ await allowPeople(scope, actor);
831
1061
  const p = await needActive(scope, id);
832
1062
  // The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
833
1063
  const next = { ...p, epoch: await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1) };
@@ -835,8 +1065,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
835
1065
  return { person: next, token: await issueLink(next) };
836
1066
  },
837
1067
 
838
- async revoke(scope: Scope, actor: Person | null, id: string): Promise<Person> {
839
- const by = await allow(scope, actor, 'people', NO_PERSON);
1068
+ async revoke(scope: HostScope, actor: Person | null, id: string): Promise<Person> {
1069
+ const by = await allowPeople(scope, actor);
840
1070
  if (by.id === id) throw new ReviewError('invalid', 'You cannot revoke yourself.');
841
1071
  const p = await needPerson(scope, id);
842
1072
  const revokedAt = p.revokedAt ?? now();
@@ -849,8 +1079,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
849
1079
  },
850
1080
 
851
1081
  /** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
852
- async setAccess(scope: Scope, actor: Person | null, id: string, access: unknown): Promise<Person> {
853
- await allow(scope, actor, 'people', NO_PERSON);
1082
+ async setAccess(scope: HostScope, actor: Person | null, id: string, access: unknown): Promise<Person> {
1083
+ await allowPeople(scope, actor);
854
1084
  const length = valid(checkAccess(access));
855
1085
  const p = await needPerson(scope, id);
856
1086
  if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
@@ -890,7 +1120,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
890
1120
  return newStudioPerson(input);
891
1121
  },
892
1122
 
893
- /** Another one-time link for a Studio person: it signs in one device, on whichever client host it is opened. Studio only. */
1123
+ /** Another one-time link for a Studio person: it signs in one device, on the studio's host or whichever client host it is opened. Studio only. */
894
1124
  async newStudioLink(actor: Person | null, id: string): Promise<Issued> {
895
1125
  allowStudio(actor);
896
1126
  const p = await personAt('studio', id);
@@ -913,9 +1143,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
913
1143
  },
914
1144
 
915
1145
  /** Who a link signs in here, without using it: mail and chat previews open links too. */
916
- async peekLink(scope: Scope, token: string): Promise<Person | null> {
917
- const found = await findLink(scope, token);
918
- return found ? linkPerson(scope, found) : null;
1146
+ async peekLink(at: HostScope, token: string): Promise<Person | null> {
1147
+ const found = await findLink(at, token);
1148
+ return found ? linkPerson(at, found) : null;
919
1149
  },
920
1150
 
921
1151
  /**
@@ -923,12 +1153,13 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
923
1153
  * two requests with one token, only the one whose delete removed the link
924
1154
  * gets a person, so a link signs in one device at most. Who it signs in is
925
1155
  * read and checked first, so the delete is the last store call. Only a
926
- * link of the scope's client, or a Studio person's, is found at all.
1156
+ * link of the scope's client, or a Studio person's, is found at all; on
1157
+ * the studio's own host, only a Studio person's.
927
1158
  */
928
- async useLink(scope: Scope, token: string): Promise<Person | null> {
929
- const found = await findLink(scope, token);
1159
+ async useLink(at: HostScope, token: string): Promise<Person | null> {
1160
+ const found = await findLink(at, token);
930
1161
  // Every read and check comes before the delete, so a store failure before it leaves the link usable.
931
- const p = found && (await linkPerson(scope, found));
1162
+ const p = found && (await linkPerson(at, found));
932
1163
  if (!found || !p) return null;
933
1164
  if ((await kv.hdel(keyOf(found.home, 'links'), found.hash)) !== 1) return null;
934
1165
  // A revoke or sign out everywhere can land between the check and the claim, before its
@@ -42,16 +42,20 @@ const PREFIX = 'hub4:';
42
42
  /**
43
43
  * The studio's own records: Studio people (profile, revocation, epoch and
44
44
  * links, as a client's below) and `settings`, one field each: `access` is the
45
- * default access length. `mark`, one field, `id`: the id of the one studio
45
+ * default access length, `hostProvenAt` when the studio's own host first
46
+ * answered its proof. `proof`: while a command waits for the studio's own
47
+ * host, one field, hex(sha256(ask)) to the answer the gate gives there.
48
+ * `mark`, one field, `id`: the id of the one studio
46
49
  * this database serves (its studio.json `id`), set once by `studio create`
47
50
  * and checked by every command before it acts (server/mark.ts). No other
48
51
  * key names a studio, so two studios on one database would be one studio.
49
52
  */
50
- type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark';
53
+ type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark' | 'proof';
51
54
 
52
55
  /**
53
56
  * The studio's clients, outside every scope: `clients`, a client's slug to
54
- * its Client; `hosts`, a host name to the slug of the client it serves;
57
+ * its Client; `hosts`, a host name to the slug of the client it serves, or
58
+ * to `studio` (STUDIO_SITE) for the studio's own host, which no client slug is;
55
59
  * `claims`, one counter per slug (`client:<slug>`) and per host
56
60
  * (`host:<host>`), raised by HINCRBY when a client is added: whoever raised
57
61
  * it to 1 holds it, so two adds at once never both get one. Archiving a
@@ -72,9 +76,11 @@ const DIRECTORY: readonly string[] = ['clients', 'hosts', 'claims', 'held'];
72
76
  * `projects`: the client's projects, a slug to its Project. `proof`: while
73
77
  * `client add` waits for the client's host, one field, hex(sha256(ask)) to
74
78
  * the answer the gate gives for that ask (PROOF_PATH in prototype.ts).
79
+ * `handoffs`: Studio opening this client from the studio's host,
80
+ * hex(sha256(token)) to a Handoff, deleted when the client's host claims it.
75
81
  */
76
82
  type ClientName = 'people' | 'revoked' | 'epochs' | 'links';
77
- type ClientOnlyName = 'projects' | 'proof';
83
+ type ClientOnlyName = 'projects' | 'proof' | 'handoffs';
78
84
 
79
85
  /**
80
86
  * A project's records. `audiences`: which prototypes are client-facing,
@@ -32,11 +32,15 @@ export declare const GATE_FONT = "/_gate/geist.woff2";
32
32
  */
33
33
  export declare const PROOF_PATH: RegExp;
34
34
  export declare const proofPath: (ask: string) => string;
35
+ export declare const handoffPath: (token: string) => string;
36
+ /** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
37
+ export declare const HANDOFF_START = "/_gate/handoff";
35
38
  /**
36
39
  * Where a request on a studio site goes:
37
40
  *
38
41
  * - `none`: nothing; the gate's plain 404, whoever asks.
39
42
  * - `link`: `/invite/<token>`, at the host root only.
43
+ * - `handoff`: `/_gate/handoff/<token>`, a handoff's claim, at the host root only.
40
44
  * - `asset`: the gate's font and the favicon, served before sign-in.
41
45
  * - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
42
46
  * - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
@@ -48,6 +52,9 @@ export type Where = {
48
52
  } | {
49
53
  kind: 'link';
50
54
  token: string;
55
+ } | {
56
+ kind: 'handoff';
57
+ token: string;
51
58
  } | {
52
59
  kind: 'asset';
53
60
  } | {
@@ -67,6 +74,49 @@ export type Route = {
67
74
  } & Where;
68
75
  /** A host name as a lookup key: lower case, no port, no trailing dot; '' for anything that is not one. */
69
76
  export declare function hostName(host: string): string;
77
+ /**
78
+ * The studio hub's own files, which the studio site's build puts beside the
79
+ * client hub (studioConfig in vite/plugin.ts): its page, and its bundle under
80
+ * one folder. Neither is a path a client's host answers: `_studio` and
81
+ * `studio.html` are no project slug, so routeOf sends both nowhere.
82
+ */
83
+ export declare const STUDIO_DIR = "_studio";
84
+ export declare const STUDIO_PAGE = "/studio.html";
85
+ /**
86
+ * Where a request on the studio's own host (`studio.<apex>`) goes. Only
87
+ * Studio signs in here, and nothing of a client's host is answered:
88
+ *
89
+ * - `none`: nothing; the gate's plain 404.
90
+ * - `link`: `/invite/<token>`, a Studio person's link.
91
+ * - `asset`: the gate's font and the favicon, served before sign-in.
92
+ * - `page`: `/`, the studio hub.
93
+ * - `file`: the studio hub's bundle, below `/_studio/`.
94
+ * - `api`: `/api/review`, where the studio hub reads every client.
95
+ * - `handoff`: `/_gate/handoff`, where the studio hub asks to open a client.
96
+ * - `proof`: `/_gate/proof/<ask>`, the host proof `studio create` and
97
+ * `studio deploy` ask the studio's own host for, as `client add` asks a client's.
98
+ */
99
+ export type StudioWhere = {
100
+ kind: 'none';
101
+ } | {
102
+ kind: 'link';
103
+ token: string;
104
+ } | {
105
+ kind: 'asset';
106
+ } | {
107
+ kind: 'page';
108
+ } | {
109
+ kind: 'file';
110
+ } | {
111
+ kind: 'api';
112
+ } | {
113
+ kind: 'handoff';
114
+ } | {
115
+ kind: 'proof';
116
+ ask: string;
117
+ };
118
+ /** The one place a request's path on the studio's own host is read. It fails closed, as routeOf does. */
119
+ export declare function studioRouteOf(pathname: string): StudioWhere;
70
120
  /**
71
121
  * The one place a studio site reads a request's path. It fails closed: a
72
122
  * path that could mean more than one file (`segments`) goes nowhere. Which client a host is, is the store's to say (a host may be a
@@ -100,6 +100,14 @@ const GATE_ASSETS = [GATE_FONT, '/favicon.svg'];
100
100
  */
101
101
  export const PROOF_PATH = /^\/_gate\/proof\/([\w-]{20,100})$/;
102
102
  export const proofPath = (ask) => `/_gate/proof/${ask}`;
103
+ /**
104
+ * A handoff's claim, on a client's host: the studio's host sends a Studio
105
+ * person here with a one-time token, and the gate signs them in on this host.
106
+ */
107
+ const HANDOFF_PATH = /^\/_gate\/handoff\/([\w-]{20,100})$/;
108
+ export const handoffPath = (token) => `/_gate/handoff/${token}`;
109
+ /** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
110
+ export const HANDOFF_START = '/_gate/handoff';
103
111
  /**
104
112
  * The studio site's functions, at a client host's root: the review API, and
105
113
  * the files function the gate rewrites a project's files to. The gate passes
@@ -122,6 +130,9 @@ function where(pathname) {
122
130
  const linked = pathname.match(LINK_PATH);
123
131
  if (linked)
124
132
  return { kind: 'link', token: linked[1] };
133
+ const handoff = pathname.match(HANDOFF_PATH);
134
+ if (handoff)
135
+ return { kind: 'handoff', token: handoff[1] };
125
136
  if (GATE_ASSETS.includes(pathname))
126
137
  return { kind: 'asset' };
127
138
  const parts = segments(pathname);
@@ -136,6 +147,33 @@ function where(pathname) {
136
147
  : { kind: 'none' };
137
148
  return isProjectSlug(first) ? projectRoute(first, rest) : { kind: 'none' };
138
149
  }
150
+ /**
151
+ * The studio hub's own files, which the studio site's build puts beside the
152
+ * client hub (studioConfig in vite/plugin.ts): its page, and its bundle under
153
+ * one folder. Neither is a path a client's host answers: `_studio` and
154
+ * `studio.html` are no project slug, so routeOf sends both nowhere.
155
+ */
156
+ export const STUDIO_DIR = '_studio';
157
+ export const STUDIO_PAGE = '/studio.html';
158
+ /** The one place a request's path on the studio's own host is read. It fails closed, as routeOf does. */
159
+ export function studioRouteOf(pathname) {
160
+ const linked = pathname.match(LINK_PATH);
161
+ if (linked)
162
+ return { kind: 'link', token: linked[1] };
163
+ if (GATE_ASSETS.includes(pathname))
164
+ return { kind: 'asset' };
165
+ if (pathname === '/')
166
+ return { kind: 'page' };
167
+ if (pathname === '/api/review')
168
+ return { kind: 'api' };
169
+ if (pathname === HANDOFF_START)
170
+ return { kind: 'handoff' };
171
+ const proof = pathname.match(PROOF_PATH);
172
+ if (proof)
173
+ return { kind: 'proof', ask: proof[1] };
174
+ const parts = segments(pathname);
175
+ return parts && parts.length > 1 && parts[0] === STUDIO_DIR ? { kind: 'file' } : { kind: 'none' };
176
+ }
139
177
  /**
140
178
  * The one place a studio site reads a request's path. It fails closed: a
141
179
  * path that could mean more than one file (`segments`) goes nowhere. Which client a host is, is the store's to say (a host may be a