@milesroxas/design-hub 0.10.0 → 0.11.1
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 +13 -11
- package/core/files.test.ts +76 -0
- package/core/files.ts +131 -0
- package/core/prototype.test.ts +124 -7
- package/core/prototype.ts +133 -24
- package/core/review/model.ts +119 -2
- package/core/review/policy.test.ts +79 -22
- package/core/review/policy.ts +36 -8
- package/core/review/service.test.ts +1464 -340
- package/core/review/service.ts +777 -191
- package/core/review/store.test.ts +91 -0
- package/core/review/store.ts +143 -33
- package/dist/core/files.d.ts +39 -0
- package/dist/core/files.js +107 -0
- package/dist/core/prototype.d.ts +61 -12
- package/dist/core/prototype.js +105 -26
- package/dist/core/review/model.d.ts +76 -2
- package/dist/core/review/model.js +76 -1
- package/dist/core/review/policy.d.ts +11 -4
- package/dist/core/review/policy.js +14 -2
- package/dist/core/review/service.d.ts +197 -45
- package/dist/core/review/service.js +702 -192
- package/dist/core/review/store.d.ts +84 -31
- package/dist/core/review/store.js +66 -33
- package/dist/scripts/bootstrap.d.ts +4 -34
- package/dist/scripts/bootstrap.js +9 -244
- package/dist/scripts/cli.js +6 -0
- package/dist/scripts/client.d.ts +45 -0
- package/dist/scripts/client.js +320 -0
- package/dist/scripts/commands.js +6 -0
- package/dist/scripts/comments.d.ts +8 -26
- package/dist/scripts/comments.js +27 -14
- package/dist/scripts/lib/builds.d.ts +40 -0
- package/dist/scripts/lib/builds.js +198 -0
- package/dist/scripts/lib/cli.js +3 -1
- package/dist/scripts/lib/deploy.d.ts +6 -90
- package/dist/scripts/lib/deploy.js +24 -137
- package/dist/scripts/lib/dns.d.ts +49 -0
- package/dist/scripts/lib/dns.js +141 -0
- package/dist/scripts/lib/fake-host.d.ts +168 -0
- package/dist/scripts/lib/fake-host.js +244 -0
- package/dist/scripts/lib/host.d.ts +87 -0
- package/dist/scripts/lib/host.js +362 -0
- package/dist/scripts/lib/linked.d.ts +51 -0
- package/dist/scripts/lib/linked.js +124 -0
- package/dist/scripts/lib/names.d.ts +35 -0
- package/dist/scripts/lib/names.js +77 -0
- package/dist/scripts/lib/review.d.ts +23 -2
- package/dist/scripts/lib/review.js +32 -15
- package/dist/scripts/lib/studio-deps.d.ts +35 -0
- package/dist/scripts/lib/studio-deps.js +79 -0
- package/dist/scripts/lib/studios.d.ts +70 -0
- package/dist/scripts/lib/studios.js +187 -0
- package/dist/scripts/lib/update.d.ts +33 -0
- package/dist/scripts/lib/update.js +40 -0
- package/dist/scripts/lib/vercel.d.ts +1 -23
- package/dist/scripts/lib/vercel.js +6 -81
- package/dist/scripts/people.d.ts +7 -11
- package/dist/scripts/people.js +51 -46
- package/dist/scripts/preflight.d.ts +8 -1
- package/dist/scripts/preflight.js +36 -19
- package/dist/scripts/project.d.ts +86 -0
- package/dist/scripts/project.js +141 -0
- package/dist/scripts/share.d.ts +105 -30
- package/dist/scripts/share.js +82 -154
- package/dist/scripts/skills.d.ts +18 -2
- package/dist/scripts/skills.js +22 -3
- package/dist/scripts/studio.d.ts +75 -0
- package/dist/scripts/studio.js +401 -0
- package/dist/scripts/upgrade.d.ts +10 -2
- package/dist/scripts/upgrade.js +44 -14
- package/dist/server/api.d.ts +22 -11
- package/dist/server/api.js +116 -47
- package/dist/server/blob.d.ts +10 -0
- package/dist/server/blob.js +53 -0
- package/dist/server/checks.js +2 -2
- package/dist/server/dev.js +4 -4
- package/dist/server/env.d.ts +2 -0
- package/dist/server/env.js +9 -0
- package/dist/server/fake-upstash.js +52 -3
- package/dist/server/hosts.d.ts +11 -0
- package/dist/server/hosts.js +31 -0
- package/dist/server/local.d.ts +7 -1
- package/dist/server/local.js +10 -1
- package/dist/server/mark.d.ts +41 -0
- package/dist/server/mark.js +111 -0
- package/dist/server/session.d.ts +17 -9
- package/dist/server/session.js +22 -45
- package/dist/server/upstash.js +20 -0
- package/dist/vercel/files.d.ts +11 -0
- package/dist/vercel/files.js +68 -0
- package/dist/vercel/index.d.ts +4 -3
- package/dist/vercel/index.js +12 -8
- package/dist/vercel/proxy.d.ts +69 -4
- package/dist/vercel/proxy.js +171 -107
- package/dist/vite/plugin.d.ts +24 -1
- package/dist/vite/plugin.js +46 -2
- package/kit/dials.tsx +2 -2
- package/package.json +2 -1
- package/shell/Guide.tsx +11 -5
- package/shell/Index.tsx +32 -10
- package/shell/Notice.tsx +1 -1
- package/shell/controls.ts +1 -1
- package/shell/hub.ts +5 -1
- package/shell/main.tsx +18 -3
- package/shell/review/Review.tsx +6 -4
- package/shell/review/ThreadCard.tsx +20 -7
- package/shell/review/client.ts +42 -14
- package/shell/review/view.test.ts +19 -0
- package/shell/review/view.ts +25 -0
- package/shell/shell.css +4 -0
- package/shell/studio/ClientHub.tsx +98 -0
- package/shell/studio/hub.css +48 -0
- package/shell/studio/index.html +14 -0
- package/shell/studio/main.tsx +17 -0
- package/skills/miles-deploy/SKILL.md +120 -65
- package/skills/miles-design-hub/SKILL.md +11 -9
- package/skills/miles-review/SKILL.md +5 -7
- package/skills/miles-worktrees/SKILL.md +3 -3
- package/templates/conductor/settings.toml +1 -1
- package/templates/studio/.env.example +9 -0
- package/templates/studio/.vercelignore +5 -0
- package/templates/studio/README.md +84 -0
- package/templates/studio/_gitignore +7 -0
- package/templates/studio/api/files.ts +4 -0
- package/templates/studio/api/review.ts +4 -0
- package/templates/studio/build.mjs +4 -0
- package/templates/studio/package.json +16 -0
- package/templates/studio/proxy.ts +4 -0
- package/templates/studio/studio.json +9 -0
- package/templates/studio/vercel.json +27 -0
package/dist/server/api.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { SLUG } from '../core/feature.js';
|
|
2
|
-
import {
|
|
2
|
+
import { hostName, isProjectSlug } from '../core/prototype.js';
|
|
3
|
+
import { createReview, NO_PROJECT, ReviewError } from '../core/review/service.js';
|
|
3
4
|
import { jevTriager } from '../core/review/triage.js';
|
|
4
|
-
import {
|
|
5
|
+
import { hostOf } from './hosts.js';
|
|
6
|
+
import { clearedCookie, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, } from './session.js';
|
|
5
7
|
import { storeConfig, upstashKV } from './upstash.js';
|
|
6
8
|
export const API_PATH = '/api/review';
|
|
7
9
|
const MAX_BODY_BYTES = 64 * 1024;
|
|
@@ -25,25 +27,37 @@ function prototypeOf(v) {
|
|
|
25
27
|
throw new ReviewError('invalid', 'Name the prototype.', ['prototype: a prototype slug']);
|
|
26
28
|
return s;
|
|
27
29
|
}
|
|
28
|
-
|
|
30
|
+
/** The scope of an op about prototypes and comments, which needs a project: without one, there is none to find. */
|
|
31
|
+
function projectScope(scope) {
|
|
32
|
+
if (!scope.project)
|
|
33
|
+
throw new ReviewError('not_found', NO_PROJECT);
|
|
34
|
+
return scope;
|
|
35
|
+
}
|
|
36
|
+
/** What a client's hub shows of a project: its slug, for the link, and its name. */
|
|
37
|
+
const listed = ({ slug, name }) => ({ slug, name });
|
|
38
|
+
async function query(op, params, { review, actor, scope }) {
|
|
29
39
|
switch (op) {
|
|
30
40
|
case 'me':
|
|
31
|
-
|
|
41
|
+
// The client is this request's: the page judges its controls in it, as the service does every op.
|
|
42
|
+
return actor ? { person: visible(actor), client: scope.client } : { person: null };
|
|
32
43
|
case 'comments':
|
|
33
44
|
// viewAs=client is Studio's Client view; the service checks the value and never widens a Client's view.
|
|
34
45
|
return {
|
|
35
|
-
comments: await review.listComments(actor, prototypeOf(params.get('prototype')), params.get('viewAs') ?? undefined),
|
|
46
|
+
comments: await review.listComments(projectScope(scope), actor, prototypeOf(params.get('prototype')), params.get('viewAs') ?? undefined),
|
|
36
47
|
};
|
|
37
48
|
case 'prototypes':
|
|
38
49
|
// The index's read: the view the caller gets and the client-facing slugs. viewAs is checked as comments checks it.
|
|
39
|
-
return await review.visiblePrototypes(actor, params.get('viewAs') ?? undefined);
|
|
50
|
+
return await review.visiblePrototypes(projectScope(scope), actor, params.get('viewAs') ?? undefined);
|
|
40
51
|
case 'controls':
|
|
41
52
|
// A prototype page's read: the caller's view, and what Studio marked on its dials and variants.
|
|
42
|
-
return await review.controlsOf(actor, params.get('prototype'));
|
|
53
|
+
return await review.controlsOf(projectScope(scope), actor, params.get('prototype'));
|
|
54
|
+
case 'projects':
|
|
55
|
+
// A client's hub: every project of the scope's client with a build to open.
|
|
56
|
+
return { projects: (await review.listProjects(scope, actor)).map(listed) };
|
|
43
57
|
case 'people':
|
|
44
|
-
return { people: (await review.listPeople(actor)).map(visible) };
|
|
58
|
+
return { people: (await review.listPeople(scope, actor)).map(visible) };
|
|
45
59
|
case 'access':
|
|
46
|
-
return { access: await review.defaultAccess(actor) };
|
|
60
|
+
return { access: await review.defaultAccess(scope, actor) };
|
|
47
61
|
default:
|
|
48
62
|
return undefined;
|
|
49
63
|
}
|
|
@@ -53,31 +67,45 @@ const link = (r) => ({
|
|
|
53
67
|
link: `/invite/${r.token}`,
|
|
54
68
|
});
|
|
55
69
|
/** Studio's commands over people, their links and how long they keep access. */
|
|
56
|
-
async function peopleCommand(op, b, { review, actor }) {
|
|
70
|
+
async function peopleCommand(op, b, { review, actor, scope }) {
|
|
57
71
|
switch (op) {
|
|
58
72
|
case 'people.add':
|
|
59
|
-
return link(await review.addPerson(actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
|
|
73
|
+
return link(await review.addPerson(scope, actor, { name: b.name, team: b.team, kind: b.kind, access: b.access }));
|
|
60
74
|
case 'people.link':
|
|
61
|
-
return link(await review.newLink(actor, str(b.id)));
|
|
75
|
+
return link(await review.newLink(scope, actor, str(b.id)));
|
|
62
76
|
case 'people.signOut':
|
|
63
|
-
return link(await review.signOutEverywhere(actor, str(b.id)));
|
|
77
|
+
return link(await review.signOutEverywhere(scope, actor, str(b.id)));
|
|
64
78
|
case 'people.revoke':
|
|
65
|
-
return { person: visible(await review.revoke(actor, str(b.id))) };
|
|
79
|
+
return { person: visible(await review.revoke(scope, actor, str(b.id))) };
|
|
66
80
|
case 'people.access':
|
|
67
|
-
return { person: visible(await review.setAccess(actor, str(b.id), b.access)) };
|
|
81
|
+
return { person: visible(await review.setAccess(scope, actor, str(b.id), b.access)) };
|
|
68
82
|
case 'access.set':
|
|
69
|
-
return { access: await review.setDefaultAccess(actor, b.access) };
|
|
83
|
+
return { access: await review.setDefaultAccess(scope, actor, b.access) };
|
|
70
84
|
default:
|
|
71
85
|
return undefined;
|
|
72
86
|
}
|
|
73
87
|
}
|
|
88
|
+
/** The commands about a project's prototypes and comments; the rest are the client's people. */
|
|
89
|
+
const PROJECT_COMMANDS = [
|
|
90
|
+
'comment.add',
|
|
91
|
+
'comment.reply',
|
|
92
|
+
'comment.edit',
|
|
93
|
+
'comment.remove',
|
|
94
|
+
'comment.resolve',
|
|
95
|
+
'comment.triage',
|
|
96
|
+
'prototype.audience',
|
|
97
|
+
'control.audience',
|
|
98
|
+
];
|
|
74
99
|
async function command(op, b, ctx) {
|
|
100
|
+
if (!PROJECT_COMMANDS.includes(op))
|
|
101
|
+
return peopleCommand(op, b, ctx);
|
|
75
102
|
const { review, actor } = ctx;
|
|
103
|
+
const scope = projectScope(ctx.scope);
|
|
76
104
|
const at = () => ({ prototype: prototypeOf(b.prototype), id: str(b.id) });
|
|
77
105
|
switch (op) {
|
|
78
106
|
case 'comment.add':
|
|
79
107
|
return {
|
|
80
|
-
comment: await review.add(actor, {
|
|
108
|
+
comment: await review.add(scope, actor, {
|
|
81
109
|
body: b.body,
|
|
82
110
|
anchor: b.anchor,
|
|
83
111
|
states: b.states,
|
|
@@ -86,37 +114,37 @@ async function command(op, b, ctx) {
|
|
|
86
114
|
};
|
|
87
115
|
case 'comment.reply':
|
|
88
116
|
return {
|
|
89
|
-
comment: await review.reply(actor, {
|
|
117
|
+
comment: await review.reply(scope, actor, {
|
|
90
118
|
prototype: prototypeOf(b.prototype),
|
|
91
119
|
thread: str(b.thread),
|
|
92
120
|
body: b.body,
|
|
93
121
|
}),
|
|
94
122
|
};
|
|
95
123
|
case 'comment.edit':
|
|
96
|
-
return { comment: await review.edit(actor, { ...at(), body: b.body }) };
|
|
124
|
+
return { comment: await review.edit(scope, actor, { ...at(), body: b.body }) };
|
|
97
125
|
case 'comment.remove':
|
|
98
|
-
return { removed: await review.remove(actor, at()) };
|
|
126
|
+
return { removed: await review.remove(scope, actor, at()) };
|
|
99
127
|
case 'comment.resolve':
|
|
100
|
-
return { comment: await review.resolve(actor, { ...at(), resolved: b.resolved === true }) };
|
|
128
|
+
return { comment: await review.resolve(scope, actor, { ...at(), resolved: b.resolved === true }) };
|
|
101
129
|
case 'comment.triage':
|
|
102
|
-
return {
|
|
130
|
+
return {
|
|
131
|
+
comment: await review.retriage(scope, actor, { ...at(), triage: b.triage, states: b.states }),
|
|
132
|
+
};
|
|
103
133
|
case 'prototype.audience':
|
|
104
134
|
return {
|
|
105
135
|
prototype: b.prototype,
|
|
106
|
-
audience: await review.setAudience(actor, b.prototype, b.audience),
|
|
136
|
+
audience: await review.setAudience(scope, actor, b.prototype, b.audience),
|
|
107
137
|
};
|
|
108
138
|
case 'control.audience':
|
|
109
139
|
return {
|
|
110
140
|
prototype: b.prototype,
|
|
111
|
-
...(await review.setControl(actor, {
|
|
141
|
+
...(await review.setControl(scope, actor, {
|
|
112
142
|
prototype: b.prototype,
|
|
113
143
|
kind: b.kind,
|
|
114
144
|
id: b.id,
|
|
115
145
|
audience: b.audience,
|
|
116
146
|
})),
|
|
117
147
|
};
|
|
118
|
-
default:
|
|
119
|
-
return peopleCommand(op, b, ctx);
|
|
120
148
|
}
|
|
121
149
|
}
|
|
122
150
|
async function readJson(request) {
|
|
@@ -161,45 +189,86 @@ export async function handleReview(request, ctx) {
|
|
|
161
189
|
return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
|
|
162
190
|
}
|
|
163
191
|
}
|
|
164
|
-
/** The person a request's signed cookie names, while they may still sign in
|
|
165
|
-
export async function actorFrom(request, review, gate) {
|
|
192
|
+
/** The person a request's signed cookie names, while they may still sign in, in `scope`. */
|
|
193
|
+
export async function actorFrom(request, review, gate, scope) {
|
|
166
194
|
const claim = await readPersonToken(gate, readCookie(request, PERSON_COOKIE));
|
|
167
|
-
return claim ? review.sessionPerson(claim.id, claim.epoch) : null;
|
|
195
|
+
return claim ? review.sessionPerson(scope, claim.id, claim.epoch) : null;
|
|
168
196
|
}
|
|
197
|
+
/** What a studio site says when it is not set up: every client host sees it, so it names no command, studio or client. */
|
|
198
|
+
const SITE_NOT_SET_UP = 'This site is not set up yet.';
|
|
169
199
|
/**
|
|
170
|
-
*
|
|
171
|
-
* its key is set and `checks` is given. `now` is the clock, so the gate
|
|
172
|
-
* the service it asks run on one (the gate's tests move it).
|
|
200
|
+
* A studio site's review service: Upstash for the store, Jev for triage
|
|
201
|
+
* when its key is set and `checks` is given. `now` is the clock, so the gate
|
|
202
|
+
* and the service it asks run on one (the gate's tests move it). Without the
|
|
203
|
+
* gate's secret or the store, the cause goes to the site's log and a visitor
|
|
204
|
+
* gets SITE_NOT_SET_UP.
|
|
173
205
|
*/
|
|
174
|
-
export function
|
|
175
|
-
const
|
|
176
|
-
if (!gate)
|
|
177
|
-
return { error: 'The prototype gate is not configured.' };
|
|
206
|
+
export function studioReview(env, checks = null, now) {
|
|
207
|
+
const keys = readGate(env);
|
|
178
208
|
const store = storeConfig(env);
|
|
179
|
-
if (!store)
|
|
180
|
-
|
|
209
|
+
if (!keys || !store) {
|
|
210
|
+
console.error('[studio]', keys ? 'The review store is not configured.' : 'GATE_SECRET is missing or short.');
|
|
211
|
+
return { error: SITE_NOT_SET_UP };
|
|
212
|
+
}
|
|
181
213
|
return {
|
|
182
|
-
|
|
214
|
+
keys,
|
|
183
215
|
review: createReview(upstashKV(store), {
|
|
184
216
|
triage: jevTriager(env.TYPESAFE_API_KEY ?? '', checks),
|
|
185
217
|
...(now ? { now } : {}),
|
|
186
218
|
}),
|
|
187
219
|
};
|
|
188
220
|
}
|
|
189
|
-
/** The
|
|
190
|
-
|
|
191
|
-
const
|
|
221
|
+
/** The client a studio request's host names, from one host lookup; null for a host no client has. */
|
|
222
|
+
async function hostClient(request, review, hosts) {
|
|
223
|
+
const host = hostName(hostOf(request));
|
|
224
|
+
return host ? await hosts(host, (h) => review.clientOfHost(h)) : null;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The scope a signed-in studio request runs in: the host's client, and the
|
|
228
|
+
* `project` parameter only when it is one of that client's projects with a
|
|
229
|
+
* build. Null for a project the client does not have or has not built yet (a
|
|
230
|
+
* malformed slug among them, refused before any key is built), so an
|
|
231
|
+
* unshared project reads as none.
|
|
232
|
+
*/
|
|
233
|
+
async function studioScope(request, review, client) {
|
|
234
|
+
const project = new URL(request.url).searchParams.get('project');
|
|
235
|
+
if (project === null)
|
|
236
|
+
return { client };
|
|
237
|
+
if (!isProjectSlug(project))
|
|
238
|
+
return null;
|
|
239
|
+
const found = await review.projectOf({ client, project });
|
|
240
|
+
return found?.build ? { client, project } : null;
|
|
241
|
+
}
|
|
242
|
+
/** The gate's answer to no session, in JSON: it names no client (S10), and clears a cookie that does not hold. */
|
|
243
|
+
const signedOut = (request) => json(401, { error: 'Sign in with the link the studio sent you.' }, readCookie(request, PERSON_COOKIE) === undefined ? {} : { 'set-cookie': clearedCookie(PERSON_COOKIE) });
|
|
244
|
+
/**
|
|
245
|
+
* A studio site's review function: the host's client is the scope, and the
|
|
246
|
+
* gate's client, of every op. The session is read before any project is
|
|
247
|
+
* looked up (S5), so a caller signed out, or signed in for another client,
|
|
248
|
+
* gets one answer whatever project the request names.
|
|
249
|
+
*/
|
|
250
|
+
export async function studioHandler(request, checks, hosts) {
|
|
251
|
+
const setup = studioReview(process.env, checks);
|
|
192
252
|
if ('error' in setup)
|
|
193
253
|
return json(503, { error: setup.error });
|
|
194
|
-
let
|
|
254
|
+
let ctx;
|
|
195
255
|
try {
|
|
196
|
-
|
|
256
|
+
const found = await hostClient(request, setup.review, hosts);
|
|
257
|
+
if (!found)
|
|
258
|
+
return json(404, { error: 'Not found.' });
|
|
259
|
+
const client = found.slug;
|
|
260
|
+
const gate = { ...setup.keys, client, since: found.createdAt };
|
|
261
|
+
const actor = await actorFrom(request, setup.review, gate, { client });
|
|
262
|
+
if (!actor)
|
|
263
|
+
return signedOut(request);
|
|
264
|
+
const scope = await studioScope(request, setup.review, client);
|
|
265
|
+
if (!scope)
|
|
266
|
+
return json(404, { error: 'Not found.' });
|
|
267
|
+
ctx = { review: setup.review, actor, scope };
|
|
197
268
|
}
|
|
198
269
|
catch (error) {
|
|
199
|
-
// The store is down: say so in JSON, as every other answer here is, so the panel can retry.
|
|
200
270
|
console.error('[review]', error);
|
|
201
|
-
// Retry-After tells the panel this passes, unlike a 503 for a site with no store.
|
|
202
271
|
return json(503, { error: STORE_DOWN }, { 'retry-after': '30' });
|
|
203
272
|
}
|
|
204
|
-
return handleReview(request,
|
|
273
|
+
return handleReview(request, ctx);
|
|
205
274
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { del, get, list, put } from '@vercel/blob';
|
|
2
|
+
import type { Files } from '../core/files.ts';
|
|
3
|
+
/** The four SDK calls the port makes, so a test can stand in for the store. */
|
|
4
|
+
export type BlobSdk = {
|
|
5
|
+
put: typeof put;
|
|
6
|
+
get: typeof get;
|
|
7
|
+
list: typeof list;
|
|
8
|
+
del: typeof del;
|
|
9
|
+
};
|
|
10
|
+
export declare function blobFiles(token?: string, sdk?: BlobSdk): Files;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The files port on a private Vercel Blob store (`studios-findings.md`,
|
|
2
|
+
// section 1). Nothing is ever public: a blob's own URL is 403 without the
|
|
3
|
+
// store's credential. With no `token`, the SDK finds its credential itself
|
|
4
|
+
// (OIDC with a store id, else BLOB_READ_WRITE_TOKEN), so the studio site's
|
|
5
|
+
// functions work either way; a script on a person's machine passes the
|
|
6
|
+
// read-write token. That token reads and deletes every client's files: it
|
|
7
|
+
// lives in the studio folder's ignored env file and the site's env, nowhere else.
|
|
8
|
+
import { del, get, list, put } from '@vercel/blob';
|
|
9
|
+
/**
|
|
10
|
+
* Whether the store's URL for `path` names `path` and nothing else. The SDK
|
|
11
|
+
* puts a path into a URL as it is, and a URL parser drops a tab or a newline,
|
|
12
|
+
* resolves `..` and `%2e%2e`, and cuts at `?` and `#`: a path it would rewrite
|
|
13
|
+
* is never sent, so no read leaves the folder it names.
|
|
14
|
+
*/
|
|
15
|
+
function unchanged(path) {
|
|
16
|
+
try {
|
|
17
|
+
return decodeURIComponent(new URL(path, 'https://store.invalid/').pathname) === `/${path}`;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function blobFiles(token, sdk = { put, get, list, del }) {
|
|
24
|
+
const auth = token ? { token } : {};
|
|
25
|
+
return {
|
|
26
|
+
async put(path, body, type) {
|
|
27
|
+
if (!unchanged(path))
|
|
28
|
+
throw new Error(`Not a file path: ${JSON.stringify(path)}.`);
|
|
29
|
+
// A build's paths are new: without allowOverwrite the store refuses one that exists, and that is a bug.
|
|
30
|
+
await sdk.put(path, new Blob([body]), { access: 'private', contentType: type, ...auth });
|
|
31
|
+
},
|
|
32
|
+
async get(path) {
|
|
33
|
+
if (!unchanged(path))
|
|
34
|
+
return null;
|
|
35
|
+
const found = await sdk.get(path, { access: 'private', ...auth });
|
|
36
|
+
return found?.statusCode === 200 ? { body: found.stream, type: found.blob.contentType } : null;
|
|
37
|
+
},
|
|
38
|
+
async list(prefix) {
|
|
39
|
+
const paths = [];
|
|
40
|
+
let cursor;
|
|
41
|
+
do {
|
|
42
|
+
const page = await sdk.list({ prefix, limit: 1000, cursor, ...auth });
|
|
43
|
+
paths.push(...page.blobs.map((b) => b.pathname));
|
|
44
|
+
cursor = page.hasMore ? page.cursor : undefined;
|
|
45
|
+
} while (cursor);
|
|
46
|
+
return paths;
|
|
47
|
+
},
|
|
48
|
+
async remove(paths) {
|
|
49
|
+
if (paths.length)
|
|
50
|
+
await sdk.del(paths, auth);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
package/dist/server/checks.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
// The project's checks, read once: .design-hub/checks.json when the project
|
|
3
3
|
// has one, the package defaults when it does not. The scripts and the dev
|
|
4
|
-
// server import `checks` from here;
|
|
5
|
-
//
|
|
4
|
+
// server import `checks` from here; a studio site's review function runs on
|
|
5
|
+
// the package defaults.
|
|
6
6
|
import { existsSync, readFileSync } from 'node:fs';
|
|
7
7
|
import { DEFAULT_CHECKS, loadChecks } from '../core/checks.js';
|
|
8
8
|
import { CHECKS_FILE } from './paths.js';
|
package/dist/server/dev.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createReview } from '../core/review/service.js';
|
|
2
|
-
import {
|
|
2
|
+
import { keyOf } from '../core/review/store.js';
|
|
3
3
|
import { jevTriager } from '../core/review/triage.js';
|
|
4
4
|
import { API_PATH, handleReview } from './api.js';
|
|
5
5
|
import { checks } from './checks.js';
|
|
6
6
|
import { readEnv } from './env.js';
|
|
7
7
|
import { fileKV } from './file-kv.js';
|
|
8
|
-
import { LOCAL_STORE, localStudio } from './local.js';
|
|
8
|
+
import { LOCAL_SCOPE, LOCAL_STORE, localStudio } from './local.js';
|
|
9
9
|
import { ROOT } from './paths.js';
|
|
10
10
|
// Connect strips the mount path from req.url; originalUrl keeps the query the handler reads.
|
|
11
11
|
async function toRequest(req) {
|
|
@@ -42,10 +42,10 @@ export function devReview() {
|
|
|
42
42
|
});
|
|
43
43
|
const actor = localStudio();
|
|
44
44
|
// The local Studio person is in the people list like anyone else, so the people panel shows them.
|
|
45
|
-
void kv.hset(
|
|
45
|
+
void kv.hset(keyOf('studio', 'people'), actor.id, JSON.stringify(actor));
|
|
46
46
|
server.middlewares.use(API_PATH, (req, res, nextMiddleware) => {
|
|
47
47
|
toRequest(req)
|
|
48
|
-
.then((request) => handleReview(request, { review, actor }))
|
|
48
|
+
.then((request) => handleReview(request, { review, actor, scope: LOCAL_SCOPE }))
|
|
49
49
|
.then((response) => send(res, response))
|
|
50
50
|
.catch(nextMiddleware);
|
|
51
51
|
});
|
package/dist/server/env.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export declare function readEnv(dir: string): Record<string, string>;
|
|
2
2
|
export declare function writeEnv(dir: string, updates: Record<string, string>): void;
|
|
3
|
+
/** Takes these keys out of .env, keeping every other line. */
|
|
4
|
+
export declare function removeEnv(dir: string, names: readonly string[]): void;
|
|
3
5
|
/** The variables a harness (Conductor, T3 Code, a script) may hand a worktree its port in, first wins. */
|
|
4
6
|
declare const HARNESS_PORT_VARS: readonly ['DESIGN_HUB_PORT', 'CONDUCTOR_PORT'];
|
|
5
7
|
/**
|
package/dist/server/env.js
CHANGED
|
@@ -46,6 +46,15 @@ export function writeEnv(dir, updates) {
|
|
|
46
46
|
const text = next.join('\n').replace(/\n*$/, '\n');
|
|
47
47
|
writeFileSync(file, text, { mode: 0o600 });
|
|
48
48
|
}
|
|
49
|
+
/** Takes these keys out of .env, keeping every other line. */
|
|
50
|
+
export function removeEnv(dir, names) {
|
|
51
|
+
const file = join(dir, ENV_FILE);
|
|
52
|
+
if (!existsSync(file))
|
|
53
|
+
return;
|
|
54
|
+
const lines = readFileSync(file, 'utf8').split('\n');
|
|
55
|
+
const kept = lines.filter((line) => !names.includes(line.match(/^\s*([A-Z0-9_]+)\s*=/)?.[1] ?? ''));
|
|
56
|
+
writeFileSync(file, kept.join('\n'), { mode: 0o600 });
|
|
57
|
+
}
|
|
49
58
|
/** The variables a harness (Conductor, T3 Code, a script) may hand a worktree its port in, first wins. */
|
|
50
59
|
const HARNESS_PORT_VARS = ['DESIGN_HUB_PORT', 'CONDUCTOR_PORT'];
|
|
51
60
|
function asPort(value) {
|
|
@@ -1,10 +1,51 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
// A fetch that answers like Upstash's REST API from memory, for tests that
|
|
3
|
-
// exercise
|
|
3
|
+
// exercise a studio site's code path (its gate and functions) without an account.
|
|
4
4
|
import { memoryKV } from '../core/review/store.js';
|
|
5
|
+
/** How many keys one SCAN call looks at: few, so a walk takes many calls, some of them finding nothing. */
|
|
6
|
+
const SCAN_PAGE = 2;
|
|
7
|
+
/** A Redis glob as a RegExp: `*`, `?`, `[…]`, and `\` before a character that stands for itself. */
|
|
8
|
+
function globToRegExp(glob) {
|
|
9
|
+
const plain = (c) => c.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
10
|
+
let out = '';
|
|
11
|
+
for (let i = 0; i < glob.length; i++) {
|
|
12
|
+
const c = glob[i];
|
|
13
|
+
const end = c === '[' ? glob.indexOf(']', i + 1) : -1;
|
|
14
|
+
if (c === '\\' && i + 1 < glob.length)
|
|
15
|
+
out += plain(glob[++i]);
|
|
16
|
+
else if (c === '*')
|
|
17
|
+
out += '[\\s\\S]*';
|
|
18
|
+
else if (c === '?')
|
|
19
|
+
out += '[\\s\\S]';
|
|
20
|
+
else if (end > i) {
|
|
21
|
+
out += `[${glob.slice(i + 1, end)}]`;
|
|
22
|
+
i = end;
|
|
23
|
+
}
|
|
24
|
+
else
|
|
25
|
+
out += plain(c);
|
|
26
|
+
}
|
|
27
|
+
return new RegExp(`^${out}$`);
|
|
28
|
+
}
|
|
5
29
|
export function fakeUpstash() {
|
|
6
|
-
|
|
7
|
-
|
|
30
|
+
// Every key Redis would have: a hash with no fields, or an empty set, is no key at all.
|
|
31
|
+
let live = [];
|
|
32
|
+
const kv = memoryKV(undefined, (s) => {
|
|
33
|
+
live = [
|
|
34
|
+
...Object.entries(s.hashes).filter(([, v]) => Object.keys(v).length),
|
|
35
|
+
...Object.entries(s.sets).filter(([, v]) => v.length),
|
|
36
|
+
]
|
|
37
|
+
.map(([k]) => k)
|
|
38
|
+
.sort();
|
|
39
|
+
});
|
|
40
|
+
function scan(cursor, args) {
|
|
41
|
+
const at = Number(cursor);
|
|
42
|
+
const m = args.indexOf('MATCH');
|
|
43
|
+
const match = m < 0 ? '*' : args[m + 1];
|
|
44
|
+
const page = live.slice(at, at + SCAN_PAGE).filter((k) => globToRegExp(match).test(k));
|
|
45
|
+
return [at + SCAN_PAGE >= live.length ? '0' : String(at + SCAN_PAGE), page];
|
|
46
|
+
}
|
|
47
|
+
async function run(cmd, args) {
|
|
48
|
+
const [key, a, b] = args;
|
|
8
49
|
switch (cmd) {
|
|
9
50
|
case 'HGET':
|
|
10
51
|
return kv.hget(key, a);
|
|
@@ -13,6 +54,8 @@ export function fakeUpstash() {
|
|
|
13
54
|
return 1;
|
|
14
55
|
case 'HDEL':
|
|
15
56
|
return kv.hdel(key, a);
|
|
57
|
+
case 'HSETNX':
|
|
58
|
+
return (await kv.hsetnx(key, a, b)) ? 1 : 0;
|
|
16
59
|
case 'HINCRBY':
|
|
17
60
|
return kv.hincrby(key, a, Number(b));
|
|
18
61
|
case 'HGETALL':
|
|
@@ -22,6 +65,12 @@ export function fakeUpstash() {
|
|
|
22
65
|
return 1;
|
|
23
66
|
case 'SMEMBERS':
|
|
24
67
|
return kv.smembers(key);
|
|
68
|
+
case 'DEL':
|
|
69
|
+
for (const k of args)
|
|
70
|
+
await kv.del(k);
|
|
71
|
+
return args.length;
|
|
72
|
+
case 'SCAN':
|
|
73
|
+
return scan(key, args.slice(1));
|
|
25
74
|
default:
|
|
26
75
|
throw new Error(`unknown command ${cmd}`);
|
|
27
76
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Client } from '../core/review/model.ts';
|
|
2
|
+
export type HostLookup = (host: string, load: (host: string) => Promise<Client | null>) => Promise<Client | null>;
|
|
3
|
+
/** One instance's cache of host answers, the unknown ones included. `now` is its clock, so tests need not wait. */
|
|
4
|
+
export declare function hostCache(now?: () => Date): HostLookup;
|
|
5
|
+
/**
|
|
6
|
+
* The host a request was sent to, as the platform hands it on: the forwarded
|
|
7
|
+
* host, else Host, else the URL's (a function's URL can carry the deployment's
|
|
8
|
+
* own host). A client may send any of these; the cookie it holds is signed to
|
|
9
|
+
* one client, so naming another host opens nothing.
|
|
10
|
+
*/
|
|
11
|
+
export declare const hostOf: (request: Request) => string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Which client a studio site's host serves. The gate and every studio
|
|
2
|
+
// function ask on each request, so the answer is kept in memory for a minute
|
|
3
|
+
// per instance: archiving a client, or adding one, takes effect within
|
|
4
|
+
// HOST_SECONDS. A store that does not answer is never kept, so an outage ends
|
|
5
|
+
// when the store is back.
|
|
6
|
+
import { HOST_SECONDS } from '../core/review/model.js';
|
|
7
|
+
/** Hosts kept at once: a wildcard domain sends any made-up name to the gate, so the map is bounded. */
|
|
8
|
+
const MAX_HOSTS = 500;
|
|
9
|
+
/** One instance's cache of host answers, the unknown ones included. `now` is its clock, so tests need not wait. */
|
|
10
|
+
export function hostCache(now = () => new Date()) {
|
|
11
|
+
const seen = new Map();
|
|
12
|
+
return async (host, load) => {
|
|
13
|
+
const at = now().getTime();
|
|
14
|
+
const hit = seen.get(host);
|
|
15
|
+
if (hit && at - hit.at < HOST_SECONDS * 1000)
|
|
16
|
+
return hit.client;
|
|
17
|
+
const client = await load(host);
|
|
18
|
+
seen.delete(host);
|
|
19
|
+
if (seen.size >= MAX_HOSTS)
|
|
20
|
+
seen.delete(seen.keys().next().value);
|
|
21
|
+
seen.set(host, { client, at });
|
|
22
|
+
return client;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The host a request was sent to, as the platform hands it on: the forwarded
|
|
27
|
+
* host, else Host, else the URL's (a function's URL can carry the deployment's
|
|
28
|
+
* own host). A client may send any of these; the cookie it holds is signed to
|
|
29
|
+
* one client, so naming another host opens nothing.
|
|
30
|
+
*/
|
|
31
|
+
export const hostOf = (request) => request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host;
|
package/dist/server/local.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Person, type Scope } from '../core/review/model.ts';
|
|
2
2
|
export declare const LOCAL_STORE: string;
|
|
3
|
+
/**
|
|
4
|
+
* The one scope of the local store: `design-hub dev` has no host to name a
|
|
5
|
+
* client, so every op there runs in this fixed client and project, and the
|
|
6
|
+
* CLI's --local commands read and write the same records.
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOCAL_SCOPE: Scope;
|
|
3
9
|
/** Whoever runs this machine's dev server or CLI: Studio, because they hold the store itself. */
|
|
4
10
|
export declare const localStudio: (id?: string, team?: string) => Person;
|
package/dist/server/local.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
// The review store and identity on this machine, shared by the dev server
|
|
3
|
-
// and the CLI: .design-hub/review.json,
|
|
3
|
+
// and the CLI's --local commands: .design-hub/review.json, one fixed scope
|
|
4
|
+
// in it, and a local Studio person named from git.
|
|
4
5
|
import { execFileSync } from 'node:child_process';
|
|
5
6
|
import { join } from 'node:path';
|
|
7
|
+
import { LOCAL_CLIENT } from '../core/review/model.js';
|
|
6
8
|
import { STATE_DIR } from './paths.js';
|
|
7
9
|
export const LOCAL_STORE = join(STATE_DIR, 'review.json');
|
|
10
|
+
/**
|
|
11
|
+
* The one scope of the local store: `design-hub dev` has no host to name a
|
|
12
|
+
* client, so every op there runs in this fixed client and project, and the
|
|
13
|
+
* CLI's --local commands read and write the same records.
|
|
14
|
+
*/
|
|
15
|
+
export const LOCAL_SCOPE = { client: LOCAL_CLIENT, project: 'site' };
|
|
8
16
|
function gitName() {
|
|
9
17
|
try {
|
|
10
18
|
return execFileSync('git', ['config', 'user.name'], { encoding: 'utf8' }).trim();
|
|
@@ -19,6 +27,7 @@ export const localStudio = (id = 'local', team = 'Local') => ({
|
|
|
19
27
|
name: gitName() || 'You',
|
|
20
28
|
team,
|
|
21
29
|
kind: 'studio',
|
|
30
|
+
client: null,
|
|
22
31
|
email: null,
|
|
23
32
|
accessEndsAt: null,
|
|
24
33
|
createdAt: new Date(0).toISOString(),
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type KV } from '../core/review/store.ts';
|
|
2
|
+
/** What the mark is checked against: a studio folder and the settings its studio.json holds. */
|
|
3
|
+
export type MarkedStudio = {
|
|
4
|
+
dir: string;
|
|
5
|
+
config: {
|
|
6
|
+
id?: string;
|
|
7
|
+
slug: string;
|
|
8
|
+
owner: string | null;
|
|
9
|
+
vercel: {
|
|
10
|
+
team: string;
|
|
11
|
+
project: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A refusal: why, and its detail (`code`, `studio`, `fix`), which the CLI
|
|
17
|
+
* prints as it prints a CliError's (scripts/lib/cli.ts, which this layer
|
|
18
|
+
* cannot import).
|
|
19
|
+
*/
|
|
20
|
+
export declare class MarkError extends Error {
|
|
21
|
+
detail: {
|
|
22
|
+
code: string;
|
|
23
|
+
studio: string;
|
|
24
|
+
fix: string;
|
|
25
|
+
};
|
|
26
|
+
constructor(message: string, detail: MarkError['detail']);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Marks the database as this studio's, for `studio create`, before anything
|
|
30
|
+
* else is written to it: 'claimed' the first time, 'kept' when it is already
|
|
31
|
+
* this studio's. One marked for another studio, or holding data it is not
|
|
32
|
+
* free for, is refused with nothing written. The mark is set by HSETNX, so of
|
|
33
|
+
* two creates at once on one empty database only one wins.
|
|
34
|
+
*/
|
|
35
|
+
export declare function claimDatabase(kv: KV, studio: MarkedStudio): Promise<'claimed' | 'kept'>;
|
|
36
|
+
/**
|
|
37
|
+
* Stops a command before it acts on a database that is not marked as this
|
|
38
|
+
* studio's, so no command of one studio ever reads or changes another's
|
|
39
|
+
* clients, people or links.
|
|
40
|
+
*/
|
|
41
|
+
export declare function mustBeOwnDatabase(kv: KV, studio: MarkedStudio): Promise<void>;
|