@milesroxas/design-hub 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/README.md +13 -11
  2. package/core/files.test.ts +76 -0
  3. package/core/files.ts +131 -0
  4. package/core/prototype.test.ts +124 -7
  5. package/core/prototype.ts +133 -24
  6. package/core/review/model.ts +119 -2
  7. package/core/review/policy.test.ts +79 -22
  8. package/core/review/policy.ts +36 -8
  9. package/core/review/service.test.ts +1464 -340
  10. package/core/review/service.ts +777 -191
  11. package/core/review/store.test.ts +91 -0
  12. package/core/review/store.ts +143 -33
  13. package/dist/core/files.d.ts +39 -0
  14. package/dist/core/files.js +107 -0
  15. package/dist/core/prototype.d.ts +61 -12
  16. package/dist/core/prototype.js +105 -26
  17. package/dist/core/review/model.d.ts +76 -2
  18. package/dist/core/review/model.js +76 -1
  19. package/dist/core/review/policy.d.ts +11 -4
  20. package/dist/core/review/policy.js +14 -2
  21. package/dist/core/review/service.d.ts +197 -45
  22. package/dist/core/review/service.js +702 -192
  23. package/dist/core/review/store.d.ts +84 -31
  24. package/dist/core/review/store.js +66 -33
  25. package/dist/scripts/bootstrap.d.ts +4 -34
  26. package/dist/scripts/bootstrap.js +9 -244
  27. package/dist/scripts/cli.js +6 -0
  28. package/dist/scripts/client.d.ts +45 -0
  29. package/dist/scripts/client.js +320 -0
  30. package/dist/scripts/commands.js +6 -0
  31. package/dist/scripts/comments.d.ts +8 -26
  32. package/dist/scripts/comments.js +27 -14
  33. package/dist/scripts/lib/builds.d.ts +40 -0
  34. package/dist/scripts/lib/builds.js +198 -0
  35. package/dist/scripts/lib/cli.js +3 -1
  36. package/dist/scripts/lib/deploy.d.ts +6 -90
  37. package/dist/scripts/lib/deploy.js +24 -137
  38. package/dist/scripts/lib/dns.d.ts +49 -0
  39. package/dist/scripts/lib/dns.js +141 -0
  40. package/dist/scripts/lib/fake-host.d.ts +168 -0
  41. package/dist/scripts/lib/fake-host.js +244 -0
  42. package/dist/scripts/lib/host.d.ts +87 -0
  43. package/dist/scripts/lib/host.js +362 -0
  44. package/dist/scripts/lib/linked.d.ts +51 -0
  45. package/dist/scripts/lib/linked.js +124 -0
  46. package/dist/scripts/lib/names.d.ts +35 -0
  47. package/dist/scripts/lib/names.js +77 -0
  48. package/dist/scripts/lib/review.d.ts +23 -2
  49. package/dist/scripts/lib/review.js +32 -15
  50. package/dist/scripts/lib/studio-deps.d.ts +35 -0
  51. package/dist/scripts/lib/studio-deps.js +79 -0
  52. package/dist/scripts/lib/studios.d.ts +70 -0
  53. package/dist/scripts/lib/studios.js +187 -0
  54. package/dist/scripts/lib/update.d.ts +33 -0
  55. package/dist/scripts/lib/update.js +40 -0
  56. package/dist/scripts/lib/vercel.d.ts +1 -23
  57. package/dist/scripts/lib/vercel.js +6 -81
  58. package/dist/scripts/people.d.ts +7 -11
  59. package/dist/scripts/people.js +51 -46
  60. package/dist/scripts/preflight.d.ts +8 -1
  61. package/dist/scripts/preflight.js +36 -19
  62. package/dist/scripts/project.d.ts +86 -0
  63. package/dist/scripts/project.js +141 -0
  64. package/dist/scripts/share.d.ts +105 -30
  65. package/dist/scripts/share.js +82 -154
  66. package/dist/scripts/skills.d.ts +18 -2
  67. package/dist/scripts/skills.js +22 -3
  68. package/dist/scripts/studio.d.ts +75 -0
  69. package/dist/scripts/studio.js +401 -0
  70. package/dist/scripts/upgrade.d.ts +10 -2
  71. package/dist/scripts/upgrade.js +44 -14
  72. package/dist/server/api.d.ts +22 -11
  73. package/dist/server/api.js +116 -47
  74. package/dist/server/blob.d.ts +10 -0
  75. package/dist/server/blob.js +53 -0
  76. package/dist/server/checks.js +2 -2
  77. package/dist/server/dev.js +4 -4
  78. package/dist/server/env.d.ts +2 -0
  79. package/dist/server/env.js +9 -0
  80. package/dist/server/fake-upstash.js +52 -3
  81. package/dist/server/hosts.d.ts +11 -0
  82. package/dist/server/hosts.js +31 -0
  83. package/dist/server/local.d.ts +7 -1
  84. package/dist/server/local.js +10 -1
  85. package/dist/server/mark.d.ts +41 -0
  86. package/dist/server/mark.js +111 -0
  87. package/dist/server/session.d.ts +17 -9
  88. package/dist/server/session.js +22 -45
  89. package/dist/server/upstash.js +20 -0
  90. package/dist/vercel/files.d.ts +11 -0
  91. package/dist/vercel/files.js +68 -0
  92. package/dist/vercel/index.d.ts +4 -3
  93. package/dist/vercel/index.js +12 -8
  94. package/dist/vercel/proxy.d.ts +69 -4
  95. package/dist/vercel/proxy.js +171 -107
  96. package/dist/vite/plugin.d.ts +24 -1
  97. package/dist/vite/plugin.js +46 -2
  98. package/kit/dials.tsx +2 -2
  99. package/package.json +2 -1
  100. package/shell/Guide.tsx +11 -5
  101. package/shell/Index.tsx +32 -10
  102. package/shell/Notice.tsx +1 -1
  103. package/shell/controls.ts +1 -1
  104. package/shell/hub.ts +5 -1
  105. package/shell/main.tsx +18 -3
  106. package/shell/review/Review.tsx +6 -4
  107. package/shell/review/ThreadCard.tsx +20 -7
  108. package/shell/review/client.ts +42 -14
  109. package/shell/review/view.test.ts +19 -0
  110. package/shell/review/view.ts +25 -0
  111. package/shell/shell.css +4 -0
  112. package/shell/studio/ClientHub.tsx +98 -0
  113. package/shell/studio/hub.css +48 -0
  114. package/shell/studio/index.html +14 -0
  115. package/shell/studio/main.tsx +17 -0
  116. package/skills/miles-deploy/SKILL.md +120 -65
  117. package/skills/miles-design-hub/SKILL.md +11 -9
  118. package/skills/miles-review/SKILL.md +5 -7
  119. package/skills/miles-worktrees/SKILL.md +3 -3
  120. package/templates/conductor/settings.toml +1 -1
  121. package/templates/studio/.env.example +9 -0
  122. package/templates/studio/.vercelignore +5 -0
  123. package/templates/studio/README.md +84 -0
  124. package/templates/studio/_gitignore +7 -0
  125. package/templates/studio/api/files.ts +4 -0
  126. package/templates/studio/api/review.ts +4 -0
  127. package/templates/studio/build.mjs +4 -0
  128. package/templates/studio/package.json +16 -0
  129. package/templates/studio/proxy.ts +4 -0
  130. package/templates/studio/studio.json +9 -0
  131. package/templates/studio/vercel.json +27 -0
@@ -3,10 +3,22 @@
3
3
  // folder the hub lists is always a path the gate knows how to refuse.
4
4
  import { SLUG } from './feature.js';
5
5
  /**
6
- * First path segments the hub owns, never a prototype: the walkthroughs,
7
- * the review API, and the shared bundle.
6
+ * Never a project's or a prototype's slug: every first path segment the gate
7
+ * answers itself (the link path, the review API, the gate's font, the built
8
+ * prototypes, the shared bundle, the walkthroughs, the hub's own top files).
8
9
  */
9
- export const RESERVED = ['guide', 'api', 'assets'];
10
+ export const RESERVED = [
11
+ 'invite',
12
+ 'api',
13
+ '_gate',
14
+ '_p',
15
+ 'assets',
16
+ 'guide',
17
+ 'index.html',
18
+ 'favicon.svg',
19
+ ];
20
+ /** Never a client's slug: the studio's own host, and the names a host is often given. */
21
+ export const RESERVED_CLIENTS = ['studio', 'www', 'api'];
10
22
  /**
11
23
  * Everything built from `prototypes/<slug>/` lands under `/_p/<slug>/`, and
12
24
  * Studio's list of every slug at `/_p/all.js`. `_p` can never be a slug: SLUG
@@ -17,12 +29,27 @@ export const ALL_FILE = `${PROTOTYPE_DIR}/all.js`;
17
29
  export const entryFile = (slug) => `${PROTOTYPE_DIR}/${slug}/entry.js`;
18
30
  /** Whether `name` can be a prototype's folder: kebab-case and not one of the hub's own paths. */
19
31
  export const isPrototypeSlug = (name) => typeof name === 'string' && SLUG.test(name) && !RESERVED.includes(name);
32
+ /** A project is a first path segment too, below its client's host, so it follows the prototype's rule. */
33
+ export const isProjectSlug = isPrototypeSlug;
34
+ /** Whether `name` can be a client: kebab-case and not one of the studio's own host names. */
35
+ export const isClientSlug = (name) => typeof name === 'string' && SLUG.test(name) && !RESERVED_CLIENTS.includes(name);
36
+ /**
37
+ * A path segment that names one file wherever it is read: the characters a
38
+ * URL path carries as they are (RFC 3986: ASCII letters and digits,
39
+ * `-._~!$&'()*+,;=:@`), letters, marks and digits of any script, and spaces
40
+ * inside a name. Never a control character, `%`, `?`, `#`, a backslash or a slash,
41
+ * which a URL parser drops, cuts at or rewrites (`.<tab>.` becomes `..`), and
42
+ * never `.` or `..`.
43
+ */
44
+ const SEGMENT = /^(?! )[\w\p{L}\p{M}\p{N} .~!$&'()*+,;=:@-]+(?<! )$/u;
45
+ export const isSegment = (s) => SEGMENT.test(s) && s !== '.' && s !== '..';
20
46
  /**
21
47
  * 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.
48
+ * could mean more than one file: it does not decode, or a segment, decoded, is
49
+ * not `isSegment` (a `%` left after decoding, `%255F`, a backslash, a control
50
+ * character, `.`, `..`, or empty other than the last). The gate sends those
51
+ * nowhere, for anyone, rather than guess how a server resolves them. An
52
+ * encoded space or letter decodes and passes.
26
53
  */
27
54
  function segments(pathname) {
28
55
  let decoded;
@@ -32,40 +59,92 @@ function segments(pathname) {
32
59
  catch {
33
60
  return null;
34
61
  }
35
- if (/[%\\]/.test(decoded))
36
- return null;
37
62
  const parts = decoded.split('/').slice(1);
38
63
  if (parts.at(-1) === '')
39
64
  parts.pop();
40
- return parts.some((p) => p === '' || p === '.' || p === '..') ? null : parts;
65
+ return parts.every(isSegment) ? parts : null;
41
66
  }
42
67
  /**
43
68
  * 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).
69
+ * walkthroughs, the review API, the shared bundle, the gate's font folder,
70
+ * and the files the hub serves at the top (shell/index.html,
71
+ * shell/public/favicon.svg). `invite` and `_p` are reserved too but are not
72
+ * the hub's: the gate answers a link itself, and `_p` is a prototype's.
46
73
  */
47
- const HUB_ROOT = [...RESERVED, '_gate', 'index.html', 'favicon.svg'];
74
+ const HUB_ROOT = ['guide', 'api', 'assets', '_gate', 'index.html', 'favicon.svg'];
48
75
  /**
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.
76
+ * Which prototype a project's file belongs to, for the gate, from the path's
77
+ * segments below the project: null for a hub path (none, or a first segment
78
+ * in HUB_ROOT). The first segment must be one of those, `_p`, or a prototype
79
+ * slug; anything else (`_P`, `_p;x`, `_p.`, a full-width `_p`) belongs to
80
+ * `''`, refused like an internal prototype. A path under `_p/` always
81
+ * belongs to one, so its name may be no slug at all (`all.js`), which no
82
+ * audience makes client-facing.
57
83
  */
58
- export function prototypeOfPath(pathname) {
59
- const parts = segments(pathname);
60
- if (!parts)
61
- return '';
62
- const [first, second] = parts;
84
+ function prototypeOfParts([first, second]) {
63
85
  if (first === undefined || HUB_ROOT.includes(first))
64
86
  return null;
65
87
  if (first === PROTOTYPE_DIR)
66
88
  return second ?? '';
67
89
  return isPrototypeSlug(first) ? first : '';
68
90
  }
91
+ /** The one-time link path, matched on the path as sent, as the gate always has. */
92
+ export const LINK_PATH = /^\/invite\/([\w-]{20,100})\/?$/;
93
+ /** What the gate pages load before sign-in, at the host root: the hub's face and the favicon. */
94
+ export const GATE_FONT = '/_gate/geist.woff2';
95
+ const GATE_ASSETS = [GATE_FONT, '/favicon.svg'];
96
+ /**
97
+ * The host proof `client add` asks a new client's host for: the studio gate
98
+ * answers a GET of it with the proof's answer only when the store holds that
99
+ * ask for the host's client. Otherwise it is a hub path like any other.
100
+ */
101
+ export const PROOF_PATH = /^\/_gate\/proof\/([\w-]{20,100})$/;
102
+ export const proofPath = (ask) => `/_gate/proof/${ask}`;
103
+ /**
104
+ * The studio site's functions, at a client host's root: the review API, and
105
+ * the files function the gate rewrites a project's files to. The gate passes
106
+ * them to anyone signed in; each checks the request again itself.
107
+ */
108
+ const FUNCTIONS = ['review', 'files'];
109
+ /** A host name as a lookup key: lower case, no port, no trailing dot; '' for anything that is not one. */
110
+ export function hostName(host) {
111
+ const name = host.trim().toLowerCase().replace(/:\d+$/, '').replace(/\.$/, '');
112
+ return name.length <= 253 && /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(name) ? name : '';
113
+ }
114
+ const ROOT_HUB = ['assets', '_gate', 'index.html'];
115
+ /** Below a project: its review API, or one of its build's files. */
116
+ function projectRoute(project, rest) {
117
+ if (rest[0] !== 'api')
118
+ return { kind: 'file', project, file: rest, prototype: prototypeOfParts(rest) };
119
+ return rest.length === 2 && rest[1] === 'review' ? { kind: 'api', project } : { kind: 'none' };
120
+ }
121
+ function where(pathname) {
122
+ const linked = pathname.match(LINK_PATH);
123
+ if (linked)
124
+ return { kind: 'link', token: linked[1] };
125
+ if (GATE_ASSETS.includes(pathname))
126
+ return { kind: 'asset' };
127
+ const parts = segments(pathname);
128
+ if (!parts)
129
+ return { kind: 'none' };
130
+ const [first, ...rest] = parts;
131
+ if (first === undefined || ROOT_HUB.includes(first))
132
+ return { kind: 'hub' };
133
+ if (first === 'api')
134
+ return rest.length === 1 && FUNCTIONS.includes(rest[0])
135
+ ? { kind: 'api', project: null }
136
+ : { kind: 'none' };
137
+ return isProjectSlug(first) ? projectRoute(first, rest) : { kind: 'none' };
138
+ }
139
+ /**
140
+ * The one place a studio site reads a request's path. It fails closed: a
141
+ * path that could mean more than one file (`segments`) goes nowhere. Which client a host is, is the store's to say (a host may be a
142
+ * codename): this names the host, never the client.
143
+ */
144
+ export function routeOf(host, pathname) {
145
+ const name = hostName(host);
146
+ return { host: name, ...(name ? where(pathname) : { kind: 'none' }) };
147
+ }
69
148
  /**
70
149
  * Whether a built prototype's entry is there for this person, asked with a
71
150
  * HEAD before it is imported. Not there: a 404 (the gate's answer for a
@@ -21,6 +21,11 @@ export declare const ACCESS: readonly ['3m', '6m', 'open'];
21
21
  export type Access = (typeof ACCESS)[number];
22
22
  /** The project default when none is set. */
23
23
  export declare const DEFAULT_ACCESS: Access;
24
+ /**
25
+ * How long a studio site's instance keeps a host's answer (server/hosts.ts):
26
+ * archiving a client, or adding one, takes effect on every instance within it.
27
+ */
28
+ export declare const HOST_SECONDS = 60;
24
29
  /** When access given at `from` ends, as an ISO time, or null for open-ended. */
25
30
  export declare function accessEnd(access: Access, from: Date): string | null;
26
31
  export declare const LIMITS: {
@@ -39,6 +44,11 @@ export type Person = {
39
44
  /** The group a person belongs to, shown beside their name: the studio, or the client's company. */
40
45
  team: string;
41
46
  kind: PersonKind;
47
+ /**
48
+ * The client a Client belongs to, or null for Studio, who works in every
49
+ * client. Never stored in the profile: it is where the person's records live.
50
+ */
51
+ client: string | null;
42
52
  /** Always null until email sign-in exists. */
43
53
  email: string | null;
44
54
  /** When their access ends (ISO time), or null for open-ended. Their sessions end with it. */
@@ -48,6 +58,58 @@ export type Person = {
48
58
  /** Bumped on revoke and on sign out everywhere, which ends every session signed with the old one. */
49
59
  epoch: number;
50
60
  };
61
+ /**
62
+ * Where an op runs: one client, and one of its projects for anything about
63
+ * prototypes and comments. Every service method takes one, every store key is
64
+ * built from one (store.ts), and a Client acts only inside their own client.
65
+ */
66
+ export type Scope = {
67
+ client: string;
68
+ project?: string;
69
+ };
70
+ /**
71
+ * The one client of this machine's own store, which `design-hub dev` and
72
+ * `--local` commands use (LOCAL_SCOPE in server/local.ts). It has no record
73
+ * and is never added or archived, so ops in it need none (`allow` in
74
+ * service.ts), and no studio may add a client by this name.
75
+ */
76
+ export declare const LOCAL_CLIENT = "site";
77
+ /** A slug a studio's client can have: a client slug, and never the local store's. */
78
+ export declare const isStudioClient: (slug: unknown) => slug is string;
79
+ /** What a slug that cannot be a studio's client is told. */
80
+ export declare const STUDIO_CLIENT_SLUG = "slug: kebab-case, and not studio, www, api or site";
81
+ /**
82
+ * A client of the studio: one host (`<client>.<apex>`, or a codename below
83
+ * the apex), every project of it, its Client people. An archived client's
84
+ * host is the plain 404. `access` is its default access length, unused until
85
+ * clients set one. `provenAt` is when its host proved it is this studio's
86
+ * site (endProof in service.ts); until then the client is still being added
87
+ * (isAdded).
88
+ */
89
+ export type Client = {
90
+ slug: string;
91
+ host: string;
92
+ name: string;
93
+ access: Access | null;
94
+ createdAt: string;
95
+ archivedAt: string | null;
96
+ provenAt: string | null;
97
+ };
98
+ /**
99
+ * Whether a client has finished being added: its host has proved it is this
100
+ * studio's site. A record with no such mark is still being added, whatever
101
+ * became of its proof, so nothing of a project and no sign-in link goes to
102
+ * its host, and `client add` with the same flags finishes it.
103
+ */
104
+ export declare const isAdded: (client: Client) => boolean;
105
+ /** A project of one client, served at `/<slug>/` on its host from the build `build` points at, or not at all while it is null. */
106
+ export type Project = {
107
+ slug: string;
108
+ client: string;
109
+ name: string;
110
+ build: string | null;
111
+ createdAt: string;
112
+ };
51
113
  /** A one-time sign-in link, stored under the hash of its token and deleted when used. */
52
114
  export type Link = {
53
115
  person: string;
@@ -117,8 +179,10 @@ export declare const threadLabel: (c: Pick<Comment, 'audience' | 'number'>) => s
117
179
  * Client anything. A missing or broken number reads as 0, no number.
118
180
  */
119
181
  export declare function readComment(json: string): Comment;
120
- /** Whether a stored profile (a person without revocation and epoch) is well formed. */
121
- export declare function isProfile(v: unknown): v is Omit<Person, 'revokedAt' | 'epoch'>;
182
+ /** What a person's profile record holds: everything but revocation, epoch and client, which live elsewhere. */
183
+ export type Profile = Omit<Person, 'revokedAt' | 'epoch' | 'client'>;
184
+ /** Whether a stored profile is well formed. */
185
+ export declare function isProfile(v: unknown): v is Profile;
122
186
  /**
123
187
  * Whether someone may sign in at `at`: not revoked, and an access end that is
124
188
  * open (null) or a parseable time still ahead. An access end that does not
@@ -139,6 +203,16 @@ export type PersonInput = Pick<Person, 'name' | 'team' | 'kind'> & {
139
203
  access: Access | null;
140
204
  };
141
205
  export declare function checkPersonInput(input: unknown): Checked<PersonInput>;
206
+ export type ClientInput = Pick<Client, 'slug' | 'host' | 'name'>;
207
+ /** A new client: a client slug, the host name it is served on (lower case), and its name. */
208
+ export declare function checkClientInput(input: unknown): Checked<ClientInput>;
209
+ export type ProjectInput = Pick<Project, 'slug' | 'name'>;
210
+ /** A new project: a project slug (never a path the gate answers itself) and its name. */
211
+ export declare function checkProjectInput(input: unknown): Checked<ProjectInput>;
212
+ /** A stored client, or null for anything that is not a well-formed one. */
213
+ export declare function readClient(json: string | null): Client | null;
214
+ /** A stored project, or null for anything that is not a well-formed one. */
215
+ export declare function readProject(json: string | null): Project | null;
142
216
  /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
143
217
  export declare function checkAudience(input: unknown): Checked<Audience | null>;
144
218
  /** A prototype's audience as Studio sets it: one of AUDIENCES, required. */
@@ -5,6 +5,8 @@
5
5
  // calls it.
6
6
  import { CONTROL_KINDS, isControlId } from '../controls.js';
7
7
  import { SLUG } from '../feature.js';
8
+ import { isBuildId } from '../files.js';
9
+ import { hostName, isClientSlug, isProjectSlug } from '../prototype.js';
8
10
  /** Which side a person is on: the studio that makes the work, or the client it is for. */
9
11
  export const PERSON_KINDS = ['studio', 'client'];
10
12
  /**
@@ -23,6 +25,11 @@ export const LINK_DAYS = 14;
23
25
  export const ACCESS = ['3m', '6m', 'open'];
24
26
  /** The project default when none is set. */
25
27
  export const DEFAULT_ACCESS = '3m';
28
+ /**
29
+ * How long a studio site's instance keeps a host's answer (server/hosts.ts):
30
+ * archiving a client, or adding one, takes effect on every instance within it.
31
+ */
32
+ export const HOST_SECONDS = 60;
26
33
  const ACCESS_MONTHS = { '3m': 3, '6m': 6 };
27
34
  /** When access given at `from` ends, as an ISO time, or null for open-ended. */
28
35
  export function accessEnd(access, from) {
@@ -47,6 +54,24 @@ export const LIMITS = {
47
54
  states: 50,
48
55
  stateName: 120,
49
56
  };
57
+ /**
58
+ * The one client of this machine's own store, which `design-hub dev` and
59
+ * `--local` commands use (LOCAL_SCOPE in server/local.ts). It has no record
60
+ * and is never added or archived, so ops in it need none (`allow` in
61
+ * service.ts), and no studio may add a client by this name.
62
+ */
63
+ export const LOCAL_CLIENT = 'site';
64
+ /** A slug a studio's client can have: a client slug, and never the local store's. */
65
+ export const isStudioClient = (slug) => isClientSlug(slug) && slug !== LOCAL_CLIENT;
66
+ /** What a slug that cannot be a studio's client is told. */
67
+ export const STUDIO_CLIENT_SLUG = 'slug: kebab-case, and not studio, www, api or site';
68
+ /**
69
+ * Whether a client has finished being added: its host has proved it is this
70
+ * studio's site. A record with no such mark is still being added, whatever
71
+ * became of its proof, so nothing of a project and no sign-in link goes to
72
+ * its host, and `client add` with the same flags finishes it.
73
+ */
74
+ export const isAdded = (client) => client.provenAt !== null;
50
75
  /**
51
76
  * How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
52
77
  * `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
@@ -77,7 +102,7 @@ const PROFILE_RULES = [
77
102
  ['accessEndsAt', isTextOrNull],
78
103
  ['createdAt', isText],
79
104
  ];
80
- /** Whether a stored profile (a person without revocation and epoch) is well formed. */
105
+ /** Whether a stored profile is well formed. */
81
106
  export function isProfile(v) {
82
107
  if (!v || typeof v !== 'object' || Array.isArray(v))
83
108
  return false;
@@ -133,6 +158,56 @@ export function checkPersonInput(input) {
133
158
  ? { ok: false, problems }
134
159
  : { ok: true, value: { name, team, kind: kind, access: access } };
135
160
  }
161
+ /** A new client: a client slug, the host name it is served on (lower case), and its name. */
162
+ export function checkClientInput(input) {
163
+ const i = (input ?? {});
164
+ const problems = [];
165
+ const name = text(i.name, 'name', LIMITS.name, problems);
166
+ if (!isStudioClient(i.slug))
167
+ problems.push(STUDIO_CLIENT_SLUG);
168
+ const host = typeof i.host === 'string' ? hostName(i.host) : '';
169
+ if (!host || host !== i.host)
170
+ problems.push('host: a host name in lower case, such as acme.example.com');
171
+ return problems.length
172
+ ? { ok: false, problems }
173
+ : { ok: true, value: { slug: i.slug, host, name } };
174
+ }
175
+ /** A new project: a project slug (never a path the gate answers itself) and its name. */
176
+ export function checkProjectInput(input) {
177
+ const i = (input ?? {});
178
+ const problems = [];
179
+ const name = text(i.name, 'name', LIMITS.name, problems);
180
+ if (!isProjectSlug(i.slug))
181
+ problems.push('slug: kebab-case, and not a path the hub answers itself');
182
+ return problems.length ? { ok: false, problems } : { ok: true, value: { slug: i.slug, name } };
183
+ }
184
+ const isRecord = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
185
+ /** A stored client, or null for anything that is not a well-formed one. */
186
+ export function readClient(json) {
187
+ const c = safeJson(json);
188
+ if (!isRecord(c) || !isClientSlug(c.slug) || typeof c.host !== 'string' || !isText(c.name))
189
+ return null;
190
+ if (!isText(c.createdAt) || !isTextOrNull(c.archivedAt) || !isTextOrNull(c.provenAt))
191
+ return null;
192
+ return c;
193
+ }
194
+ /** A stored project, or null for anything that is not a well-formed one. */
195
+ export function readProject(json) {
196
+ const p = safeJson(json);
197
+ if (!isRecord(p) || !isProjectSlug(p.slug) || !isClientSlug(p.client) || !isText(p.name))
198
+ return null;
199
+ if (!isText(p.createdAt) || !(p.build === null || isBuildId(p.build)))
200
+ return null;
201
+ return p;
202
+ }
203
+ function safeJson(json) {
204
+ try {
205
+ return json === null ? null : JSON.parse(json);
206
+ }
207
+ catch {
208
+ return null;
209
+ }
210
+ }
136
211
  /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
137
212
  export function checkAudience(input) {
138
213
  if (input === undefined || input === null)
@@ -1,13 +1,20 @@
1
- import type { Audience, Comment, Person, PersonKind } from './model.ts';
2
- export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people' | 'audience';
1
+ import type { Audience, Comment, Person, PersonKind, Scope } from './model.ts';
2
+ export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people' | 'audience' | 'setup';
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'>;
4
+ type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt' | 'client'>;
5
+ /**
6
+ * Whether an op in `scope` is the actor's at all: Studio works in every
7
+ * client, a Client only in their own. A person whose kind and client
8
+ * disagree (Studio with a client, a Client with none) is in no scope, and
9
+ * so is every op on a scope that names no client: it fails closed here.
10
+ */
11
+ export declare function inScope(actor: Pick<Person, 'kind' | 'client'>, scope: Scope): boolean;
5
12
  /**
6
13
  * `root` is the thread acted on, `subject` the comment in it when that is a
7
14
  * reply (edit or delete one); without `subject`, the root is the subject.
8
15
  * Without a root, the action is on no thread: listing, or a new thread.
9
16
  */
10
- export declare function can(actor: Actor | null, action: Action, root?: Comment, subject?: Comment): boolean;
17
+ export declare function can(actor: Actor | null, scope: Scope, action: Action, root?: Comment, subject?: Comment): boolean;
11
18
  /**
12
19
  * The view a person gets: their own kind, or a narrower one they ask for.
13
20
  * Studio may look as a Client does (Client view); a Client asking for
@@ -1,3 +1,14 @@
1
+ /**
2
+ * Whether an op in `scope` is the actor's at all: Studio works in every
3
+ * client, a Client only in their own. A person whose kind and client
4
+ * disagree (Studio with a client, a Client with none) is in no scope, and
5
+ * so is every op on a scope that names no client: it fails closed here.
6
+ */
7
+ export function inScope(actor, scope) {
8
+ if (typeof scope.client !== 'string' || scope.client === '')
9
+ return false;
10
+ return actor.kind === 'studio' ? actor.client === null : actor.client === scope.client;
11
+ }
1
12
  /** Whether a comment reaches a Client. Only the exact value counts, so a record without one stays inside. */
2
13
  const clientFacing = (c) => c.audience === 'client';
3
14
  /**
@@ -5,8 +16,8 @@ const clientFacing = (c) => c.audience === 'client';
5
16
  * reply (edit or delete one); without `subject`, the root is the subject.
6
17
  * Without a root, the action is on no thread: listing, or a new thread.
7
18
  */
8
- export function can(actor, action, root, subject) {
9
- if (!actor || actor.revokedAt)
19
+ export function can(actor, scope, action, root, subject) {
20
+ if (!actor || actor.revokedAt || !inScope(actor, scope))
10
21
  return false;
11
22
  const studio = actor.kind === 'studio';
12
23
  if (!studio && [root, subject].some((c) => c && !clientFacing(c)))
@@ -24,6 +35,7 @@ export function can(actor, action, root, subject) {
24
35
  case 'triage':
25
36
  case 'people':
26
37
  case 'audience':
38
+ case 'setup':
27
39
  return studio;
28
40
  }
29
41
  }