@milesroxas/design-hub 0.12.0 → 0.13.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 +4 -4
- package/core/review/service.test.ts +39 -38
- package/core/review/store.ts +2 -2
- package/core/review/suite-kv.ts +17 -0
- package/dist/core/review/store.js +2 -2
- package/dist/core/review/suite-kv.d.ts +7 -0
- package/dist/core/review/suite-kv.js +12 -0
- package/dist/scripts/client.js +3 -3
- package/dist/scripts/lib/cli.js +5 -0
- package/dist/scripts/lib/fake-host.d.ts +1 -2
- package/dist/scripts/lib/fake-host.js +20 -9
- package/dist/scripts/lib/host.d.ts +13 -25
- package/dist/scripts/lib/host.js +15 -324
- package/dist/scripts/lib/studio-deps.d.ts +16 -0
- package/dist/scripts/lib/studio-deps.js +41 -16
- package/dist/scripts/lib/studios.d.ts +14 -4
- package/dist/scripts/lib/studios.js +23 -5
- package/dist/scripts/lib/vercel-host.d.ts +17 -0
- package/dist/scripts/lib/vercel-host.js +410 -0
- package/dist/scripts/share.js +3 -2
- package/dist/scripts/studio.d.ts +8 -2
- package/dist/scripts/studio.js +38 -14
- package/dist/server/api.d.ts +6 -5
- package/dist/server/api.js +12 -10
- package/dist/server/gate.d.ts +97 -0
- package/dist/server/gate.js +537 -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 +2 -2
- 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 +18 -525
- package/dist/vite/plugin.js +1 -1
- package/package.json +3 -1
- package/skills/miles-deploy/SKILL.md +2 -1
- package/templates/studio/.env.example +3 -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/dist/vercel/proxy.d.ts
CHANGED
|
@@ -1,82 +1,6 @@
|
|
|
1
|
-
import { GATE_FONT, type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export declare
|
|
7
|
-
export { GATE_FONT };
|
|
8
|
-
export declare function gatePreview(page: string | null, siteName?: string): Promise<{
|
|
9
|
-
html: string;
|
|
10
|
-
csp: string;
|
|
11
|
-
}>;
|
|
12
|
-
export declare const isRead: (request: Request) => boolean;
|
|
13
|
-
type Extra = {
|
|
14
|
-
cookie?: string;
|
|
15
|
-
allow?: string;
|
|
16
|
-
referrer?: 'no-referrer' | 'same-origin';
|
|
17
|
-
/** Seconds, on a 503 that passes (a store that did not answer), so the review panel retries instead of switching off. */
|
|
18
|
-
retryAfter?: number;
|
|
19
|
-
};
|
|
20
|
-
export declare const plain: (request: Request, status: number, text: string, extra?: Extra) => Response;
|
|
21
|
-
/** One answer for a prototype a Client may not have, whether it is internal or not there at all. */
|
|
22
|
-
export declare const notFound: (request: Request) => Response;
|
|
23
|
-
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
24
|
-
export declare const storeDown: (request: Request) => Response;
|
|
25
|
-
/** One request at one moment: `at` is the gate's clock, so freshness is tested without waiting. */
|
|
26
|
-
export type Ctx = {
|
|
27
|
-
siteName: string;
|
|
28
|
-
request: Request;
|
|
29
|
-
at: Date;
|
|
30
|
-
};
|
|
31
|
-
export type ProxyOptions = {
|
|
32
|
-
/** The gate pages' title, the same on every client's host. */
|
|
33
|
-
siteName?: string;
|
|
34
|
-
/** The gate's clock; tests move it instead of waiting. */
|
|
35
|
-
now?: () => Date;
|
|
36
|
-
};
|
|
37
|
-
/** Who a request's host is: its client, and the gate and the client's scope built from that one lookup. */
|
|
38
|
-
export type Place = {
|
|
39
|
-
client: Client;
|
|
40
|
-
gate: Gate;
|
|
41
|
-
scope: Scope;
|
|
42
|
-
};
|
|
43
|
-
/** A request on a studio site that names a known client. */
|
|
44
|
-
export type Found = {
|
|
45
|
-
review: Review;
|
|
46
|
-
place: Place;
|
|
47
|
-
route: Route;
|
|
48
|
-
};
|
|
49
|
-
/** Why a studio request ends before it is served; `refuse` answers each one the same wherever it is met. */
|
|
50
|
-
export type Refusal = {
|
|
51
|
-
refused: 'not-found';
|
|
52
|
-
} | {
|
|
53
|
-
refused: 'store-down';
|
|
54
|
-
} | {
|
|
55
|
-
refused: 'unavailable';
|
|
56
|
-
error: string;
|
|
57
|
-
} | {
|
|
58
|
-
refused: 'signed-out';
|
|
59
|
-
stale: boolean;
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* The client `pathname` is asked of on the request's host, or why there is
|
|
63
|
-
* none: an unknown host is the plain 404, and so is the studio's own host,
|
|
64
|
-
* which serves no client's anything.
|
|
65
|
-
*/
|
|
66
|
-
export declare function findPlace(request: Request, pathname: string, hosts: HostLookup, at: Date): Promise<Found | Refusal>;
|
|
67
|
-
/** A request that may go on: who asks, the person to re-issue a cookie for, and the project it is for. */
|
|
68
|
-
export type Admitted = Found & {
|
|
69
|
-
kind: PersonKind;
|
|
70
|
-
current: Person | null;
|
|
71
|
-
project: Project | null;
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* The checks every studio request passes, in the gate and again in the files
|
|
75
|
-
* function: a session signed to this host's client (asked of the store again
|
|
76
|
-
* once its stamp is stale), then the boundary.
|
|
77
|
-
*/
|
|
78
|
-
export declare function admit(found: Found, request: Request, at: Date): Promise<Admitted | Refusal>;
|
|
79
|
-
/** The answer for a refusal: the same from the gate and from the files function. */
|
|
80
|
-
export declare function refuse(ctx: Ctx, refusal: Refusal): Promise<Response> | Response;
|
|
81
|
-
/** The middleware a studio site's proxy.ts default-exports. Its sign-in page names no client (S10). */
|
|
82
|
-
export declare function createStudioProxy({ siteName, now, }?: ProxyOptions): (request: Request) => Promise<Response>;
|
|
1
|
+
import { type Decision, GATE_FONT, gatePreview, type ProxyOptions } from '../server/gate.ts';
|
|
2
|
+
export { GATE_FONT, gatePreview };
|
|
3
|
+
/** A gate's decision as Vercel's middleware answers it. */
|
|
4
|
+
export declare function onVercel(decision: Decision): Response;
|
|
5
|
+
/** The middleware a studio site's proxy.ts default-exports. */
|
|
6
|
+
export declare function createStudioProxy(options?: ProxyOptions): (request: Request) => Promise<Response>;
|
package/dist/vercel/proxy.js
CHANGED
|
@@ -1,530 +1,23 @@
|
|
|
1
1
|
// Vercel Routing Middleware (a studio site's proxy.ts, named in vercel.json
|
|
2
|
-
// as proxy.entrypoint): the gate
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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 client's hub
|
|
10
|
-
//
|
|
11
|
-
// A used, expired or unknown link is 404. Studio has its own host as well,
|
|
12
|
-
// `studio.<apex>`, where only a Studio person's link and cookie hold, and
|
|
13
|
-
// from which a Studio person opens any client with a one-time handoff that
|
|
14
|
-
// the client's host claims (`/_gate/handoff/<token>`). Every other path needs
|
|
15
|
-
// the cookie, signed to the host's client. It carries when the store last said yes;
|
|
16
|
-
// after CHECK_SECONDS (5 minutes) the gate asks the store again on the next
|
|
17
|
-
// request of any kind, so a revoke, a sign out everywhere or a changed access
|
|
18
|
-
// end reaches every request within minutes. A store that does not answer is
|
|
19
|
-
// 503, never a sign out. A Client asking for a prototype that is not
|
|
20
|
-
// client-facing, its page or any file built from it under
|
|
21
|
-
// /<project>/_p/<slug>/, gets 404. Without a session a page gets the sign-in
|
|
22
|
-
// page (401), which says what to do and has no form, and anything else a
|
|
23
|
-
// plain-text 401. Without GATE_SECRET or the store the gate fails closed (503).
|
|
2
|
+
// as proxy.entrypoint): the gate (server/gate.ts), with its decisions carried
|
|
3
|
+
// out the way Vercel's middleware does, `next()` to pass on and `rewrite()`
|
|
4
|
+
// to go on to a function. Everything the gate decides is in the gate; this
|
|
5
|
+
// file only says it in Vercel's words.
|
|
24
6
|
import { next, rewrite } from '@vercel/functions';
|
|
25
|
-
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
export
|
|
37
|
-
|
|
38
|
-
// the pages carry no 'unsafe-inline', and they run no script at all. The gate
|
|
39
|
-
// is hub chrome, so it wears the hub's neutral system look (shell/system.css)
|
|
40
|
-
// inlined, never the project's brand. Geist (latin, variable) is the one
|
|
41
|
-
// font, preloaded so the first paint is already in it.
|
|
42
|
-
const CSS = `
|
|
43
|
-
@font-face{font-family:Geist;src:url(${GATE_FONT}) format("woff2");font-weight:100 900;font-style:normal;font-display:swap}
|
|
44
|
-
: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)}
|
|
45
|
-
@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)}}
|
|
46
|
-
*{box-sizing:border-box}
|
|
47
|
-
html{height:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}
|
|
48
|
-
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}
|
|
49
|
-
.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)}
|
|
50
|
-
@starting-style{.gate{opacity:0;transform:translateY(6px)}}
|
|
51
|
-
h1{margin:0 0 4px;font-size:20px;line-height:28px;font-weight:600;letter-spacing:-.02em;text-wrap:balance}
|
|
52
|
-
p{margin:0;color:var(--ink-3);text-wrap:pretty}
|
|
53
|
-
p+p{margin-top:8px}
|
|
54
|
-
strong{font-weight:500;color:var(--ink)}
|
|
55
|
-
form{margin:0}
|
|
56
|
-
.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)}
|
|
57
|
-
.go:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring)}
|
|
58
|
-
.go:active{transform:scale(.97)}
|
|
59
|
-
@media(hover:hover) and (pointer:fine){.go:hover{background:var(--brand-hover)}}
|
|
60
|
-
@media(max-width:400px){.gate{padding:24px}}
|
|
61
|
-
@media(prefers-reduced-motion:reduce){.gate{transition:opacity 240ms var(--ease-out)}@starting-style{.gate{transform:none}}.go:active{transform:none}}
|
|
62
|
-
`;
|
|
63
|
-
function shell(siteName, page, body) {
|
|
64
|
-
return `<!doctype html>
|
|
65
|
-
<html lang="en">
|
|
66
|
-
<head>
|
|
67
|
-
<meta charset="utf-8">
|
|
68
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
69
|
-
<meta name="robots" content="noindex, nofollow">
|
|
70
|
-
<meta name="color-scheme" content="light dark">
|
|
71
|
-
<title>${escapeHtml(siteName)}</title>
|
|
72
|
-
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
|
73
|
-
<link rel="preload" href="${GATE_FONT}" as="font" type="font/woff2" crossorigin>
|
|
74
|
-
<style>${CSS}</style>
|
|
75
|
-
</head>
|
|
76
|
-
<body>
|
|
77
|
-
<main class="gate" data-gate="${page}">
|
|
78
|
-
<h1>${escapeHtml(siteName)}</h1>
|
|
79
|
-
${body}
|
|
80
|
-
</main>
|
|
81
|
-
</body>
|
|
82
|
-
</html>
|
|
83
|
-
`;
|
|
84
|
-
}
|
|
85
|
-
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>
|
|
86
|
-
<p>For another device, or if your link has stopped working, ask the studio for a new one.</p>`);
|
|
87
|
-
// A GET shows who the link is for and a POST signs them in, so a mail
|
|
88
|
-
// scanner that opens every link never uses it up.
|
|
89
|
-
const linkPage = (siteName, person) => shell(siteName, 'link', `<p>This link signs in <strong>${escapeHtml(person.name)}</strong>, ${escapeHtml(person.team)}, on this device.</p>
|
|
90
|
-
<p>It works once. For another device, ask the studio for a new link.</p>
|
|
91
|
-
<form method="post"><button class="go" type="submit" autofocus>Continue</button></form>`);
|
|
92
|
-
// Someone who opens their link again on a device it already signed in gets
|
|
93
|
-
// here too, so the page offers the way in as well: the prototypes on a
|
|
94
|
-
// client's host, the list of clients on the studio's own.
|
|
95
|
-
const usedPage = (siteName, home = 'prototypes') => shell(siteName, 'used', `<p>This link has been used, has expired or was removed. Each link signs in one device, then stops working.</p>
|
|
96
|
-
<p>Ask the studio for a new one. If this device is already signed in, open the ${home}.</p>
|
|
97
|
-
<a class="go" href="/">Open the ${home}</a>`);
|
|
98
|
-
/**
|
|
99
|
-
* On the studio's own host, after Open: why the client did not open, and the
|
|
100
|
-
* way back to the clients. Only a Studio person signed in there meets it.
|
|
101
|
-
*/
|
|
102
|
-
const stoppedPage = (siteName, why) => shell(siteName, 'stopped', `<p>${escapeHtml(why)}</p>\n<a class="go" href="/">Back to the clients</a>`);
|
|
103
|
-
const SIGNED_OUT = 'Sign in with the link the studio sent you.';
|
|
104
|
-
const USED = 'This link has been used, has expired or was removed.';
|
|
105
|
-
const OPEN_IN_BROWSER = 'Open this link in a browser to sign in.';
|
|
106
|
-
const PREVIEWS = {
|
|
107
|
-
'sign-in': signInPage,
|
|
108
|
-
link: (siteName) => linkPage(siteName, { name: 'Rae Chen', team: 'Acme' }),
|
|
109
|
-
used: (siteName) => usedPage(siteName),
|
|
110
|
-
stopped: (siteName) => stoppedPage(siteName, NOT_OPENED),
|
|
111
|
-
};
|
|
112
|
-
// `design-hub dev` has no gate; the Vite plugin serves this at /_gate/ (?page=link or ?page=used) so the pages can be reviewed.
|
|
113
|
-
export async function gatePreview(page, siteName = DEFAULT_SITE_NAME) {
|
|
114
|
-
// hasOwn, so ?page=__proto__ or toString never reaches Object.prototype.
|
|
115
|
-
const render = page !== null && Object.hasOwn(PREVIEWS, page) ? PREVIEWS[page] : signInPage;
|
|
116
|
-
return { html: render(siteName), csp: await csp() };
|
|
117
|
-
}
|
|
118
|
-
let cspPromise;
|
|
119
|
-
function csp() {
|
|
120
|
-
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'`);
|
|
121
|
-
return cspPromise;
|
|
122
|
-
}
|
|
123
|
-
const NO_STORE = 'no-store, max-age=0';
|
|
124
|
-
function wantsHtml(request) {
|
|
125
|
-
const dest = request.headers.get('sec-fetch-dest');
|
|
126
|
-
if (dest)
|
|
127
|
-
return dest === 'document' || dest === 'iframe';
|
|
128
|
-
return (request.headers.get('accept') ?? '').includes('text/html');
|
|
129
|
-
}
|
|
130
|
-
export const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
|
|
131
|
-
/**
|
|
132
|
-
* Every answer the gate makes itself carries these: never cached, never
|
|
133
|
-
* indexed, no referrer. The link page alone sends a same-origin referrer, so
|
|
134
|
-
* its Continue POST carries a real Origin: with no-referrer a browser sends
|
|
135
|
-
* `Origin: null`, which sameOrigin trusts only beside Sec-Fetch-Site, and
|
|
136
|
-
* older Safari sends no Sec-Fetch-Site.
|
|
137
|
-
*/
|
|
138
|
-
function gateHeaders(type, { cookie, allow, referrer = 'no-referrer', retryAfter }) {
|
|
139
|
-
const headers = new Headers({
|
|
140
|
-
'cache-control': NO_STORE,
|
|
141
|
-
'content-type': type,
|
|
142
|
-
'x-robots-tag': 'noindex, nofollow',
|
|
143
|
-
'referrer-policy': referrer,
|
|
144
|
-
});
|
|
145
|
-
if (cookie)
|
|
146
|
-
headers.append('set-cookie', cookie);
|
|
147
|
-
if (allow)
|
|
148
|
-
headers.set('allow', allow);
|
|
149
|
-
if (retryAfter)
|
|
150
|
-
headers.set('retry-after', String(retryAfter));
|
|
151
|
-
return headers;
|
|
152
|
-
}
|
|
153
|
-
export const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
|
|
154
|
-
status,
|
|
155
|
-
headers: gateHeaders('text/plain; charset=utf-8', extra),
|
|
156
|
-
});
|
|
157
|
-
/** A gate page for a browser, or `text` for anything else (a fetch, an asset, a preview bot). */
|
|
158
|
-
async function page(request, status, html, text, extra = {}) {
|
|
159
|
-
if (!wantsHtml(request))
|
|
160
|
-
return plain(request, status, text, extra);
|
|
161
|
-
const headers = gateHeaders('text/html; charset=utf-8', extra);
|
|
162
|
-
headers.set('content-security-policy', await csp());
|
|
163
|
-
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
164
|
-
}
|
|
165
|
-
const unavailable = (request, error) => plain(request, 503, error);
|
|
166
|
-
/** One answer for a prototype a Client may not have, whether it is internal or not there at all. */
|
|
167
|
-
export const notFound = (request) => plain(request, 404, NOT_FOUND);
|
|
168
|
-
const NOT_FOUND = 'Not found.';
|
|
169
|
-
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
170
|
-
export const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
|
|
171
|
-
const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
|
|
172
|
-
/**
|
|
173
|
-
* A store call that can fail without signing anyone out: `undefined` when the
|
|
174
|
-
* store did not answer, so the caller answers 503 and leaves the cookie alone.
|
|
175
|
-
*/
|
|
176
|
-
async function ask(run) {
|
|
177
|
-
try {
|
|
178
|
-
return await run();
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
console.error('[gate]', error);
|
|
182
|
-
return undefined;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* What a link that signs nobody in here answers, on any host: the used page,
|
|
187
|
-
* 404, its way in the host's own `/`. On the studio's own host only Studio
|
|
188
|
-
* links are looked up, so a Client's link there reads as an unknown one.
|
|
189
|
-
*/
|
|
190
|
-
const noLink = ({ siteName, request }, at) => page(request, 404, usedPage(siteName, at === STUDIO_SITE ? 'clients' : 'prototypes'), USED);
|
|
191
|
-
async function showLink(ctx, review, at, token) {
|
|
192
|
-
const { siteName, request } = ctx;
|
|
193
|
-
const person = await ask(() => review.peekLink(at, token));
|
|
194
|
-
if (person === undefined)
|
|
195
|
-
return storeDown(request);
|
|
196
|
-
return person
|
|
197
|
-
? page(request, 200, linkPage(siteName, person), OPEN_IN_BROWSER, { referrer: 'same-origin' })
|
|
198
|
-
: noLink(ctx, at);
|
|
199
|
-
}
|
|
200
|
-
/** The cookie for someone the store just said yes to, lasting to their access end. */
|
|
201
|
-
async function sessionFor(gate, person, at) {
|
|
202
|
-
const claim = { id: person.id, epoch: person.epoch, kind: person.kind };
|
|
203
|
-
const token = await issuePersonToken(gate, claim, person.accessEndsAt, at);
|
|
204
|
-
return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
|
|
205
|
-
}
|
|
206
|
-
/** A 303 to `location` on this host, setting `cookie`: where a link or a handoff sends the person it signed in. */
|
|
207
|
-
function signedIn(cookie, location) {
|
|
208
|
-
const headers = gateHeaders('text/plain; charset=utf-8', { cookie });
|
|
209
|
-
headers.set('location', location);
|
|
210
|
-
return new Response(null, { status: 303, headers });
|
|
211
|
-
}
|
|
212
|
-
async function signInWithLink(ctx, { review, gate }, at, token) {
|
|
213
|
-
const { request } = ctx;
|
|
214
|
-
const person = await ask(() => review.useLink(at, token));
|
|
215
|
-
if (person === undefined)
|
|
216
|
-
return storeDown(request);
|
|
217
|
-
if (!person)
|
|
218
|
-
return noLink(ctx, at);
|
|
219
|
-
return signedIn(await sessionFor(gate, person, ctx.at), '/');
|
|
220
|
-
}
|
|
221
|
-
/** A one-time link where `at` reaches: shown on a read, used on a same-origin POST. */
|
|
222
|
-
async function link(ctx, setup, at, token) {
|
|
223
|
-
const { request } = ctx;
|
|
224
|
-
if (request.method === 'POST' && !sameOrigin(request))
|
|
225
|
-
return plain(request, 403, 'Forbidden');
|
|
226
|
-
if (request.method !== 'POST' && !isRead(request))
|
|
227
|
-
return plain(request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
|
|
228
|
-
return request.method === 'POST'
|
|
229
|
-
? signInWithLink(ctx, setup, at, token)
|
|
230
|
-
: showLink(ctx, setup.review, at, token);
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* No session. A page gets the sign-in page, and anything else (an asset, a
|
|
234
|
-
* fetch, a POST to any path but a link) a plain 401: there is nothing to
|
|
235
|
-
* sign in with here. A cookie that was sent but does not hold is cleared.
|
|
236
|
-
*/
|
|
237
|
-
function signedOut({ siteName, request }, staleCookie) {
|
|
238
|
-
const extra = staleCookie ? { cookie: clearedCookie(PERSON_COOKIE) } : {};
|
|
239
|
-
if (!isRead(request))
|
|
240
|
-
return plain(request, 401, SIGNED_OUT, extra);
|
|
241
|
-
return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
|
|
242
|
-
}
|
|
243
|
-
const passHeaders = (cookie) => ({
|
|
244
|
-
'cache-control': 'private, no-store',
|
|
245
|
-
...(cookie ? { 'set-cookie': cookie } : {}),
|
|
246
|
-
});
|
|
247
|
-
/**
|
|
248
|
-
* Whether a prototype is open to a Client in `scope`: client-facing, asked of
|
|
249
|
-
* the store on every request, no cache. A name that is no prototype slug
|
|
250
|
-
* (`all.js`, or '' for a path that could mean more than one file) is never
|
|
251
|
-
* open, without asking. `undefined` when the store did not answer.
|
|
252
|
-
*/
|
|
253
|
-
async function prototypeOpen(review, scope, slug) {
|
|
254
|
-
if (!isPrototypeSlug(slug))
|
|
255
|
-
return false;
|
|
256
|
-
const audience = await ask(() => review.audienceOf(scope, slug));
|
|
257
|
-
return audience === undefined ? undefined : audience === 'client';
|
|
258
|
-
}
|
|
259
|
-
const NOT_THERE = { refused: 'not-found' };
|
|
260
|
-
const DOWN = { refused: 'store-down' };
|
|
261
|
-
/** What the request's host serves, or why nothing: a site not set up, a store that did not answer, or the plain 404. */
|
|
262
|
-
async function lookUp(request, hosts, at) {
|
|
263
|
-
const setup = studioReview(process.env, null, () => at);
|
|
264
|
-
if ('error' in setup)
|
|
265
|
-
return { refused: 'unavailable', error: setup.error };
|
|
266
|
-
const host = hostName(hostOf(request));
|
|
267
|
-
const site = host ? await ask(() => hosts(host, (h) => setup.review.siteOfHost(h))) : null;
|
|
268
|
-
if (site === undefined)
|
|
269
|
-
return DOWN;
|
|
270
|
-
return site ? { review: setup.review, keys: setup.keys, site } : NOT_THERE;
|
|
271
|
-
}
|
|
272
|
-
/** A client's host: its gate and scope, and where `pathname` goes on it. */
|
|
273
|
-
function placeOf({ review, keys }, client, request, pathname) {
|
|
274
|
-
const gate = { ...keys, client: client.slug, since: client.createdAt };
|
|
275
|
-
const place = { client, gate, scope: { client: client.slug } };
|
|
276
|
-
return { review, place, route: routeOf(hostOf(request), pathname) };
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* The client `pathname` is asked of on the request's host, or why there is
|
|
280
|
-
* none: an unknown host is the plain 404, and so is the studio's own host,
|
|
281
|
-
* which serves no client's anything.
|
|
282
|
-
*/
|
|
283
|
-
export async function findPlace(request, pathname, hosts, at) {
|
|
284
|
-
const looked = await lookUp(request, hosts, at);
|
|
285
|
-
if ('refused' in looked)
|
|
286
|
-
return looked;
|
|
287
|
-
if (looked.site === STUDIO_SITE)
|
|
288
|
-
return NOT_THERE;
|
|
289
|
-
return placeOf(looked, looked.site, request, pathname);
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* The project a route names, when it is the client's, and for a Client, only
|
|
293
|
-
* a client-facing prototype's files. Every other answer is the plain 404, so
|
|
294
|
-
* another client's project, a project with no build (its files and its
|
|
295
|
-
* review API alike) and an internal prototype read the same as a path that
|
|
296
|
-
* is not there.
|
|
297
|
-
*/
|
|
298
|
-
async function boundary({ review, place, route }, kind) {
|
|
299
|
-
if (route.kind === 'none' || route.kind === 'link' || route.kind === 'handoff')
|
|
300
|
-
return NOT_THERE;
|
|
301
|
-
const named = route.kind === 'file' || route.kind === 'api' ? route.project : null;
|
|
302
|
-
if (named === null)
|
|
303
|
-
return null;
|
|
304
|
-
const scope = { client: place.client.slug, project: named };
|
|
305
|
-
const project = await ask(() => review.projectOf(scope));
|
|
306
|
-
if (project === undefined)
|
|
307
|
-
return DOWN;
|
|
308
|
-
if (!project || project.build === null)
|
|
309
|
-
return NOT_THERE;
|
|
310
|
-
if (route.kind !== 'file' || route.prototype === null || kind === 'studio')
|
|
311
|
-
return project;
|
|
312
|
-
const open = await prototypeOpen(review, scope, route.prototype);
|
|
313
|
-
return open === undefined ? DOWN : open ? project : NOT_THERE;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* The session a request's cookie carries where `at` is, signed to `gate`'s
|
|
317
|
-
* client, and asked of the store again once its stamp is stale. On the
|
|
318
|
-
* studio's own host only a Studio person's holds.
|
|
319
|
-
*/
|
|
320
|
-
async function session(review, gate, at, request, time) {
|
|
321
|
-
const cookie = readCookie(request, PERSON_COOKIE);
|
|
322
|
-
const claim = await readPersonToken(gate, cookie, time);
|
|
323
|
-
if (!claim || (at === STUDIO_SITE && claim.kind !== 'studio'))
|
|
324
|
-
return { refused: 'signed-out', stale: cookie !== undefined };
|
|
325
|
-
if (isFresh(claim, time))
|
|
326
|
-
return { claim, current: null };
|
|
327
|
-
const asked = await ask(() => review.sessionPerson(at, claim.id, claim.epoch));
|
|
328
|
-
if (asked === undefined)
|
|
329
|
-
return DOWN;
|
|
330
|
-
return asked ? { claim, current: asked } : { refused: 'signed-out', stale: true };
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* The checks every studio request passes, in the gate and again in the files
|
|
334
|
-
* function: a session signed to this host's client (asked of the store again
|
|
335
|
-
* once its stamp is stale), then the boundary.
|
|
336
|
-
*/
|
|
337
|
-
export async function admit(found, request, at) {
|
|
338
|
-
const held = await session(found.review, found.place.gate, found.place.scope, request, at);
|
|
339
|
-
if ('refused' in held)
|
|
340
|
-
return held;
|
|
341
|
-
const { claim, current } = held;
|
|
342
|
-
const kind = current ? current.kind : claim.kind;
|
|
343
|
-
const project = await boundary(found, kind);
|
|
344
|
-
return project && 'refused' in project ? project : { ...found, kind, current, project };
|
|
345
|
-
}
|
|
346
|
-
/** The answer for a refusal: the same from the gate and from the files function. */
|
|
347
|
-
export function refuse(ctx, refusal) {
|
|
348
|
-
switch (refusal.refused) {
|
|
349
|
-
case 'not-found':
|
|
350
|
-
return notFound(ctx.request);
|
|
351
|
-
case 'store-down':
|
|
352
|
-
return storeDown(ctx.request);
|
|
353
|
-
case 'unavailable':
|
|
354
|
-
return unavailable(ctx.request, refusal.error);
|
|
355
|
-
case 'signed-out':
|
|
356
|
-
return signedOut(ctx, refusal.stale);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Where an admitted request goes on to: a project's file to the files
|
|
361
|
-
* function and its review API to the review function, each with the project
|
|
362
|
-
* the path named, and anything else on to the site's own files.
|
|
363
|
-
*/
|
|
364
|
-
function onward(url, route, cookie) {
|
|
365
|
-
const headers = passHeaders(cookie);
|
|
366
|
-
if (route.kind === 'file')
|
|
367
|
-
return rewrite(new URL(`/api/files?path=${encodeURIComponent(url.pathname)}`, url), { headers });
|
|
368
|
-
if (route.kind !== 'api' || route.project === null)
|
|
369
|
-
return next({ headers });
|
|
370
|
-
const to = new URL(`/api/review${url.search}`, url);
|
|
371
|
-
to.searchParams.set('project', route.project);
|
|
372
|
-
return rewrite(to, { headers });
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
* The host proof `client add` asks a new client's host for (PROOF_PATH): its
|
|
376
|
-
* answer, on a GET whose ask the store holds for this host's client. Any
|
|
377
|
-
* other ask, method or store failure goes on as the hub path it is, so the
|
|
378
|
-
* path tells a caller without the ask nothing, and a host no client has
|
|
379
|
-
* never gets here (findPlace's plain 404).
|
|
380
|
-
*/
|
|
381
|
-
async function proven({ review, place, route }, request, pathname) {
|
|
382
|
-
const given = route.kind === 'hub' && request.method === 'GET' ? pathname.match(PROOF_PATH)?.[1] : undefined;
|
|
383
|
-
const answer = given ? await ask(() => review.proofAnswer(place.scope, given)) : null;
|
|
384
|
-
return answer ? plain(request, 200, answer) : null;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* A handoff's claim on a client's host (`/_gate/handoff/<token>`), a GET the
|
|
388
|
-
* studio's host redirected here: the store gives up the handoff once, only
|
|
389
|
-
* when it was made for this host's client and is not past its minute, and
|
|
390
|
-
* the gate sets this host's own cookie for the Studio person it names. It
|
|
391
|
-
* goes on to the client's hub, or to the project the handoff named, a path
|
|
392
|
-
* built here from the stored slug, never from the request. Anything else is
|
|
393
|
-
* a used link's answer; nothing is used up by a HEAD or a POST.
|
|
394
|
-
*/
|
|
395
|
-
async function openHandoff(ctx, { review, place }, token) {
|
|
396
|
-
const { request, siteName } = ctx;
|
|
397
|
-
if (request.method !== 'GET')
|
|
398
|
-
return plain(request, 405, 'Method not allowed', { allow: 'GET' });
|
|
399
|
-
const claimed = await ask(() => review.claimHandoff(place.scope, token));
|
|
400
|
-
if (claimed === undefined)
|
|
401
|
-
return storeDown(request);
|
|
402
|
-
if (!claimed)
|
|
403
|
-
return page(request, 404, usedPage(siteName), USED);
|
|
404
|
-
const to = claimed.project ? `/${claimed.project}/` : '/';
|
|
405
|
-
return signedIn(await sessionFor(place.gate, claimed.person, ctx.at), to);
|
|
406
|
-
}
|
|
407
|
-
const NOT_OPENED = 'This client or project cannot be opened now. It may still be waiting for its address, or it was archived or is not shared yet.';
|
|
408
|
-
const PICK_A_CLIENT = 'Open a client from the list of clients.';
|
|
409
|
-
/** A handoff that did not start, for a browser the stopped page, for anything else its text. */
|
|
410
|
-
const stopped = (ctx, status, why, extra = {}) => page(ctx.request, status, stoppedPage(ctx.siteName, why), why, extra);
|
|
411
|
-
/** Form bodies the studio hub sends: two slugs, never more than this. */
|
|
412
|
-
const MAX_FORM_BYTES = 1024;
|
|
413
|
-
/** The client and project a handoff is asked for, from a same-origin form POST; null for anything else. */
|
|
414
|
-
async function handoffAsked(request) {
|
|
415
|
-
const type = request.headers.get('content-type') ?? '';
|
|
416
|
-
if (!type.startsWith('application/x-www-form-urlencoded'))
|
|
417
|
-
return null;
|
|
418
|
-
const text = await request.text();
|
|
419
|
-
if (text.length > MAX_FORM_BYTES)
|
|
420
|
-
return null;
|
|
421
|
-
const form = new URLSearchParams(text);
|
|
422
|
-
return { client: form.get('client') ?? '', project: form.get('project') || null };
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* A handoff asked for on the studio's own host (POST /_gate/handoff): the
|
|
426
|
-
* Studio person is read from the store, whatever the cookie's stamp says,
|
|
427
|
-
* the service writes a one-time handoff for that one client, and the answer
|
|
428
|
-
* sends the browser to the client's host to claim it. The address is the
|
|
429
|
-
* client's stored host and the token, never text the request sent; a client
|
|
430
|
-
* still being added, archived or unknown, or a project it has not built, is
|
|
431
|
-
* the plain 404.
|
|
432
|
-
*/
|
|
433
|
-
async function startHandoff(ctx, review, { claim }) {
|
|
434
|
-
const { request } = ctx;
|
|
435
|
-
if (request.method !== 'POST')
|
|
436
|
-
return plain(request, 405, 'Method not allowed', { allow: 'POST' });
|
|
437
|
-
if (!sameOrigin(request))
|
|
438
|
-
return plain(request, 403, 'Forbidden');
|
|
439
|
-
const asked = await handoffAsked(request);
|
|
440
|
-
if (!asked)
|
|
441
|
-
return stopped(ctx, 400, PICK_A_CLIENT);
|
|
442
|
-
const person = await ask(() => review.sessionPerson(STUDIO_SITE, claim.id, claim.epoch));
|
|
443
|
-
if (person === undefined)
|
|
444
|
-
return stopped(ctx, 503, STORE_DOWN, { retryAfter: 30 });
|
|
445
|
-
if (!person)
|
|
446
|
-
return page(request, 401, signInPage(ctx.siteName), SIGNED_OUT, { cookie: clearedCookie(PERSON_COOKIE) });
|
|
447
|
-
let made;
|
|
448
|
-
try {
|
|
449
|
-
made = await review.startHandoff(person, asked);
|
|
450
|
-
}
|
|
451
|
-
catch (error) {
|
|
452
|
-
if (error instanceof ReviewError)
|
|
453
|
-
return stopped(ctx, 404, NOT_OPENED);
|
|
454
|
-
console.error('[gate]', error);
|
|
455
|
-
return stopped(ctx, 503, STORE_DOWN, { retryAfter: 30 });
|
|
456
|
-
}
|
|
457
|
-
const headers = gateHeaders('text/plain; charset=utf-8', {});
|
|
458
|
-
headers.set('location', `https://${made.host}${handoffPath(made.token)}`);
|
|
459
|
-
return new Response(null, { status: 303, headers });
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* The host proof `studio create` and `studio deploy` ask the studio's own
|
|
463
|
-
* host for: its answer, on a GET whose ask the store holds. Any other ask,
|
|
464
|
-
* method or store failure is the plain 404 every unknown path there gets.
|
|
465
|
-
*/
|
|
466
|
-
async function studioProof(ctx, review, given) {
|
|
467
|
-
const { request } = ctx;
|
|
468
|
-
const answer = request.method === 'GET' ? await ask(() => review.studioProofAnswer(given)) : null;
|
|
469
|
-
return answer ? plain(request, 200, answer) : notFound(request);
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* The studio's own host (`studio.<apex>`): Studio signs in here with a
|
|
473
|
-
* Studio person's link, and nobody else gets in. Its session is signed to
|
|
474
|
-
* `studio`, so a client host's cookie never holds here, and a Client's
|
|
475
|
-
* cookie answers the sign-in page a client host shows, word for word; a
|
|
476
|
-
* Client's link reads as an unknown one (the used page). Its host proof
|
|
477
|
-
* is answered before any session. Signed in, `/` is the studio hub and `/api/review` its data (the
|
|
478
|
-
* review function checks the session again), and a POST to /_gate/handoff
|
|
479
|
-
* opens a client. Everything else is the plain 404, every path a client's
|
|
480
|
-
* host answers included.
|
|
481
|
-
*/
|
|
482
|
-
async function studioHost(ctx, { review, keys }, url) {
|
|
483
|
-
const { request } = ctx;
|
|
484
|
-
const where = studioRouteOf(url.pathname);
|
|
485
|
-
const gate = studioGate(keys);
|
|
486
|
-
if (where.kind === 'none')
|
|
487
|
-
return notFound(request);
|
|
488
|
-
if (where.kind === 'link')
|
|
489
|
-
return link(ctx, { review, gate }, STUDIO_SITE, where.token);
|
|
490
|
-
if (where.kind === 'proof')
|
|
491
|
-
return studioProof(ctx, review, where.ask);
|
|
492
|
-
if (where.kind === 'asset' && isRead(request))
|
|
493
|
-
return next();
|
|
494
|
-
const held = await session(review, gate, STUDIO_SITE, request, ctx.at);
|
|
495
|
-
if ('refused' in held)
|
|
496
|
-
return refuse(ctx, held);
|
|
497
|
-
if (where.kind === 'handoff')
|
|
498
|
-
return startHandoff(ctx, review, held);
|
|
499
|
-
const headers = passHeaders(held.current ? await sessionFor(gate, held.current, ctx.at) : undefined);
|
|
500
|
-
return where.kind === 'page' ? rewrite(new URL(STUDIO_PAGE, url), { headers }) : next({ headers });
|
|
501
|
-
}
|
|
502
|
-
/** The middleware a studio site's proxy.ts default-exports. Its sign-in page names no client (S10). */
|
|
503
|
-
export function createStudioProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date(), } = {}) {
|
|
504
|
-
const hosts = hostCache(now);
|
|
7
|
+
import { createGate, GATE_FONT, gatePreview } from '../server/gate.js';
|
|
8
|
+
export { GATE_FONT, gatePreview };
|
|
9
|
+
/** A gate's decision as Vercel's middleware answers it. */
|
|
10
|
+
export function onVercel(decision) {
|
|
11
|
+
if (decision instanceof Response)
|
|
12
|
+
return decision;
|
|
13
|
+
if (decision.pass === 'rewrite')
|
|
14
|
+
return rewrite(decision.to, { headers: decision.headers });
|
|
15
|
+
return decision.headers ? next({ headers: decision.headers }) : next();
|
|
16
|
+
}
|
|
17
|
+
/** The middleware a studio site's proxy.ts default-exports. */
|
|
18
|
+
export function createStudioProxy(options = {}) {
|
|
19
|
+
const gate = createGate(options);
|
|
505
20
|
return async function proxy(request) {
|
|
506
|
-
|
|
507
|
-
const url = new URL(request.url);
|
|
508
|
-
const looked = await lookUp(request, hosts, ctx.at);
|
|
509
|
-
if ('refused' in looked)
|
|
510
|
-
return refuse(ctx, looked);
|
|
511
|
-
if (looked.site === STUDIO_SITE)
|
|
512
|
-
return studioHost(ctx, looked, url);
|
|
513
|
-
const found = placeOf(looked, looked.site, request, url.pathname);
|
|
514
|
-
const proof = await proven(found, request, url.pathname);
|
|
515
|
-
if (proof)
|
|
516
|
-
return proof;
|
|
517
|
-
const { route, place } = found;
|
|
518
|
-
if (route.kind === 'link')
|
|
519
|
-
return link(ctx, { review: found.review, gate: place.gate }, place.scope, route.token);
|
|
520
|
-
if (route.kind === 'handoff')
|
|
521
|
-
return openHandoff(ctx, found, route.token);
|
|
522
|
-
if (route.kind === 'asset' && isRead(request))
|
|
523
|
-
return next();
|
|
524
|
-
const admitted = await admit(found, request, ctx.at);
|
|
525
|
-
if ('refused' in admitted)
|
|
526
|
-
return refuse(ctx, admitted);
|
|
527
|
-
const cookie = admitted.current ? await sessionFor(place.gate, admitted.current, ctx.at) : undefined;
|
|
528
|
-
return onward(url, route, cookie);
|
|
21
|
+
return onVercel(await gate(request));
|
|
529
22
|
};
|
|
530
23
|
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -13,8 +13,8 @@ import { build, searchForWorkspaceRoot } from 'vite';
|
|
|
13
13
|
import { isProjectSlug, STUDIO_DIR, STUDIO_PAGE } from '../core/prototype.js';
|
|
14
14
|
import { devReview } from '../server/dev.js';
|
|
15
15
|
import { devPort, readEnv } from '../server/env.js';
|
|
16
|
+
import { GATE_FONT, gatePreview } from '../server/gate.js';
|
|
16
17
|
import { PACKAGE, PACKAGE_KIT, SHELL } from '../server/paths.js';
|
|
17
|
-
import { GATE_FONT, gatePreview } from '../vercel/proxy.js';
|
|
18
18
|
import { kitResolver } from './kit.js';
|
|
19
19
|
import { lapse } from './lapse.js';
|
|
20
20
|
import { markdownDocs } from './markdown.js';
|