@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.
- package/README.md +8 -6
- package/core/prototype.test.ts +52 -0
- package/core/prototype.ts +59 -0
- package/core/review/model.ts +20 -0
- package/core/review/service.test.ts +262 -37
- package/core/review/service.ts +276 -52
- package/core/review/store.ts +12 -6
- package/core/review/suite-kv.ts +17 -0
- package/dist/core/prototype.d.ts +50 -0
- package/dist/core/prototype.js +38 -0
- package/dist/core/review/model.d.ts +22 -0
- package/dist/core/review/model.js +8 -0
- package/dist/core/review/service.d.ts +98 -18
- package/dist/core/review/service.js +248 -46
- package/dist/core/review/store.d.ts +10 -4
- package/dist/core/review/store.js +2 -2
- package/dist/core/review/suite-kv.d.ts +7 -0
- package/dist/core/review/suite-kv.js +12 -0
- package/dist/scripts/client.d.ts +3 -3
- package/dist/scripts/client.js +55 -69
- package/dist/scripts/lib/cli.js +5 -0
- package/dist/scripts/lib/dns.d.ts +32 -0
- package/dist/scripts/lib/dns.js +69 -1
- package/dist/scripts/lib/fake-host.d.ts +35 -11
- package/dist/scripts/lib/fake-host.js +27 -13
- package/dist/scripts/lib/host.d.ts +13 -25
- package/dist/scripts/lib/host.js +15 -324
- package/dist/scripts/lib/studio-deps.d.ts +16 -0
- package/dist/scripts/lib/studio-deps.js +41 -16
- package/dist/scripts/lib/studios.d.ts +17 -5
- package/dist/scripts/lib/studios.js +25 -5
- package/dist/scripts/lib/vercel-host.d.ts +17 -0
- package/dist/scripts/lib/vercel-host.js +410 -0
- package/dist/scripts/people.d.ts +7 -4
- package/dist/scripts/people.js +147 -39
- package/dist/scripts/share.js +3 -2
- package/dist/scripts/studio.d.ts +10 -4
- package/dist/scripts/studio.js +136 -22
- package/dist/server/api.d.ts +12 -13
- package/dist/server/api.js +141 -52
- package/dist/server/gate.d.ts +97 -0
- package/dist/server/gate.js +537 -0
- package/dist/server/hosts.d.ts +3 -3
- package/dist/server/hosts.js +10 -10
- package/dist/server/mark.d.ts +17 -0
- package/dist/server/mark.js +46 -1
- package/dist/server/session.d.ts +9 -1
- package/dist/server/session.js +10 -3
- package/dist/server/stores/backends.d.ts +22 -0
- package/dist/server/stores/backends.js +96 -0
- package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
- package/dist/server/stores/index.d.ts +41 -0
- package/dist/server/stores/index.js +63 -0
- package/dist/server/stores/pglite.d.ts +16 -0
- package/dist/server/stores/pglite.js +98 -0
- package/dist/server/stores/postgres.d.ts +63 -0
- package/dist/server/stores/postgres.js +153 -0
- package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
- package/dist/vercel/files.js +1 -1
- package/dist/vercel/proxy.d.ts +6 -78
- package/dist/vercel/proxy.js +18 -359
- package/dist/vite/plugin.d.ts +6 -2
- package/dist/vite/plugin.js +22 -4
- package/package.json +3 -1
- package/shell/Index.tsx +29 -0
- package/shell/review/People.tsx +79 -49
- package/shell/review/Review.tsx +49 -1
- package/shell/review/ThreadCard.tsx +13 -1
- package/shell/review/client.ts +54 -21
- package/shell/review/review.css +1 -0
- package/shell/review/unsent.test.ts +63 -0
- package/shell/review/unsent.ts +64 -0
- package/shell/review/view.ts +22 -6
- package/shell/studio/ClientHub.tsx +9 -36
- package/shell/studio/StudioHub.tsx +293 -0
- package/shell/studio/hub.css +80 -5
- package/shell/studio/page.tsx +57 -0
- package/shell/studio/studio.html +14 -0
- package/shell/studio/studio.tsx +19 -0
- package/skills/miles-deploy/SKILL.md +17 -6
- package/skills/miles-design-hub/SKILL.md +3 -3
- package/skills/miles-review/SKILL.md +1 -1
- package/templates/studio/.env.example +3 -0
- package/templates/studio/README.md +45 -13
- package/templates/studio/package.json +1 -1
- /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
- /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
package/core/review/service.ts
CHANGED
|
@@ -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
|
-
/**
|
|
293
|
-
|
|
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
|
-
|
|
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(
|
|
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) &&
|
|
326
|
+
return active(p) && reaches(p, at) ? p : null;
|
|
308
327
|
}
|
|
309
328
|
|
|
310
329
|
async function studioDefault(): Promise<Access> {
|
|
@@ -383,16 +402,41 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
383
402
|
return scope.client;
|
|
384
403
|
}
|
|
385
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
|
+
|
|
386
430
|
/** Someone Studio can still give a link to. */
|
|
387
|
-
async function needActive(scope:
|
|
431
|
+
async function needActive(scope: HostScope, id: string): Promise<Person> {
|
|
388
432
|
const p = await needPerson(scope, id);
|
|
389
433
|
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
390
434
|
if (!active(p)) throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
391
435
|
return p;
|
|
392
436
|
}
|
|
393
437
|
|
|
394
|
-
async function needPerson(scope:
|
|
395
|
-
const p = await
|
|
438
|
+
async function needPerson(scope: HostScope, id: string): Promise<Person> {
|
|
439
|
+
const p = await personHere(scope, id);
|
|
396
440
|
if (!p) throw new ReviewError('not_found', NO_PERSON);
|
|
397
441
|
return p;
|
|
398
442
|
}
|
|
@@ -499,6 +543,37 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
499
543
|
};
|
|
500
544
|
const saveProject = (p: Project) =>
|
|
501
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
|
+
}
|
|
502
577
|
|
|
503
578
|
return {
|
|
504
579
|
person,
|
|
@@ -509,11 +584,160 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
509
584
|
* records that disagree, so an unknown host is never a scope.
|
|
510
585
|
*/
|
|
511
586
|
async clientOfHost(host: string): Promise<Client | null> {
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
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;
|
|
517
741
|
},
|
|
518
742
|
|
|
519
743
|
/** One project of the scope's client, asked with no actor: null when the client has none by that name. */
|
|
@@ -522,11 +746,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
522
746
|
/** Every project of the scope's client that has a build to open, by name. */
|
|
523
747
|
async listProjects(scope: Scope, actor: Person | null): Promise<Project[]> {
|
|
524
748
|
await allow(scope, actor, 'read', NO_PROJECT);
|
|
525
|
-
|
|
526
|
-
return stored
|
|
527
|
-
.map(readProject)
|
|
528
|
-
.filter((p): p is Project => p?.client === scope.client && p.build !== null)
|
|
529
|
-
.sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
|
|
749
|
+
return (await projectsOf(scope.client)).filter((p) => p.build !== null);
|
|
530
750
|
},
|
|
531
751
|
|
|
532
752
|
/**
|
|
@@ -784,37 +1004,40 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
784
1004
|
if (isBuildId(build) && build !== live) await kv.hdel(keyOf(scope, 'builds'), build);
|
|
785
1005
|
},
|
|
786
1006
|
|
|
787
|
-
/** The person a signed session names, if they still may sign in with it, here. */
|
|
788
|
-
async sessionPerson(
|
|
789
|
-
const p = await person(
|
|
790
|
-
return active(p) && p.epoch === epoch &&
|
|
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;
|
|
791
1011
|
},
|
|
792
1012
|
|
|
793
|
-
/** The scope's Clients and every Studio person. */
|
|
794
|
-
async listPeople(
|
|
795
|
-
await
|
|
796
|
-
const all = [...(await peopleAt('studio')), ...(await peopleAt(
|
|
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))];
|
|
797
1017
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
798
1018
|
},
|
|
799
1019
|
|
|
800
1020
|
/**
|
|
801
1021
|
* A new person and their first link: a Client of the scope's client, or a
|
|
802
|
-
* Studio person
|
|
803
|
-
*
|
|
804
|
-
*
|
|
805
|
-
* client's name.
|
|
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.
|
|
806
1026
|
*/
|
|
807
|
-
async addPerson(
|
|
808
|
-
const by = await
|
|
1027
|
+
async addPerson(at: HostScope, actor: Person | null, input: unknown): Promise<Issued> {
|
|
1028
|
+
const by = await allowPeople(at, actor);
|
|
809
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']);
|
|
810
1034
|
// S13: the person's own length, else (a Client) the client's default, then the studio's, then 3m.
|
|
811
|
-
const length = access ?? (
|
|
1035
|
+
const length = access ?? (scope ? await readDefault(scope) : 'open');
|
|
812
1036
|
const p: Person = {
|
|
813
1037
|
id: newId(),
|
|
814
1038
|
...fields,
|
|
815
|
-
team:
|
|
816
|
-
|
|
817
|
-
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,
|
|
818
1041
|
email: null,
|
|
819
1042
|
accessEndsAt: accessEnd(length, deps.now()),
|
|
820
1043
|
createdAt: now(),
|
|
@@ -826,15 +1049,15 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
826
1049
|
},
|
|
827
1050
|
|
|
828
1051
|
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
829
|
-
async newLink(scope:
|
|
830
|
-
await
|
|
1052
|
+
async newLink(scope: HostScope, actor: Person | null, id: string): Promise<Issued> {
|
|
1053
|
+
await allowPeople(scope, actor);
|
|
831
1054
|
const p = await needActive(scope, id);
|
|
832
1055
|
return { person: p, token: await issueLink(p) };
|
|
833
1056
|
},
|
|
834
1057
|
|
|
835
1058
|
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
836
|
-
async signOutEverywhere(scope:
|
|
837
|
-
await
|
|
1059
|
+
async signOutEverywhere(scope: HostScope, actor: Person | null, id: string): Promise<Issued> {
|
|
1060
|
+
await allowPeople(scope, actor);
|
|
838
1061
|
const p = await needActive(scope, id);
|
|
839
1062
|
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
840
1063
|
const next = { ...p, epoch: await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1) };
|
|
@@ -842,8 +1065,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
842
1065
|
return { person: next, token: await issueLink(next) };
|
|
843
1066
|
},
|
|
844
1067
|
|
|
845
|
-
async revoke(scope:
|
|
846
|
-
const by = await
|
|
1068
|
+
async revoke(scope: HostScope, actor: Person | null, id: string): Promise<Person> {
|
|
1069
|
+
const by = await allowPeople(scope, actor);
|
|
847
1070
|
if (by.id === id) throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
848
1071
|
const p = await needPerson(scope, id);
|
|
849
1072
|
const revokedAt = p.revokedAt ?? now();
|
|
@@ -856,8 +1079,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
856
1079
|
},
|
|
857
1080
|
|
|
858
1081
|
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
859
|
-
async setAccess(scope:
|
|
860
|
-
await
|
|
1082
|
+
async setAccess(scope: HostScope, actor: Person | null, id: string, access: unknown): Promise<Person> {
|
|
1083
|
+
await allowPeople(scope, actor);
|
|
861
1084
|
const length = valid(checkAccess(access));
|
|
862
1085
|
const p = await needPerson(scope, id);
|
|
863
1086
|
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
@@ -897,7 +1120,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
897
1120
|
return newStudioPerson(input);
|
|
898
1121
|
},
|
|
899
1122
|
|
|
900
|
-
/** 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. */
|
|
901
1124
|
async newStudioLink(actor: Person | null, id: string): Promise<Issued> {
|
|
902
1125
|
allowStudio(actor);
|
|
903
1126
|
const p = await personAt('studio', id);
|
|
@@ -920,9 +1143,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
920
1143
|
},
|
|
921
1144
|
|
|
922
1145
|
/** Who a link signs in here, without using it: mail and chat previews open links too. */
|
|
923
|
-
async peekLink(
|
|
924
|
-
const found = await findLink(
|
|
925
|
-
return found ? linkPerson(
|
|
1146
|
+
async peekLink(at: HostScope, token: string): Promise<Person | null> {
|
|
1147
|
+
const found = await findLink(at, token);
|
|
1148
|
+
return found ? linkPerson(at, found) : null;
|
|
926
1149
|
},
|
|
927
1150
|
|
|
928
1151
|
/**
|
|
@@ -930,12 +1153,13 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
930
1153
|
* two requests with one token, only the one whose delete removed the link
|
|
931
1154
|
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
932
1155
|
* read and checked first, so the delete is the last store call. Only a
|
|
933
|
-
* 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.
|
|
934
1158
|
*/
|
|
935
|
-
async useLink(
|
|
936
|
-
const found = await findLink(
|
|
1159
|
+
async useLink(at: HostScope, token: string): Promise<Person | null> {
|
|
1160
|
+
const found = await findLink(at, token);
|
|
937
1161
|
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
938
|
-
const p = found && (await linkPerson(
|
|
1162
|
+
const p = found && (await linkPerson(at, found));
|
|
939
1163
|
if (!found || !p) return null;
|
|
940
1164
|
if ((await kv.hdel(keyOf(found.home, 'links'), found.hash)) !== 1) return null;
|
|
941
1165
|
// A revoke or sign out everywhere can land between the check and the claim, before its
|
package/core/review/store.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The one storage port the review service needs: a few Redis-shaped hash
|
|
2
|
-
// and set operations.
|
|
3
|
-
// (server/
|
|
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.ts';
|
|
6
6
|
import { isClientSlug, isProjectSlug } from '../prototype.ts';
|
|
@@ -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
|
|
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,
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { type KV, memoryKV, type Snapshot } from './store.ts';
|
|
6
|
+
|
|
7
|
+
type Make = (initial?: Snapshot, onChange?: (s: Snapshot) => void) => KV;
|
|
8
|
+
|
|
9
|
+
let make: Make = memoryKV;
|
|
10
|
+
|
|
11
|
+
/** A new store for one test, holding `initial`. */
|
|
12
|
+
export const suiteKV: Make = (initial, onChange) => make(initial, onChange);
|
|
13
|
+
|
|
14
|
+
/** For a test file that runs the service suite on another store: call before the suite loads. */
|
|
15
|
+
export function runSuiteOn(other: Make): void {
|
|
16
|
+
make = other;
|
|
17
|
+
}
|
package/dist/core/prototype.d.ts
CHANGED
|
@@ -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
|