@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
@@ -0,0 +1,91 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'node:test';
3
+ import type { Scope } from './model.ts';
4
+ import { keyOf, sweepPrefix } from './store.ts';
5
+
6
+ const A: Scope = { client: 'acme', project: 'rebrand' };
7
+
8
+ test('keys: the studio’s, a client’s and a project’s, each under hub4:', () => {
9
+ assert.equal(keyOf('studio', 'people'), 'hub4:studio:people');
10
+ assert.equal(keyOf('studio', 'settings'), 'hub4:studio:settings');
11
+ assert.equal(keyOf(A, 'people'), 'hub4:c:acme:people');
12
+ assert.equal(keyOf({ client: 'acme' }, 'links'), 'hub4:c:acme:links');
13
+ assert.equal(keyOf(A, 'audiences'), 'hub4:c:acme:p:rebrand:audiences');
14
+ assert.equal(keyOf(A, 'prototypes'), 'hub4:c:acme:p:rebrand:prototypes');
15
+ assert.equal(keyOf(A, 'comments', 'sample-list'), 'hub4:c:acme:p:rebrand:comments:sample-list');
16
+ assert.equal(keyOf(A, 'controls', 'sample-list'), 'hub4:c:acme:p:rebrand:controls:sample-list');
17
+ // A reserved prototype name is read like any other, and finds nothing.
18
+ assert.equal(keyOf(A, 'comments', 'guide'), 'hub4:c:acme:p:rebrand:comments:guide');
19
+ });
20
+
21
+ test('a key is never built from a part that could reach another scope’s keys', () => {
22
+ const bad: [string, () => string][] = [
23
+ ['a client with a colon', () => keyOf({ client: 'acme:p:x' }, 'people')],
24
+ ['an empty client', () => keyOf({ client: '' }, 'people')],
25
+ ['the studio’s host as a client', () => keyOf({ client: 'studio' }, 'people')],
26
+ ['a reserved client', () => keyOf({ client: 'www' }, 'people')],
27
+ ['a project op with no project', () => keyOf({ client: 'acme' }, 'audiences')],
28
+ ['a project with a colon', () => keyOf({ client: 'acme', project: 'a:b' }, 'counters')],
29
+ ['a reserved project', () => keyOf({ client: 'acme', project: 'invite' }, 'counters')],
30
+ ['a prototype with a colon', () => keyOf(A, 'comments', 'x:y')],
31
+ ['no prototype', () => keyOf(A, 'controls', undefined as unknown as string)],
32
+ ['a client that is not text', () => keyOf({ client: 3 as unknown as string }, 'people')],
33
+ ];
34
+ for (const [what, build] of bad) assert.throws(build, /Not a store key/, what);
35
+ });
36
+
37
+ test('a sweep prefix holds every key of its scope and no other scope’s, the studio’s or the local store’s', () => {
38
+ const acme = sweepPrefix({ client: 'acme' });
39
+ const rebrand = sweepPrefix(A);
40
+ assert.equal(acme, 'hub4:c:acme:');
41
+ assert.equal(rebrand, 'hub4:c:acme:p:rebrand:');
42
+ const ofProject = (s: Scope) => [
43
+ keyOf(s, 'audiences'),
44
+ keyOf(s, 'counters'),
45
+ keyOf(s, 'prototypes'),
46
+ keyOf(s, 'comments', 'sample-list'),
47
+ keyOf(s, 'controls', 'sample-list'),
48
+ ];
49
+ const ofClient = (s: Scope) => [
50
+ ...(['people', 'revoked', 'epochs', 'links'] as const).map((n) => keyOf(s, n)),
51
+ keyOf(s, 'projects'),
52
+ keyOf(s, 'proof'),
53
+ ...ofProject(s),
54
+ ];
55
+ const studio = (
56
+ [
57
+ 'people',
58
+ 'revoked',
59
+ 'epochs',
60
+ 'links',
61
+ 'settings',
62
+ 'mark',
63
+ 'clients',
64
+ 'hosts',
65
+ 'claims',
66
+ 'held',
67
+ ] as const
68
+ ).map((n) => keyOf('studio', n));
69
+ for (const key of ofClient(A)) assert.ok(key.startsWith(acme), key);
70
+ for (const key of ofProject(A)) assert.ok(key.startsWith(rebrand), key);
71
+ // acme never reaches acme-two or acm, nor rebrand reaches rebrand-two; no client reaches the studio's keys.
72
+ const notAcme = [
73
+ ...ofClient({ client: 'acme-two', project: 'rebrand' }),
74
+ ...ofClient({ client: 'acm', project: 'rebrand' }),
75
+ ...ofClient({ client: 'site', project: 'site' }),
76
+ ...studio,
77
+ ];
78
+ for (const key of notAcme) assert.ok(!key.startsWith(acme), key);
79
+ for (const key of ofProject({ client: 'acme', project: 'rebrand-two' }))
80
+ assert.ok(!key.startsWith(rebrand), key);
81
+ // The local store's records are this machine's own: no prefix is ever built to sweep them.
82
+ assert.throws(() => sweepPrefix({ client: 'site' }), /local store/);
83
+ assert.throws(() => sweepPrefix({ client: 'site', project: 'site' }), /local store/);
84
+ for (const bad of [
85
+ { client: 'acme:p:x' },
86
+ { client: '' },
87
+ { client: 'studio' },
88
+ { client: 'acme', project: 'a:b' },
89
+ ])
90
+ assert.throws(() => sweepPrefix(bad), /Not a store key/, JSON.stringify(bad));
91
+ });
@@ -2,17 +2,34 @@
2
2
  // and set operations. Production is Upstash Redis over REST
3
3
  // (server/upstash.ts); dev and tests use the in-memory store below, which
4
4
  // the dev server persists to a file (server/file-kv.ts).
5
+ import { SLUG } from '../feature.ts';
6
+ import { isClientSlug, isProjectSlug } from '../prototype.ts';
7
+ import { LOCAL_CLIENT, type Scope } from './model.ts';
8
+
9
+ /** A comment's or a control's prototype: any kebab-case name, so reading one that is reserved finds nothing, as before. */
10
+ const isKebab = (v: unknown): v is string => typeof v === 'string' && SLUG.test(v);
5
11
 
6
12
  export interface KV {
7
13
  hget(key: string, field: string): Promise<string | null>;
8
14
  hset(key: string, field: string, value: string): Promise<void>;
9
15
  /** How many fields it removed: 1, or 0 when the field was not there. Atomic, so it can claim a record. */
10
16
  hdel(key: string, field: string): Promise<number>;
17
+ /** Sets a field only when it is not there: true when this call set it. Atomic, so of two at once one claims it. */
18
+ hsetnx(key: string, field: string, value: string): Promise<boolean>;
11
19
  hgetall(key: string): Promise<Record<string, string>>;
12
20
  /** Adds `by` to a field's integer (absent is 0) and returns the result. Atomic: two calls always add both. */
13
21
  hincrby(key: string, field: string, by: number): Promise<number>;
14
22
  sadd(key: string, member: string): Promise<void>;
15
23
  smembers(key: string): Promise<string[]>;
24
+ /** Removes a whole key, a hash or a set; one that is not there is no error. */
25
+ del(key: string): Promise<void>;
26
+ /**
27
+ * Removes every key, hash or set, whose name starts with `prefix`, taken as
28
+ * plain text (no character in it is a pattern); none is no error. Not one
29
+ * atomic step: a key written while it runs may stay. Redis: SCAN … MATCH,
30
+ * then DEL; Postgres: DELETE … WHERE key LIKE, the prefix escaped.
31
+ */
32
+ delPrefix(prefix: string): Promise<void>;
16
33
  }
17
34
 
18
35
  /**
@@ -20,40 +37,115 @@ export interface KV {
20
37
  * another app's data. Bumped when the stored shapes change without a
21
38
  * migration: records under an older prefix are simply never read.
22
39
  */
23
- const PREFIX = 'hub3:';
40
+ const PREFIX = 'hub4:';
24
41
 
25
- export const KEY = {
26
- /** A person's profile: everything but revocation and epoch, which nothing here can overwrite. */
27
- people: `${PREFIX}people`,
28
- /** When a person was revoked. Written once by revoke, never cleared. */
29
- revoked: `${PREFIX}revoked`,
30
- /** A person's session epoch, only ever raised, by HINCRBY. Absent is 0. */
31
- epochs: `${PREFIX}epochs`,
32
- /** One-time sign-in links: hex(sha256(token)) to a Link. */
33
- links: `${PREFIX}links`,
34
- /** Project settings, one field each: `access` is the default access length. */
35
- settings: `${PREFIX}settings`,
36
- prototypes: `${PREFIX}prototypes`,
37
- /**
38
- * Which prototypes are client-facing: field `<slug>`, value `client`. Only
39
- * client-facing is stored; internal is the field's absence, and any other
40
- * value reads as internal too.
41
- */
42
- audiences: `${PREFIX}audiences`,
43
- /**
44
- * The last thread number given on each prototype, only ever raised by HINCRBY: field
45
- * `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
46
- */
47
- counters: `${PREFIX}counters`,
48
- comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
49
- /**
50
- * What Studio marked on one prototype's controls (core/controls.ts): field
51
- * `dial:<id>` is `client` or `internal`, overruling the code either way;
52
- * field `variant:<name>` is `client`, and internal is its absence. One
53
- * field per control, so two people marking at once never undo each other.
54
- */
55
- controls: (prototype: string) => `${PREFIX}controls:${prototype}`,
56
- } as const;
42
+ /**
43
+ * The studio's own records: Studio people (profile, revocation, epoch and
44
+ * links, as a client's below) and `settings`, one field each: `access` is the
45
+ * default access length. `mark`, one field, `id`: the id of the one studio
46
+ * this database serves (its studio.json `id`), set once by `studio create`
47
+ * and checked by every command before it acts (server/mark.ts). No other
48
+ * key names a studio, so two studios on one database would be one studio.
49
+ */
50
+ type StudioName = 'people' | 'revoked' | 'epochs' | 'links' | 'settings' | 'mark';
51
+
52
+ /**
53
+ * The studio's clients, outside every scope: `clients`, a client's slug to
54
+ * its Client; `hosts`, a host name to the slug of the client it serves;
55
+ * `claims`, one counter per slug (`client:<slug>`) and per host
56
+ * (`host:<host>`), raised by HINCRBY when a client is added: whoever raised
57
+ * it to 1 holds it, so two adds at once never both get one. Archiving a
58
+ * client deletes its fields, so the slug and the host can be claimed again.
59
+ * `held`, the same fields, each the time a client that had it was archived:
60
+ * neither is added again until every gate's cached answer for the host has
61
+ * surely run out (REUSE_SECONDS in service.ts).
62
+ */
63
+ type DirectoryName = 'clients' | 'hosts' | 'claims' | 'held';
64
+ const DIRECTORY: readonly string[] = ['clients', 'hosts', 'claims', 'held'];
65
+
66
+ /**
67
+ * A client's people, in four records. `people`: a profile, everything but
68
+ * revocation and epoch, which nothing here can overwrite. `revoked`: when a
69
+ * person was revoked, written once by revoke, never cleared. `epochs`: a
70
+ * person's session epoch, only ever raised, by HINCRBY; absent is 0.
71
+ * `links`: one-time sign-in links, hex(sha256(token)) to a Link. And
72
+ * `projects`: the client's projects, a slug to its Project. `proof`: while
73
+ * `client add` waits for the client's host, one field, hex(sha256(ask)) to
74
+ * the answer the gate gives for that ask (PROOF_PATH in prototype.ts).
75
+ */
76
+ type ClientName = 'people' | 'revoked' | 'epochs' | 'links';
77
+ type ClientOnlyName = 'projects' | 'proof';
78
+
79
+ /**
80
+ * A project's records. `audiences`: which prototypes are client-facing,
81
+ * field `<slug>`, value `client`; only client-facing is stored, internal is
82
+ * the field's absence, and any other value reads as internal too.
83
+ * `counters`: the last thread number given on each prototype, only ever
84
+ * raised by HINCRBY: field `<prototype>` counts client-facing threads,
85
+ * `<prototype>:internal` internal ones. `prototypes`: the set of prototypes
86
+ * with comments. `comments`, per prototype: one field per comment.
87
+ * `controls`, per prototype: what Studio marked on its controls
88
+ * (core/controls.ts): field `dial:<id>` is `client` or `internal`, overruling
89
+ * the code either way; field `variant:<name>` is `client`, and internal is
90
+ * its absence. One field per control, so two people marking at once never
91
+ * undo each other. `builds`: every build a share started for the project,
92
+ * field `<build>` to `{ n, live }`: `n` is the order it was started in, from
93
+ * field `next` (HINCRBY), and `live` whether the project ever pointed at it
94
+ * (startBuild and setBuild in service.ts).
95
+ */
96
+ type ProjectName = 'audiences' | 'counters' | 'prototypes' | 'builds';
97
+ type PrototypeName = 'comments' | 'controls';
98
+
99
+ const PROJECT_NAMES: readonly string[] = [
100
+ 'audiences',
101
+ 'counters',
102
+ 'prototypes',
103
+ 'builds',
104
+ 'comments',
105
+ 'controls',
106
+ ];
107
+ const PER_PROTOTYPE: readonly string[] = ['comments', 'controls'];
108
+
109
+ function part(ok: (v: unknown) => v is string, v: unknown, what: string): string {
110
+ // A bad part is a bug in the caller, never input to answer: fail closed before any store call.
111
+ if (!ok(v)) throw new Error(`Not a store key: ${what} is ${JSON.stringify(v)}.`);
112
+ return v;
113
+ }
114
+
115
+ const clientPrefix = (client: unknown) => `${PREFIX}c:${part(isClientSlug, client, 'client')}:`;
116
+ const projectPrefix = (scope: Scope) =>
117
+ `${clientPrefix(scope.client)}p:${part(isProjectSlug, scope.project, 'project')}:`;
118
+
119
+ /**
120
+ * Every store key, and the only place one is built. `'studio'` names the
121
+ * studio's own records; a Scope names its client's, and its project's for a
122
+ * project name. Every part is checked (a client slug, a project slug, a
123
+ * kebab-case prototype), so no value can reach into another client's keys:
124
+ * no part can hold the `:` that joins them.
125
+ */
126
+ export function keyOf(scope: 'studio', name: StudioName | DirectoryName): string;
127
+ export function keyOf(scope: 'studio' | Scope, name: ClientName): string;
128
+ export function keyOf(scope: Scope, name: ClientOnlyName | ProjectName): string;
129
+ export function keyOf(scope: Scope, name: PrototypeName, prototype: string): string;
130
+ export function keyOf(scope: 'studio' | Scope, name: string, prototype?: string): string {
131
+ if (scope === 'studio') return DIRECTORY.includes(name) ? `${PREFIX}${name}` : `${PREFIX}studio:${name}`;
132
+ if (!PROJECT_NAMES.includes(name)) return `${clientPrefix(scope.client)}${name}`;
133
+ const project = `${projectPrefix(scope)}${name}`;
134
+ return PER_PROTOTYPE.includes(name) ? `${project}:${part(isKebab, prototype, 'prototype')}` : project;
135
+ }
136
+
137
+ /**
138
+ * What every key of a client starts with, or of one of its projects when the
139
+ * scope names one: what `delPrefix` takes to sweep that scope clean, whatever
140
+ * index names its keys or not. Built by the parts keyOf builds keys from, and
141
+ * ending in the `:` no part can hold, so `acme` never reaches `acme-two` and
142
+ * no client reaches the studio's keys. Never the local store's client: its
143
+ * records are this machine's own, and nothing archives or reuses it.
144
+ */
145
+ export function sweepPrefix(scope: Scope): string {
146
+ if (scope.client === LOCAL_CLIENT) throw new Error('Not a scope to sweep: the local store’s records stay.');
147
+ return scope.project === undefined ? clientPrefix(scope.client) : projectPrefix(scope);
148
+ }
57
149
 
58
150
  export type Snapshot = { hashes: Record<string, Record<string, string>>; sets: Record<string, string[]> };
59
151
 
@@ -77,6 +169,12 @@ export function memoryKV(initial: Snapshot = { hashes: {}, sets: {} }, onChange?
77
169
  changed();
78
170
  return 1;
79
171
  },
172
+ hsetnx: async (key, field, value) => {
173
+ if (hashes.get(key)?.has(field)) return false;
174
+ hash(key).set(field, value);
175
+ changed();
176
+ return true;
177
+ },
80
178
  hgetall: async (key) => Object.fromEntries(hashes.get(key) ?? []),
81
179
  hincrby: async (key, field, by) => {
82
180
  const n = Number(hash(key).get(field) ?? 0) + by;
@@ -89,5 +187,17 @@ export function memoryKV(initial: Snapshot = { hashes: {}, sets: {} }, onChange?
89
187
  changed();
90
188
  },
91
189
  smembers: async (key) => [...(sets.get(key) ?? [])],
190
+ del: async (key) => {
191
+ const had = hashes.delete(key);
192
+ if (sets.delete(key) || had) changed();
193
+ },
194
+ delPrefix: async (prefix) => {
195
+ const gone = [...hashes.keys(), ...sets.keys()].filter((k) => k.startsWith(prefix));
196
+ for (const key of gone) {
197
+ hashes.delete(key);
198
+ sets.delete(key);
199
+ }
200
+ if (gone.length) changed();
201
+ },
92
202
  };
93
203
  }
@@ -0,0 +1,39 @@
1
+ import type { Scope } from './review/model.ts';
2
+ /** A file's bytes as the store hands them, and the type it was stored with. */
3
+ export type StoredFile = {
4
+ body: ReadableStream<Uint8Array> | Bytes;
5
+ type: string;
6
+ };
7
+ type Bytes = Uint8Array<ArrayBuffer>;
8
+ export interface Files {
9
+ /** Writes a file that is not there yet. A build's paths are never overwritten. */
10
+ put(path: string, body: Bytes, type: string): Promise<void>;
11
+ /** The file and its type, or null when there is none. */
12
+ get(path: string): Promise<StoredFile | null>;
13
+ /** Every path that starts with `prefix`. A scope's prefix ends in `/`, so `c/acme/` never lists `c/acme-co/`. */
14
+ list(prefix: string): Promise<string[]>;
15
+ /** Removes these paths; one that is not there is no error. */
16
+ remove(paths: string[]): Promise<void>;
17
+ }
18
+ export declare const isBuildId: (v: unknown) => v is string;
19
+ /**
20
+ * Removes every file under a prefix, in the store's batches of 1,000, and
21
+ * says how many there were. Takes a prefix one of the functions below built,
22
+ * so it never reaches past its scope.
23
+ */
24
+ export declare function clearPrefix(files: Files, prefix: string): Promise<number>;
25
+ /** Removes these paths, in the store's batches of 1,000. */
26
+ export declare function removeAll(files: Files, paths: string[]): Promise<void>;
27
+ /** Every file of one client, every project and build: what archiving the client removes. Ends in `/`. */
28
+ export declare function clientPrefix(client: string): string;
29
+ /** Every build of one project, ending in `/`, so `c/acme/rebrand/` never lists `c/acme/rebrand-2/`. */
30
+ export declare function projectPrefix(scope: Scope): string;
31
+ /** The folder of one build of a project, ending in `/`. */
32
+ export declare function buildPrefix(scope: Scope, build: string): string;
33
+ /** One file of one build: `c/<client>/<project>/<build>/<file>`, with `file` given as its segments. */
34
+ export declare function filePath(scope: Scope, build: string, file: readonly string[]): string;
35
+ /** The type a built file is stored with, from its extension. */
36
+ export declare function typeOf(path: string): string;
37
+ /** The files port in memory, for tests and for a studio run on one machine. */
38
+ export declare function memoryFiles(): Files;
39
+ export {};
@@ -0,0 +1,107 @@
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.js';
8
+ /**
9
+ * A build's id: random, lower-case letters and digits, and never used twice
10
+ * for the life of the store, so a path never comes back with other bytes.
11
+ */
12
+ const BUILD_ID = /^[a-z0-9]{16,64}$/;
13
+ export const isBuildId = (v) => typeof v === 'string' && BUILD_ID.test(v);
14
+ function checked(ok, what, v) {
15
+ // A bad part is a bug in the caller, never input to answer: fail closed before any store call.
16
+ if (!ok)
17
+ throw new Error(`Not a file path: ${what} is ${JSON.stringify(v)}.`);
18
+ }
19
+ /** A file inside a build, as a list of path segments, each one `isSegment`: the rule the studio gate reads a path by. */
20
+ const isFile = (segments) => segments.length > 0 && segments.every(isSegment);
21
+ /**
22
+ * Removes every file under a prefix, in the store's batches of 1,000, and
23
+ * says how many there were. Takes a prefix one of the functions below built,
24
+ * so it never reaches past its scope.
25
+ */
26
+ export async function clearPrefix(files, prefix) {
27
+ const paths = await files.list(prefix);
28
+ await removeAll(files, paths);
29
+ return paths.length;
30
+ }
31
+ /** Removes these paths, in the store's batches of 1,000. */
32
+ export async function removeAll(files, paths) {
33
+ for (let i = 0; i < paths.length; i += 1000)
34
+ await files.remove(paths.slice(i, i + 1000));
35
+ }
36
+ /** Every file of one client, every project and build: what archiving the client removes. Ends in `/`. */
37
+ export function clientPrefix(client) {
38
+ checked(isClientSlug(client), 'client', client);
39
+ return `c/${client}/`;
40
+ }
41
+ /** Every build of one project, ending in `/`, so `c/acme/rebrand/` never lists `c/acme/rebrand-2/`. */
42
+ export function projectPrefix(scope) {
43
+ checked(isClientSlug(scope.client), 'client', scope.client);
44
+ checked(isProjectSlug(scope.project), 'project', scope.project);
45
+ return `c/${scope.client}/${scope.project}/`;
46
+ }
47
+ /** The folder of one build of a project, ending in `/`. */
48
+ export function buildPrefix(scope, build) {
49
+ const prefix = projectPrefix(scope);
50
+ checked(isBuildId(build), 'build', build);
51
+ return `${prefix}${build}/`;
52
+ }
53
+ /** One file of one build: `c/<client>/<project>/<build>/<file>`, with `file` given as its segments. */
54
+ export function filePath(scope, build, file) {
55
+ checked(isFile(file), 'file', file);
56
+ return `${buildPrefix(scope, build)}${file.join('/')}`;
57
+ }
58
+ const TYPES = {
59
+ html: 'text/html; charset=utf-8',
60
+ js: 'text/javascript; charset=utf-8',
61
+ mjs: 'text/javascript; charset=utf-8',
62
+ css: 'text/css; charset=utf-8',
63
+ json: 'application/json',
64
+ webmanifest: 'application/manifest+json',
65
+ map: 'application/json',
66
+ md: 'text/markdown; charset=utf-8',
67
+ txt: 'text/plain; charset=utf-8',
68
+ xml: 'application/xml',
69
+ pdf: 'application/pdf',
70
+ svg: 'image/svg+xml',
71
+ png: 'image/png',
72
+ jpg: 'image/jpeg',
73
+ jpeg: 'image/jpeg',
74
+ gif: 'image/gif',
75
+ webp: 'image/webp',
76
+ avif: 'image/avif',
77
+ ico: 'image/x-icon',
78
+ woff2: 'font/woff2',
79
+ woff: 'font/woff',
80
+ ttf: 'font/ttf',
81
+ otf: 'font/otf',
82
+ mp4: 'video/mp4',
83
+ webm: 'video/webm',
84
+ wasm: 'application/wasm',
85
+ };
86
+ /** The type a built file is stored with, from its extension. */
87
+ export function typeOf(path) {
88
+ const ext = path.match(/\.([a-z0-9]+)$/i)?.[1]?.toLowerCase() ?? '';
89
+ return Object.hasOwn(TYPES, ext) ? TYPES[ext] : 'application/octet-stream';
90
+ }
91
+ /** The files port in memory, for tests and for a studio run on one machine. */
92
+ export function memoryFiles() {
93
+ const files = new Map();
94
+ return {
95
+ put: async (path, body, type) => {
96
+ if (files.has(path))
97
+ throw new Error(`${path} exists; a build's paths are never overwritten.`);
98
+ files.set(path, { body, type });
99
+ },
100
+ get: async (path) => files.get(path) ?? null,
101
+ list: async (prefix) => [...files.keys()].filter((p) => p.startsWith(prefix)).sort(),
102
+ remove: async (paths) => {
103
+ for (const p of paths)
104
+ files.delete(p);
105
+ },
106
+ };
107
+ }
@@ -1,8 +1,11 @@
1
1
  /**
2
- * First path segments the hub owns, never a prototype: the walkthroughs,
3
- * the review API, and the shared bundle.
2
+ * Never a project's or a prototype's slug: every first path segment the gate
3
+ * answers itself (the link path, the review API, the gate's font, the built
4
+ * prototypes, the shared bundle, the walkthroughs, the hub's own top files).
4
5
  */
5
- export declare const RESERVED: readonly ['guide', 'api', 'assets'];
6
+ export declare const RESERVED: readonly ['invite', 'api', '_gate', '_p', 'assets', 'guide', 'index.html', 'favicon.svg'];
7
+ /** Never a client's slug: the studio's own host, and the names a host is often given. */
8
+ export declare const RESERVED_CLIENTS: readonly ['studio', 'www', 'api'];
6
9
  /**
7
10
  * Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
8
11
  * Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
@@ -13,17 +16,63 @@ export declare const ALL_FILE = "_p/all.js";
13
16
  export declare const entryFile: (slug: string) => string;
14
17
  /** Whether `name` can be a prototype's folder: kebab-case and not one of the hub's own paths. */
15
18
  export declare const isPrototypeSlug: (name: unknown) => name is string;
19
+ /** A project is a first path segment too, below its client's host, so it follows the prototype's rule. */
20
+ export declare const isProjectSlug: typeof isPrototypeSlug;
21
+ /** Whether `name` can be a client: kebab-case and not one of the studio's own host names. */
22
+ export declare const isClientSlug: (name: unknown) => name is string;
23
+ export declare const isSegment: (s: string) => boolean;
24
+ /** The one-time link path, matched on the path as sent, as the gate always has. */
25
+ export declare const LINK_PATH: RegExp;
26
+ /** What the gate pages load before sign-in, at the host root: the hub's face and the favicon. */
27
+ export declare const GATE_FONT = "/_gate/geist.woff2";
16
28
  /**
17
- * Which prototype a request path belongs to, for the gate: the name a
18
- * prototype path carries, or null for a hub path (`/`, or a first segment in
19
- * HUB_ROOT). The first segment, decoded, must be one of those, `_p`, or a
20
- * prototype slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`)
21
- * belongs to `''`, refused like an internal prototype. A path under `/_p/`
22
- * always belongs to one, so its name may be no slug at all (`all.js`), which
23
- * no audience makes client-facing. A path that could mean more than one file
24
- * belongs to `''` too.
29
+ * The host proof `client add` asks a new client's host for: the studio gate
30
+ * answers a GET of it with the proof's answer only when the store holds that
31
+ * ask for the host's client. Otherwise it is a hub path like any other.
25
32
  */
26
- export declare function prototypeOfPath(pathname: string): string | null;
33
+ export declare const PROOF_PATH: RegExp;
34
+ export declare const proofPath: (ask: string) => string;
35
+ /**
36
+ * Where a request on a studio site goes:
37
+ *
38
+ * - `none`: nothing; the gate's plain 404, whoever asks.
39
+ * - `link`: `/invite/<token>`, at the host root only.
40
+ * - `asset`: the gate's font and the favicon, served before sign-in.
41
+ * - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
42
+ * - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
43
+ * - `file`: a file of a project's build, as its segments ([] for the project's own page), and
44
+ * the prototype it belongs to (`prototypeOfParts`), read below the project.
45
+ */
46
+ export type Where = {
47
+ kind: 'none';
48
+ } | {
49
+ kind: 'link';
50
+ token: string;
51
+ } | {
52
+ kind: 'asset';
53
+ } | {
54
+ kind: 'hub';
55
+ } | {
56
+ kind: 'api';
57
+ project: string | null;
58
+ } | {
59
+ kind: 'file';
60
+ project: string;
61
+ file: string[];
62
+ prototype: string | null;
63
+ };
64
+ /** Where a request goes, and the host it was sent to: lower case, no port, or '' when it is no host name. */
65
+ export type Route = {
66
+ host: string;
67
+ } & Where;
68
+ /** A host name as a lookup key: lower case, no port, no trailing dot; '' for anything that is not one. */
69
+ export declare function hostName(host: string): string;
70
+ /**
71
+ * The one place a studio site reads a request's path. It fails closed: a
72
+ * 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
73
+ * codename): this names the host, never the client.
74
+ */
75
+ export declare function routeOf(host: string, pathname: string): Route;
27
76
  /**
28
77
  * Whether a built prototype's entry is there for this person, asked with a
29
78
  * HEAD before it is imported. Not there: a 404 (the gate's answer for a