@milesroxas/design-hub 0.8.0 → 0.9.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 (64) hide show
  1. package/README.md +7 -2
  2. package/core/prototype.test.ts +83 -0
  3. package/core/prototype.ts +84 -0
  4. package/core/review/model.ts +20 -2
  5. package/core/review/policy.test.ts +4 -2
  6. package/core/review/policy.ts +4 -2
  7. package/core/review/service.test.ts +200 -16
  8. package/core/review/service.ts +91 -5
  9. package/core/review/store.ts +10 -1
  10. package/dist/core/prototype.d.ts +35 -0
  11. package/dist/core/prototype.js +84 -0
  12. package/dist/core/review/model.d.ts +13 -2
  13. package/dist/core/review/model.js +13 -0
  14. package/dist/core/review/policy.d.ts +1 -1
  15. package/dist/core/review/policy.js +1 -0
  16. package/dist/core/review/service.d.ts +24 -2
  17. package/dist/core/review/service.js +81 -6
  18. package/dist/core/review/store.d.ts +10 -1
  19. package/dist/core/review/store.js +10 -1
  20. package/dist/scripts/comments.js +4 -3
  21. package/dist/scripts/lib/cdp.js +6 -1
  22. package/dist/scripts/lib/export-smoke.d.ts +18 -0
  23. package/dist/scripts/lib/export-smoke.js +73 -0
  24. package/dist/scripts/new.js +5 -2
  25. package/dist/scripts/release-check.d.ts +5 -3
  26. package/dist/scripts/release-check.js +17 -4
  27. package/dist/scripts/share.d.ts +2 -2
  28. package/dist/server/api.js +8 -0
  29. package/dist/server/session.d.ts +5 -1
  30. package/dist/server/session.js +39 -21
  31. package/dist/vercel/proxy.js +44 -8
  32. package/dist/vite/boundary.d.ts +93 -0
  33. package/dist/vite/boundary.js +218 -0
  34. package/dist/vite/css-boundary.d.ts +39 -0
  35. package/dist/vite/css-boundary.js +167 -0
  36. package/dist/vite/kit.js +15 -6
  37. package/dist/vite/leaks.d.ts +37 -0
  38. package/dist/vite/leaks.js +331 -0
  39. package/dist/vite/plugin.js +2 -1
  40. package/dist/vite/registry.d.ts +15 -4
  41. package/dist/vite/registry.js +531 -27
  42. package/dist/vite/settle.d.ts +27 -0
  43. package/dist/vite/settle.js +185 -0
  44. package/package.json +4 -3
  45. package/shell/ClientViewNote.tsx +18 -0
  46. package/shell/Guide.tsx +17 -2
  47. package/shell/Index.tsx +257 -25
  48. package/shell/audience.test.ts +63 -0
  49. package/shell/audience.ts +47 -0
  50. package/shell/hub.ts +36 -0
  51. package/shell/main.tsx +39 -5
  52. package/shell/registry.ts +28 -7
  53. package/shell/registry.virtual.d.ts +12 -4
  54. package/shell/review/Pins.tsx +8 -6
  55. package/shell/review/Review.tsx +8 -4
  56. package/shell/review/ThreadCard.tsx +6 -14
  57. package/shell/review/client.ts +16 -4
  58. package/shell/review/review.css +37 -24
  59. package/shell/shell.css +50 -3
  60. package/shell/system.css +5 -0
  61. package/skills/miles-deploy/SKILL.md +17 -0
  62. package/skills/miles-new-feature/SKILL.md +1 -1
  63. package/skills/miles-prototype/SKILL.md +14 -1
  64. package/skills/miles-review/SKILL.md +1 -1
@@ -0,0 +1,331 @@
1
+ /// <reference types="node" />
2
+ // The output-side check, a backstop for whole-file inlining and nothing
3
+ // more: after the bundle is settled (vite/settle.ts), no built file outside
4
+ // `_p/<slug>/` may be a whole file of `prototypes/<slug>/`, however it got
5
+ // there. The input checks (vite/registry.ts, vite/css-boundary.ts) refuse
6
+ // each way of reaching a prototype's file that someone has thought of; this
7
+ // one reads what was written, for the forms Vite 8.3.5 writes a whole file in
8
+ // (`vite:asset`; the Vite range is pinned for this):
9
+ //
10
+ // - the built file is byte for byte the source: an asset copy (`?url`, an
11
+ // emitted file);
12
+ // - a `data:` URI in a built file decodes to exactly the source: base64
13
+ // (`assetToDataURL`), or the URL-encoded SVG form (`svgToDataURL`, which
14
+ // Vite does not export: its six replacements are repeated here, and the
15
+ // LEAKS row for it fails if Vite changes them);
16
+ // - a string in a built script is exactly the source's whole text (`?raw`, an
17
+ // inlined text): every string read with the parser Vite exports
18
+ // (`parseSync`), so escaping and quote style never hide it.
19
+ //
20
+ // Nothing else counts: a line, a CSS rule or any other part of a file is
21
+ // never looked for, so two prototypes that share a sentence, a rule or an SVG
22
+ // path build. A match is legitimate when the reader's owner (the hub and the
23
+ // kit, for a file outside `_p/`; that prototype, for one under `_p/<slug>/`)
24
+ // has a source with the same whole content: the image in `kit/` and in two
25
+ // prototypes, the prototype font that is byte for byte Geist.
26
+ //
27
+ // Sources: every file under each prototype's folder on its real path
28
+ // (symlinks are skipped: a symlink's target is checked as its real owner's),
29
+ // except a prototype's own node_modules, of which only the files the build
30
+ // read count; and every file the build read from outside `prototypes/`, for
31
+ // the hub's and the kit's own content.
32
+ //
33
+ // It is linear: each built file is read once, for its hash, the hash of each
34
+ // data URI it holds and the hash of each string over MIN in a script; each
35
+ // source is hashed once, by a streamed read, and only when its size is one a
36
+ // built form has (most sources are never opened). Two rules keep a file it
37
+ // cannot use from failing the build with a wrong message: a source it cannot
38
+ // open (no permission) is skipped, since Vite could not have read it either;
39
+ // a source larger than LARGE is compared only against whole built files, as
40
+ // Vite inlines a file into a script or a data URI only by reading it into
41
+ // memory.
42
+ import { createHash } from 'node:crypto';
43
+ import { closeSync, existsSync, openSync, readdirSync, readFileSync, readSync, statSync, } from 'node:fs';
44
+ import { join } from 'node:path';
45
+ import { parseSync, Visitor } from 'vite';
46
+ import { PROTOTYPE_DIR } from '../core/prototype.js';
47
+ import { real, relativeTo } from './boundary.js';
48
+ /**
49
+ * The smallest source that counts, in bytes: a shorter file (an empty
50
+ * `.gitkeep`, `{"a":1}`) is never looked for, as it matches a short string
51
+ * by chance.
52
+ */
53
+ export const MIN = 40;
54
+ /** A source larger than this, in bytes, is only ever compared with whole built files. */
55
+ const LARGE = 64 * 1024 * 1024;
56
+ const FORMS = {
57
+ bytes: 'as a copy of its bytes',
58
+ base64: 'as a base64 data URI',
59
+ svg: 'as a URL-encoded SVG data URI',
60
+ string: 'as a string',
61
+ };
62
+ const hash = (data) => createHash('sha256').update(data).digest('hex');
63
+ const decoder = new TextDecoder('utf-8', { fatal: true });
64
+ /** A file's text, or null when it is not UTF-8 text. */
65
+ function textOf(bytes) {
66
+ if (bytes.includes(0))
67
+ return null;
68
+ try {
69
+ return decoder.decode(bytes);
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ }
75
+ /** What Vite writes for an SVG it inlines as text (`svgToDataURL`), or null when it writes base64 instead. */
76
+ export function svgDataUrlPayload(text) {
77
+ if (text.includes('<text') || text.includes('<foreignObject') || /"[^"']*'[^"]*"|'[^'"]*"[^']*'/.test(text))
78
+ return null;
79
+ return text
80
+ .trim()
81
+ .replaceAll(/>\s+</g, '><')
82
+ .replaceAll('"', "'")
83
+ .replaceAll('%', '%25')
84
+ .replaceAll('#', '%23')
85
+ .replaceAll('<', '%3c')
86
+ .replaceAll('>', '%3e')
87
+ .replaceAll(/\s+/g, '%20');
88
+ }
89
+ const SVG_HEAD = 'image/svg+xml';
90
+ /** The whole content a `data:` URI holds: its decoded bytes, or the payload of Vite's URL-encoded SVG form. */
91
+ export function dataUriForms(uri) {
92
+ const comma = uri.indexOf(',');
93
+ if (!uri.startsWith('data:') || comma < 0)
94
+ return [];
95
+ const head = uri.slice(5, comma);
96
+ const payload = uri.slice(comma + 1);
97
+ if (head.endsWith(';base64')) {
98
+ const bytes = Buffer.from(payload, 'base64');
99
+ return [{ form: 'base64', hash: hash(bytes), size: bytes.length }];
100
+ }
101
+ return head === SVG_HEAD ? [{ form: 'svg', hash: hash(payload), size: payload.length }] : [];
102
+ }
103
+ /** Every `data:` URI in a text (a stylesheet, or one string of a script): up to its closing quote, or without one up to `)` or a space. */
104
+ export function dataUrisIn(text) {
105
+ const found = [];
106
+ for (let at = text.indexOf('data:'); at >= 0; at = text.indexOf('data:', at + 5)) {
107
+ const quote = text[at - 1];
108
+ const rest = text.slice(at);
109
+ const end = quote === '"' || quote === "'" ? rest.indexOf(quote) : rest.search(/[)\s"']/);
110
+ found.push(end < 0 ? rest : rest.slice(0, end));
111
+ }
112
+ return found;
113
+ }
114
+ /** Every string a script holds, decoded, at least MIN bytes long: literals and template parts. Null when it does not parse. */
115
+ function stringsOf(code, fileName) {
116
+ const parsed = parseSync(fileName, code, { lang: 'js', sourceType: 'module' });
117
+ if (parsed.errors.length)
118
+ return null;
119
+ const found = [];
120
+ const keep = (s) => {
121
+ if (s && Buffer.byteLength(s) >= MIN)
122
+ found.push(s);
123
+ };
124
+ new Visitor({
125
+ Literal(node) {
126
+ if (typeof node.value === 'string')
127
+ keep(node.value);
128
+ },
129
+ TemplateElement(node) {
130
+ keep(node.value.cooked);
131
+ },
132
+ }).visit(parsed.program);
133
+ return found;
134
+ }
135
+ /** The owner of a built file by where it was written: `_p/<slug>/` is that prototype's, anything else the hub's. */
136
+ const ownerAt = (fileName) => {
137
+ const [top, slug, ...rest] = fileName.split('/');
138
+ return top === PROTOTYPE_DIR && rest.length ? slug : null;
139
+ };
140
+ const unparsed = (fileName) => `${fileName} does not parse, so the build cannot read it for a prototype's content.`;
141
+ function read({ fileName, source }) {
142
+ const bytes = Buffer.from(source);
143
+ const found = [{ form: 'bytes', hash: hash(bytes), size: bytes.length }];
144
+ const text = typeof source === 'string' ? source : textOf(bytes);
145
+ if (text === null)
146
+ return { fileName, owner: ownerAt(fileName), found };
147
+ const script = /\.[cm]?js$/.test(fileName);
148
+ if (!script && text.includes('data:'))
149
+ found.push(...dataUrisIn(text).flatMap(dataUriForms));
150
+ if (script) {
151
+ const strings = stringsOf(text, fileName);
152
+ if (strings === null)
153
+ return unparsed(fileName);
154
+ for (const s of strings) {
155
+ const utf8 = Buffer.from(s);
156
+ // The whole string as a data URI (an SVG one holds quotes), and one anywhere inside it: `?inline`
157
+ // CSS holds one in a longer string.
158
+ const uris = new Set([...(s.startsWith('data:') ? [s] : []), ...dataUrisIn(s)]);
159
+ found.push({ form: 'string', hash: hash(utf8), size: utf8.length }, ...[...uris].flatMap(dataUriForms));
160
+ }
161
+ }
162
+ return { fileName, owner: ownerAt(fileName), found };
163
+ }
164
+ /** Every file under a folder, symlinks and node_modules skipped, and a folder it cannot list (no permission) skipped too. */
165
+ function walk(dir) {
166
+ let entries;
167
+ try {
168
+ entries = readdirSync(dir, { withFileTypes: true });
169
+ }
170
+ catch {
171
+ return [];
172
+ }
173
+ return entries.flatMap((d) => {
174
+ const path = join(dir, d.name);
175
+ if (d.isSymbolicLink() || d.name === 'node_modules')
176
+ return [];
177
+ return d.isDirectory() ? walk(path) : d.isFile() ? [path] : [];
178
+ });
179
+ }
180
+ /** A file's size, or null when it is not a file that can be stat'ed. */
181
+ function sizeOf(file) {
182
+ try {
183
+ const s = statSync(file);
184
+ return s.isFile() ? s.size : null;
185
+ }
186
+ catch {
187
+ return null;
188
+ }
189
+ }
190
+ const CHUNK = 1024 * 1024;
191
+ /** A file's hash, read in chunks so a large file is never held whole; null when it cannot be read. */
192
+ function streamedHash(file) {
193
+ let fd;
194
+ try {
195
+ fd = openSync(file, 'r');
196
+ }
197
+ catch {
198
+ return null;
199
+ }
200
+ try {
201
+ const h = createHash('sha256');
202
+ const buffer = Buffer.allocUnsafe(CHUNK);
203
+ for (let n = readSync(fd, buffer); n > 0; n = readSync(fd, buffer))
204
+ h.update(buffer.subarray(0, n));
205
+ return h.digest('hex');
206
+ }
207
+ catch {
208
+ return null;
209
+ }
210
+ finally {
211
+ closeSync(fd);
212
+ }
213
+ }
214
+ /** The payload Vite would write for an SVG source, or null when it writes base64 or the file cannot be read. */
215
+ function svgPayloadOf(file) {
216
+ try {
217
+ const text = textOf(readFileSync(file));
218
+ return text === null ? null : svgDataUrlPayload(text);
219
+ }
220
+ catch {
221
+ return null;
222
+ }
223
+ }
224
+ /** Every source by real path, with its owner: each prototype folder's files, and the files the build read (`read`). */
225
+ function sourcesOf(prototypes, owner, inputs) {
226
+ const walked = existsSync(prototypes) ? walk(prototypes).map(real) : [];
227
+ const read = new Set(inputs.map((id) => real(id.replace(/^\0/, '').replace(/\?.*$/, ''))));
228
+ const sources = new Map();
229
+ for (const file of [...walked, ...read])
230
+ if (!sources.has(file))
231
+ sources.set(file, owner(file));
232
+ return { sources, read };
233
+ }
234
+ const contentKey = (h) => `c:${h}`;
235
+ const svgKey = (h) => `s:${h}`;
236
+ /** The content of a source the build read: the only sources a string is compared with. */
237
+ const readKey = (h) => `r:${h}`;
238
+ const keyOf = (f) => (f.form === 'svg' ? svgKey(f.hash) : contentKey(f.hash));
239
+ /**
240
+ * The key a built form names its source by. A string only names a source the
241
+ * build read: equal text in a file nothing reads (a sentence in alpha's
242
+ * tagline.txt and in beta's JSX) is a coincidence, not a leak. A data URI or
243
+ * a copy names any source, since Vite inlines some files without a hook seeing them.
244
+ */
245
+ const sourceKeyOf = (f) => (f.form === 'string' ? readKey(f.hash) : keyOf(f));
246
+ /** The sizes the built forms have: whole built files, and what is inlined (a data URI's bytes, a string); and whether any SVG data URI is there. */
247
+ function sizesOf(reads) {
248
+ const copies = new Set();
249
+ const inlined = new Set();
250
+ let svg = false;
251
+ for (const f of reads.flatMap((r) => r.found))
252
+ if (f.form === 'bytes')
253
+ copies.add(f.size);
254
+ else if (f.form === 'svg')
255
+ svg = true;
256
+ else
257
+ inlined.add(f.size);
258
+ return { copies, inlined, svg };
259
+ }
260
+ /** The keys one source holds, read only when a built form could be it: its content's hash, and its SVG payload's. */
261
+ function keysOf(file, size, sizes) {
262
+ const keys = [];
263
+ const small = size <= LARGE;
264
+ if (sizes.copies.has(size) || (small && sizes.inlined.has(size))) {
265
+ const h = streamedHash(file);
266
+ if (h)
267
+ keys.push(contentKey(h));
268
+ }
269
+ const payload = sizes.svg && small && file.endsWith('.svg') ? svgPayloadOf(file) : null;
270
+ if (payload)
271
+ keys.push(svgKey(hash(payload)));
272
+ return keys;
273
+ }
274
+ function holdersOf(sources, read, reads) {
275
+ const sizes = sizesOf(reads);
276
+ const holders = new Map();
277
+ for (const [file, o] of sources) {
278
+ const size = sizeOf(file);
279
+ if (size === null || size < MIN)
280
+ continue;
281
+ const keys = keysOf(file, size, sizes);
282
+ if (read.has(file))
283
+ keys.push(...keys.filter((k) => k.startsWith('c:')).map((k) => readKey(k.slice(2))));
284
+ for (const key of keys) {
285
+ const by = holders.get(key) ?? new Map();
286
+ if (!by.has(o))
287
+ by.set(o, file);
288
+ holders.set(key, by);
289
+ }
290
+ }
291
+ return holders;
292
+ }
293
+ /** The error for a built file that holds a prototype's file whole outside its folder. */
294
+ export const leaked = (fileName, file, slug, form) => `${fileName} holds ${file} ${FORMS[form]}, which belongs to prototypes/${slug}/ and may be written only under ${PROTOTYPE_DIR}/${slug}/. A prototype's files are read only by that prototype: copy the file into the one that reads it, or move it into kit/.`;
295
+ /**
296
+ * Every built file that holds a whole file of a prototype other than its own
297
+ * owner (for a file outside `_p/`, any prototype's), as problems naming the
298
+ * built file and the source, unless its owner has a source with the same
299
+ * content. `inputs` is every file the bundle read (every prototype file
300
+ * the build loaded, every chunk's modules, every asset's original files),
301
+ * recorded before settle moved anything; a string is compared only with
302
+ * those. A built script that does not parse is a problem, never skipped.
303
+ */
304
+ export function leaks(root, prototypes, owner, inputs, written) {
305
+ const all = written.map(read);
306
+ const problems = all.filter((r) => typeof r === 'string');
307
+ const reads = all.filter((r) => typeof r !== 'string');
308
+ const { sources, read: readByBuild } = sourcesOf(prototypes, owner, inputs);
309
+ const holders = holdersOf(sources, readByBuild, reads);
310
+ for (const r of reads)
311
+ problems.push(...leaksIn(root, r, holders));
312
+ return problems;
313
+ }
314
+ /** What one built file holds of another owner: each source named once. */
315
+ function leaksIn(root, r, holders) {
316
+ const problems = [];
317
+ const named = new Set();
318
+ for (const f of r.found) {
319
+ // Legitimate when the reader's owner holds the content in any source, read or not.
320
+ const by = holders.get(keyOf(f));
321
+ if (!by || by.has(r.owner) || by.has(null))
322
+ continue;
323
+ for (const [slug, file] of holders.get(sourceKeyOf(f)) ?? []) {
324
+ if (slug === null || named.has(file))
325
+ continue;
326
+ named.add(file);
327
+ problems.push(leaked(r.fileName, relativeTo(root, file), slug, f.form));
328
+ }
329
+ }
330
+ return problems;
331
+ }
@@ -52,8 +52,9 @@ function gateFont() {
52
52
  export function designHub({ root }) {
53
53
  return [
54
54
  react(),
55
- kitResolver(join(root, 'kit'), PACKAGE_KIT),
55
+ // The boundary resolves first, so it sees every import, `@kit/*` included.
56
56
  registry(root),
57
+ kitResolver(join(root, 'kit'), PACKAGE_KIT),
57
58
  lapse(root),
58
59
  markdownDocs(),
59
60
  devReview(),
@@ -1,7 +1,18 @@
1
- import type { Plugin } from 'vite';
2
- /** The prototype folders: kebab-case, with a README. `guide` is the walkthrough route, never a prototype. */
1
+ import { type Plugin } from 'vite';
2
+ /** The prototype folders: kebab-case, with a README, and never one of the hub's own paths (core/prototype.ts). */
3
3
  export declare const prototypeSlugs: (prototypes: string) => string[];
4
4
  /** The features with a walkthrough at docs/sprints/<feature>/guide.md. */
5
5
  export declare const guideFeatures: (sprints: string) => string[];
6
- export declare function registrySource(root: string): string;
7
- export declare function registry(root: string): Plugin;
6
+ /** One prototype's entry: its README text, and a loader for its code when it has an index.tsx. */
7
+ export declare function entrySource(root: string, slug: string): string;
8
+ export type Mode = 'serve' | 'build';
9
+ /**
10
+ * The registry module: `allSlugs()` and `entryOf(slug)`, both async, and
11
+ * `guideDocs`. Served, a map of every entry. Built, no slug: `_p/all.js` and
12
+ * `_p/<slug>/entry.js` are fetched by path from `base`, so the gate decides
13
+ * who gets each.
14
+ */
15
+ export declare function registrySource(root: string, mode?: Mode, base?: string): string;
16
+ /** The source of Studio's list of every slug, `_p/all.js`. */
17
+ export declare const allSource: (slugs: string[]) => string;
18
+ export declare function registry(root: string): Plugin[];