@milesroxas/design-hub 0.8.0 → 0.9.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 +7 -2
- package/core/prototype.test.ts +83 -0
- package/core/prototype.ts +84 -0
- package/core/review/model.ts +20 -2
- package/core/review/policy.test.ts +4 -2
- package/core/review/policy.ts +4 -2
- package/core/review/service.test.ts +200 -16
- package/core/review/service.ts +91 -5
- package/core/review/store.ts +10 -1
- package/dist/core/prototype.d.ts +35 -0
- package/dist/core/prototype.js +84 -0
- package/dist/core/review/model.d.ts +13 -2
- package/dist/core/review/model.js +13 -0
- package/dist/core/review/policy.d.ts +1 -1
- package/dist/core/review/policy.js +1 -0
- package/dist/core/review/service.d.ts +24 -2
- package/dist/core/review/service.js +81 -6
- package/dist/core/review/store.d.ts +10 -1
- package/dist/core/review/store.js +10 -1
- package/dist/scripts/comments.js +4 -3
- package/dist/scripts/lib/cdp.js +6 -1
- package/dist/scripts/lib/export-smoke.d.ts +18 -0
- package/dist/scripts/lib/export-smoke.js +73 -0
- package/dist/scripts/new.js +5 -2
- package/dist/scripts/release-check.d.ts +5 -3
- package/dist/scripts/release-check.js +17 -4
- package/dist/scripts/share.d.ts +2 -2
- package/dist/server/api.js +8 -0
- package/dist/server/session.d.ts +5 -1
- package/dist/server/session.js +39 -21
- package/dist/vercel/proxy.js +44 -8
- package/dist/vite/boundary.d.ts +93 -0
- package/dist/vite/boundary.js +218 -0
- package/dist/vite/css-boundary.d.ts +39 -0
- package/dist/vite/css-boundary.js +167 -0
- package/dist/vite/kit.js +15 -6
- package/dist/vite/leaks.d.ts +37 -0
- package/dist/vite/leaks.js +331 -0
- package/dist/vite/plugin.js +2 -1
- package/dist/vite/registry.d.ts +15 -4
- package/dist/vite/registry.js +531 -27
- package/dist/vite/settle.d.ts +27 -0
- package/dist/vite/settle.js +185 -0
- package/package.json +4 -3
- package/shell/ClientViewNote.tsx +18 -0
- package/shell/Guide.tsx +17 -2
- package/shell/Index.tsx +257 -25
- package/shell/audience.test.ts +63 -0
- package/shell/audience.ts +47 -0
- package/shell/hub.ts +36 -0
- package/shell/main.tsx +39 -5
- package/shell/registry.ts +28 -7
- package/shell/registry.virtual.d.ts +12 -4
- package/shell/review/Pins.tsx +8 -6
- package/shell/review/Review.tsx +8 -4
- package/shell/review/ThreadCard.tsx +6 -14
- package/shell/review/client.ts +16 -4
- package/shell/review/review.css +37 -24
- package/shell/shell.css +50 -3
- package/shell/system.css +5 -0
- package/skills/miles-deploy/SKILL.md +17 -0
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-prototype/SKILL.md +14 -1
- package/skills/miles-review/SKILL.md +1 -1
package/core/review/service.ts
CHANGED
|
@@ -5,6 +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.ts';
|
|
8
|
+
import { isPrototypeSlug } from '../prototype.ts';
|
|
8
9
|
import {
|
|
9
10
|
type Access,
|
|
10
11
|
type Anchor,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
checkAudience,
|
|
19
20
|
checkBody,
|
|
20
21
|
checkPersonInput,
|
|
22
|
+
checkPrototypeAudience,
|
|
21
23
|
checkStates,
|
|
22
24
|
checkTriage,
|
|
23
25
|
checkViewAs,
|
|
@@ -28,6 +30,7 @@ import {
|
|
|
28
30
|
LINK_DAYS,
|
|
29
31
|
type Link,
|
|
30
32
|
type Person,
|
|
33
|
+
type PersonKind,
|
|
31
34
|
readComment,
|
|
32
35
|
type Triage,
|
|
33
36
|
} from './model.ts';
|
|
@@ -188,6 +191,24 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
188
191
|
|
|
189
192
|
// Every comment read goes through readComment, so a record with no audience reads as internal.
|
|
190
193
|
// Unfiltered: nothing below hands these out without visibleTo or seen.
|
|
194
|
+
/**
|
|
195
|
+
* The next thread number for an audience on a prototype. Atomic and only raised: two threads at
|
|
196
|
+
* once get two numbers, and a failed save skips one. Client-facing threads keep the field
|
|
197
|
+
* numbers always had. Internal threads count in their own, which starts past any internal
|
|
198
|
+
* thread numbered before the two counted apart, so no internal number is given twice.
|
|
199
|
+
*/
|
|
200
|
+
const nextNumber = async (prototype: string, audience: Audience): Promise<number> => {
|
|
201
|
+
if (audience === 'client') return kv.hincrby(KEY.counters, prototype, 1);
|
|
202
|
+
const field = `${prototype}:internal`;
|
|
203
|
+
const n = await kv.hincrby(KEY.counters, field, 1);
|
|
204
|
+
if (n !== 1) return n;
|
|
205
|
+
const before = Math.max(
|
|
206
|
+
0,
|
|
207
|
+
...(await commentsOf(prototype)).filter((c) => c.audience === 'internal').map((c) => c.number),
|
|
208
|
+
);
|
|
209
|
+
return before ? kv.hincrby(KEY.counters, field, before) : n;
|
|
210
|
+
};
|
|
211
|
+
|
|
191
212
|
const commentsOf = async (prototype: string) =>
|
|
192
213
|
Object.values(await kv.hgetall(KEY.comments(prototype)))
|
|
193
214
|
.map(readComment)
|
|
@@ -201,6 +222,17 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
201
222
|
const saveComment = (prototype: string, c: Comment) =>
|
|
202
223
|
kv.hset(KEY.comments(prototype), c.id, JSON.stringify(c));
|
|
203
224
|
|
|
225
|
+
const isClientFacing = async (prototype: string) =>
|
|
226
|
+
isPrototypeSlug(prototype) && (await kv.hget(KEY.audiences, prototype)) === 'client';
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Whether a view reaches a prototype's comments at all. A Client's view
|
|
230
|
+
* reaches only a client-facing prototype: on any other, every op answers
|
|
231
|
+
* as if it had no comments, so no answer confirms an internal slug.
|
|
232
|
+
*/
|
|
233
|
+
const reachable = async (view: PersonKind, prototype: string) =>
|
|
234
|
+
view === 'studio' || (await isClientFacing(prototype));
|
|
235
|
+
|
|
204
236
|
/**
|
|
205
237
|
* A comment and its thread's root, if `actor` may see both; else null. A
|
|
206
238
|
* reply whose root is gone is not there either.
|
|
@@ -210,6 +242,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
210
242
|
prototype: string,
|
|
211
243
|
id: string,
|
|
212
244
|
): Promise<{ comment: Comment; root: Comment } | null> {
|
|
245
|
+
if (!(await reachable(viewOf(actor), prototype))) return null;
|
|
213
246
|
const comment = await commentAt(prototype, id);
|
|
214
247
|
if (!comment) return null;
|
|
215
248
|
const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
|
|
@@ -364,20 +397,23 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
364
397
|
async listComments(actor: Person | null, prototype: string, viewAs?: unknown): Promise<Comment[]> {
|
|
365
398
|
allow(actor, 'read');
|
|
366
399
|
const view = viewOf(actor, valid(checkViewAs(viewAs)));
|
|
400
|
+
if (!(await reachable(view, prototype))) return [];
|
|
367
401
|
return visibleTo(view, await commentsOf(prototype));
|
|
368
402
|
},
|
|
369
403
|
|
|
370
404
|
/** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
|
|
371
405
|
async allComments(actor: Person | null): Promise<Comment[]> {
|
|
372
406
|
allow(actor, 'read');
|
|
373
|
-
const
|
|
374
|
-
|
|
407
|
+
const view = viewOf(actor);
|
|
408
|
+
const prototypes = [];
|
|
409
|
+
for (const p of await kv.smembers(KEY.prototypes)) if (await reachable(view, p)) prototypes.push(p);
|
|
410
|
+
return visibleTo(view, (await Promise.all(prototypes.map(commentsOf))).flat());
|
|
375
411
|
},
|
|
376
412
|
|
|
377
413
|
/**
|
|
378
414
|
* A new thread. A Client's is client-facing whatever the input says;
|
|
379
415
|
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
380
|
-
* prototype
|
|
416
|
+
* next of its audience on the prototype, never one used before.
|
|
381
417
|
*/
|
|
382
418
|
async add(
|
|
383
419
|
actor: Person | null,
|
|
@@ -389,6 +425,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
389
425
|
checkAnchor(input.anchor),
|
|
390
426
|
checkAudience(input.audience),
|
|
391
427
|
);
|
|
428
|
+
// A Client's thread on a prototype that is not client-facing: the answer an unknown id gets.
|
|
429
|
+
if (!(await reachable(viewOf(actor), anchor.prototype)))
|
|
430
|
+
throw new ReviewError('not_found', 'No such comment.');
|
|
392
431
|
const id = newId();
|
|
393
432
|
const triage = deps.triage
|
|
394
433
|
? await deps
|
|
@@ -408,9 +447,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
408
447
|
resolvedBy: null,
|
|
409
448
|
triage,
|
|
410
449
|
audience: audienceFor(actor, asked),
|
|
411
|
-
|
|
412
|
-
number: await kv.hincrby(KEY.counters, anchor.prototype, 1),
|
|
450
|
+
number: 0,
|
|
413
451
|
};
|
|
452
|
+
c.number = await nextNumber(anchor.prototype, c.audience);
|
|
414
453
|
await kv.sadd(KEY.prototypes, anchor.prototype);
|
|
415
454
|
await saveComment(anchor.prototype, c);
|
|
416
455
|
return c;
|
|
@@ -490,6 +529,53 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
490
529
|
return next;
|
|
491
530
|
},
|
|
492
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Marks a prototype client-facing or internal. Only client-facing is
|
|
534
|
+
* stored, so internal deletes the field and a prototype nobody marked is
|
|
535
|
+
* internal. Studio only.
|
|
536
|
+
*/
|
|
537
|
+
async setAudience(actor: Person | null, prototype: unknown, audience: unknown): Promise<Audience> {
|
|
538
|
+
allow(actor, 'audience');
|
|
539
|
+
const [slug, value] = validAll<[string, Audience]>(
|
|
540
|
+
isPrototypeSlug(prototype)
|
|
541
|
+
? { ok: true, value: prototype }
|
|
542
|
+
: { ok: false, problems: ['prototype: a prototype slug'] },
|
|
543
|
+
checkPrototypeAudience(audience),
|
|
544
|
+
);
|
|
545
|
+
if (value === 'client') await kv.hset(KEY.audiences, slug, 'client');
|
|
546
|
+
else await kv.hdel(KEY.audiences, slug);
|
|
547
|
+
return value;
|
|
548
|
+
},
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Who a prototype is for, asked by the gate with no actor. Absent, or any
|
|
552
|
+
* value but `client`, is internal; a name that cannot be a prototype is
|
|
553
|
+
* internal without asking the store.
|
|
554
|
+
*/
|
|
555
|
+
async audienceOf(prototype: string): Promise<Audience> {
|
|
556
|
+
return (await isClientFacing(prototype)) ? 'client' : 'internal';
|
|
557
|
+
},
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* What the index is built from: the view the actor gets (Client view for
|
|
561
|
+
* Studio with `viewAs: 'client'`) and every client-facing slug. The
|
|
562
|
+
* server keeps no list of internal ones: Studio's view reads the build's
|
|
563
|
+
* own list, which the gate refuses to a Client.
|
|
564
|
+
*/
|
|
565
|
+
async visiblePrototypes(
|
|
566
|
+
actor: Person | null,
|
|
567
|
+
viewAs?: unknown,
|
|
568
|
+
): Promise<{ view: PersonKind; clientFacing: string[] }> {
|
|
569
|
+
allow(actor, 'read');
|
|
570
|
+
const view = viewOf(actor, valid(checkViewAs(viewAs)));
|
|
571
|
+
const all = await kv.hgetall(KEY.audiences);
|
|
572
|
+
const clientFacing = Object.entries(all)
|
|
573
|
+
.filter(([slug, value]) => value === 'client' && isPrototypeSlug(slug))
|
|
574
|
+
.map(([slug]) => slug)
|
|
575
|
+
.sort();
|
|
576
|
+
return { view, clientFacing };
|
|
577
|
+
},
|
|
578
|
+
|
|
493
579
|
/** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
|
|
494
580
|
async locate(actor: Person | null, id: string): Promise<{ prototype: string; comment: Comment } | null> {
|
|
495
581
|
allow(actor, 'read');
|
package/core/review/store.ts
CHANGED
|
@@ -34,7 +34,16 @@ export const KEY = {
|
|
|
34
34
|
/** Project settings, one field each: `access` is the default access length. */
|
|
35
35
|
settings: `${PREFIX}settings`,
|
|
36
36
|
prototypes: `${PREFIX}prototypes`,
|
|
37
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* Which prototypes are client-facing: field `<slug>`, value `client`. Only
|
|
39
|
+
* client-facing is stored; internal is the field's absence, and any other
|
|
40
|
+
* value reads as internal too.
|
|
41
|
+
*/
|
|
42
|
+
audiences: `${PREFIX}audiences`,
|
|
43
|
+
/**
|
|
44
|
+
* The last thread number given on each prototype, only ever raised by HINCRBY: field
|
|
45
|
+
* `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
|
|
46
|
+
*/
|
|
38
47
|
counters: `${PREFIX}counters`,
|
|
39
48
|
comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
|
|
40
49
|
} as const;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First path segments the hub owns, never a prototype: the walkthroughs,
|
|
3
|
+
* the review API, and the shared bundle.
|
|
4
|
+
*/
|
|
5
|
+
export declare const RESERVED: readonly ['guide', 'api', 'assets'];
|
|
6
|
+
/**
|
|
7
|
+
* Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
|
|
8
|
+
* Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
|
|
9
|
+
* has no underscore.
|
|
10
|
+
*/
|
|
11
|
+
export declare const PROTOTYPE_DIR = "_p";
|
|
12
|
+
export declare const ALL_FILE = "_p/all.js";
|
|
13
|
+
export declare const entryFile: (slug: string) => string;
|
|
14
|
+
/** Whether `name` can be a prototype's folder: kebab-case and not one of the hub's own paths. */
|
|
15
|
+
export declare const isPrototypeSlug: (name: unknown) => name is string;
|
|
16
|
+
/**
|
|
17
|
+
* Which prototype a request path belongs to, for the gate: the name a
|
|
18
|
+
* prototype path carries, or null for a hub path (`/`, or a first segment in
|
|
19
|
+
* HUB_ROOT). The first segment, decoded, must be one of those, `_p`, or a
|
|
20
|
+
* prototype slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`)
|
|
21
|
+
* belongs to `''`, refused like an internal prototype. A path under `/_p/`
|
|
22
|
+
* always belongs to one, so its name may be no slug at all (`all.js`), which
|
|
23
|
+
* no audience makes client-facing. A path that could mean more than one file
|
|
24
|
+
* belongs to `''` too.
|
|
25
|
+
*/
|
|
26
|
+
export declare function prototypeOfPath(pathname: string): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Whether a built prototype's entry is there for this person, asked with a
|
|
29
|
+
* HEAD before it is imported. Not there: a 404 (the gate's answer for a
|
|
30
|
+
* prototype this person may not open, or none at all), or a 200 that is not
|
|
31
|
+
* JavaScript (the host's page for an entry that was never built). Any other
|
|
32
|
+
* answer (401, 500, 503) is an error, never "not there", so an outage never
|
|
33
|
+
* reads as nothing shared.
|
|
34
|
+
*/
|
|
35
|
+
export declare function probeEntry(path: string, fetcher: typeof fetch): Promise<boolean>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// What a prototype's slug may be, and where the files built from it live.
|
|
2
|
+
// One rule for the registry's scan, `design-hub new` and the gate, so a
|
|
3
|
+
// folder the hub lists is always a path the gate knows how to refuse.
|
|
4
|
+
import { SLUG } from './feature.js';
|
|
5
|
+
/**
|
|
6
|
+
* First path segments the hub owns, never a prototype: the walkthroughs,
|
|
7
|
+
* the review API, and the shared bundle.
|
|
8
|
+
*/
|
|
9
|
+
export const RESERVED = ['guide', 'api', 'assets'];
|
|
10
|
+
/**
|
|
11
|
+
* Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
|
|
12
|
+
* Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
|
|
13
|
+
* has no underscore.
|
|
14
|
+
*/
|
|
15
|
+
export const PROTOTYPE_DIR = '_p';
|
|
16
|
+
export const ALL_FILE = `${PROTOTYPE_DIR}/all.js`;
|
|
17
|
+
export const entryFile = (slug) => `${PROTOTYPE_DIR}/${slug}/entry.js`;
|
|
18
|
+
/** Whether `name` can be a prototype's folder: kebab-case and not one of the hub's own paths. */
|
|
19
|
+
export const isPrototypeSlug = (name) => typeof name === 'string' && SLUG.test(name) && !RESERVED.includes(name);
|
|
20
|
+
/**
|
|
21
|
+
* A path's segments as a file server might read them, or null when the path
|
|
22
|
+
* could mean more than one file: it does not decode, still holds a `%` after
|
|
23
|
+
* decoding (`%255F`), holds a backslash, a `.` or `..` segment, or an empty
|
|
24
|
+
* segment other than the last. The gate refuses those to a Client rather than
|
|
25
|
+
* guess how the server resolves them. An encoded space or letter decodes and passes.
|
|
26
|
+
*/
|
|
27
|
+
function segments(pathname) {
|
|
28
|
+
let decoded;
|
|
29
|
+
try {
|
|
30
|
+
decoded = decodeURIComponent(pathname);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (/[%\\]/.test(decoded))
|
|
36
|
+
return null;
|
|
37
|
+
const parts = decoded.split('/').slice(1);
|
|
38
|
+
if (parts.at(-1) === '')
|
|
39
|
+
parts.pop();
|
|
40
|
+
return parts.some((p) => p === '' || p === '.' || p === '..') ? null : parts;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The first path segments that are the hub's own, for anyone signed in: the
|
|
44
|
+
* reserved routes, the gate's font folder, and the files the hub serves at
|
|
45
|
+
* the top (shell/index.html, shell/public/favicon.svg).
|
|
46
|
+
*/
|
|
47
|
+
const HUB_ROOT = [...RESERVED, '_gate', 'index.html', 'favicon.svg'];
|
|
48
|
+
/**
|
|
49
|
+
* Which prototype a request path belongs to, for the gate: the name a
|
|
50
|
+
* prototype path carries, or null for a hub path (`/`, or a first segment in
|
|
51
|
+
* HUB_ROOT). The first segment, decoded, must be one of those, `_p`, or a
|
|
52
|
+
* prototype slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`)
|
|
53
|
+
* belongs to `''`, refused like an internal prototype. A path under `/_p/`
|
|
54
|
+
* always belongs to one, so its name may be no slug at all (`all.js`), which
|
|
55
|
+
* no audience makes client-facing. A path that could mean more than one file
|
|
56
|
+
* belongs to `''` too.
|
|
57
|
+
*/
|
|
58
|
+
export function prototypeOfPath(pathname) {
|
|
59
|
+
const parts = segments(pathname);
|
|
60
|
+
if (!parts)
|
|
61
|
+
return '';
|
|
62
|
+
const [first, second] = parts;
|
|
63
|
+
if (first === undefined || HUB_ROOT.includes(first))
|
|
64
|
+
return null;
|
|
65
|
+
if (first === PROTOTYPE_DIR)
|
|
66
|
+
return second ?? '';
|
|
67
|
+
return isPrototypeSlug(first) ? first : '';
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether a built prototype's entry is there for this person, asked with a
|
|
71
|
+
* HEAD before it is imported. Not there: a 404 (the gate's answer for a
|
|
72
|
+
* prototype this person may not open, or none at all), or a 200 that is not
|
|
73
|
+
* JavaScript (the host's page for an entry that was never built). Any other
|
|
74
|
+
* answer (401, 500, 503) is an error, never "not there", so an outage never
|
|
75
|
+
* reads as nothing shared.
|
|
76
|
+
*/
|
|
77
|
+
export async function probeEntry(path, fetcher) {
|
|
78
|
+
const head = await fetcher(path, { method: 'HEAD', credentials: 'same-origin' });
|
|
79
|
+
if (head.status === 404)
|
|
80
|
+
return false;
|
|
81
|
+
if (head.status !== 200)
|
|
82
|
+
throw new Error(`${path} answered ${head.status}.`);
|
|
83
|
+
return /javascript/.test(head.headers.get('content-type') ?? '');
|
|
84
|
+
}
|
|
@@ -96,11 +96,20 @@ export type Comment = {
|
|
|
96
96
|
/** The thread's audience. Set on the root when it is made and copied onto every reply; never changed. */
|
|
97
97
|
audience: Audience;
|
|
98
98
|
/**
|
|
99
|
-
* The thread's number on its prototype, from 1, copied onto every reply.
|
|
100
|
-
*
|
|
99
|
+
* The thread's number on its prototype and audience, from 1, copied onto every reply.
|
|
100
|
+
* Internal and client-facing threads count apart, so a Client's numbers run 1, 2, 3 with no
|
|
101
|
+
* gap where an internal thread sits; `threadLabel` tells the two apart. Never reused, so a
|
|
102
|
+
* deleted thread moves no other number. 0 for a thread stored before numbers existed.
|
|
101
103
|
*/
|
|
102
104
|
number: number;
|
|
103
105
|
};
|
|
106
|
+
/**
|
|
107
|
+
* How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
|
|
108
|
+
* `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
|
|
109
|
+
* and `comments pull` use it, so "fix I2" names one thread. The pin and the card's badge show
|
|
110
|
+
* the bare number; their amber says internal.
|
|
111
|
+
*/
|
|
112
|
+
export declare const threadLabel: (c: Pick<Comment, 'audience' | 'number'>) => string;
|
|
104
113
|
/**
|
|
105
114
|
* A stored comment as the rules read it. A record with no audience, or one
|
|
106
115
|
* that is not a known value, is internal: a missing field never shows a
|
|
@@ -131,6 +140,8 @@ export type PersonInput = Pick<Person, 'name' | 'team' | 'kind'> & {
|
|
|
131
140
|
export declare function checkPersonInput(input: unknown): Checked<PersonInput>;
|
|
132
141
|
/** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
|
|
133
142
|
export declare function checkAudience(input: unknown): Checked<Audience | null>;
|
|
143
|
+
/** A prototype's audience as Studio sets it: one of AUDIENCES, required. */
|
|
144
|
+
export declare function checkPrototypeAudience(input: unknown): Checked<Audience>;
|
|
134
145
|
/** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
|
|
135
146
|
export declare function checkViewAs(input: unknown): Checked<PersonKind | null>;
|
|
136
147
|
export declare function checkBody(body: unknown): Checked<string>;
|
|
@@ -46,6 +46,13 @@ export const LIMITS = {
|
|
|
46
46
|
states: 50,
|
|
47
47
|
stateName: 120,
|
|
48
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
|
|
51
|
+
* `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
|
|
52
|
+
* and `comments pull` use it, so "fix I2" names one thread. The pin and the card's badge show
|
|
53
|
+
* the bare number; their amber says internal.
|
|
54
|
+
*/
|
|
55
|
+
export const threadLabel = (c) => !c.number ? '' : c.audience === 'internal' ? `I${c.number}` : String(c.number);
|
|
49
56
|
/**
|
|
50
57
|
* A stored comment as the rules read it. A record with no audience, or one
|
|
51
58
|
* that is not a known value, is internal: a missing field never shows a
|
|
@@ -133,6 +140,12 @@ export function checkAudience(input) {
|
|
|
133
140
|
? { ok: true, value: input }
|
|
134
141
|
: { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
|
|
135
142
|
}
|
|
143
|
+
/** A prototype's audience as Studio sets it: one of AUDIENCES, required. */
|
|
144
|
+
export function checkPrototypeAudience(input) {
|
|
145
|
+
return AUDIENCES.includes(input)
|
|
146
|
+
? { ok: true, value: input }
|
|
147
|
+
: { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
|
|
148
|
+
}
|
|
136
149
|
/** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
|
|
137
150
|
export function checkViewAs(input) {
|
|
138
151
|
if (input === undefined || input === null)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Audience, Comment, Person, PersonKind } from './model.ts';
|
|
2
|
-
export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people';
|
|
2
|
+
export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people' | 'audience';
|
|
3
3
|
/** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
|
|
4
4
|
type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
|
|
5
5
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Access, type Comment, type Person, type Triage } from './model.ts';
|
|
1
|
+
import { type Access, type Audience, type Comment, type Person, type PersonKind, 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 {
|
|
@@ -64,7 +64,7 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
|
|
|
64
64
|
/**
|
|
65
65
|
* A new thread. A Client's is client-facing whatever the input says;
|
|
66
66
|
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
67
|
-
* prototype
|
|
67
|
+
* next of its audience on the prototype, never one used before.
|
|
68
68
|
*/
|
|
69
69
|
add(actor: Person | null, input: {
|
|
70
70
|
body?: unknown;
|
|
@@ -98,6 +98,28 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
|
|
|
98
98
|
triage?: unknown;
|
|
99
99
|
states?: unknown;
|
|
100
100
|
}): Promise<Comment>;
|
|
101
|
+
/**
|
|
102
|
+
* Marks a prototype client-facing or internal. Only client-facing is
|
|
103
|
+
* stored, so internal deletes the field and a prototype nobody marked is
|
|
104
|
+
* internal. Studio only.
|
|
105
|
+
*/
|
|
106
|
+
setAudience(actor: Person | null, prototype: unknown, audience: unknown): Promise<Audience>;
|
|
107
|
+
/**
|
|
108
|
+
* Who a prototype is for, asked by the gate with no actor. Absent, or any
|
|
109
|
+
* value but `client`, is internal; a name that cannot be a prototype is
|
|
110
|
+
* internal without asking the store.
|
|
111
|
+
*/
|
|
112
|
+
audienceOf(prototype: string): Promise<Audience>;
|
|
113
|
+
/**
|
|
114
|
+
* What the index is built from: the view the actor gets (Client view for
|
|
115
|
+
* Studio with `viewAs: 'client'`) and every client-facing slug. The
|
|
116
|
+
* server keeps no list of internal ones: Studio's view reads the build's
|
|
117
|
+
* own list, which the gate refuses to a Client.
|
|
118
|
+
*/
|
|
119
|
+
visiblePrototypes(actor: Person | null, viewAs?: unknown): Promise<{
|
|
120
|
+
view: PersonKind;
|
|
121
|
+
clientFacing: string[];
|
|
122
|
+
}>;
|
|
101
123
|
/** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
|
|
102
124
|
locate(actor: Person | null, id: string): Promise<{
|
|
103
125
|
prototype: string;
|
|
@@ -5,7 +5,8 @@
|
|
|
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 {
|
|
8
|
+
import { isPrototypeSlug } from '../prototype.js';
|
|
9
|
+
import { accessEnd, authorOf, checkAccess, checkAnchor, checkAudience, checkBody, checkPersonInput, checkPrototypeAudience, checkStates, checkTriage, checkViewAs, DEFAULT_ACCESS, hasAccess, isProfile, isRoot, LINK_DAYS, readComment, } from './model.js';
|
|
9
10
|
import { audienceFor, can, viewOf, visibleTo } from './policy.js';
|
|
10
11
|
import { KEY } from './store.js';
|
|
11
12
|
export class ReviewError extends Error {
|
|
@@ -135,6 +136,22 @@ export function createReview(kv, overrides = {}) {
|
|
|
135
136
|
}
|
|
136
137
|
// Every comment read goes through readComment, so a record with no audience reads as internal.
|
|
137
138
|
// Unfiltered: nothing below hands these out without visibleTo or seen.
|
|
139
|
+
/**
|
|
140
|
+
* The next thread number for an audience on a prototype. Atomic and only raised: two threads at
|
|
141
|
+
* once get two numbers, and a failed save skips one. Client-facing threads keep the field
|
|
142
|
+
* numbers always had. Internal threads count in their own, which starts past any internal
|
|
143
|
+
* thread numbered before the two counted apart, so no internal number is given twice.
|
|
144
|
+
*/
|
|
145
|
+
const nextNumber = async (prototype, audience) => {
|
|
146
|
+
if (audience === 'client')
|
|
147
|
+
return kv.hincrby(KEY.counters, prototype, 1);
|
|
148
|
+
const field = `${prototype}:internal`;
|
|
149
|
+
const n = await kv.hincrby(KEY.counters, field, 1);
|
|
150
|
+
if (n !== 1)
|
|
151
|
+
return n;
|
|
152
|
+
const before = Math.max(0, ...(await commentsOf(prototype)).filter((c) => c.audience === 'internal').map((c) => c.number));
|
|
153
|
+
return before ? kv.hincrby(KEY.counters, field, before) : n;
|
|
154
|
+
};
|
|
138
155
|
const commentsOf = async (prototype) => Object.values(await kv.hgetall(KEY.comments(prototype)))
|
|
139
156
|
.map(readComment)
|
|
140
157
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
@@ -143,11 +160,20 @@ export function createReview(kv, overrides = {}) {
|
|
|
143
160
|
return json === null ? null : readComment(json);
|
|
144
161
|
};
|
|
145
162
|
const saveComment = (prototype, c) => kv.hset(KEY.comments(prototype), c.id, JSON.stringify(c));
|
|
163
|
+
const isClientFacing = async (prototype) => isPrototypeSlug(prototype) && (await kv.hget(KEY.audiences, prototype)) === 'client';
|
|
164
|
+
/**
|
|
165
|
+
* Whether a view reaches a prototype's comments at all. A Client's view
|
|
166
|
+
* reaches only a client-facing prototype: on any other, every op answers
|
|
167
|
+
* as if it had no comments, so no answer confirms an internal slug.
|
|
168
|
+
*/
|
|
169
|
+
const reachable = async (view, prototype) => view === 'studio' || (await isClientFacing(prototype));
|
|
146
170
|
/**
|
|
147
171
|
* A comment and its thread's root, if `actor` may see both; else null. A
|
|
148
172
|
* reply whose root is gone is not there either.
|
|
149
173
|
*/
|
|
150
174
|
async function seen(actor, prototype, id) {
|
|
175
|
+
if (!(await reachable(viewOf(actor), prototype)))
|
|
176
|
+
return null;
|
|
151
177
|
const comment = await commentAt(prototype, id);
|
|
152
178
|
if (!comment)
|
|
153
179
|
return null;
|
|
@@ -291,22 +317,31 @@ export function createReview(kv, overrides = {}) {
|
|
|
291
317
|
async listComments(actor, prototype, viewAs) {
|
|
292
318
|
allow(actor, 'read');
|
|
293
319
|
const view = viewOf(actor, valid(checkViewAs(viewAs)));
|
|
320
|
+
if (!(await reachable(view, prototype)))
|
|
321
|
+
return [];
|
|
294
322
|
return visibleTo(view, await commentsOf(prototype));
|
|
295
323
|
},
|
|
296
324
|
/** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
|
|
297
325
|
async allComments(actor) {
|
|
298
326
|
allow(actor, 'read');
|
|
299
|
-
const
|
|
300
|
-
|
|
327
|
+
const view = viewOf(actor);
|
|
328
|
+
const prototypes = [];
|
|
329
|
+
for (const p of await kv.smembers(KEY.prototypes))
|
|
330
|
+
if (await reachable(view, p))
|
|
331
|
+
prototypes.push(p);
|
|
332
|
+
return visibleTo(view, (await Promise.all(prototypes.map(commentsOf))).flat());
|
|
301
333
|
},
|
|
302
334
|
/**
|
|
303
335
|
* A new thread. A Client's is client-facing whatever the input says;
|
|
304
336
|
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
305
|
-
* prototype
|
|
337
|
+
* next of its audience on the prototype, never one used before.
|
|
306
338
|
*/
|
|
307
339
|
async add(actor, input) {
|
|
308
340
|
allow(actor, 'comment');
|
|
309
341
|
const [body, anchor, asked] = validAll(checkBody(input.body), checkAnchor(input.anchor), checkAudience(input.audience));
|
|
342
|
+
// A Client's thread on a prototype that is not client-facing: the answer an unknown id gets.
|
|
343
|
+
if (!(await reachable(viewOf(actor), anchor.prototype)))
|
|
344
|
+
throw new ReviewError('not_found', 'No such comment.');
|
|
310
345
|
const id = newId();
|
|
311
346
|
const triage = deps.triage
|
|
312
347
|
? await deps
|
|
@@ -326,9 +361,9 @@ export function createReview(kv, overrides = {}) {
|
|
|
326
361
|
resolvedBy: null,
|
|
327
362
|
triage,
|
|
328
363
|
audience: audienceFor(actor, asked),
|
|
329
|
-
|
|
330
|
-
number: await kv.hincrby(KEY.counters, anchor.prototype, 1),
|
|
364
|
+
number: 0,
|
|
331
365
|
};
|
|
366
|
+
c.number = await nextNumber(anchor.prototype, c.audience);
|
|
332
367
|
await kv.sadd(KEY.prototypes, anchor.prototype);
|
|
333
368
|
await saveComment(anchor.prototype, c);
|
|
334
369
|
return c;
|
|
@@ -391,6 +426,46 @@ export function createReview(kv, overrides = {}) {
|
|
|
391
426
|
await saveComment(input.prototype, next);
|
|
392
427
|
return next;
|
|
393
428
|
},
|
|
429
|
+
/**
|
|
430
|
+
* Marks a prototype client-facing or internal. Only client-facing is
|
|
431
|
+
* stored, so internal deletes the field and a prototype nobody marked is
|
|
432
|
+
* internal. Studio only.
|
|
433
|
+
*/
|
|
434
|
+
async setAudience(actor, prototype, audience) {
|
|
435
|
+
allow(actor, 'audience');
|
|
436
|
+
const [slug, value] = validAll(isPrototypeSlug(prototype)
|
|
437
|
+
? { ok: true, value: prototype }
|
|
438
|
+
: { ok: false, problems: ['prototype: a prototype slug'] }, checkPrototypeAudience(audience));
|
|
439
|
+
if (value === 'client')
|
|
440
|
+
await kv.hset(KEY.audiences, slug, 'client');
|
|
441
|
+
else
|
|
442
|
+
await kv.hdel(KEY.audiences, slug);
|
|
443
|
+
return value;
|
|
444
|
+
},
|
|
445
|
+
/**
|
|
446
|
+
* Who a prototype is for, asked by the gate with no actor. Absent, or any
|
|
447
|
+
* value but `client`, is internal; a name that cannot be a prototype is
|
|
448
|
+
* internal without asking the store.
|
|
449
|
+
*/
|
|
450
|
+
async audienceOf(prototype) {
|
|
451
|
+
return (await isClientFacing(prototype)) ? 'client' : 'internal';
|
|
452
|
+
},
|
|
453
|
+
/**
|
|
454
|
+
* What the index is built from: the view the actor gets (Client view for
|
|
455
|
+
* Studio with `viewAs: 'client'`) and every client-facing slug. The
|
|
456
|
+
* server keeps no list of internal ones: Studio's view reads the build's
|
|
457
|
+
* own list, which the gate refuses to a Client.
|
|
458
|
+
*/
|
|
459
|
+
async visiblePrototypes(actor, viewAs) {
|
|
460
|
+
allow(actor, 'read');
|
|
461
|
+
const view = viewOf(actor, valid(checkViewAs(viewAs)));
|
|
462
|
+
const all = await kv.hgetall(KEY.audiences);
|
|
463
|
+
const clientFacing = Object.entries(all)
|
|
464
|
+
.filter(([slug, value]) => value === 'client' && isPrototypeSlug(slug))
|
|
465
|
+
.map(([slug]) => slug)
|
|
466
|
+
.sort();
|
|
467
|
+
return { view, clientFacing };
|
|
468
|
+
},
|
|
394
469
|
/** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
|
|
395
470
|
async locate(actor, id) {
|
|
396
471
|
allow(actor, 'read');
|
|
@@ -21,7 +21,16 @@ export declare const KEY: {
|
|
|
21
21
|
/** Project settings, one field each: `access` is the default access length. */
|
|
22
22
|
readonly settings: "hub3:settings";
|
|
23
23
|
readonly prototypes: "hub3:prototypes";
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Which prototypes are client-facing: field `<slug>`, value `client`. Only
|
|
26
|
+
* client-facing is stored; internal is the field's absence, and any other
|
|
27
|
+
* value reads as internal too.
|
|
28
|
+
*/
|
|
29
|
+
readonly audiences: "hub3:audiences";
|
|
30
|
+
/**
|
|
31
|
+
* The last thread number given on each prototype, only ever raised by HINCRBY: field
|
|
32
|
+
* `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
|
|
33
|
+
*/
|
|
25
34
|
readonly counters: "hub3:counters";
|
|
26
35
|
readonly comments: (prototype: string) => string;
|
|
27
36
|
};
|
|
@@ -20,7 +20,16 @@ export const KEY = {
|
|
|
20
20
|
/** Project settings, one field each: `access` is the default access length. */
|
|
21
21
|
settings: `${PREFIX}settings`,
|
|
22
22
|
prototypes: `${PREFIX}prototypes`,
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Which prototypes are client-facing: field `<slug>`, value `client`. Only
|
|
25
|
+
* client-facing is stored; internal is the field's absence, and any other
|
|
26
|
+
* value reads as internal too.
|
|
27
|
+
*/
|
|
28
|
+
audiences: `${PREFIX}audiences`,
|
|
29
|
+
/**
|
|
30
|
+
* The last thread number given on each prototype, only ever raised by HINCRBY: field
|
|
31
|
+
* `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
|
|
32
|
+
*/
|
|
24
33
|
counters: `${PREFIX}counters`,
|
|
25
34
|
comments: (prototype) => `${PREFIX}comments:${prototype}`,
|
|
26
35
|
};
|
package/dist/scripts/comments.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
7
7
|
import { join, relative } from 'node:path';
|
|
8
8
|
import { featureTitle } from '../core/feature.js';
|
|
9
|
-
import { KINDS } from '../core/review/model.js';
|
|
9
|
+
import { KINDS, threadLabel } from '../core/review/model.js';
|
|
10
10
|
import { jevTriager } from '../core/review/triage.js';
|
|
11
11
|
import { checks } from '../server/checks.js';
|
|
12
12
|
import { readEnv } from '../server/env.js';
|
|
@@ -47,8 +47,9 @@ function threadLines(root, comments) {
|
|
|
47
47
|
: '';
|
|
48
48
|
const resolved = root.status === 'resolved' ? ' Resolved.' : '';
|
|
49
49
|
const replies = comments.filter((c) => c.thread === root.id && !isRoot(c));
|
|
50
|
-
// The
|
|
51
|
-
const
|
|
50
|
+
// The label the panel shows (I2 internal, 2 client-facing, none before numbers), then who it is for.
|
|
51
|
+
const label = threadLabel(root);
|
|
52
|
+
const head = `${label ? `#${label} ` : ''}(${AUDIENCE_WORD[root.audience]})`;
|
|
52
53
|
return [
|
|
53
54
|
`- ${head} ${line(root)}${where}${resolved} id \`${root.id}\``,
|
|
54
55
|
...replies.map((r) => ` - ${line(r)}`),
|