@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
@@ -26,6 +26,16 @@ export declare const DEFAULT_ACCESS: Access;
26
26
  * archiving a client, or adding one, takes effect on every instance within it.
27
27
  */
28
28
  export declare const HOST_SECONDS = 60;
29
+ /** How long a handoff lasts: Studio opens a client from the studio's host, and the client's host claims it within this. */
30
+ export declare const HANDOFF_SECONDS = 60;
31
+ /**
32
+ * The studio's own host (`studio.<apex>`): its entry in the store's `hosts`,
33
+ * in place of a client's slug, and the client a session on it is signed to.
34
+ * Never a client's slug (RESERVED_CLIENTS), so no client host is ever it.
35
+ */
36
+ export declare const STUDIO_SITE = "studio";
37
+ /** What a studio site's host serves: one client, the studio's own host, or nothing. */
38
+ export type Site = Client | typeof STUDIO_SITE | null;
29
39
  /** When access given at `from` ends, as an ISO time, or null for open-ended. */
30
40
  export declare function accessEnd(access: Access, from: Date): string | null;
31
41
  export declare const LIMITS: {
@@ -115,6 +125,18 @@ export type Link = {
115
125
  person: string;
116
126
  expiresAt: string;
117
127
  };
128
+ /**
129
+ * A handoff: a Studio person, signed in on the studio's host, opening one
130
+ * client's hub (or one of its projects) on that client's host. Stored with
131
+ * the client under the hash of its token, deleted when its host claims it,
132
+ * good for HANDOFF_SECONDS. `project` is a project of the client with a
133
+ * build when it was made, or null for the client's hub.
134
+ */
135
+ export type Handoff = {
136
+ person: string;
137
+ project: string | null;
138
+ expiresAt: string;
139
+ };
118
140
  /** A comment's author as it was when they wrote it, so a revoked person's comments still read. */
119
141
  export type Author = Pick<Person, 'id' | 'name' | 'team'>;
120
142
  export type Anchor = {
@@ -30,6 +30,14 @@ export const DEFAULT_ACCESS = '3m';
30
30
  * archiving a client, or adding one, takes effect on every instance within it.
31
31
  */
32
32
  export const HOST_SECONDS = 60;
33
+ /** How long a handoff lasts: Studio opens a client from the studio's host, and the client's host claims it within this. */
34
+ export const HANDOFF_SECONDS = 60;
35
+ /**
36
+ * The studio's own host (`studio.<apex>`): its entry in the store's `hosts`,
37
+ * in place of a client's slug, and the client a session on it is signed to.
38
+ * Never a client's slug (RESERVED_CLIENTS), so no client host is ever it.
39
+ */
40
+ export const STUDIO_SITE = 'studio';
33
41
  const ACCESS_MONTHS = { '3m': 3, '6m': 6 };
34
42
  /** When access given at `from` ends, as an ISO time, or null for open-ended. */
35
43
  export function accessEnd(access, from) {
@@ -1,5 +1,5 @@
1
1
  import { type ControlKind, type Marks } from '../controls.ts';
2
- import { type Access, type Audience, type Client, type Comment, type Person, type PersonKind, type Project, type Scope, type Triage } from './model.ts';
2
+ import { type Access, type Audience, type Client, type Comment, type Person, type PersonKind, type Project, type Scope, type Site, STUDIO_SITE, type Triage } from './model.ts';
3
3
  import { type KV } from './store.ts';
4
4
  export type ErrorCode = 'unauthorized' | 'forbidden' | 'not_found' | 'invalid';
5
5
  export declare class ReviewError extends Error {
@@ -42,6 +42,11 @@ export type Archived = {
42
42
  ownHost: boolean;
43
43
  };
44
44
  export declare const NO_PROJECT = "No such project.";
45
+ /**
46
+ * What a host the gate answers runs in: one client's scope, or the studio's
47
+ * own host (STUDIO_SITE), where only Studio people sign in.
48
+ */
49
+ export type HostScope = Scope | typeof STUDIO_SITE;
45
50
  /** A person and a new one-time link's token for them. */
46
51
  type Issued = {
47
52
  person: Person;
@@ -55,6 +60,80 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
55
60
  * records that disagree, so an unknown host is never a scope.
56
61
  */
57
62
  clientOfHost(host: string): Promise<Client | null>;
63
+ /**
64
+ * What a host serves, asked by the gate and the studio's functions with
65
+ * no actor: a client (as clientOfHost), the studio's own host, or null.
66
+ */
67
+ siteOfHost: (host: string) => Promise<Site>;
68
+ /**
69
+ * Whether the studio's own host has answered its proof (endStudioProof):
70
+ * from then on commands leave it be, and its owner has had a link there.
71
+ * Studio only.
72
+ */
73
+ studioHostProven(actor: Person | null): Promise<boolean>;
74
+ /**
75
+ * A new proof for the studio's own host, in place of any before it, as
76
+ * startProof is for a client's: only the gate reading this store answers
77
+ * its ask there. Studio only.
78
+ */
79
+ startStudioProof(actor: Person | null): Promise<{
80
+ ask: string;
81
+ answer: string;
82
+ }>;
83
+ /** Ends the studio host's proof once it answered, marking the host proven; the mark goes on first. Studio only. */
84
+ endStudioProof(actor: Person | null): Promise<void>;
85
+ /** The answer to the studio host's proof, asked by the gate on that host with no actor: null for any other ask. */
86
+ studioProofAnswer(ask: string): Promise<string | null>;
87
+ /**
88
+ * Makes `host` the studio's own host (`studio.<apex>`), where Studio
89
+ * signs in and sees every client. Refused for a host a client has, or
90
+ * one that is not a host name. Safe to run again. Studio only.
91
+ */
92
+ setStudioHost(actor: Person | null, host: string): Promise<'added' | 'kept'>;
93
+ /**
94
+ * Every client record the studio has, by name, each with all its
95
+ * projects by name, archived ones too (an archive that stopped part way).
96
+ * The studio's own view, for its hub and `client list`. Studio only.
97
+ */
98
+ listClients(actor: Person | null): Promise<{
99
+ client: Client;
100
+ projects: Project[];
101
+ }[]>;
102
+ /**
103
+ * The client an op on the studio's own host names, for its people: known,
104
+ * not archived and added, as a handoff checks it (openable). Anything
105
+ * else is not found, an unknown client's answer. Studio only.
106
+ */
107
+ studioClient(actor: Person | null, slug: unknown): Promise<Client>;
108
+ /**
109
+ * A handoff: the actor, a Studio person signed in on the studio's host,
110
+ * opens one client's hub, or one of its projects with a build, on the
111
+ * client's host. Answers the host from the client's record and a new
112
+ * token, which the host claims once within HANDOFF_SECONDS (claimHandoff).
113
+ * A client archived, gone or still being added (no provenAt), a project
114
+ * it does not have or has not built, and a host that is no host name are
115
+ * all not found. The store keeps the token as its hash. Studio only.
116
+ */
117
+ startHandoff(actor: Person | null, input: {
118
+ client: unknown;
119
+ project: unknown;
120
+ }): Promise<{
121
+ host: string;
122
+ token: string;
123
+ }>;
124
+ /**
125
+ * Claims a handoff on the scope's client's host, asked by the gate with
126
+ * no actor: the Studio person it was made for and the project to open
127
+ * (null for the client's hub), or null. Only a handoff made for this
128
+ * client is found; one past HANDOFF_SECONDS, one for a client no longer
129
+ * added, or a person who may no longer sign in is null. The delete is
130
+ * the claim, as useLink's, so of two requests with one token only one
131
+ * gets a person; every check comes before it.
132
+ */
133
+ claimHandoff(scope: Scope, token: string): Promise<{
134
+ person: Person;
135
+ project: string | null;
136
+ } | null>;
58
137
  /** One project of the scope's client, asked with no actor: null when the client has none by that name. */
59
138
  projectOf: ({ client, project }: Scope) => Promise<Project | null>;
60
139
  /** Every project of the scope's client that has a build to open, by name. */
@@ -159,25 +238,25 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
159
238
  }>;
160
239
  /** Forgets builds of the scope's project, once their files are gone; never the live one. Studio only. */
161
240
  forgetBuilds(scope: Scope, actor: Person | null, builds: string[]): Promise<void>;
162
- /** The person a signed session names, if they still may sign in with it, here. */
163
- sessionPerson(scope: Scope, id: string, epoch: number): Promise<Person | null>;
164
- /** The scope's Clients and every Studio person. */
165
- listPeople(scope: Scope, actor: Person | null): Promise<Person[]>;
241
+ /** 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. */
242
+ sessionPerson(at: HostScope, id: string, epoch: number): Promise<Person | null>;
243
+ /** The scope's Clients and every Studio person; on the studio's own host, the Studio people alone. */
244
+ listPeople(at: HostScope, actor: Person | null): Promise<Person[]>;
166
245
  /**
167
246
  * A new person and their first link: a Client of the scope's client, or a
168
- * Studio person. Without an access length, a Client gets the default and
169
- * Studio is open-ended. Without a team, a Studio person gets the studio's,
170
- * which every Studio person carries, so the adder's; a Client gets their
171
- * client's name.
247
+ * Studio person; on the studio's own host, a Studio person only. Without
248
+ * an access length, a Client gets the default and Studio is open-ended.
249
+ * Without a team, a Studio person gets the studio's, which every Studio
250
+ * person carries, so the adder's; a Client gets their client's name.
172
251
  */
173
- addPerson(scope: Scope, actor: Person | null, input: unknown): Promise<Issued>;
252
+ addPerson(at: HostScope, actor: Person | null, input: unknown): Promise<Issued>;
174
253
  /** Another one-time link, for another device. Their sessions and other unused links stay. */
175
- newLink(scope: Scope, actor: Person | null, id: string): Promise<Issued>;
254
+ newLink(scope: HostScope, actor: Person | null, id: string): Promise<Issued>;
176
255
  /** Ends every session they hold and every unused link, and gives one new link. */
177
- signOutEverywhere(scope: Scope, actor: Person | null, id: string): Promise<Issued>;
178
- revoke(scope: Scope, actor: Person | null, id: string): Promise<Person>;
256
+ signOutEverywhere(scope: HostScope, actor: Person | null, id: string): Promise<Issued>;
257
+ revoke(scope: HostScope, actor: Person | null, id: string): Promise<Person>;
179
258
  /** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
180
- setAccess(scope: Scope, actor: Person | null, id: string, access: unknown): Promise<Person>;
259
+ setAccess(scope: HostScope, actor: Person | null, id: string, access: unknown): Promise<Person>;
181
260
  /** How long a new Client of the scope's client keeps access unless Studio says otherwise: the client's default, else the studio's. */
182
261
  defaultAccess(scope: Scope, actor: Person | null): Promise<Access>;
183
262
  /**
@@ -189,21 +268,22 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
189
268
  setDefaultAccess(scope: Scope, actor: Person | null, access: unknown): Promise<Access>;
190
269
  /** A new Studio person, who works in every client. No link: each comes from newStudioLink, for one host. Studio only. */
191
270
  addStudioPerson(actor: Person | null, input: unknown): Promise<Person>;
192
- /** Another one-time link for a Studio person: it signs in one device, on whichever client host it is opened. Studio only. */
271
+ /** 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. */
193
272
  newStudioLink(actor: Person | null, id: string): Promise<Issued>;
194
273
  /** The studio's default access length, what a client without its own gives. Studio only. */
195
274
  studioAccess(actor: Person | null): Promise<Access>;
196
275
  setStudioAccess(actor: Person | null, access: unknown): Promise<Access>;
197
276
  /** Who a link signs in here, without using it: mail and chat previews open links too. */
198
- peekLink(scope: Scope, token: string): Promise<Person | null>;
277
+ peekLink(at: HostScope, token: string): Promise<Person | null>;
199
278
  /**
200
279
  * Uses a link up and returns who it signs in. The delete is the claim: of
201
280
  * two requests with one token, only the one whose delete removed the link
202
281
  * gets a person, so a link signs in one device at most. Who it signs in is
203
282
  * read and checked first, so the delete is the last store call. Only a
204
- * link of the scope's client, or a Studio person's, is found at all.
283
+ * link of the scope's client, or a Studio person's, is found at all; on
284
+ * the studio's own host, only a Studio person's.
205
285
  */
206
- useLink(scope: Scope, token: string): Promise<Person | null>;
286
+ useLink(at: HostScope, token: string): Promise<Person | null>;
207
287
  /**
208
288
  * A prototype's comments as the actor's view sees them. `viewAs: 'client'`
209
289
  * is Studio's Client view; it never widens a Client's.
@@ -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)