@milesroxas/design-hub 0.6.2 → 0.7.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 +16 -6
- package/core/review/model.ts +82 -6
- package/core/review/policy.test.ts +2 -0
- package/core/review/policy.ts +2 -2
- package/core/review/service.test.ts +397 -27
- package/core/review/service.ts +168 -36
- package/core/review/store.ts +22 -4
- package/dist/core/review/model.d.ts +35 -4
- package/dist/core/review/model.js +66 -4
- package/dist/core/review/service.d.ts +24 -6
- package/dist/core/review/service.js +155 -36
- package/dist/core/review/store.d.ts +15 -4
- package/dist/core/review/store.js +19 -3
- package/dist/scripts/bootstrap.d.ts +16 -14
- package/dist/scripts/bootstrap.js +56 -39
- package/dist/scripts/lib/deploy.d.ts +12 -4
- package/dist/scripts/lib/deploy.js +47 -13
- package/dist/scripts/lib/vercel.d.ts +8 -2
- package/dist/scripts/lib/vercel.js +23 -9
- package/dist/scripts/people.d.ts +13 -26
- package/dist/scripts/people.js +68 -35
- package/dist/scripts/preflight.js +2 -2
- package/dist/scripts/share.d.ts +2 -3
- package/dist/scripts/share.js +18 -10
- package/dist/server/api.d.ts +6 -2
- package/dist/server/api.js +53 -22
- package/dist/server/fake-upstash.js +3 -2
- package/dist/server/local.js +2 -0
- package/dist/server/session.d.ts +25 -10
- package/dist/server/session.js +69 -52
- package/dist/server/upstash.js +4 -3
- package/dist/vercel/proxy.d.ts +5 -3
- package/dist/vercel/proxy.js +183 -157
- package/dist/vite/plugin.js +5 -5
- package/package.json +2 -2
- package/shell/review/People.tsx +178 -45
- package/shell/review/Review.tsx +7 -7
- package/shell/review/client.ts +36 -17
- package/shell/review/review.css +2 -0
- package/skills/miles-deploy/SKILL.md +44 -16
- package/skills/miles-design-hub/SKILL.md +5 -5
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-review/SKILL.md +7 -5
- package/skills/miles-worktrees/SKILL.md +1 -1
- package/templates/conductor/settings.toml +1 -1
- package/templates/guide.md +1 -1
package/dist/vercel/proxy.js
CHANGED
|
@@ -1,84 +1,59 @@
|
|
|
1
1
|
// Vercel Routing Middleware (the project's proxy.ts, named in vercel.json as
|
|
2
2
|
// proxy.entrypoint): the gate in front of every path of the deployed
|
|
3
|
-
// prototypes, the review API included.
|
|
4
|
-
//
|
|
3
|
+
// prototypes, the review API included. There is one way in, a one-time link
|
|
4
|
+
// that Studio makes for one named person:
|
|
5
5
|
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
// next page load
|
|
6
|
+
// GET /invite/<token> shows who the link is for, and uses nothing up, so
|
|
7
|
+
// a mail or chat preview that opens it costs nothing
|
|
8
|
+
// POST /invite/<token> uses the link up, sets the person's signed cookie
|
|
9
|
+
// (server/session.ts) and goes to the hub
|
|
11
10
|
//
|
|
12
|
-
//
|
|
11
|
+
// A used, expired or unknown link is 404. Every other path needs the cookie.
|
|
12
|
+
// It carries when the store last said yes; after CHECK_SECONDS (5 minutes)
|
|
13
|
+
// the gate asks the store again on the next request of any kind, so a revoke,
|
|
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. Without a session a
|
|
16
|
+
// page gets the sign-in page (401), which says what to do and has no form,
|
|
17
|
+
// and anything else a plain-text 401. Without GATE_SECRET the gate fails
|
|
18
|
+
// closed (503).
|
|
13
19
|
import { next } from '@vercel/functions';
|
|
14
20
|
import { base64, sha256 } from '../core/bytes.js';
|
|
15
21
|
import { escapeHtml } from '../core/html.js';
|
|
16
22
|
import { deployedReview } from '../server/api.js';
|
|
17
|
-
import { clearedCookie,
|
|
23
|
+
import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, } from '../server/session.js';
|
|
18
24
|
const DEFAULT_SITE_NAME = 'Prototypes';
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// What the gate page itself loads, served before sign-in: the hub's face
|
|
23
|
-
// (the Vite plugin emits it at this fixed path) and the favicon. Nothing else.
|
|
25
|
+
const LINK_PATH = /^\/invite\/([\w-]{20,100})\/?$/;
|
|
26
|
+
// What the gate pages load before sign-in: the hub's face (the Vite plugin
|
|
27
|
+
// emits it at this fixed path) and the favicon. Nothing else.
|
|
24
28
|
export const GATE_FONT = '/_gate/geist.woff2';
|
|
25
29
|
const GATE_ASSETS = new Set([GATE_FONT, '/favicon.svg']);
|
|
26
|
-
// The gate
|
|
27
|
-
//
|
|
28
|
-
// wears the hub's neutral system look (shell/system.css)
|
|
29
|
-
// project's brand. Geist (latin, variable) is the one
|
|
30
|
-
// first paint is already in it.
|
|
30
|
+
// The gate pages. The CSS is a constant so its CSP hash is computed once and
|
|
31
|
+
// the pages carry no 'unsafe-inline', and they run no script at all. The gate
|
|
32
|
+
// is hub chrome, so it wears the hub's neutral system look (shell/system.css)
|
|
33
|
+
// inlined, never the project's brand. Geist (latin, variable) is the one
|
|
34
|
+
// font, preloaded so the first paint is already in it.
|
|
31
35
|
const CSS = `
|
|
32
36
|
@font-face{font-family:Geist;src:url(${GATE_FONT}) format("woff2");font-weight:100 900;font-style:normal;font-display:swap}
|
|
33
|
-
:root{color-scheme:light;--bg:oklch(.985 0 0);--surface:oklch(1 0 0);--ink:oklch(.205 0 0);--ink-
|
|
34
|
-
@media(prefers-color-scheme:dark){:root{color-scheme:dark;--bg:oklch(.145 0 0);--surface:oklch(.175 0 0);--ink:oklch(.985 0 0);--ink-
|
|
37
|
+
:root{color-scheme:light;--bg:oklch(.985 0 0);--surface:oklch(1 0 0);--ink:oklch(.205 0 0);--ink-3:oklch(.46 0 0);--brand:oklch(.205 0 0);--brand-hover:oklch(.3 0 0);--on-brand:oklch(.985 0 0);--ring:0 0 0 2px oklch(1 0 0),0 0 0 4px oklch(.556 0 0);--shadow:0 1px 2px 0 oklch(0 0 0/.05);--raised:0 0 0 1px oklch(0 0 0/.06),0 1px 2px oklch(0 0 0/.05),0 12px 24px -8px oklch(0 0 0/.1);--ease-out:cubic-bezier(.23,1,.32,1);--ease-press:cubic-bezier(.33,1,.68,1);--ease-hover:cubic-bezier(.25,.46,.45,.94)}
|
|
38
|
+
@media(prefers-color-scheme:dark){:root{color-scheme:dark;--bg:oklch(.145 0 0);--surface:oklch(.175 0 0);--ink:oklch(.985 0 0);--ink-3:oklch(.72 0 0);--brand:oklch(.985 0 0);--brand-hover:oklch(.87 0 0);--on-brand:oklch(.205 0 0);--ring:0 0 0 2px oklch(.175 0 0),0 0 0 4px oklch(.708 0 0);--shadow:0 0 #0000;--raised:0 0 0 1px oklch(1 0 0/.09)}}
|
|
35
39
|
*{box-sizing:border-box}
|
|
36
40
|
html{height:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}
|
|
37
41
|
body{margin:0;min-height:100%;display:grid;place-items:center;padding:max(24px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));background:var(--bg);color:var(--ink);font:14px/20px Geist,ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;font-optical-sizing:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}
|
|
38
|
-
[hidden]{display:none!important}
|
|
39
42
|
.gate{width:100%;max-width:360px;padding:32px;background:var(--surface);border-radius:12px;box-shadow:var(--raised);transition:opacity 240ms var(--ease-out),transform 240ms var(--ease-out)}
|
|
40
43
|
@starting-style{.gate{opacity:0;transform:translateY(6px)}}
|
|
41
44
|
h1{margin:0 0 4px;font-size:20px;line-height:28px;font-weight:600;letter-spacing:-.02em;text-wrap:balance}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
input:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring),var(--shadow)}
|
|
48
|
-
input[aria-invalid=true]{border-color:var(--error-line)}
|
|
49
|
-
input[aria-invalid=true]:focus-visible{box-shadow:var(--ring-error),var(--shadow)}
|
|
50
|
-
.toggle{position:absolute;top:4px;right:4px;width:32px;height:32px;display:grid;place-items:center;margin:0;padding:0;border:0;border-radius:2px;background:transparent;color:var(--ink-4);cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;transition:color 120ms var(--ease-hover),background-color 120ms var(--ease-hover)}
|
|
51
|
-
.toggle::before{content:"";position:absolute;inset:-4px}
|
|
52
|
-
.toggle svg{display:block}
|
|
53
|
-
.toggle:focus-visible{outline:2px solid transparent;box-shadow:var(--ring)}
|
|
54
|
-
.toggle:active{color:var(--ink-2)}
|
|
55
|
-
.toggle[aria-pressed=true] .show,.toggle[aria-pressed=false] .hide{display:none}
|
|
56
|
-
@media(hover:hover) and (pointer:fine){.toggle:hover{color:var(--ink-2);background:var(--fill-hover)}}
|
|
57
|
-
.note{margin:8px 0 0;color:var(--ink-3)}
|
|
58
|
-
.error{color:var(--error)}
|
|
59
|
-
.go{display:block;width:100%;height:44px;margin-top:20px;border:0;border-radius:6px;background:var(--brand);color:var(--on-brand);font:inherit;font-weight:500;cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;box-shadow:var(--shadow);transition:background-color 120ms var(--ease-hover),transform 160ms var(--ease-press),opacity 120ms var(--ease-hover)}
|
|
45
|
+
p{margin:0;color:var(--ink-3);text-wrap:pretty}
|
|
46
|
+
p+p{margin-top:8px}
|
|
47
|
+
strong{font-weight:500;color:var(--ink)}
|
|
48
|
+
form{margin:0}
|
|
49
|
+
.go{display:block;width:100%;height:44px;margin-top:24px;padding:0;border:0;border-radius:6px;background:var(--brand);color:var(--on-brand);font:inherit;font-weight:500;line-height:44px;text-align:center;text-decoration:none;cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;box-shadow:var(--shadow);transition:background-color 120ms var(--ease-hover),transform 160ms var(--ease-press)}
|
|
60
50
|
.go:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring)}
|
|
61
51
|
.go:active{transform:scale(.97)}
|
|
62
|
-
form[data-busy] .go{opacity:.7;pointer-events:none}
|
|
63
52
|
@media(hover:hover) and (pointer:fine){.go:hover{background:var(--brand-hover)}}
|
|
64
|
-
@media(pointer:coarse){input{height:44px;font-size:16px}.toggle{width:36px;height:36px}}
|
|
65
53
|
@media(max-width:400px){.gate{padding:24px}}
|
|
66
54
|
@media(prefers-reduced-motion:reduce){.gate{transition:opacity 240ms var(--ease-out)}@starting-style{.gate{transform:none}}.go:active{transform:none}}
|
|
67
55
|
`;
|
|
68
|
-
|
|
69
|
-
// without the fragment), shows the password on request, and says when Caps
|
|
70
|
-
// Lock is on. The toggle swallows pointerdown so the caret stays in the field.
|
|
71
|
-
const JS = `var f=document.forms[0],p=document.getElementById('password'),t=document.getElementById('toggle'),c=document.getElementById('caps');
|
|
72
|
-
if(location.hash){f.action=location.pathname+location.search+location.hash}
|
|
73
|
-
t.addEventListener('pointerdown',function(e){e.preventDefault()});
|
|
74
|
-
t.addEventListener('click',function(){var s=p.type==='password';p.type=s?'text':'password';t.setAttribute('aria-pressed',s);t.setAttribute('aria-label',s?'Hide password':'Show password')});
|
|
75
|
-
function caps(e){if(e.getModifierState){c.hidden=!e.getModifierState('CapsLock')}}
|
|
76
|
-
p.addEventListener('keydown',caps);p.addEventListener('keyup',caps);p.addEventListener('blur',function(){c.hidden=true});
|
|
77
|
-
f.addEventListener('submit',function(){f.setAttribute('data-busy','')});
|
|
78
|
-
addEventListener('pageshow',function(){f.removeAttribute('data-busy')});`;
|
|
79
|
-
const EYE = '<svg class="show" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.42 12.713c-.136-.215-.204-.323-.242-.49a1.173 1.173 0 0 1 0-.446c.038-.167.106-.274.242-.49C3.546 9.505 6.895 5 12 5s8.455 4.505 9.58 6.287c.137.215.205.323.243.49.029.125.029.322 0 .446-.038.167-.106.274-.242.49C20.454 14.495 17.105 19 12 19S3.545 14.495 2.42 12.713Z"/><path d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z"/></svg>' +
|
|
80
|
-
'<svg class="hide" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.743 5.092C11.149 5.032 11.569 5 12 5c5.105 0 8.455 4.505 9.58 6.287.137.215.205.323.243.49.029.125.029.322 0 .446-.038.167-.107.275-.244.492-.3.474-.757 1.141-1.363 1.865M6.724 6.715c-2.162 1.467-3.63 3.504-4.303 4.57-.137.216-.205.324-.243.49a1.173 1.173 0 0 0 0 .446c.038.167.106.274.242.49C3.546 14.495 6.895 19 12 19c2.059 0 3.832-.732 5.289-1.723M3 3l18 18M9.879 9.879a3 3 0 1 0 4.243 4.243"/></svg>';
|
|
81
|
-
function shell(siteName, body, script) {
|
|
56
|
+
function shell(siteName, page, body) {
|
|
82
57
|
return `<!doctype html>
|
|
83
58
|
<html lang="en">
|
|
84
59
|
<head>
|
|
@@ -92,53 +67,43 @@ function shell(siteName, body, script) {
|
|
|
92
67
|
<style>${CSS}</style>
|
|
93
68
|
</head>
|
|
94
69
|
<body>
|
|
95
|
-
<main class="gate">
|
|
70
|
+
<main class="gate" data-gate="${page}">
|
|
96
71
|
<h1>${escapeHtml(siteName)}</h1>
|
|
97
72
|
${body}
|
|
98
73
|
</main>
|
|
99
|
-
${script ? `<script>${JS}</script>` : ''}
|
|
100
74
|
</body>
|
|
101
75
|
</html>
|
|
102
76
|
`;
|
|
103
77
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
? `<p id="err" class="note error" role="alert">That password didn’t match. Try again.</p>`
|
|
107
|
-
: '';
|
|
108
|
-
const invalid = failed ? ' aria-invalid="true" aria-describedby="err"' : '';
|
|
109
|
-
return shell(siteName, `<p class="hint">Use the password from the message that shared this link.</p>
|
|
110
|
-
<form method="post" novalidate>
|
|
111
|
-
<label for="password">Password</label>
|
|
112
|
-
<div class="field">
|
|
113
|
-
<input id="password" name="password" type="password" autocomplete="current-password" autocapitalize="off" autocorrect="off" spellcheck="false" required autofocus maxlength="${MAX_PASSWORD_LENGTH}"${invalid}>
|
|
114
|
-
<button id="toggle" class="toggle" type="button" aria-pressed="false" aria-label="Show password" aria-controls="password">${EYE}</button>
|
|
115
|
-
</div>
|
|
116
|
-
<p id="caps" class="note" aria-live="polite" hidden>Caps Lock is on.</p>
|
|
117
|
-
${error}
|
|
118
|
-
<button class="go" type="submit">Open prototypes</button>
|
|
119
|
-
</form>`, true);
|
|
120
|
-
}
|
|
78
|
+
const signInPage = (siteName) => shell(siteName, 'sign-in', `<p>To sign in, open the link the studio sent you. Each link signs in one device, then stops working.</p>
|
|
79
|
+
<p>For another device, or if your link has stopped working, ask the studio for a new one.</p>`);
|
|
121
80
|
// A GET shows who the link is for and a POST signs them in, so a mail
|
|
122
|
-
// scanner that opens every link never uses
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
81
|
+
// scanner that opens every link never uses it up.
|
|
82
|
+
const linkPage = (siteName, person) => shell(siteName, 'link', `<p>This link signs in <strong>${escapeHtml(person.name)}</strong>, ${escapeHtml(person.team)}, on this device.</p>
|
|
83
|
+
<p>It works once. For another device, ask the studio for a new link.</p>
|
|
84
|
+
<form method="post"><button class="go" type="submit" autofocus>Continue</button></form>`);
|
|
85
|
+
// Someone who opens their link again on a device it already signed in gets
|
|
86
|
+
// here too, so the page offers the way in as well.
|
|
87
|
+
const usedPage = (siteName) => shell(siteName, 'used', `<p>This link has been used, has expired or was removed. Each link signs in one device, then stops working.</p>
|
|
88
|
+
<p>Ask the studio for a new one. If this device is already signed in, open the prototypes.</p>
|
|
89
|
+
<a class="go" href="/">Open the prototypes</a>`);
|
|
90
|
+
const SIGNED_OUT = 'Sign in with the link the studio sent you.';
|
|
91
|
+
const USED = 'This link has been used, has expired or was removed.';
|
|
92
|
+
const OPEN_IN_BROWSER = 'Open this link in a browser to sign in.';
|
|
93
|
+
const PREVIEWS = {
|
|
94
|
+
'sign-in': signInPage,
|
|
95
|
+
link: (siteName) => linkPage(siteName, { name: 'Rae Chen', team: 'Acme' }),
|
|
96
|
+
used: usedPage,
|
|
97
|
+
};
|
|
98
|
+
// `design-hub dev` has no gate; the Vite plugin serves this at /_gate/ (?page=link or ?page=used) so the pages can be reviewed.
|
|
99
|
+
export async function gatePreview(page, siteName = DEFAULT_SITE_NAME) {
|
|
100
|
+
// hasOwn, so ?page=__proto__ or toString never reaches Object.prototype.
|
|
101
|
+
const render = page !== null && Object.hasOwn(PREVIEWS, page) ? PREVIEWS[page] : signInPage;
|
|
102
|
+
return { html: render(siteName), csp: await csp() };
|
|
132
103
|
}
|
|
133
104
|
let cspPromise;
|
|
134
105
|
function csp() {
|
|
135
|
-
cspPromise ??=
|
|
136
|
-
const base = `default-src 'none'; style-src 'sha256-${base64(css)}'; font-src 'self'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`;
|
|
137
|
-
return {
|
|
138
|
-
withScript: `${base}; script-src 'sha256-${base64(js)}'`,
|
|
139
|
-
noScript: `${base}; script-src 'none'`,
|
|
140
|
-
};
|
|
141
|
-
});
|
|
106
|
+
cspPromise ??= sha256(CSS).then((css) => `default-src 'none'; style-src 'sha256-${base64(css)}'; script-src 'none'; font-src 'self'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`);
|
|
142
107
|
return cspPromise;
|
|
143
108
|
}
|
|
144
109
|
const NO_STORE = 'no-store, max-age=0';
|
|
@@ -148,91 +113,152 @@ function wantsHtml(request) {
|
|
|
148
113
|
return dest === 'document' || dest === 'iframe';
|
|
149
114
|
return (request.headers.get('accept') ?? '').includes('text/html');
|
|
150
115
|
}
|
|
151
|
-
|
|
152
|
-
|
|
116
|
+
const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
|
|
117
|
+
/**
|
|
118
|
+
* Every answer the gate makes itself carries these: never cached, never
|
|
119
|
+
* indexed, no referrer. The link page alone sends a same-origin referrer, so
|
|
120
|
+
* its Continue POST carries a real Origin: with no-referrer a browser sends
|
|
121
|
+
* `Origin: null`, which sameOrigin trusts only beside Sec-Fetch-Site, and
|
|
122
|
+
* older Safari sends no Sec-Fetch-Site.
|
|
123
|
+
*/
|
|
124
|
+
function gateHeaders(type, { cookie, allow, referrer = 'no-referrer', retryAfter }) {
|
|
125
|
+
const headers = new Headers({
|
|
126
|
+
'cache-control': NO_STORE,
|
|
127
|
+
'content-type': type,
|
|
128
|
+
'x-robots-tag': 'noindex, nofollow',
|
|
129
|
+
'referrer-policy': referrer,
|
|
130
|
+
});
|
|
153
131
|
if (cookie)
|
|
154
132
|
headers.append('set-cookie', cookie);
|
|
155
|
-
if (
|
|
156
|
-
headers.set('
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
headers.set('content-type', 'text/html; charset=utf-8');
|
|
161
|
-
headers.set('content-security-policy', script ? policy.withScript : policy.noScript);
|
|
162
|
-
headers.set('referrer-policy', 'no-referrer');
|
|
163
|
-
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
133
|
+
if (allow)
|
|
134
|
+
headers.set('allow', allow);
|
|
135
|
+
if (retryAfter)
|
|
136
|
+
headers.set('retry-after', String(retryAfter));
|
|
137
|
+
return headers;
|
|
164
138
|
}
|
|
165
|
-
const plain = (status, text, extra = {}) => new Response(text, {
|
|
139
|
+
const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
|
|
166
140
|
status,
|
|
167
|
-
headers:
|
|
141
|
+
headers: gateHeaders('text/plain; charset=utf-8', extra),
|
|
168
142
|
});
|
|
169
|
-
|
|
170
|
-
async function
|
|
171
|
-
if (!(request
|
|
172
|
-
return
|
|
143
|
+
/** A gate page for a browser, or `text` for anything else (a fetch, an asset, a preview bot). */
|
|
144
|
+
async function page(request, status, html, text, extra = {}) {
|
|
145
|
+
if (!wantsHtml(request))
|
|
146
|
+
return plain(request, status, text, extra);
|
|
147
|
+
const headers = gateHeaders('text/html; charset=utf-8', extra);
|
|
148
|
+
headers.set('content-security-policy', await csp());
|
|
149
|
+
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
150
|
+
}
|
|
151
|
+
const unavailable = (request, error) => plain(request, 503, error);
|
|
152
|
+
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
153
|
+
const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
|
|
154
|
+
const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
|
|
155
|
+
const NO_STORE_YET = 'Review is not set up on this site: pnpm bootstrap --vercel <project> --review.';
|
|
156
|
+
/**
|
|
157
|
+
* A store call that can fail without signing anyone out: `undefined` when the
|
|
158
|
+
* store did not answer, so the caller answers 503 and leaves the cookie alone.
|
|
159
|
+
*/
|
|
160
|
+
async function ask(run) {
|
|
173
161
|
try {
|
|
174
|
-
|
|
175
|
-
return typeof value === 'string' && value.length <= MAX_PASSWORD_LENGTH ? value : null;
|
|
162
|
+
return await run();
|
|
176
163
|
}
|
|
177
|
-
catch {
|
|
178
|
-
|
|
164
|
+
catch (error) {
|
|
165
|
+
console.error('[gate]', error);
|
|
166
|
+
return undefined;
|
|
179
167
|
}
|
|
180
168
|
}
|
|
181
|
-
async function
|
|
182
|
-
const
|
|
169
|
+
async function showLink(ctx, token) {
|
|
170
|
+
const { siteName, request } = ctx;
|
|
171
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
183
172
|
if ('error' in setup)
|
|
184
|
-
return
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return redirect('/', cookie);
|
|
173
|
+
return unavailable(request, setup.error);
|
|
174
|
+
const person = await ask(() => setup.review.peekLink(token));
|
|
175
|
+
if (person === undefined)
|
|
176
|
+
return storeDown(request);
|
|
177
|
+
return person
|
|
178
|
+
? page(request, 200, linkPage(siteName, person), OPEN_IN_BROWSER, { referrer: 'same-origin' })
|
|
179
|
+
: page(request, 404, usedPage(siteName), USED);
|
|
192
180
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (!wantsHtml(request))
|
|
198
|
-
return pass();
|
|
199
|
-
const setup = deployedReview(process.env);
|
|
200
|
-
const current = 'error' in setup ? null : await setup.review.sessionPerson(claim.id, claim.epoch).catch(() => null);
|
|
201
|
-
if (current)
|
|
202
|
-
return pass();
|
|
203
|
-
return htmlPage(request, 401, passwordPage(siteName, false), true, clearedCookie(PERSON_COOKIE));
|
|
181
|
+
/** The cookie for someone the store just said yes to, lasting to their access end. */
|
|
182
|
+
async function sessionFor(gate, person, at) {
|
|
183
|
+
const token = await issuePersonToken(gate, { id: person.id, epoch: person.epoch }, person.accessEndsAt, at);
|
|
184
|
+
return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
|
|
204
185
|
}
|
|
205
|
-
async function
|
|
186
|
+
async function signInWithLink(ctx, token) {
|
|
187
|
+
const { siteName, request, at } = ctx;
|
|
206
188
|
if (!sameOrigin(request))
|
|
207
|
-
return plain(403, 'Forbidden');
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
189
|
+
return plain(request, 403, 'Forbidden');
|
|
190
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
191
|
+
if ('error' in setup)
|
|
192
|
+
return unavailable(request, setup.error);
|
|
193
|
+
const person = await ask(() => setup.review.useLink(token));
|
|
194
|
+
if (person === undefined)
|
|
195
|
+
return storeDown(request);
|
|
196
|
+
if (!person)
|
|
197
|
+
return page(request, 404, usedPage(siteName), USED);
|
|
198
|
+
const headers = gateHeaders('text/plain; charset=utf-8', {
|
|
199
|
+
cookie: await sessionFor(setup.gate, person, at),
|
|
200
|
+
});
|
|
201
|
+
headers.set('location', '/');
|
|
202
|
+
return new Response(null, { status: 303, headers });
|
|
203
|
+
}
|
|
204
|
+
async function link(ctx, token) {
|
|
205
|
+
if (ctx.request.method === 'POST')
|
|
206
|
+
return signInWithLink(ctx, token);
|
|
207
|
+
if (isRead(ctx.request))
|
|
208
|
+
return showLink(ctx, token);
|
|
209
|
+
return plain(ctx.request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* No session. A page gets the sign-in page, and anything else (an asset, a
|
|
213
|
+
* fetch, a POST to any path but a link) a plain 401: there is nothing to
|
|
214
|
+
* sign in with here. A cookie that was sent but does not hold is cleared.
|
|
215
|
+
*/
|
|
216
|
+
function signedOut({ siteName, request }, staleCookie) {
|
|
217
|
+
const extra = staleCookie ? { cookie: clearedCookie(PERSON_COOKIE) } : {};
|
|
218
|
+
if (!isRead(request))
|
|
219
|
+
return plain(request, 401, SIGNED_OUT, extra);
|
|
220
|
+
return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
|
|
221
|
+
}
|
|
222
|
+
const pass = (cookie) => next({ headers: { 'cache-control': 'private, no-store', ...(cookie ? { 'set-cookie': cookie } : {}) } });
|
|
223
|
+
/**
|
|
224
|
+
* A signed session. While the store's last yes is fresh the signature is
|
|
225
|
+
* enough. Once it is stale the store is asked, whatever the request says it
|
|
226
|
+
* is (its headers are the client's to choose): yes passes with a fresh cookie
|
|
227
|
+
* 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.
|
|
229
|
+
*/
|
|
230
|
+
async function personSession(ctx, gate, claim) {
|
|
231
|
+
if (isFresh(claim, ctx.at))
|
|
232
|
+
return pass();
|
|
233
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
234
|
+
if ('error' in setup)
|
|
235
|
+
return unavailable(ctx.request, setup.error);
|
|
236
|
+
const current = await ask(() => setup.review.sessionPerson(claim.id, claim.epoch));
|
|
237
|
+
if (current === undefined)
|
|
238
|
+
return storeDown(ctx.request);
|
|
239
|
+
return current ? pass(await sessionFor(gate, current, ctx.at)) : signedOut(ctx, true);
|
|
214
240
|
}
|
|
215
241
|
/** The middleware a project's proxy.ts default-exports. */
|
|
216
|
-
export function createProxy({ siteName = DEFAULT_SITE_NAME } = {}) {
|
|
242
|
+
export function createProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date() } = {}) {
|
|
217
243
|
return async function proxy(request) {
|
|
218
244
|
const gate = readGate(process.env);
|
|
219
245
|
if (!gate)
|
|
220
|
-
return
|
|
246
|
+
return unavailable(request, 'The prototype gate is not configured.');
|
|
247
|
+
const ctx = { siteName, request, at: now() };
|
|
221
248
|
const url = new URL(request.url);
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
224
|
-
return
|
|
225
|
-
if (GATE_ASSETS.has(url.pathname) && (request
|
|
249
|
+
const linked = url.pathname.match(LINK_PATH);
|
|
250
|
+
if (linked)
|
|
251
|
+
return link(ctx, linked[1]);
|
|
252
|
+
if (GATE_ASSETS.has(url.pathname) && isRead(request))
|
|
226
253
|
return next();
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const claim = await readPersonToken(gate, readCookie(request, PERSON_COOKIE));
|
|
254
|
+
const cookie = readCookie(request, PERSON_COOKIE);
|
|
255
|
+
const claim = await readPersonToken(gate, cookie, ctx.at);
|
|
230
256
|
if (claim)
|
|
231
|
-
return personSession(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
return
|
|
236
|
-
return
|
|
257
|
+
return personSession(ctx, gate, claim);
|
|
258
|
+
// Tokens are signed to the store's URL, so without one none verifies. Clearing cookies then
|
|
259
|
+
// would turn a missing env var into everyone locked out, since links work once: answer 503 instead.
|
|
260
|
+
if (cookie !== undefined && gate.site === '')
|
|
261
|
+
return unavailable(request, NO_STORE_YET);
|
|
262
|
+
return signedOut(ctx, cookie !== undefined);
|
|
237
263
|
};
|
|
238
264
|
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -19,10 +19,10 @@ import { lapse } from './lapse.js';
|
|
|
19
19
|
import { markdownDocs } from './markdown.js';
|
|
20
20
|
import { registry } from './registry.js';
|
|
21
21
|
const require = createRequire(import.meta.url);
|
|
22
|
-
// The
|
|
23
|
-
// bundle.
|
|
24
|
-
// `design-hub dev` has no gate, so there /_gate/ previews the page
|
|
25
|
-
//
|
|
22
|
+
// The gate pages load before sign-in, so they cannot reach the hashed
|
|
23
|
+
// bundle. Their font goes out at the one fixed path the gate lets through.
|
|
24
|
+
// `design-hub dev` has no gate, so there /_gate/ previews the sign-in page,
|
|
25
|
+
// ?page=link a link's page and ?page=used a used or expired link's.
|
|
26
26
|
const gateFontFile = () => join(dirname(require.resolve('@fontsource-variable/geist')), 'files', 'geist-latin-wght-normal.woff2');
|
|
27
27
|
function gateFont() {
|
|
28
28
|
return {
|
|
@@ -40,7 +40,7 @@ function gateFont() {
|
|
|
40
40
|
}
|
|
41
41
|
if (url.pathname !== '/_gate/')
|
|
42
42
|
return nextMiddleware();
|
|
43
|
-
const page = await gatePreview(url.searchParams.
|
|
43
|
+
const page = await gatePreview(url.searchParams.get('page'));
|
|
44
44
|
res.setHeader('content-type', 'text/html; charset=utf-8');
|
|
45
45
|
res.setHeader('content-security-policy', page.csp);
|
|
46
46
|
res.end(page.html);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milesroxas/design-hub",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The Design Hub product: the hub that lists and serves prototypes, the review panel and its API, the
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "The Design Hub product: the hub that lists and serves prototypes, the review panel and its API, the sign-in gate, the kit, and the design-hub CLI. A project installs it and keeps only its prototypes, kit materials and docs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|