@milesroxas/design-hub 0.13.0 → 0.15.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 (101) hide show
  1. package/README.md +38 -33
  2. package/core/catalog.test.ts +28 -1
  3. package/core/catalog.ts +21 -4
  4. package/core/prototype.test.ts +12 -1
  5. package/core/prototype.ts +21 -2
  6. package/core/review/model.ts +71 -6
  7. package/core/review/service.test.ts +536 -1
  8. package/core/review/service.ts +452 -25
  9. package/core/review/store.ts +45 -4
  10. package/dist/core/catalog.d.ts +8 -1
  11. package/dist/core/catalog.js +12 -3
  12. package/dist/core/prototype.d.ts +20 -2
  13. package/dist/core/prototype.js +17 -2
  14. package/dist/core/review/model.d.ts +42 -3
  15. package/dist/core/review/model.js +52 -3
  16. package/dist/core/review/service.d.ts +81 -7
  17. package/dist/core/review/service.js +407 -23
  18. package/dist/core/review/store.d.ts +29 -2
  19. package/dist/core/review/store.js +4 -0
  20. package/dist/scripts/client.d.ts +1 -1
  21. package/dist/scripts/client.js +1 -1
  22. package/dist/scripts/lib/catalog.d.ts +1 -1
  23. package/dist/scripts/lib/catalog.js +3 -3
  24. package/dist/scripts/lib/dns.d.ts +16 -2
  25. package/dist/scripts/lib/dns.js +66 -3
  26. package/dist/scripts/lib/fake-host.d.ts +16 -1
  27. package/dist/scripts/lib/fake-host.js +36 -1
  28. package/dist/scripts/lib/host.d.ts +2 -0
  29. package/dist/scripts/lib/resend.d.ts +36 -0
  30. package/dist/scripts/lib/resend.js +66 -0
  31. package/dist/scripts/lib/studio-deps.d.ts +7 -0
  32. package/dist/scripts/lib/studio-deps.js +6 -1
  33. package/dist/scripts/lib/studios.d.ts +2 -2
  34. package/dist/scripts/lib/studios.js +5 -5
  35. package/dist/scripts/lib/vercel-host.js +6 -0
  36. package/dist/scripts/new.d.ts +8 -3
  37. package/dist/scripts/new.js +15 -4
  38. package/dist/scripts/people.d.ts +16 -4
  39. package/dist/scripts/people.js +67 -17
  40. package/dist/scripts/preflight.js +1 -1
  41. package/dist/scripts/share.js +3 -3
  42. package/dist/scripts/studio.d.ts +19 -3
  43. package/dist/scripts/studio.js +327 -6
  44. package/dist/server/api.d.ts +25 -2
  45. package/dist/server/api.js +77 -17
  46. package/dist/server/dev-studio.d.ts +36 -0
  47. package/dist/server/dev-studio.js +181 -0
  48. package/dist/server/dev.d.ts +16 -1
  49. package/dist/server/dev.js +23 -3
  50. package/dist/server/gate.d.ts +26 -3
  51. package/dist/server/gate.js +323 -52
  52. package/dist/server/mailer.d.ts +60 -0
  53. package/dist/server/mailer.js +104 -0
  54. package/dist/server/mailers/fake.d.ts +8 -0
  55. package/dist/server/mailers/fake.js +16 -0
  56. package/dist/server/mailers/resend.d.ts +21 -0
  57. package/dist/server/mailers/resend.js +49 -0
  58. package/dist/server/session.js +4 -1
  59. package/dist/vercel/proxy.js +10 -3
  60. package/dist/vite/plugin.js +6 -1
  61. package/kit/ViewTools.tsx +9 -0
  62. package/kit/prefs.ts +3 -4
  63. package/kit/ui/Menu.tsx +29 -18
  64. package/kit/ui/layer.ts +41 -0
  65. package/package.json +1 -1
  66. package/shell/Command.tsx +142 -0
  67. package/shell/Index.tsx +902 -400
  68. package/shell/Milestone.tsx +302 -0
  69. package/shell/Sidebar.tsx +236 -0
  70. package/shell/index.css +224 -0
  71. package/shell/milestones.test.ts +157 -0
  72. package/shell/milestones.ts +144 -0
  73. package/shell/registry.ts +4 -3
  74. package/shell/review/Bar.tsx +1 -0
  75. package/shell/review/Card.tsx +17 -7
  76. package/shell/review/People.tsx +908 -134
  77. package/shell/review/Review.tsx +2 -2
  78. package/shell/review/anchor.ts +6 -1
  79. package/shell/review/client.ts +41 -7
  80. package/shell/review/menu-place.test.ts +41 -0
  81. package/shell/review/place.test.ts +26 -1
  82. package/shell/review/place.ts +17 -0
  83. package/shell/review/review.css +97 -11
  84. package/shell/review/view.test.ts +115 -1
  85. package/shell/review/view.ts +92 -2
  86. package/shell/shell.css +8 -175
  87. package/shell/sidebar.css +86 -0
  88. package/shell/studio/ClientHub.tsx +21 -10
  89. package/shell/studio/StudioHub.tsx +121 -45
  90. package/shell/studio/hub.css +118 -27
  91. package/shell/studio/page.tsx +28 -17
  92. package/shell/system.css +12 -0
  93. package/skills/miles-deploy/SKILL.md +25 -4
  94. package/skills/miles-new-feature/SKILL.md +2 -1
  95. package/skills/miles-prototype/SKILL.md +1 -1
  96. package/templates/app/README.md +1 -1
  97. package/templates/brand/README.md +1 -1
  98. package/templates/site/README.md +1 -1
  99. package/templates/space/README.md +1 -1
  100. package/templates/studio/.env.example +6 -0
  101. package/templates/studio/package.json +1 -1
@@ -8,6 +8,8 @@ import { LOCAL_CLIENT, type Scope } from './model.ts';
8
8
 
9
9
  /** A comment's or a control's prototype: any kebab-case name, so reading one that is reserved finds nothing, as before. */
10
10
  const isKebab = (v: unknown): v is string => typeof v === 'string' && SLUG.test(v);
11
+ /** A minute's number in a key: a whole count, never anything that could hold a `:`. */
12
+ const isCount = (v: unknown): v is number => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
11
13
 
12
14
  export interface KV {
13
15
  hget(key: string, field: string): Promise<string | null>;
@@ -49,8 +51,14 @@ const PREFIX = 'hub4:';
49
51
  * this database serves (its studio.json `id`), set once by `studio create`
50
52
  * and checked by every command before it acts (server/mark.ts). No other
51
53
  * key names a studio, so two studios on one database would be one studio.
54
+ * `mail`, two fields, `okAt` and `failedAt`: when a mail the studio sends was
55
+ * last taken and last not, set after every send by whoever sent it.
56
+ * `hits:<minute>` (minutes since the epoch): the gate's cap per network
57
+ * address, `<kind>:<HMAC of the address>` to the requests counted that
58
+ * minute, raised by HINCRBY, so the count is the cap; the key of two minutes
59
+ * before goes on the next count.
52
60
  */
53
- type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark' | 'proof';
61
+ type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark' | 'proof' | 'mail';
54
62
 
55
63
  /**
56
64
  * The studio's clients, outside every scope: `clients`, a client's slug to
@@ -78,8 +86,38 @@ const DIRECTORY: readonly string[] = ['clients', 'hosts', 'claims', 'held'];
78
86
  * the answer the gate gives for that ask (PROOF_PATH in prototype.ts).
79
87
  * `handoffs`: Studio opening this client from the studio's host,
80
88
  * hex(sha256(token)) to a Handoff, deleted when the client's host claims it.
89
+ *
90
+ * Email sign-in, kept the same way for a client's people and the studio's
91
+ * own: `emails`, an address to the id of the one live person holding it,
92
+ * claimed with HSETNX and released when the address changes or the person is
93
+ * revoked. `codes`, a person's id to their one live Code; a dead or expired
94
+ * one stays until a new code replaces it or a right answer removes it.
95
+ * `tries`, `<person id>:<code's end, ms>` to the wrong tries on that code,
96
+ * raised by HINCRBY before each compare, so each code counts its own and no
97
+ * reset is ever raced; ended codes' fields go when a new code is made. `sends`,
98
+ * `<person id>:<hour>` (hours since the epoch) to the codes made for them
99
+ * that hour, raised by HINCRBY before a code is made, so the count is the
100
+ * limit; their other hours' fields go on their next ask. `asks`,
101
+ * `<person id>:<minute>` to the asks of that clock minute, raised by HINCRBY:
102
+ * only the ask that raised it to 1 may make a code, so two at once make one;
103
+ * their other minutes' fields go on their next ask. `used` (email.md
104
+ * calls it `claims`, which the studio's directory already names), what a
105
+ * used link or code was claimed by, for a second press from the same browser:
106
+ * hex(sha256(token)) of a link, or a code's HMAC, to a Claim, written with
107
+ * HSETNX, so of two claims at once one holds; and a person's id to their one
108
+ * Claim's field, so their earlier one goes when they make a new one.
81
109
  */
82
- type ClientName = 'people' | 'revoked' | 'epochs' | 'links';
110
+ type ClientName =
111
+ | 'people'
112
+ | 'revoked'
113
+ | 'epochs'
114
+ | 'links'
115
+ | 'emails'
116
+ | 'codes'
117
+ | 'tries'
118
+ | 'sends'
119
+ | 'asks'
120
+ | 'used';
83
121
  type ClientOnlyName = 'projects' | 'proof' | 'handoffs';
84
122
 
85
123
  /**
@@ -112,7 +150,7 @@ const PROJECT_NAMES: readonly string[] = [
112
150
  ];
113
151
  const PER_PROTOTYPE: readonly string[] = ['comments', 'controls'];
114
152
 
115
- function part(ok: (v: unknown) => v is string, v: unknown, what: string): string {
153
+ function part<T extends string | number>(ok: (v: unknown) => v is T, v: unknown, what: string): T {
116
154
  // A bad part is a bug in the caller, never input to answer: fail closed before any store call.
117
155
  if (!ok(v)) throw new Error(`Not a store key: ${what} is ${JSON.stringify(v)}.`);
118
156
  return v;
@@ -130,10 +168,13 @@ const projectPrefix = (scope: Scope) =>
130
168
  * no part can hold the `:` that joins them.
131
169
  */
132
170
  export function keyOf(scope: 'studio', name: StudioName | DirectoryName): string;
171
+ export function keyOf(scope: 'studio', name: 'hits', minute: number): string;
133
172
  export function keyOf(scope: 'studio' | Scope, name: ClientName): string;
134
173
  export function keyOf(scope: Scope, name: ClientOnlyName | ProjectName): string;
135
174
  export function keyOf(scope: Scope, name: PrototypeName, prototype: string): string;
136
- export function keyOf(scope: 'studio' | Scope, name: string, prototype?: string): string {
175
+ export function keyOf(scope: 'studio' | Scope, name: string, prototype?: string | number): string {
176
+ if (scope === 'studio' && name === 'hits')
177
+ return `${PREFIX}studio:hits:${part(isCount, prototype, 'minute')}`;
137
178
  if (scope === 'studio') return DIRECTORY.includes(name) ? `${PREFIX}${name}` : `${PREFIX}studio:${name}`;
138
179
  if (!PROJECT_NAMES.includes(name)) return `${clientPrefix(scope.client)}${name}`;
139
180
  const project = `${projectPrefix(scope)}${name}`;
@@ -1,6 +1,7 @@
1
1
  import { type Track } from './project.ts';
2
2
  import { type State } from './states.ts';
3
- export type Kind = 'sprint' | 'alternate' | 'exploration';
3
+ /** A tool makes assets for several features, so it answers no one feature's question and lists apart. */
4
+ export type Kind = 'sprint' | 'alternate' | 'exploration' | 'tool';
4
5
  export type Meta = {
5
6
  slug: string;
6
7
  title: string;
@@ -12,11 +13,17 @@ export type Meta = {
12
13
  kind: Kind;
13
14
  feature: string;
14
15
  created: string;
16
+ /** The day the client saw it, for `status: presented` (a take shown and not picked); empty otherwise. */
17
+ presented: string;
18
+ /** The one question it answers: the first paragraph under `## Question`; empty without one. */
19
+ question: string;
15
20
  /** null when the README has no States section. */
16
21
  states: State[] | null;
17
22
  };
18
23
  export declare function readMeta(slug: string, readme: string): Meta;
19
24
  export declare const isLive: (m: Pick<Meta, 'status'>) => boolean;
25
+ /** Still the feature's own work: neither archived nor a take the client saw and did not pick. */
26
+ export declare const isOpen: (m: Pick<Meta, 'status'>) => boolean;
20
27
  export declare const byIndexOrder: (a: Meta, b: Meta) => number;
21
28
  /** Where a prototype stands, in words: the Space, the shell its track draws, or a board of artboards. */
22
29
  export declare function placeLabel(m: Pick<Meta, 'context' | 'track'>): string;
@@ -5,6 +5,11 @@
5
5
  import { frontmatter } from './frontmatter.js';
6
6
  import { TRACKS } from './project.js';
7
7
  import { parseStates } from './states.js';
8
+ const KINDS = ['sprint', 'alternate', 'exploration', 'tool'];
9
+ /** The first paragraph under `## Question`, on one line. */
10
+ const questionOf = (readme) => (readme.match(/^## Question[ \t]*\n+([^\n#|][^\n]*(?:\n[^\n#|][^\n]*)*)/m)?.[1] ?? '')
11
+ .replace(/\s+/g, ' ')
12
+ .trim();
8
13
  export function readMeta(slug, readme) {
9
14
  const { data } = frontmatter(readme);
10
15
  return {
@@ -14,15 +19,19 @@ export function readMeta(slug, readme) {
14
19
  context: data.context || 'space',
15
20
  track: TRACKS.includes(data.track) ? data.track : '',
16
21
  status: data.status || 'exploring',
17
- kind: data.kind === 'sprint' || data.kind === 'alternate' ? data.kind : 'exploration',
22
+ kind: KINDS.includes(data.kind ?? '') ? data.kind : 'exploration',
18
23
  feature: data.feature || '',
19
24
  created: data.created || '',
25
+ presented: data.presented || '',
26
+ question: questionOf(readme),
20
27
  states: parseStates(readme),
21
28
  };
22
29
  }
23
30
  export const isLive = (m) => m.status !== 'archived';
24
- // Open questions sit above answered ones; within a status, newest first.
25
- const rank = (status) => (status === 'exploring' ? 0 : 1);
31
+ /** Still the feature's own work: neither archived nor a take the client saw and did not pick. */
32
+ export const isOpen = (m) => isLive(m) && m.status !== 'presented';
33
+ // Open work (a question being explored, a tool being built) sits above answered work; within a status, newest first.
34
+ const rank = (status) => (status === 'exploring' || status === 'in-progress' ? 0 : 1);
26
35
  export const byIndexOrder = (a, b) => rank(a.status) - rank(b.status) || b.created.localeCompare(a.created) || a.title.localeCompare(b.title);
27
36
  /** Where a prototype stands, in words: the Space, the shell its track draws, or a board of artboards. */
28
37
  export function placeLabel(m) {
@@ -4,8 +4,8 @@
4
4
  * prototypes, the shared bundle, the walkthroughs, the hub's own top files).
5
5
  */
6
6
  export declare const RESERVED: readonly ['invite', 'api', '_gate', '_p', 'assets', 'guide', 'index.html', 'favicon.svg'];
7
- /** Never a client's slug: the studio's own host, and the names a host is often given. */
8
- export declare const RESERVED_CLIENTS: readonly ['studio', 'www', 'api'];
7
+ /** Never a client's slug: the studio's own host, the names a host is often given, and `mail`, the default sender's subdomain (email.md, E11). */
8
+ export declare const RESERVED_CLIENTS: readonly ['studio', 'www', 'api', 'mail'];
9
9
  /**
10
10
  * Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
11
11
  * Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
@@ -35,6 +35,15 @@ export declare const proofPath: (ask: string) => string;
35
35
  export declare const handoffPath: (token: string) => string;
36
36
  /** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
37
37
  export declare const HANDOFF_START = "/_gate/handoff";
38
+ /**
39
+ * Email sign-in, on a client's host and on the studio's (email.md): a POST of
40
+ * an address asks for a code, a POST of the code uses it. The gate answers
41
+ * them only when the studio has a mailer; otherwise each is what any other
42
+ * path there is.
43
+ */
44
+ export declare const CODE_ASK = "/_gate/code";
45
+ export declare const CODE_USE = "/_gate/code/use";
46
+ type CodeStep = 'ask' | 'use';
38
47
  /**
39
48
  * Where a request on a studio site goes:
40
49
  *
@@ -42,6 +51,7 @@ export declare const HANDOFF_START = "/_gate/handoff";
42
51
  * - `link`: `/invite/<token>`, at the host root only.
43
52
  * - `handoff`: `/_gate/handoff/<token>`, a handoff's claim, at the host root only.
44
53
  * - `asset`: the gate's font and the favicon, served before sign-in.
54
+ * - `code`: `/_gate/code` and `/_gate/code/use`, email sign-in, when the studio has a mailer.
45
55
  * - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
46
56
  * - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
47
57
  * - `file`: a file of a project's build, as its segments ([] for the project's own page), and
@@ -57,6 +67,9 @@ export type Where = {
57
67
  token: string;
58
68
  } | {
59
69
  kind: 'asset';
70
+ } | {
71
+ kind: 'code';
72
+ step: CodeStep;
60
73
  } | {
61
74
  kind: 'hub';
62
75
  } | {
@@ -93,6 +106,7 @@ export declare const STUDIO_PAGE = "/studio.html";
93
106
  * - `file`: the studio hub's bundle, below `/_studio/`.
94
107
  * - `api`: `/api/review`, where the studio hub reads every client.
95
108
  * - `handoff`: `/_gate/handoff`, where the studio hub asks to open a client.
109
+ * - `code`: `/_gate/code` and `/_gate/code/use`, a Studio person's email sign-in.
96
110
  * - `proof`: `/_gate/proof/<ask>`, the host proof `studio create` and
97
111
  * `studio deploy` ask the studio's own host for, as `client add` asks a client's.
98
112
  */
@@ -111,6 +125,9 @@ export type StudioWhere = {
111
125
  kind: 'api';
112
126
  } | {
113
127
  kind: 'handoff';
128
+ } | {
129
+ kind: 'code';
130
+ step: CodeStep;
114
131
  } | {
115
132
  kind: 'proof';
116
133
  ask: string;
@@ -132,3 +149,4 @@ export declare function routeOf(host: string, pathname: string): Route;
132
149
  * reads as nothing shared.
133
150
  */
134
151
  export declare function probeEntry(path: string, fetcher: typeof fetch): Promise<boolean>;
152
+ export {};
@@ -17,8 +17,8 @@ export const RESERVED = [
17
17
  'index.html',
18
18
  'favicon.svg',
19
19
  ];
20
- /** Never a client's slug: the studio's own host, and the names a host is often given. */
21
- export const RESERVED_CLIENTS = ['studio', 'www', 'api'];
20
+ /** Never a client's slug: the studio's own host, the names a host is often given, and `mail`, the default sender's subdomain (email.md, E11). */
21
+ export const RESERVED_CLIENTS = ['studio', 'www', 'api', 'mail'];
22
22
  /**
23
23
  * Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
24
24
  * Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
@@ -108,6 +108,15 @@ const HANDOFF_PATH = /^\/_gate\/handoff\/([\w-]{20,100})$/;
108
108
  export const handoffPath = (token) => `/_gate/handoff/${token}`;
109
109
  /** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
110
110
  export const HANDOFF_START = '/_gate/handoff';
111
+ /**
112
+ * Email sign-in, on a client's host and on the studio's (email.md): a POST of
113
+ * an address asks for a code, a POST of the code uses it. The gate answers
114
+ * them only when the studio has a mailer; otherwise each is what any other
115
+ * path there is.
116
+ */
117
+ export const CODE_ASK = '/_gate/code';
118
+ export const CODE_USE = '/_gate/code/use';
119
+ const codeStep = (pathname) => pathname === CODE_ASK ? 'ask' : pathname === CODE_USE ? 'use' : null;
111
120
  /**
112
121
  * The studio site's functions, at a client host's root: the review API, and
113
122
  * the files function the gate rewrites a project's files to. The gate passes
@@ -135,6 +144,9 @@ function where(pathname) {
135
144
  return { kind: 'handoff', token: handoff[1] };
136
145
  if (GATE_ASSETS.includes(pathname))
137
146
  return { kind: 'asset' };
147
+ const step = codeStep(pathname);
148
+ if (step)
149
+ return { kind: 'code', step };
138
150
  const parts = segments(pathname);
139
151
  if (!parts)
140
152
  return { kind: 'none' };
@@ -168,6 +180,9 @@ export function studioRouteOf(pathname) {
168
180
  return { kind: 'api' };
169
181
  if (pathname === HANDOFF_START)
170
182
  return { kind: 'handoff' };
183
+ const step = codeStep(pathname);
184
+ if (step)
185
+ return { kind: 'code', step };
171
186
  const proof = pathname.match(PROOF_PATH);
172
187
  if (proof)
173
188
  return { kind: 'proof', ask: proof[1] };
@@ -59,7 +59,12 @@ export type Person = {
59
59
  * client. Never stored in the profile: it is where the person's records live.
60
60
  */
61
61
  client: string | null;
62
- /** Always null until email sign-in exists. */
62
+ /**
63
+ * The address they sign in with by code (lower case, trimmed), or null for
64
+ * links only. Within one client's people, or the studio's own, one live
65
+ * person holds an address (`emails` in store.ts). A profile stored before
66
+ * email existed reads as null.
67
+ */
63
68
  email: string | null;
64
69
  /** When their access ends (ISO time), or null for open-ended. Their sessions end with it. */
65
70
  accessEndsAt: string | null;
@@ -125,6 +130,25 @@ export type Link = {
125
130
  person: string;
126
131
  expiresAt: string;
127
132
  };
133
+ /**
134
+ * A person's one live sign-in code (`codes` in store.ts): an HMAC of the code
135
+ * under a key the service is given, never the code or a bare digest, since
136
+ * 8 digits can be tried in full. A new code replaces it; a right answer
137
+ * removes it.
138
+ */
139
+ export type Code = {
140
+ hash: string;
141
+ expiresAt: string;
142
+ sentAt: string;
143
+ };
144
+ /** The code rules (E6): 8 digits, 10 minutes, 5 wrong tries, one a minute and 5 an hour per person. */
145
+ export declare const CODE: {
146
+ readonly digits: 8;
147
+ readonly minutes: 10;
148
+ readonly tries: 5;
149
+ readonly gapSeconds: 60;
150
+ readonly perHour: 5;
151
+ };
128
152
  /**
129
153
  * A handoff: a Studio person, signed in on the studio's host, opening one
130
154
  * client's hub (or one of its projects) on that client's host. Stored with
@@ -220,9 +244,18 @@ export type Checked<T> = {
220
244
  ok: false;
221
245
  problems: string[];
222
246
  };
247
+ /** What a refusal says when it names fields only: the panel then shows the fields' problems. */
248
+ export declare const NOT_VALID = "Some fields are not valid.";
249
+ /** What a refusal says when an address is the one thing wrong, so a person reads a sentence, not a field. */
250
+ export declare const NOT_AN_EMAIL = "That is not an email address. Use one such as name@example.com, or leave it empty.";
223
251
  export declare function checkAccess(input: unknown): Checked<Access>;
224
- /** `team` absent: the service fills it, the studio's name for Studio and the client's for a Client. */
225
- export type PersonInput = Pick<Person, 'name' | 'kind'> & {
252
+ export declare const EMAIL_PROBLEM = "email: an address such as name@example.com, or empty for links only";
253
+ /** An address as stored: trimmed and lower case, or null when it is not one. */
254
+ export declare function emailOf(input: unknown): string | null;
255
+ /** An address as Studio sets it: null, or empty, is links only; anything else must be an address. */
256
+ export declare function checkEmail(input: unknown): Checked<string | null>;
257
+ /** `team` absent: the service fills it, the studio's name for Studio and the client's for a Client. `email` absent is links only. */
258
+ export type PersonInput = Pick<Person, 'name' | 'kind' | 'email'> & {
226
259
  team?: string;
227
260
  access: Access | null;
228
261
  };
@@ -254,6 +287,12 @@ export declare function checkControl(input: {
254
287
  /** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
255
288
  export declare function checkViewAs(input: unknown): Checked<PersonKind | null>;
256
289
  export declare function checkBody(body: unknown): Checked<string>;
290
+ /**
291
+ * A path on the page's own site. Starting with `/` is not enough: a browser
292
+ * reads `//host`, `/\host` and `/<tab>/host` as another site, so the path is
293
+ * resolved the way a browser would and must stay on the same origin.
294
+ */
295
+ export declare function isSitePath(v: unknown): v is string;
257
296
  export declare function checkAnchor(input: unknown): Checked<Anchor>;
258
297
  /** The README states a client offers for triage: trimmed, capped, de-duplicated. */
259
298
  export declare function checkStates(input: unknown): string[];
@@ -80,6 +80,8 @@ export const STUDIO_CLIENT_SLUG = 'slug: kebab-case, and not studio, www, api or
80
80
  * its host, and `client add` with the same flags finishes it.
81
81
  */
82
82
  export const isAdded = (client) => client.provenAt !== null;
83
+ /** The code rules (E6): 8 digits, 10 minutes, 5 wrong tries, one a minute and 5 an hour per person. */
84
+ export const CODE = { digits: 8, minutes: 10, tries: 5, gapSeconds: 60, perHour: 5 };
83
85
  /**
84
86
  * How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
85
87
  * `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
@@ -106,7 +108,8 @@ const PROFILE_RULES = [
106
108
  ['name', isText],
107
109
  ['team', isText],
108
110
  ['kind', (v) => PERSON_KINDS.includes(v)],
109
- ['email', isTextOrNull],
111
+ // Absent on a profile stored before email existed: it reads as null (no migration).
112
+ ['email', (v) => v === undefined || isTextOrNull(v)],
110
113
  ['accessEndsAt', isTextOrNull],
111
114
  ['createdAt', isText],
112
115
  ];
@@ -132,6 +135,10 @@ export function hasAccess(p, at) {
132
135
  }
133
136
  export const authorOf = (p) => ({ id: p.id, name: p.name, team: p.team });
134
137
  export const isRoot = (c) => c.thread === c.id;
138
+ /** What a refusal says when it names fields only: the panel then shows the fields' problems. */
139
+ export const NOT_VALID = 'Some fields are not valid.';
140
+ /** What a refusal says when an address is the one thing wrong, so a person reads a sentence, not a field. */
141
+ export const NOT_AN_EMAIL = 'That is not an email address. Use one such as name@example.com, or leave it empty.';
135
142
  const text = (v, path, max, problems) => {
136
143
  if (typeof v !== 'string' || !v.trim()) {
137
144
  problems.push(`${path}: required`);
@@ -149,10 +156,35 @@ const ACCESS_PROBLEM = `access: one of ${ACCESS.join(', ')}`;
149
156
  export function checkAccess(input) {
150
157
  return isAccess(input) ? { ok: true, value: input } : { ok: false, problems: [ACCESS_PROBLEM] };
151
158
  }
159
+ /**
160
+ * What an address may be: the form a browser's email field takes (WHATWG),
161
+ * lower case, 254 characters at most. Nothing in it can split a mail header
162
+ * or name a second recipient: no space, comma, semicolon, angle bracket or quote.
163
+ */
164
+ const EMAIL = /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
165
+ const EMAIL_MAX = 254;
166
+ export const EMAIL_PROBLEM = 'email: an address such as name@example.com, or empty for links only';
167
+ /** An address as stored: trimmed and lower case, or null when it is not one. */
168
+ export function emailOf(input) {
169
+ if (typeof input !== 'string')
170
+ return null;
171
+ const address = input.trim().toLowerCase();
172
+ return address.length <= EMAIL_MAX && EMAIL.test(address) ? address : null;
173
+ }
174
+ /** An address as Studio sets it: null, or empty, is links only; anything else must be an address. */
175
+ export function checkEmail(input) {
176
+ if (input === null || (typeof input === 'string' && input.trim() === ''))
177
+ return { ok: true, value: null };
178
+ const address = emailOf(input);
179
+ return address ? { ok: true, value: address } : { ok: false, problems: [EMAIL_PROBLEM] };
180
+ }
152
181
  export function checkPersonInput(input) {
153
182
  const i = (input ?? {});
154
183
  const problems = [];
155
184
  const name = text(i.name, 'name', LIMITS.name, problems);
185
+ const email = checkEmail(i.email ?? null);
186
+ if (!email.ok)
187
+ problems.push(...email.problems);
156
188
  const team = i.team === undefined ? undefined : text(i.team, 'team', LIMITS.team, problems);
157
189
  // Most people added are on the client's side; Studio is the deliberate choice.
158
190
  const kind = i.kind ?? 'client';
@@ -170,6 +202,7 @@ export function checkPersonInput(input) {
170
202
  name,
171
203
  ...(team === undefined ? {} : { team }),
172
204
  kind: kind,
205
+ email: email.ok ? email.value : null,
173
206
  access: access,
174
207
  },
175
208
  };
@@ -273,14 +306,30 @@ export function checkBody(body) {
273
306
  }
274
307
  const isSlug = (v) => typeof v === 'string' && SLUG.test(v);
275
308
  const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
309
+ const PATH_BASE = 'https://path.invalid';
310
+ /**
311
+ * A path on the page's own site. Starting with `/` is not enough: a browser
312
+ * reads `//host`, `/\host` and `/<tab>/host` as another site, so the path is
313
+ * resolved the way a browser would and must stay on the same origin.
314
+ */
315
+ export function isSitePath(v) {
316
+ if (typeof v !== 'string' || !v.startsWith('/'))
317
+ return false;
318
+ try {
319
+ return new URL(v, PATH_BASE).origin === PATH_BASE;
320
+ }
321
+ catch {
322
+ return false;
323
+ }
324
+ }
276
325
  /** One rule per field: the field, whether a value passes, and what the field must be. */
277
326
  const ANCHOR_RULES = [
278
327
  ['prototype', isSlug, 'a prototype slug'],
279
328
  ['feature', (v) => v === '' || isSlug(v), 'a feature slug or empty'],
280
329
  [
281
330
  'url',
282
- (v) => typeof v === 'string' && v.startsWith('/') && v.length <= LIMITS.url,
283
- `a path starting with /, ${LIMITS.url} characters at most`,
331
+ (v) => isSitePath(v) && v.length <= LIMITS.url,
332
+ `a path on this site starting with /, ${LIMITS.url} characters at most`,
284
333
  ],
285
334
  [
286
335
  'dials',
@@ -19,6 +19,29 @@ export type Deps = {
19
19
  now: () => Date;
20
20
  random: (bytes: number) => Uint8Array;
21
21
  triage: Triager | null;
22
+ /**
23
+ * The key a sign-in code is stored under, as an HMAC (on a studio site, the
24
+ * gate's secret). The service reads no environment: a caller that makes or
25
+ * checks codes gives it; without one, askCode and useCode throw.
26
+ */
27
+ codeKey: string | null;
28
+ };
29
+ /**
30
+ * The gate's cap per network address, per clock minute (email.md, section 3):
31
+ * sign-in POSTs (a link's Continue, asking for a code, using one) and views of
32
+ * a link's page, each counted apart.
33
+ */
34
+ declare const CAP: {
35
+ readonly post: 10;
36
+ readonly view: 60;
37
+ };
38
+ export type CapKind = keyof typeof CAP;
39
+ /** What a mail provider answered a send: taken, refused, or no answer at all (server/mailer.ts). */
40
+ export type Sent = 'sent' | 'refused' | 'down';
41
+ /** When a mail the studio sent was last taken, and last not: null for never. */
42
+ export type MailRecord = {
43
+ okAt: string | null;
44
+ failedAt: string | null;
22
45
  };
23
46
  /**
24
47
  * How long a slug or host stays out of reach after its client is archived:
@@ -248,15 +271,27 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
248
271
  * an access length, a Client gets the default and Studio is open-ended.
249
272
  * Without a team, a Studio person gets the studio's, which every Studio
250
273
  * person carries, so the adder's; a Client gets their client's name.
274
+ * An email, when given, is claimed first: an address a live person of
275
+ * the same home holds is refused, with nothing written. The service
276
+ * never mails anyone; whoever calls it does.
251
277
  */
252
278
  addPerson(at: HostScope, actor: Person | null, input: unknown): Promise<Issued>;
253
279
  /** Another one-time link, for another device. Their sessions and other unused links stay. */
254
280
  newLink(scope: HostScope, actor: Person | null, id: string): Promise<Issued>;
255
- /** Ends every session they hold and every unused link, and gives one new link. */
281
+ /** Ends every session they hold, every unused link and their sign-in code, and gives one new link. */
256
282
  signOutEverywhere(scope: HostScope, actor: Person | null, id: string): Promise<Issued>;
257
283
  revoke(scope: HostScope, actor: Person | null, id: string): Promise<Person>;
258
284
  /** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
259
285
  setAccess(scope: HostScope, actor: Person | null, id: string, access: unknown): Promise<Person>;
286
+ /**
287
+ * Sets, changes or clears (null, or empty) the address one person signs
288
+ * in with by code. The new address is claimed before the profile names
289
+ * it, and refused while another live person of the same home holds it;
290
+ * the old one is freed after. The same address saved again claims it
291
+ * again, which mends an entry left missing or stale. Any change ends their live code, which was
292
+ * sent to the old address. Studio only (the `people` action).
293
+ */
294
+ setEmail(at: HostScope, actor: Person | null, id: string, email: unknown): Promise<Person>;
260
295
  /** How long a new Client of the scope's client keeps access unless Studio says otherwise: the client's default, else the studio's. */
261
296
  defaultAccess(scope: Scope, actor: Person | null): Promise<Access>;
262
297
  /**
@@ -276,14 +311,53 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
276
311
  /** Who a link signs in here, without using it: mail and chat previews open links too. */
277
312
  peekLink(at: HostScope, token: string): Promise<Person | null>;
278
313
  /**
279
- * Uses a link up and returns who it signs in. The delete is the claim: of
280
- * two requests with one token, only the one whose delete removed the link
281
- * gets a person, so a link signs in one device at most. Who it signs in is
282
- * read and checked first, so the delete is the last store call. Only a
283
- * link of the scope's client, or a Studio person's, is found at all; on
314
+ * Uses a link up and returns who it signs in. The claim (HSETNX on
315
+ * `used`, then the link's delete) decides: of two requests with one
316
+ * token, only the one that wrote the claim gets a person, so a link signs
317
+ * in one device at most. The other is a second press: it signs in only
318
+ * the same browser (`browser`, its `try` cookie) inside CLAIM_SECONDS,
319
+ * the person read again. Who it signs in is read and checked first. Only
320
+ * a link of the scope's client, or a Studio person's, is found at all; on
284
321
  * the studio's own host, only a Studio person's.
285
322
  */
286
- useLink(at: HostScope, token: string): Promise<Person | null>;
323
+ useLink(at: HostScope, token: string, browser?: string | null): Promise<Person | null>;
324
+ /**
325
+ * A new sign-in code for whoever holds `email` where `at` is (emailPerson),
326
+ * asked by the gate with no actor, in place of their live one: the person
327
+ * and the code, for the caller to mail, or null. Null, never a throw, for
328
+ * an address that is malformed, unknown here, revoked, past its access
329
+ * end, asked again inside a minute, or past CODE.perHour this hour, so
330
+ * the gate gives one answer whatever comes back. The store keeps only the
331
+ * code's HMAC (`hash`, which the mail's idempotency key names); the new
332
+ * code's wrong tries are counted apart from any other's.
333
+ */
334
+ askCode(at: HostScope, email: unknown): Promise<{
335
+ person: Person;
336
+ code: string;
337
+ hash: string;
338
+ } | null>;
339
+ /**
340
+ * Counts one request from a network address against the gate's cap
341
+ * (CAP), in this clock minute, before anyone is looked up: true while it
342
+ * is within. The address is kept as an HMAC under the code key, never as
343
+ * it is; the minute two before goes, so the store holds about two.
344
+ */
345
+ hit(kind: CapKind, address: string): Promise<boolean>;
346
+ /** Records what a mail the studio sent was answered, for Studio: the time of the last that was taken, or of the last that was not. */
347
+ recordMail(answer: Sent): Promise<void>;
348
+ /** When a mail the studio sent was last taken, and last not. */
349
+ mailRecord(): Promise<MailRecord>;
350
+ /**
351
+ * Signs in with a code, asked by the gate with no actor: who it signs in,
352
+ * or null. The wrong tries are raised before the compare, so the count is
353
+ * the limit: past CODE.tries the code is dead, a right answer too, and no
354
+ * two requests at once get a 6th try. The compare runs in constant time.
355
+ * The claim is useLink's: of two right answers at once, one signs in, and
356
+ * the other only when it is the same browser pressing again (`browser`).
357
+ * The person is read again after it, so a revoke, a sign out everywhere
358
+ * or a changed address that landed meanwhile signs nobody in.
359
+ */
360
+ useCode(at: HostScope, email: unknown, code: unknown, browser?: string | null): Promise<Person | null>;
287
361
  /**
288
362
  * A prototype's comments as the actor's view sees them. `viewAs: 'client'`
289
363
  * is Studio's Client view; it never widens a Client's.