@milesroxas/design-hub 0.10.0 → 0.11.1

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 (131) hide show
  1. package/README.md +13 -11
  2. package/core/files.test.ts +76 -0
  3. package/core/files.ts +131 -0
  4. package/core/prototype.test.ts +124 -7
  5. package/core/prototype.ts +133 -24
  6. package/core/review/model.ts +119 -2
  7. package/core/review/policy.test.ts +79 -22
  8. package/core/review/policy.ts +36 -8
  9. package/core/review/service.test.ts +1464 -340
  10. package/core/review/service.ts +777 -191
  11. package/core/review/store.test.ts +91 -0
  12. package/core/review/store.ts +143 -33
  13. package/dist/core/files.d.ts +39 -0
  14. package/dist/core/files.js +107 -0
  15. package/dist/core/prototype.d.ts +61 -12
  16. package/dist/core/prototype.js +105 -26
  17. package/dist/core/review/model.d.ts +76 -2
  18. package/dist/core/review/model.js +76 -1
  19. package/dist/core/review/policy.d.ts +11 -4
  20. package/dist/core/review/policy.js +14 -2
  21. package/dist/core/review/service.d.ts +197 -45
  22. package/dist/core/review/service.js +702 -192
  23. package/dist/core/review/store.d.ts +84 -31
  24. package/dist/core/review/store.js +66 -33
  25. package/dist/scripts/bootstrap.d.ts +4 -34
  26. package/dist/scripts/bootstrap.js +9 -244
  27. package/dist/scripts/cli.js +6 -0
  28. package/dist/scripts/client.d.ts +45 -0
  29. package/dist/scripts/client.js +320 -0
  30. package/dist/scripts/commands.js +6 -0
  31. package/dist/scripts/comments.d.ts +8 -26
  32. package/dist/scripts/comments.js +27 -14
  33. package/dist/scripts/lib/builds.d.ts +40 -0
  34. package/dist/scripts/lib/builds.js +198 -0
  35. package/dist/scripts/lib/cli.js +3 -1
  36. package/dist/scripts/lib/deploy.d.ts +6 -90
  37. package/dist/scripts/lib/deploy.js +24 -137
  38. package/dist/scripts/lib/dns.d.ts +49 -0
  39. package/dist/scripts/lib/dns.js +141 -0
  40. package/dist/scripts/lib/fake-host.d.ts +168 -0
  41. package/dist/scripts/lib/fake-host.js +244 -0
  42. package/dist/scripts/lib/host.d.ts +87 -0
  43. package/dist/scripts/lib/host.js +362 -0
  44. package/dist/scripts/lib/linked.d.ts +51 -0
  45. package/dist/scripts/lib/linked.js +124 -0
  46. package/dist/scripts/lib/names.d.ts +35 -0
  47. package/dist/scripts/lib/names.js +77 -0
  48. package/dist/scripts/lib/review.d.ts +23 -2
  49. package/dist/scripts/lib/review.js +32 -15
  50. package/dist/scripts/lib/studio-deps.d.ts +35 -0
  51. package/dist/scripts/lib/studio-deps.js +79 -0
  52. package/dist/scripts/lib/studios.d.ts +70 -0
  53. package/dist/scripts/lib/studios.js +187 -0
  54. package/dist/scripts/lib/update.d.ts +33 -0
  55. package/dist/scripts/lib/update.js +40 -0
  56. package/dist/scripts/lib/vercel.d.ts +1 -23
  57. package/dist/scripts/lib/vercel.js +6 -81
  58. package/dist/scripts/people.d.ts +7 -11
  59. package/dist/scripts/people.js +51 -46
  60. package/dist/scripts/preflight.d.ts +8 -1
  61. package/dist/scripts/preflight.js +36 -19
  62. package/dist/scripts/project.d.ts +86 -0
  63. package/dist/scripts/project.js +141 -0
  64. package/dist/scripts/share.d.ts +105 -30
  65. package/dist/scripts/share.js +82 -154
  66. package/dist/scripts/skills.d.ts +18 -2
  67. package/dist/scripts/skills.js +22 -3
  68. package/dist/scripts/studio.d.ts +75 -0
  69. package/dist/scripts/studio.js +401 -0
  70. package/dist/scripts/upgrade.d.ts +10 -2
  71. package/dist/scripts/upgrade.js +44 -14
  72. package/dist/server/api.d.ts +22 -11
  73. package/dist/server/api.js +116 -47
  74. package/dist/server/blob.d.ts +10 -0
  75. package/dist/server/blob.js +53 -0
  76. package/dist/server/checks.js +2 -2
  77. package/dist/server/dev.js +4 -4
  78. package/dist/server/env.d.ts +2 -0
  79. package/dist/server/env.js +9 -0
  80. package/dist/server/fake-upstash.js +52 -3
  81. package/dist/server/hosts.d.ts +11 -0
  82. package/dist/server/hosts.js +31 -0
  83. package/dist/server/local.d.ts +7 -1
  84. package/dist/server/local.js +10 -1
  85. package/dist/server/mark.d.ts +41 -0
  86. package/dist/server/mark.js +111 -0
  87. package/dist/server/session.d.ts +17 -9
  88. package/dist/server/session.js +22 -45
  89. package/dist/server/upstash.js +20 -0
  90. package/dist/vercel/files.d.ts +11 -0
  91. package/dist/vercel/files.js +68 -0
  92. package/dist/vercel/index.d.ts +4 -3
  93. package/dist/vercel/index.js +12 -8
  94. package/dist/vercel/proxy.d.ts +69 -4
  95. package/dist/vercel/proxy.js +171 -107
  96. package/dist/vite/plugin.d.ts +24 -1
  97. package/dist/vite/plugin.js +46 -2
  98. package/kit/dials.tsx +2 -2
  99. package/package.json +2 -1
  100. package/shell/Guide.tsx +11 -5
  101. package/shell/Index.tsx +32 -10
  102. package/shell/Notice.tsx +1 -1
  103. package/shell/controls.ts +1 -1
  104. package/shell/hub.ts +5 -1
  105. package/shell/main.tsx +18 -3
  106. package/shell/review/Review.tsx +6 -4
  107. package/shell/review/ThreadCard.tsx +20 -7
  108. package/shell/review/client.ts +42 -14
  109. package/shell/review/view.test.ts +19 -0
  110. package/shell/review/view.ts +25 -0
  111. package/shell/shell.css +4 -0
  112. package/shell/studio/ClientHub.tsx +98 -0
  113. package/shell/studio/hub.css +48 -0
  114. package/shell/studio/index.html +14 -0
  115. package/shell/studio/main.tsx +17 -0
  116. package/skills/miles-deploy/SKILL.md +120 -65
  117. package/skills/miles-design-hub/SKILL.md +11 -9
  118. package/skills/miles-review/SKILL.md +5 -7
  119. package/skills/miles-worktrees/SKILL.md +3 -3
  120. package/templates/conductor/settings.toml +1 -1
  121. package/templates/studio/.env.example +9 -0
  122. package/templates/studio/.vercelignore +5 -0
  123. package/templates/studio/README.md +84 -0
  124. package/templates/studio/_gitignore +7 -0
  125. package/templates/studio/api/files.ts +4 -0
  126. package/templates/studio/api/review.ts +4 -0
  127. package/templates/studio/build.mjs +4 -0
  128. package/templates/studio/package.json +16 -0
  129. package/templates/studio/proxy.ts +4 -0
  130. package/templates/studio/studio.json +9 -0
  131. package/templates/studio/vercel.json +27 -0
package/core/prototype.ts CHANGED
@@ -4,10 +4,23 @@
4
4
  import { SLUG } from './feature.ts';
5
5
 
6
6
  /**
7
- * First path segments the hub owns, never a prototype: the walkthroughs,
8
- * the review API, and the shared bundle.
7
+ * Never a project's or a prototype's slug: every first path segment the gate
8
+ * answers itself (the link path, the review API, the gate's font, the built
9
+ * prototypes, the shared bundle, the walkthroughs, the hub's own top files).
9
10
  */
10
- export const RESERVED = ['guide', 'api', 'assets'] as const;
11
+ export const RESERVED = [
12
+ 'invite',
13
+ 'api',
14
+ '_gate',
15
+ '_p',
16
+ 'assets',
17
+ 'guide',
18
+ 'index.html',
19
+ 'favicon.svg',
20
+ ] as const;
21
+
22
+ /** Never a client's slug: the studio's own host, and the names a host is often given. */
23
+ export const RESERVED_CLIENTS = ['studio', 'www', 'api'] as const;
11
24
 
12
25
  /**
13
26
  * Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
@@ -22,12 +35,31 @@ export const entryFile = (slug: string) => `${PROTOTYPE_DIR}/${slug}/entry.js`;
22
35
  export const isPrototypeSlug = (name: unknown): name is string =>
23
36
  typeof name === 'string' && SLUG.test(name) && !(RESERVED as readonly string[]).includes(name);
24
37
 
38
+ /** A project is a first path segment too, below its client's host, so it follows the prototype's rule. */
39
+ export const isProjectSlug = isPrototypeSlug;
40
+
41
+ /** Whether `name` can be a client: kebab-case and not one of the studio's own host names. */
42
+ export const isClientSlug = (name: unknown): name is string =>
43
+ typeof name === 'string' && SLUG.test(name) && !(RESERVED_CLIENTS as readonly string[]).includes(name);
44
+
45
+ /**
46
+ * A path segment that names one file wherever it is read: the characters a
47
+ * URL path carries as they are (RFC 3986: ASCII letters and digits,
48
+ * `-._~!$&'()*+,;=:@`), letters, marks and digits of any script, and spaces
49
+ * inside a name. Never a control character, `%`, `?`, `#`, a backslash or a slash,
50
+ * which a URL parser drops, cuts at or rewrites (`.<tab>.` becomes `..`), and
51
+ * never `.` or `..`.
52
+ */
53
+ const SEGMENT = /^(?! )[\w\p{L}\p{M}\p{N} .~!$&'()*+,;=:@-]+(?<! )$/u;
54
+ export const isSegment = (s: string): boolean => SEGMENT.test(s) && s !== '.' && s !== '..';
55
+
25
56
  /**
26
57
  * A path's segments as a file server might read them, or null when the path
27
- * could mean more than one file: it does not decode, still holds a `%` after
28
- * decoding (`%255F`), holds a backslash, a `.` or `..` segment, or an empty
29
- * segment other than the last. The gate refuses those to a Client rather than
30
- * guess how the server resolves them. An encoded space or letter decodes and passes.
58
+ * could mean more than one file: it does not decode, or a segment, decoded, is
59
+ * not `isSegment` (a `%` left after decoding, `%255F`, a backslash, a control
60
+ * character, `.`, `..`, or empty other than the last). The gate sends those
61
+ * nowhere, for anyone, rather than guess how a server resolves them. An
62
+ * encoded space or letter decodes and passes.
31
63
  */
32
64
  function segments(pathname: string): string[] | null {
33
65
  let decoded: string;
@@ -36,38 +68,115 @@ function segments(pathname: string): string[] | null {
36
68
  } catch {
37
69
  return null;
38
70
  }
39
- if (/[%\\]/.test(decoded)) return null;
40
71
  const parts = decoded.split('/').slice(1);
41
72
  if (parts.at(-1) === '') parts.pop();
42
- return parts.some((p) => p === '' || p === '.' || p === '..') ? null : parts;
73
+ return parts.every(isSegment) ? parts : null;
43
74
  }
44
75
 
45
76
  /**
46
77
  * The first path segments that are the hub's own, for anyone signed in: the
47
- * reserved routes, the gate's font folder, and the files the hub serves at
48
- * the top (shell/index.html, shell/public/favicon.svg).
78
+ * walkthroughs, the review API, the shared bundle, the gate's font folder,
79
+ * and the files the hub serves at the top (shell/index.html,
80
+ * shell/public/favicon.svg). `invite` and `_p` are reserved too but are not
81
+ * the hub's: the gate answers a link itself, and `_p` is a prototype's.
49
82
  */
50
- const HUB_ROOT: readonly string[] = [...RESERVED, '_gate', 'index.html', 'favicon.svg'];
83
+ const HUB_ROOT: readonly string[] = ['guide', 'api', 'assets', '_gate', 'index.html', 'favicon.svg'];
51
84
 
52
85
  /**
53
- * Which prototype a request path belongs to, for the gate: the name a
54
- * prototype path carries, or null for a hub path (`/`, or a first segment in
55
- * HUB_ROOT). The first segment, decoded, must be one of those, `_p`, or a
56
- * prototype slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`)
57
- * belongs to `''`, refused like an internal prototype. A path under `/_p/`
58
- * always belongs to one, so its name may be no slug at all (`all.js`), which
59
- * no audience makes client-facing. A path that could mean more than one file
60
- * belongs to `''` too.
86
+ * Which prototype a project's file belongs to, for the gate, from the path's
87
+ * segments below the project: null for a hub path (none, or a first segment
88
+ * in HUB_ROOT). The first segment must be one of those, `_p`, or a prototype
89
+ * slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`) belongs to
90
+ * `''`, refused like an internal prototype. A path under `_p/` always
91
+ * belongs to one, so its name may be no slug at all (`all.js`), which no
92
+ * audience makes client-facing.
61
93
  */
62
- export function prototypeOfPath(pathname: string): string | null {
63
- const parts = segments(pathname);
64
- if (!parts) return '';
65
- const [first, second] = parts;
94
+ function prototypeOfParts([first, second]: readonly string[]): string | null {
66
95
  if (first === undefined || HUB_ROOT.includes(first)) return null;
67
96
  if (first === PROTOTYPE_DIR) return second ?? '';
68
97
  return isPrototypeSlug(first) ? first : '';
69
98
  }
70
99
 
100
+ /** The one-time link path, matched on the path as sent, as the gate always has. */
101
+ export const LINK_PATH = /^\/invite\/([\w-]{20,100})\/?$/;
102
+ /** What the gate pages load before sign-in, at the host root: the hub's face and the favicon. */
103
+ export const GATE_FONT = '/_gate/geist.woff2';
104
+ const GATE_ASSETS: readonly string[] = [GATE_FONT, '/favicon.svg'];
105
+ /**
106
+ * The host proof `client add` asks a new client's host for: the studio gate
107
+ * answers a GET of it with the proof's answer only when the store holds that
108
+ * ask for the host's client. Otherwise it is a hub path like any other.
109
+ */
110
+ export const PROOF_PATH = /^\/_gate\/proof\/([\w-]{20,100})$/;
111
+ export const proofPath = (ask: string) => `/_gate/proof/${ask}`;
112
+ /**
113
+ * The studio site's functions, at a client host's root: the review API, and
114
+ * the files function the gate rewrites a project's files to. The gate passes
115
+ * them to anyone signed in; each checks the request again itself.
116
+ */
117
+ const FUNCTIONS: readonly string[] = ['review', 'files'];
118
+
119
+ /**
120
+ * Where a request on a studio site goes:
121
+ *
122
+ * - `none`: nothing; the gate's plain 404, whoever asks.
123
+ * - `link`: `/invite/<token>`, at the host root only.
124
+ * - `asset`: the gate's font and the favicon, served before sign-in.
125
+ * - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
126
+ * - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
127
+ * - `file`: a file of a project's build, as its segments ([] for the project's own page), and
128
+ * the prototype it belongs to (`prototypeOfParts`), read below the project.
129
+ */
130
+ export type Where =
131
+ | { kind: 'none' }
132
+ | { kind: 'link'; token: string }
133
+ | { kind: 'asset' }
134
+ | { kind: 'hub' }
135
+ | { kind: 'api'; project: string | null }
136
+ | { kind: 'file'; project: string; file: string[]; prototype: string | null };
137
+
138
+ /** Where a request goes, and the host it was sent to: lower case, no port, or '' when it is no host name. */
139
+ export type Route = { host: string } & Where;
140
+
141
+ /** A host name as a lookup key: lower case, no port, no trailing dot; '' for anything that is not one. */
142
+ export function hostName(host: string): string {
143
+ const name = host.trim().toLowerCase().replace(/:\d+$/, '').replace(/\.$/, '');
144
+ return name.length <= 253 && /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(name) ? name : '';
145
+ }
146
+
147
+ const ROOT_HUB: readonly string[] = ['assets', '_gate', 'index.html'];
148
+
149
+ /** Below a project: its review API, or one of its build's files. */
150
+ function projectRoute(project: string, rest: string[]): Where {
151
+ if (rest[0] !== 'api') return { kind: 'file', project, file: rest, prototype: prototypeOfParts(rest) };
152
+ return rest.length === 2 && rest[1] === 'review' ? { kind: 'api', project } : { kind: 'none' };
153
+ }
154
+
155
+ function where(pathname: string): Where {
156
+ const linked = pathname.match(LINK_PATH);
157
+ if (linked) return { kind: 'link', token: linked[1] };
158
+ if (GATE_ASSETS.includes(pathname)) return { kind: 'asset' };
159
+ const parts = segments(pathname);
160
+ if (!parts) return { kind: 'none' };
161
+ const [first, ...rest] = parts;
162
+ if (first === undefined || ROOT_HUB.includes(first)) return { kind: 'hub' };
163
+ if (first === 'api')
164
+ return rest.length === 1 && FUNCTIONS.includes(rest[0])
165
+ ? { kind: 'api', project: null }
166
+ : { kind: 'none' };
167
+ return isProjectSlug(first) ? projectRoute(first, rest) : { kind: 'none' };
168
+ }
169
+
170
+ /**
171
+ * The one place a studio site reads a request's path. It fails closed: a
172
+ * 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
173
+ * codename): this names the host, never the client.
174
+ */
175
+ export function routeOf(host: string, pathname: string): Route {
176
+ const name = hostName(host);
177
+ return { host: name, ...(name ? where(pathname) : { kind: 'none' }) };
178
+ }
179
+
71
180
  /**
72
181
  * Whether a built prototype's entry is there for this person, asked with a
73
182
  * HEAD before it is imported. Not there: a 404 (the gate's answer for a
@@ -6,6 +6,8 @@
6
6
 
7
7
  import { CONTROL_KINDS, type ControlKind, isControlId } from '../controls.ts';
8
8
  import { SLUG } from '../feature.ts';
9
+ import { isBuildId } from '../files.ts';
10
+ import { hostName, isClientSlug, isProjectSlug } from '../prototype.ts';
9
11
 
10
12
  /** Which side a person is on: the studio that makes the work, or the client it is for. */
11
13
  export const PERSON_KINDS = ['studio', 'client'] as const;
@@ -33,6 +35,11 @@ export const ACCESS = ['3m', '6m', 'open'] as const;
33
35
  export type Access = (typeof ACCESS)[number];
34
36
  /** The project default when none is set. */
35
37
  export const DEFAULT_ACCESS: Access = '3m';
38
+ /**
39
+ * How long a studio site's instance keeps a host's answer (server/hosts.ts):
40
+ * archiving a client, or adding one, takes effect on every instance within it.
41
+ */
42
+ export const HOST_SECONDS = 60;
36
43
 
37
44
  const ACCESS_MONTHS: Record<Exclude<Access, 'open'>, number> = { '3m': 3, '6m': 6 };
38
45
 
@@ -66,6 +73,11 @@ export type Person = {
66
73
  /** The group a person belongs to, shown beside their name: the studio, or the client's company. */
67
74
  team: string;
68
75
  kind: PersonKind;
76
+ /**
77
+ * The client a Client belongs to, or null for Studio, who works in every
78
+ * client. Never stored in the profile: it is where the person's records live.
79
+ */
80
+ client: string | null;
69
81
  /** Always null until email sign-in exists. */
70
82
  email: string | null;
71
83
  /** When their access ends (ISO time), or null for open-ended. Their sessions end with it. */
@@ -76,6 +88,55 @@ export type Person = {
76
88
  epoch: number;
77
89
  };
78
90
 
91
+ /**
92
+ * Where an op runs: one client, and one of its projects for anything about
93
+ * prototypes and comments. Every service method takes one, every store key is
94
+ * built from one (store.ts), and a Client acts only inside their own client.
95
+ */
96
+ export type Scope = { client: string; project?: string };
97
+
98
+ /**
99
+ * The one client of this machine's own store, which `design-hub dev` and
100
+ * `--local` commands use (LOCAL_SCOPE in server/local.ts). It has no record
101
+ * and is never added or archived, so ops in it need none (`allow` in
102
+ * service.ts), and no studio may add a client by this name.
103
+ */
104
+ export const LOCAL_CLIENT = 'site';
105
+
106
+ /** A slug a studio's client can have: a client slug, and never the local store's. */
107
+ export const isStudioClient = (slug: unknown): slug is string => isClientSlug(slug) && slug !== LOCAL_CLIENT;
108
+ /** What a slug that cannot be a studio's client is told. */
109
+ export const STUDIO_CLIENT_SLUG = 'slug: kebab-case, and not studio, www, api or site';
110
+
111
+ /**
112
+ * A client of the studio: one host (`<client>.<apex>`, or a codename below
113
+ * the apex), every project of it, its Client people. An archived client's
114
+ * host is the plain 404. `access` is its default access length, unused until
115
+ * clients set one. `provenAt` is when its host proved it is this studio's
116
+ * site (endProof in service.ts); until then the client is still being added
117
+ * (isAdded).
118
+ */
119
+ export type Client = {
120
+ slug: string;
121
+ host: string;
122
+ name: string;
123
+ access: Access | null;
124
+ createdAt: string;
125
+ archivedAt: string | null;
126
+ provenAt: string | null;
127
+ };
128
+
129
+ /**
130
+ * Whether a client has finished being added: its host has proved it is this
131
+ * studio's site. A record with no such mark is still being added, whatever
132
+ * became of its proof, so nothing of a project and no sign-in link goes to
133
+ * its host, and `client add` with the same flags finishes it.
134
+ */
135
+ export const isAdded = (client: Client): boolean => client.provenAt !== null;
136
+
137
+ /** A project of one client, served at `/<slug>/` on its host from the build `build` points at, or not at all while it is null. */
138
+ export type Project = { slug: string; client: string; name: string; build: string | null; createdAt: string };
139
+
79
140
  /** A one-time sign-in link, stored under the hash of its token and deleted when used. */
80
141
  export type Link = { person: string; expiresAt: string };
81
142
 
@@ -163,8 +224,11 @@ const PROFILE_RULES: [string, (v: unknown) => boolean][] = [
163
224
  ['createdAt', isText],
164
225
  ];
165
226
 
166
- /** Whether a stored profile (a person without revocation and epoch) is well formed. */
167
- export function isProfile(v: unknown): v is Omit<Person, 'revokedAt' | 'epoch'> {
227
+ /** What a person's profile record holds: everything but revocation, epoch and client, which live elsewhere. */
228
+ export type Profile = Omit<Person, 'revokedAt' | 'epoch' | 'client'>;
229
+
230
+ /** Whether a stored profile is well formed. */
231
+ export function isProfile(v: unknown): v is Profile {
168
232
  if (!v || typeof v !== 'object' || Array.isArray(v)) return false;
169
233
  const o = v as Record<string, unknown>;
170
234
  return PROFILE_RULES.every(([key, ok]) => ok(o[key]));
@@ -227,6 +291,59 @@ export function checkPersonInput(input: unknown): Checked<PersonInput> {
227
291
  : { ok: true, value: { name, team, kind: kind as PersonKind, access: access as Access | null } };
228
292
  }
229
293
 
294
+ export type ClientInput = Pick<Client, 'slug' | 'host' | 'name'>;
295
+
296
+ /** A new client: a client slug, the host name it is served on (lower case), and its name. */
297
+ export function checkClientInput(input: unknown): Checked<ClientInput> {
298
+ const i = (input ?? {}) as Record<string, unknown>;
299
+ const problems: string[] = [];
300
+ const name = text(i.name, 'name', LIMITS.name, problems);
301
+ if (!isStudioClient(i.slug)) problems.push(STUDIO_CLIENT_SLUG);
302
+ const host = typeof i.host === 'string' ? hostName(i.host) : '';
303
+ if (!host || host !== i.host) problems.push('host: a host name in lower case, such as acme.example.com');
304
+ return problems.length
305
+ ? { ok: false, problems }
306
+ : { ok: true, value: { slug: i.slug as string, host, name } };
307
+ }
308
+
309
+ export type ProjectInput = Pick<Project, 'slug' | 'name'>;
310
+
311
+ /** A new project: a project slug (never a path the gate answers itself) and its name. */
312
+ export function checkProjectInput(input: unknown): Checked<ProjectInput> {
313
+ const i = (input ?? {}) as Record<string, unknown>;
314
+ const problems: string[] = [];
315
+ const name = text(i.name, 'name', LIMITS.name, problems);
316
+ if (!isProjectSlug(i.slug)) problems.push('slug: kebab-case, and not a path the hub answers itself');
317
+ return problems.length ? { ok: false, problems } : { ok: true, value: { slug: i.slug as string, name } };
318
+ }
319
+
320
+ const isRecord = (v: unknown): v is Record<string, unknown> =>
321
+ !!v && typeof v === 'object' && !Array.isArray(v);
322
+
323
+ /** A stored client, or null for anything that is not a well-formed one. */
324
+ export function readClient(json: string | null): Client | null {
325
+ const c = safeJson(json);
326
+ if (!isRecord(c) || !isClientSlug(c.slug) || typeof c.host !== 'string' || !isText(c.name)) return null;
327
+ if (!isText(c.createdAt) || !isTextOrNull(c.archivedAt) || !isTextOrNull(c.provenAt)) return null;
328
+ return c as Client;
329
+ }
330
+
331
+ /** A stored project, or null for anything that is not a well-formed one. */
332
+ export function readProject(json: string | null): Project | null {
333
+ const p = safeJson(json);
334
+ if (!isRecord(p) || !isProjectSlug(p.slug) || !isClientSlug(p.client) || !isText(p.name)) return null;
335
+ if (!isText(p.createdAt) || !(p.build === null || isBuildId(p.build))) return null;
336
+ return p as Project;
337
+ }
338
+
339
+ function safeJson(json: string | null): unknown {
340
+ try {
341
+ return json === null ? null : JSON.parse(json);
342
+ } catch {
343
+ return null;
344
+ }
345
+ }
346
+
230
347
  /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
231
348
  export function checkAudience(input: unknown): Checked<Audience | null> {
232
349
  if (input === undefined || input === null) return { ok: true, value: null };
@@ -1,13 +1,14 @@
1
1
  import assert from 'node:assert/strict';
2
2
  import { test } from 'node:test';
3
- import type { Audience, Comment, Person, PersonKind } from './model.ts';
4
- import { type Action, audienceFor, can, viewOf, visibleTo } from './policy.ts';
3
+ import type { Audience, Comment, Person, PersonKind, Scope } from './model.ts';
4
+ import { type Action, audienceFor, can, inScope, viewOf, visibleTo } from './policy.ts';
5
5
 
6
6
  const person = (id: string, kind: PersonKind, revokedAt: string | null = null): Person => ({
7
7
  id,
8
8
  name: id,
9
9
  team: kind === 'studio' ? 'Studio' : 'Acme',
10
10
  kind,
11
+ client: kind === 'studio' ? null : 'acme',
11
12
  email: null,
12
13
  accessEndsAt: null,
13
14
  createdAt: '',
@@ -50,57 +51,113 @@ const TABLE: [Action, Audience, boolean, boolean, boolean, boolean][] = [
50
51
  ['people', 'internal', true, true, false, false],
51
52
  ['audience', 'client', true, true, false, false],
52
53
  ['audience', 'internal', true, true, false, false],
54
+ ['setup', 'client', true, true, false, false],
55
+ ['setup', 'internal', true, true, false, false],
53
56
  ];
54
57
 
58
+ /** Where the table runs: the Client's own client. */
59
+ const A: Scope = { client: 'acme', project: 'rebrand' };
60
+ /** Another client's: the service answers anything here as not found. */
61
+ const B: Scope = { client: 'bravo', project: 'launch' };
62
+
55
63
  const studio = person('s', 'studio');
56
64
  const client = person('c', 'client');
57
65
 
58
66
  for (const [action, audience, studioOwn, studioOther, clientOwn, clientOther] of TABLE) {
59
67
  test(`${action} on a ${audience} thread: Studio and Client, own and another's`, () => {
60
- assert.equal(can(studio, action, thread(studio.id, audience)), studioOwn, 'Studio, own');
61
- assert.equal(can(studio, action, thread('someone', audience)), studioOther, "Studio, another's");
62
- assert.equal(can(client, action, thread(client.id, audience)), clientOwn, 'Client, own');
63
- assert.equal(can(client, action, thread('someone', audience)), clientOther, "Client, another's");
68
+ assert.equal(can(studio, A, action, thread(studio.id, audience)), studioOwn, 'Studio, own');
69
+ assert.equal(can(studio, A, action, thread('someone', audience)), studioOther, "Studio, another's");
70
+ assert.equal(can(client, A, action, thread(client.id, audience)), clientOwn, 'Client, own');
71
+ assert.equal(can(client, A, action, thread('someone', audience)), clientOther, "Client, another's");
72
+ });
73
+
74
+ test(`${action} on a ${audience} thread in another client’s scope: Studio as anywhere, a Client never`, () => {
75
+ assert.equal(can(studio, B, action, thread(studio.id, audience)), studioOwn, 'Studio, own');
76
+ assert.equal(can(studio, B, action, thread('someone', audience)), studioOther, "Studio, another's");
77
+ assert.equal(can(client, B, action, thread(client.id, audience)), false, 'Client, own');
78
+ assert.equal(can(client, B, action, thread('someone', audience)), false, "Client, another's");
79
+ assert.equal(can(client, B, action), false, 'Client, no thread');
64
80
  });
65
81
 
66
82
  test(`${action} on a ${audience} thread: nobody signed in and a revoked person may not`, () => {
67
83
  for (const kind of ['studio', 'client'] as const) {
68
84
  const gone = person('r', kind, '2030-01-01T00:00:00Z');
69
- assert.equal(can(gone, action, thread(gone.id, audience)), false, `revoked ${kind}, own`);
70
- assert.equal(can(gone, action, thread('someone', audience)), false, `revoked ${kind}, another's`);
85
+ assert.equal(can(gone, A, action, thread(gone.id, audience)), false, `revoked ${kind}, own`);
86
+ assert.equal(can(gone, A, action, thread('someone', audience)), false, `revoked ${kind}, another's`);
71
87
  }
72
- assert.equal(can(null, action, thread('someone', audience)), false, 'null');
73
- assert.equal(can(null, action), false, 'null, no subject');
88
+ assert.equal(can(null, A, action, thread('someone', audience)), false, 'null');
89
+ assert.equal(can(null, A, action), false, 'null, no subject');
74
90
  });
75
91
  }
76
92
 
93
+ // The scope rows: who an op in a scope belongs to at all.
94
+ const SCOPES: [string, Pick<Person, 'kind' | 'client'>, Scope, boolean][] = [
95
+ ['Studio, in a client', { kind: 'studio', client: null }, A, true],
96
+ ['Studio, in another client', { kind: 'studio', client: null }, B, true],
97
+ ['Studio, in a client with no project', { kind: 'studio', client: null }, { client: 'bravo' }, true],
98
+ ['a Client, in their own client', { kind: 'client', client: 'acme' }, A, true],
99
+ [
100
+ 'a Client, in another project of it',
101
+ { kind: 'client', client: 'acme' },
102
+ { client: 'acme', project: 'x' },
103
+ true,
104
+ ],
105
+ ['a Client, in another client', { kind: 'client', client: 'acme' }, B, false],
106
+ ['a Client, in a client spelled otherwise', { kind: 'client', client: 'acme' }, { client: 'Acme' }, false],
107
+ ['Studio that names a client', { kind: 'studio', client: 'acme' }, A, false],
108
+ ['a Client with no client', { kind: 'client', client: null }, A, false],
109
+ // A scope that names no client is nobody's, a Client who names none included.
110
+ ['Studio, in a scope with an empty client', { kind: 'studio', client: null }, { client: '' }, false],
111
+ [
112
+ 'a Client with no client, in a scope with none',
113
+ { kind: 'client', client: undefined as unknown as null },
114
+ {} as Scope,
115
+ false,
116
+ ],
117
+ [
118
+ 'a Client with a null client, in a scope with a null one',
119
+ { kind: 'client', client: null },
120
+ { client: null } as unknown as Scope,
121
+ false,
122
+ ],
123
+ ];
124
+
125
+ for (const [who, actor, scope, inside] of SCOPES)
126
+ test(`inScope: ${who} is ${inside ? 'in' : 'out of'} scope`, () => {
127
+ assert.equal(inScope(actor, scope), inside);
128
+ // can() asks inScope first, so out of scope nothing is allowed, whatever the action.
129
+ if (!inside)
130
+ for (const action of ['read', 'comment', 'resolve', 'people'] as const)
131
+ assert.equal(can({ ...studio, ...actor }, scope, action), false, action);
132
+ });
133
+
77
134
  test('with no thread, both kinds read and start one; only Studio triages, manages people and sets a prototype’s audience', () => {
78
135
  for (const action of ['read', 'comment'] as const) {
79
- assert.equal(can(studio, action), true);
80
- assert.equal(can(client, action), true);
136
+ assert.equal(can(studio, A, action), true);
137
+ assert.equal(can(client, A, action), true);
81
138
  }
82
139
  for (const action of ['triage', 'people', 'audience'] as const) {
83
- assert.equal(can(studio, action), true);
84
- assert.equal(can(client, action), false);
140
+ assert.equal(can(studio, A, action), true);
141
+ assert.equal(can(client, A, action), false);
85
142
  }
86
143
  });
87
144
 
88
145
  test('a reply is judged by its own author, and inside its thread’s audience', () => {
89
146
  const root = thread(studio.id, 'client');
90
147
  const mine = comment('r', 'c', client.id, 'client');
91
- assert.equal(can(client, 'edit', root, mine), true);
92
- assert.equal(can(client, 'delete', root, mine), true);
93
- assert.equal(can(studio, 'edit', root, mine), false);
94
- assert.equal(can(studio, 'delete', root, mine), true);
148
+ assert.equal(can(client, A, 'edit', root, mine), true);
149
+ assert.equal(can(client, A, 'delete', root, mine), true);
150
+ assert.equal(can(studio, A, 'edit', root, mine), false);
151
+ assert.equal(can(studio, A, 'delete', root, mine), true);
95
152
  // A client-facing reply in an internal thread, or an internal reply in a client-facing one: never a Client's.
96
- assert.equal(can(client, 'edit', thread(studio.id, 'internal'), mine), false);
97
- assert.equal(can(client, 'read', root, comment('r', 'c', client.id, 'internal')), false);
153
+ assert.equal(can(client, A, 'edit', thread(studio.id, 'internal'), mine), false);
154
+ assert.equal(can(client, A, 'read', root, comment('r', 'c', client.id, 'internal')), false);
98
155
  });
99
156
 
100
157
  test('a record without an audience is internal to the rules', () => {
101
158
  const legacy = { ...thread(client.id), audience: undefined } as unknown as Comment;
102
- assert.equal(can(client, 'read', legacy), false);
103
- assert.equal(can(studio, 'read', legacy), true);
159
+ assert.equal(can(client, A, 'read', legacy), false);
160
+ assert.equal(can(studio, A, 'read', legacy), true);
104
161
  assert.deepEqual(visibleTo('client', [legacy]), []);
105
162
  });
106
163
 
@@ -1,17 +1,38 @@
1
1
  // Who may do what. Studio runs the review: it adds and revokes people,
2
- // deletes any comment, corrects triage and sets which prototypes are
3
- // client-facing. A Client comments, replies, edits
2
+ // deletes any comment, corrects triage, sets which prototypes are
3
+ // client-facing, and sets up clients, their projects and their builds. A Client comments, replies, edits
4
4
  // and deletes what they wrote, and resolves or reopens any client-facing
5
5
  // thread, because clients work alongside others in their own organisation.
6
6
  // An internal thread does not exist for a Client: every action on one is
7
7
  // refused. A revoked person, or a visitor who is not signed in, may do
8
- // nothing here.
9
- import type { Audience, Comment, Person, PersonKind } from './model.ts';
8
+ // nothing here. Nor may anyone outside the op's scope: a Client works only in
9
+ // their own client, and the service answers anything outside it as not found.
10
+ import type { Audience, Comment, Person, PersonKind, Scope } from './model.ts';
10
11
 
11
- export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people' | 'audience';
12
+ export type Action =
13
+ | 'read'
14
+ | 'comment'
15
+ | 'edit'
16
+ | 'delete'
17
+ | 'resolve'
18
+ | 'triage'
19
+ | 'people'
20
+ | 'audience'
21
+ | 'setup';
12
22
 
13
23
  /** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
14
- type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
24
+ type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt' | 'client'>;
25
+
26
+ /**
27
+ * Whether an op in `scope` is the actor's at all: Studio works in every
28
+ * client, a Client only in their own. A person whose kind and client
29
+ * disagree (Studio with a client, a Client with none) is in no scope, and
30
+ * so is every op on a scope that names no client: it fails closed here.
31
+ */
32
+ export function inScope(actor: Pick<Person, 'kind' | 'client'>, scope: Scope): boolean {
33
+ if (typeof scope.client !== 'string' || scope.client === '') return false;
34
+ return actor.kind === 'studio' ? actor.client === null : actor.client === scope.client;
35
+ }
15
36
 
16
37
  /** Whether a comment reaches a Client. Only the exact value counts, so a record without one stays inside. */
17
38
  const clientFacing = (c: Pick<Comment, 'audience'>) => c.audience === 'client';
@@ -21,8 +42,14 @@ const clientFacing = (c: Pick<Comment, 'audience'>) => c.audience === 'client';
21
42
  * reply (edit or delete one); without `subject`, the root is the subject.
22
43
  * Without a root, the action is on no thread: listing, or a new thread.
23
44
  */
24
- export function can(actor: Actor | null, action: Action, root?: Comment, subject?: Comment): boolean {
25
- if (!actor || actor.revokedAt) return false;
45
+ export function can(
46
+ actor: Actor | null,
47
+ scope: Scope,
48
+ action: Action,
49
+ root?: Comment,
50
+ subject?: Comment,
51
+ ): boolean {
52
+ if (!actor || actor.revokedAt || !inScope(actor, scope)) return false;
26
53
  const studio = actor.kind === 'studio';
27
54
  if (!studio && [root, subject].some((c) => c && !clientFacing(c))) return false;
28
55
  const own = (subject ?? root)?.author.id === actor.id;
@@ -38,6 +65,7 @@ export function can(actor: Actor | null, action: Action, root?: Comment, subject
38
65
  case 'triage':
39
66
  case 'people':
40
67
  case 'audience':
68
+ case 'setup':
41
69
  return studio;
42
70
  }
43
71
  }