@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
package/core/review/service.ts
CHANGED
|
@@ -6,16 +6,23 @@
|
|
|
6
6
|
// two edits of the same comment at once keep the last.
|
|
7
7
|
import { base64url, hex, sha256 } from '../bytes.ts';
|
|
8
8
|
import {
|
|
9
|
+
type Access,
|
|
10
|
+
accessEnd,
|
|
9
11
|
authorOf,
|
|
10
12
|
type Checked,
|
|
11
13
|
type Comment,
|
|
14
|
+
checkAccess,
|
|
12
15
|
checkAnchor,
|
|
13
16
|
checkBody,
|
|
14
17
|
checkPersonInput,
|
|
15
18
|
checkStates,
|
|
16
19
|
checkTriage,
|
|
17
|
-
|
|
20
|
+
DEFAULT_ACCESS,
|
|
21
|
+
hasAccess,
|
|
22
|
+
isProfile,
|
|
18
23
|
isRoot,
|
|
24
|
+
LINK_DAYS,
|
|
25
|
+
type Link,
|
|
19
26
|
type Person,
|
|
20
27
|
type Triage,
|
|
21
28
|
} from './model.ts';
|
|
@@ -52,7 +59,9 @@ const defaults: Deps = {
|
|
|
52
59
|
triage: null,
|
|
53
60
|
};
|
|
54
61
|
|
|
55
|
-
|
|
62
|
+
/** What a link's token looks like: 32 random bytes, base64url. Anything else is never looked up. */
|
|
63
|
+
const LINK_TOKEN = /^[\w-]{20,100}$/;
|
|
64
|
+
const linkKey = async (token: string) => hex(await sha256(token));
|
|
56
65
|
|
|
57
66
|
export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
58
67
|
const deps = { ...defaults, ...overrides };
|
|
@@ -61,9 +70,17 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
61
70
|
const newId = () => `${deps.now().getTime().toString(36).padStart(9, '0')}${hex(deps.random(5))}`;
|
|
62
71
|
|
|
63
72
|
const parse = <T>(json: string | null): T | null => (json === null ? null : (JSON.parse(json) as T));
|
|
73
|
+
/** A stored value that may not be JSON at all: null rather than a throw. */
|
|
74
|
+
const safeParse = (json: string | null): unknown => {
|
|
75
|
+
try {
|
|
76
|
+
return json === null ? null : JSON.parse(json);
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
64
81
|
|
|
65
82
|
function allow(actor: Person | null, action: Action, subject?: Comment): asserts actor is Person {
|
|
66
|
-
if (!actor) throw new ReviewError('unauthorized', 'Sign in with
|
|
83
|
+
if (!actor) throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
67
84
|
if (!can(actor, action, subject)) throw new ReviewError('forbidden', 'You cannot do that.');
|
|
68
85
|
}
|
|
69
86
|
|
|
@@ -79,24 +96,78 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
79
96
|
return [a.value, b.value];
|
|
80
97
|
}
|
|
81
98
|
|
|
82
|
-
|
|
83
|
-
|
|
99
|
+
// A person is three records. The profile (KEY.people) is read-modify-write, so
|
|
100
|
+
// a stale write of it can land after anything. Revocation and the epoch live
|
|
101
|
+
// outside it: revocation is written once and never cleared, the epoch only
|
|
102
|
+
// rises (HINCRBY), so no profile write, however stale, can revive a session.
|
|
103
|
+
const compose = (
|
|
104
|
+
json: string | null,
|
|
105
|
+
revoked: string | null | undefined,
|
|
106
|
+
epoch: string | null | undefined,
|
|
107
|
+
) => {
|
|
108
|
+
const profile = safeParse(json);
|
|
109
|
+
if (!isProfile(profile)) return null;
|
|
110
|
+
// Any revocation record means revoked, an empty one included: it reads as the start of time.
|
|
111
|
+
const revokedAt = revoked == null ? null : revoked || new Date(0).toISOString();
|
|
112
|
+
return { ...profile, revokedAt, epoch: Number(epoch ?? 0) };
|
|
113
|
+
};
|
|
114
|
+
const person = async (id: string): Promise<Person | null> => {
|
|
115
|
+
const [json, revoked, epoch] = await Promise.all([
|
|
116
|
+
kv.hget(KEY.people, id),
|
|
117
|
+
kv.hget(KEY.revoked, id),
|
|
118
|
+
kv.hget(KEY.epochs, id),
|
|
119
|
+
]);
|
|
120
|
+
return compose(json, revoked, epoch);
|
|
121
|
+
};
|
|
122
|
+
/** Writes the profile alone: revocation and epoch are never part of what it saves. */
|
|
123
|
+
const savePerson = ({ revokedAt: _r, epoch: _e, ...profile }: Person) =>
|
|
124
|
+
kv.hset(KEY.people, profile.id, JSON.stringify(profile));
|
|
125
|
+
/** Not revoked, and their access has not ended. */
|
|
126
|
+
const active = (p: Person | null): p is Person => !!p && hasAccess(p, deps.now());
|
|
84
127
|
|
|
85
|
-
async function
|
|
128
|
+
async function issueLink(p: Person): Promise<string> {
|
|
86
129
|
// The link carries the token; the store keeps only its hash, so a leaked store signs nobody in.
|
|
87
130
|
const token = base64url(deps.random(32));
|
|
88
|
-
const
|
|
131
|
+
const link: Link = {
|
|
89
132
|
person: p.id,
|
|
90
|
-
expiresAt: new Date(deps.now().getTime() +
|
|
133
|
+
expiresAt: new Date(deps.now().getTime() + LINK_DAYS * DAY_MS).toISOString(),
|
|
91
134
|
};
|
|
92
|
-
await kv.hset(KEY.
|
|
135
|
+
await kv.hset(KEY.links, await linkKey(token), JSON.stringify(link));
|
|
93
136
|
return token;
|
|
94
137
|
}
|
|
95
138
|
|
|
96
|
-
async function
|
|
97
|
-
const all = await kv.hgetall(KEY.
|
|
139
|
+
async function dropLinks(personId: string): Promise<void> {
|
|
140
|
+
const all = await kv.hgetall(KEY.links);
|
|
98
141
|
for (const [hash, json] of Object.entries(all))
|
|
99
|
-
if ((JSON.parse(json) as
|
|
142
|
+
if ((JSON.parse(json) as Link).person === personId) await kv.hdel(KEY.links, hash);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The link a token names, or null for a malformed or unknown token. */
|
|
146
|
+
async function findLink(token: string): Promise<{ key: string; link: Link } | null> {
|
|
147
|
+
if (!LINK_TOKEN.test(token)) return null;
|
|
148
|
+
const key = await linkKey(token);
|
|
149
|
+
const link = parse<Link>(await kv.hget(KEY.links, key));
|
|
150
|
+
return link ? { key, link } : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The person a current link signs in, while they may sign in. */
|
|
154
|
+
async function linkPerson(link: Link): Promise<Person | null> {
|
|
155
|
+
if (link.expiresAt <= now()) return null;
|
|
156
|
+
const p = await person(link.person);
|
|
157
|
+
return active(p) ? p : null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function readDefault(): Promise<Access> {
|
|
161
|
+
const stored = await kv.hget(KEY.settings, 'access');
|
|
162
|
+
return checkAccess(stored).ok ? (stored as Access) : DEFAULT_ACCESS;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Someone Studio can still give a link to. */
|
|
166
|
+
async function needActive(id: string): Promise<Person> {
|
|
167
|
+
const p = await needPerson(id);
|
|
168
|
+
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
169
|
+
if (!active(p)) throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
170
|
+
return p;
|
|
100
171
|
}
|
|
101
172
|
|
|
102
173
|
async function needPerson(id: string): Promise<Person> {
|
|
@@ -131,56 +202,117 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
131
202
|
/** The person a signed session names, if they still may sign in with it. */
|
|
132
203
|
async sessionPerson(id: string, epoch: number): Promise<Person | null> {
|
|
133
204
|
const p = await person(id);
|
|
134
|
-
return p &&
|
|
205
|
+
return active(p) && p.epoch === epoch ? p : null;
|
|
135
206
|
},
|
|
136
207
|
|
|
137
208
|
async listPeople(actor: Person | null): Promise<Person[]> {
|
|
138
209
|
allow(actor, 'people');
|
|
139
|
-
const
|
|
210
|
+
const [profiles, revoked, epochs] = await Promise.all([
|
|
211
|
+
kv.hgetall(KEY.people),
|
|
212
|
+
kv.hgetall(KEY.revoked),
|
|
213
|
+
kv.hgetall(KEY.epochs),
|
|
214
|
+
]);
|
|
215
|
+
// A profile that is not a well-formed person is skipped, never a throw.
|
|
216
|
+
const all = Object.entries(profiles)
|
|
217
|
+
.map(([id, json]) => compose(json, revoked[id], epochs[id]))
|
|
218
|
+
.filter((p): p is Person => p !== null);
|
|
140
219
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
141
220
|
},
|
|
142
221
|
|
|
143
|
-
|
|
222
|
+
/** A new person and their first link. Without an access length, a Client gets the project default and Studio is open-ended. */
|
|
223
|
+
async addPerson(actor: Person | null, input: unknown): Promise<{ person: Person; token: string }> {
|
|
144
224
|
allow(actor, 'people');
|
|
225
|
+
const { access, ...fields } = valid(checkPersonInput(input));
|
|
226
|
+
const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault());
|
|
145
227
|
const p: Person = {
|
|
146
228
|
id: newId(),
|
|
147
|
-
...
|
|
229
|
+
...fields,
|
|
230
|
+
email: null,
|
|
231
|
+
accessEndsAt: accessEnd(length, deps.now()),
|
|
148
232
|
createdAt: now(),
|
|
149
233
|
revokedAt: null,
|
|
150
234
|
epoch: 0,
|
|
151
235
|
};
|
|
152
236
|
await savePerson(p);
|
|
153
|
-
return { person: p, token: await
|
|
237
|
+
return { person: p, token: await issueLink(p) };
|
|
154
238
|
},
|
|
155
239
|
|
|
156
|
-
/**
|
|
157
|
-
async
|
|
240
|
+
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
241
|
+
async newLink(actor: Person | null, id: string): Promise<{ person: Person; token: string }> {
|
|
158
242
|
allow(actor, 'people');
|
|
159
|
-
const p = await
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
243
|
+
const p = await needActive(id);
|
|
244
|
+
return { person: p, token: await issueLink(p) };
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
248
|
+
async signOutEverywhere(actor: Person | null, id: string): Promise<{ person: Person; token: string }> {
|
|
249
|
+
allow(actor, 'people');
|
|
250
|
+
const p = await needActive(id);
|
|
251
|
+
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
252
|
+
const next = { ...p, epoch: await kv.hincrby(KEY.epochs, id, 1) };
|
|
253
|
+
await dropLinks(id);
|
|
254
|
+
return { person: next, token: await issueLink(next) };
|
|
165
255
|
},
|
|
166
256
|
|
|
167
257
|
async revoke(actor: Person | null, id: string): Promise<Person> {
|
|
168
258
|
allow(actor, 'people');
|
|
169
259
|
if (actor.id === id) throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
170
260
|
const p = await needPerson(id);
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
261
|
+
const revokedAt = p.revokedAt ?? now();
|
|
262
|
+
// Revocation and the epoch before the links go, so a failure part-way never leaves an active
|
|
263
|
+
// person; a leftover link opens nothing for someone revoked.
|
|
264
|
+
if (!p.revokedAt) await kv.hset(KEY.revoked, id, revokedAt);
|
|
265
|
+
const epoch = await kv.hincrby(KEY.epochs, id, 1);
|
|
266
|
+
await dropLinks(id);
|
|
267
|
+
return { ...p, revokedAt, epoch };
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
271
|
+
async setAccess(actor: Person | null, id: string, access: unknown): Promise<Person> {
|
|
272
|
+
allow(actor, 'people');
|
|
273
|
+
const length = valid(checkAccess(access));
|
|
274
|
+
const p = await needPerson(id);
|
|
275
|
+
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
276
|
+
await savePerson({ ...p, accessEndsAt: accessEnd(length, deps.now()) });
|
|
277
|
+
// Read back, so the answer carries a revocation or epoch that landed meanwhile.
|
|
278
|
+
return (await person(id)) as Person;
|
|
279
|
+
},
|
|
280
|
+
|
|
281
|
+
/** How long a new Client keeps access unless Studio says otherwise. */
|
|
282
|
+
async defaultAccess(actor: Person | null): Promise<Access> {
|
|
283
|
+
allow(actor, 'people');
|
|
284
|
+
return readDefault();
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
async setDefaultAccess(actor: Person | null, access: unknown): Promise<Access> {
|
|
288
|
+
allow(actor, 'people');
|
|
289
|
+
const length = valid(checkAccess(access));
|
|
290
|
+
await kv.hset(KEY.settings, 'access', length);
|
|
291
|
+
return length;
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
295
|
+
async peekLink(token: string): Promise<Person | null> {
|
|
296
|
+
const found = await findLink(token);
|
|
297
|
+
return found ? linkPerson(found.link) : null;
|
|
175
298
|
},
|
|
176
299
|
|
|
177
|
-
/**
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
300
|
+
/**
|
|
301
|
+
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
302
|
+
* two requests with one token, only the one whose delete removed the link
|
|
303
|
+
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
304
|
+
* read and checked first, so the delete is the last store call.
|
|
305
|
+
*/
|
|
306
|
+
async useLink(token: string): Promise<Person | null> {
|
|
307
|
+
const found = await findLink(token);
|
|
308
|
+
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
309
|
+
const p = found && (await linkPerson(found.link));
|
|
310
|
+
if (!found || !p) return null;
|
|
311
|
+
if ((await kv.hdel(KEY.links, found.key)) !== 1) return null;
|
|
312
|
+
// A revoke or sign out everywhere can land between the check and the claim, before its
|
|
313
|
+
// dropLinks reaches this link: read again, and sign in only someone still active on the same epoch.
|
|
314
|
+
const after = await person(p.id);
|
|
315
|
+
return active(after) && after.epoch === p.epoch ? after : null;
|
|
184
316
|
},
|
|
185
317
|
|
|
186
318
|
async listComments(actor: Person | null, prototype: string): Promise<Comment[]> {
|
package/core/review/store.ts
CHANGED
|
@@ -6,18 +6,35 @@
|
|
|
6
6
|
export interface KV {
|
|
7
7
|
hget(key: string, field: string): Promise<string | null>;
|
|
8
8
|
hset(key: string, field: string, value: string): Promise<void>;
|
|
9
|
-
|
|
9
|
+
/** How many fields it removed: 1, or 0 when the field was not there. Atomic, so it can claim a record. */
|
|
10
|
+
hdel(key: string, field: string): Promise<number>;
|
|
10
11
|
hgetall(key: string): Promise<Record<string, string>>;
|
|
12
|
+
/** Adds `by` to a field's integer (absent is 0) and returns the result. Atomic: two calls always add both. */
|
|
13
|
+
hincrby(key: string, field: string, by: number): Promise<number>;
|
|
11
14
|
sadd(key: string, member: string): Promise<void>;
|
|
12
15
|
smembers(key: string): Promise<string[]>;
|
|
13
16
|
}
|
|
14
17
|
|
|
15
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Every key the hub writes starts here, so a shared Redis never mixes it with
|
|
20
|
+
* another app's data. Bumped when the stored shapes change without a
|
|
21
|
+
* migration: records under an older prefix are simply never read.
|
|
22
|
+
*/
|
|
23
|
+
const PREFIX = 'hub3:';
|
|
24
|
+
|
|
16
25
|
export const KEY = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
/** A person's profile: everything but revocation and epoch, which nothing here can overwrite. */
|
|
27
|
+
people: `${PREFIX}people`,
|
|
28
|
+
/** When a person was revoked. Written once by revoke, never cleared. */
|
|
29
|
+
revoked: `${PREFIX}revoked`,
|
|
30
|
+
/** A person's session epoch, only ever raised, by HINCRBY. Absent is 0. */
|
|
31
|
+
epochs: `${PREFIX}epochs`,
|
|
32
|
+
/** One-time sign-in links: hex(sha256(token)) to a Link. */
|
|
33
|
+
links: `${PREFIX}links`,
|
|
34
|
+
/** Project settings, one field each: `access` is the default access length. */
|
|
35
|
+
settings: `${PREFIX}settings`,
|
|
36
|
+
prototypes: `${PREFIX}prototypes`,
|
|
37
|
+
comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
|
|
21
38
|
} as const;
|
|
22
39
|
|
|
23
40
|
export type Snapshot = { hashes: Record<string, Record<string, string>>; sets: Record<string, string[]> };
|
|
@@ -38,10 +55,17 @@ export function memoryKV(initial: Snapshot = { hashes: {}, sets: {} }, onChange?
|
|
|
38
55
|
changed();
|
|
39
56
|
},
|
|
40
57
|
hdel: async (key, field) => {
|
|
41
|
-
hashes.get(key)?.delete(field);
|
|
58
|
+
if (!hashes.get(key)?.delete(field)) return 0;
|
|
42
59
|
changed();
|
|
60
|
+
return 1;
|
|
43
61
|
},
|
|
44
62
|
hgetall: async (key) => Object.fromEntries(hashes.get(key) ?? []),
|
|
63
|
+
hincrby: async (key, field, by) => {
|
|
64
|
+
const n = Number(hash(key).get(field) ?? 0) + by;
|
|
65
|
+
hash(key).set(field, String(n));
|
|
66
|
+
changed();
|
|
67
|
+
return n;
|
|
68
|
+
},
|
|
45
69
|
sadd: async (key, member) => {
|
|
46
70
|
(sets.get(key) ?? sets.set(key, new Set()).get(key)!).add(member);
|
|
47
71
|
changed();
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
/** Which side a person is on: the studio that makes the work, or the client it is for. */
|
|
2
|
+
export declare const PERSON_KINDS: readonly ['studio', 'client'];
|
|
3
|
+
export type PersonKind = (typeof PERSON_KINDS)[number];
|
|
3
4
|
/** How a comment is triaged: something broken, a design opinion, or the words. */
|
|
4
5
|
export declare const KINDS: readonly ['bug', 'taste', 'copy'];
|
|
5
6
|
export type Kind = (typeof KINDS)[number];
|
|
6
|
-
/** How long an
|
|
7
|
-
export declare const
|
|
7
|
+
/** How long an unused sign-in link lasts. A link signs in one device, then stops working. */
|
|
8
|
+
export declare const LINK_DAYS = 14;
|
|
9
|
+
/**
|
|
10
|
+
* How long a person keeps access: three months, six months, or open-ended.
|
|
11
|
+
* The project sets a default; Studio can set it for one person.
|
|
12
|
+
*/
|
|
13
|
+
export declare const ACCESS: readonly ['3m', '6m', 'open'];
|
|
14
|
+
export type Access = (typeof ACCESS)[number];
|
|
15
|
+
/** The project default when none is set. */
|
|
16
|
+
export declare const DEFAULT_ACCESS: Access;
|
|
17
|
+
/** When access given at `from` ends, as an ISO time, or null for open-ended. */
|
|
18
|
+
export declare function accessEnd(access: Access, from: Date): string | null;
|
|
8
19
|
export declare const LIMITS: {
|
|
9
20
|
readonly name: 80;
|
|
10
21
|
readonly team: 80;
|
|
@@ -20,12 +31,21 @@ export type Person = {
|
|
|
20
31
|
name: string;
|
|
21
32
|
/** The group a person belongs to, shown beside their name: the studio, or the client's company. */
|
|
22
33
|
team: string;
|
|
23
|
-
|
|
34
|
+
kind: PersonKind;
|
|
35
|
+
/** Always null until email sign-in exists. */
|
|
36
|
+
email: string | null;
|
|
37
|
+
/** When their access ends (ISO time), or null for open-ended. Their sessions end with it. */
|
|
38
|
+
accessEndsAt: string | null;
|
|
24
39
|
createdAt: string;
|
|
25
40
|
revokedAt: string | null;
|
|
26
|
-
/** Bumped on revoke and on
|
|
41
|
+
/** Bumped on revoke and on sign out everywhere, which ends every session signed with the old one. */
|
|
27
42
|
epoch: number;
|
|
28
43
|
};
|
|
44
|
+
/** A one-time sign-in link, stored under the hash of its token and deleted when used. */
|
|
45
|
+
export type Link = {
|
|
46
|
+
person: string;
|
|
47
|
+
expiresAt: string;
|
|
48
|
+
};
|
|
29
49
|
/** A comment's author as it was when they wrote it, so a revoked person's comments still read. */
|
|
30
50
|
export type Author = Pick<Person, 'id' | 'name' | 'team'>;
|
|
31
51
|
export type Anchor = {
|
|
@@ -68,6 +88,14 @@ export type Comment = {
|
|
|
68
88
|
resolvedBy: Author | null;
|
|
69
89
|
triage: Triage | null;
|
|
70
90
|
};
|
|
91
|
+
/** Whether a stored profile (a person without revocation and epoch) is well formed. */
|
|
92
|
+
export declare function isProfile(v: unknown): v is Omit<Person, 'revokedAt' | 'epoch'>;
|
|
93
|
+
/**
|
|
94
|
+
* Whether someone may sign in at `at`: not revoked, and an access end that is
|
|
95
|
+
* open (null) or a parseable time still ahead. An access end that does not
|
|
96
|
+
* parse is no access.
|
|
97
|
+
*/
|
|
98
|
+
export declare function hasAccess(p: Pick<Person, 'revokedAt' | 'accessEndsAt'>, at: Date): boolean;
|
|
71
99
|
export declare const authorOf: (p: Person) => Author;
|
|
72
100
|
export declare const isRoot: (c: Comment) => boolean;
|
|
73
101
|
export type Checked<T> = {
|
|
@@ -77,7 +105,11 @@ export type Checked<T> = {
|
|
|
77
105
|
ok: false;
|
|
78
106
|
problems: string[];
|
|
79
107
|
};
|
|
80
|
-
export declare function
|
|
108
|
+
export declare function checkAccess(input: unknown): Checked<Access>;
|
|
109
|
+
export type PersonInput = Pick<Person, 'name' | 'team' | 'kind'> & {
|
|
110
|
+
access: Access | null;
|
|
111
|
+
};
|
|
112
|
+
export declare function checkPersonInput(input: unknown): Checked<PersonInput>;
|
|
81
113
|
export declare function checkBody(body: unknown): Checked<string>;
|
|
82
114
|
export declare function checkAnchor(input: unknown): Checked<Anchor>;
|
|
83
115
|
/** The README states a client offers for triage: trimmed, capped, de-duplicated. */
|
|
@@ -4,11 +4,33 @@
|
|
|
4
4
|
// enforced by the service, whichever adapter (Vercel, Vite dev, the CLI)
|
|
5
5
|
// calls it.
|
|
6
6
|
import { SLUG } from '../feature.js';
|
|
7
|
-
|
|
7
|
+
/** Which side a person is on: the studio that makes the work, or the client it is for. */
|
|
8
|
+
export const PERSON_KINDS = ['studio', 'client'];
|
|
8
9
|
/** How a comment is triaged: something broken, a design opinion, or the words. */
|
|
9
10
|
export const KINDS = ['bug', 'taste', 'copy'];
|
|
10
|
-
/** How long an
|
|
11
|
-
export const
|
|
11
|
+
/** How long an unused sign-in link lasts. A link signs in one device, then stops working. */
|
|
12
|
+
export const LINK_DAYS = 14;
|
|
13
|
+
/**
|
|
14
|
+
* How long a person keeps access: three months, six months, or open-ended.
|
|
15
|
+
* The project sets a default; Studio can set it for one person.
|
|
16
|
+
*/
|
|
17
|
+
export const ACCESS = ['3m', '6m', 'open'];
|
|
18
|
+
/** The project default when none is set. */
|
|
19
|
+
export const DEFAULT_ACCESS = '3m';
|
|
20
|
+
const ACCESS_MONTHS = { '3m': 3, '6m': 6 };
|
|
21
|
+
/** When access given at `from` ends, as an ISO time, or null for open-ended. */
|
|
22
|
+
export function accessEnd(access, from) {
|
|
23
|
+
if (access === 'open')
|
|
24
|
+
return null;
|
|
25
|
+
// On the same day of the month, or the target month's last day when it is shorter (30 Nov + 3m is 28 Feb).
|
|
26
|
+
const end = new Date(from.getTime());
|
|
27
|
+
const day = end.getUTCDate();
|
|
28
|
+
end.setUTCDate(1);
|
|
29
|
+
end.setUTCMonth(end.getUTCMonth() + ACCESS_MONTHS[access]);
|
|
30
|
+
const last = new Date(Date.UTC(end.getUTCFullYear(), end.getUTCMonth() + 1, 0)).getUTCDate();
|
|
31
|
+
end.setUTCDate(Math.min(day, last));
|
|
32
|
+
return end.toISOString();
|
|
33
|
+
}
|
|
12
34
|
export const LIMITS = {
|
|
13
35
|
name: 80,
|
|
14
36
|
team: 80,
|
|
@@ -19,6 +41,38 @@ export const LIMITS = {
|
|
|
19
41
|
states: 50,
|
|
20
42
|
stateName: 120,
|
|
21
43
|
};
|
|
44
|
+
const isText = (v) => typeof v === 'string';
|
|
45
|
+
const isTextOrNull = (v) => v === null || typeof v === 'string';
|
|
46
|
+
/** What a stored profile must hold to be a person; anything else is no person at all. */
|
|
47
|
+
const PROFILE_RULES = [
|
|
48
|
+
['id', (v) => isText(v) && v !== ''],
|
|
49
|
+
['name', isText],
|
|
50
|
+
['team', isText],
|
|
51
|
+
['kind', (v) => PERSON_KINDS.includes(v)],
|
|
52
|
+
['email', isTextOrNull],
|
|
53
|
+
['accessEndsAt', isTextOrNull],
|
|
54
|
+
['createdAt', isText],
|
|
55
|
+
];
|
|
56
|
+
/** Whether a stored profile (a person without revocation and epoch) is well formed. */
|
|
57
|
+
export function isProfile(v) {
|
|
58
|
+
if (!v || typeof v !== 'object' || Array.isArray(v))
|
|
59
|
+
return false;
|
|
60
|
+
const o = v;
|
|
61
|
+
return PROFILE_RULES.every(([key, ok]) => ok(o[key]));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Whether someone may sign in at `at`: not revoked, and an access end that is
|
|
65
|
+
* open (null) or a parseable time still ahead. An access end that does not
|
|
66
|
+
* parse is no access.
|
|
67
|
+
*/
|
|
68
|
+
export function hasAccess(p, at) {
|
|
69
|
+
if (p.revokedAt !== null)
|
|
70
|
+
return false;
|
|
71
|
+
if (p.accessEndsAt === null)
|
|
72
|
+
return true;
|
|
73
|
+
const end = Date.parse(p.accessEndsAt);
|
|
74
|
+
return Number.isFinite(end) && end > at.getTime();
|
|
75
|
+
}
|
|
22
76
|
export const authorOf = (p) => ({ id: p.id, name: p.name, team: p.team });
|
|
23
77
|
export const isRoot = (c) => c.thread === c.id;
|
|
24
78
|
const text = (v, path, max, problems) => {
|
|
@@ -33,15 +87,27 @@ const text = (v, path, max, problems) => {
|
|
|
33
87
|
};
|
|
34
88
|
const fraction = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 1;
|
|
35
89
|
const size = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 && v < 100_000;
|
|
90
|
+
const isAccess = (v) => ACCESS.includes(v);
|
|
91
|
+
const ACCESS_PROBLEM = `access: one of ${ACCESS.join(', ')}`;
|
|
92
|
+
export function checkAccess(input) {
|
|
93
|
+
return isAccess(input) ? { ok: true, value: input } : { ok: false, problems: [ACCESS_PROBLEM] };
|
|
94
|
+
}
|
|
36
95
|
export function checkPersonInput(input) {
|
|
37
96
|
const i = (input ?? {});
|
|
38
97
|
const problems = [];
|
|
39
98
|
const name = text(i.name, 'name', LIMITS.name, problems);
|
|
40
99
|
const team = text(i.team, 'team', LIMITS.team, problems);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
100
|
+
// Most people added are on the client's side; Studio is the deliberate choice.
|
|
101
|
+
const kind = i.kind ?? 'client';
|
|
102
|
+
if (!PERSON_KINDS.includes(kind))
|
|
103
|
+
problems.push(`kind: one of ${PERSON_KINDS.join(', ')}`);
|
|
104
|
+
// Absent, the service picks: the project default for a Client, open-ended for Studio.
|
|
105
|
+
const access = i.access ?? null;
|
|
106
|
+
if (access !== null && !isAccess(access))
|
|
107
|
+
problems.push(ACCESS_PROBLEM);
|
|
108
|
+
return problems.length
|
|
109
|
+
? { ok: false, problems }
|
|
110
|
+
: { ok: true, value: { name, team, kind: kind, access: access } };
|
|
45
111
|
}
|
|
46
112
|
export function checkBody(body) {
|
|
47
113
|
const problems = [];
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { Comment, Person } from './model.ts';
|
|
2
2
|
export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people';
|
|
3
|
-
|
|
3
|
+
/** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
|
|
4
|
+
type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
|
|
5
|
+
export declare function can(actor: Actor | null, action: Action, subject?: Comment): boolean;
|
|
6
|
+
export {};
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
export function can(actor, action, subject) {
|
|
2
2
|
if (!actor || actor.revokedAt)
|
|
3
3
|
return false;
|
|
4
|
-
const
|
|
4
|
+
const studio = actor.kind === 'studio';
|
|
5
5
|
const own = subject?.author.id === actor.id;
|
|
6
6
|
switch (action) {
|
|
7
7
|
case 'read':
|
|
8
8
|
case 'comment':
|
|
9
|
+
case 'resolve':
|
|
9
10
|
return true;
|
|
10
11
|
case 'edit':
|
|
11
12
|
return own;
|
|
12
13
|
case 'delete':
|
|
13
|
-
|
|
14
|
-
return own || editor;
|
|
14
|
+
return own || studio;
|
|
15
15
|
case 'triage':
|
|
16
16
|
case 'people':
|
|
17
|
-
return
|
|
17
|
+
return studio;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Comment, type Person, type Triage } from './model.ts';
|
|
1
|
+
import { type Access, type Comment, type Person, type Triage } from './model.ts';
|
|
2
2
|
import { type KV } from './store.ts';
|
|
3
3
|
export type ErrorCode = 'unauthorized' | 'forbidden' | 'not_found' | 'invalid';
|
|
4
4
|
export declare class ReviewError extends Error {
|
|
@@ -24,18 +24,36 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
|
|
|
24
24
|
/** The person a signed session names, if they still may sign in with it. */
|
|
25
25
|
sessionPerson(id: string, epoch: number): Promise<Person | null>;
|
|
26
26
|
listPeople(actor: Person | null): Promise<Person[]>;
|
|
27
|
-
|
|
27
|
+
/** A new person and their first link. Without an access length, a Client gets the project default and Studio is open-ended. */
|
|
28
|
+
addPerson(actor: Person | null, input: unknown): Promise<{
|
|
28
29
|
person: Person;
|
|
29
30
|
token: string;
|
|
30
31
|
}>;
|
|
31
|
-
/**
|
|
32
|
-
|
|
32
|
+
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
33
|
+
newLink(actor: Person | null, id: string): Promise<{
|
|
34
|
+
person: Person;
|
|
35
|
+
token: string;
|
|
36
|
+
}>;
|
|
37
|
+
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
38
|
+
signOutEverywhere(actor: Person | null, id: string): Promise<{
|
|
33
39
|
person: Person;
|
|
34
40
|
token: string;
|
|
35
41
|
}>;
|
|
36
42
|
revoke(actor: Person | null, id: string): Promise<Person>;
|
|
37
|
-
/**
|
|
38
|
-
|
|
43
|
+
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
44
|
+
setAccess(actor: Person | null, id: string, access: unknown): Promise<Person>;
|
|
45
|
+
/** How long a new Client keeps access unless Studio says otherwise. */
|
|
46
|
+
defaultAccess(actor: Person | null): Promise<Access>;
|
|
47
|
+
setDefaultAccess(actor: Person | null, access: unknown): Promise<Access>;
|
|
48
|
+
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
49
|
+
peekLink(token: string): Promise<Person | null>;
|
|
50
|
+
/**
|
|
51
|
+
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
52
|
+
* two requests with one token, only the one whose delete removed the link
|
|
53
|
+
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
54
|
+
* read and checked first, so the delete is the last store call.
|
|
55
|
+
*/
|
|
56
|
+
useLink(token: string): Promise<Person | null>;
|
|
39
57
|
listComments(actor: Person | null, prototype: string): Promise<Comment[]>;
|
|
40
58
|
/** Every comment on every prototype, for the CLI, which holds the store's own credentials. */
|
|
41
59
|
allComments(): Promise<Comment[]>;
|