@milesroxas/design-hub 0.8.1 → 0.10.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/controls.test.ts +176 -0
- package/core/controls.ts +195 -0
- package/core/prototype.test.ts +83 -0
- package/core/prototype.ts +84 -0
- package/core/review/model.ts +32 -0
- package/core/review/policy.test.ts +4 -2
- package/core/review/policy.ts +4 -2
- package/core/review/service.test.ts +223 -4
- package/core/review/service.ts +111 -2
- package/core/review/store.ts +13 -0
- package/dist/core/controls.d.ts +55 -0
- package/dist/core/controls.js +174 -0
- package/dist/core/prototype.d.ts +35 -0
- package/dist/core/prototype.js +84 -0
- package/dist/core/review/model.d.ts +13 -0
- package/dist/core/review/model.js +27 -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 +50 -1
- package/dist/core/review/service.js +97 -3
- package/dist/core/review/store.d.ts +13 -0
- package/dist/core/review/store.js +13 -0
- package/dist/scripts/guard.d.ts +11 -0
- package/dist/scripts/guard.js +44 -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/lib/rules.d.ts +1 -0
- package/dist/scripts/lib/rules.js +1 -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 +21 -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/kit/Artboard.tsx +21 -8
- package/kit/Space.tsx +4 -3
- package/kit/Variants.tsx +193 -65
- package/kit/ViewTools.tsx +77 -11
- package/kit/audience.ts +85 -0
- package/kit/dials.css +59 -0
- package/kit/dials.tsx +272 -35
- package/kit/harness.css +34 -0
- package/kit/prefs.ts +23 -1
- package/package.json +5 -4
- package/shell/ClientViewNote.tsx +46 -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/controls.ts +26 -0
- package/shell/hub.ts +36 -0
- package/shell/main.tsx +51 -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 +54 -3
- package/shell/system.css +5 -0
- package/skills/miles-brand/SKILL.md +11 -1
- package/skills/miles-deploy/SKILL.md +19 -0
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-prototype/SKILL.md +48 -2
package/dist/scripts/guard.js
CHANGED
|
@@ -43,6 +43,49 @@ export function featureOf(file) {
|
|
|
43
43
|
return null;
|
|
44
44
|
return top;
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether a prototype stands on a Board: brand collateral, written to still
|
|
48
|
+
* files. Read from the change itself, else from the feature's own index.tsx,
|
|
49
|
+
* so its stylesheet is judged by what its markup stands on.
|
|
50
|
+
*/
|
|
51
|
+
export function standsOnBoard(change, feature) {
|
|
52
|
+
if (change.added.includes('@kit/Artboard'))
|
|
53
|
+
return true;
|
|
54
|
+
const index = join(PROTOTYPES, feature, 'index.tsx');
|
|
55
|
+
return existsSync(index) && readFileSync(index, 'utf8').includes('@kit/Artboard');
|
|
56
|
+
}
|
|
57
|
+
/** What a board must not hold, each with what to do instead. `none` values turn motion off, so they pass. */
|
|
58
|
+
const ON_BOARD = [
|
|
59
|
+
{
|
|
60
|
+
test: /@keyframes|\b(?:animation|transition)(?:-[a-z-]+)?\s*:\s*(?!\s|none\b|0s\b)|from\s+['"]motion\/react['"]/,
|
|
61
|
+
note: 'adds motion to collateral, which exports as a still file; a piece that moves is a Space prototype, where View has the motion tools',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
test: /\.dark-mode\b|prefers-color-scheme|\buseDark\b|\buseAppearance\b/,
|
|
65
|
+
note: 'styles collateral by theme, but a board is held to the look it exports in; a light or dark version is a Tone dial on the piece',
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* The View tools are set by what a prototype stands on, never by hand: the
|
|
70
|
+
* shells and Space mount them, and a Board has none.
|
|
71
|
+
*/
|
|
72
|
+
export function viewFindings(change, board) {
|
|
73
|
+
const file = relative(ROOT, change.file);
|
|
74
|
+
const out = [];
|
|
75
|
+
if (/from\s+['"]@kit\/ViewTools['"]/.test(change.added))
|
|
76
|
+
out.push({
|
|
77
|
+
file,
|
|
78
|
+
rule: RULE.view,
|
|
79
|
+
level: 'block',
|
|
80
|
+
note: 'mounts ViewTools; the shell or Space mounts it. Narrow it with <Space tools={[...]}>; a Board has none',
|
|
81
|
+
});
|
|
82
|
+
if (!board)
|
|
83
|
+
return out;
|
|
84
|
+
for (const { test, note } of ON_BOARD)
|
|
85
|
+
if (test.test(change.added))
|
|
86
|
+
out.push({ file, rule: RULE.view, level: 'warn', note });
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
46
89
|
const DEPENDENCY_LINE = /^\s*"[^"]+":\s*"[\^~>=<\d]/gm;
|
|
47
90
|
export function codeFindings(change) {
|
|
48
91
|
const file = relative(ROOT, change.file);
|
|
@@ -90,6 +133,7 @@ export function codeFindings(change) {
|
|
|
90
133
|
level: 'block',
|
|
91
134
|
note: `imports ${crossing.join(', ')}; a prototype imports only @kit/* and its own folder`,
|
|
92
135
|
});
|
|
136
|
+
out.push(...viewFindings(change, standsOnBoard(change, feature)));
|
|
93
137
|
return out;
|
|
94
138
|
}
|
|
95
139
|
const clip = (text) => text.length > MAX_CHANGE_CHARS ? `${text.slice(0, MAX_CHANGE_CHARS)}\n…[clipped]` : text;
|
package/dist/scripts/lib/cdp.js
CHANGED
|
@@ -41,6 +41,9 @@ export async function launch(chrome) {
|
|
|
41
41
|
`--user-data-dir=${profile}`,
|
|
42
42
|
'--no-first-run',
|
|
43
43
|
'--no-default-browser-check',
|
|
44
|
+
// No component updates or background fetches: an export needs neither, and their fetcher leaves a temp folder.
|
|
45
|
+
'--disable-component-update',
|
|
46
|
+
'--disable-background-networking',
|
|
44
47
|
'--hide-scrollbars',
|
|
45
48
|
'--mute-audio',
|
|
46
49
|
'--force-color-profile=srgb',
|
|
@@ -49,7 +52,9 @@ export async function launch(chrome) {
|
|
|
49
52
|
const exited = new Promise((resolve) => child.once('exit', () => resolve()));
|
|
50
53
|
// The profile goes once Chrome has let go of it; a leftover temp folder is not worth failing an export over.
|
|
51
54
|
const cleanup = async () => {
|
|
52
|
-
|
|
55
|
+
// Let Chrome exit on its own first: killed, it leaves its temp folders behind.
|
|
56
|
+
await within(exited, CLOSE_TIMEOUT_MS);
|
|
57
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
53
58
|
child.kill('SIGKILL');
|
|
54
59
|
await within(exited, CLOSE_TIMEOUT_MS);
|
|
55
60
|
try {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** `root` is the throwaway project, removed before this returns. */
|
|
2
|
+
export type Smoke = {
|
|
3
|
+
ran: true;
|
|
4
|
+
files: string[];
|
|
5
|
+
root: string;
|
|
6
|
+
} | {
|
|
7
|
+
ran: false;
|
|
8
|
+
reason: string;
|
|
9
|
+
};
|
|
10
|
+
/** The browser export would use, or null. */
|
|
11
|
+
export declare const findChrome: () => string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Exports the brand template's one artboard through `cli` (the CLI entry:
|
|
14
|
+
* the source `scripts/cli.ts` or the built `dist/scripts/cli.js`) and
|
|
15
|
+
* checks a PNG came out. Not run without Chrome, saying so. Throws, with
|
|
16
|
+
* what the command printed, when the export fails. Leaves nothing behind.
|
|
17
|
+
*/
|
|
18
|
+
export declare function exportSmoke(cli: string): Promise<Smoke>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// A real `design-hub export` of one brand prototype on a cold cache: a
|
|
3
|
+
// throwaway project with the package's brand template, the CLI run in a
|
|
4
|
+
// child process of its own (ROOT is fixed when paths.ts loads), and the
|
|
5
|
+
// PNG it writes checked. Export is the one command that starts the hub
|
|
6
|
+
// with no one watching, so a break in it shows only when someone runs it.
|
|
7
|
+
// `scripts/export.test.ts` runs this when Chrome is found and skips with the
|
|
8
|
+
// reason when it is not; `release-check` runs it and fails without Chrome.
|
|
9
|
+
import { execFile } from 'node:child_process';
|
|
10
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync, } from 'node:fs';
|
|
11
|
+
import { tmpdir } from 'node:os';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { chromeCandidates } from '../../core/export.js';
|
|
14
|
+
import { TEMPLATES } from '../../server/paths.js';
|
|
15
|
+
const SLUG = 'smoke-post';
|
|
16
|
+
const TIMEOUT_MS = 180_000;
|
|
17
|
+
const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
18
|
+
/** The browser export would use, or null. */
|
|
19
|
+
export const findChrome = () => chromeCandidates(process.platform, process.env).find((p) => existsSync(p)) ?? null;
|
|
20
|
+
/** A project with one brand prototype from the package's template, and the kit file the hub imports. */
|
|
21
|
+
function project() {
|
|
22
|
+
// Real, as the CLI reads $DESIGN_HUB_ROOT, so the files it names are relative to this folder.
|
|
23
|
+
const root = realpathSync(mkdtempSync(join(tmpdir(), 'hub-export-')));
|
|
24
|
+
mkdirSync(join(root, 'kit'), { recursive: true });
|
|
25
|
+
writeFileSync(join(root, 'kit', 'tokens.css'), ':root { --t: 1; }\n');
|
|
26
|
+
const dir = join(root, 'prototypes', SLUG);
|
|
27
|
+
cpSync(join(TEMPLATES, 'brand'), dir, { recursive: true });
|
|
28
|
+
const vars = {
|
|
29
|
+
title: 'Smoke post',
|
|
30
|
+
slug: SLUG,
|
|
31
|
+
created: '2030-01-01',
|
|
32
|
+
kind: 'exploration',
|
|
33
|
+
feature: "''",
|
|
34
|
+
track: 'brand',
|
|
35
|
+
};
|
|
36
|
+
for (const name of readdirSync(dir)) {
|
|
37
|
+
let text = readFileSync(join(dir, name), 'utf8');
|
|
38
|
+
for (const [k, v] of Object.entries(vars))
|
|
39
|
+
text = text.replaceAll(`{{${k}}}`, v);
|
|
40
|
+
writeFileSync(join(dir, name), text);
|
|
41
|
+
}
|
|
42
|
+
return root;
|
|
43
|
+
}
|
|
44
|
+
const run = (cli, root) => new Promise((done) => {
|
|
45
|
+
execFile(process.execPath, [cli, 'export', SLUG, '--out', join(root, 'exports')], { cwd: root, env: { ...process.env, DESIGN_HUB_ROOT: root }, timeout: TIMEOUT_MS, encoding: 'utf8' }, (error, stdout, stderr) => done({ code: error ? Number(error.code ?? 1) || 1 : 0, stdout, stderr }));
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* Exports the brand template's one artboard through `cli` (the CLI entry:
|
|
49
|
+
* the source `scripts/cli.ts` or the built `dist/scripts/cli.js`) and
|
|
50
|
+
* checks a PNG came out. Not run without Chrome, saying so. Throws, with
|
|
51
|
+
* what the command printed, when the export fails. Leaves nothing behind.
|
|
52
|
+
*/
|
|
53
|
+
export async function exportSmoke(cli) {
|
|
54
|
+
if (!findChrome())
|
|
55
|
+
return { ran: false, reason: 'No Chrome, Chromium or Edge found; set CHROME_PATH to one.' };
|
|
56
|
+
const root = project();
|
|
57
|
+
try {
|
|
58
|
+
const { code, stdout, stderr } = await run(cli, root);
|
|
59
|
+
let out = {};
|
|
60
|
+
try {
|
|
61
|
+
out = JSON.parse(stdout);
|
|
62
|
+
}
|
|
63
|
+
catch { }
|
|
64
|
+
const files = (out.files ?? []).map((f) => join(root, f));
|
|
65
|
+
const pngs = files.filter((f) => existsSync(f) && readFileSync(f).subarray(0, 4).equals(PNG));
|
|
66
|
+
if (code !== 0 || out.ok !== true || !pngs.length)
|
|
67
|
+
throw new Error(`design-hub export failed (exit ${code}):\n${stdout}\n${stderr}`.trim());
|
|
68
|
+
return { ran: true, files: (out.files ?? []).map(String), root };
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -6,6 +6,7 @@ export declare const RULE: {
|
|
|
6
6
|
readonly abstraction: 'No abstraction for later';
|
|
7
7
|
readonly handling: 'No handling a fixture cannot reach';
|
|
8
8
|
readonly state: 'Every state is in the README';
|
|
9
|
+
readonly view: 'View tools fit what the prototype is';
|
|
9
10
|
};
|
|
10
11
|
export type SemanticRule = 'fetching' | 'abstraction' | 'handling' | 'state';
|
|
11
12
|
/** Each question reads `change`, `readmeStates` and `fixtureSets` from the state by path. */
|
|
@@ -10,6 +10,7 @@ export const RULE = {
|
|
|
10
10
|
abstraction: 'No abstraction for later',
|
|
11
11
|
handling: 'No handling a fixture cannot reach',
|
|
12
12
|
state: 'Every state is in the README',
|
|
13
|
+
view: 'View tools fit what the prototype is',
|
|
13
14
|
};
|
|
14
15
|
/** Each question reads `change`, `readmeStates` and `fixtureSets` from the state by path. */
|
|
15
16
|
export const NOULS = {
|
package/dist/scripts/new.js
CHANGED
|
@@ -9,6 +9,7 @@ import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, wri
|
|
|
9
9
|
import { dirname, join, relative } from 'node:path';
|
|
10
10
|
import { featureTitle, guideHref, SLUG } from '../core/feature.js';
|
|
11
11
|
import { TRACKS, trackFor } from '../core/project.js';
|
|
12
|
+
import { isPrototypeSlug, RESERVED } from '../core/prototype.js';
|
|
12
13
|
import { devPort, readEnv } from '../server/env.js';
|
|
13
14
|
import { DOCS, PROTOTYPES, ROOT, TEMPLATES } from '../server/paths.js';
|
|
14
15
|
import { readProject } from '../server/project.js';
|
|
@@ -120,8 +121,10 @@ export function kindFor(flags, feature, primary = (f) => primaryOf(f)?.slug) {
|
|
|
120
121
|
function prototype(slug, { flags }) {
|
|
121
122
|
if (!SLUG.test(slug))
|
|
122
123
|
throw new CliError('The slug is kebab-case.', { got: slug });
|
|
123
|
-
if (slug
|
|
124
|
-
throw new CliError(
|
|
124
|
+
if (!isPrototypeSlug(slug))
|
|
125
|
+
throw new CliError(`${slug}/ is the hub's own path (${RESERVED.join(', ')}); pick another slug.`, {
|
|
126
|
+
got: slug,
|
|
127
|
+
});
|
|
125
128
|
const feature = flags.feature ?? '';
|
|
126
129
|
if (feature && !SLUG.test(feature))
|
|
127
130
|
throw new CliError('--feature takes a kebab-case slug.', { got: feature });
|
|
@@ -9,11 +9,13 @@ export declare const spec: {
|
|
|
9
9
|
readonly description: 'A term to refuse, case-insensitive. Repeat for more.';
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
|
-
readonly output: '{ ok, terms, files, hits: { <file>: [terms] } }';
|
|
12
|
+
readonly output: '{ ok, terms, files, hits: { <file>: [terms] }, export: { ran, files | reason } }';
|
|
13
13
|
};
|
|
14
|
-
export declare function main({ flags }: Args<typeof spec>): {
|
|
14
|
+
export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
15
15
|
ok: boolean;
|
|
16
16
|
terms: string[];
|
|
17
17
|
files: number;
|
|
18
18
|
hits: Record<string, string[]>;
|
|
19
|
-
|
|
19
|
+
export: import("./lib/export-smoke.ts").Smoke;
|
|
20
|
+
fix?: string | undefined;
|
|
21
|
+
}>;
|
|
@@ -3,19 +3,22 @@
|
|
|
3
3
|
// The gate before a repo is published: no tracked file may contain a
|
|
4
4
|
// term from the list, case-insensitive (a client's name, a person's name).
|
|
5
5
|
// Tracked means `git ls-files`, so an untracked plan never trips it and a
|
|
6
|
-
// gitignored secret never hides a hit.
|
|
6
|
+
// gitignored secret never hides a hit. It also runs a real cold
|
|
7
|
+
// `design-hub export` (scripts/lib/export-smoke.ts), which `pnpm test`
|
|
8
|
+
// skips on a machine without Chrome: here no Chrome is a failure.
|
|
7
9
|
import { execFileSync } from 'node:child_process';
|
|
8
10
|
import { readFileSync } from 'node:fs';
|
|
9
11
|
import { join } from 'node:path';
|
|
10
12
|
import { ROOT } from '../server/paths.js';
|
|
11
13
|
import { CliError } from './lib/cli.js';
|
|
14
|
+
import { exportSmoke } from './lib/export-smoke.js';
|
|
12
15
|
export const spec = {
|
|
13
16
|
name: 'release-check',
|
|
14
17
|
summary: 'Fail when any tracked file contains one of the given terms.',
|
|
15
18
|
flags: { term: { type: 'list', description: 'A term to refuse, case-insensitive. Repeat for more.' } },
|
|
16
|
-
output: '{ ok, terms, files, hits: { <file>: [terms] } }',
|
|
19
|
+
output: '{ ok, terms, files, hits: { <file>: [terms] }, export: { ran, files | reason } }',
|
|
17
20
|
};
|
|
18
|
-
export function main({ flags }) {
|
|
21
|
+
export async function main({ flags }) {
|
|
19
22
|
const terms = flags.term.map((t) => t.toLowerCase()).filter(Boolean);
|
|
20
23
|
if (!terms.length)
|
|
21
24
|
throw new CliError('Give at least one --term.');
|
|
@@ -35,5 +38,15 @@ export function main({ flags }) {
|
|
|
35
38
|
if (found.length)
|
|
36
39
|
hits[file] = found;
|
|
37
40
|
}
|
|
38
|
-
|
|
41
|
+
// The entry this process runs (scripts/cli.ts or dist/scripts/cli.js) exports the same way a project does.
|
|
42
|
+
const exported = await exportSmoke(process.argv[1]);
|
|
43
|
+
const ok = Object.keys(hits).length === 0 && exported.ran;
|
|
44
|
+
return {
|
|
45
|
+
ok,
|
|
46
|
+
terms,
|
|
47
|
+
files: files.length,
|
|
48
|
+
hits,
|
|
49
|
+
export: exported,
|
|
50
|
+
...(exported.ran ? {} : { fix: `Install Chrome, or set CHROME_PATH: ${exported.reason}` }),
|
|
51
|
+
};
|
|
39
52
|
}
|
package/dist/scripts/share.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
|
40
40
|
error: string;
|
|
41
41
|
fix: string;
|
|
42
42
|
} | {
|
|
43
|
+
ok?: undefined;
|
|
44
|
+
error?: undefined;
|
|
43
45
|
share: {
|
|
44
46
|
url: string;
|
|
45
47
|
open: {
|
|
@@ -51,9 +53,7 @@ export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
|
51
53
|
review: string;
|
|
52
54
|
next: string;
|
|
53
55
|
steps?: undefined;
|
|
54
|
-
ok?: undefined;
|
|
55
56
|
stage?: undefined;
|
|
56
|
-
error?: undefined;
|
|
57
57
|
fix?: undefined;
|
|
58
58
|
via: "cli" | "git";
|
|
59
59
|
deployment: string;
|
package/dist/server/api.js
CHANGED
|
@@ -34,6 +34,12 @@ async function query(op, params, { review, actor }) {
|
|
|
34
34
|
return {
|
|
35
35
|
comments: await review.listComments(actor, prototypeOf(params.get('prototype')), params.get('viewAs') ?? undefined),
|
|
36
36
|
};
|
|
37
|
+
case 'prototypes':
|
|
38
|
+
// The index's read: the view the caller gets and the client-facing slugs. viewAs is checked as comments checks it.
|
|
39
|
+
return await review.visiblePrototypes(actor, params.get('viewAs') ?? undefined);
|
|
40
|
+
case 'controls':
|
|
41
|
+
// A prototype page's read: the caller's view, and what Studio marked on its dials and variants.
|
|
42
|
+
return await review.controlsOf(actor, params.get('prototype'));
|
|
37
43
|
case 'people':
|
|
38
44
|
return { people: (await review.listPeople(actor)).map(visible) };
|
|
39
45
|
case 'access':
|
|
@@ -94,6 +100,21 @@ async function command(op, b, ctx) {
|
|
|
94
100
|
return { comment: await review.resolve(actor, { ...at(), resolved: b.resolved === true }) };
|
|
95
101
|
case 'comment.triage':
|
|
96
102
|
return { comment: await review.retriage(actor, { ...at(), triage: b.triage, states: b.states }) };
|
|
103
|
+
case 'prototype.audience':
|
|
104
|
+
return {
|
|
105
|
+
prototype: b.prototype,
|
|
106
|
+
audience: await review.setAudience(actor, b.prototype, b.audience),
|
|
107
|
+
};
|
|
108
|
+
case 'control.audience':
|
|
109
|
+
return {
|
|
110
|
+
prototype: b.prototype,
|
|
111
|
+
...(await review.setControl(actor, {
|
|
112
|
+
prototype: b.prototype,
|
|
113
|
+
kind: b.kind,
|
|
114
|
+
id: b.id,
|
|
115
|
+
audience: b.audience,
|
|
116
|
+
})),
|
|
117
|
+
};
|
|
97
118
|
default:
|
|
98
119
|
return peopleCommand(op, b, ctx);
|
|
99
120
|
}
|
package/dist/server/session.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type PersonKind } from '../core/review/model.ts';
|
|
1
2
|
export declare const PERSON_COOKIE = "__Host-hub_person";
|
|
2
3
|
/** How long the store's last yes is trusted before the gate asks again. */
|
|
3
4
|
export declare const CHECK_SECONDS: number;
|
|
@@ -20,8 +21,11 @@ export declare function sessionSeconds(endsAt: string | null, at?: Date): number
|
|
|
20
21
|
export type PersonClaim = {
|
|
21
22
|
id: string;
|
|
22
23
|
epoch: number;
|
|
24
|
+
kind: PersonKind;
|
|
23
25
|
};
|
|
24
|
-
|
|
26
|
+
/** `kind` is null for a p3 token, which never counts as fresh. */
|
|
27
|
+
export type SessionClaim = Omit<PersonClaim, 'kind'> & {
|
|
28
|
+
kind: PersonKind | null;
|
|
25
29
|
checkedAt: number;
|
|
26
30
|
};
|
|
27
31
|
/** A token for someone the store said yes to at `at`, lasting to their access end. */
|
package/dist/server/session.js
CHANGED
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
// lasts until their access ends, at most 400 days (the longest a browser
|
|
7
7
|
// keeps a cookie). HMAC-SHA256 with GATE_SECRET.
|
|
8
8
|
//
|
|
9
|
-
// It also carries when the store last said yes (checkedAt). For CHECK_SECONDS
|
|
9
|
+
// It also carries the person's kind, and when the store last said yes (checkedAt). For CHECK_SECONDS
|
|
10
10
|
// after that the gate trusts the signature; then it asks the store again and
|
|
11
11
|
// issues a fresh cookie, so a revoke, a sign out everywhere or a changed
|
|
12
12
|
// access end reaches every request within CHECK_SECONDS, whatever it claims to be.
|
|
13
13
|
// Every function that reads the clock takes `at`, so tests need not wait.
|
|
14
14
|
import { base64url } from '../core/bytes.js';
|
|
15
|
+
import { PERSON_KINDS } from '../core/review/model.js';
|
|
15
16
|
export const PERSON_COOKIE = '__Host-hub_person';
|
|
16
17
|
const MAX_SESSION_SECONDS = 60 * 60 * 24 * 400;
|
|
17
18
|
/** How long the store's last yes is trusted before the gate asks again. */
|
|
@@ -47,41 +48,58 @@ export function sessionSeconds(endsAt, at = new Date()) {
|
|
|
47
48
|
const left = Math.floor(Date.parse(endsAt) / 1000) - seconds(at);
|
|
48
49
|
return Number.isFinite(left) ? Math.max(0, Math.min(MAX_SESSION_SECONDS, left)) : 0;
|
|
49
50
|
}
|
|
50
|
-
const VERSION = '
|
|
51
|
+
const VERSION = 'p4';
|
|
52
|
+
const OLD_VERSION = 'p3';
|
|
51
53
|
// The site is signed, never written into the token; a newline cannot occur in a URL, so the parts cannot run together.
|
|
52
|
-
const personMessage = (site, id, epoch, expires, checkedAt) => `${VERSION}\n${site}\n${id}.${epoch}.${expires}.${checkedAt}`;
|
|
53
|
-
const
|
|
54
|
+
const personMessage = (site, id, epoch, kind, expires, checkedAt) => `${VERSION}\n${site}\n${id}.${epoch}.${kind}.${expires}.${checkedAt}`;
|
|
55
|
+
const oldMessage = (site, id, epoch, expires, checkedAt) => `${OLD_VERSION}\n${site}\n${id}.${epoch}.${expires}.${checkedAt}`;
|
|
56
|
+
const tokenText = (id, epoch, kind, expires, checkedAt) => `${VERSION}.${id}.${epoch}.${kind}.${expires}.${checkedAt}`;
|
|
54
57
|
/** A token for someone the store said yes to at `at`, lasting to their access end. */
|
|
55
58
|
export async function issuePersonToken(gate, claim, endsAt, at = new Date()) {
|
|
56
59
|
const checkedAt = seconds(at);
|
|
57
60
|
const expires = checkedAt + sessionSeconds(endsAt, at);
|
|
58
|
-
const message = personMessage(gate.site, claim.id, claim.epoch, expires, checkedAt);
|
|
59
|
-
return `${tokenText(claim.id, claim.epoch, expires, checkedAt)}.${await hmac(gate.secret, message)}`;
|
|
61
|
+
const message = personMessage(gate.site, claim.id, claim.epoch, claim.kind, expires, checkedAt);
|
|
62
|
+
return `${tokenText(claim.id, claim.epoch, claim.kind, expires, checkedAt)}.${await hmac(gate.secret, message)}`;
|
|
60
63
|
}
|
|
61
64
|
const isCount = (n) => Number.isInteger(n) && n >= 0;
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
const isKind = (k) => PERSON_KINDS.includes(k);
|
|
66
|
+
function parseCurrent(parts) {
|
|
67
|
+
const [, id, epoch, kind, expires, checkedAt, sig, extra] = parts;
|
|
68
|
+
if (!/^[a-z0-9]+$/.test(id ?? '') || !isKind(kind) || !sig || extra !== undefined)
|
|
69
|
+
return null;
|
|
70
|
+
if (![epoch, expires, checkedAt].every((n) => isCount(Number(n))))
|
|
71
|
+
return null;
|
|
72
|
+
const claim = { id, epoch: Number(epoch), kind, checkedAt: Number(checkedAt) };
|
|
73
|
+
const message = (site) => personMessage(site, id, claim.epoch, kind, Number(expires), claim.checkedAt);
|
|
74
|
+
return { claim, expires: Number(expires), sig, message };
|
|
75
|
+
}
|
|
76
|
+
function parseOld(parts) {
|
|
77
|
+
const [, id, epoch, expires, checkedAt, sig, extra] = parts;
|
|
78
|
+
if (!/^[a-z0-9]+$/.test(id ?? '') || !sig || extra !== undefined)
|
|
79
|
+
return null;
|
|
80
|
+
if (![epoch, expires, checkedAt].every((n) => isCount(Number(n))))
|
|
81
|
+
return null;
|
|
82
|
+
const claim = { id, epoch: Number(epoch), kind: null, checkedAt: Number(checkedAt) };
|
|
83
|
+
const message = (site) => oldMessage(site, id, claim.epoch, Number(expires), claim.checkedAt);
|
|
84
|
+
return { claim, expires: Number(expires), sig, message };
|
|
85
|
+
}
|
|
86
|
+
const PARSERS = {
|
|
87
|
+
[VERSION]: parseCurrent,
|
|
88
|
+
[OLD_VERSION]: parseOld,
|
|
69
89
|
};
|
|
70
90
|
/** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
|
|
71
91
|
export async function readPersonToken(gate, token, at = new Date()) {
|
|
72
92
|
const parts = (token ?? '').split('.');
|
|
73
|
-
|
|
93
|
+
const parse = Object.hasOwn(PARSERS, parts[0]) ? PARSERS[parts[0]] : null;
|
|
94
|
+
const parsed = parse?.(parts);
|
|
95
|
+
if (!parsed || parsed.expires <= seconds(at))
|
|
74
96
|
return null;
|
|
75
|
-
|
|
76
|
-
if (Number(expires) <= seconds(at))
|
|
77
|
-
return null;
|
|
78
|
-
const message = personMessage(gate.site, id, Number(epoch), Number(expires), Number(checkedAt));
|
|
79
|
-
return sameText(sig, await hmac(gate.secret, message))
|
|
80
|
-
? { id, epoch: Number(epoch), checkedAt: Number(checkedAt) }
|
|
81
|
-
: null;
|
|
97
|
+
return sameText(parsed.sig, await hmac(gate.secret, parsed.message(gate.site))) ? parsed.claim : null;
|
|
82
98
|
}
|
|
83
99
|
/** Whether the store's yes in this claim is recent enough to trust without asking again. */
|
|
84
100
|
export function isFresh(claim, at = new Date()) {
|
|
101
|
+
if (claim.kind === null)
|
|
102
|
+
return false;
|
|
85
103
|
const age = seconds(at) - claim.checkedAt;
|
|
86
104
|
return age >= 0 && age < CHECK_SECONDS;
|
|
87
105
|
}
|
package/dist/vercel/proxy.js
CHANGED
|
@@ -12,13 +12,16 @@
|
|
|
12
12
|
// It carries when the store last said yes; after CHECK_SECONDS (5 minutes)
|
|
13
13
|
// the gate asks the store again on the next request of any kind, so a revoke,
|
|
14
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.
|
|
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
|
|
16
18
|
// page gets the sign-in page (401), which says what to do and has no form,
|
|
17
19
|
// and anything else a plain-text 401. Without GATE_SECRET the gate fails
|
|
18
20
|
// closed (503).
|
|
19
21
|
import { next } from '@vercel/functions';
|
|
20
22
|
import { base64, sha256 } from '../core/bytes.js';
|
|
21
23
|
import { escapeHtml } from '../core/html.js';
|
|
24
|
+
import { isPrototypeSlug, prototypeOfPath } from '../core/prototype.js';
|
|
22
25
|
import { deployedReview } from '../server/api.js';
|
|
23
26
|
import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, } from '../server/session.js';
|
|
24
27
|
const DEFAULT_SITE_NAME = 'Prototypes';
|
|
@@ -149,6 +152,9 @@ async function page(request, status, html, text, extra = {}) {
|
|
|
149
152
|
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
150
153
|
}
|
|
151
154
|
const unavailable = (request, error) => plain(request, 503, error);
|
|
155
|
+
/** 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);
|
|
157
|
+
const NOT_FOUND = 'Not found.';
|
|
152
158
|
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
153
159
|
const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
|
|
154
160
|
const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
|
|
@@ -180,7 +186,8 @@ async function showLink(ctx, token) {
|
|
|
180
186
|
}
|
|
181
187
|
/** The cookie for someone the store just said yes to, lasting to their access end. */
|
|
182
188
|
async function sessionFor(gate, person, at) {
|
|
183
|
-
const
|
|
189
|
+
const claim = { id: person.id, epoch: person.epoch, kind: person.kind };
|
|
190
|
+
const token = await issuePersonToken(gate, claim, person.accessEndsAt, at);
|
|
184
191
|
return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
|
|
185
192
|
}
|
|
186
193
|
async function signInWithLink(ctx, token) {
|
|
@@ -220,23 +227,52 @@ function signedOut({ siteName, request }, staleCookie) {
|
|
|
220
227
|
return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
|
|
221
228
|
}
|
|
222
229
|
const pass = (cookie) => next({ headers: { 'cache-control': 'private, no-store', ...(cookie ? { 'set-cookie': cookie } : {}) } });
|
|
230
|
+
/**
|
|
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.
|
|
237
|
+
*/
|
|
238
|
+
async function clientMay(ctx, review, slug) {
|
|
239
|
+
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);
|
|
245
|
+
}
|
|
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));
|
|
252
|
+
}
|
|
223
253
|
/**
|
|
224
254
|
* A signed session. While the store's last yes is fresh the signature is
|
|
225
255
|
* enough. Once it is stale the store is asked, whatever the request says it
|
|
226
256
|
* is (its headers are the client's to choose): yes passes with a fresh cookie
|
|
227
257
|
* whose expiry follows the person's current access end; no signs out and
|
|
228
|
-
* clears the cookie; no answer is 503 and the cookie stays.
|
|
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.
|
|
229
261
|
*/
|
|
230
262
|
async function personSession(ctx, gate, claim) {
|
|
231
|
-
|
|
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))
|
|
232
266
|
return pass();
|
|
233
267
|
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
234
268
|
if ('error' in setup)
|
|
235
269
|
return unavailable(ctx.request, setup.error);
|
|
236
|
-
const current = await
|
|
237
|
-
if (current
|
|
238
|
-
return
|
|
239
|
-
|
|
270
|
+
const current = fresh ? null : await recheck(ctx, setup.review, claim);
|
|
271
|
+
if (current instanceof Response)
|
|
272
|
+
return current;
|
|
273
|
+
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);
|
|
240
276
|
}
|
|
241
277
|
/** The middleware a project's proxy.ts default-exports. */
|
|
242
278
|
export function createProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date() } = {}) {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import nodePath from 'node:path';
|
|
2
|
+
/** The virtual module Studio and a Client load a prototype through: its README and its loader. */
|
|
3
|
+
export declare const ENTRY_ID = "virtual:design-hub/entry/";
|
|
4
|
+
/**
|
|
5
|
+
* A path with every symlink followed and in the letter case it has on disk,
|
|
6
|
+
* or the path itself when it is not a file on disk (a virtual module). The
|
|
7
|
+
* native call, because Node's own keeps the case as written: on a disk that
|
|
8
|
+
* folds case (macOS, Windows) `Prototypes/beta/x.css` is `prototypes/beta/x.css`,
|
|
9
|
+
* and an owner must be the same either way.
|
|
10
|
+
*/
|
|
11
|
+
export declare const real: (path: string) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Which prototype folder a module or file belongs to, on its real path (a
|
|
14
|
+
* symlink into another prototype's folder is that prototype's): the first
|
|
15
|
+
* segment under `prototypes/`, whatever its name (a file directly there is
|
|
16
|
+
* owned by its own name, so nothing under `prototypes/` is ever the hub's),
|
|
17
|
+
* or null for anything outside it. A prototype's virtual entry belongs to its
|
|
18
|
+
* slug. Whether that owner is a prototype at all is for the caller: a folder
|
|
19
|
+
* with no README, or a file directly in `prototypes/`, is refused (looseFile).
|
|
20
|
+
*/
|
|
21
|
+
export declare function ownerOf(prototypes: string, paths?: typeof nodePath): (id: string) => string | null;
|
|
22
|
+
/** The owners of a set of modules or files, each once, sorted; none for shared code. */
|
|
23
|
+
export declare function ownersOf(owner: (id: string) => string | null, ids: readonly string[]): string[];
|
|
24
|
+
/** Where a built file goes: under `_p/<slug>/` when one prototype owns it, else the shared `assets/`. */
|
|
25
|
+
export declare function placed(owners: string[], pattern: string, shared?: boolean): string;
|
|
26
|
+
/** A file resolved against the Vite root: Rolldown reports an asset's original files relative to it. */
|
|
27
|
+
export declare const fromRoot: (viteRoot: string) => (file: string) => string;
|
|
28
|
+
/** A path for an error message: from the project root, with no `\0` or query. */
|
|
29
|
+
export declare const relativeTo: (root: string, path: string) => string;
|
|
30
|
+
/** The error for a file reaching a prototype's file, naming both: another prototype's, or (from the kit or the hub) any prototype's. */
|
|
31
|
+
export declare function crossImport(root: string, importer: string, imported: string, fromPrototype?: boolean): string;
|
|
32
|
+
/** Who owns a built file's content: a prototype's slug, or null for the hub, the kit or a package. */
|
|
33
|
+
export type Owner = string | null;
|
|
34
|
+
/**
|
|
35
|
+
* One source of a built asset's content: a file, or for a stylesheet a CSS
|
|
36
|
+
* module it holds. `writer` is the owner of the chunk that wrote a
|
|
37
|
+
* stylesheet, whose name Vite took from that chunk.
|
|
38
|
+
*/
|
|
39
|
+
export type Source = {
|
|
40
|
+
owner: Owner;
|
|
41
|
+
file: string;
|
|
42
|
+
writer?: Owner;
|
|
43
|
+
};
|
|
44
|
+
/** Where one owner's copy of an asset lives. */
|
|
45
|
+
export type Placement = {
|
|
46
|
+
owner: Owner;
|
|
47
|
+
fileName: string;
|
|
48
|
+
};
|
|
49
|
+
/** Files the build names on purpose: never moved, copied, renamed or read for references. */
|
|
50
|
+
export declare const isFixed: (fileName: string) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Where an asset must be, one file per owner of its content: `_p/<slug>/`
|
|
53
|
+
* for a prototype, `assets/` for the hub or the kit. Null when it is already
|
|
54
|
+
* there or nothing shows where its content comes from.
|
|
55
|
+
*/
|
|
56
|
+
export declare function placementsOf(fileName: string, sources: Source[] | null, slugs: readonly string[], hash: string): Placement[] | null;
|
|
57
|
+
/**
|
|
58
|
+
* What a reader gets for a file it reads: its own owner's copy, else the
|
|
59
|
+
* hub's (anyone may read the kit), else nothing, which the build refuses.
|
|
60
|
+
*/
|
|
61
|
+
export declare function copyFor(places: Placement[], reader: Owner): Placement | null;
|
|
62
|
+
/** The error for a file that reads what another prototype owns, naming reader and source. */
|
|
63
|
+
export declare const readsAcross: (reader: string, source: Source) => string;
|
|
64
|
+
/** The error for a reach into `prototypes/` outside any prototype's folder. */
|
|
65
|
+
export declare const looseFile: (root: string, importer: string, file: string) => string;
|
|
66
|
+
/** A stylesheet language whose own `@import` and `@use` resolve inside its preprocessor. */
|
|
67
|
+
export declare const PREPROCESSED: RegExp;
|
|
68
|
+
/** The error for a prototype or the project's kit/ importing Sass, Less or Stylus. */
|
|
69
|
+
export declare const preprocessed: (root: string, importer: string, file: string) => string;
|
|
70
|
+
/** The error for a stylesheet the build cannot read. */
|
|
71
|
+
export declare const unreadable: (root: string, file: string, line: number, column: number, reason: string) => string;
|
|
72
|
+
/**
|
|
73
|
+
* The paths a module names with `new URL('…', import.meta.url)`, found with
|
|
74
|
+
* the parser Vite itself uses (oxc, through `parseSync`), on the code Vite's
|
|
75
|
+
* own `new URL` plugin sees: TypeScript and JSX already compiled. So a quote
|
|
76
|
+
* in a regex literal, in JSX text, in a string or in a comment never hides
|
|
77
|
+
* one or fakes one. A template with `${…}` becomes an import.meta.glob, which
|
|
78
|
+
* the resolver sees; a data URL and one marked `@vite-ignore` are skipped, as
|
|
79
|
+
* Vite skips them. The query stays: `?inline` is a reach too. Null when the
|
|
80
|
+
* code does not parse (Vite then reports the error itself).
|
|
81
|
+
*/
|
|
82
|
+
export declare function newUrlTargets(code: string, file: string): string[] | null;
|
|
83
|
+
/** The error for a built file with no record of where its content comes from. */
|
|
84
|
+
export declare const unrecorded: (fileName: string) => string;
|
|
85
|
+
/** The error for a worker that reaches a prototype's folder. */
|
|
86
|
+
export declare const workerRefused: (root: string, file: string) => string;
|
|
87
|
+
/**
|
|
88
|
+
* A file's text with every reference to `from` pointed at `to`: the whole
|
|
89
|
+
* path, and the path relative to the referring file when that differs.
|
|
90
|
+
*/
|
|
91
|
+
export declare function repointText(text: string, referrer: string, from: string, to: string): string;
|
|
92
|
+
/** A chunk where its modules say: `_p/<slug>/` for one prototype's, never two. */
|
|
93
|
+
export declare function chunkProblems(fileName: string, owners: string[]): string[];
|