@milesroxas/design-hub 0.11.2 → 0.13.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 (87) hide show
  1. package/README.md +8 -6
  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 +262 -37
  6. package/core/review/service.ts +276 -52
  7. package/core/review/store.ts +12 -6
  8. package/core/review/suite-kv.ts +17 -0
  9. package/dist/core/prototype.d.ts +50 -0
  10. package/dist/core/prototype.js +38 -0
  11. package/dist/core/review/model.d.ts +22 -0
  12. package/dist/core/review/model.js +8 -0
  13. package/dist/core/review/service.d.ts +98 -18
  14. package/dist/core/review/service.js +248 -46
  15. package/dist/core/review/store.d.ts +10 -4
  16. package/dist/core/review/store.js +2 -2
  17. package/dist/core/review/suite-kv.d.ts +7 -0
  18. package/dist/core/review/suite-kv.js +12 -0
  19. package/dist/scripts/client.d.ts +3 -3
  20. package/dist/scripts/client.js +55 -69
  21. package/dist/scripts/lib/cli.js +5 -0
  22. package/dist/scripts/lib/dns.d.ts +32 -0
  23. package/dist/scripts/lib/dns.js +69 -1
  24. package/dist/scripts/lib/fake-host.d.ts +35 -11
  25. package/dist/scripts/lib/fake-host.js +27 -13
  26. package/dist/scripts/lib/host.d.ts +13 -25
  27. package/dist/scripts/lib/host.js +15 -324
  28. package/dist/scripts/lib/studio-deps.d.ts +16 -0
  29. package/dist/scripts/lib/studio-deps.js +41 -16
  30. package/dist/scripts/lib/studios.d.ts +17 -5
  31. package/dist/scripts/lib/studios.js +25 -5
  32. package/dist/scripts/lib/vercel-host.d.ts +17 -0
  33. package/dist/scripts/lib/vercel-host.js +410 -0
  34. package/dist/scripts/people.d.ts +7 -4
  35. package/dist/scripts/people.js +147 -39
  36. package/dist/scripts/share.js +3 -2
  37. package/dist/scripts/studio.d.ts +10 -4
  38. package/dist/scripts/studio.js +136 -22
  39. package/dist/server/api.d.ts +12 -13
  40. package/dist/server/api.js +141 -52
  41. package/dist/server/gate.d.ts +97 -0
  42. package/dist/server/gate.js +537 -0
  43. package/dist/server/hosts.d.ts +3 -3
  44. package/dist/server/hosts.js +10 -10
  45. package/dist/server/mark.d.ts +17 -0
  46. package/dist/server/mark.js +46 -1
  47. package/dist/server/session.d.ts +9 -1
  48. package/dist/server/session.js +10 -3
  49. package/dist/server/stores/backends.d.ts +22 -0
  50. package/dist/server/stores/backends.js +96 -0
  51. package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
  52. package/dist/server/stores/index.d.ts +41 -0
  53. package/dist/server/stores/index.js +63 -0
  54. package/dist/server/stores/pglite.d.ts +16 -0
  55. package/dist/server/stores/pglite.js +98 -0
  56. package/dist/server/stores/postgres.d.ts +63 -0
  57. package/dist/server/stores/postgres.js +153 -0
  58. package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
  59. package/dist/vercel/files.js +1 -1
  60. package/dist/vercel/proxy.d.ts +6 -78
  61. package/dist/vercel/proxy.js +18 -359
  62. package/dist/vite/plugin.d.ts +6 -2
  63. package/dist/vite/plugin.js +22 -4
  64. package/package.json +3 -1
  65. package/shell/Index.tsx +29 -0
  66. package/shell/review/People.tsx +79 -49
  67. package/shell/review/Review.tsx +49 -1
  68. package/shell/review/ThreadCard.tsx +13 -1
  69. package/shell/review/client.ts +54 -21
  70. package/shell/review/review.css +1 -0
  71. package/shell/review/unsent.test.ts +63 -0
  72. package/shell/review/unsent.ts +64 -0
  73. package/shell/review/view.ts +22 -6
  74. package/shell/studio/ClientHub.tsx +9 -36
  75. package/shell/studio/StudioHub.tsx +293 -0
  76. package/shell/studio/hub.css +80 -5
  77. package/shell/studio/page.tsx +57 -0
  78. package/shell/studio/studio.html +14 -0
  79. package/shell/studio/studio.tsx +19 -0
  80. package/skills/miles-deploy/SKILL.md +17 -6
  81. package/skills/miles-design-hub/SKILL.md +3 -3
  82. package/skills/miles-review/SKILL.md +1 -1
  83. package/templates/studio/.env.example +3 -0
  84. package/templates/studio/README.md +45 -13
  85. package/templates/studio/package.json +1 -1
  86. /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
  87. /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
@@ -13,7 +13,7 @@ import { base64url, hex, sha256 } from '../bytes.js';
13
13
  import { controlField, readMarks } from '../controls.js';
14
14
  import { isBuildId } from '../files.js';
15
15
  import { hostName, isClientSlug, isProjectSlug, isPrototypeSlug } from '../prototype.js';
16
- import { accessEnd, authorOf, checkAccess, checkAnchor, checkAudience, checkBody, checkClientInput, checkControl, checkPersonInput, checkProjectInput, checkPrototypeAudience, checkStates, checkTriage, checkViewAs, DEFAULT_ACCESS, HOST_SECONDS, hasAccess, isProfile, isRoot, isStudioClient, LINK_DAYS, LOCAL_CLIENT, readClient, readComment, readProject, } from './model.js';
16
+ import { accessEnd, authorOf, checkAccess, checkAnchor, checkAudience, checkBody, checkClientInput, checkControl, checkPersonInput, checkProjectInput, checkPrototypeAudience, checkStates, checkTriage, checkViewAs, DEFAULT_ACCESS, HANDOFF_SECONDS, HOST_SECONDS, hasAccess, isAdded, isProfile, isRoot, isStudioClient, LINK_DAYS, LOCAL_CLIENT, readClient, readComment, readProject, STUDIO_SITE, } from './model.js';
17
17
  import { audienceFor, can, inScope, viewOf, visibleTo } from './policy.js';
18
18
  import { keyOf, sweepPrefix } from './store.js';
19
19
  export class ReviewError extends Error {
@@ -59,6 +59,8 @@ const NO_PROTOTYPE = 'No such prototype.';
59
59
  const NO_CLIENT = 'No such client.';
60
60
  export const NO_PROJECT = 'No such project.';
61
61
  const homeOf = ({ client }) => (client === null ? 'studio' : { client });
62
+ /** Whether a person may be signed in where `at` is: in the client's scope, or Studio on the studio's host. */
63
+ const reaches = (p, at) => at === STUDIO_SITE ? p.kind === 'studio' && p.client === null : inScope(p, at);
62
64
  export function createReview(kv, overrides = {}) {
63
65
  const deps = { ...defaults, ...overrides };
64
66
  const now = () => deps.now().toISOString();
@@ -197,12 +199,17 @@ export function createReview(kv, overrides = {}) {
197
199
  if (JSON.parse(json).person === p.id)
198
200
  await kv.hdel(links, hash);
199
201
  }
200
- /** The link a token names in the scope's client or the studio, or null for a malformed or unknown token. */
201
- async function findLink(scope, token) {
202
+ /**
203
+ * The link a token names where `at` reaches: a client's scope finds its
204
+ * own client's links and the studio's; the studio's own host only the
205
+ * studio's. Null for a malformed or unknown token.
206
+ */
207
+ async function findLink(at, token) {
202
208
  if (!LINK_TOKEN.test(token))
203
209
  return null;
204
210
  const hash = await linkKey(token);
205
- for (const home of [scope, 'studio']) {
211
+ const homes = at === STUDIO_SITE ? ['studio'] : [at, 'studio'];
212
+ for (const home of homes) {
206
213
  const link = parse(await kv.hget(keyOf(home, 'links'), hash));
207
214
  if (link)
208
215
  return { home, hash, link };
@@ -210,11 +217,11 @@ export function createReview(kv, overrides = {}) {
210
217
  return null;
211
218
  }
212
219
  /** The person a current link signs in, while they may sign in here. */
213
- async function linkPerson(scope, { home, link }) {
220
+ async function linkPerson(at, { home, link }) {
214
221
  if (link.expiresAt <= now())
215
222
  return null;
216
223
  const p = await personAt(home, link.person);
217
- return active(p) && inScope(p, scope) ? p : null;
224
+ return active(p) && reaches(p, at) ? p : null;
218
225
  }
219
226
  async function studioDefault() {
220
227
  const stored = await kv.hget(keyOf('studio', 'settings'), 'access');
@@ -288,6 +295,28 @@ export function createReview(kv, overrides = {}) {
288
295
  throw new ReviewError('not_found', NO_CLIENT);
289
296
  return scope.client;
290
297
  }
298
+ /**
299
+ * The first check of an op on people, where `at` is: in a client's scope,
300
+ * as `allow` checks every op there; on the studio's own host, Studio, over
301
+ * the studio's own people alone.
302
+ */
303
+ async function allowPeople(at, actor) {
304
+ if (at !== STUDIO_SITE)
305
+ return allow(at, actor, 'people', NO_PERSON);
306
+ allowStudio(actor);
307
+ return actor;
308
+ }
309
+ /** Someone `at` reaches: a Client of the scope's client or a Studio person; on the studio's host, Studio only. */
310
+ const personHere = (at, id) => at === STUDIO_SITE ? personAt('studio', id) : person(at, id);
311
+ /**
312
+ * A client the studio's own host may act for: a studio client's slug, its
313
+ * record there and not archived, added (provenAt), on a host name. Else
314
+ * null, whatever the reason, so each reads as an unknown client.
315
+ */
316
+ async function openable(slug) {
317
+ const c = isStudioClient(slug) ? await clientAt(slug) : null;
318
+ return c && c.archivedAt === null && isAdded(c) && hostName(c.host) === c.host ? c : null;
319
+ }
291
320
  /** Someone Studio can still give a link to. */
292
321
  async function needActive(scope, id) {
293
322
  const p = await needPerson(scope, id);
@@ -298,7 +327,7 @@ export function createReview(kv, overrides = {}) {
298
327
  return p;
299
328
  }
300
329
  async function needPerson(scope, id) {
301
- const p = await person(scope, id);
330
+ const p = await personHere(scope, id);
302
331
  if (!p)
303
332
  throw new ReviewError('not_found', NO_PERSON);
304
333
  return p;
@@ -380,6 +409,36 @@ export function createReview(kv, overrides = {}) {
380
409
  return p?.client === client && p.slug === project ? p : null;
381
410
  };
382
411
  const saveProject = (p) => kv.hset(keyOf({ client: p.client }, 'projects'), p.slug, JSON.stringify(p));
412
+ /** Every well-formed project of a client, by name, built or not. */
413
+ const projectsOf = async (client) => Object.values(await kv.hgetall(keyOf({ client }, 'projects')))
414
+ .map(readProject)
415
+ .filter((p) => p?.client === client)
416
+ .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
417
+ /**
418
+ * What a host serves: null for a host no client has, an archived client,
419
+ * or records that disagree, so an unknown host is never a scope; the
420
+ * studio's own host when its entry names it (setStudioHost).
421
+ */
422
+ async function siteOfHost(host) {
423
+ const name = hostName(host);
424
+ if (!name)
425
+ return null;
426
+ const slug = await kv.hget(keyOf('studio', 'hosts'), name);
427
+ if (slug === STUDIO_SITE)
428
+ return STUDIO_SITE;
429
+ const c = isClientSlug(slug) ? await clientAt(slug) : null;
430
+ return c && c.host === name && c.archivedAt === null ? c : null;
431
+ }
432
+ /** A stored handoff that is still good, or null for one past its time or not well formed. */
433
+ function readHandoff(json) {
434
+ const h = safeParse(json);
435
+ if (!h || typeof h.person !== 'string' || typeof h.expiresAt !== 'string' || h.expiresAt <= now())
436
+ return null;
437
+ const project = h.project ?? null;
438
+ return project === null || isProjectSlug(project)
439
+ ? { person: h.person, project, expiresAt: h.expiresAt }
440
+ : null;
441
+ }
383
442
  return {
384
443
  person,
385
444
  /**
@@ -388,23 +447,161 @@ export function createReview(kv, overrides = {}) {
388
447
  * records that disagree, so an unknown host is never a scope.
389
448
  */
390
449
  async clientOfHost(host) {
391
- const name = hostName(host);
392
- if (!name)
450
+ const site = await siteOfHost(host);
451
+ return site === STUDIO_SITE ? null : site;
452
+ },
453
+ /**
454
+ * What a host serves, asked by the gate and the studio's functions with
455
+ * no actor: a client (as clientOfHost), the studio's own host, or null.
456
+ */
457
+ siteOfHost,
458
+ /**
459
+ * Whether the studio's own host has answered its proof (endStudioProof):
460
+ * from then on commands leave it be, and its owner has had a link there.
461
+ * Studio only.
462
+ */
463
+ async studioHostProven(actor) {
464
+ allowStudio(actor);
465
+ return (await kv.hget(keyOf('studio', 'settings'), 'hostProvenAt')) !== null;
466
+ },
467
+ /**
468
+ * A new proof for the studio's own host, in place of any before it, as
469
+ * startProof is for a client's: only the gate reading this store answers
470
+ * its ask there. Studio only.
471
+ */
472
+ async startStudioProof(actor) {
473
+ allowStudio(actor);
474
+ const ask = base64url(deps.random(32));
475
+ const answer = base64url(deps.random(32));
476
+ const key = keyOf('studio', 'proof');
477
+ await kv.del(key);
478
+ await kv.hset(key, await linkKey(ask), answer);
479
+ return { ask, answer };
480
+ },
481
+ /** Ends the studio host's proof once it answered, marking the host proven; the mark goes on first. Studio only. */
482
+ async endStudioProof(actor) {
483
+ allowStudio(actor);
484
+ await kv.hsetnx(keyOf('studio', 'settings'), 'hostProvenAt', now());
485
+ await kv.del(keyOf('studio', 'proof'));
486
+ },
487
+ /** The answer to the studio host's proof, asked by the gate on that host with no actor: null for any other ask. */
488
+ async studioProofAnswer(ask) {
489
+ if (typeof ask !== 'string' || !LINK_TOKEN.test(ask))
490
+ return null;
491
+ return kv.hget(keyOf('studio', 'proof'), await linkKey(ask));
492
+ },
493
+ /**
494
+ * Makes `host` the studio's own host (`studio.<apex>`), where Studio
495
+ * signs in and sees every client. Refused for a host a client has, or
496
+ * one that is not a host name. Safe to run again. Studio only.
497
+ */
498
+ async setStudioHost(actor, host) {
499
+ allowStudio(actor);
500
+ if (hostName(host) !== host)
501
+ throw new ReviewError('invalid', 'Some fields are not valid.', ['host: a host name in lower case']);
502
+ const hosts = keyOf('studio', 'hosts');
503
+ if (await kv.hsetnx(hosts, host, STUDIO_SITE))
504
+ return 'added';
505
+ if ((await kv.hget(hosts, host)) === STUDIO_SITE)
506
+ return 'kept';
507
+ throw new ReviewError('invalid', 'That host already serves a client.', ['host: the studio’s own']);
508
+ },
509
+ /**
510
+ * Every client record the studio has, by name, each with all its
511
+ * projects by name, archived ones too (an archive that stopped part way).
512
+ * The studio's own view, for its hub and `client list`. Studio only.
513
+ */
514
+ async listClients(actor) {
515
+ allowStudio(actor);
516
+ const records = Object.entries(await kv.hgetall(keyOf('studio', 'clients')))
517
+ .map(([slug, json]) => ({ slug, c: readClient(json) }))
518
+ .filter((r) => r.c?.slug === r.slug)
519
+ .map((r) => r.c)
520
+ .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
521
+ return Promise.all(records.map(async (client) => ({ client, projects: await projectsOf(client.slug) })));
522
+ },
523
+ /**
524
+ * The client an op on the studio's own host names, for its people: known,
525
+ * not archived and added, as a handoff checks it (openable). Anything
526
+ * else is not found, an unknown client's answer. Studio only.
527
+ */
528
+ async studioClient(actor, slug) {
529
+ allowStudio(actor);
530
+ const c = await openable(slug);
531
+ if (!c)
532
+ throw new ReviewError('not_found', NO_CLIENT);
533
+ return c;
534
+ },
535
+ /**
536
+ * A handoff: the actor, a Studio person signed in on the studio's host,
537
+ * opens one client's hub, or one of its projects with a build, on the
538
+ * client's host. Answers the host from the client's record and a new
539
+ * token, which the host claims once within HANDOFF_SECONDS (claimHandoff).
540
+ * A client archived, gone or still being added (no provenAt), a project
541
+ * it does not have or has not built, and a host that is no host name are
542
+ * all not found. The store keeps the token as its hash. Studio only.
543
+ */
544
+ async startHandoff(actor, input) {
545
+ allowStudio(actor);
546
+ const c = await openable(input.client);
547
+ if (!c)
548
+ throw new ReviewError('not_found', NO_CLIENT);
549
+ const project = input.project ?? null;
550
+ if (project !== null) {
551
+ const p = isProjectSlug(project) ? await projectAt({ client: c.slug, project }) : null;
552
+ if (!p?.build)
553
+ throw new ReviewError('not_found', NO_PROJECT);
554
+ }
555
+ const key = keyOf({ client: c.slug }, 'handoffs');
556
+ // Handoffs nobody claimed go as new ones are made, so the record never grows past a few.
557
+ for (const [hash, json] of Object.entries(await kv.hgetall(key)))
558
+ if (!readHandoff(json))
559
+ await kv.hdel(key, hash);
560
+ const token = base64url(deps.random(32));
561
+ const handoff = {
562
+ person: actor.id,
563
+ project: project,
564
+ expiresAt: new Date(deps.now().getTime() + HANDOFF_SECONDS * 1000).toISOString(),
565
+ };
566
+ await kv.hset(key, await linkKey(token), JSON.stringify(handoff));
567
+ return { host: c.host, token };
568
+ },
569
+ /**
570
+ * Claims a handoff on the scope's client's host, asked by the gate with
571
+ * no actor: the Studio person it was made for and the project to open
572
+ * (null for the client's hub), or null. Only a handoff made for this
573
+ * client is found; one past HANDOFF_SECONDS, one for a client no longer
574
+ * added, or a person who may no longer sign in is null. The delete is
575
+ * the claim, as useLink's, so of two requests with one token only one
576
+ * gets a person; every check comes before it.
577
+ */
578
+ async claimHandoff(scope, token) {
579
+ if (!isStudioClient(scope.client) || typeof token !== 'string' || !LINK_TOKEN.test(token))
580
+ return null;
581
+ const key = keyOf({ client: scope.client }, 'handoffs');
582
+ const hash = await linkKey(token);
583
+ const handoff = readHandoff(await kv.hget(key, hash));
584
+ if (!handoff)
585
+ return null;
586
+ const { project } = handoff;
587
+ const c = await clientAt(scope.client);
588
+ if (!c || c.archivedAt !== null || !isAdded(c))
589
+ return null;
590
+ const p = await personAt('studio', handoff.person);
591
+ if (!active(p))
393
592
  return null;
394
- const slug = await kv.hget(keyOf('studio', 'hosts'), name);
395
- const c = isClientSlug(slug) ? await clientAt(slug) : null;
396
- return c && c.host === name && c.archivedAt === null ? c : null;
593
+ if ((await kv.hdel(key, hash)) !== 1)
594
+ return null;
595
+ // A revoke or sign out everywhere can land between the check and the claim: read again.
596
+ const after = await personAt('studio', p.id);
597
+ return active(after) && after.epoch === p.epoch ? { person: after, project } : null;
397
598
  },
398
599
  /** One project of the scope's client, asked with no actor: null when the client has none by that name. */
399
600
  projectOf: projectAt,
400
601
  /** Every project of the scope's client that has a build to open, by name. */
401
602
  async listProjects(scope, actor) {
402
603
  await allow(scope, actor, 'read', NO_PROJECT);
403
- const stored = Object.values(await kv.hgetall(keyOf({ client: scope.client }, 'projects')));
404
- return stored
405
- .map(readProject)
406
- .filter((p) => p?.client === scope.client && p.build !== null)
407
- .sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
604
+ return (await projectsOf(scope.client)).filter((p) => p.build !== null);
408
605
  },
409
606
  /**
410
607
  * A new client and its host. Studio only. The slug and the host are each
@@ -663,34 +860,38 @@ export function createReview(kv, overrides = {}) {
663
860
  if (isBuildId(build) && build !== live)
664
861
  await kv.hdel(keyOf(scope, 'builds'), build);
665
862
  },
666
- /** The person a signed session names, if they still may sign in with it, here. */
667
- async sessionPerson(scope, id, epoch) {
668
- const p = await person(scope, id);
669
- return active(p) && p.epoch === epoch && inScope(p, scope) ? p : null;
863
+ /** 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. */
864
+ async sessionPerson(at, id, epoch) {
865
+ const p = at === STUDIO_SITE ? await personAt('studio', id) : await person(at, id);
866
+ return active(p) && p.epoch === epoch && reaches(p, at) ? p : null;
670
867
  },
671
- /** The scope's Clients and every Studio person. */
672
- async listPeople(scope, actor) {
673
- await allow(scope, actor, 'people', NO_PERSON);
674
- const all = [...(await peopleAt('studio')), ...(await peopleAt(scope))];
868
+ /** The scope's Clients and every Studio person; on the studio's own host, the Studio people alone. */
869
+ async listPeople(at, actor) {
870
+ await allowPeople(at, actor);
871
+ const all = [...(await peopleAt('studio')), ...(at === STUDIO_SITE ? [] : await peopleAt(at))];
675
872
  return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
676
873
  },
677
874
  /**
678
875
  * A new person and their first link: a Client of the scope's client, or a
679
- * Studio person. Without an access length, a Client gets the default and
680
- * Studio is open-ended. Without a team, a Studio person gets the studio's,
681
- * which every Studio person carries, so the adder's; a Client gets their
682
- * client's name.
876
+ * Studio person; on the studio's own host, a Studio person only. Without
877
+ * an access length, a Client gets the default and Studio is open-ended.
878
+ * Without a team, a Studio person gets the studio's, which every Studio
879
+ * person carries, so the adder's; a Client gets their client's name.
683
880
  */
684
- async addPerson(scope, actor, input) {
685
- const by = await allow(scope, actor, 'people', NO_PERSON);
881
+ async addPerson(at, actor, input) {
882
+ const by = await allowPeople(at, actor);
686
883
  const { access, team, ...fields } = valid(checkPersonInput(input));
884
+ // A Client belongs to one client: the studio's own host names none.
885
+ const scope = fields.kind === 'studio' ? null : at;
886
+ if (scope === STUDIO_SITE)
887
+ throw new ReviewError('invalid', 'Some fields are not valid.', ['kind: studio here']);
687
888
  // S13: the person's own length, else (a Client) the client's default, then the studio's, then 3m.
688
- const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault(scope));
889
+ const length = access ?? (scope ? await readDefault(scope) : 'open');
689
890
  const p = {
690
891
  id: newId(),
691
892
  ...fields,
692
- team: team ?? (fields.kind === 'studio' ? by.team : ((await clientAt(scope.client))?.name ?? 'Client')),
693
- client: fields.kind === 'studio' ? null : scope.client,
893
+ team: team ?? (scope ? ((await clientAt(scope.client))?.name ?? 'Client') : by.team),
894
+ client: scope ? scope.client : null,
694
895
  email: null,
695
896
  accessEndsAt: accessEnd(length, deps.now()),
696
897
  createdAt: now(),
@@ -702,13 +903,13 @@ export function createReview(kv, overrides = {}) {
702
903
  },
703
904
  /** Another one-time link, for another device. Their sessions and other unused links stay. */
704
905
  async newLink(scope, actor, id) {
705
- await allow(scope, actor, 'people', NO_PERSON);
906
+ await allowPeople(scope, actor);
706
907
  const p = await needActive(scope, id);
707
908
  return { person: p, token: await issueLink(p) };
708
909
  },
709
910
  /** Ends every session they hold and every unused link, and gives one new link. */
710
911
  async signOutEverywhere(scope, actor, id) {
711
- await allow(scope, actor, 'people', NO_PERSON);
912
+ await allowPeople(scope, actor);
712
913
  const p = await needActive(scope, id);
713
914
  // The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
714
915
  const next = { ...p, epoch: await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1) };
@@ -716,7 +917,7 @@ export function createReview(kv, overrides = {}) {
716
917
  return { person: next, token: await issueLink(next) };
717
918
  },
718
919
  async revoke(scope, actor, id) {
719
- const by = await allow(scope, actor, 'people', NO_PERSON);
920
+ const by = await allowPeople(scope, actor);
720
921
  if (by.id === id)
721
922
  throw new ReviewError('invalid', 'You cannot revoke yourself.');
722
923
  const p = await needPerson(scope, id);
@@ -731,7 +932,7 @@ export function createReview(kv, overrides = {}) {
731
932
  },
732
933
  /** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
733
934
  async setAccess(scope, actor, id, access) {
734
- await allow(scope, actor, 'people', NO_PERSON);
935
+ await allowPeople(scope, actor);
735
936
  const length = valid(checkAccess(access));
736
937
  const p = await needPerson(scope, id);
737
938
  if (p.revokedAt)
@@ -769,7 +970,7 @@ export function createReview(kv, overrides = {}) {
769
970
  allowStudio(actor);
770
971
  return newStudioPerson(input);
771
972
  },
772
- /** Another one-time link for a Studio person: it signs in one device, on whichever client host it is opened. Studio only. */
973
+ /** 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. */
773
974
  async newStudioLink(actor, id) {
774
975
  allowStudio(actor);
775
976
  const p = await personAt('studio', id);
@@ -791,21 +992,22 @@ export function createReview(kv, overrides = {}) {
791
992
  return length;
792
993
  },
793
994
  /** Who a link signs in here, without using it: mail and chat previews open links too. */
794
- async peekLink(scope, token) {
795
- const found = await findLink(scope, token);
796
- return found ? linkPerson(scope, found) : null;
995
+ async peekLink(at, token) {
996
+ const found = await findLink(at, token);
997
+ return found ? linkPerson(at, found) : null;
797
998
  },
798
999
  /**
799
1000
  * Uses a link up and returns who it signs in. The delete is the claim: of
800
1001
  * two requests with one token, only the one whose delete removed the link
801
1002
  * gets a person, so a link signs in one device at most. Who it signs in is
802
1003
  * read and checked first, so the delete is the last store call. Only a
803
- * link of the scope's client, or a Studio person's, is found at all.
1004
+ * link of the scope's client, or a Studio person's, is found at all; on
1005
+ * the studio's own host, only a Studio person's.
804
1006
  */
805
- async useLink(scope, token) {
806
- const found = await findLink(scope, token);
1007
+ async useLink(at, token) {
1008
+ const found = await findLink(at, token);
807
1009
  // Every read and check comes before the delete, so a store failure before it leaves the link usable.
808
- const p = found && (await linkPerson(scope, found));
1010
+ const p = found && (await linkPerson(at, found));
809
1011
  if (!found || !p)
810
1012
  return null;
811
1013
  if ((await kv.hdel(keyOf(found.home, 'links'), found.hash)) !== 1)
@@ -24,15 +24,19 @@ export interface KV {
24
24
  /**
25
25
  * The studio's own records: Studio people (profile, revocation, epoch and
26
26
  * links, as a client's below) and `settings`, one field each: `access` is the
27
- * default access length. `mark`, one field, `id`: the id of the one studio
27
+ * default access length, `hostProvenAt` when the studio's own host first
28
+ * answered its proof. `proof`: while a command waits for the studio's own
29
+ * host, one field, hex(sha256(ask)) to the answer the gate gives there.
30
+ * `mark`, one field, `id`: the id of the one studio
28
31
  * this database serves (its studio.json `id`), set once by `studio create`
29
32
  * and checked by every command before it acts (server/mark.ts). No other
30
33
  * key names a studio, so two studios on one database would be one studio.
31
34
  */
32
- type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark';
35
+ type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark' | 'proof';
33
36
  /**
34
37
  * The studio's clients, outside every scope: `clients`, a client's slug to
35
- * its Client; `hosts`, a host name to the slug of the client it serves;
38
+ * its Client; `hosts`, a host name to the slug of the client it serves, or
39
+ * to `studio` (STUDIO_SITE) for the studio's own host, which no client slug is;
36
40
  * `claims`, one counter per slug (`client:<slug>`) and per host
37
41
  * (`host:<host>`), raised by HINCRBY when a client is added: whoever raised
38
42
  * it to 1 holds it, so two adds at once never both get one. Archiving a
@@ -51,9 +55,11 @@ type DirectoryName = 'clients' | 'hosts' | 'claims' | 'held';
51
55
  * `projects`: the client's projects, a slug to its Project. `proof`: while
52
56
  * `client add` waits for the client's host, one field, hex(sha256(ask)) to
53
57
  * the answer the gate gives for that ask (PROOF_PATH in prototype.ts).
58
+ * `handoffs`: Studio opening this client from the studio's host,
59
+ * hex(sha256(token)) to a Handoff, deleted when the client's host claims it.
54
60
  */
55
61
  type ClientName = 'people' | 'revoked' | 'epochs' | 'links';
56
- type ClientOnlyName = 'projects' | 'proof';
62
+ type ClientOnlyName = 'projects' | 'proof' | 'handoffs';
57
63
  /**
58
64
  * A project's records. `audiences`: which prototypes are client-facing,
59
65
  * field `<slug>`, value `client`; only client-facing is stored, internal is
@@ -1,6 +1,6 @@
1
1
  // The one storage port the review service needs: a few Redis-shaped hash
2
- // and set operations. Production is Upstash Redis over REST
3
- // (server/upstash.ts); dev and tests use the in-memory store below, which
2
+ // and set operations. A studio's is Upstash Redis over REST or Postgres
3
+ // (server/stores/); dev and tests use the in-memory store below, which
4
4
  // the dev server persists to a file (server/file-kv.ts).
5
5
  import { SLUG } from '../feature.js';
6
6
  import { isClientSlug, isProjectSlug } from '../prototype.js';
@@ -0,0 +1,7 @@
1
+ import { type KV, type Snapshot } from './store.ts';
2
+ type Make = (initial?: Snapshot, onChange?: (s: Snapshot) => void) => KV;
3
+ /** A new store for one test, holding `initial`. */
4
+ export declare const suiteKV: Make;
5
+ /** For a test file that runs the service suite on another store: call before the suite loads. */
6
+ export declare function runSuiteOn(other: Make): void;
7
+ export {};
@@ -0,0 +1,12 @@
1
+ // The store the service suite (service.test.ts) runs on: the memory store,
2
+ // unless a test file picks another before it loads the suite, as
3
+ // server/stores/service.postgres.test.ts picks Postgres. One signature for
4
+ // both: the records to start with, and what to call after each write.
5
+ import { memoryKV } from './store.js';
6
+ let make = memoryKV;
7
+ /** A new store for one test, holding `initial`. */
8
+ export const suiteKV = (initial, onChange) => make(initial, onChange);
9
+ /** For a test file that runs the service suite on another store: call before the suite loads. */
10
+ export function runSuiteOn(other) {
11
+ make = other;
12
+ }
@@ -3,11 +3,11 @@ import { type Args } from './lib/cli.ts';
3
3
  import { type StudioDeps } from './lib/studio-deps.ts';
4
4
  export declare const spec: {
5
5
  readonly name: 'client';
6
- readonly summary: 'Add a client to a studio with its host and first sign-in links, archive one, or set how long its people keep access.';
6
+ readonly summary: 'Add a client to a studio with its host and its first person’s sign-in link, archive one, list them, or set how long its people keep access.';
7
7
  readonly positionals: [{
8
8
  readonly name: 'action';
9
9
  readonly required: true;
10
- readonly description: 'add <slug>: the client, its host and its first links. archive <slug>: remove it, its people, projects, comments and files, then its DNS record (only one this studio made) and its host on the site; run it again to finish one that stopped part way. An archived slug or host can be added again two minutes later. access [slug]: read or set (--access) the client’s default access length; without a slug, the studio’s.';
10
+ readonly description: 'add <slug>: the client, its host and its first person’s link (Studio opens it from studio.<apex>). list: every client, with its host, state and project count. archive <slug>: remove it, its people, projects, comments and files, then its DNS record (only one this studio made) and its host on the site; run it again to finish one that stopped part way. An archived slug or host can be added again two minutes later. access [slug]: read or set (--access) the client’s default access length; without a slug, the studio’s.';
11
11
  }, {
12
12
  readonly name: 'slug';
13
13
  readonly description: 'The client, kebab-case; never studio, www, api or site.';
@@ -39,7 +39,7 @@ export declare const spec: {
39
39
  readonly description: 'For access: 3m, 6m or open; with a slug, studio hands the client back to the studio’s default. A person’s own length wins over both.';
40
40
  };
41
41
  };
42
- readonly output: 'add: { ok, client, host, dns, links: { client, studio? }, notice?, next }. archive: { ok, client, host, domain: removed | absent | wildcard | kept (another client has the host) | unknown, files, dns?: { removed, left, fix? } | { record, fix } }. access: { ok, client?, access, default }';
42
+ readonly output: 'add: { ok, client, host, dns, links: { client }, notice?, next }. list: { ok, clients: [{ slug, name, host, state: added | waiting | archived, projects }] } (waiting: its add has not finished; archived: its archive has not finished). archive: { ok, client, host, domain: removed | absent | wildcard | kept (another client has the host) | unknown, files, dns?: { removed, left, fix? } | { record, fix } }. access: { ok, client?, access, default }';
43
43
  };
44
44
  export declare function run({ positionals: [action, slug], flags }: Args<typeof spec>, deps: StudioDeps): Promise<Record<string, unknown>>;
45
45
  export declare const main: (args: Args<typeof spec>) => Promise<Record<string, unknown>>;