@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/README.md
CHANGED
|
@@ -35,7 +35,7 @@ A feature group, top to bottom:
|
|
|
35
35
|
1. **Heading.** The feature's name. `Walkthrough →` ends the line when `docs/sprints/<feature>/guide.md` exists.
|
|
36
36
|
2. **Primary.** The one `sprint` row, its title 16px against 14px for every other row. Size alone marks it; no badge, no new shape.
|
|
37
37
|
3. **Alternates.** Under an `Alternates` caption (12px, 500, quaternary text), indented 20px and joined to the primary by a 1px rail in `--border-secondary` on the primary's text edge. The rail makes them children of that one row; a heading of their own would make them peers of the feature. A stray second `sprint` renders here too.
|
|
38
|
-
4. **
|
|
38
|
+
4. **Explorations.** A toggle (`Explorations`, the count, a chevron), collapsed by default because explorations are working material, not the feature. The open state persists per group in `localStorage` (`index-explorations-open`). Typing in the filter opens every fold, so a match is never hidden. Open, the explorations are cards on a grid (`minmax(208px, 1fr)`, 8px gap). Every exploration lives here, never in the row list.
|
|
39
39
|
|
|
40
40
|
Within any list: exploring above decided, then newest first.
|
|
41
41
|
|
|
@@ -49,6 +49,10 @@ Surfaces:
|
|
|
49
49
|
- **Foot.** The count alone, in body type over a hairline (the Ledger Foot Rule).
|
|
50
50
|
- **Lede.** 18px/28px Geist at 400, secondary text. `--font-voice` resolves to Geist inside the hub's `.sys` scope (`shell/system.css`), so the index never borrows the project's faces.
|
|
51
51
|
|
|
52
|
+
### Audience
|
|
53
|
+
|
|
54
|
+
Every prototype is Internal until Studio marks it Client-facing; a Client's index lists only the client-facing ones, and the gate refuses an internal prototype's page and files to a Client. Studio, outside Client view, sees each prototype's audience in a fifth column beside its row (under a card), never inside the link: `Internal` on the internal comments' amber (`--sys-internal`), `Client-facing` in quaternary text, then a switch (`role="switch"`, a 28px track, its hit area grown to 40px). A switch shows at once and is undone, with a toast, when the save fails. **Client view** in the head shows the index as a client gets it, with the comment layer's note at the bottom and **Leave**; in it no switch and no audience word shows. A Client with nothing shared sees `Nothing shared yet`.
|
|
55
|
+
|
|
52
56
|
### Motion
|
|
53
57
|
|
|
54
58
|
Nothing animates on a keyboard action.
|
|
@@ -76,8 +80,9 @@ Every person is named, and signs in with a one-time link that Studio makes in th
|
|
|
76
80
|
- **A second device gets a second link** (`design-hub people link <id>`), and the first stays signed in. **Sign out everywhere** (`people sign-out <id>`) bumps the person's epoch, drops their unused links and returns one new link. **Revoke** ends their access everywhere.
|
|
77
81
|
- **What a stale write cannot undo.** A person is a profile (name, team, kind, access end), written read-modify-write, plus two records only revoke and sign out everywhere touch: the revocation, written once and never cleared, and the epoch, raised only by `HINCRBY`. So a profile change that started before a revoke or a sign out everywhere and lands after it never clears the revocation or lowers the epoch, and two sign outs everywhere at once raise it twice.
|
|
78
82
|
- **Access lasts the engagement.** Each person has an access end: 3 months, 6 months, or open-ended. The project default (`people access --access <length>`, or `bootstrap --review --access`) applies to a new Client and is 3 months when unset; a new Studio person is open-ended, and Studio can set one person's length from the People tab or `people access <id>`.
|
|
79
|
-
- **The session** is one cookie, `__Host-hub_person`: `HttpOnly`, `Secure`, `SameSite=Lax`, signed with HMAC-SHA256 over the person, their epoch, an expiry, `checkedAt` (when the store last said yes) and the review store's URL, which is signed but never written into the token, so two projects that share a `GATE_SECRET` never accept each other's cookies, and versioned (`
|
|
83
|
+
- **The session** is one cookie, `__Host-hub_person`: `HttpOnly`, `Secure`, `SameSite=Lax`, signed with HMAC-SHA256 over the person, their epoch, their kind (Studio or Client), an expiry, `checkedAt` (when the store last said yes) and the review store's URL, which is signed but never written into the token, so two projects that share a `GATE_SECRET` never accept each other's cookies, and versioned (`p4`), so a cookie from an older release never verifies. The one exception is `p3`, the version before the kind: a valid one still passes, read as stale, so the next request asks the store and re-issues it as `p4`, and nobody is signed out by the upgrade. It lasts to the access end, at most 400 days, the longest a browser keeps one. For `CHECK_SECONDS` (5 minutes) after `checkedAt` any request passes on the signature. After that the next request, of any kind, asks the store: a current person passes with a fresh cookie whose expiry follows their current access end; a revoked, signed-out-everywhere or ended person gets 401 and the cookie cleared. The request's own headers never decide whether the store is asked, since the client chooses them. A store that fails answers 503 with `Retry-After`, and leaves the cookie, so an outage signs nobody out; the review panel reads `Retry-After` as try again, and a 503 without it as a site with no store. The API function answers the same way in JSON. The sign-in POST also refuses a store failure with 503, and a link is checked before it is used up, so a failure never burns one.
|
|
80
84
|
- **The link page** sends `Referrer-Policy: same-origin`, so Continue's POST carries a real `Origin` (with `no-referrer` a browser sends `Origin: null`, trusted only beside `Sec-Fetch-Site`, which older Safari does not send). Every other gate response sends `no-referrer`.
|
|
85
|
+
- **Internal prototypes.** Everything built from `prototypes/<slug>/` (its README, code, CSS, fixtures, lazy chunks and images) lands under `/_p/<slug>/`, its entry at `/_p/<slug>/entry.js`, and Studio's list of every slug at `/_p/all.js`; the shared bundle in `/assets/` names no prototype's folder (`vite/registry.ts`, `vite/boundary.ts`, `vite/settle.ts`; the build fails if a file breaks that). The exception is the walkthroughs: their compiled text and every feature's name sit in `/assets/` and reach every signed-in person, so a walkthrough must not name or describe internal work until that is built. A built file is owned by where its content comes from, never by who reads it, and each source is recorded as the file is written (`vite/settle.ts`): a stylesheet's CSS modules, an asset's original files, a worker's output; a file with no record and no fixed name fails the build. Nothing a prototype owns is inlined by the size limit, and nothing under `prototypes/` is ever the hub's: a file directly there, or in a folder without a README, is refused (shared code belongs in `kit/`). Any import or `new URL` (`?inline` included) into a prototype its importer does not own is refused in dev and in a build. Every stylesheet is read for reaches inside Vite's postcss run (`vite/css-boundary.ts`), each reference followed as a path and through Vite's own resolvers (aliases and package paths included); Sass, Less and Stylus are refused in prototypes and the project's `kit/`, since their imports resolve inside the preprocessor; and every worker bundle refuses a module under `prototypes/` and resolves `@kit/*`. After the bundle is settled the build reads what it wrote (`vite/leaks.ts`): a file outside `/_p/<slug>/` that holds a whole file of `prototypes/<slug>/` (its bytes, a base64 or SVG data URI of it anywhere in the file, or, for a file the build read, its whole text as a string in a script; 40 bytes or more) fails the build, unless the reader's owner has a source with the same content. It is a backstop for whole-file inlining: no part of a file (a line, a rule) is looked for. Vite is pinned to 8.3 (`~8.3.5`) because the boundary leans on its internals; `vite/build.test.ts` is what to run on a Vite upgrade. A file a prototype owns lives in its folder; the hub's and the kit's live in `/assets/` and are never copied into a prototype's folder; the same bytes from several owners become one copy per owner, each named from its owner's source. A file that reads what another prototype owns, or a hub or kit file that reads what a prototype owns, fails the build, naming both. A prototype that reaches into another (an import, a CSS `@import` or `url()`, a `new URL`) or starts a web worker fails the build. A Client asking for a prototype's page (`/<slug>/`) or anything under `/_p/<slug>/` gets 404 unless that prototype is client-facing, asked of the store on every such request; `/_p/all.js` is always 404 to a Client, and an internal prototype and one that does not exist answer byte for byte the same. A path that could mean more than one file (a `.` or `..` segment, a backslash, an empty segment, a `%` left after decoding, or one that does not decode) gets the same 404, as does any first segment that is not `/`, the hub's own (`assets`, `api`, `guide`, `_gate`, `index.html`, `favicon.svg`), `_p` or a prototype slug. A Client's comments on a prototype that is not client-facing answer as if it had none. Studio passes on the cookie's kind with no store read. `/`, `/assets/*`, `/guide/*` and `/api/*` pass as before. `guide`, `api` and `assets` are never a prototype's slug (`core/prototype.ts`).
|
|
81
86
|
- **Signed out**, a page gets the sign-in page (401). It has no form: it says to open the link the studio sent, that each link signs in one device and then stops working, and to ask the studio for a new one for another device. Anything else gets a plain-text 401, an anonymous `POST` included.
|
|
82
87
|
|
|
83
88
|
The middleware reads one environment variable, set in the Vercel project for Production and Preview and never committed (`.env.example` names it): `GATE_SECRET`, at least 32 random characters that sign the session cookie. With it missing the gate answers 503 on every path, so a misconfigured deploy never serves the prototypes open. People and links live in the review store, so a site without it has a gate nobody can pass. The gate pages ship a hash-based Content Security Policy with no `unsafe-inline` and no script, plus `font-src 'self'` and `img-src 'self'`, and send no referrer. They wear the hub's neutral look in Geist, so two files pass the gate without a session: `/_gate/geist.woff2` (the latin subset, emitted by `vite/plugin.ts` at that fixed path) and `/favicon.svg`. Nothing else does. Every response carries `X-Robots-Tag: noindex`, so no search engine lists the site. The pages' title is the `siteName` the project's `proxy.ts` passes to `createProxy`. Under `design-hub dev` there is no gate; `/_gate/` previews the sign-in page, `?page=link` a link's page and `?page=used` a used one.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import { DialStore } from 'dialkit';
|
|
4
|
+
import {
|
|
5
|
+
client,
|
|
6
|
+
clientVariants,
|
|
7
|
+
controlField,
|
|
8
|
+
defaultOf,
|
|
9
|
+
dialAudience,
|
|
10
|
+
isControlId,
|
|
11
|
+
keepDials,
|
|
12
|
+
type Marks,
|
|
13
|
+
NO_MARKS,
|
|
14
|
+
readConfig,
|
|
15
|
+
readMarks,
|
|
16
|
+
withDefaults,
|
|
17
|
+
withMark,
|
|
18
|
+
} from './controls.ts';
|
|
19
|
+
|
|
20
|
+
const select = { type: 'select', options: ['comfortable', 'compact'], default: 'comfortable' };
|
|
21
|
+
|
|
22
|
+
test('a dial is internal until marked: in code with client(), or in the hub, which overrules the code', () => {
|
|
23
|
+
const { dials } = readConfig('Review', {
|
|
24
|
+
fixtures: select,
|
|
25
|
+
density: client(select),
|
|
26
|
+
avatars: client(true),
|
|
27
|
+
});
|
|
28
|
+
assert.deepEqual(dials, [
|
|
29
|
+
{ id: 'Review/fixtures', path: ['fixtures'], inCode: 'internal' },
|
|
30
|
+
{ id: 'Review/density', path: ['density'], inCode: 'client' },
|
|
31
|
+
{ id: 'Review/avatars', path: ['avatars'], inCode: 'client' },
|
|
32
|
+
]);
|
|
33
|
+
const marks: Marks = { dials: { 'Review/fixtures': 'client', 'Review/density': 'internal' }, variants: [] };
|
|
34
|
+
const audience = Object.fromEntries(dials.map((d) => [d.id, dialAudience(marks, d.id, d.inCode)]));
|
|
35
|
+
assert.deepEqual(audience, {
|
|
36
|
+
'Review/fixtures': 'client',
|
|
37
|
+
'Review/density': 'internal',
|
|
38
|
+
'Review/avatars': 'client',
|
|
39
|
+
});
|
|
40
|
+
assert.equal(dialAudience(NO_MARKS, 'Review/fixtures', 'internal'), 'internal');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('readConfig unwraps every client() and reads folders, a marked folder marking each dial in it', () => {
|
|
44
|
+
const { config, dials } = readConfig('Card', {
|
|
45
|
+
radius: [8, 0, 24, 1],
|
|
46
|
+
shadow: client({ blur: [12, 0, 40, 1], _collapsed: true, tint: { hue: 200 } }),
|
|
47
|
+
motion: { rise: client([220, 0, 600, 10]), ease: { type: 'easing', ease: [0, 0, 1, 1], duration: 0.2 } },
|
|
48
|
+
});
|
|
49
|
+
assert.deepEqual(config, {
|
|
50
|
+
radius: [8, 0, 24, 1],
|
|
51
|
+
shadow: { blur: [12, 0, 40, 1], _collapsed: true, tint: { hue: 200 } },
|
|
52
|
+
motion: { rise: [220, 0, 600, 10], ease: { type: 'easing', ease: [0, 0, 1, 1], duration: 0.2 } },
|
|
53
|
+
});
|
|
54
|
+
assert.deepEqual(
|
|
55
|
+
dials.map((d) => `${d.id} ${d.inCode}`),
|
|
56
|
+
[
|
|
57
|
+
'Card/radius internal',
|
|
58
|
+
'Card/shadow.blur client',
|
|
59
|
+
'Card/shadow.tint.hue client',
|
|
60
|
+
'Card/motion.rise client',
|
|
61
|
+
'Card/motion.ease internal',
|
|
62
|
+
],
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('keepDials leaves out every dial not shown, and a folder with none left', () => {
|
|
67
|
+
const { config } = readConfig('Card', {
|
|
68
|
+
radius: [8, 0, 24, 1],
|
|
69
|
+
shadow: { _collapsed: true, blur: [12, 0, 40, 1], spread: 2 },
|
|
70
|
+
motion: { rise: [220, 0, 600, 10] },
|
|
71
|
+
});
|
|
72
|
+
const kept = keepDials('Card', config, (id) => id === 'Card/shadow.blur');
|
|
73
|
+
assert.deepEqual(kept, { shadow: { blur: [12, 0, 40, 1], _collapsed: true } });
|
|
74
|
+
assert.deepEqual(
|
|
75
|
+
keepDials('Card', config, () => false),
|
|
76
|
+
{},
|
|
77
|
+
);
|
|
78
|
+
assert.deepEqual(
|
|
79
|
+
keepDials('Card', config, () => true),
|
|
80
|
+
config,
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Every kind of dial DialKit has, with and without a default of its own.
|
|
85
|
+
const EVERY = {
|
|
86
|
+
bare: 0.4,
|
|
87
|
+
slider: [220, 0, 600, 10],
|
|
88
|
+
outside: [900, 0, 600, 10],
|
|
89
|
+
swapped: [5, 10, 0],
|
|
90
|
+
open: [7],
|
|
91
|
+
toggle: true,
|
|
92
|
+
word: 'hello',
|
|
93
|
+
chosen: { type: 'select', options: ['a', 'b'], default: 'b' },
|
|
94
|
+
first: { type: 'select', options: [{ value: 'x', label: 'X' }, 'y'] },
|
|
95
|
+
none: { type: 'select', options: [] },
|
|
96
|
+
color: { type: 'color', default: '#ff0000' },
|
|
97
|
+
black: { type: 'color' },
|
|
98
|
+
text: { type: 'text', default: 'Title' },
|
|
99
|
+
blank: { type: 'text' },
|
|
100
|
+
image: { type: 'image', options: ['/a.png', '/b.png'] },
|
|
101
|
+
pad: { type: 'pad', x: [0.333, 0, 1, 0.01], y: [5, -10, 10] },
|
|
102
|
+
centre: { type: 'pad' },
|
|
103
|
+
spring: { type: 'spring', visualDuration: 0.3, bounce: 0.2 },
|
|
104
|
+
ease: { type: 'easing', ease: [0.2, 0, 0, 1], duration: 0.3 },
|
|
105
|
+
folder: { depth: [2, 0, 8, 1], inner: { on: false } },
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
test('defaultOf is DialKit’s own starting value, for every kind of dial', () => {
|
|
109
|
+
DialStore.registerPanel('every', 'Every', EVERY as any);
|
|
110
|
+
const flat = DialStore.getValues('every') as Record<string, unknown>;
|
|
111
|
+
DialStore.unregisterPanel('every');
|
|
112
|
+
const ours = withDefaults(EVERY, {});
|
|
113
|
+
const flatten = (node: Record<string, unknown>, prefix = ''): Record<string, unknown> =>
|
|
114
|
+
Object.fromEntries(
|
|
115
|
+
Object.entries(node).flatMap(([k, v]) =>
|
|
116
|
+
prefix === '' && k === 'folder'
|
|
117
|
+
? Object.entries(flatten(v as Record<string, unknown>, 'folder.'))
|
|
118
|
+
: k === 'inner'
|
|
119
|
+
? Object.entries(flatten(v as Record<string, unknown>, `${prefix}inner.`))
|
|
120
|
+
: [[prefix + k, v]],
|
|
121
|
+
),
|
|
122
|
+
);
|
|
123
|
+
assert.deepEqual(flatten(ours), flat);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('withDefaults keeps the values DialKit gave and fills the dials it did not draw', () => {
|
|
127
|
+
const config = { fixtures: select, density: select, shadow: { blur: [12, 0, 40, 1], spread: 2 } };
|
|
128
|
+
assert.deepEqual(withDefaults(config, { density: 'compact', shadow: { blur: 30 } }), {
|
|
129
|
+
fixtures: 'comfortable',
|
|
130
|
+
density: 'compact',
|
|
131
|
+
shadow: { blur: 30, spread: 2 },
|
|
132
|
+
});
|
|
133
|
+
assert.equal(defaultOf([3, 0, 10, 1]), 3);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('a Client gets the variants Studio marked, in the prototype’s order, and the first when none is', () => {
|
|
137
|
+
const names = ['Baseline', 'Lift', 'Sink'];
|
|
138
|
+
assert.deepEqual(clientVariants(names, NO_MARKS), ['Baseline']);
|
|
139
|
+
assert.deepEqual(clientVariants(names, { dials: {}, variants: ['Sink', 'Lift'] }), ['Lift', 'Sink']);
|
|
140
|
+
// A mark for a variant the prototype no longer has counts for nothing.
|
|
141
|
+
assert.deepEqual(clientVariants(names, { dials: {}, variants: ['Gone'] }), ['Baseline']);
|
|
142
|
+
assert.deepEqual(clientVariants([], NO_MARKS), []);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('marks read from the store ignore what is not theirs, and withMark switches one', () => {
|
|
146
|
+
const marks = readMarks({
|
|
147
|
+
[controlField('dial', 'Review/density')]: 'client',
|
|
148
|
+
[controlField('dial', 'Review/fixtures')]: 'internal',
|
|
149
|
+
[controlField('dial', 'Review/odd')]: 'yes',
|
|
150
|
+
[controlField('variant', 'Lift')]: 'client',
|
|
151
|
+
[controlField('variant', 'Sink')]: 'internal',
|
|
152
|
+
'thing:x': 'client',
|
|
153
|
+
'dial:': 'client',
|
|
154
|
+
plain: 'client',
|
|
155
|
+
});
|
|
156
|
+
assert.deepEqual(marks, {
|
|
157
|
+
dials: { 'Review/density': 'client', 'Review/fixtures': 'internal' },
|
|
158
|
+
variants: ['Lift'],
|
|
159
|
+
});
|
|
160
|
+
const next = withMark(
|
|
161
|
+
withMark(marks, 'variant', 'Baseline', 'client'),
|
|
162
|
+
'dial',
|
|
163
|
+
'Review/density',
|
|
164
|
+
'internal',
|
|
165
|
+
);
|
|
166
|
+
assert.deepEqual(next.variants, ['Baseline', 'Lift']);
|
|
167
|
+
assert.equal(next.dials['Review/density'], 'internal');
|
|
168
|
+
assert.deepEqual(withMark(next, 'variant', 'Lift', 'internal').variants, ['Baseline']);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('a control id is one trimmed line of at most 200 characters', () => {
|
|
172
|
+
for (const ok of ['Review/density', 'Outline, as legal asked', 'a', 'x'.repeat(200)])
|
|
173
|
+
assert.ok(isControlId(ok), ok);
|
|
174
|
+
for (const bad of ['', ' a', 'a ', 'a\nb', 'a\u0000', 'x'.repeat(201), 3, null, undefined])
|
|
175
|
+
assert.ok(!isControlId(bad), String(bad));
|
|
176
|
+
});
|
package/core/controls.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// Which of a prototype's controls a Client gets: its dials and its variants.
|
|
2
|
+
// Pure, so the server, the kit and the tests read one set of rules.
|
|
3
|
+
//
|
|
4
|
+
// A dial is internal until marked client-facing. It is marked in two places:
|
|
5
|
+
// in code, with `client(...)` around the dial where it is declared, which is
|
|
6
|
+
// where it starts; and in the hub, by Studio, which is stored per deployed
|
|
7
|
+
// site and overrules the code either way. A variant is marked only in the
|
|
8
|
+
// hub. The View tools are never asked about: they are always a Client's.
|
|
9
|
+
//
|
|
10
|
+
// The mark decides what a Client's panel draws and nothing else. A prototype's
|
|
11
|
+
// code is one file a Client's browser loads, so an internal dial's name, range
|
|
12
|
+
// and default are still in it; its value stays at the default.
|
|
13
|
+
|
|
14
|
+
export type ControlAudience = 'internal' | 'client';
|
|
15
|
+
|
|
16
|
+
export const CONTROL_KINDS = ['dial', 'variant'] as const;
|
|
17
|
+
export type ControlKind = (typeof CONTROL_KINDS)[number];
|
|
18
|
+
|
|
19
|
+
/** What the hub stored for one prototype: a mark per dial Studio switched, and the variants a Client gets. */
|
|
20
|
+
export type Marks = { dials: Record<string, ControlAudience>; variants: string[] };
|
|
21
|
+
|
|
22
|
+
export const NO_MARKS: Marks = { dials: {}, variants: [] };
|
|
23
|
+
|
|
24
|
+
const MAX_ID = 200;
|
|
25
|
+
|
|
26
|
+
/** A dial's or a variant's name as the store keeps it: one line of text, trimmed, at most 200 characters. */
|
|
27
|
+
export const isControlId = (v: unknown): v is string =>
|
|
28
|
+
typeof v === 'string' &&
|
|
29
|
+
v.length > 0 &&
|
|
30
|
+
v.length <= MAX_ID &&
|
|
31
|
+
v.trim() === v &&
|
|
32
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what is refused
|
|
33
|
+
!/[\u0000-\u001f\u007f]/.test(v);
|
|
34
|
+
|
|
35
|
+
/** A dial is named by its panel and its key, a folder's dial by the keys down to it: `Review/density`, `Card/shadow.blur`. */
|
|
36
|
+
export const dialId = (panel: string, path: readonly string[]) => `${panel}/${path.join('.')}`;
|
|
37
|
+
|
|
38
|
+
/** The store's field for one control. */
|
|
39
|
+
export const controlField = (kind: ControlKind, id: string) => `${kind}:${id}`;
|
|
40
|
+
|
|
41
|
+
/** The marks in a prototype's stored fields. A field or a value that is not one of ours is left out, so it reads as unmarked. */
|
|
42
|
+
export function readMarks(fields: Record<string, string>): Marks {
|
|
43
|
+
const dials: Record<string, ControlAudience> = {};
|
|
44
|
+
const variants: string[] = [];
|
|
45
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
46
|
+
const at = field.indexOf(':');
|
|
47
|
+
const [kind, id] = [field.slice(0, at), field.slice(at + 1)];
|
|
48
|
+
if (!isControlId(id)) continue;
|
|
49
|
+
if (kind === 'dial' && (value === 'client' || value === 'internal')) dials[id] = value;
|
|
50
|
+
if (kind === 'variant' && value === 'client') variants.push(id);
|
|
51
|
+
}
|
|
52
|
+
return { dials, variants: variants.sort() };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The marks after one control is switched. */
|
|
56
|
+
export function withMark(marks: Marks, kind: ControlKind, id: string, audience: ControlAudience): Marks {
|
|
57
|
+
if (kind === 'dial') return { ...marks, dials: { ...marks.dials, [id]: audience } };
|
|
58
|
+
const rest = marks.variants.filter((v) => v !== id);
|
|
59
|
+
return { ...marks, variants: audience === 'client' ? [...rest, id].sort() : rest };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Who a dial is for: the hub's mark when Studio set one, else what the code says, else internal. */
|
|
63
|
+
export const dialAudience = (marks: Marks, id: string, inCode: ControlAudience): ControlAudience =>
|
|
64
|
+
marks.dials[id] ?? inCode;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The variants a Client gets, in the order the prototype declares them. One
|
|
68
|
+
* is no picker. None marked is still one: the first, so a page is never empty.
|
|
69
|
+
*/
|
|
70
|
+
export function clientVariants(names: readonly string[], marks: Marks): string[] {
|
|
71
|
+
const kept = names.filter((n) => marks.variants.includes(n));
|
|
72
|
+
return kept.length ? kept : names.slice(0, 1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The config a prototype hands to useDials, read without DialKit's types: a
|
|
76
|
+
// leaf is a dial, an object without a `type` is a folder of them.
|
|
77
|
+
|
|
78
|
+
type Config = Record<string, unknown>;
|
|
79
|
+
type Marked = { __client: unknown };
|
|
80
|
+
|
|
81
|
+
const isRecord = (v: unknown): v is Config => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
82
|
+
const isMarked = (v: unknown): v is Marked => isRecord(v) && '__client' in v;
|
|
83
|
+
const isFolder = (v: unknown): v is Config => isRecord(v) && !('type' in v);
|
|
84
|
+
/** DialKit's own key for a folder that starts closed: not a dial. */
|
|
85
|
+
const SETTING = '_collapsed';
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Marks a dial client-facing where it is declared, or a folder and every dial
|
|
89
|
+
* in it. Typed as the dial itself, so the values a prototype reads keep their types.
|
|
90
|
+
*/
|
|
91
|
+
export const client = <T>(dial: T): T => ({ __client: dial }) as T;
|
|
92
|
+
|
|
93
|
+
export type Dial = { id: string; path: string[]; inCode: ControlAudience };
|
|
94
|
+
|
|
95
|
+
/** A config as DialKit takes it (every `client()` unwrapped) and the dials in it. */
|
|
96
|
+
export function readConfig(panel: string, config: Config): { config: Config; dials: Dial[] } {
|
|
97
|
+
const dials: Dial[] = [];
|
|
98
|
+
const walk = (node: Config, path: string[], inherited: ControlAudience): Config => {
|
|
99
|
+
const out: Config = {};
|
|
100
|
+
for (const [key, raw] of Object.entries(node)) {
|
|
101
|
+
if (key === SETTING) {
|
|
102
|
+
out[key] = raw;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const inCode: ControlAudience = isMarked(raw) ? 'client' : inherited;
|
|
106
|
+
const value = isMarked(raw) ? raw.__client : raw;
|
|
107
|
+
const at = [...path, key];
|
|
108
|
+
if (isFolder(value)) out[key] = walk(value, at, inCode);
|
|
109
|
+
else {
|
|
110
|
+
out[key] = value;
|
|
111
|
+
dials.push({ id: dialId(panel, at), path: at, inCode });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
};
|
|
116
|
+
return { config: walk(config, [], 'internal'), dials };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The part of an unwrapped config whose dials `shown` keeps. A folder with none left goes with them. */
|
|
120
|
+
export function keepDials(panel: string, config: Config, shown: (id: string) => boolean): Config {
|
|
121
|
+
const walk = (node: Config, path: string[]): Config => {
|
|
122
|
+
const out: Config = {};
|
|
123
|
+
for (const [key, value] of Object.entries(node)) {
|
|
124
|
+
if (key === SETTING) continue;
|
|
125
|
+
const at = [...path, key];
|
|
126
|
+
if (isFolder(value)) {
|
|
127
|
+
const inner = walk(value, at);
|
|
128
|
+
if (Object.keys(inner).length)
|
|
129
|
+
out[key] = SETTING in value ? { ...inner, [SETTING]: value[SETTING] } : inner;
|
|
130
|
+
} else if (shown(dialId(panel, at))) out[key] = value;
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
};
|
|
134
|
+
return walk(config, []);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
|
138
|
+
const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v));
|
|
139
|
+
|
|
140
|
+
/** A slider's starting value: its first number, kept inside a range that is given. */
|
|
141
|
+
function sliderDefault(value: readonly unknown[]): number {
|
|
142
|
+
const seed = finite(value[0]) ? value[0] : 0;
|
|
143
|
+
if (!finite(value[1]) || !finite(value[2])) return seed;
|
|
144
|
+
const [min, max] = value[1] <= value[2] ? [value[1], value[2]] : [value[2], value[1]];
|
|
145
|
+
return clamp(seed, min, min === max ? min + 1 : max);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const firstOption = (options: unknown): unknown => {
|
|
149
|
+
const first = Array.isArray(options) ? options[0] : undefined;
|
|
150
|
+
if (first === undefined) return '';
|
|
151
|
+
return typeof first === 'string' ? first : (first as { value: unknown }).value;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** One axis of a pad: `[default, min, max, step?]`, the default snapped to the step as DialKit snaps it. */
|
|
155
|
+
function padAxis(axis: unknown): number {
|
|
156
|
+
const [initial, min, max, given] = (Array.isArray(axis) ? axis : [0, -1, 1, 0.01]) as number[];
|
|
157
|
+
const step = given ?? (max - min) / 200;
|
|
158
|
+
const at = clamp(initial, min, max);
|
|
159
|
+
if (at === min || at === max) return at;
|
|
160
|
+
return clamp(Number((min + Math.round((at - min) / step) * step).toPrecision(12)), min, max);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** By `type`: what DialKit starts each kind of dial at when its config names no default. */
|
|
164
|
+
const TYPED: Record<string, (v: Config) => unknown> = {
|
|
165
|
+
select: (v) => v.default ?? firstOption(v.options),
|
|
166
|
+
color: (v) => v.default ?? '#000000',
|
|
167
|
+
image: (v) => v.default ?? firstOption(v.options ?? []),
|
|
168
|
+
text: (v) => v.default ?? '',
|
|
169
|
+
pad: (v) => ({ x: padAxis(v.x), y: padAxis(v.y) }),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* What a dial reads when nobody can move it: the value DialKit would start it
|
|
174
|
+
* at. A test holds this equal to DialKit's own for every kind of dial.
|
|
175
|
+
*/
|
|
176
|
+
export function defaultOf(dial: unknown): unknown {
|
|
177
|
+
if (Array.isArray(dial)) return sliderDefault(dial);
|
|
178
|
+
if (!isRecord(dial) || typeof dial.type !== 'string') return dial;
|
|
179
|
+
const typed = TYPED[dial.type];
|
|
180
|
+
return typed ? typed(dial) : dial;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The values a prototype reads: DialKit's for the dials it drew, the default
|
|
185
|
+
* for every dial it did not, in the shape of the whole config.
|
|
186
|
+
*/
|
|
187
|
+
export function withDefaults(config: Config, values: Config): Config {
|
|
188
|
+
const out: Config = {};
|
|
189
|
+
for (const [key, dial] of Object.entries(config)) {
|
|
190
|
+
if (key === SETTING) continue;
|
|
191
|
+
if (isFolder(dial)) out[key] = withDefaults(dial, isRecord(values[key]) ? values[key] : {});
|
|
192
|
+
else out[key] = key in values ? values[key] : defaultOf(dial);
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import { ALL_FILE, entryFile, isPrototypeSlug, probeEntry, prototypeOfPath, RESERVED } from './prototype.ts';
|
|
4
|
+
|
|
5
|
+
test('a prototype slug is kebab-case and never one of the hub’s own paths', () => {
|
|
6
|
+
assert.deepEqual([...RESERVED], ['guide', 'api', 'assets']);
|
|
7
|
+
for (const reserved of RESERVED) assert.equal(isPrototypeSlug(reserved), false);
|
|
8
|
+
assert.equal(isPrototypeSlug('sample-list'), true);
|
|
9
|
+
for (const bad of ['_p', 'Sample', 'a_b', '', 'a.js', 'a/b', null, 3])
|
|
10
|
+
assert.equal(isPrototypeSlug(bad), false);
|
|
11
|
+
assert.equal(entryFile('sample-list'), '_p/sample-list/entry.js');
|
|
12
|
+
assert.equal(ALL_FILE, '_p/all.js');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('a path names its prototype, or none for the hub’s own', () => {
|
|
16
|
+
const cases: [string, string | null][] = [
|
|
17
|
+
['/', null],
|
|
18
|
+
['/assets/index-abc.js', null],
|
|
19
|
+
['/guide/sample/', null],
|
|
20
|
+
['/api/review', null],
|
|
21
|
+
['/favicon.svg', null],
|
|
22
|
+
['/_gate/geist.woff2', null],
|
|
23
|
+
['/index.html', null],
|
|
24
|
+
['/robots.txt', ''],
|
|
25
|
+
['/invite/short', 'invite'],
|
|
26
|
+
['/Sample-List/', ''],
|
|
27
|
+
['/_p;x/secret/entry.js', ''],
|
|
28
|
+
['/_p./secret/entry.js', ''],
|
|
29
|
+
['/_p%20/secret/entry.js', ''],
|
|
30
|
+
['/_p%00/secret/entry.js', ''],
|
|
31
|
+
['/%EF%BC%BFp/secret/entry.js', ''],
|
|
32
|
+
['/_p/secret/entry.js', ''],
|
|
33
|
+
['/assets;x/y.js', ''],
|
|
34
|
+
['/sample-list/', 'sample-list'],
|
|
35
|
+
['/sample-list', 'sample-list'],
|
|
36
|
+
['/sample-list/deep/path', 'sample-list'],
|
|
37
|
+
['/_p/sample-list/entry.js', 'sample-list'],
|
|
38
|
+
['/_p/sample-list', 'sample-list'],
|
|
39
|
+
['/_p/all.js', 'all.js'],
|
|
40
|
+
['/_p/', ''],
|
|
41
|
+
['/_p', ''],
|
|
42
|
+
['//_p//sample-list/x.js', ''],
|
|
43
|
+
['/assets/..%2f_p/secret/entry.js', ''],
|
|
44
|
+
['/api/..%2f_p/secret/entry.js', ''],
|
|
45
|
+
['/_p/open/..%2fsecret/entry.js', ''],
|
|
46
|
+
['/_p/open/.%2fentry.js', ''],
|
|
47
|
+
['/assets//x.js', ''],
|
|
48
|
+
['/_P/secret/entry.js', ''],
|
|
49
|
+
['/_P/all.js', ''],
|
|
50
|
+
['/%255Fp/secret/entry.js', ''],
|
|
51
|
+
['/_p%5csecret%5centry.js', ''],
|
|
52
|
+
['/assets%5cx.js', ''],
|
|
53
|
+
['/assets/a%20b.png', null],
|
|
54
|
+
['/assets/caf%C3%A9.png', null],
|
|
55
|
+
['/_p/secret/caf%C3%A9%20x.png', 'secret'],
|
|
56
|
+
['/guide/', null],
|
|
57
|
+
['/%5Fp/sample-list/entry.js', 'sample-list'],
|
|
58
|
+
['/_p/sample%2Dlist/entry.js', 'sample-list'],
|
|
59
|
+
['/_p%2Fsample-list%2Fentry.js', 'sample-list'],
|
|
60
|
+
['/%73ample-list/', 'sample-list'],
|
|
61
|
+
['/%E0%A4%A/', ''],
|
|
62
|
+
];
|
|
63
|
+
for (const [path, owner] of cases) assert.equal(prototypeOfPath(path), owner, path);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('an entry is there on a JavaScript 200, not there on a 404 or the host’s page, and an error otherwise', async () => {
|
|
67
|
+
const asked: [string, RequestInit | undefined][] = [];
|
|
68
|
+
const answer = (status: number, type = 'text/javascript') =>
|
|
69
|
+
(async (url: string, init?: RequestInit) => {
|
|
70
|
+
asked.push([url, init]);
|
|
71
|
+
return new Response(null, { status, headers: { 'content-type': type } });
|
|
72
|
+
}) as unknown as typeof fetch;
|
|
73
|
+
assert.equal(await probeEntry('/_p/a/entry.js', answer(200)), true);
|
|
74
|
+
assert.equal(
|
|
75
|
+
await probeEntry('/_p/a/entry.js', answer(200, 'application/javascript; charset=utf-8')),
|
|
76
|
+
true,
|
|
77
|
+
);
|
|
78
|
+
assert.equal(await probeEntry('/_p/a/entry.js', answer(404)), false);
|
|
79
|
+
assert.equal(await probeEntry('/_p/a/entry.js', answer(200, 'text/html; charset=utf-8')), false);
|
|
80
|
+
for (const status of [401, 403, 500, 503])
|
|
81
|
+
await assert.rejects(probeEntry('/_p/a/entry.js', answer(status)), new RegExp(`answered ${status}`));
|
|
82
|
+
assert.deepEqual(asked[0], ['/_p/a/entry.js', { method: 'HEAD', credentials: 'same-origin' }]);
|
|
83
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// What a prototype's slug may be, and where the files built from it live.
|
|
2
|
+
// One rule for the registry's scan, `design-hub new` and the gate, so a
|
|
3
|
+
// folder the hub lists is always a path the gate knows how to refuse.
|
|
4
|
+
import { SLUG } from './feature.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* First path segments the hub owns, never a prototype: the walkthroughs,
|
|
8
|
+
* the review API, and the shared bundle.
|
|
9
|
+
*/
|
|
10
|
+
export const RESERVED = ['guide', 'api', 'assets'] as const;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
|
|
14
|
+
* Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
|
|
15
|
+
* has no underscore.
|
|
16
|
+
*/
|
|
17
|
+
export const PROTOTYPE_DIR = '_p';
|
|
18
|
+
export const ALL_FILE = `${PROTOTYPE_DIR}/all.js`;
|
|
19
|
+
export const entryFile = (slug: string) => `${PROTOTYPE_DIR}/${slug}/entry.js`;
|
|
20
|
+
|
|
21
|
+
/** Whether `name` can be a prototype's folder: kebab-case and not one of the hub's own paths. */
|
|
22
|
+
export const isPrototypeSlug = (name: unknown): name is string =>
|
|
23
|
+
typeof name === 'string' && SLUG.test(name) && !(RESERVED as readonly string[]).includes(name);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A path's segments as a file server might read them, or null when the path
|
|
27
|
+
* could mean more than one file: it does not decode, still holds a `%` after
|
|
28
|
+
* decoding (`%255F`), holds a backslash, a `.` or `..` segment, or an empty
|
|
29
|
+
* segment other than the last. The gate refuses those to a Client rather than
|
|
30
|
+
* guess how the server resolves them. An encoded space or letter decodes and passes.
|
|
31
|
+
*/
|
|
32
|
+
function segments(pathname: string): string[] | null {
|
|
33
|
+
let decoded: string;
|
|
34
|
+
try {
|
|
35
|
+
decoded = decodeURIComponent(pathname);
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (/[%\\]/.test(decoded)) return null;
|
|
40
|
+
const parts = decoded.split('/').slice(1);
|
|
41
|
+
if (parts.at(-1) === '') parts.pop();
|
|
42
|
+
return parts.some((p) => p === '' || p === '.' || p === '..') ? null : parts;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The first path segments that are the hub's own, for anyone signed in: the
|
|
47
|
+
* reserved routes, the gate's font folder, and the files the hub serves at
|
|
48
|
+
* the top (shell/index.html, shell/public/favicon.svg).
|
|
49
|
+
*/
|
|
50
|
+
const HUB_ROOT: readonly string[] = [...RESERVED, '_gate', 'index.html', 'favicon.svg'];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Which prototype a request path belongs to, for the gate: the name a
|
|
54
|
+
* prototype path carries, or null for a hub path (`/`, or a first segment in
|
|
55
|
+
* HUB_ROOT). The first segment, decoded, must be one of those, `_p`, or a
|
|
56
|
+
* prototype slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`)
|
|
57
|
+
* belongs to `''`, refused like an internal prototype. A path under `/_p/`
|
|
58
|
+
* always belongs to one, so its name may be no slug at all (`all.js`), which
|
|
59
|
+
* no audience makes client-facing. A path that could mean more than one file
|
|
60
|
+
* belongs to `''` too.
|
|
61
|
+
*/
|
|
62
|
+
export function prototypeOfPath(pathname: string): string | null {
|
|
63
|
+
const parts = segments(pathname);
|
|
64
|
+
if (!parts) return '';
|
|
65
|
+
const [first, second] = parts;
|
|
66
|
+
if (first === undefined || HUB_ROOT.includes(first)) return null;
|
|
67
|
+
if (first === PROTOTYPE_DIR) return second ?? '';
|
|
68
|
+
return isPrototypeSlug(first) ? first : '';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a built prototype's entry is there for this person, asked with a
|
|
73
|
+
* HEAD before it is imported. Not there: a 404 (the gate's answer for a
|
|
74
|
+
* prototype this person may not open, or none at all), or a 200 that is not
|
|
75
|
+
* JavaScript (the host's page for an entry that was never built). Any other
|
|
76
|
+
* answer (401, 500, 503) is an error, never "not there", so an outage never
|
|
77
|
+
* reads as nothing shared.
|
|
78
|
+
*/
|
|
79
|
+
export async function probeEntry(path: string, fetcher: typeof fetch): Promise<boolean> {
|
|
80
|
+
const head = await fetcher(path, { method: 'HEAD', credentials: 'same-origin' });
|
|
81
|
+
if (head.status === 404) return false;
|
|
82
|
+
if (head.status !== 200) throw new Error(`${path} answered ${head.status}.`);
|
|
83
|
+
return /javascript/.test(head.headers.get('content-type') ?? '');
|
|
84
|
+
}
|
package/core/review/model.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// enforced by the service, whichever adapter (Vercel, Vite dev, the CLI)
|
|
5
5
|
// calls it.
|
|
6
6
|
|
|
7
|
+
import { CONTROL_KINDS, type ControlKind, isControlId } from '../controls.ts';
|
|
7
8
|
import { SLUG } from '../feature.ts';
|
|
8
9
|
|
|
9
10
|
/** Which side a person is on: the studio that makes the work, or the client it is for. */
|
|
@@ -234,6 +235,37 @@ export function checkAudience(input: unknown): Checked<Audience | null> {
|
|
|
234
235
|
: { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
|
|
235
236
|
}
|
|
236
237
|
|
|
238
|
+
/** A prototype's audience as Studio sets it: one of AUDIENCES, required. */
|
|
239
|
+
export function checkPrototypeAudience(input: unknown): Checked<Audience> {
|
|
240
|
+
return AUDIENCES.includes(input as Audience)
|
|
241
|
+
? { ok: true, value: input as Audience }
|
|
242
|
+
: { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** One control's mark as Studio sets it: which kind, its id, and who it is for. */
|
|
246
|
+
export function checkControl(input: {
|
|
247
|
+
kind: unknown;
|
|
248
|
+
id: unknown;
|
|
249
|
+
audience: unknown;
|
|
250
|
+
}): Checked<{ kind: ControlKind; id: string; audience: Audience }> {
|
|
251
|
+
const problems: string[] = [];
|
|
252
|
+
if (!CONTROL_KINDS.includes(input.kind as ControlKind))
|
|
253
|
+
problems.push(`kind: one of ${CONTROL_KINDS.join(', ')}`);
|
|
254
|
+
if (!isControlId(input.id)) problems.push('id: the control’s name, one line of at most 200 characters');
|
|
255
|
+
if (!AUDIENCES.includes(input.audience as Audience))
|
|
256
|
+
problems.push(`audience: one of ${AUDIENCES.join(', ')}`);
|
|
257
|
+
return problems.length
|
|
258
|
+
? { ok: false, problems }
|
|
259
|
+
: {
|
|
260
|
+
ok: true,
|
|
261
|
+
value: {
|
|
262
|
+
kind: input.kind as ControlKind,
|
|
263
|
+
id: input.id as string,
|
|
264
|
+
audience: input.audience as Audience,
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
237
269
|
/** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
|
|
238
270
|
export function checkViewAs(input: unknown): Checked<PersonKind | null> {
|
|
239
271
|
if (input === undefined || input === null) return { ok: true, value: null };
|
|
@@ -48,6 +48,8 @@ const TABLE: [Action, Audience, boolean, boolean, boolean, boolean][] = [
|
|
|
48
48
|
['triage', 'internal', true, true, false, false],
|
|
49
49
|
['people', 'client', true, true, false, false],
|
|
50
50
|
['people', 'internal', true, true, false, false],
|
|
51
|
+
['audience', 'client', true, true, false, false],
|
|
52
|
+
['audience', 'internal', true, true, false, false],
|
|
51
53
|
];
|
|
52
54
|
|
|
53
55
|
const studio = person('s', 'studio');
|
|
@@ -72,12 +74,12 @@ for (const [action, audience, studioOwn, studioOther, clientOwn, clientOther] of
|
|
|
72
74
|
});
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
test('with no thread, both kinds read and start one; only Studio triages and
|
|
77
|
+
test('with no thread, both kinds read and start one; only Studio triages, manages people and sets a prototype’s audience', () => {
|
|
76
78
|
for (const action of ['read', 'comment'] as const) {
|
|
77
79
|
assert.equal(can(studio, action), true);
|
|
78
80
|
assert.equal(can(client, action), true);
|
|
79
81
|
}
|
|
80
|
-
for (const action of ['triage', 'people'] as const) {
|
|
82
|
+
for (const action of ['triage', 'people', 'audience'] as const) {
|
|
81
83
|
assert.equal(can(studio, action), true);
|
|
82
84
|
assert.equal(can(client, action), false);
|
|
83
85
|
}
|