@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
@@ -1,35 +1,34 @@
1
- // Vercel Routing Middleware (the project's proxy.ts, named in vercel.json as
2
- // proxy.entrypoint): the gate in front of every path of the deployed
3
- // prototypes, the review API included. There is one way in, a one-time link
4
- // that Studio makes for one named person:
1
+ // Vercel Routing Middleware (a studio site's proxy.ts, named in vercel.json
2
+ // as proxy.entrypoint): the gate in front of every path of every client's
3
+ // host, the review API included. There is one way in, a one-time link that
4
+ // Studio makes for one named person, on that person's client's host:
5
5
  //
6
6
  // GET /invite/<token> shows who the link is for, and uses nothing up, so
7
7
  // a mail or chat preview that opens it costs nothing
8
8
  // POST /invite/<token> uses the link up, sets the person's signed cookie
9
- // (server/session.ts) and goes to the hub
9
+ // (server/session.ts) and goes to the client's hub
10
10
  //
11
- // A used, expired or unknown link is 404. Every other path needs the cookie.
12
- // It carries when the store last said yes; after CHECK_SECONDS (5 minutes)
13
- // the gate asks the store again on the next request of any kind, so a revoke,
14
- // a sign out everywhere or a changed access end reaches every request within
15
- // minutes. A store that does not answer is 503, never a sign out. A Client
16
- // asking for a prototype that is not client-facing, its page or any file
17
- // built from it under /_p/<slug>/, gets 404. Without a session a
18
- // page gets the sign-in page (401), which says what to do and has no form,
19
- // and anything else a plain-text 401. Without GATE_SECRET the gate fails
20
- // closed (503).
21
- import { next } from '@vercel/functions';
11
+ // A used, expired or unknown link is 404. Every other path needs the cookie,
12
+ // signed to the host's client. It carries when the store last said yes;
13
+ // after CHECK_SECONDS (5 minutes) the gate asks the store again on the next
14
+ // request of any kind, so a revoke, a sign out everywhere or a changed access
15
+ // end reaches every request within minutes. A store that does not answer is
16
+ // 503, never a sign out. A Client asking for a prototype that is not
17
+ // client-facing, its page or any file built from it under
18
+ // /<project>/_p/<slug>/, gets 404. Without a session a page gets the sign-in
19
+ // page (401), which says what to do and has no form, and anything else a
20
+ // plain-text 401. Without GATE_SECRET or the store the gate fails closed (503).
21
+ import { next, rewrite } from '@vercel/functions';
22
22
  import { base64, sha256 } from '../core/bytes.js';
23
23
  import { escapeHtml } from '../core/html.js';
24
- import { isPrototypeSlug, prototypeOfPath } from '../core/prototype.js';
25
- import { deployedReview } from '../server/api.js';
26
- import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, } from '../server/session.js';
27
- const DEFAULT_SITE_NAME = 'Prototypes';
28
- const LINK_PATH = /^\/invite\/([\w-]{20,100})\/?$/;
24
+ import { GATE_FONT, isPrototypeSlug, PROOF_PATH, routeOf } from '../core/prototype.js';
25
+ import { studioReview } from '../server/api.js';
26
+ import { hostCache, hostOf } from '../server/hosts.js';
27
+ import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, } from '../server/session.js';
28
+ export const DEFAULT_SITE_NAME = 'Prototypes';
29
29
  // What the gate pages load before sign-in: the hub's face (the Vite plugin
30
- // emits it at this fixed path) and the favicon. Nothing else.
31
- export const GATE_FONT = '/_gate/geist.woff2';
32
- const GATE_ASSETS = new Set([GATE_FONT, '/favicon.svg']);
30
+ // emits it at this fixed path, at the host root) and the favicon. Nothing else.
31
+ export { GATE_FONT };
33
32
  // The gate pages. The CSS is a constant so its CSP hash is computed once and
34
33
  // the pages carry no 'unsafe-inline', and they run no script at all. The gate
35
34
  // is hub chrome, so it wears the hub's neutral system look (shell/system.css)
@@ -116,7 +115,7 @@ function wantsHtml(request) {
116
115
  return dest === 'document' || dest === 'iframe';
117
116
  return (request.headers.get('accept') ?? '').includes('text/html');
118
117
  }
119
- const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
118
+ export const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
120
119
  /**
121
120
  * Every answer the gate makes itself carries these: never cached, never
122
121
  * indexed, no referrer. The link page alone sends a same-origin referrer, so
@@ -139,7 +138,7 @@ function gateHeaders(type, { cookie, allow, referrer = 'no-referrer', retryAfter
139
138
  headers.set('retry-after', String(retryAfter));
140
139
  return headers;
141
140
  }
142
- const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
141
+ export const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
143
142
  status,
144
143
  headers: gateHeaders('text/plain; charset=utf-8', extra),
145
144
  });
@@ -153,12 +152,11 @@ async function page(request, status, html, text, extra = {}) {
153
152
  }
154
153
  const unavailable = (request, error) => plain(request, 503, error);
155
154
  /** One answer for a prototype a Client may not have, whether it is internal or not there at all. */
156
- const notFound = (request) => plain(request, 404, NOT_FOUND);
155
+ export const notFound = (request) => plain(request, 404, NOT_FOUND);
157
156
  const NOT_FOUND = 'Not found.';
158
157
  /** The store did not answer: a 503 that passes, so it says when to try again. */
159
- const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
158
+ export const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
160
159
  const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
161
- const NO_STORE_YET = 'Review is not set up on this site: pnpm bootstrap --vercel <project> --review.';
162
160
  /**
163
161
  * A store call that can fail without signing anyone out: `undefined` when the
164
162
  * store did not answer, so the caller answers 503 and leaves the cookie alone.
@@ -172,12 +170,9 @@ async function ask(run) {
172
170
  return undefined;
173
171
  }
174
172
  }
175
- async function showLink(ctx, token) {
173
+ async function showLink(ctx, review, scope, token) {
176
174
  const { siteName, request } = ctx;
177
- const setup = deployedReview(process.env, null, () => ctx.at);
178
- if ('error' in setup)
179
- return unavailable(request, setup.error);
180
- const person = await ask(() => setup.review.peekLink(token));
175
+ const person = await ask(() => review.peekLink(scope, token));
181
176
  if (person === undefined)
182
177
  return storeDown(request);
183
178
  return person
@@ -190,30 +185,27 @@ async function sessionFor(gate, person, at) {
190
185
  const token = await issuePersonToken(gate, claim, person.accessEndsAt, at);
191
186
  return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
192
187
  }
193
- async function signInWithLink(ctx, token) {
188
+ async function signInWithLink(ctx, { review, gate }, scope, token) {
194
189
  const { siteName, request, at } = ctx;
195
- if (!sameOrigin(request))
196
- return plain(request, 403, 'Forbidden');
197
- const setup = deployedReview(process.env, null, () => ctx.at);
198
- if ('error' in setup)
199
- return unavailable(request, setup.error);
200
- const person = await ask(() => setup.review.useLink(token));
190
+ const person = await ask(() => review.useLink(scope, token));
201
191
  if (person === undefined)
202
192
  return storeDown(request);
203
193
  if (!person)
204
194
  return page(request, 404, usedPage(siteName), USED);
205
- const headers = gateHeaders('text/plain; charset=utf-8', {
206
- cookie: await sessionFor(setup.gate, person, at),
207
- });
195
+ const headers = gateHeaders('text/plain; charset=utf-8', { cookie: await sessionFor(gate, person, at) });
208
196
  headers.set('location', '/');
209
197
  return new Response(null, { status: 303, headers });
210
198
  }
211
- async function link(ctx, token) {
212
- if (ctx.request.method === 'POST')
213
- return signInWithLink(ctx, token);
214
- if (isRead(ctx.request))
215
- return showLink(ctx, token);
216
- return plain(ctx.request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
199
+ /** A one-time link in `scope`: shown on a read, used on a same-origin POST. */
200
+ async function link(ctx, setup, scope, token) {
201
+ const { request } = ctx;
202
+ if (request.method === 'POST' && !sameOrigin(request))
203
+ return plain(request, 403, 'Forbidden');
204
+ if (request.method !== 'POST' && !isRead(request))
205
+ return plain(request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
206
+ return request.method === 'POST'
207
+ ? signInWithLink(ctx, setup, scope, token)
208
+ : showLink(ctx, setup.review, scope, token);
217
209
  }
218
210
  /**
219
211
  * No session. A page gets the sign-in page, and anything else (an asset, a
@@ -226,75 +218,147 @@ function signedOut({ siteName, request }, staleCookie) {
226
218
  return plain(request, 401, SIGNED_OUT, extra);
227
219
  return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
228
220
  }
229
- const pass = (cookie) => next({ headers: { 'cache-control': 'private, no-store', ...(cookie ? { 'set-cookie': cookie } : {}) } });
221
+ const passHeaders = (cookie) => ({
222
+ 'cache-control': 'private, no-store',
223
+ ...(cookie ? { 'set-cookie': cookie } : {}),
224
+ });
230
225
  /**
231
- * The prototype boundary, for a Client only: a path that belongs to a
232
- * prototype (its page, or a file under /_p/<slug>/) passes when that
233
- * prototype is client-facing, asked of the store on every request. Anything
234
- * else is the same 404, for an internal prototype, one that does not exist and
235
- * /_p/all.js alike, so the answer says nothing about what is there. A store
236
- * that does not answer is 503. Hub paths pass with no store read.
226
+ * Whether a prototype is open to a Client in `scope`: client-facing, asked of
227
+ * the store on every request, no cache. A name that is no prototype slug
228
+ * (`all.js`, or '' for a path that could mean more than one file) is never
229
+ * open, without asking. `undefined` when the store did not answer.
237
230
  */
238
- async function clientMay(ctx, review, slug) {
231
+ async function prototypeOpen(review, scope, slug) {
239
232
  if (!isPrototypeSlug(slug))
240
- return notFound(ctx.request);
241
- const audience = await ask(() => review.audienceOf(slug));
242
- if (audience === undefined)
243
- return storeDown(ctx.request);
244
- return audience === 'client' ? null : notFound(ctx.request);
233
+ return false;
234
+ const audience = await ask(() => review.audienceOf(scope, slug));
235
+ return audience === undefined ? undefined : audience === 'client';
245
236
  }
246
- /** Asks the store whether a stale session still holds: the person, or the answer that ends the request. */
247
- async function recheck(ctx, review, claim) {
248
- const current = await ask(() => review.sessionPerson(claim.id, claim.epoch));
249
- if (current === undefined)
250
- return storeDown(ctx.request);
251
- return current ?? (await signedOut(ctx, true));
237
+ const NOT_THERE = { refused: 'not-found' };
238
+ const DOWN = { refused: 'store-down' };
239
+ /** The client `pathname` is asked of on the request's host, or why there is none: an unknown host is the plain 404. */
240
+ export async function findPlace(request, pathname, hosts, at) {
241
+ const setup = studioReview(process.env, null, () => at);
242
+ if ('error' in setup)
243
+ return { refused: 'unavailable', error: setup.error };
244
+ const route = routeOf(hostOf(request), pathname);
245
+ const client = route.host ? await ask(() => hosts(route.host, (h) => setup.review.clientOfHost(h))) : null;
246
+ if (client === undefined)
247
+ return DOWN;
248
+ if (!client)
249
+ return NOT_THERE;
250
+ const gate = { ...setup.keys, client: client.slug, since: client.createdAt };
251
+ const place = { client, gate, scope: { client: client.slug } };
252
+ return { review: setup.review, place, route };
252
253
  }
253
254
  /**
254
- * A signed session. While the store's last yes is fresh the signature is
255
- * enough. Once it is stale the store is asked, whatever the request says it
256
- * is (its headers are the client's to choose): yes passes with a fresh cookie
257
- * whose expiry follows the person's current access end; no signs out and
258
- * clears the cookie; no answer is 503 and the cookie stays. A p3 cookie,
259
- * which carries no kind, is always stale, so its first request re-issues it.
260
- * Then, for anyone but Studio, the prototype boundary on a prototype's path.
255
+ * The project a route names, when it is the client's, and for a Client, only
256
+ * a client-facing prototype's files. Every other answer is the plain 404, so
257
+ * another client's project, a project with no build (its files and its
258
+ * review API alike) and an internal prototype read the same as a path that
259
+ * is not there.
261
260
  */
262
- async function personSession(ctx, gate, claim) {
263
- const slug = prototypeOfPath(new URL(ctx.request.url).pathname);
264
- const fresh = isFresh(claim, ctx.at);
265
- if (fresh && (claim.kind === 'studio' || slug === null))
266
- return pass();
267
- const setup = deployedReview(process.env, null, () => ctx.at);
268
- if ('error' in setup)
269
- return unavailable(ctx.request, setup.error);
270
- const current = fresh ? null : await recheck(ctx, setup.review, claim);
271
- if (current instanceof Response)
272
- return current;
261
+ async function boundary({ review, place, route }, kind) {
262
+ if (route.kind === 'none' || route.kind === 'link')
263
+ return NOT_THERE;
264
+ const named = route.kind === 'file' || route.kind === 'api' ? route.project : null;
265
+ if (named === null)
266
+ return null;
267
+ const scope = { client: place.client.slug, project: named };
268
+ const project = await ask(() => review.projectOf(scope));
269
+ if (project === undefined)
270
+ return DOWN;
271
+ if (!project || project.build === null)
272
+ return NOT_THERE;
273
+ if (route.kind !== 'file' || route.prototype === null || kind === 'studio')
274
+ return project;
275
+ const open = await prototypeOpen(review, scope, route.prototype);
276
+ return open === undefined ? DOWN : open ? project : NOT_THERE;
277
+ }
278
+ /**
279
+ * The checks every studio request passes, in the gate and again in the files
280
+ * function: a session signed to this host's client (asked of the store again
281
+ * once its stamp is stale), then the boundary.
282
+ */
283
+ export async function admit(found, request, at) {
284
+ const cookie = readCookie(request, PERSON_COOKIE);
285
+ const claim = await readPersonToken(found.place.gate, cookie, at);
286
+ if (!claim)
287
+ return { refused: 'signed-out', stale: cookie !== undefined };
288
+ let current = null;
289
+ if (!isFresh(claim, at)) {
290
+ const asked = await ask(() => found.review.sessionPerson(found.place.scope, claim.id, claim.epoch));
291
+ if (asked === undefined)
292
+ return DOWN;
293
+ if (!asked)
294
+ return { refused: 'signed-out', stale: true };
295
+ current = asked;
296
+ }
273
297
  const kind = current ? current.kind : claim.kind;
274
- const refused = kind !== 'studio' && slug !== null ? await clientMay(ctx, setup.review, slug) : null;
275
- return refused ?? pass(current ? await sessionFor(gate, current, ctx.at) : undefined);
298
+ const project = await boundary(found, kind);
299
+ return project && 'refused' in project ? project : { ...found, kind, current, project };
300
+ }
301
+ /** The answer for a refusal: the same from the gate and from the files function. */
302
+ export function refuse(ctx, refusal) {
303
+ switch (refusal.refused) {
304
+ case 'not-found':
305
+ return notFound(ctx.request);
306
+ case 'store-down':
307
+ return storeDown(ctx.request);
308
+ case 'unavailable':
309
+ return unavailable(ctx.request, refusal.error);
310
+ case 'signed-out':
311
+ return signedOut(ctx, refusal.stale);
312
+ }
313
+ }
314
+ /**
315
+ * Where an admitted request goes on to: a project's file to the files
316
+ * function and its review API to the review function, each with the project
317
+ * the path named, and anything else on to the site's own files.
318
+ */
319
+ function onward(url, route, cookie) {
320
+ const headers = passHeaders(cookie);
321
+ if (route.kind === 'file')
322
+ return rewrite(new URL(`/api/files?path=${encodeURIComponent(url.pathname)}`, url), { headers });
323
+ if (route.kind !== 'api' || route.project === null)
324
+ return next({ headers });
325
+ const to = new URL(`/api/review${url.search}`, url);
326
+ to.searchParams.set('project', route.project);
327
+ return rewrite(to, { headers });
328
+ }
329
+ /**
330
+ * The host proof `client add` asks a new client's host for (PROOF_PATH): its
331
+ * answer, on a GET whose ask the store holds for this host's client. Any
332
+ * other ask, method or store failure goes on as the hub path it is, so the
333
+ * path tells a caller without the ask nothing, and a host no client has
334
+ * never gets here (findPlace's plain 404).
335
+ */
336
+ async function proven({ review, place, route }, request, pathname) {
337
+ const given = route.kind === 'hub' && request.method === 'GET' ? pathname.match(PROOF_PATH)?.[1] : undefined;
338
+ const answer = given ? await ask(() => review.proofAnswer(place.scope, given)) : null;
339
+ return answer ? plain(request, 200, answer) : null;
276
340
  }
277
- /** The middleware a project's proxy.ts default-exports. */
278
- export function createProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date() } = {}) {
341
+ /** The middleware a studio site's proxy.ts default-exports. Its sign-in page names no client (S10). */
342
+ export function createStudioProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date(), } = {}) {
343
+ const hosts = hostCache(now);
279
344
  return async function proxy(request) {
280
- const gate = readGate(process.env);
281
- if (!gate)
282
- return unavailable(request, 'The prototype gate is not configured.');
283
345
  const ctx = { siteName, request, at: now() };
284
346
  const url = new URL(request.url);
285
- const linked = url.pathname.match(LINK_PATH);
286
- if (linked)
287
- return link(ctx, linked[1]);
288
- if (GATE_ASSETS.has(url.pathname) && isRead(request))
347
+ const found = await findPlace(request, url.pathname, hosts, ctx.at);
348
+ if ('refused' in found)
349
+ return refuse(ctx, found);
350
+ const proof = await proven(found, request, url.pathname);
351
+ if (proof)
352
+ return proof;
353
+ const { route, place } = found;
354
+ if (route.kind === 'link')
355
+ return link(ctx, { review: found.review, gate: place.gate }, place.scope, route.token);
356
+ if (route.kind === 'asset' && isRead(request))
289
357
  return next();
290
- const cookie = readCookie(request, PERSON_COOKIE);
291
- const claim = await readPersonToken(gate, cookie, ctx.at);
292
- if (claim)
293
- return personSession(ctx, gate, claim);
294
- // Tokens are signed to the store's URL, so without one none verifies. Clearing cookies then
295
- // would turn a missing env var into everyone locked out, since links work once: answer 503 instead.
296
- if (cookie !== undefined && gate.site === '')
297
- return unavailable(request, NO_STORE_YET);
298
- return signedOut(ctx, cookie !== undefined);
358
+ const admitted = await admit(found, request, ctx.at);
359
+ if ('refused' in admitted)
360
+ return refuse(ctx, admitted);
361
+ const cookie = admitted.current ? await sessionFor(place.gate, admitted.current, ctx.at) : undefined;
362
+ return onward(url, route, cookie);
299
363
  };
300
364
  }
@@ -2,6 +2,12 @@ import { type InlineConfig, type PluginOption } from 'vite';
2
2
  export type HubOptions = {
3
3
  /** The project: the folder with prototypes/, kit/ and docs/. */
4
4
  root: string;
5
+ /**
6
+ * The path the hub is served under: `/` (the default), or `/<project>/`
7
+ * when a studio serves the build below a client's host. Every path the hub
8
+ * asks for or links to starts with it.
9
+ */
10
+ base?: string;
5
11
  };
6
12
  /** Every plugin the hub needs, for a project that writes its own vite.config.ts. */
7
13
  export declare function designHub({ root }: HubOptions): PluginOption[];
@@ -12,4 +18,21 @@ export declare function designHub({ root }: HubOptions): PluginOption[];
12
18
  * than drifting to the next one, because a fallback silently serves the wrong
13
19
  * project. A build needs no port, so Vercel builds without a .env.
14
20
  */
15
- export declare function hubConfig({ root }: HubOptions, command: 'serve' | 'build'): InlineConfig;
21
+ export declare function hubConfig({ root, base }: HubOptions, command: 'serve' | 'build'): InlineConfig;
22
+ /**
23
+ * A project's build below a base, as a studio serves it: what `design-hub
24
+ * share` uploads. `outDir` defaults to the project's `dist/`. Vite logs only
25
+ * warnings and errors, both to stderr, so a command's stdout stays its one
26
+ * JSON object.
27
+ */
28
+ export declare function buildHub(options: HubOptions & {
29
+ outDir?: string;
30
+ }): Promise<void>;
31
+ /**
32
+ * A studio site's build (its `build.mjs`): the client hub from
33
+ * `shell/studio/`, the gate's font at the host root where the gate pages ask
34
+ * for it, and the favicon. `root` is the studio folder; it keeps no kit, so
35
+ * `@kit/*` is the package's.
36
+ */
37
+ export declare function studioConfig(root: string): InlineConfig;
38
+ export declare const buildStudio: (root: string) => Promise<void>;
@@ -9,7 +9,8 @@ import { readFileSync } from 'node:fs';
9
9
  import { createRequire } from 'node:module';
10
10
  import { dirname, join } from 'node:path';
11
11
  import react from '@vitejs/plugin-react';
12
- import { searchForWorkspaceRoot } from 'vite';
12
+ import { build, searchForWorkspaceRoot } from 'vite';
13
+ import { isProjectSlug } from '../core/prototype.js';
13
14
  import { devReview } from '../server/dev.js';
14
15
  import { devPort, readEnv } from '../server/env.js';
15
16
  import { PACKAGE, PACKAGE_KIT, SHELL } from '../server/paths.js';
@@ -48,6 +49,12 @@ function gateFont() {
48
49
  },
49
50
  };
50
51
  }
52
+ /** The base itself, or an error: `/`, or one project slug between slashes, nothing that leaves the host. */
53
+ function baseOf(base = '/') {
54
+ if (base === '/' || isProjectSlug(base.match(/^\/(.*)\/$/)?.[1]))
55
+ return base;
56
+ throw new Error(`base must be / or /<project>/ with a project slug, not ${JSON.stringify(base)}.`);
57
+ }
51
58
  /** Every plugin the hub needs, for a project that writes its own vite.config.ts. */
52
59
  export function designHub({ root }) {
53
60
  return [
@@ -68,12 +75,13 @@ export function designHub({ root }) {
68
75
  * than drifting to the next one, because a fallback silently serves the wrong
69
76
  * project. A build needs no port, so Vercel builds without a .env.
70
77
  */
71
- export function hubConfig({ root }, command) {
78
+ export function hubConfig({ root, base }, command) {
72
79
  const port = devPort(readEnv(root));
73
80
  if (command === 'serve' && port === undefined)
74
81
  throw new Error('PORT is not set. `design-hub dev` claims one; a config of your own runs `design-hub bootstrap` first.');
75
82
  return {
76
83
  configFile: false,
84
+ base: baseOf(base),
77
85
  root: SHELL,
78
86
  publicDir: join(SHELL, 'public'),
79
87
  envDir: root,
@@ -88,3 +96,39 @@ export function hubConfig({ root }, command) {
88
96
  build: { outDir: join(root, 'dist'), emptyOutDir: true },
89
97
  };
90
98
  }
99
+ /**
100
+ * A project's build below a base, as a studio serves it: what `design-hub
101
+ * share` uploads. `outDir` defaults to the project's `dist/`. Vite logs only
102
+ * warnings and errors, both to stderr, so a command's stdout stays its one
103
+ * JSON object.
104
+ */
105
+ export async function buildHub(options) {
106
+ const config = hubConfig(options, 'build');
107
+ await build({
108
+ ...config,
109
+ logLevel: 'warn',
110
+ build: { ...config.build, outDir: options.outDir ?? config.build?.outDir },
111
+ });
112
+ }
113
+ /**
114
+ * A studio site's build (its `build.mjs`): the client hub from
115
+ * `shell/studio/`, the gate's font at the host root where the gate pages ask
116
+ * for it, and the favicon. `root` is the studio folder; it keeps no kit, so
117
+ * `@kit/*` is the package's.
118
+ */
119
+ export function studioConfig(root) {
120
+ return {
121
+ configFile: false,
122
+ base: '/',
123
+ root: join(SHELL, 'studio'),
124
+ publicDir: join(SHELL, 'public'),
125
+ envDir: root,
126
+ cacheDir: join(root, 'node_modules', '.vite'),
127
+ plugins: [react(), kitResolver(join(root, 'kit'), PACKAGE_KIT), gateFont()],
128
+ resolve: { dedupe: ['react', 'react-dom'] },
129
+ build: { outDir: join(root, 'dist'), emptyOutDir: true },
130
+ };
131
+ }
132
+ export const buildStudio = async (root) => {
133
+ await build(studioConfig(root));
134
+ };
package/kit/Artboard.tsx CHANGED
@@ -8,8 +8,9 @@ import {
8
8
  useState,
9
9
  } from 'react';
10
10
  import { FORMATS, type Format, type FormatName, sizeLabel, zoomFor } from '../core/formats.ts';
11
- import { restoreDials, useDials } from './dials';
12
- import { ViewTools } from './ViewTools';
11
+ import { client, restoreDials, useDials } from './dials';
12
+ import { pinAppearance, urlAppearance } from './prefs';
13
+ import { ViewScope } from './ViewTools';
13
14
  import './artboard.css';
14
15
 
15
16
  export { FORMAT_NAMES, FORMATS, type FormatName } from '../core/formats.ts';
@@ -54,6 +55,8 @@ function useRoom() {
54
55
  return { ref, room };
55
56
  }
56
57
 
58
+ const NO_VIEW: readonly never[] = [];
59
+
57
60
  type BoardProps = { title: string; lede?: ReactNode; children: ReactNode };
58
61
 
59
62
  /**
@@ -65,8 +68,9 @@ export function Board({ title, lede, children }: BoardProps) {
65
68
  const view = useDials(
66
69
  'Board',
67
70
  {
68
- zoom: { type: 'select', options: ZOOMS, default: 'fit' },
69
- guides: true,
71
+ // How a person looks at the boards, not the design: a Client's from the start. They are a board's only viewing tools.
72
+ zoom: client({ type: 'select', options: ZOOMS, default: 'fit' }),
73
+ guides: client(true),
70
74
  },
71
75
  {
72
76
  hints: {
@@ -81,6 +85,13 @@ export function Board({ title, lede, children }: BoardProps) {
81
85
  if (exporting) void settleForExport();
82
86
  }, []);
83
87
 
88
+ // Collateral has one look: light, or the one ?theme= names, which is what export writes.
89
+ // A person's own dark theme never recolours a piece that ships light.
90
+ useEffect(() => {
91
+ pinAppearance(urlAppearance() ?? 'light');
92
+ return () => pinAppearance(null);
93
+ }, []);
94
+
84
95
  if (exporting)
85
96
  return (
86
97
  <BoardView.Provider value={{ zoom: '100%', guides: false, room }}>
@@ -95,10 +106,12 @@ export function Board({ title, lede, children }: BoardProps) {
95
106
  <h1 className="board-title">{title}</h1>
96
107
  {lede ? <p className="board-lede">{lede}</p> : null}
97
108
  </header>
98
- <div className="board-grid" ref={ref}>
99
- {children}
100
- </div>
101
- <ViewTools />
109
+ {/* No View panel on a board: a still file has no motion to slow and no theme to flip. */}
110
+ <ViewScope.Provider value={NO_VIEW}>
111
+ <div className="board-grid" ref={ref}>
112
+ {children}
113
+ </div>
114
+ </ViewScope.Provider>
102
115
  </main>
103
116
  </BoardView.Provider>
104
117
  );
package/kit/Space.tsx CHANGED
@@ -1,12 +1,13 @@
1
1
  import type { ReactNode } from 'react';
2
2
  import './space.css';
3
- import { ViewTools } from './ViewTools';
3
+ import { type ViewTool, ViewTools } from './ViewTools';
4
4
 
5
5
  type Props = {
6
6
  title: string;
7
7
  lede?: ReactNode;
8
8
  width?: 'narrow' | 'wide' | 'full';
9
- tools?: boolean;
9
+ /** The View tools: all of them, none (`false`), or only the ones that change something here. */
10
+ tools?: boolean | readonly ViewTool[];
10
11
  children: ReactNode;
11
12
  };
12
13
 
@@ -19,7 +20,7 @@ export function Space({ title, lede, width = 'wide', tools = true, children }: P
19
20
  {lede ? <p className="space-lede">{lede}</p> : null}
20
21
  </header>
21
22
  {children}
22
- {tools ? <ViewTools /> : null}
23
+ {tools ? <ViewTools tools={tools === true ? undefined : tools} /> : null}
23
24
  </main>
24
25
  );
25
26
  }