@milesroxas/design-hub 0.13.0 → 0.14.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 (67) hide show
  1. package/README.md +1 -1
  2. package/core/prototype.test.ts +12 -1
  3. package/core/prototype.ts +21 -2
  4. package/core/review/model.ts +71 -6
  5. package/core/review/service.test.ts +536 -1
  6. package/core/review/service.ts +452 -25
  7. package/core/review/store.ts +45 -4
  8. package/dist/core/prototype.d.ts +20 -2
  9. package/dist/core/prototype.js +17 -2
  10. package/dist/core/review/model.d.ts +42 -3
  11. package/dist/core/review/model.js +52 -3
  12. package/dist/core/review/service.d.ts +81 -7
  13. package/dist/core/review/service.js +407 -23
  14. package/dist/core/review/store.d.ts +29 -2
  15. package/dist/core/review/store.js +4 -0
  16. package/dist/scripts/client.d.ts +1 -1
  17. package/dist/scripts/client.js +1 -1
  18. package/dist/scripts/lib/dns.d.ts +16 -2
  19. package/dist/scripts/lib/dns.js +66 -3
  20. package/dist/scripts/lib/fake-host.d.ts +16 -1
  21. package/dist/scripts/lib/fake-host.js +36 -1
  22. package/dist/scripts/lib/host.d.ts +2 -0
  23. package/dist/scripts/lib/resend.d.ts +36 -0
  24. package/dist/scripts/lib/resend.js +66 -0
  25. package/dist/scripts/lib/studio-deps.d.ts +7 -0
  26. package/dist/scripts/lib/studio-deps.js +6 -1
  27. package/dist/scripts/lib/studios.d.ts +2 -2
  28. package/dist/scripts/lib/studios.js +5 -5
  29. package/dist/scripts/lib/vercel-host.js +6 -0
  30. package/dist/scripts/people.d.ts +16 -4
  31. package/dist/scripts/people.js +67 -17
  32. package/dist/scripts/studio.d.ts +19 -3
  33. package/dist/scripts/studio.js +327 -6
  34. package/dist/server/api.d.ts +14 -2
  35. package/dist/server/api.js +71 -17
  36. package/dist/server/dev.js +21 -1
  37. package/dist/server/gate.d.ts +26 -3
  38. package/dist/server/gate.js +323 -52
  39. package/dist/server/mailer.d.ts +60 -0
  40. package/dist/server/mailer.js +104 -0
  41. package/dist/server/mailers/fake.d.ts +8 -0
  42. package/dist/server/mailers/fake.js +16 -0
  43. package/dist/server/mailers/resend.d.ts +21 -0
  44. package/dist/server/mailers/resend.js +49 -0
  45. package/dist/server/session.js +4 -1
  46. package/dist/vercel/proxy.js +10 -3
  47. package/dist/vite/plugin.js +3 -1
  48. package/kit/ui/Menu.tsx +29 -18
  49. package/kit/ui/layer.ts +41 -0
  50. package/package.json +1 -1
  51. package/shell/review/Bar.tsx +1 -0
  52. package/shell/review/Card.tsx +17 -7
  53. package/shell/review/People.tsx +908 -134
  54. package/shell/review/Review.tsx +2 -2
  55. package/shell/review/anchor.ts +6 -1
  56. package/shell/review/client.ts +41 -7
  57. package/shell/review/menu-place.test.ts +41 -0
  58. package/shell/review/place.test.ts +26 -1
  59. package/shell/review/place.ts +17 -0
  60. package/shell/review/review.css +97 -11
  61. package/shell/review/view.test.ts +115 -1
  62. package/shell/review/view.ts +92 -2
  63. package/shell/studio/StudioHub.tsx +1 -0
  64. package/shell/studio/hub.css +99 -13
  65. package/skills/miles-deploy/SKILL.md +24 -3
  66. package/templates/studio/.env.example +6 -0
  67. package/templates/studio/package.json +1 -1
@@ -4,6 +4,7 @@ import { isAdded, STUDIO_SITE, } from '../core/review/model.js';
4
4
  import { createReview, NO_PROJECT, ReviewError, } from '../core/review/service.js';
5
5
  import { jevTriager } from '../core/review/triage.js';
6
6
  import { hostOf } from './hosts.js';
7
+ import { chooseMailer, sendInvite } from './mailer.js';
7
8
  import { clearedCookie, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, studioGate, } from './session.js';
8
9
  import { chooseStore } from './stores/index.js';
9
10
  export const API_PATH = '/api/review';
@@ -36,7 +37,8 @@ function projectScope(scope) {
36
37
  }
37
38
  /** What a client's hub shows of a project: its slug, for the link, and its name. */
38
39
  const listed = ({ slug, name }) => ({ slug, name });
39
- async function query(op, params, { review, actor, scope }) {
40
+ async function query(op, params, ctx) {
41
+ const { review, actor, scope } = ctx;
40
42
  switch (op) {
41
43
  case 'me':
42
44
  // The client is this request's: the page judges its controls in it, as the service does every op.
@@ -57,19 +59,49 @@ async function query(op, params, { review, actor, scope }) {
57
59
  return { projects: (await review.listProjects(scope, actor)).map(listed) };
58
60
  case 'people':
59
61
  return { people: (await review.listPeople(scope, actor)).map(visible) };
60
- case 'access':
61
- return { access: await review.defaultAccess(scope, actor) };
62
+ case 'access': {
63
+ // The default first: it refuses anyone but Studio, so a Client never learns how mail stands.
64
+ const access = await review.defaultAccess(scope, actor);
65
+ return { access, mail: ctx.mailUnknown ? null : await mailState(review, ctx.mailer ?? null) };
66
+ }
62
67
  default:
63
68
  return undefined;
64
69
  }
65
70
  }
71
+ /** How sign-in mail stands, for Studio: off with no mailer, failing while the last failed send is newer than the last that went (email.md, "When mail breaks"). */
72
+ async function mailState(review, mailer) {
73
+ if (!mailer)
74
+ return 'off';
75
+ const { okAt, failedAt } = await review.mailRecord();
76
+ return failedAt !== null && (okAt === null || failedAt > okAt) ? 'failing' : 'on';
77
+ }
66
78
  /** A link as a client's host answers it: at its own root, where its gate answers it. */
67
79
  const link = (r) => ({ person: visible(r.person), link: `/invite/${r.token}` });
80
+ /**
81
+ * Mails a new person their invite (email.md, E10), unless Studio said not to
82
+ * (`mail: false`), they have no address, the studio has no mailer, or there
83
+ * is no host to name (sendInvite).
84
+ */
85
+ async function invite(review, { mailer, homeOf }, issued, wanted) {
86
+ const { person, token } = issued;
87
+ const home = wanted === false ? null : homeOf(person);
88
+ if (!mailer || !home || !person.email)
89
+ return 'off';
90
+ return sendInvite(mailer, review, { ...person, email: person.email }, token, home);
91
+ }
68
92
  /** Studio's commands over people, their links and how long they keep access. */
69
- async function peopleCommand(op, b, { review, actor, at, link }) {
93
+ async function peopleCommand(op, b, { review, actor, at, link, invites }) {
70
94
  switch (op) {
71
- case 'people.add':
72
- return link(await review.addPerson(at, actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
95
+ case 'people.add': {
96
+ const issued = await review.addPerson(at, actor, {
97
+ name: b.name,
98
+ team: b.team,
99
+ kind: b.kind,
100
+ access: b.access,
101
+ email: b.email,
102
+ });
103
+ return { ...link(issued), mailed: await invite(review, invites, issued, b.mail) };
104
+ }
73
105
  case 'people.link':
74
106
  return link(await review.newLink(at, actor, str(b.id)));
75
107
  case 'people.signOut':
@@ -78,6 +110,9 @@ async function peopleCommand(op, b, { review, actor, at, link }) {
78
110
  return { person: visible(await review.revoke(at, actor, str(b.id))) };
79
111
  case 'people.access':
80
112
  return { person: visible(await review.setAccess(at, actor, str(b.id), b.access)) };
113
+ case 'people.email':
114
+ // `email` must be sent: an address sets or changes it, null or empty clears it.
115
+ return { person: visible(await review.setEmail(at, actor, str(b.id), b.email)) };
81
116
  default:
82
117
  return undefined;
83
118
  }
@@ -93,12 +128,22 @@ const PROJECT_COMMANDS = [
93
128
  'prototype.audience',
94
129
  'control.audience',
95
130
  ];
131
+ /**
132
+ * Invites where a client's people are added: a Client's names the host the
133
+ * context gives, their client's own. A Studio person signs in by email on
134
+ * the studio's own host, which a client's host does not know, so none is
135
+ * mailed here; the studio hub mails them.
136
+ */
137
+ const clientInvites = ({ mailer = null, host = null }) => ({
138
+ mailer,
139
+ homeOf: (p) => (p.client === null ? null : host),
140
+ });
96
141
  async function command(op, b, ctx) {
97
142
  const { review, actor } = ctx;
98
143
  if (op === 'access.set')
99
144
  return { access: await review.setDefaultAccess(ctx.scope, actor, b.access) };
100
145
  if (!PROJECT_COMMANDS.includes(op))
101
- return peopleCommand(op, b, { review, actor, at: ctx.scope, link });
146
+ return peopleCommand(op, b, { review, actor, at: ctx.scope, link, invites: clientInvites(ctx) });
102
147
  const scope = projectScope(ctx.scope);
103
148
  const at = () => ({ prototype: prototypeOf(b.prototype), id: str(b.id) });
104
149
  switch (op) {
@@ -212,6 +257,7 @@ const PEOPLE_COMMANDS = [
212
257
  'people.signOut',
213
258
  'people.revoke',
214
259
  'people.access',
260
+ 'people.email',
215
261
  ];
216
262
  /**
217
263
  * The review API on the studio's own host, for a Studio person signed in
@@ -224,7 +270,7 @@ const PEOPLE_COMMANDS = [
224
270
  * No client's host has any of this: there `clients` is an unknown query, and
225
271
  * a `client` parameter is never read (S10).
226
272
  */
227
- const studioOps = (review, actor, host) => {
273
+ const studioOps = (review, actor, host, mailer) => {
228
274
  const where = async (client) => {
229
275
  if (client === undefined || client === null)
230
276
  return { at: STUDIO_SITE, host };
@@ -233,14 +279,14 @@ const studioOps = (review, actor, host) => {
233
279
  };
234
280
  const people = async (client) => {
235
281
  const w = await where(client);
282
+ // The host a person signs in on: a Client's from their client's record, never this request's; a Studio person's this one.
283
+ const homeOf = (p) => (p.client === null ? host : w.host);
236
284
  return {
237
285
  review,
238
286
  actor,
239
287
  at: w.at,
240
- link: (r) => ({
241
- person: visible(r.person),
242
- link: `https://${r.person.client === null ? host : w.host}/invite/${r.token}`,
243
- }),
288
+ link: (r) => ({ person: visible(r.person), link: `https://${homeOf(r.person)}/invite/${r.token}` }),
289
+ invites: { mailer, homeOf },
244
290
  };
245
291
  };
246
292
  return {
@@ -257,6 +303,7 @@ const studioOps = (review, actor, host) => {
257
303
  if (op === 'access')
258
304
  return {
259
305
  access: at === STUDIO_SITE ? await review.studioAccess(actor) : await review.defaultAccess(at, actor),
306
+ mail: await mailState(review, mailer),
260
307
  };
261
308
  // A client's section lists its own people; the Studio people have a section of their own.
262
309
  const listed = await review.listPeople(at, actor);
@@ -294,6 +341,8 @@ export function studioReview(env, checks = null, now) {
294
341
  keys,
295
342
  review: createReview(store.open(), {
296
343
  triage: jevTriager(env.TYPESAFE_API_KEY ?? '', checks),
344
+ // Sign-in codes are kept as an HMAC under the gate's secret (email.md, section 4).
345
+ codeKey: keys.secret,
297
346
  ...(now ? { now } : {}),
298
347
  }),
299
348
  };
@@ -330,30 +379,35 @@ const signedOut = (request) => json(401, { error: 'Sign in with the link the stu
330
379
  * signed out, or signed in for another client, gets one answer whatever
331
380
  * project the request names.
332
381
  */
333
- async function opsFor(request, { review, keys }, { host, site }) {
382
+ async function opsFor(request, { review, keys, mailer }, { host, site }) {
334
383
  if (site === STUDIO_SITE) {
335
384
  const actor = await actorFrom(request, review, studioGate(keys), STUDIO_SITE);
336
- return actor ? studioOps(review, actor, host) : signedOut(request);
385
+ return actor ? studioOps(review, actor, host, mailer) : signedOut(request);
337
386
  }
338
387
  const client = site.slug;
339
388
  const actor = await actorFrom(request, review, { ...keys, client, since: site.createdAt }, { client });
340
389
  if (!actor)
341
390
  return signedOut(request);
342
391
  const scope = await studioScope(request, review, client);
343
- return scope ? clientOps({ review, actor, scope }) : json(404, { error: 'Not found.' });
392
+ // An invite names the client's host from its record, as its links are on it (S9: a codename host too).
393
+ return scope
394
+ ? clientOps({ review, actor, scope, mailer, host: site.host })
395
+ : json(404, { error: 'Not found.' });
344
396
  }
345
397
  /**
346
398
  * A studio site's review function: the host's client is the scope, and the
347
399
  * gate's client, of every op; the studio's own host has the studio hub's.
400
+ * `mailer` is the studio's, chosen from the env as the gate chooses it
401
+ * (chooseMailer), unless a test gives one or null.
348
402
  */
349
- export async function studioHandler(request, checks, hosts) {
403
+ export async function studioHandler(request, checks, hosts, mailer = chooseMailer(process.env)) {
350
404
  const setup = studioReview(process.env, checks);
351
405
  if ('error' in setup)
352
406
  return json(503, { error: setup.error });
353
407
  let ops;
354
408
  try {
355
409
  const found = await hostSite(request, setup.review, hosts);
356
- ops = found ? await opsFor(request, setup, found) : json(404, { error: 'Not found.' });
410
+ ops = found ? await opsFor(request, { ...setup, mailer }, found) : json(404, { error: 'Not found.' });
357
411
  }
358
412
  catch (error) {
359
413
  console.error('[review]', error);
@@ -6,6 +6,7 @@ import { checks } from './checks.js';
6
6
  import { readEnv } from './env.js';
7
7
  import { fileKV } from './file-kv.js';
8
8
  import { LOCAL_SCOPE, LOCAL_STORE, localStudio } from './local.js';
9
+ import { fakeMailer } from './mailers/fake.js';
9
10
  import { ROOT } from './paths.js';
10
11
  // Connect strips the mount path from req.url; originalUrl keeps the query the handler reads.
11
12
  async function toRequest(req) {
@@ -30,6 +31,24 @@ async function send(res, response) {
30
31
  });
31
32
  res.end(Buffer.from(await response.arrayBuffer()));
32
33
  }
34
+ /**
35
+ * A mailer that keeps mail in memory, for seeing the People tab's mail lines
36
+ * in dev: `DESIGN_HUB_DEV_MAIL=on` sends, `failing` refuses, each recorded
37
+ * once at start so the tab opens in that state, and `off` has none. Unset,
38
+ * there is none and the tab shows no sign-in line: dev cannot know how the
39
+ * project's studio sends mail. Nothing leaves the machine.
40
+ */
41
+ function devMailer(review) {
42
+ const mode = process.env.DESIGN_HUB_DEV_MAIL;
43
+ if (mode !== 'on' && mode !== 'failing')
44
+ return null;
45
+ const mailer = fakeMailer('Dev Studio');
46
+ mailer.answer = mode === 'on' ? 'sent' : 'refused';
47
+ void review.recordMail(mailer.answer);
48
+ return mailer;
49
+ }
50
+ /** Unset, dev says nothing of sign-in mail; `off`, `on` and `failing` show each state. */
51
+ const mailUnknown = () => !['off', 'on', 'failing'].includes(process.env.DESIGN_HUB_DEV_MAIL ?? '');
33
52
  export function devReview() {
34
53
  return {
35
54
  name: 'dev-review',
@@ -41,11 +60,12 @@ export function devReview() {
41
60
  triage: jevTriager(readEnv(ROOT).TYPESAFE_API_KEY ?? '', checks),
42
61
  });
43
62
  const actor = localStudio();
63
+ const mailer = devMailer(review);
44
64
  // The local Studio person is in the people list like anyone else, so the people panel shows them.
45
65
  void kv.hset(keyOf('studio', 'people'), actor.id, JSON.stringify(actor));
46
66
  server.middlewares.use(API_PATH, (req, res, nextMiddleware) => {
47
67
  toRequest(req)
48
- .then((request) => handleReview(request, { review, actor, scope: LOCAL_SCOPE }))
68
+ .then((request) => handleReview(request, { review, actor, scope: LOCAL_SCOPE, mailer, mailUnknown: mailUnknown() }))
49
69
  .then((response) => send(res, response))
50
70
  .catch(nextMiddleware);
51
71
  });
@@ -2,6 +2,7 @@ import { GATE_FONT, type Route } from '../core/prototype.ts';
2
2
  import { type Client, type Person, type PersonKind, type Project, type Scope } from '../core/review/model.ts';
3
3
  import { type Review } from '../core/review/service.ts';
4
4
  import { type HostLookup } from './hosts.ts';
5
+ import { type Mailer } from './mailer.ts';
5
6
  import { type Gate } from './session.ts';
6
7
  export declare const DEFAULT_SITE_NAME = "Prototypes";
7
8
  /**
@@ -11,7 +12,7 @@ export declare const DEFAULT_SITE_NAME = "Prototypes";
11
12
  * (`next`) or to another URL of the site (`rewrite`, a function), each with
12
13
  * the headers to add to the answer.
13
14
  */
14
- export type Decision = Response | {
15
+ export type Decision = Response | Later | {
15
16
  pass: 'next';
16
17
  headers?: Record<string, string>;
17
18
  } | {
@@ -19,6 +20,15 @@ export type Decision = Response | {
19
20
  to: URL;
20
21
  headers: Record<string, string>;
21
22
  };
23
+ /**
24
+ * A Response with work to finish after it is sent (a code's mail): the
25
+ * adapter answers at once and keeps the work running (on Vercel, waitUntil).
26
+ * The work never rejects, and the answer never waits for it (E7).
27
+ */
28
+ export type Later = {
29
+ answer: Response;
30
+ after: Promise<void>;
31
+ };
22
32
  export { GATE_FONT };
23
33
  export declare function gatePreview(page: string | null, siteName?: string): Promise<{
24
34
  html: string;
@@ -37,17 +47,30 @@ export declare const plain: (request: Request, status: number, text: string, ext
37
47
  export declare const notFound: (request: Request) => Response;
38
48
  /** The store did not answer: a 503 that passes, so it says when to try again. */
39
49
  export declare const storeDown: (request: Request) => Response;
40
- /** One request at one moment: `at` is the gate's clock, so freshness is tested without waiting. */
50
+ /**
51
+ * One request at one moment: `at` is the gate's clock, so freshness is tested
52
+ * without waiting. `mailer`: the studio's, or none (no email sign-in).
53
+ * `address`: the network address the host's adapter gives, never a header
54
+ * the gate reads itself; none is not counted against the cap.
55
+ */
41
56
  export type Ctx = {
42
57
  siteName: string;
43
58
  request: Request;
44
59
  at: Date;
60
+ mailer?: Mailer | null;
61
+ address?: string | null;
45
62
  };
46
63
  export type ProxyOptions = {
47
64
  /** The gate pages' title, the same on every client's host. */
48
65
  siteName?: string;
49
66
  /** The gate's clock; tests move it instead of waiting. */
50
67
  now?: () => Date;
68
+ /** The studio's mailer, or null for none; absent, it is chosen from the env on each request (chooseMailer). */
69
+ mailer?: Mailer | null;
70
+ };
71
+ /** What the host's adapter knows of a request that no header the caller sends can say: its network address. */
72
+ export type From = {
73
+ address?: string | null;
51
74
  };
52
75
  /** Who a request's host is: its client, and the gate and the client's scope built from that one lookup. */
53
76
  export type Place = {
@@ -94,4 +117,4 @@ export declare function admit(found: Found, request: Request, at: Date): Promise
94
117
  /** The answer for a refusal: the same from the gate and from the files function. */
95
118
  export declare function refuse(ctx: Ctx, refusal: Refusal): Promise<Response> | Response;
96
119
  /** The gate a host's adapter runs on every request (vercel/proxy.ts on Vercel). Its sign-in page names no client (S10). */
97
- export declare function createGate({ siteName, now }?: ProxyOptions): (request: Request) => Promise<Decision>;
120
+ export declare function createGate(options?: ProxyOptions): (request: Request, { address }?: From) => Promise<Decision>;