@milesroxas/design-hub 0.8.1 → 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.
- package/README.md +7 -2
- package/core/prototype.test.ts +83 -0
- package/core/prototype.ts +84 -0
- package/core/review/model.ts +7 -0
- package/core/review/policy.test.ts +4 -2
- package/core/review/policy.ts +4 -2
- package/core/review/service.test.ts +144 -4
- package/core/review/service.ts +70 -2
- package/core/review/store.ts +6 -0
- package/dist/core/prototype.d.ts +35 -0
- package/dist/core/prototype.js +84 -0
- package/dist/core/review/model.d.ts +2 -0
- package/dist/core/review/model.js +6 -0
- package/dist/core/review/policy.d.ts +1 -1
- package/dist/core/review/policy.js +1 -0
- package/dist/core/review/service.d.ts +23 -1
- package/dist/core/review/service.js +62 -3
- package/dist/core/review/store.d.ts +6 -0
- package/dist/core/review/store.js +6 -0
- package/dist/scripts/lib/cdp.js +6 -1
- package/dist/scripts/lib/export-smoke.d.ts +18 -0
- package/dist/scripts/lib/export-smoke.js +73 -0
- package/dist/scripts/new.js +5 -2
- package/dist/scripts/release-check.d.ts +5 -3
- package/dist/scripts/release-check.js +17 -4
- package/dist/scripts/share.d.ts +2 -2
- package/dist/server/api.js +8 -0
- package/dist/server/session.d.ts +5 -1
- package/dist/server/session.js +39 -21
- package/dist/vercel/proxy.js +44 -8
- package/dist/vite/boundary.d.ts +93 -0
- package/dist/vite/boundary.js +218 -0
- package/dist/vite/css-boundary.d.ts +39 -0
- package/dist/vite/css-boundary.js +167 -0
- package/dist/vite/kit.js +15 -6
- package/dist/vite/leaks.d.ts +37 -0
- package/dist/vite/leaks.js +331 -0
- package/dist/vite/plugin.js +2 -1
- package/dist/vite/registry.d.ts +15 -4
- package/dist/vite/registry.js +531 -27
- package/dist/vite/settle.d.ts +27 -0
- package/dist/vite/settle.js +185 -0
- package/package.json +4 -3
- package/shell/ClientViewNote.tsx +18 -0
- package/shell/Guide.tsx +17 -2
- package/shell/Index.tsx +257 -25
- package/shell/audience.test.ts +63 -0
- package/shell/audience.ts +47 -0
- package/shell/hub.ts +36 -0
- package/shell/main.tsx +39 -5
- package/shell/registry.ts +28 -7
- package/shell/registry.virtual.d.ts +12 -4
- package/shell/review/client.ts +16 -4
- package/shell/review/review.css +2 -2
- package/shell/shell.css +50 -3
- package/shell/system.css +5 -0
- package/skills/miles-deploy/SKILL.md +17 -0
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-prototype/SKILL.md +14 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// The prototype boundary inside a stylesheet. `@import` and `composes`
|
|
3
|
+
// inline another file's CSS, which leaves no built file behind for the
|
|
4
|
+
// end-of-build check (vite/settle.ts) to see, and Vite resolves `url()`
|
|
5
|
+
// itself. So the plugin adds this to Vite's own postcss run, where it sees
|
|
6
|
+
// every stylesheet whoever owns it (a prototype's, the kit's, the hub's), in
|
|
7
|
+
// dev and in a build, after `@import` is inlined: each node still carries the
|
|
8
|
+
// file it came from, at any depth. Owners are decided on real paths, so a
|
|
9
|
+
// symlink into another prototype's folder is that prototype's.
|
|
10
|
+
//
|
|
11
|
+
// Only values that are references count: `@import`, `url()`, the strings in
|
|
12
|
+
// `image-set()`, and in a CSS module `composes … from`, `@value … from` and `:import("…")`. A string in `content:` is text.
|
|
13
|
+
// Comments never count: postcss keeps them apart. Each reference is followed
|
|
14
|
+
// as a path and as Vite resolves it (vite/registry.ts hands over Vite's own
|
|
15
|
+
// resolvers), so an alias or a package path into a prototype's folder counts.
|
|
16
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
17
|
+
import { dirname, resolve } from 'node:path';
|
|
18
|
+
import postcss, {} from 'postcss';
|
|
19
|
+
import { crossImport, looseFile, real } from './boundary.js';
|
|
20
|
+
const URL_REF = /url\(\s*(["']?)([^"')]*?)\1\s*\)/g;
|
|
21
|
+
const IMAGE_SET = /(?:-webkit-)?image-set\(([^;]*)\)/g;
|
|
22
|
+
const STRING = /(["'])((?:\\.|(?!\1)[^\\])*)\1/g;
|
|
23
|
+
const FROM = /\bfrom\s+(["'])([^"']+)\1/;
|
|
24
|
+
const IMPORT_SELECTOR = /:import\(\s*(["']?)([^"')]+)\1\s*\)/;
|
|
25
|
+
/** Data, remote and fragment references name no file. */
|
|
26
|
+
const isRef = (ref) => ref !== '' && !/^(#|[a-z][\w+.-]*:)/i.test(ref);
|
|
27
|
+
/** Nor, read as a path, do aliases and package paths: those are the resolver's. */
|
|
28
|
+
const isFile = (ref) => isRef(ref) && !/^[~@]/.test(ref);
|
|
29
|
+
/**
|
|
30
|
+
* A reference unescaped as Vite unescapes it before resolving (`doUrlReplace`
|
|
31
|
+
* in `vite:css`, which Vite does not export): a backslash before any
|
|
32
|
+
* character that is not a letter, digit or `_` is dropped, so
|
|
33
|
+
* `..\/beta\/x.png` is `../beta/x.png`. A hex escape (`\2f`) Vite leaves as
|
|
34
|
+
* written, and so does this: Vite then names no such file.
|
|
35
|
+
*/
|
|
36
|
+
const unescaped = (ref) => ref.replace(/\\(\W)/g, '$1');
|
|
37
|
+
/** The references in a declaration's value, unescaped: `url()` and the strings in `image-set()`. */
|
|
38
|
+
export function valueRefs(value) {
|
|
39
|
+
const urls = [...value.matchAll(URL_REF)].map((m) => m[2]);
|
|
40
|
+
const sets = [...value.matchAll(IMAGE_SET)].flatMap((m) => [...m[1].matchAll(STRING)].map((s) => s[2]));
|
|
41
|
+
return [...urls, ...sets].map(unescaped).filter(isRef);
|
|
42
|
+
}
|
|
43
|
+
/** A URL as Vite reads it before resolving: decoded when it decodes, without its fragment. */
|
|
44
|
+
const asVite = (ref) => {
|
|
45
|
+
let decoded = ref;
|
|
46
|
+
try {
|
|
47
|
+
decoded = decodeURI(ref);
|
|
48
|
+
}
|
|
49
|
+
catch { }
|
|
50
|
+
return decoded.split('#')[0];
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Every file a reference may name: as a path beside its stylesheet (or an
|
|
54
|
+
* absolute path that exists), and as Vite resolves it, aliases and package
|
|
55
|
+
* paths included. Both count, so neither spelling gets round the other.
|
|
56
|
+
*/
|
|
57
|
+
async function targetsOf(ref, from, resolve) {
|
|
58
|
+
const found = [];
|
|
59
|
+
const file = fileOfRef(ref, from);
|
|
60
|
+
if (file)
|
|
61
|
+
found.push(file);
|
|
62
|
+
const resolved = resolve && isRef(ref) ? await resolve(asVite(ref), from) : undefined;
|
|
63
|
+
if (resolved)
|
|
64
|
+
found.push(resolved.replace(/[?#].*$/, ''));
|
|
65
|
+
return found;
|
|
66
|
+
}
|
|
67
|
+
/** The file `@import` names, quoted or as `url()`. */
|
|
68
|
+
export const importRef = (params) => [...valueRefs(params), ...[...params.matchAll(STRING)].map((m) => m[2])][0];
|
|
69
|
+
/**
|
|
70
|
+
* A reference as a file on disk, or null: relative to the file that makes
|
|
71
|
+
* it, or an absolute path that exists (a `/` path that does not is the
|
|
72
|
+
* public folder's).
|
|
73
|
+
*/
|
|
74
|
+
export function fileOfRef(ref, from) {
|
|
75
|
+
const path = ref.split(/[?#]/)[0];
|
|
76
|
+
if (!isFile(path))
|
|
77
|
+
return null;
|
|
78
|
+
if (path.startsWith('/'))
|
|
79
|
+
return existsSync(path) ? path : null;
|
|
80
|
+
return resolve(dirname(from), path);
|
|
81
|
+
}
|
|
82
|
+
/** The file a CSS modules node names: `composes … from`, `@value … from`, or `:import("…")`. */
|
|
83
|
+
export function moduleRef(node) {
|
|
84
|
+
if (node.type === 'decl' && node.prop === 'composes')
|
|
85
|
+
return node.value.match(FROM)?.[2];
|
|
86
|
+
if (node.type === 'atrule' && node.name === 'value')
|
|
87
|
+
return node.params.match(FROM)?.[2];
|
|
88
|
+
if (node.type === 'rule')
|
|
89
|
+
return node.selector.match(IMPORT_SELECTOR)?.[2];
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Every file a CSS module's chain reaches, read from disk, each once:
|
|
94
|
+
* `composes … from`, `@value … from` and `:import("…")`, through every module
|
|
95
|
+
* they name. postcss-modules resolves these before any other plugin sees the
|
|
96
|
+
* sheet, so they are followed here.
|
|
97
|
+
*/
|
|
98
|
+
export async function modulesChain(file, resolve, seen = new Set()) {
|
|
99
|
+
if (seen.has(file) || !existsSync(file))
|
|
100
|
+
return [];
|
|
101
|
+
seen.add(file);
|
|
102
|
+
const refs = [];
|
|
103
|
+
postcss.parse(readFileSync(file, 'utf8'), { from: file }).walk((node) => {
|
|
104
|
+
const ref = moduleRef(node);
|
|
105
|
+
if (ref)
|
|
106
|
+
refs.push(ref);
|
|
107
|
+
});
|
|
108
|
+
const found = [];
|
|
109
|
+
for (const ref of refs)
|
|
110
|
+
for (const target of await targetsOf(ref, file, resolve))
|
|
111
|
+
found.push(target, ...(await modulesChain(target, resolve, seen)));
|
|
112
|
+
return found;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The postcss plugin: for the stylesheet being compiled (the root), a node
|
|
116
|
+
* inlined from another owner's file, or a reference into a prototype the
|
|
117
|
+
* root's owner is not, throws the error that names both files. The kit and
|
|
118
|
+
* the hub own no prototype, so they may reach none.
|
|
119
|
+
*/
|
|
120
|
+
export function cssBoundary(project, owner, isPrototype, resolvers) {
|
|
121
|
+
const ownerOf = (file) => owner(real(file));
|
|
122
|
+
const plugin = () => ({
|
|
123
|
+
postcssPlugin: 'design-hub-css-boundary',
|
|
124
|
+
async Once(root) {
|
|
125
|
+
const rootFile = root.source?.input.file;
|
|
126
|
+
if (!rootFile)
|
|
127
|
+
return;
|
|
128
|
+
// Fail closed: without Vite's resolvers an alias or a package path would go unread.
|
|
129
|
+
const resolve = resolvers();
|
|
130
|
+
if (!resolve)
|
|
131
|
+
throw new Error(`${rootFile}: the stylesheet check ran before Vite resolved its config.`);
|
|
132
|
+
const rootOwner = ownerOf(rootFile);
|
|
133
|
+
const refuse = (reader, target, node) => {
|
|
134
|
+
const to = ownerOf(target);
|
|
135
|
+
if (to === null || (to === rootOwner && isPrototype(to)))
|
|
136
|
+
return;
|
|
137
|
+
const message = isPrototype(to)
|
|
138
|
+
? crossImport(project, real(reader), real(target), ownerOf(reader) !== null)
|
|
139
|
+
: looseFile(project, real(reader), real(target));
|
|
140
|
+
throw node ? node.error(message) : new Error(message);
|
|
141
|
+
};
|
|
142
|
+
const reads = [];
|
|
143
|
+
root.walk((node) => {
|
|
144
|
+
const file = node.source?.input.file ?? rootFile;
|
|
145
|
+
if (file !== rootFile)
|
|
146
|
+
refuse(rootFile, file, node);
|
|
147
|
+
if (node.type === 'decl')
|
|
148
|
+
for (const ref of valueRefs(node.value))
|
|
149
|
+
reads.push({ file, node, ref, by: resolve.url });
|
|
150
|
+
if (node.type === 'atrule' && node.name === 'import') {
|
|
151
|
+
const ref = importRef(node.params);
|
|
152
|
+
if (ref)
|
|
153
|
+
reads.push({ file, node, ref, by: resolve.css });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
for (const { file, node, ref, by } of reads)
|
|
157
|
+
for (const target of await targetsOf(ref, file, by))
|
|
158
|
+
refuse(file, target, node);
|
|
159
|
+
// postcss-modules runs first and resolves composes, @value and :import, so the chain is read from the files.
|
|
160
|
+
if (/\.module\.\w+$/.test(rootFile))
|
|
161
|
+
for (const target of await modulesChain(rootFile, resolve.css))
|
|
162
|
+
refuse(rootFile, target);
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
plugin.postcss = true;
|
|
166
|
+
return plugin;
|
|
167
|
+
}
|
package/dist/vite/kit.js
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
// name and changing the copy: every import of it, the hub's included, then
|
|
5
5
|
// takes the project's. `@milesroxas/design-hub/kit/<name>` always means the package's
|
|
6
6
|
// copy, for a project file that extends it (kit/icons.ts adds glyphs to the base set).
|
|
7
|
-
import { existsSync, statSync } from 'node:fs';
|
|
7
|
+
import { existsSync, realpathSync, statSync } from 'node:fs';
|
|
8
8
|
import { join } from 'node:path';
|
|
9
9
|
const KIT_PREFIX = '@kit/';
|
|
10
10
|
const PACKAGE_PREFIX = '@milesroxas/design-hub/kit/';
|
|
11
|
+
/** Vite's own test for a `?url` import (`urlRE`). */
|
|
12
|
+
const URL_QUERY = /(\?|&)url(?:&|$)/;
|
|
11
13
|
const CANDIDATES = ['', '.ts', '.tsx', '.css', '.json', '/index.ts', '/index.tsx'];
|
|
12
14
|
const isFile = (path) => existsSync(path) && statSync(path).isFile();
|
|
13
15
|
/** The file `@kit/<rest>` names in the first of `dirs` that has it, or null. */
|
|
@@ -25,11 +27,18 @@ export function kitResolver(projectKit, packageKit) {
|
|
|
25
27
|
name: 'design-hub-kit',
|
|
26
28
|
enforce: 'pre',
|
|
27
29
|
resolveId(id) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
// A query (`?worker`, `?url`) rides along on the file it finds.
|
|
31
|
+
const [path, query] = id.split(/(?=\?)/);
|
|
32
|
+
const found = path.startsWith(KIT_PREFIX)
|
|
33
|
+
? findKitFile([projectKit, packageKit], path.slice(KIT_PREFIX.length))
|
|
34
|
+
: path.startsWith(PACKAGE_PREFIX)
|
|
35
|
+
? findKitFile([packageKit], path.slice(PACKAGE_PREFIX.length))
|
|
36
|
+
: null;
|
|
37
|
+
// `?url` on its real path: Vite turns it into `?transform-only` and resolves that itself, on the
|
|
38
|
+
// real path, so both must name one module. Anything else keeps the path in kit/, so a kit/ linked
|
|
39
|
+
// from outside the project still resolves the project's own dependencies (`motion/react`).
|
|
40
|
+
const file = found && (query && URL_QUERY.test(query) ? realpathSync(found) : found);
|
|
41
|
+
return file && query ? file + query : file;
|
|
33
42
|
},
|
|
34
43
|
};
|
|
35
44
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The smallest source that counts, in bytes: a shorter file (an empty
|
|
3
|
+
* `.gitkeep`, `{"a":1}`) is never looked for, as it matches a short string
|
|
4
|
+
* by chance.
|
|
5
|
+
*/
|
|
6
|
+
export declare const MIN = 40;
|
|
7
|
+
type Owner = string | null;
|
|
8
|
+
type Form = 'bytes' | 'base64' | 'svg' | 'string';
|
|
9
|
+
/** One form of a built file: what it is, and the hash and size of the whole content it holds. */
|
|
10
|
+
type Found = {
|
|
11
|
+
form: Form;
|
|
12
|
+
hash: string;
|
|
13
|
+
size: number;
|
|
14
|
+
};
|
|
15
|
+
/** What Vite writes for an SVG it inlines as text (`svgToDataURL`), or null when it writes base64 instead. */
|
|
16
|
+
export declare function svgDataUrlPayload(text: string): string | null;
|
|
17
|
+
/** The whole content a `data:` URI holds: its decoded bytes, or the payload of Vite's URL-encoded SVG form. */
|
|
18
|
+
export declare function dataUriForms(uri: string): Found[];
|
|
19
|
+
/** 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. */
|
|
20
|
+
export declare function dataUrisIn(text: string): string[];
|
|
21
|
+
export type Written = {
|
|
22
|
+
fileName: string;
|
|
23
|
+
source: string | Uint8Array;
|
|
24
|
+
};
|
|
25
|
+
/** The error for a built file that holds a prototype's file whole outside its folder. */
|
|
26
|
+
export declare const leaked: (fileName: string, file: string, slug: string, form: Form) => string;
|
|
27
|
+
/**
|
|
28
|
+
* Every built file that holds a whole file of a prototype other than its own
|
|
29
|
+
* owner (for a file outside `_p/`, any prototype's), as problems naming the
|
|
30
|
+
* built file and the source, unless its owner has a source with the same
|
|
31
|
+
* content. `inputs` is every file the bundle read (every prototype file
|
|
32
|
+
* the build loaded, every chunk's modules, every asset's original files),
|
|
33
|
+
* recorded before settle moved anything; a string is compared only with
|
|
34
|
+
* those. A built script that does not parse is a problem, never skipped.
|
|
35
|
+
*/
|
|
36
|
+
export declare function leaks(root: string, prototypes: string, owner: (id: string) => Owner, inputs: readonly string[], written: readonly Written[]): string[];
|
|
37
|
+
export {};
|
|
@@ -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
|
+
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -52,8 +52,9 @@ function gateFont() {
|
|
|
52
52
|
export function designHub({ root }) {
|
|
53
53
|
return [
|
|
54
54
|
react(),
|
|
55
|
-
|
|
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(),
|
package/dist/vite/registry.d.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
import type
|
|
2
|
-
/** The prototype folders: kebab-case, with a README
|
|
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
|
-
|
|
7
|
-
export declare function
|
|
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[];
|