@milesroxas/design-hub 0.9.0 → 0.11.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 (150) hide show
  1. package/README.md +13 -11
  2. package/core/controls.test.ts +176 -0
  3. package/core/controls.ts +195 -0
  4. package/core/files.test.ts +76 -0
  5. package/core/files.ts +131 -0
  6. package/core/prototype.test.ts +124 -7
  7. package/core/prototype.ts +133 -24
  8. package/core/review/model.ts +144 -2
  9. package/core/review/policy.test.ts +79 -22
  10. package/core/review/policy.ts +36 -8
  11. package/core/review/service.test.ts +1525 -322
  12. package/core/review/service.ts +810 -183
  13. package/core/review/store.test.ts +91 -0
  14. package/core/review/store.ts +143 -26
  15. package/dist/core/controls.d.ts +55 -0
  16. package/dist/core/controls.js +174 -0
  17. package/dist/core/files.d.ts +39 -0
  18. package/dist/core/files.js +107 -0
  19. package/dist/core/prototype.d.ts +61 -12
  20. package/dist/core/prototype.js +105 -26
  21. package/dist/core/review/model.d.ts +87 -2
  22. package/dist/core/review/model.js +97 -1
  23. package/dist/core/review/policy.d.ts +11 -4
  24. package/dist/core/review/policy.js +14 -2
  25. package/dist/core/review/service.d.ts +222 -43
  26. package/dist/core/review/service.js +727 -182
  27. package/dist/core/review/store.d.ts +84 -24
  28. package/dist/core/review/store.js +66 -26
  29. package/dist/scripts/bootstrap.d.ts +4 -34
  30. package/dist/scripts/bootstrap.js +9 -244
  31. package/dist/scripts/cli.js +6 -0
  32. package/dist/scripts/client.d.ts +45 -0
  33. package/dist/scripts/client.js +320 -0
  34. package/dist/scripts/commands.js +6 -0
  35. package/dist/scripts/comments.d.ts +8 -26
  36. package/dist/scripts/comments.js +27 -14
  37. package/dist/scripts/guard.d.ts +11 -0
  38. package/dist/scripts/guard.js +44 -0
  39. package/dist/scripts/lib/builds.d.ts +40 -0
  40. package/dist/scripts/lib/builds.js +198 -0
  41. package/dist/scripts/lib/cli.js +3 -1
  42. package/dist/scripts/lib/deploy.d.ts +6 -90
  43. package/dist/scripts/lib/deploy.js +24 -137
  44. package/dist/scripts/lib/dns.d.ts +49 -0
  45. package/dist/scripts/lib/dns.js +141 -0
  46. package/dist/scripts/lib/fake-host.d.ts +168 -0
  47. package/dist/scripts/lib/fake-host.js +244 -0
  48. package/dist/scripts/lib/host.d.ts +87 -0
  49. package/dist/scripts/lib/host.js +362 -0
  50. package/dist/scripts/lib/linked.d.ts +51 -0
  51. package/dist/scripts/lib/linked.js +124 -0
  52. package/dist/scripts/lib/names.d.ts +35 -0
  53. package/dist/scripts/lib/names.js +77 -0
  54. package/dist/scripts/lib/review.d.ts +23 -2
  55. package/dist/scripts/lib/review.js +32 -15
  56. package/dist/scripts/lib/rules.d.ts +1 -0
  57. package/dist/scripts/lib/rules.js +1 -0
  58. package/dist/scripts/lib/studio-deps.d.ts +35 -0
  59. package/dist/scripts/lib/studio-deps.js +79 -0
  60. package/dist/scripts/lib/studios.d.ts +70 -0
  61. package/dist/scripts/lib/studios.js +187 -0
  62. package/dist/scripts/lib/update.d.ts +33 -0
  63. package/dist/scripts/lib/update.js +40 -0
  64. package/dist/scripts/lib/vercel.d.ts +1 -23
  65. package/dist/scripts/lib/vercel.js +6 -81
  66. package/dist/scripts/people.d.ts +7 -11
  67. package/dist/scripts/people.js +51 -46
  68. package/dist/scripts/preflight.d.ts +8 -1
  69. package/dist/scripts/preflight.js +36 -19
  70. package/dist/scripts/project.d.ts +86 -0
  71. package/dist/scripts/project.js +141 -0
  72. package/dist/scripts/share.d.ts +105 -30
  73. package/dist/scripts/share.js +82 -154
  74. package/dist/scripts/skills.d.ts +18 -2
  75. package/dist/scripts/skills.js +22 -3
  76. package/dist/scripts/studio.d.ts +75 -0
  77. package/dist/scripts/studio.js +401 -0
  78. package/dist/scripts/upgrade.d.ts +10 -2
  79. package/dist/scripts/upgrade.js +44 -14
  80. package/dist/server/api.d.ts +22 -11
  81. package/dist/server/api.js +125 -44
  82. package/dist/server/blob.d.ts +10 -0
  83. package/dist/server/blob.js +53 -0
  84. package/dist/server/checks.js +2 -2
  85. package/dist/server/dev.js +4 -4
  86. package/dist/server/env.d.ts +2 -0
  87. package/dist/server/env.js +9 -0
  88. package/dist/server/fake-upstash.js +52 -3
  89. package/dist/server/hosts.d.ts +11 -0
  90. package/dist/server/hosts.js +31 -0
  91. package/dist/server/local.d.ts +7 -1
  92. package/dist/server/local.js +10 -1
  93. package/dist/server/mark.d.ts +41 -0
  94. package/dist/server/mark.js +111 -0
  95. package/dist/server/session.d.ts +17 -9
  96. package/dist/server/session.js +22 -45
  97. package/dist/server/upstash.js +20 -0
  98. package/dist/vercel/files.d.ts +11 -0
  99. package/dist/vercel/files.js +68 -0
  100. package/dist/vercel/index.d.ts +4 -3
  101. package/dist/vercel/index.js +12 -8
  102. package/dist/vercel/proxy.d.ts +69 -4
  103. package/dist/vercel/proxy.js +171 -107
  104. package/dist/vite/plugin.d.ts +24 -1
  105. package/dist/vite/plugin.js +46 -2
  106. package/kit/Artboard.tsx +21 -8
  107. package/kit/Space.tsx +4 -3
  108. package/kit/Variants.tsx +193 -65
  109. package/kit/ViewTools.tsx +77 -11
  110. package/kit/audience.ts +85 -0
  111. package/kit/dials.css +59 -0
  112. package/kit/dials.tsx +272 -35
  113. package/kit/harness.css +34 -0
  114. package/kit/prefs.ts +23 -1
  115. package/package.json +3 -2
  116. package/shell/ClientViewNote.tsx +31 -3
  117. package/shell/Guide.tsx +11 -5
  118. package/shell/Index.tsx +32 -10
  119. package/shell/Notice.tsx +1 -1
  120. package/shell/controls.ts +26 -0
  121. package/shell/hub.ts +5 -1
  122. package/shell/main.tsx +33 -6
  123. package/shell/review/Review.tsx +2 -2
  124. package/shell/review/ThreadCard.tsx +5 -5
  125. package/shell/review/client.ts +28 -11
  126. package/shell/review/view.test.ts +19 -0
  127. package/shell/review/view.ts +25 -0
  128. package/shell/shell.css +8 -0
  129. package/shell/studio/ClientHub.tsx +98 -0
  130. package/shell/studio/hub.css +48 -0
  131. package/shell/studio/index.html +14 -0
  132. package/shell/studio/main.tsx +17 -0
  133. package/skills/miles-brand/SKILL.md +11 -1
  134. package/skills/miles-deploy/SKILL.md +121 -64
  135. package/skills/miles-design-hub/SKILL.md +11 -9
  136. package/skills/miles-prototype/SKILL.md +34 -1
  137. package/skills/miles-review/SKILL.md +5 -7
  138. package/skills/miles-worktrees/SKILL.md +3 -3
  139. package/templates/conductor/settings.toml +1 -1
  140. package/templates/studio/.env.example +9 -0
  141. package/templates/studio/.vercelignore +5 -0
  142. package/templates/studio/README.md +84 -0
  143. package/templates/studio/_gitignore +7 -0
  144. package/templates/studio/api/files.ts +4 -0
  145. package/templates/studio/api/review.ts +4 -0
  146. package/templates/studio/build.mjs +4 -0
  147. package/templates/studio/package.json +16 -0
  148. package/templates/studio/proxy.ts +4 -0
  149. package/templates/studio/studio.json +9 -0
  150. package/templates/studio/vercel.json +27 -0
package/core/files.ts ADDED
@@ -0,0 +1,131 @@
1
+ // The files port: where a studio keeps every project's builds. A build is
2
+ // uploaded under `c/<client>/<project>/<build>/…`, and the studio site serves
3
+ // the one the project's record points at (`Project.build`). Production is a
4
+ // private Vercel Blob store (server/blob.ts); tests use the in-memory store
5
+ // below. Every path is built here, from checked parts, so no request can name
6
+ // a file outside its own client and project.
7
+ import { isClientSlug, isProjectSlug, isSegment } from './prototype.ts';
8
+ import type { Scope } from './review/model.ts';
9
+
10
+ /** A file's bytes as the store hands them, and the type it was stored with. */
11
+ export type StoredFile = { body: ReadableStream<Uint8Array> | Bytes; type: string };
12
+ type Bytes = Uint8Array<ArrayBuffer>;
13
+
14
+ export interface Files {
15
+ /** Writes a file that is not there yet. A build's paths are never overwritten. */
16
+ put(path: string, body: Bytes, type: string): Promise<void>;
17
+ /** The file and its type, or null when there is none. */
18
+ get(path: string): Promise<StoredFile | null>;
19
+ /** Every path that starts with `prefix`. A scope's prefix ends in `/`, so `c/acme/` never lists `c/acme-co/`. */
20
+ list(prefix: string): Promise<string[]>;
21
+ /** Removes these paths; one that is not there is no error. */
22
+ remove(paths: string[]): Promise<void>;
23
+ }
24
+
25
+ /**
26
+ * A build's id: random, lower-case letters and digits, and never used twice
27
+ * for the life of the store, so a path never comes back with other bytes.
28
+ */
29
+ const BUILD_ID = /^[a-z0-9]{16,64}$/;
30
+ export const isBuildId = (v: unknown): v is string => typeof v === 'string' && BUILD_ID.test(v);
31
+
32
+ function checked(ok: boolean, what: string, v: unknown): void {
33
+ // A bad part is a bug in the caller, never input to answer: fail closed before any store call.
34
+ if (!ok) throw new Error(`Not a file path: ${what} is ${JSON.stringify(v)}.`);
35
+ }
36
+
37
+ /** A file inside a build, as a list of path segments, each one `isSegment`: the rule the studio gate reads a path by. */
38
+ const isFile = (segments: readonly string[]) => segments.length > 0 && segments.every(isSegment);
39
+
40
+ /**
41
+ * Removes every file under a prefix, in the store's batches of 1,000, and
42
+ * says how many there were. Takes a prefix one of the functions below built,
43
+ * so it never reaches past its scope.
44
+ */
45
+ export async function clearPrefix(files: Files, prefix: string): Promise<number> {
46
+ const paths = await files.list(prefix);
47
+ await removeAll(files, paths);
48
+ return paths.length;
49
+ }
50
+
51
+ /** Removes these paths, in the store's batches of 1,000. */
52
+ export async function removeAll(files: Files, paths: string[]): Promise<void> {
53
+ for (let i = 0; i < paths.length; i += 1000) await files.remove(paths.slice(i, i + 1000));
54
+ }
55
+
56
+ /** Every file of one client, every project and build: what archiving the client removes. Ends in `/`. */
57
+ export function clientPrefix(client: string): string {
58
+ checked(isClientSlug(client), 'client', client);
59
+ return `c/${client}/`;
60
+ }
61
+
62
+ /** Every build of one project, ending in `/`, so `c/acme/rebrand/` never lists `c/acme/rebrand-2/`. */
63
+ export function projectPrefix(scope: Scope): string {
64
+ checked(isClientSlug(scope.client), 'client', scope.client);
65
+ checked(isProjectSlug(scope.project), 'project', scope.project);
66
+ return `c/${scope.client}/${scope.project}/`;
67
+ }
68
+
69
+ /** The folder of one build of a project, ending in `/`. */
70
+ export function buildPrefix(scope: Scope, build: string): string {
71
+ const prefix = projectPrefix(scope);
72
+ checked(isBuildId(build), 'build', build);
73
+ return `${prefix}${build}/`;
74
+ }
75
+
76
+ /** One file of one build: `c/<client>/<project>/<build>/<file>`, with `file` given as its segments. */
77
+ export function filePath(scope: Scope, build: string, file: readonly string[]): string {
78
+ checked(isFile(file), 'file', file);
79
+ return `${buildPrefix(scope, build)}${file.join('/')}`;
80
+ }
81
+
82
+ const TYPES: Record<string, string> = {
83
+ html: 'text/html; charset=utf-8',
84
+ js: 'text/javascript; charset=utf-8',
85
+ mjs: 'text/javascript; charset=utf-8',
86
+ css: 'text/css; charset=utf-8',
87
+ json: 'application/json',
88
+ webmanifest: 'application/manifest+json',
89
+ map: 'application/json',
90
+ md: 'text/markdown; charset=utf-8',
91
+ txt: 'text/plain; charset=utf-8',
92
+ xml: 'application/xml',
93
+ pdf: 'application/pdf',
94
+ svg: 'image/svg+xml',
95
+ png: 'image/png',
96
+ jpg: 'image/jpeg',
97
+ jpeg: 'image/jpeg',
98
+ gif: 'image/gif',
99
+ webp: 'image/webp',
100
+ avif: 'image/avif',
101
+ ico: 'image/x-icon',
102
+ woff2: 'font/woff2',
103
+ woff: 'font/woff',
104
+ ttf: 'font/ttf',
105
+ otf: 'font/otf',
106
+ mp4: 'video/mp4',
107
+ webm: 'video/webm',
108
+ wasm: 'application/wasm',
109
+ };
110
+
111
+ /** The type a built file is stored with, from its extension. */
112
+ export function typeOf(path: string): string {
113
+ const ext = path.match(/\.([a-z0-9]+)$/i)?.[1]?.toLowerCase() ?? '';
114
+ return Object.hasOwn(TYPES, ext) ? TYPES[ext] : 'application/octet-stream';
115
+ }
116
+
117
+ /** The files port in memory, for tests and for a studio run on one machine. */
118
+ export function memoryFiles(): Files {
119
+ const files = new Map<string, StoredFile>();
120
+ return {
121
+ put: async (path, body, type) => {
122
+ if (files.has(path)) throw new Error(`${path} exists; a build's paths are never overwritten.`);
123
+ files.set(path, { body, type });
124
+ },
125
+ get: async (path) => files.get(path) ?? null,
126
+ list: async (prefix) => [...files.keys()].filter((p) => p.startsWith(prefix)).sort(),
127
+ remove: async (paths) => {
128
+ for (const p of paths) files.delete(p);
129
+ },
130
+ };
131
+ }
@@ -1,18 +1,45 @@
1
1
  import assert from 'node:assert/strict';
2
2
  import { test } from 'node:test';
3
- import { ALL_FILE, entryFile, isPrototypeSlug, probeEntry, prototypeOfPath, RESERVED } from './prototype.ts';
3
+ import {
4
+ ALL_FILE,
5
+ entryFile,
6
+ hostName,
7
+ isClientSlug,
8
+ isProjectSlug,
9
+ isPrototypeSlug,
10
+ probeEntry,
11
+ RESERVED,
12
+ RESERVED_CLIENTS,
13
+ routeOf,
14
+ } from './prototype.ts';
4
15
 
5
- test('a prototype slug is kebab-case and never one of the hub’s own paths', () => {
6
- assert.deepEqual([...RESERVED], ['guide', 'api', 'assets']);
7
- for (const reserved of RESERVED) assert.equal(isPrototypeSlug(reserved), false);
16
+ test('a prototype or project slug is kebab-case and never a first segment the gate answers', () => {
17
+ assert.deepEqual(
18
+ [...RESERVED],
19
+ ['invite', 'api', '_gate', '_p', 'assets', 'guide', 'index.html', 'favicon.svg'],
20
+ );
21
+ for (const reserved of RESERVED) {
22
+ assert.equal(isPrototypeSlug(reserved), false, reserved);
23
+ assert.equal(isProjectSlug(reserved), false, reserved);
24
+ }
8
25
  assert.equal(isPrototypeSlug('sample-list'), true);
26
+ assert.equal(isProjectSlug('rebrand'), true);
9
27
  for (const bad of ['_p', 'Sample', 'a_b', '', 'a.js', 'a/b', null, 3])
10
28
  assert.equal(isPrototypeSlug(bad), false);
11
29
  assert.equal(entryFile('sample-list'), '_p/sample-list/entry.js');
12
30
  assert.equal(ALL_FILE, '_p/all.js');
13
31
  });
14
32
 
15
- test('a path names its prototype, or none for the hub’s own', () => {
33
+ test('a client slug is kebab-case and never the studio’s own host names', () => {
34
+ assert.deepEqual([...RESERVED_CLIENTS], ['studio', 'www', 'api']);
35
+ for (const reserved of RESERVED_CLIENTS) assert.equal(isClientSlug(reserved), false, reserved);
36
+ assert.equal(isClientSlug('acme'), true);
37
+ // A prototype's reserved names are no concern of a host's.
38
+ assert.equal(isClientSlug('guide'), true);
39
+ for (const bad of ['Acme', 'a_b', 'a:b', '', 'a.b', null, 3]) assert.equal(isClientSlug(bad), false);
40
+ });
41
+
42
+ test("below a project, a path names its prototype, null for the hub’s own, or '' for nothing a Client may have", () => {
16
43
  const cases: [string, string | null][] = [
17
44
  ['/', null],
18
45
  ['/assets/index-abc.js', null],
@@ -22,7 +49,8 @@ test('a path names its prototype, or none for the hub’s own', () => {
22
49
  ['/_gate/geist.woff2', null],
23
50
  ['/index.html', null],
24
51
  ['/robots.txt', ''],
25
- ['/invite/short', 'invite'],
52
+ // A link path the gate does not answer is no prototype's and no hub path: refused to a Client.
53
+ ['/invite/short', ''],
26
54
  ['/Sample-List/', ''],
27
55
  ['/_p;x/secret/entry.js', ''],
28
56
  ['/_p./secret/entry.js', ''],
@@ -60,7 +88,7 @@ test('a path names its prototype, or none for the hub’s own', () => {
60
88
  ['/%73ample-list/', 'sample-list'],
61
89
  ['/%E0%A4%A/', ''],
62
90
  ];
63
- for (const [path, owner] of cases) assert.equal(prototypeOfPath(path), owner, path);
91
+ for (const [path, owner] of cases) assert.equal(ownerBelow(path), owner, path);
64
92
  });
65
93
 
66
94
  test('an entry is there on a JavaScript 200, not there on a 404 or the host’s page, and an error otherwise', async () => {
@@ -81,3 +109,92 @@ test('an entry is there on a JavaScript 200, not there on a 404 or the host’s
81
109
  await assert.rejects(probeEntry('/_p/a/entry.js', answer(status)), new RegExp(`answered ${status}`));
82
110
  assert.deepEqual(asked[0], ['/_p/a/entry.js', { method: 'HEAD', credentials: 'same-origin' }]);
83
111
  });
112
+
113
+ test('a host is a lookup key: lower case, no port, no trailing dot, or none at all', () => {
114
+ assert.equal(hostName('Acme.Studio.test:443'), 'acme.studio.test');
115
+ assert.equal(hostName('acme.studio.test.'), 'acme.studio.test');
116
+ for (const host of ['', 'localhost', 'a b.test', 'acme..test', 'acme.test/x', `${'a'.repeat(250)}.test`])
117
+ assert.equal(hostName(host), '', host);
118
+ });
119
+
120
+ const H = 'acme.studio.test';
121
+ const route = (path: string) => {
122
+ const { host, ...where } = routeOf(H, path);
123
+ assert.equal(host, H);
124
+ return where;
125
+ };
126
+
127
+ /** What a path below project `rebrand` belongs to: a prototype, the hub (null), or '' when it goes nowhere. */
128
+ function ownerBelow(path: string): string | null {
129
+ const where = route(`/rebrand${path}`);
130
+ if (where.kind === 'file') return where.prototype;
131
+ return where.kind === 'api' ? null : '';
132
+ }
133
+
134
+ test('a studio path goes to the link, the gate’s assets, the client’s hub, a function, or a project', () => {
135
+ const token = 'a'.repeat(43);
136
+ assert.deepEqual(route(`/invite/${token}`), { kind: 'link', token });
137
+ assert.deepEqual(route(`/invite/${token}/`), { kind: 'link', token });
138
+ for (const path of ['/_gate/geist.woff2', '/favicon.svg']) assert.deepEqual(route(path), { kind: 'asset' });
139
+ for (const path of ['/', '/index.html', '/assets/hub-x.js', '/_gate/other.woff2'])
140
+ assert.deepEqual(route(path), { kind: 'hub' }, path);
141
+ for (const path of ['/api/review', '/api/files'])
142
+ assert.deepEqual(route(path), { kind: 'api', project: null }, path);
143
+ assert.deepEqual(route('/rebrand/api/review'), { kind: 'api', project: 'rebrand' });
144
+ assert.deepEqual(route('/rebrand/'), { kind: 'file', project: 'rebrand', file: [], prototype: null });
145
+ assert.deepEqual(route('/rebrand'), { kind: 'file', project: 'rebrand', file: [], prototype: null });
146
+ assert.deepEqual(route('/rebrand/assets/index-x.js'), {
147
+ kind: 'file',
148
+ project: 'rebrand',
149
+ file: ['assets', 'index-x.js'],
150
+ prototype: null,
151
+ });
152
+ assert.deepEqual(route('/rebrand/_p/plan/entry.js'), {
153
+ kind: 'file',
154
+ project: 'rebrand',
155
+ file: ['_p', 'plan', 'entry.js'],
156
+ prototype: 'plan',
157
+ });
158
+ assert.deepEqual(route('/rebrand/plan/'), {
159
+ kind: 'file',
160
+ project: 'rebrand',
161
+ file: ['plan'],
162
+ prototype: 'plan',
163
+ });
164
+ assert.deepEqual(route('/rebrand/_p/all.js').kind, 'file');
165
+ assert.equal((route('/rebrand/_p/all.js') as { prototype: string }).prototype, 'all.js');
166
+ assert.equal((route('/rebrand/caf%C3%A9.png') as { file: string[] }).file[0], 'café.png');
167
+ });
168
+
169
+ test('anything else on a studio host goes nowhere: odd paths, the reserved names, and any host that is no name', () => {
170
+ for (const path of [
171
+ '/invite/short',
172
+ '/invite/',
173
+ '/guide/x/',
174
+ '/_p/plan/entry.js',
175
+ '/api/other',
176
+ '/api/review/x',
177
+ '/rebrand/api/files',
178
+ '/rebrand/api/review/x',
179
+ '/Rebrand/',
180
+ '/robots.txt',
181
+ '/rebrand/..%2fother/x.js',
182
+ '/rebrand//x.js',
183
+ '/rebrand/%255Fp/x',
184
+ '/rebrand%5cx',
185
+ '/%E0%A4%A/',
186
+ '/rebrand/assets/.%09./_p/all.js',
187
+ '/rebrand/assets/.%0A./_p/x/entry.js',
188
+ '/rebrand/assets/.%0D./x.js',
189
+ '/rebrand/assets/x.js%3Fy',
190
+ '/rebrand/assets/x.js%23y',
191
+ '/rebrand/assets/%20x.js',
192
+ '/rebrand/assets/x.js%20',
193
+ '/rebrand/assets/x%E2%80%AE.js',
194
+ ])
195
+ assert.deepEqual(route(path), { kind: 'none' }, path);
196
+ // A path that could mean more than one file is refused below a project too, so its prototype is ''.
197
+ assert.equal((route('/rebrand/_P/plan/x.js') as { prototype: string }).prototype, '');
198
+ for (const host of ['', 'localhost', 'a b'])
199
+ assert.deepEqual(routeOf(host, '/rebrand/'), { host: '', kind: 'none' }, host);
200
+ });
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