@milesroxas/design-hub 0.6.1 → 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 +17 -7
- package/core/review/model.ts +90 -10
- package/core/review/policy.test.ts +62 -0
- package/core/review/policy.ts +13 -9
- package/core/review/service.test.ts +464 -54
- package/core/review/service.ts +168 -36
- package/core/review/store.ts +31 -7
- package/dist/core/review/model.d.ts +39 -7
- package/dist/core/review/model.js +73 -7
- package/dist/core/review/policy.d.ts +4 -1
- package/dist/core/review/policy.js +4 -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 -5
- package/dist/core/review/store.js +27 -6
- 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/review.js +3 -3
- package/dist/scripts/lib/vercel.d.ts +8 -2
- package/dist/scripts/lib/vercel.js +23 -9
- package/dist/scripts/people.d.ts +16 -29
- package/dist/scripts/people.js +72 -39
- 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/dev.js +3 -3
- package/dist/server/fake-upstash.js +3 -2
- package/dist/server/local.d.ts +2 -2
- package/dist/server/local.js +6 -4
- 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/kit/ui/Menu.tsx +14 -4
- package/kit/ui/ui.css +16 -3
- package/package.json +2 -2
- package/shell/review/Bar.tsx +5 -4
- package/shell/review/People.tsx +194 -55
- package/shell/review/Review.tsx +9 -8
- package/shell/review/ThreadCard.tsx +24 -21
- package/shell/review/client.ts +37 -18
- package/shell/review/review.css +9 -1
- 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
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// person, per comment), so two people commenting at once never collide;
|
|
6
6
|
// two edits of the same comment at once keep the last.
|
|
7
7
|
import { base64url, hex, sha256 } from '../bytes.js';
|
|
8
|
-
import { authorOf, checkAnchor, checkBody, checkPersonInput, checkStates, checkTriage,
|
|
8
|
+
import { accessEnd, authorOf, checkAccess, checkAnchor, checkBody, checkPersonInput, checkStates, checkTriage, DEFAULT_ACCESS, hasAccess, isProfile, isRoot, LINK_DAYS, } from './model.js';
|
|
9
9
|
import { can } from './policy.js';
|
|
10
10
|
import { KEY } from './store.js';
|
|
11
11
|
export class ReviewError extends Error {
|
|
@@ -23,15 +23,27 @@ const defaults = {
|
|
|
23
23
|
random: (n) => crypto.getRandomValues(new Uint8Array(n)),
|
|
24
24
|
triage: null,
|
|
25
25
|
};
|
|
26
|
+
/** What a link's token looks like: 32 random bytes, base64url. Anything else is never looked up. */
|
|
27
|
+
const LINK_TOKEN = /^[\w-]{20,100}$/;
|
|
28
|
+
const linkKey = async (token) => hex(await sha256(token));
|
|
26
29
|
export function createReview(kv, overrides = {}) {
|
|
27
30
|
const deps = { ...defaults, ...overrides };
|
|
28
31
|
const now = () => deps.now().toISOString();
|
|
29
32
|
// Sortable by time, unique without coordination.
|
|
30
33
|
const newId = () => `${deps.now().getTime().toString(36).padStart(9, '0')}${hex(deps.random(5))}`;
|
|
31
34
|
const parse = (json) => (json === null ? null : JSON.parse(json));
|
|
35
|
+
/** A stored value that may not be JSON at all: null rather than a throw. */
|
|
36
|
+
const safeParse = (json) => {
|
|
37
|
+
try {
|
|
38
|
+
return json === null ? null : JSON.parse(json);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
32
44
|
function allow(actor, action, subject) {
|
|
33
45
|
if (!actor)
|
|
34
|
-
throw new ReviewError('unauthorized', 'Sign in with
|
|
46
|
+
throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
35
47
|
if (!can(actor, action, subject))
|
|
36
48
|
throw new ReviewError('forbidden', 'You cannot do that.');
|
|
37
49
|
}
|
|
@@ -47,23 +59,73 @@ export function createReview(kv, overrides = {}) {
|
|
|
47
59
|
throw new ReviewError('invalid', 'Some fields are not valid.', problems);
|
|
48
60
|
return [a.value, b.value];
|
|
49
61
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
// A person is three records. The profile (KEY.people) is read-modify-write, so
|
|
63
|
+
// a stale write of it can land after anything. Revocation and the epoch live
|
|
64
|
+
// outside it: revocation is written once and never cleared, the epoch only
|
|
65
|
+
// rises (HINCRBY), so no profile write, however stale, can revive a session.
|
|
66
|
+
const compose = (json, revoked, epoch) => {
|
|
67
|
+
const profile = safeParse(json);
|
|
68
|
+
if (!isProfile(profile))
|
|
69
|
+
return null;
|
|
70
|
+
// Any revocation record means revoked, an empty one included: it reads as the start of time.
|
|
71
|
+
const revokedAt = revoked == null ? null : revoked || new Date(0).toISOString();
|
|
72
|
+
return { ...profile, revokedAt, epoch: Number(epoch ?? 0) };
|
|
73
|
+
};
|
|
74
|
+
const person = async (id) => {
|
|
75
|
+
const [json, revoked, epoch] = await Promise.all([
|
|
76
|
+
kv.hget(KEY.people, id),
|
|
77
|
+
kv.hget(KEY.revoked, id),
|
|
78
|
+
kv.hget(KEY.epochs, id),
|
|
79
|
+
]);
|
|
80
|
+
return compose(json, revoked, epoch);
|
|
81
|
+
};
|
|
82
|
+
/** Writes the profile alone: revocation and epoch are never part of what it saves. */
|
|
83
|
+
const savePerson = ({ revokedAt: _r, epoch: _e, ...profile }) => kv.hset(KEY.people, profile.id, JSON.stringify(profile));
|
|
84
|
+
/** Not revoked, and their access has not ended. */
|
|
85
|
+
const active = (p) => !!p && hasAccess(p, deps.now());
|
|
86
|
+
async function issueLink(p) {
|
|
53
87
|
// The link carries the token; the store keeps only its hash, so a leaked store signs nobody in.
|
|
54
88
|
const token = base64url(deps.random(32));
|
|
55
|
-
const
|
|
89
|
+
const link = {
|
|
56
90
|
person: p.id,
|
|
57
|
-
expiresAt: new Date(deps.now().getTime() +
|
|
91
|
+
expiresAt: new Date(deps.now().getTime() + LINK_DAYS * DAY_MS).toISOString(),
|
|
58
92
|
};
|
|
59
|
-
await kv.hset(KEY.
|
|
93
|
+
await kv.hset(KEY.links, await linkKey(token), JSON.stringify(link));
|
|
60
94
|
return token;
|
|
61
95
|
}
|
|
62
|
-
async function
|
|
63
|
-
const all = await kv.hgetall(KEY.
|
|
96
|
+
async function dropLinks(personId) {
|
|
97
|
+
const all = await kv.hgetall(KEY.links);
|
|
64
98
|
for (const [hash, json] of Object.entries(all))
|
|
65
99
|
if (JSON.parse(json).person === personId)
|
|
66
|
-
await kv.hdel(KEY.
|
|
100
|
+
await kv.hdel(KEY.links, hash);
|
|
101
|
+
}
|
|
102
|
+
/** The link a token names, or null for a malformed or unknown token. */
|
|
103
|
+
async function findLink(token) {
|
|
104
|
+
if (!LINK_TOKEN.test(token))
|
|
105
|
+
return null;
|
|
106
|
+
const key = await linkKey(token);
|
|
107
|
+
const link = parse(await kv.hget(KEY.links, key));
|
|
108
|
+
return link ? { key, link } : null;
|
|
109
|
+
}
|
|
110
|
+
/** The person a current link signs in, while they may sign in. */
|
|
111
|
+
async function linkPerson(link) {
|
|
112
|
+
if (link.expiresAt <= now())
|
|
113
|
+
return null;
|
|
114
|
+
const p = await person(link.person);
|
|
115
|
+
return active(p) ? p : null;
|
|
116
|
+
}
|
|
117
|
+
async function readDefault() {
|
|
118
|
+
const stored = await kv.hget(KEY.settings, 'access');
|
|
119
|
+
return checkAccess(stored).ok ? stored : DEFAULT_ACCESS;
|
|
120
|
+
}
|
|
121
|
+
/** Someone Studio can still give a link to. */
|
|
122
|
+
async function needActive(id) {
|
|
123
|
+
const p = await needPerson(id);
|
|
124
|
+
if (p.revokedAt)
|
|
125
|
+
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
126
|
+
if (!active(p))
|
|
127
|
+
throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
128
|
+
return p;
|
|
67
129
|
}
|
|
68
130
|
async function needPerson(id) {
|
|
69
131
|
const p = await person(id);
|
|
@@ -92,55 +154,112 @@ export function createReview(kv, overrides = {}) {
|
|
|
92
154
|
/** The person a signed session names, if they still may sign in with it. */
|
|
93
155
|
async sessionPerson(id, epoch) {
|
|
94
156
|
const p = await person(id);
|
|
95
|
-
return p &&
|
|
157
|
+
return active(p) && p.epoch === epoch ? p : null;
|
|
96
158
|
},
|
|
97
159
|
async listPeople(actor) {
|
|
98
160
|
allow(actor, 'people');
|
|
99
|
-
const
|
|
161
|
+
const [profiles, revoked, epochs] = await Promise.all([
|
|
162
|
+
kv.hgetall(KEY.people),
|
|
163
|
+
kv.hgetall(KEY.revoked),
|
|
164
|
+
kv.hgetall(KEY.epochs),
|
|
165
|
+
]);
|
|
166
|
+
// A profile that is not a well-formed person is skipped, never a throw.
|
|
167
|
+
const all = Object.entries(profiles)
|
|
168
|
+
.map(([id, json]) => compose(json, revoked[id], epochs[id]))
|
|
169
|
+
.filter((p) => p !== null);
|
|
100
170
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
101
171
|
},
|
|
102
|
-
|
|
172
|
+
/** A new person and their first link. Without an access length, a Client gets the project default and Studio is open-ended. */
|
|
173
|
+
async addPerson(actor, input) {
|
|
103
174
|
allow(actor, 'people');
|
|
175
|
+
const { access, ...fields } = valid(checkPersonInput(input));
|
|
176
|
+
const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault());
|
|
104
177
|
const p = {
|
|
105
178
|
id: newId(),
|
|
106
|
-
...
|
|
179
|
+
...fields,
|
|
180
|
+
email: null,
|
|
181
|
+
accessEndsAt: accessEnd(length, deps.now()),
|
|
107
182
|
createdAt: now(),
|
|
108
183
|
revokedAt: null,
|
|
109
184
|
epoch: 0,
|
|
110
185
|
};
|
|
111
186
|
await savePerson(p);
|
|
112
|
-
return { person: p, token: await
|
|
187
|
+
return { person: p, token: await issueLink(p) };
|
|
113
188
|
},
|
|
114
|
-
/**
|
|
115
|
-
async
|
|
189
|
+
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
190
|
+
async newLink(actor, id) {
|
|
116
191
|
allow(actor, 'people');
|
|
117
|
-
const p = await
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
192
|
+
const p = await needActive(id);
|
|
193
|
+
return { person: p, token: await issueLink(p) };
|
|
194
|
+
},
|
|
195
|
+
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
196
|
+
async signOutEverywhere(actor, id) {
|
|
197
|
+
allow(actor, 'people');
|
|
198
|
+
const p = await needActive(id);
|
|
199
|
+
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
200
|
+
const next = { ...p, epoch: await kv.hincrby(KEY.epochs, id, 1) };
|
|
201
|
+
await dropLinks(id);
|
|
202
|
+
return { person: next, token: await issueLink(next) };
|
|
124
203
|
},
|
|
125
204
|
async revoke(actor, id) {
|
|
126
205
|
allow(actor, 'people');
|
|
127
206
|
if (actor.id === id)
|
|
128
207
|
throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
129
208
|
const p = await needPerson(id);
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
209
|
+
const revokedAt = p.revokedAt ?? now();
|
|
210
|
+
// Revocation and the epoch before the links go, so a failure part-way never leaves an active
|
|
211
|
+
// person; a leftover link opens nothing for someone revoked.
|
|
212
|
+
if (!p.revokedAt)
|
|
213
|
+
await kv.hset(KEY.revoked, id, revokedAt);
|
|
214
|
+
const epoch = await kv.hincrby(KEY.epochs, id, 1);
|
|
215
|
+
await dropLinks(id);
|
|
216
|
+
return { ...p, revokedAt, epoch };
|
|
217
|
+
},
|
|
218
|
+
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
219
|
+
async setAccess(actor, id, access) {
|
|
220
|
+
allow(actor, 'people');
|
|
221
|
+
const length = valid(checkAccess(access));
|
|
222
|
+
const p = await needPerson(id);
|
|
223
|
+
if (p.revokedAt)
|
|
224
|
+
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
225
|
+
await savePerson({ ...p, accessEndsAt: accessEnd(length, deps.now()) });
|
|
226
|
+
// Read back, so the answer carries a revocation or epoch that landed meanwhile.
|
|
227
|
+
return (await person(id));
|
|
228
|
+
},
|
|
229
|
+
/** How long a new Client keeps access unless Studio says otherwise. */
|
|
230
|
+
async defaultAccess(actor) {
|
|
231
|
+
allow(actor, 'people');
|
|
232
|
+
return readDefault();
|
|
233
|
+
},
|
|
234
|
+
async setDefaultAccess(actor, access) {
|
|
235
|
+
allow(actor, 'people');
|
|
236
|
+
const length = valid(checkAccess(access));
|
|
237
|
+
await kv.hset(KEY.settings, 'access', length);
|
|
238
|
+
return length;
|
|
239
|
+
},
|
|
240
|
+
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
241
|
+
async peekLink(token) {
|
|
242
|
+
const found = await findLink(token);
|
|
243
|
+
return found ? linkPerson(found.link) : null;
|
|
134
244
|
},
|
|
135
|
-
/**
|
|
136
|
-
|
|
137
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
247
|
+
* two requests with one token, only the one whose delete removed the link
|
|
248
|
+
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
249
|
+
* read and checked first, so the delete is the last store call.
|
|
250
|
+
*/
|
|
251
|
+
async useLink(token) {
|
|
252
|
+
const found = await findLink(token);
|
|
253
|
+
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
254
|
+
const p = found && (await linkPerson(found.link));
|
|
255
|
+
if (!found || !p)
|
|
138
256
|
return null;
|
|
139
|
-
|
|
140
|
-
if (!invite || invite.expiresAt <= now())
|
|
257
|
+
if ((await kv.hdel(KEY.links, found.key)) !== 1)
|
|
141
258
|
return null;
|
|
142
|
-
|
|
143
|
-
|
|
259
|
+
// A revoke or sign out everywhere can land between the check and the claim, before its
|
|
260
|
+
// dropLinks reaches this link: read again, and sign in only someone still active on the same epoch.
|
|
261
|
+
const after = await person(p.id);
|
|
262
|
+
return active(after) && after.epoch === p.epoch ? after : null;
|
|
144
263
|
},
|
|
145
264
|
async listComments(actor, prototype) {
|
|
146
265
|
allow(actor, 'read');
|
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
export interface KV {
|
|
2
2
|
hget(key: string, field: string): Promise<string | null>;
|
|
3
3
|
hset(key: string, field: string, value: string): Promise<void>;
|
|
4
|
-
|
|
4
|
+
/** How many fields it removed: 1, or 0 when the field was not there. Atomic, so it can claim a record. */
|
|
5
|
+
hdel(key: string, field: string): Promise<number>;
|
|
5
6
|
hgetall(key: string): Promise<Record<string, string>>;
|
|
7
|
+
/** Adds `by` to a field's integer (absent is 0) and returns the result. Atomic: two calls always add both. */
|
|
8
|
+
hincrby(key: string, field: string, by: number): Promise<number>;
|
|
6
9
|
sadd(key: string, member: string): Promise<void>;
|
|
7
10
|
smembers(key: string): Promise<string[]>;
|
|
8
11
|
}
|
|
9
|
-
/** Every key the hub writes starts here, so a shared Redis never mixes it with another app's data. */
|
|
10
12
|
export declare const KEY: {
|
|
11
|
-
|
|
12
|
-
readonly
|
|
13
|
-
|
|
13
|
+
/** A person's profile: everything but revocation and epoch, which nothing here can overwrite. */
|
|
14
|
+
readonly people: "hub3:people";
|
|
15
|
+
/** When a person was revoked. Written once by revoke, never cleared. */
|
|
16
|
+
readonly revoked: "hub3:revoked";
|
|
17
|
+
/** A person's session epoch, only ever raised, by HINCRBY. Absent is 0. */
|
|
18
|
+
readonly epochs: "hub3:epochs";
|
|
19
|
+
/** One-time sign-in links: hex(sha256(token)) to a Link. */
|
|
20
|
+
readonly links: "hub3:links";
|
|
21
|
+
/** Project settings, one field each: `access` is the default access length. */
|
|
22
|
+
readonly settings: "hub3:settings";
|
|
23
|
+
readonly prototypes: "hub3:prototypes";
|
|
14
24
|
readonly comments: (prototype: string) => string;
|
|
15
25
|
};
|
|
16
26
|
export type Snapshot = {
|
|
@@ -2,12 +2,25 @@
|
|
|
2
2
|
// and set operations. Production is Upstash Redis over REST
|
|
3
3
|
// (server/upstash.ts); dev and tests use the in-memory store below, which
|
|
4
4
|
// the dev server persists to a file (server/file-kv.ts).
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Every key the hub writes starts here, so a shared Redis never mixes it with
|
|
7
|
+
* another app's data. Bumped when the stored shapes change without a
|
|
8
|
+
* migration: records under an older prefix are simply never read.
|
|
9
|
+
*/
|
|
10
|
+
const PREFIX = 'hub3:';
|
|
6
11
|
export const KEY = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
/** A person's profile: everything but revocation and epoch, which nothing here can overwrite. */
|
|
13
|
+
people: `${PREFIX}people`,
|
|
14
|
+
/** When a person was revoked. Written once by revoke, never cleared. */
|
|
15
|
+
revoked: `${PREFIX}revoked`,
|
|
16
|
+
/** A person's session epoch, only ever raised, by HINCRBY. Absent is 0. */
|
|
17
|
+
epochs: `${PREFIX}epochs`,
|
|
18
|
+
/** One-time sign-in links: hex(sha256(token)) to a Link. */
|
|
19
|
+
links: `${PREFIX}links`,
|
|
20
|
+
/** Project settings, one field each: `access` is the default access length. */
|
|
21
|
+
settings: `${PREFIX}settings`,
|
|
22
|
+
prototypes: `${PREFIX}prototypes`,
|
|
23
|
+
comments: (prototype) => `${PREFIX}comments:${prototype}`,
|
|
11
24
|
};
|
|
12
25
|
export function memoryKV(initial = { hashes: {}, sets: {} }, onChange) {
|
|
13
26
|
const hashes = new Map(Object.entries(initial.hashes).map(([k, v]) => [k, new Map(Object.entries(v))]));
|
|
@@ -24,10 +37,18 @@ export function memoryKV(initial = { hashes: {}, sets: {} }, onChange) {
|
|
|
24
37
|
changed();
|
|
25
38
|
},
|
|
26
39
|
hdel: async (key, field) => {
|
|
27
|
-
hashes.get(key)?.delete(field)
|
|
40
|
+
if (!hashes.get(key)?.delete(field))
|
|
41
|
+
return 0;
|
|
28
42
|
changed();
|
|
43
|
+
return 1;
|
|
29
44
|
},
|
|
30
45
|
hgetall: async (key) => Object.fromEntries(hashes.get(key) ?? []),
|
|
46
|
+
hincrby: async (key, field, by) => {
|
|
47
|
+
const n = Number(hash(key).get(field) ?? 0) + by;
|
|
48
|
+
hash(key).set(field, String(n));
|
|
49
|
+
changed();
|
|
50
|
+
return n;
|
|
51
|
+
},
|
|
31
52
|
sadd: async (key, member) => {
|
|
32
53
|
(sets.get(key) ?? sets.set(key, new Set()).get(key)).add(member);
|
|
33
54
|
changed();
|
|
@@ -27,22 +27,23 @@ export declare const spec: {
|
|
|
27
27
|
};
|
|
28
28
|
readonly vercel: {
|
|
29
29
|
readonly type: 'string';
|
|
30
|
-
readonly description: 'Link this Vercel project and set the
|
|
30
|
+
readonly description: 'Link this Vercel project and set the gate’s signing secret on it.';
|
|
31
31
|
};
|
|
32
32
|
readonly team: {
|
|
33
33
|
readonly type: 'string';
|
|
34
34
|
readonly description: 'The Vercel team (its slug) the project lives in or is created in. Absent, the CLI’s current team.';
|
|
35
35
|
};
|
|
36
|
-
readonly 'gate-password': {
|
|
37
|
-
readonly type: 'string';
|
|
38
|
-
readonly description: 'The share password (8+ characters). Given again, it rotates.';
|
|
39
|
-
};
|
|
40
36
|
readonly review: {
|
|
41
37
|
readonly type: 'boolean';
|
|
42
38
|
readonly description: 'Provision the review store (Upstash Redis) on the Vercel project.';
|
|
43
39
|
};
|
|
40
|
+
readonly access: {
|
|
41
|
+
readonly type: 'string';
|
|
42
|
+
readonly choices: readonly ["3m", "6m", "open"];
|
|
43
|
+
readonly description: 'With --review: how long a new client keeps access by default, 3m, 6m or open. Absent, the store keeps what it has (3m when unset).';
|
|
44
|
+
};
|
|
44
45
|
};
|
|
45
|
-
readonly output: '{ ok, node, port, install, typesafe, conductor?, vercel: { project, linked, git, protection, env }?,
|
|
46
|
+
readonly output: '{ ok, node, port, install, typesafe, conductor?, vercel: { project, linked, git, protection, env }?, review: { store, local, access? }?, warnings }';
|
|
46
47
|
};
|
|
47
48
|
/**
|
|
48
49
|
* What `design-hub dev`, `open` and `preview` run first, so a fresh clone or
|
|
@@ -61,13 +62,14 @@ export declare function ensurePort(): {
|
|
|
61
62
|
* when a step throws: `vercel link` appends to it, and a later failure (the
|
|
62
63
|
* Upstash terms, most often) would otherwise leave the tree dirty.
|
|
63
64
|
*/
|
|
64
|
-
export declare function keepingGitignore<T>(dir: string, steps: () => T): T
|
|
65
|
-
/**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
export declare function keepingGitignore<T>(dir: string, steps: () => T | Promise<T>): Promise<T>;
|
|
66
|
+
/**
|
|
67
|
+
* The gate's env: the signing secret, kept when .env has one and made once
|
|
68
|
+
* otherwise. Nothing else: people sign in with their own one-time links.
|
|
69
|
+
*/
|
|
70
|
+
export declare function gateValues(env: Record<string, string>): {
|
|
71
|
+
GATE_SECRET: string;
|
|
70
72
|
};
|
|
71
|
-
export declare function main({ flags }: Args<typeof spec>): {
|
|
73
|
+
export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
72
74
|
warnings: string[];
|
|
73
|
-
}
|
|
75
|
+
}>;
|
|
@@ -8,11 +8,13 @@ import { randomBytes } from 'node:crypto';
|
|
|
8
8
|
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
9
9
|
import { tmpdir } from 'node:os';
|
|
10
10
|
import { join } from 'node:path';
|
|
11
|
+
import { ACCESS } from '../core/review/model.js';
|
|
11
12
|
import { harnessPort, readEnv, writeEnv } from '../server/env.js';
|
|
12
13
|
import { ROOT, TEMPLATES } from '../server/paths.js';
|
|
13
14
|
import { CliError } from './lib/cli.js';
|
|
14
15
|
import { gitConnected, restoreGitignore, upstashTerms } from './lib/deploy.js';
|
|
15
16
|
import { holderOf, pickPort, readClaims, writeClaim } from './lib/ports.js';
|
|
17
|
+
import { cliActor, cliReview } from './lib/review.js';
|
|
16
18
|
import { ENVIRONMENTS, envNames, linkedOrg, linkedProject, projectInfo, STORE_ENV, setEnv, vercel, whoami, } from './lib/vercel.js';
|
|
17
19
|
export const spec = {
|
|
18
20
|
name: 'bootstrap',
|
|
@@ -26,21 +28,25 @@ export const spec = {
|
|
|
26
28
|
'typesafe-key': { type: 'string', description: "Turn TypeSafe's Jev checks on with this API key." },
|
|
27
29
|
conductor: { type: 'boolean', description: 'Write .conductor/settings.toml for Conductor workspaces.' },
|
|
28
30
|
branch: { type: 'string', default: 'main', description: 'The branch Conductor workspaces land on.' },
|
|
29
|
-
vercel: {
|
|
30
|
-
team: {
|
|
31
|
+
vercel: {
|
|
31
32
|
type: 'string',
|
|
32
|
-
description: '
|
|
33
|
+
description: 'Link this Vercel project and set the gate’s signing secret on it.',
|
|
33
34
|
},
|
|
34
|
-
|
|
35
|
+
team: {
|
|
35
36
|
type: 'string',
|
|
36
|
-
description: 'The
|
|
37
|
+
description: 'The Vercel team (its slug) the project lives in or is created in. Absent, the CLI’s current team.',
|
|
37
38
|
},
|
|
38
39
|
review: {
|
|
39
40
|
type: 'boolean',
|
|
40
41
|
description: 'Provision the review store (Upstash Redis) on the Vercel project.',
|
|
41
42
|
},
|
|
43
|
+
access: {
|
|
44
|
+
type: 'string',
|
|
45
|
+
choices: ACCESS,
|
|
46
|
+
description: 'With --review: how long a new client keeps access by default, 3m, 6m or open. Absent, the store keeps what it has (3m when unset).',
|
|
47
|
+
},
|
|
42
48
|
},
|
|
43
|
-
output: '{ ok, node, port, install, typesafe, conductor?, vercel: { project, linked, git, protection, env }?,
|
|
49
|
+
output: '{ ok, node, port, install, typesafe, conductor?, vercel: { project, linked, git, protection, env }?, review: { store, local, access? }?, warnings }',
|
|
44
50
|
};
|
|
45
51
|
const MIN_NODE = [22, 18];
|
|
46
52
|
const UPSTASH = 'upstash/upstash-kv';
|
|
@@ -186,12 +192,12 @@ function link(project, team, warnings) {
|
|
|
186
192
|
* when a step throws: `vercel link` appends to it, and a later failure (the
|
|
187
193
|
* Upstash terms, most often) would otherwise leave the tree dirty.
|
|
188
194
|
*/
|
|
189
|
-
export function keepingGitignore(dir, steps) {
|
|
195
|
+
export async function keepingGitignore(dir, steps) {
|
|
190
196
|
const file = join(dir, '.gitignore');
|
|
191
197
|
const read = () => (existsSync(file) ? readFileSync(file, 'utf8') : '');
|
|
192
198
|
const before = read();
|
|
193
199
|
try {
|
|
194
|
-
return steps();
|
|
200
|
+
return await steps();
|
|
195
201
|
}
|
|
196
202
|
finally {
|
|
197
203
|
const kept = restoreGitignore(before, read());
|
|
@@ -223,8 +229,8 @@ function connectGit(project, team, warnings) {
|
|
|
223
229
|
(out?.stderr.trim().split('\n').pop() ?? ''));
|
|
224
230
|
return 'none';
|
|
225
231
|
}
|
|
226
|
-
/** Sets each value where it is missing
|
|
227
|
-
function ensureEnv(values
|
|
232
|
+
/** Sets each value where it is missing, and keeps the ones already there. */
|
|
233
|
+
function ensureEnv(values) {
|
|
228
234
|
const state = {};
|
|
229
235
|
for (const target of ENVIRONMENTS) {
|
|
230
236
|
const names = envNames(ROOT, target);
|
|
@@ -234,24 +240,18 @@ function ensureEnv(values, rotate) {
|
|
|
234
240
|
});
|
|
235
241
|
for (const [name, value] of Object.entries(values)) {
|
|
236
242
|
const key = `${name}:${target}`;
|
|
237
|
-
|
|
238
|
-
if (exists && !rotate.has(name)) {
|
|
243
|
+
if (names.has(name)) {
|
|
239
244
|
state[key] = 'kept';
|
|
240
245
|
continue;
|
|
241
246
|
}
|
|
242
|
-
const out = setEnv(ROOT, name, target, value
|
|
247
|
+
const out = setEnv(ROOT, name, target, value);
|
|
243
248
|
if (out?.status !== 0)
|
|
244
249
|
throw new CliError(`vercel env add ${name} ${target} failed.`, { stderr: out?.stderr.trim() });
|
|
245
|
-
state[key] =
|
|
250
|
+
state[key] = 'set';
|
|
246
251
|
}
|
|
247
252
|
}
|
|
248
253
|
return state;
|
|
249
254
|
}
|
|
250
|
-
// A password a reviewer can type from a message: lowercase letters and digits, no look-alikes.
|
|
251
|
-
function readablePassword() {
|
|
252
|
-
const alphabet = 'abcdefghjkmnpqrstuvwxyz23456789';
|
|
253
|
-
return Array.from(randomBytes(12), (b) => alphabet[b % alphabet.length]).join('');
|
|
254
|
-
}
|
|
255
255
|
/** Reads one environment's values into memory and deletes the file, keeping only `names`. */
|
|
256
256
|
function pullValues(target, names) {
|
|
257
257
|
const dir = mkdtempSync(join(tmpdir(), 'hub-env-'));
|
|
@@ -332,42 +332,55 @@ function typesafe(given, current) {
|
|
|
332
332
|
writeEnv(ROOT, { TYPESAFE_API_KEY: given });
|
|
333
333
|
return given;
|
|
334
334
|
}
|
|
335
|
-
/**
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
335
|
+
/**
|
|
336
|
+
* The gate's env: the signing secret, kept when .env has one and made once
|
|
337
|
+
* otherwise. Nothing else: people sign in with their own one-time links.
|
|
338
|
+
*/
|
|
339
|
+
export function gateValues(env) {
|
|
340
|
+
return { GATE_SECRET: env.GATE_SECRET || randomBytes(32).toString('base64') };
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Sets the project default access length in the deployed store. When the
|
|
344
|
+
* store's credentials did not reach .env, the CLI would reach the local
|
|
345
|
+
* store instead, so it says how to set it later rather than set the wrong one.
|
|
346
|
+
*/
|
|
347
|
+
async function setDefaultAccess(access, warnings) {
|
|
348
|
+
const { review, store } = cliReview(false);
|
|
349
|
+
if (store !== 'deployed') {
|
|
350
|
+
warnings.push(`Could not set the default access length: the store's credentials are not in .env. Once they are, run design-hub people access --access ${access}.`);
|
|
351
|
+
return undefined;
|
|
352
|
+
}
|
|
353
|
+
return review.setDefaultAccess(cliActor(), access);
|
|
342
354
|
}
|
|
343
|
-
function setUpVercel(project, flags, env, typesafeKey, warnings) {
|
|
355
|
+
async function setUpVercel(project, flags, env, typesafeKey, warnings) {
|
|
344
356
|
if (!whoami(ROOT))
|
|
345
357
|
throw new CliError('The Vercel CLI is not installed or not signed in.', {
|
|
346
358
|
fix: 'design-hub signin, send the user its url, then run this again.',
|
|
347
359
|
});
|
|
348
360
|
const linked = link(project, flags.team, warnings);
|
|
349
|
-
const
|
|
350
|
-
writeEnv(ROOT,
|
|
351
|
-
const values = {
|
|
361
|
+
const gate = gateValues(env);
|
|
362
|
+
writeEnv(ROOT, gate);
|
|
363
|
+
const values = { ...gate };
|
|
352
364
|
// Comment triage runs on the server, so with the review store on, the key goes to Vercel too.
|
|
353
365
|
if (flags.review && typesafeKey)
|
|
354
366
|
values.TYPESAFE_API_KEY = typesafeKey;
|
|
355
|
-
if (rotated)
|
|
356
|
-
warnings.push('The password changed: every visitor signs in again.');
|
|
357
367
|
const result = {
|
|
358
368
|
vercel: {
|
|
359
369
|
project,
|
|
360
370
|
linked,
|
|
361
371
|
git: connectGit(project, flags.team, warnings),
|
|
362
372
|
protection: protection(project, flags.team),
|
|
363
|
-
env: ensureEnv(values
|
|
373
|
+
env: ensureEnv(values),
|
|
364
374
|
},
|
|
365
|
-
share: { password },
|
|
366
375
|
};
|
|
367
|
-
if (!flags.review)
|
|
376
|
+
if (!flags.review) {
|
|
377
|
+
warnings.push('Nobody can sign in yet: each person signs in with a one-time link the review store keeps. Run this again with --review.');
|
|
368
378
|
return result;
|
|
369
|
-
|
|
370
|
-
|
|
379
|
+
}
|
|
380
|
+
const review = provisionReview(project, flags.team, warnings);
|
|
381
|
+
const access = flags.access ? await setDefaultAccess(flags.access, warnings) : undefined;
|
|
382
|
+
warnings.push('Run design-hub share so the new env reaches the site; then design-hub people add --name "<you>" --team "<studio>" --kind studio --site <url>.');
|
|
383
|
+
return { ...result, review: access ? { ...review, access } : review };
|
|
371
384
|
}
|
|
372
385
|
function envIgnored(warnings) {
|
|
373
386
|
try {
|
|
@@ -383,9 +396,13 @@ function envIgnored(warnings) {
|
|
|
383
396
|
if (tracked.status === 0)
|
|
384
397
|
warnings.push('.claude/launch.json is committed but holds this folder’s port. Run `git rm --cached .claude/launch.json` and add it to .gitignore.');
|
|
385
398
|
}
|
|
386
|
-
export function main({ flags }) {
|
|
399
|
+
export async function main({ flags }) {
|
|
387
400
|
if (flags.review && !flags.vercel)
|
|
388
401
|
throw new CliError('--review needs --vercel <project>.');
|
|
402
|
+
if (flags.access && !flags.review)
|
|
403
|
+
throw new CliError('--access needs --review.', {
|
|
404
|
+
fix: 'Or set it later: design-hub people access --access <3m|6m|open>.',
|
|
405
|
+
});
|
|
389
406
|
const warnings = [];
|
|
390
407
|
const node = checkNode();
|
|
391
408
|
const env = readEnv(ROOT);
|
|
@@ -402,7 +419,7 @@ export function main({ flags }) {
|
|
|
402
419
|
result.conductor = writeConductor(flags.branch, warnings);
|
|
403
420
|
const project = flags.vercel;
|
|
404
421
|
if (project)
|
|
405
|
-
Object.assign(result, keepingGitignore(ROOT, () => setUpVercel(project, flags, env, typesafeKey, warnings)));
|
|
422
|
+
Object.assign(result, await keepingGitignore(ROOT, () => setUpVercel(project, flags, env, typesafeKey, warnings)));
|
|
406
423
|
envIgnored(warnings);
|
|
407
424
|
return { ...result, warnings };
|
|
408
425
|
}
|
|
@@ -45,9 +45,9 @@ export type Plan = {
|
|
|
45
45
|
*/
|
|
46
46
|
export declare function plan(repo: Repo, forceCli: boolean): Plan;
|
|
47
47
|
/**
|
|
48
|
-
* How `design-hub share` asks for the home page: as a browser does
|
|
49
|
-
* answers a client that does not accept HTML with a plain-text
|
|
50
|
-
* no
|
|
48
|
+
* How `design-hub share` asks for the home page: as a browser does, signed
|
|
49
|
+
* out. The gate answers a client that does not accept HTML with a plain-text
|
|
50
|
+
* 401, which has no sign-in page to find.
|
|
51
51
|
*/
|
|
52
52
|
export declare const GATE_REQUEST: {
|
|
53
53
|
readonly redirect: 'manual';
|
|
@@ -68,8 +68,16 @@ export type Verdict = {
|
|
|
68
68
|
problem: string;
|
|
69
69
|
fix: string;
|
|
70
70
|
};
|
|
71
|
-
/** Whether the production URL answers a signed-out visitor with the hub's
|
|
71
|
+
/** Whether the production URL answers a signed-out visitor with the hub's sign-in page. */
|
|
72
72
|
export declare function gateVerdict(answer: Answer): Verdict;
|
|
73
|
+
/**
|
|
74
|
+
* A link no store holds, asked for as a browser does once the sign-in page
|
|
75
|
+
* passed: it proves the review store is wired, since people sign in only with
|
|
76
|
+
* links it keeps. 43 characters, the length of a real token.
|
|
77
|
+
*/
|
|
78
|
+
export declare const PROBE_LINK: string;
|
|
79
|
+
/** Whether the probe link's answer shows a review store behind the gate: 404 means it looked and found nothing. */
|
|
80
|
+
export declare function storeVerdict(status: number): Verdict;
|
|
73
81
|
export type Seen = {
|
|
74
82
|
url: string;
|
|
75
83
|
state: string;
|