@milesroxas/design-hub 0.12.0 → 0.14.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 +5 -5
- package/core/prototype.test.ts +12 -1
- package/core/prototype.ts +21 -2
- package/core/review/model.ts +71 -6
- package/core/review/service.test.ts +575 -39
- package/core/review/service.ts +452 -25
- package/core/review/store.ts +47 -6
- package/core/review/suite-kv.ts +17 -0
- package/dist/core/prototype.d.ts +20 -2
- package/dist/core/prototype.js +17 -2
- package/dist/core/review/model.d.ts +42 -3
- package/dist/core/review/model.js +52 -3
- package/dist/core/review/service.d.ts +81 -7
- package/dist/core/review/service.js +407 -23
- package/dist/core/review/store.d.ts +29 -2
- package/dist/core/review/store.js +6 -2
- package/dist/core/review/suite-kv.d.ts +7 -0
- package/dist/core/review/suite-kv.js +12 -0
- package/dist/scripts/client.d.ts +1 -1
- package/dist/scripts/client.js +4 -4
- package/dist/scripts/lib/cli.js +5 -0
- package/dist/scripts/lib/dns.d.ts +16 -2
- package/dist/scripts/lib/dns.js +66 -3
- package/dist/scripts/lib/fake-host.d.ts +17 -3
- package/dist/scripts/lib/fake-host.js +56 -10
- package/dist/scripts/lib/host.d.ts +15 -25
- package/dist/scripts/lib/host.js +15 -324
- package/dist/scripts/lib/resend.d.ts +36 -0
- package/dist/scripts/lib/resend.js +66 -0
- package/dist/scripts/lib/studio-deps.d.ts +23 -0
- package/dist/scripts/lib/studio-deps.js +46 -16
- package/dist/scripts/lib/studios.d.ts +16 -6
- package/dist/scripts/lib/studios.js +28 -10
- package/dist/scripts/lib/vercel-host.d.ts +17 -0
- package/dist/scripts/lib/vercel-host.js +416 -0
- package/dist/scripts/people.d.ts +16 -4
- package/dist/scripts/people.js +67 -17
- package/dist/scripts/share.js +3 -2
- package/dist/scripts/studio.d.ts +26 -4
- package/dist/scripts/studio.js +364 -19
- package/dist/server/api.d.ts +20 -7
- package/dist/server/api.js +83 -27
- package/dist/server/dev.js +21 -1
- package/dist/server/gate.d.ts +120 -0
- package/dist/server/gate.js +808 -0
- package/dist/server/mailer.d.ts +60 -0
- package/dist/server/mailer.js +104 -0
- package/dist/server/mailers/fake.d.ts +8 -0
- package/dist/server/mailers/fake.js +16 -0
- package/dist/server/mailers/resend.d.ts +21 -0
- package/dist/server/mailers/resend.js +49 -0
- package/dist/server/mark.d.ts +17 -0
- package/dist/server/mark.js +46 -1
- package/dist/server/session.d.ts +2 -1
- package/dist/server/session.js +6 -3
- package/dist/server/stores/backends.d.ts +22 -0
- package/dist/server/stores/backends.js +96 -0
- package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
- package/dist/server/stores/index.d.ts +41 -0
- package/dist/server/stores/index.js +63 -0
- package/dist/server/stores/pglite.d.ts +16 -0
- package/dist/server/stores/pglite.js +98 -0
- package/dist/server/stores/postgres.d.ts +63 -0
- package/dist/server/stores/postgres.js +153 -0
- package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
- package/dist/vercel/files.js +1 -1
- package/dist/vercel/proxy.d.ts +6 -82
- package/dist/vercel/proxy.js +24 -524
- package/dist/vite/plugin.js +4 -2
- package/kit/ui/Menu.tsx +29 -18
- package/kit/ui/layer.ts +41 -0
- package/package.json +3 -1
- package/shell/review/Bar.tsx +1 -0
- package/shell/review/Card.tsx +17 -7
- package/shell/review/People.tsx +908 -134
- package/shell/review/Review.tsx +2 -2
- package/shell/review/anchor.ts +6 -1
- package/shell/review/client.ts +41 -7
- package/shell/review/menu-place.test.ts +41 -0
- package/shell/review/place.test.ts +26 -1
- package/shell/review/place.ts +17 -0
- package/shell/review/review.css +97 -11
- package/shell/review/view.test.ts +115 -1
- package/shell/review/view.ts +92 -2
- package/shell/studio/StudioHub.tsx +1 -0
- package/shell/studio/hub.css +99 -13
- package/skills/miles-deploy/SKILL.md +26 -4
- package/templates/studio/.env.example +9 -0
- package/templates/studio/README.md +13 -0
- package/templates/studio/package.json +1 -1
- /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
- /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
package/README.md
CHANGED
|
@@ -72,9 +72,9 @@ A speculation rule in `shell/index.html` prefetches a prototype's page when its
|
|
|
72
72
|
|
|
73
73
|
## The studio site, and signing in
|
|
74
74
|
|
|
75
|
-
`miles-deploy` owns the steps; this is what they rest on. A project has no deployment of its own. A **studio** is one site on one hosting account, with one database (Upstash for Redis), one private file store (Vercel Blob), one `GATE_SECRET` and one apex domain, that serves every client of the studio; two studios share none of these. Its folder, made by `design-hub studio create` from `templates/studio/`, is thin like a project: one-line `proxy.ts`, `api/review.ts` and `api/files.ts` from `./vercel`, a `vercel.json`, a `studio.json` (id, slug, apex, team, DNS; no secret) and a gitignored `.env`. A machine finds its studios by slug in `~/.design-hub/studios.json`. `design-hub signin` starts `vercel login`'s device sign-in in the background and returns its link, so the user clicks instead of typing.
|
|
75
|
+
`miles-deploy` owns the steps; this is what they rest on. A project has no deployment of its own. A **studio** is one site on one hosting account, with one database (Upstash for Redis, or Postgres: Neon, Supabase or any other, chosen at `studio create --kv`), one private file store (Vercel Blob), one `GATE_SECRET` and one apex domain, that serves every client of the studio; two studios share none of these. Its folder, made by `design-hub studio create` from `templates/studio/`, is thin like a project: one-line `proxy.ts`, `api/review.ts` and `api/files.ts` from `./vercel`, a `vercel.json`, a `studio.json` (id, slug, apex, team, DNS, the database's kind and the host; no secret) and a gitignored `.env`. A machine finds its studios by slug in `~/.design-hub/studios.json`. `design-hub signin` starts `vercel login`'s device sign-in in the background and returns its link, so the user clicks instead of typing.
|
|
76
76
|
|
|
77
|
-
A client's address is `<client>.<apex>` (or a codename under the apex, `client add --host`), and a project is `/<project>/` below it. `design-hub project link` writes `.design-hub/studio.json` in the project (studio, its id, client, project; committed). `design-hub share` builds the project with base `/<project>/`, uploads `dist/` under `c/<client>/<project>/<build>/` in the file store with a new random build id, checks every file is there, and only then points the project's record at that build, so a half-finished upload is never served; the live build and the two before it are kept. Every path on every client's host sits behind the gate, enforced on the server by Vercel Routing Middleware (`createStudioProxy` in `vercel/proxy.ts`, which the studio's `vercel.json` names as `proxy.entrypoint`). The host names the client, from one store lookup kept a minute per instance; a host no client has is the plain 404 before anything else is read. The files function and the review function run the gate's checks again themselves, in the order host, session, project, so neither trusts anything the gate set. A client-side gate was rejected because the bundle and the state behind it would still download.
|
|
77
|
+
A client's address is `<client>.<apex>` (or a codename under the apex, `client add --host`), and a project is `/<project>/` below it. `design-hub project link` writes `.design-hub/studio.json` in the project (studio, its id, client, project; committed). `design-hub share` builds the project with base `/<project>/`, uploads `dist/` under `c/<client>/<project>/<build>/` in the file store with a new random build id, checks every file is there, and only then points the project's record at that build, so a half-finished upload is never served; the live build and the two before it are kept. Every path on every client's host sits behind the gate, enforced on the server by the gate (`server/gate.ts`, free of any host: it answers each request with a decision), run as Vercel Routing Middleware by its adapter (`createStudioProxy` in `vercel/proxy.ts`, which the studio's `vercel.json` names as `proxy.entrypoint`). The host names the client, from one store lookup kept a minute per instance; a host no client has is the plain 404 before anything else is read. The files function and the review function run the gate's checks again themselves, in the order host, session, project, so neither trusts anything the gate set. A client-side gate was rejected because the bundle and the state behind it would still download.
|
|
78
78
|
|
|
79
79
|
Studio people have a host of their own, `studio.<apex>`, which `studio create` (or `studio deploy`, for an older studio) adds and proves as it proves a client's. Only Studio people sign in there; a Client, a cookie of a client's host, or none gets that host's sign-in page, word for word a client host's. Its page is the studio hub (`shell/studio/StudioHub.tsx`, built apart from the client hub under `_studio/`, so no client's host serves it): every client with its projects, an **Open** that hands the person off to the client's host (a one-time record in the client's keys, claimed once there within 60 seconds, which sets that host's own Studio cookie), and a **People** panel per client and one for the studio's own people, the review panel's People controls. Its review API answers `me`, `clients` and the people ops, each naming the client it is for with `client`, which the service checks as the handoff does (known, added, not archived) and answers as an unknown client otherwise; without `client` an op is the studio's own people. No client's host reads a `client` parameter.
|
|
80
80
|
|
|
@@ -87,7 +87,7 @@ Every person is named, and signs in with a one-time link that Studio makes in th
|
|
|
87
87
|
- **The session** is one cookie, `__Host-hub_person`: `HttpOnly`, `Secure`, `SameSite=Lax`, host-only. Its token is `p5.<id>.<epoch>.<kind>.<client>.<expires>.<checkedAt>.<sig>`, signed with HMAC-SHA256 over all of those and over two things it never carries: the review store's URL, so two studios that share a `GATE_SECRET` never accept each other's cookies, and when the client was added, so a slug archived and added again accepts no token from before. A token verifies only on its own client's host, a Studio person's too. Any other version never verifies. 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. 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. The hub reads any 401, the gate's plain text or the API's JSON, as signed out.
|
|
88
88
|
- **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`.
|
|
89
89
|
- **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 (`/<project>/<slug>/`) or anything under `/<project>/_p/<slug>/` gets 404 unless that prototype is client-facing, asked of the store on every such request; `/<project>/_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. Below a project, any first segment that is not the hub's own (`assets`, `api`, `guide`, `_gate`, `index.html`, `favicon.svg`), `_p` or a prototype slug gets the same 404. A path that could mean more than one file (a `.` or `..` segment, a backslash, a control character, an empty segment, a `%` left after decoding, or one that does not decode) goes nowhere for anyone, Studio included. Another client's project, and a project with no build, are the same 404, its review API too. 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 audience read. The client's hub at `/` and a project's `/assets/*`, `/guide/*` and review API pass on the session alone. `invite`, `api`, `_gate`, `_p`, `assets`, `guide`, `index.html` and `favicon.svg` are never a prototype's slug (`RESERVED` in `core/prototype.ts`).
|
|
90
|
-
- **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
|
|
90
|
+
- **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 works once, on one device, and to ask the studio for a new one. Anything else gets a plain-text 401, an anonymous `POST` included.
|
|
91
91
|
|
|
92
92
|
The studio site reads its env from the Vercel project, never committed: `GATE_SECRET`, at least 32 random characters that sign the session cookie, and the database's and file store's credentials, which `studio create` sets and copies into the studio folder's `.env` (`templates/studio/.env.example` names them). With the secret or the database missing, every host answers 503 "This site is not set up yet.", which names no command, studio or client, so a misconfigured site never serves anything open. 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 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 signed-out page names no client. Under `design-hub dev` there is no gate; `/_gate/` previews the sign-in page, `?page=link` a link's page, `?page=used` a used one and `?page=stopped` the studio host's page for an Open that cannot start. On the studio host the used page's way in says the clients, not the prototypes.
|
|
93
93
|
|
|
@@ -95,6 +95,6 @@ The studio site reads its env from the Vercel project, never committed: `GATE_SE
|
|
|
95
95
|
|
|
96
96
|
Every signed-in person can comment as themselves. The People tab lists each person with their team, kind and when their access ends, and says how links behave where Studio shares them.
|
|
97
97
|
|
|
98
|
-
The panel (`shell/review/`) mounts on every prototype page in its own React root, so a prototype that throws never takes it down. A comment carries the prototype, the URL, every dial panel's values (`snapshotDials`), and the element it was pinned to as a selector path with an offset; Show state reopens the URL and sets the dials back. Pins draw over their elements and below the Controls panel. The API is one endpoint, `<project>/api/review` (`server/api.ts`, reached on a studio site through its one-line `api/review.ts`, scoped by the host's client and the project the path names): queries by GET, commands by POST with a JSON body, same-origin only. The rules (who may delete, what a valid comment is, which client a person may act in) live once, in `core/review/`, behind a small storage port: the studio's Upstash Redis over REST, or a file in `.design-hub/review.json` under `design-hub dev`, where whoever runs the server is a local Studio person in one fixed scope. Every key is built by one function from a scope, under the prefix `hub4:`: the studio's (`studio:people`, `clients`, `hosts`, …), a client's (`c:<client>:people`, `:links`, `:projects`, …) and a project's (`c:<client>:p:<project>:comments:<prototype>`, …), so no op reaches another client's records.
|
|
98
|
+
The panel (`shell/review/`) mounts on every prototype page in its own React root, so a prototype that throws never takes it down. A comment carries the prototype, the URL, every dial panel's values (`snapshotDials`), and the element it was pinned to as a selector path with an offset; Show state reopens the URL and sets the dials back. Pins draw over their elements and below the Controls panel. The API is one endpoint, `<project>/api/review` (`server/api.ts`, reached on a studio site through its one-line `api/review.ts`, scoped by the host's client and the project the path names): queries by GET, commands by POST with a JSON body, same-origin only. The rules (who may delete, what a valid comment is, which client a person may act in) live once, in `core/review/`, behind a small storage port: the studio's database (`server/stores/`: Upstash Redis over REST, or Postgres in one table of key, field and value), or a file in `.design-hub/review.json` under `design-hub dev`, where whoever runs the server is a local Studio person in one fixed scope. Every key is built by one function from a scope, under the prefix `hub4:`: the studio's (`studio:people`, `clients`, `hosts`, …), a client's (`c:<client>:people`, `:links`, `:projects`, …) and a project's (`c:<client>:p:<project>:comments:<prototype>`, …), so no op reaches another client's records.
|
|
99
99
|
|
|
100
|
-
`design-hub dev` serves the prototypes open, with the review panel on the local store. The gate and the studio's functions are covered by `pnpm test` (`vercel/
|
|
100
|
+
`design-hub dev` serves the prototypes open, with the review panel on the local store. The gate and the studio's functions are covered by `pnpm test` (`vercel/gate.test.ts`, against an in-memory Upstash and file store, and again in `vercel/gate.postgres.test.ts` on an embedded Postgres).
|
package/core/prototype.test.ts
CHANGED
|
@@ -33,7 +33,8 @@ test('a prototype or project slug is kebab-case and never a first segment the ga
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
test('a client slug is kebab-case and never the studio’s own host names', () => {
|
|
36
|
-
|
|
36
|
+
// `mail` too: the default sender's subdomain (email.md, E11).
|
|
37
|
+
assert.deepEqual([...RESERVED_CLIENTS], ['studio', 'www', 'api', 'mail']);
|
|
37
38
|
for (const reserved of RESERVED_CLIENTS) assert.equal(isClientSlug(reserved), false, reserved);
|
|
38
39
|
assert.equal(isClientSlug('acme'), true);
|
|
39
40
|
// A prototype's reserved names are no concern of a host's.
|
|
@@ -143,6 +144,11 @@ test('a studio path goes to the link, the gate’s assets, the client’s hub, a
|
|
|
143
144
|
for (const path of ['/api/review', '/api/files'])
|
|
144
145
|
assert.deepEqual(route(path), { kind: 'api', project: null }, path);
|
|
145
146
|
assert.deepEqual(route('/rebrand/api/review'), { kind: 'api', project: 'rebrand' });
|
|
147
|
+
// Email sign-in's two paths, exactly; anything near them is the hub's, as before.
|
|
148
|
+
assert.deepEqual(route('/_gate/code'), { kind: 'code', step: 'ask' });
|
|
149
|
+
assert.deepEqual(route('/_gate/code/use'), { kind: 'code', step: 'use' });
|
|
150
|
+
for (const path of ['/_gate/code/', '/_gate/code/use/', '/_gate/code/other'])
|
|
151
|
+
assert.deepEqual(route(path), { kind: 'hub' }, path);
|
|
146
152
|
assert.deepEqual(route('/rebrand/'), { kind: 'file', project: 'rebrand', file: [], prototype: null });
|
|
147
153
|
assert.deepEqual(route('/rebrand'), { kind: 'file', project: 'rebrand', file: [], prototype: null });
|
|
148
154
|
assert.deepEqual(route('/rebrand/assets/index-x.js'), {
|
|
@@ -229,8 +235,11 @@ test('on the studio’s own host only its hub, its bundle, its API, a link, the
|
|
|
229
235
|
['/api/review', 'api'],
|
|
230
236
|
['/_gate/handoff', 'handoff'],
|
|
231
237
|
[`/_gate/proof/${token}`, 'proof'],
|
|
238
|
+
['/_gate/code', 'code'],
|
|
239
|
+
['/_gate/code/use', 'code'],
|
|
232
240
|
];
|
|
233
241
|
for (const [path, kind] of cases) assert.equal(studioRouteOf(path).kind, kind, path);
|
|
242
|
+
assert.deepEqual(studioRouteOf('/_gate/code/use'), { kind: 'code', step: 'use' });
|
|
234
243
|
for (const path of [
|
|
235
244
|
STUDIO_PAGE,
|
|
236
245
|
'/index.html',
|
|
@@ -244,6 +253,8 @@ test('on the studio’s own host only its hub, its bundle, its API, a link, the
|
|
|
244
253
|
'/_studio/%2e%2e/x',
|
|
245
254
|
'/_gate/proof/x',
|
|
246
255
|
`/_gate/handoff/${token}`,
|
|
256
|
+
'/_gate/code/',
|
|
257
|
+
'/_gate/code/other',
|
|
247
258
|
])
|
|
248
259
|
assert.equal(studioRouteOf(path).kind, 'none', path);
|
|
249
260
|
// A client host never reaches the studio hub's files.
|
package/core/prototype.ts
CHANGED
|
@@ -19,8 +19,8 @@ export const RESERVED = [
|
|
|
19
19
|
'favicon.svg',
|
|
20
20
|
] as const;
|
|
21
21
|
|
|
22
|
-
/** Never a client's slug: the studio's own host,
|
|
23
|
-
export const RESERVED_CLIENTS = ['studio', 'www', 'api'] as const;
|
|
22
|
+
/** Never a client's slug: the studio's own host, the names a host is often given, and `mail`, the default sender's subdomain (email.md, E11). */
|
|
23
|
+
export const RESERVED_CLIENTS = ['studio', 'www', 'api', 'mail'] as const;
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
|
|
@@ -117,6 +117,17 @@ const HANDOFF_PATH = /^\/_gate\/handoff\/([\w-]{20,100})$/;
|
|
|
117
117
|
export const handoffPath = (token: string) => `/_gate/handoff/${token}`;
|
|
118
118
|
/** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
|
|
119
119
|
export const HANDOFF_START = '/_gate/handoff';
|
|
120
|
+
/**
|
|
121
|
+
* Email sign-in, on a client's host and on the studio's (email.md): a POST of
|
|
122
|
+
* an address asks for a code, a POST of the code uses it. The gate answers
|
|
123
|
+
* them only when the studio has a mailer; otherwise each is what any other
|
|
124
|
+
* path there is.
|
|
125
|
+
*/
|
|
126
|
+
export const CODE_ASK = '/_gate/code';
|
|
127
|
+
export const CODE_USE = '/_gate/code/use';
|
|
128
|
+
type CodeStep = 'ask' | 'use';
|
|
129
|
+
const codeStep = (pathname: string): CodeStep | null =>
|
|
130
|
+
pathname === CODE_ASK ? 'ask' : pathname === CODE_USE ? 'use' : null;
|
|
120
131
|
/**
|
|
121
132
|
* The studio site's functions, at a client host's root: the review API, and
|
|
122
133
|
* the files function the gate rewrites a project's files to. The gate passes
|
|
@@ -131,6 +142,7 @@ const FUNCTIONS: readonly string[] = ['review', 'files'];
|
|
|
131
142
|
* - `link`: `/invite/<token>`, at the host root only.
|
|
132
143
|
* - `handoff`: `/_gate/handoff/<token>`, a handoff's claim, at the host root only.
|
|
133
144
|
* - `asset`: the gate's font and the favicon, served before sign-in.
|
|
145
|
+
* - `code`: `/_gate/code` and `/_gate/code/use`, email sign-in, when the studio has a mailer.
|
|
134
146
|
* - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
|
|
135
147
|
* - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
|
|
136
148
|
* - `file`: a file of a project's build, as its segments ([] for the project's own page), and
|
|
@@ -141,6 +153,7 @@ export type Where =
|
|
|
141
153
|
| { kind: 'link'; token: string }
|
|
142
154
|
| { kind: 'handoff'; token: string }
|
|
143
155
|
| { kind: 'asset' }
|
|
156
|
+
| { kind: 'code'; step: CodeStep }
|
|
144
157
|
| { kind: 'hub' }
|
|
145
158
|
| { kind: 'api'; project: string | null }
|
|
146
159
|
| { kind: 'file'; project: string; file: string[]; prototype: string | null };
|
|
@@ -168,6 +181,8 @@ function where(pathname: string): Where {
|
|
|
168
181
|
const handoff = pathname.match(HANDOFF_PATH);
|
|
169
182
|
if (handoff) return { kind: 'handoff', token: handoff[1] };
|
|
170
183
|
if (GATE_ASSETS.includes(pathname)) return { kind: 'asset' };
|
|
184
|
+
const step = codeStep(pathname);
|
|
185
|
+
if (step) return { kind: 'code', step };
|
|
171
186
|
const parts = segments(pathname);
|
|
172
187
|
if (!parts) return { kind: 'none' };
|
|
173
188
|
const [first, ...rest] = parts;
|
|
@@ -199,6 +214,7 @@ export const STUDIO_PAGE = '/studio.html';
|
|
|
199
214
|
* - `file`: the studio hub's bundle, below `/_studio/`.
|
|
200
215
|
* - `api`: `/api/review`, where the studio hub reads every client.
|
|
201
216
|
* - `handoff`: `/_gate/handoff`, where the studio hub asks to open a client.
|
|
217
|
+
* - `code`: `/_gate/code` and `/_gate/code/use`, a Studio person's email sign-in.
|
|
202
218
|
* - `proof`: `/_gate/proof/<ask>`, the host proof `studio create` and
|
|
203
219
|
* `studio deploy` ask the studio's own host for, as `client add` asks a client's.
|
|
204
220
|
*/
|
|
@@ -210,6 +226,7 @@ export type StudioWhere =
|
|
|
210
226
|
| { kind: 'file' }
|
|
211
227
|
| { kind: 'api' }
|
|
212
228
|
| { kind: 'handoff' }
|
|
229
|
+
| { kind: 'code'; step: CodeStep }
|
|
213
230
|
| { kind: 'proof'; ask: string };
|
|
214
231
|
|
|
215
232
|
/** The one place a request's path on the studio's own host is read. It fails closed, as routeOf does. */
|
|
@@ -220,6 +237,8 @@ export function studioRouteOf(pathname: string): StudioWhere {
|
|
|
220
237
|
if (pathname === '/') return { kind: 'page' };
|
|
221
238
|
if (pathname === '/api/review') return { kind: 'api' };
|
|
222
239
|
if (pathname === HANDOFF_START) return { kind: 'handoff' };
|
|
240
|
+
const step = codeStep(pathname);
|
|
241
|
+
if (step) return { kind: 'code', step };
|
|
223
242
|
const proof = pathname.match(PROOF_PATH);
|
|
224
243
|
if (proof) return { kind: 'proof', ask: proof[1] };
|
|
225
244
|
const parts = segments(pathname);
|
package/core/review/model.ts
CHANGED
|
@@ -89,7 +89,12 @@ export type Person = {
|
|
|
89
89
|
* client. Never stored in the profile: it is where the person's records live.
|
|
90
90
|
*/
|
|
91
91
|
client: string | null;
|
|
92
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* The address they sign in with by code (lower case, trimmed), or null for
|
|
94
|
+
* links only. Within one client's people, or the studio's own, one live
|
|
95
|
+
* person holds an address (`emails` in store.ts). A profile stored before
|
|
96
|
+
* email existed reads as null.
|
|
97
|
+
*/
|
|
93
98
|
email: string | null;
|
|
94
99
|
/** When their access ends (ISO time), or null for open-ended. Their sessions end with it. */
|
|
95
100
|
accessEndsAt: string | null;
|
|
@@ -151,6 +156,17 @@ export type Project = { slug: string; client: string; name: string; build: strin
|
|
|
151
156
|
/** A one-time sign-in link, stored under the hash of its token and deleted when used. */
|
|
152
157
|
export type Link = { person: string; expiresAt: string };
|
|
153
158
|
|
|
159
|
+
/**
|
|
160
|
+
* A person's one live sign-in code (`codes` in store.ts): an HMAC of the code
|
|
161
|
+
* under a key the service is given, never the code or a bare digest, since
|
|
162
|
+
* 8 digits can be tried in full. A new code replaces it; a right answer
|
|
163
|
+
* removes it.
|
|
164
|
+
*/
|
|
165
|
+
export type Code = { hash: string; expiresAt: string; sentAt: string };
|
|
166
|
+
|
|
167
|
+
/** The code rules (E6): 8 digits, 10 minutes, 5 wrong tries, one a minute and 5 an hour per person. */
|
|
168
|
+
export const CODE = { digits: 8, minutes: 10, tries: 5, gapSeconds: 60, perHour: 5 } as const;
|
|
169
|
+
|
|
154
170
|
/**
|
|
155
171
|
* A handoff: a Studio person, signed in on the studio's host, opening one
|
|
156
172
|
* client's hub (or one of its projects) on that client's host. Stored with
|
|
@@ -239,7 +255,8 @@ const PROFILE_RULES: [string, (v: unknown) => boolean][] = [
|
|
|
239
255
|
['name', isText],
|
|
240
256
|
['team', isText],
|
|
241
257
|
['kind', (v) => PERSON_KINDS.includes(v as PersonKind)],
|
|
242
|
-
|
|
258
|
+
// Absent on a profile stored before email existed: it reads as null (no migration).
|
|
259
|
+
['email', (v) => v === undefined || isTextOrNull(v)],
|
|
243
260
|
['accessEndsAt', isTextOrNull],
|
|
244
261
|
['createdAt', isText],
|
|
245
262
|
];
|
|
@@ -273,6 +290,12 @@ export const isRoot = (c: Comment) => c.thread === c.id;
|
|
|
273
290
|
|
|
274
291
|
export type Checked<T> = { ok: true; value: T } | { ok: false; problems: string[] };
|
|
275
292
|
|
|
293
|
+
/** What a refusal says when it names fields only: the panel then shows the fields' problems. */
|
|
294
|
+
export const NOT_VALID = 'Some fields are not valid.';
|
|
295
|
+
/** What a refusal says when an address is the one thing wrong, so a person reads a sentence, not a field. */
|
|
296
|
+
export const NOT_AN_EMAIL =
|
|
297
|
+
'That is not an email address. Use one such as name@example.com, or leave it empty.';
|
|
298
|
+
|
|
276
299
|
const text = (v: unknown, path: string, max: number, problems: string[]): string => {
|
|
277
300
|
if (typeof v !== 'string' || !v.trim()) {
|
|
278
301
|
problems.push(`${path}: required`);
|
|
@@ -293,13 +316,39 @@ export function checkAccess(input: unknown): Checked<Access> {
|
|
|
293
316
|
return isAccess(input) ? { ok: true, value: input } : { ok: false, problems: [ACCESS_PROBLEM] };
|
|
294
317
|
}
|
|
295
318
|
|
|
296
|
-
/**
|
|
297
|
-
|
|
319
|
+
/**
|
|
320
|
+
* What an address may be: the form a browser's email field takes (WHATWG),
|
|
321
|
+
* lower case, 254 characters at most. Nothing in it can split a mail header
|
|
322
|
+
* or name a second recipient: no space, comma, semicolon, angle bracket or quote.
|
|
323
|
+
*/
|
|
324
|
+
const EMAIL =
|
|
325
|
+
/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
326
|
+
const EMAIL_MAX = 254;
|
|
327
|
+
export const EMAIL_PROBLEM = 'email: an address such as name@example.com, or empty for links only';
|
|
328
|
+
|
|
329
|
+
/** An address as stored: trimmed and lower case, or null when it is not one. */
|
|
330
|
+
export function emailOf(input: unknown): string | null {
|
|
331
|
+
if (typeof input !== 'string') return null;
|
|
332
|
+
const address = input.trim().toLowerCase();
|
|
333
|
+
return address.length <= EMAIL_MAX && EMAIL.test(address) ? address : null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** An address as Studio sets it: null, or empty, is links only; anything else must be an address. */
|
|
337
|
+
export function checkEmail(input: unknown): Checked<string | null> {
|
|
338
|
+
if (input === null || (typeof input === 'string' && input.trim() === '')) return { ok: true, value: null };
|
|
339
|
+
const address = emailOf(input);
|
|
340
|
+
return address ? { ok: true, value: address } : { ok: false, problems: [EMAIL_PROBLEM] };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** `team` absent: the service fills it, the studio's name for Studio and the client's for a Client. `email` absent is links only. */
|
|
344
|
+
export type PersonInput = Pick<Person, 'name' | 'kind' | 'email'> & { team?: string; access: Access | null };
|
|
298
345
|
|
|
299
346
|
export function checkPersonInput(input: unknown): Checked<PersonInput> {
|
|
300
347
|
const i = (input ?? {}) as Record<string, unknown>;
|
|
301
348
|
const problems: string[] = [];
|
|
302
349
|
const name = text(i.name, 'name', LIMITS.name, problems);
|
|
350
|
+
const email = checkEmail(i.email ?? null);
|
|
351
|
+
if (!email.ok) problems.push(...email.problems);
|
|
303
352
|
const team = i.team === undefined ? undefined : text(i.team, 'team', LIMITS.team, problems);
|
|
304
353
|
// Most people added are on the client's side; Studio is the deliberate choice.
|
|
305
354
|
const kind = i.kind ?? 'client';
|
|
@@ -315,6 +364,7 @@ export function checkPersonInput(input: unknown): Checked<PersonInput> {
|
|
|
315
364
|
name,
|
|
316
365
|
...(team === undefined ? {} : { team }),
|
|
317
366
|
kind: kind as PersonKind,
|
|
367
|
+
email: email.ok ? email.value : null,
|
|
318
368
|
access: access as Access | null,
|
|
319
369
|
},
|
|
320
370
|
};
|
|
@@ -430,14 +480,29 @@ const isSlug = (v: unknown) => typeof v === 'string' && SLUG.test(v);
|
|
|
430
480
|
const isObject = (v: unknown): v is Record<string, unknown> =>
|
|
431
481
|
!!v && typeof v === 'object' && !Array.isArray(v);
|
|
432
482
|
|
|
483
|
+
const PATH_BASE = 'https://path.invalid';
|
|
484
|
+
/**
|
|
485
|
+
* A path on the page's own site. Starting with `/` is not enough: a browser
|
|
486
|
+
* reads `//host`, `/\host` and `/<tab>/host` as another site, so the path is
|
|
487
|
+
* resolved the way a browser would and must stay on the same origin.
|
|
488
|
+
*/
|
|
489
|
+
export function isSitePath(v: unknown): v is string {
|
|
490
|
+
if (typeof v !== 'string' || !v.startsWith('/')) return false;
|
|
491
|
+
try {
|
|
492
|
+
return new URL(v, PATH_BASE).origin === PATH_BASE;
|
|
493
|
+
} catch {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
433
498
|
/** One rule per field: the field, whether a value passes, and what the field must be. */
|
|
434
499
|
const ANCHOR_RULES: [keyof Anchor, (v: unknown) => boolean, string][] = [
|
|
435
500
|
['prototype', isSlug, 'a prototype slug'],
|
|
436
501
|
['feature', (v) => v === '' || isSlug(v), 'a feature slug or empty'],
|
|
437
502
|
[
|
|
438
503
|
'url',
|
|
439
|
-
(v) =>
|
|
440
|
-
`a path starting with /, ${LIMITS.url} characters at most`,
|
|
504
|
+
(v) => isSitePath(v) && v.length <= LIMITS.url,
|
|
505
|
+
`a path on this site starting with /, ${LIMITS.url} characters at most`,
|
|
441
506
|
],
|
|
442
507
|
[
|
|
443
508
|
'dials',
|