@milesroxas/design-hub 0.9.0 → 0.11.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.
Files changed (150) hide show
  1. package/README.md +13 -11
  2. package/core/controls.test.ts +176 -0
  3. package/core/controls.ts +195 -0
  4. package/core/files.test.ts +76 -0
  5. package/core/files.ts +131 -0
  6. package/core/prototype.test.ts +124 -7
  7. package/core/prototype.ts +133 -24
  8. package/core/review/model.ts +144 -2
  9. package/core/review/policy.test.ts +79 -22
  10. package/core/review/policy.ts +36 -8
  11. package/core/review/service.test.ts +1525 -322
  12. package/core/review/service.ts +810 -183
  13. package/core/review/store.test.ts +91 -0
  14. package/core/review/store.ts +143 -26
  15. package/dist/core/controls.d.ts +55 -0
  16. package/dist/core/controls.js +174 -0
  17. package/dist/core/files.d.ts +39 -0
  18. package/dist/core/files.js +107 -0
  19. package/dist/core/prototype.d.ts +61 -12
  20. package/dist/core/prototype.js +105 -26
  21. package/dist/core/review/model.d.ts +87 -2
  22. package/dist/core/review/model.js +97 -1
  23. package/dist/core/review/policy.d.ts +11 -4
  24. package/dist/core/review/policy.js +14 -2
  25. package/dist/core/review/service.d.ts +222 -43
  26. package/dist/core/review/service.js +727 -182
  27. package/dist/core/review/store.d.ts +84 -24
  28. package/dist/core/review/store.js +66 -26
  29. package/dist/scripts/bootstrap.d.ts +4 -34
  30. package/dist/scripts/bootstrap.js +9 -244
  31. package/dist/scripts/cli.js +6 -0
  32. package/dist/scripts/client.d.ts +45 -0
  33. package/dist/scripts/client.js +320 -0
  34. package/dist/scripts/commands.js +6 -0
  35. package/dist/scripts/comments.d.ts +8 -26
  36. package/dist/scripts/comments.js +27 -14
  37. package/dist/scripts/guard.d.ts +11 -0
  38. package/dist/scripts/guard.js +44 -0
  39. package/dist/scripts/lib/builds.d.ts +40 -0
  40. package/dist/scripts/lib/builds.js +198 -0
  41. package/dist/scripts/lib/cli.js +3 -1
  42. package/dist/scripts/lib/deploy.d.ts +6 -90
  43. package/dist/scripts/lib/deploy.js +24 -137
  44. package/dist/scripts/lib/dns.d.ts +49 -0
  45. package/dist/scripts/lib/dns.js +141 -0
  46. package/dist/scripts/lib/fake-host.d.ts +168 -0
  47. package/dist/scripts/lib/fake-host.js +244 -0
  48. package/dist/scripts/lib/host.d.ts +87 -0
  49. package/dist/scripts/lib/host.js +362 -0
  50. package/dist/scripts/lib/linked.d.ts +51 -0
  51. package/dist/scripts/lib/linked.js +124 -0
  52. package/dist/scripts/lib/names.d.ts +35 -0
  53. package/dist/scripts/lib/names.js +77 -0
  54. package/dist/scripts/lib/review.d.ts +23 -2
  55. package/dist/scripts/lib/review.js +32 -15
  56. package/dist/scripts/lib/rules.d.ts +1 -0
  57. package/dist/scripts/lib/rules.js +1 -0
  58. package/dist/scripts/lib/studio-deps.d.ts +35 -0
  59. package/dist/scripts/lib/studio-deps.js +79 -0
  60. package/dist/scripts/lib/studios.d.ts +70 -0
  61. package/dist/scripts/lib/studios.js +187 -0
  62. package/dist/scripts/lib/update.d.ts +33 -0
  63. package/dist/scripts/lib/update.js +40 -0
  64. package/dist/scripts/lib/vercel.d.ts +1 -23
  65. package/dist/scripts/lib/vercel.js +6 -81
  66. package/dist/scripts/people.d.ts +7 -11
  67. package/dist/scripts/people.js +51 -46
  68. package/dist/scripts/preflight.d.ts +8 -1
  69. package/dist/scripts/preflight.js +36 -19
  70. package/dist/scripts/project.d.ts +86 -0
  71. package/dist/scripts/project.js +141 -0
  72. package/dist/scripts/share.d.ts +105 -30
  73. package/dist/scripts/share.js +82 -154
  74. package/dist/scripts/skills.d.ts +18 -2
  75. package/dist/scripts/skills.js +22 -3
  76. package/dist/scripts/studio.d.ts +75 -0
  77. package/dist/scripts/studio.js +401 -0
  78. package/dist/scripts/upgrade.d.ts +10 -2
  79. package/dist/scripts/upgrade.js +44 -14
  80. package/dist/server/api.d.ts +22 -11
  81. package/dist/server/api.js +125 -44
  82. package/dist/server/blob.d.ts +10 -0
  83. package/dist/server/blob.js +53 -0
  84. package/dist/server/checks.js +2 -2
  85. package/dist/server/dev.js +4 -4
  86. package/dist/server/env.d.ts +2 -0
  87. package/dist/server/env.js +9 -0
  88. package/dist/server/fake-upstash.js +52 -3
  89. package/dist/server/hosts.d.ts +11 -0
  90. package/dist/server/hosts.js +31 -0
  91. package/dist/server/local.d.ts +7 -1
  92. package/dist/server/local.js +10 -1
  93. package/dist/server/mark.d.ts +41 -0
  94. package/dist/server/mark.js +111 -0
  95. package/dist/server/session.d.ts +17 -9
  96. package/dist/server/session.js +22 -45
  97. package/dist/server/upstash.js +20 -0
  98. package/dist/vercel/files.d.ts +11 -0
  99. package/dist/vercel/files.js +68 -0
  100. package/dist/vercel/index.d.ts +4 -3
  101. package/dist/vercel/index.js +12 -8
  102. package/dist/vercel/proxy.d.ts +69 -4
  103. package/dist/vercel/proxy.js +171 -107
  104. package/dist/vite/plugin.d.ts +24 -1
  105. package/dist/vite/plugin.js +46 -2
  106. package/kit/Artboard.tsx +21 -8
  107. package/kit/Space.tsx +4 -3
  108. package/kit/Variants.tsx +193 -65
  109. package/kit/ViewTools.tsx +77 -11
  110. package/kit/audience.ts +85 -0
  111. package/kit/dials.css +59 -0
  112. package/kit/dials.tsx +272 -35
  113. package/kit/harness.css +34 -0
  114. package/kit/prefs.ts +23 -1
  115. package/package.json +3 -2
  116. package/shell/ClientViewNote.tsx +31 -3
  117. package/shell/Guide.tsx +11 -5
  118. package/shell/Index.tsx +32 -10
  119. package/shell/Notice.tsx +1 -1
  120. package/shell/controls.ts +26 -0
  121. package/shell/hub.ts +5 -1
  122. package/shell/main.tsx +33 -6
  123. package/shell/review/Review.tsx +2 -2
  124. package/shell/review/ThreadCard.tsx +5 -5
  125. package/shell/review/client.ts +28 -11
  126. package/shell/review/view.test.ts +19 -0
  127. package/shell/review/view.ts +25 -0
  128. package/shell/shell.css +8 -0
  129. package/shell/studio/ClientHub.tsx +98 -0
  130. package/shell/studio/hub.css +48 -0
  131. package/shell/studio/index.html +14 -0
  132. package/shell/studio/main.tsx +17 -0
  133. package/skills/miles-brand/SKILL.md +11 -1
  134. package/skills/miles-deploy/SKILL.md +121 -64
  135. package/skills/miles-design-hub/SKILL.md +11 -9
  136. package/skills/miles-prototype/SKILL.md +34 -1
  137. package/skills/miles-review/SKILL.md +5 -7
  138. package/skills/miles-worktrees/SKILL.md +3 -3
  139. package/templates/conductor/settings.toml +1 -1
  140. package/templates/studio/.env.example +9 -0
  141. package/templates/studio/.vercelignore +5 -0
  142. package/templates/studio/README.md +84 -0
  143. package/templates/studio/_gitignore +7 -0
  144. package/templates/studio/api/files.ts +4 -0
  145. package/templates/studio/api/review.ts +4 -0
  146. package/templates/studio/build.mjs +4 -0
  147. package/templates/studio/package.json +16 -0
  148. package/templates/studio/proxy.ts +4 -0
  149. package/templates/studio/studio.json +9 -0
  150. package/templates/studio/vercel.json +27 -0
@@ -0,0 +1,141 @@
1
+ /// <reference types="node" />
2
+ // The DNS port: how a new client's host comes to point at the studio site.
3
+ // Three adapters (studios-findings.md, sections 2 and 3):
4
+ //
5
+ // wildcard Vercel DNS: `*.<apex>` is on the studio project once, at
6
+ // `studio create`, so every host already resolves; no call.
7
+ // per-host Cloudflare by its API: one DNS-only CNAME per host, looked up
8
+ // first and never overwritten; on archive, removed only when it
9
+ // is exactly the record this adapter made.
10
+ // manual any other provider: the record, for a person to add, and on
11
+ // archive, to remove.
12
+ //
13
+ // The Cloudflare token edits one zone's DNS. It lives in the studio folder's
14
+ // gitignored `.env` and is sent only in the Authorization header; no error
15
+ // here repeats it.
16
+ import { CliError } from './cli.js';
17
+ /** The wildcard serves every host and stays: nothing to add or remove per host. */
18
+ export const wildcardDns = () => ({
19
+ mode: 'wildcard',
20
+ ensure: async () => 'exists',
21
+ release: async () => ({ removed: [], left: [] }),
22
+ });
23
+ export const manualDns = (target) => {
24
+ const record = async (host) => ({ record: `CNAME ${host} ${await target(host)}` });
25
+ return { mode: 'manual', ensure: record, release: record };
26
+ };
27
+ const CF = 'https://api.cloudflare.com/client/v4';
28
+ /** What Vercel accepts a host pointing at: its own target, or the generic name. */
29
+ const GENERIC = 'cname.vercel-dns.com';
30
+ const bare = (name) => name.toLowerCase().replace(/\.$/, '');
31
+ const shown = (r) => `${r.type} ${r.name} ${r.content}`;
32
+ /** The comment the adapter writes on each record, naming the studio that made it. */
33
+ const madeBy = (studio) => `design-hub studio ${studio}`;
34
+ /**
35
+ * Whether the records already at a host are the one this studio needs: a
36
+ * single DNS-only CNAME to Vercel. Anything else (an A or AAAA record,
37
+ * another CNAME, a proxied one) is a person's to change, so it is refused
38
+ * with what is there, never overwritten.
39
+ */
40
+ export function classify(found, target) {
41
+ const [only] = found;
42
+ const ok = found.length === 1 &&
43
+ only.type === 'CNAME' &&
44
+ [target, GENERIC].includes(bare(only.content)) &&
45
+ only.proxied !== true;
46
+ if (ok)
47
+ return 'exists';
48
+ throw new CliError('The host already has DNS records this studio did not make.', {
49
+ found: found.map((r) => ({ type: r.type, content: r.content, proxied: r.proxied ?? false })),
50
+ fix: `Point it at ${target} with one DNS-only CNAME at Cloudflare, or pick another --host.`,
51
+ });
52
+ }
53
+ export function cloudflareDns({ token, apex, zone, studio, target, fetcher = fetch, }) {
54
+ async function cf(path, init = {}) {
55
+ const res = await fetcher(`${CF}${path}`, {
56
+ ...init,
57
+ headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
58
+ });
59
+ return (await res.json().catch(() => ({ success: false, result: null })));
60
+ }
61
+ async function zoneId() {
62
+ if (zone)
63
+ return zone;
64
+ const reply = await cf(`/zones?name=${encodeURIComponent(apex)}`);
65
+ const active = (reply.result ?? []).filter((z) => z.status === 'active');
66
+ if (!reply.success || active.length !== 1)
67
+ throw new CliError(`Cloudflare has no one active zone for ${apex} that this token can read.`, {
68
+ errors: reply.errors ?? [],
69
+ fix: 'Give the studio a token with DNS edit on that zone (studio link --cloudflare-token-file), or set dns.zone in studio.json.',
70
+ });
71
+ return active[0].id;
72
+ }
73
+ const look = async (id, host) => {
74
+ const reply = await cf(`/zones/${id}/dns_records?name.exact=${encodeURIComponent(host)}`);
75
+ if (!reply.success)
76
+ throw new CliError('Cloudflare would not list the host’s records.', {
77
+ errors: reply.errors ?? [],
78
+ fix: 'Check that the token has DNS edit on the zone.',
79
+ });
80
+ return reply.result ?? [];
81
+ };
82
+ return {
83
+ mode: 'per-host',
84
+ async ensure(host) {
85
+ const to = bare(await target(host));
86
+ const id = await zoneId();
87
+ const found = await look(id, host);
88
+ if (found.length)
89
+ return classify(found, to);
90
+ const body = {
91
+ type: 'CNAME',
92
+ name: host,
93
+ content: to,
94
+ proxied: false,
95
+ ttl: 60,
96
+ comment: madeBy(studio),
97
+ };
98
+ const made = await cf(`/zones/${id}/dns_records`, {
99
+ method: 'POST',
100
+ body: JSON.stringify(body),
101
+ });
102
+ if (made.success)
103
+ return 'added';
104
+ // Whatever the error, look again: two adds at once, or one that landed before its answer was lost.
105
+ const after = await look(id, host);
106
+ if (after.length)
107
+ return classify(after, to);
108
+ throw new CliError('Cloudflare would not add the host’s record.', {
109
+ errors: made.errors ?? [],
110
+ fix: 'Check that the token has DNS edit on the zone, then run this again.',
111
+ });
112
+ },
113
+ /**
114
+ * Removes the record ensure made, and nothing else: the same name, type
115
+ * CNAME, the content it would write today and this studio's comment.
116
+ * Every other record at the name is a person's, left alone and named.
117
+ */
118
+ async release(host) {
119
+ const to = bare(await target(host));
120
+ const id = await zoneId();
121
+ const found = await look(id, host);
122
+ const ours = (r) => typeof r.id === 'string' &&
123
+ /^[a-f0-9]{32}$/.test(r.id) &&
124
+ bare(r.name) === host &&
125
+ r.type === 'CNAME' &&
126
+ bare(r.content) === to &&
127
+ r.comment === madeBy(studio);
128
+ const removed = [];
129
+ for (const r of found.filter(ours)) {
130
+ const gone = await cf(`/zones/${id}/dns_records/${r.id}`, { method: 'DELETE' });
131
+ if (!gone.success)
132
+ throw new CliError('Cloudflare would not remove the host’s record.', {
133
+ errors: gone.errors ?? [],
134
+ fix: 'Check that the token has DNS edit on the zone, then run client archive again.',
135
+ });
136
+ removed.push(shown(r));
137
+ }
138
+ return { removed, left: found.filter((r) => !ours(r)).map(shown) };
139
+ },
140
+ };
141
+ }
@@ -0,0 +1,168 @@
1
+ import { type Review } from '../../core/review/service.ts';
2
+ import type { Deployment, DomainState, Host } from './host.ts';
3
+ import type { StudioDeps } from './studio-deps.ts';
4
+ export type FakeHost = {
5
+ host: Host;
6
+ calls: string[];
7
+ env: Map<string, string>;
8
+ domains: Map<string, DomainState>;
9
+ deployments: Deployment[];
10
+ /** Hosts whose DNS reaches the studio site; every host does unless a test empties this and adds its own. */
11
+ up: Set<string> | 'all';
12
+ /** What the studio site's gate answers a host proof's ask on a host, or null; fakeDeps answers from its store. */
13
+ gate: (host: string, ask: string) => Promise<string | null>;
14
+ /** Ids Vercel refuses to delete: the proof after deleting must catch them. */
15
+ stuck: Set<string>;
16
+ /** The verification a new domain needs, or none. */
17
+ verify: DomainState['verification'];
18
+ /** Whether the folder is linked to the studio's project, so a second link keeps it. */
19
+ linked: boolean;
20
+ };
21
+ export declare function fakeHost(older?: string[]): FakeHost;
22
+ /**
23
+ * The studio site as a fetch from outside meets it, over the commands' own
24
+ * store, answering as the gate does (vercel/proxy.ts, which `pnpm quality`
25
+ * keeps scripts from importing): a host no client has is the plain 404, the
26
+ * host proof's path answers the proof's answer for its ask, a link page is
27
+ * 200 while the link is live and a POST uses it up, anything else is the
28
+ * signed-out 401. It keeps each host's answer a minute, unknown hosts too,
29
+ * with the gate's own cache (server/hosts.ts).
30
+ */
31
+ export declare function fakeSite(review: Review): typeof fetch;
32
+ /**
33
+ * Every dependency of the studio and client commands, faked: one host, one
34
+ * review store (one database: a second studio made on it is refused) and one
35
+ * file store. `review` is the test's own view of that store, on the same
36
+ * clock. Cloudflare is a DNS that records each host it is asked to add or
37
+ * remove.
38
+ */
39
+ export declare function fakeDeps(home: string, cwd?: string): {
40
+ deps: StudioDeps;
41
+ fake: FakeHost;
42
+ kv: import("../../core/review/store.ts").KV;
43
+ review: {
44
+ person: (scope: import("../../core/review/model.ts").Scope, id: string) => Promise<import("../../core/review/model.ts").Person | null>;
45
+ clientOfHost(host: string): Promise<import("../../core/review/model.ts").Client | null>;
46
+ projectOf: ({ client, project }: import("../../core/review/model.ts").Scope) => Promise<import("../../core/review/model.ts").Project | null>;
47
+ listProjects(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Project[]>;
48
+ addClient(actor: import("../../core/review/model.ts").Person | null, input: unknown): Promise<import("../../core/review/model.ts").Client>;
49
+ clientWait(actor: import("../../core/review/model.ts").Person | null, input: {
50
+ slug: string;
51
+ host: string;
52
+ }): Promise<number>;
53
+ startProof(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<{
54
+ ask: string;
55
+ answer: string;
56
+ }>;
57
+ endProof(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Client>;
58
+ proofAnswer(scope: import("../../core/review/model.ts").Scope, ask: string): Promise<string | null>;
59
+ clientRecord(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Client>;
60
+ setClientAccess(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, access: unknown): Promise<import("../../core/review/model.ts").Client>;
61
+ archiveClient(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/service.ts").Archived | null>;
62
+ forgetClient(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<void>;
63
+ addProject(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: unknown): Promise<import("../../core/review/model.ts").Project>;
64
+ startBuild(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, build: unknown): Promise<void>;
65
+ setBuild(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, build: unknown): Promise<import("../../core/review/model.ts").Project>;
66
+ buildsOf(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<{
67
+ live: string | null;
68
+ builds: import("../../core/review/service.ts").Build[];
69
+ }>;
70
+ forgetBuilds(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, builds: string[]): Promise<void>;
71
+ sessionPerson(scope: import("../../core/review/model.ts").Scope, id: string, epoch: number): Promise<import("../../core/review/model.ts").Person | null>;
72
+ listPeople(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Person[]>;
73
+ addPerson(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: unknown): Promise<{
74
+ person: import("../../core/review/model.ts").Person;
75
+ token: string;
76
+ }>;
77
+ newLink(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, id: string): Promise<{
78
+ person: import("../../core/review/model.ts").Person;
79
+ token: string;
80
+ }>;
81
+ signOutEverywhere(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, id: string): Promise<{
82
+ person: import("../../core/review/model.ts").Person;
83
+ token: string;
84
+ }>;
85
+ revoke(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, id: string): Promise<import("../../core/review/model.ts").Person>;
86
+ setAccess(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, id: string, access: unknown): Promise<import("../../core/review/model.ts").Person>;
87
+ defaultAccess(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Access>;
88
+ setDefaultAccess(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, access: unknown): Promise<import("../../core/review/model.ts").Access>;
89
+ addStudioPerson(actor: import("../../core/review/model.ts").Person | null, input: unknown): Promise<import("../../core/review/model.ts").Person>;
90
+ newStudioLink(actor: import("../../core/review/model.ts").Person | null, id: string): Promise<{
91
+ person: import("../../core/review/model.ts").Person;
92
+ token: string;
93
+ }>;
94
+ studioAccess(actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Access>;
95
+ setStudioAccess(actor: import("../../core/review/model.ts").Person | null, access: unknown): Promise<import("../../core/review/model.ts").Access>;
96
+ peekLink(scope: import("../../core/review/model.ts").Scope, token: string): Promise<import("../../core/review/model.ts").Person | null>;
97
+ useLink(scope: import("../../core/review/model.ts").Scope, token: string): Promise<import("../../core/review/model.ts").Person | null>;
98
+ listComments(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, prototype: string, viewAs?: unknown): Promise<import("../../core/review/model.ts").Comment[]>;
99
+ allComments(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null): Promise<import("../../core/review/model.ts").Comment[]>;
100
+ add(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
101
+ body?: unknown;
102
+ anchor?: unknown;
103
+ states?: unknown;
104
+ audience?: unknown;
105
+ }): Promise<import("../../core/review/model.ts").Comment>;
106
+ reply(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
107
+ prototype: string;
108
+ thread: string;
109
+ body?: unknown;
110
+ }): Promise<import("../../core/review/model.ts").Comment>;
111
+ edit(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
112
+ prototype: string;
113
+ id: string;
114
+ body?: unknown;
115
+ }): Promise<import("../../core/review/model.ts").Comment>;
116
+ remove(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
117
+ prototype: string;
118
+ id: string;
119
+ }): Promise<string[]>;
120
+ resolve(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
121
+ prototype: string;
122
+ id: string;
123
+ resolved: boolean;
124
+ }): Promise<import("../../core/review/model.ts").Comment>;
125
+ retriage(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
126
+ prototype: string;
127
+ id: string;
128
+ triage?: unknown;
129
+ states?: unknown;
130
+ }): Promise<import("../../core/review/model.ts").Comment>;
131
+ setAudience(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, prototype: unknown, audience: unknown): Promise<import("../../core/review/model.ts").Audience>;
132
+ controlsOf(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, prototype: unknown): Promise<{
133
+ view: import("../../core/review/model.ts").PersonKind;
134
+ } & import("../../core/controls.ts").Marks>;
135
+ setControl(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, input: {
136
+ prototype: unknown;
137
+ kind: unknown;
138
+ id: unknown;
139
+ audience: unknown;
140
+ }): Promise<{
141
+ kind: import("../../core/controls.ts").ControlKind;
142
+ id: string;
143
+ audience: import("../../core/review/model.ts").Audience;
144
+ }>;
145
+ audienceOf(scope: import("../../core/review/model.ts").Scope, prototype: string): Promise<import("../../core/review/model.ts").Audience>;
146
+ visiblePrototypes(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, viewAs?: unknown): Promise<{
147
+ view: import("../../core/review/model.ts").PersonKind;
148
+ clientFacing: string[];
149
+ }>;
150
+ locate(scope: import("../../core/review/model.ts").Scope, actor: import("../../core/review/model.ts").Person | null, id: string): Promise<{
151
+ prototype: string;
152
+ comment: import("../../core/review/model.ts").Comment;
153
+ } | null>;
154
+ };
155
+ files: import("../../core/files.ts").Files;
156
+ ensured: string[];
157
+ released: string[];
158
+ hosts: string[];
159
+ clock: {
160
+ at: number;
161
+ };
162
+ pnpmFails: Set<string>;
163
+ dns: {
164
+ served: Set<string> | 'all' | 'unreachable';
165
+ seen: Set<string> | 'all';
166
+ negative: number | null;
167
+ };
168
+ };
@@ -0,0 +1,244 @@
1
+ /// <reference types="node" />
2
+ // A host that answers like a studio's Vercel team from memory, for the
3
+ // command tests: its env, its domains, its deployments, and whether a host
4
+ // answers over HTTPS. `calls` records every call in order, so a test can say
5
+ // what a command asked for and what it never did; fakeDeps adds pnpm's runs
6
+ // in the studio folder to the same list.
7
+ import { writeFileSync } from 'node:fs';
8
+ import { join } from 'node:path';
9
+ import { memoryFiles } from '../../core/files.js';
10
+ import { LINK_PATH, PROOF_PATH } from '../../core/prototype.js';
11
+ import { createReview } from '../../core/review/service.js';
12
+ import { memoryKV } from '../../core/review/store.js';
13
+ import { hostCache } from '../../server/hosts.js';
14
+ import { manualDns, wildcardDns } from './dns.js';
15
+ export function fakeHost(older = ['dpl_old1', 'dpl_old2']) {
16
+ let next = 0;
17
+ const fake = {
18
+ calls: [],
19
+ env: new Map(),
20
+ domains: new Map(),
21
+ deployments: older.map((id) => ({ id, state: 'READY' })),
22
+ up: 'all',
23
+ stuck: new Set(),
24
+ verify: [],
25
+ linked: false,
26
+ gate: async () => null,
27
+ host: undefined,
28
+ };
29
+ const log = (what) => fake.calls.push(what);
30
+ const store = (names) => {
31
+ for (const [k, v] of Object.entries(names))
32
+ fake.env.set(k, v);
33
+ };
34
+ fake.host = {
35
+ async link(create) {
36
+ log(`link ${create}`);
37
+ if (fake.linked)
38
+ return 'kept';
39
+ fake.linked = true;
40
+ return create ? 'created' : 'linked';
41
+ },
42
+ async setSecret(name, value) {
43
+ log(`setSecret ${name}`);
44
+ if (fake.env.has(name))
45
+ return 'kept';
46
+ fake.env.set(name, value);
47
+ return 'set';
48
+ },
49
+ async hasEnv(name) {
50
+ log(`hasEnv ${name}`);
51
+ return fake.env.has(name);
52
+ },
53
+ async pullEnv(names) {
54
+ log(`pullEnv ${names.join(',')}`);
55
+ return Object.fromEntries(names.filter((n) => fake.env.has(n)).map((n) => [n, fake.env.get(n)]));
56
+ },
57
+ async provisionDatabase(existing) {
58
+ log(`provisionDatabase${existing ? ` ${existing}` : ''}`);
59
+ if (fake.env.has('KV_REST_API_URL'))
60
+ return 'kept';
61
+ store({ KV_REST_API_URL: 'https://kv.studio.test', KV_REST_API_TOKEN: 'kv-token-xxxxxxxx' });
62
+ return existing ? 'connected' : 'created';
63
+ },
64
+ async disconnectDatabase(name) {
65
+ log(`disconnectDatabase ${name}`);
66
+ fake.env.delete('KV_REST_API_URL');
67
+ fake.env.delete('KV_REST_API_TOKEN');
68
+ return true;
69
+ },
70
+ async provisionFiles(region) {
71
+ log(`provisionFiles ${region}`);
72
+ if (fake.env.has('BLOB_READ_WRITE_TOKEN'))
73
+ return 'kept';
74
+ store({ BLOB_READ_WRITE_TOKEN: 'vercel_blob_rw_xxxxxxxx' });
75
+ return 'created';
76
+ },
77
+ async addDomain(host) {
78
+ log(`addDomain ${host}`);
79
+ const state = fake.domains.get(host) ?? {
80
+ verified: fake.verify.length === 0,
81
+ verification: fake.verify,
82
+ };
83
+ fake.domains.set(host, state);
84
+ return state;
85
+ },
86
+ async removeDomain(host) {
87
+ log(`removeDomain ${host}`);
88
+ return fake.domains.delete(host) ? 'removed' : 'absent';
89
+ },
90
+ async recordFor(host) {
91
+ log(`recordFor ${host}`);
92
+ return 'abc123.vercel-dns-016.com';
93
+ },
94
+ async reaches(host, proof) {
95
+ log(`reaches ${host}`);
96
+ if (fake.up !== 'all' && !fake.up.has(host))
97
+ return false;
98
+ return (await fake.gate(host, proof.ask)) === proof.answer;
99
+ },
100
+ async deploy() {
101
+ log('deploy');
102
+ const d = { id: `dpl_new${++next}`, state: 'READY' };
103
+ fake.deployments.push(d);
104
+ return d;
105
+ },
106
+ async deployments() {
107
+ log('deployments');
108
+ return fake.deployments.map((d) => ({ ...d }));
109
+ },
110
+ async removeDeployment(id) {
111
+ log(`removeDeployment ${id}`);
112
+ if (!fake.stuck.has(id))
113
+ fake.deployments = fake.deployments.filter((d) => d.id !== id);
114
+ },
115
+ };
116
+ return fake;
117
+ }
118
+ const text = (status, body) => new Response(body, { status, headers: { 'content-type': 'text/plain; charset=utf-8' } });
119
+ /**
120
+ * The studio site as a fetch from outside meets it, over the commands' own
121
+ * store, answering as the gate does (vercel/proxy.ts, which `pnpm quality`
122
+ * keeps scripts from importing): a host no client has is the plain 404, the
123
+ * host proof's path answers the proof's answer for its ask, a link page is
124
+ * 200 while the link is live and a POST uses it up, anything else is the
125
+ * signed-out 401. It keeps each host's answer a minute, unknown hosts too,
126
+ * with the gate's own cache (server/hosts.ts).
127
+ */
128
+ export function fakeSite(review) {
129
+ const hosts = hostCache();
130
+ return (async (input, init) => {
131
+ const request = new Request(input, init);
132
+ const url = new URL(request.url);
133
+ const client = await hosts(url.hostname, (h) => review.clientOfHost(h));
134
+ if (!client)
135
+ return text(404, 'Not found.');
136
+ const scope = { client: client.slug };
137
+ const proof = url.pathname.match(PROOF_PATH);
138
+ const answer = proof && request.method === 'GET' ? await review.proofAnswer(scope, proof[1]) : null;
139
+ if (answer)
140
+ return text(200, answer);
141
+ const link = url.pathname.match(LINK_PATH)?.[1];
142
+ if (!link)
143
+ return text(401, 'Sign in with the link the studio sent you.');
144
+ // A POST uses the link up, as the gate's Continue does; a read only looks.
145
+ const by = { post: () => review.useLink(scope, link), read: () => review.peekLink(scope, link) };
146
+ const live = await (request.method === 'POST' ? by.post : by.read)();
147
+ return live ? text(request.method === 'POST' ? 303 : 200, '') : text(404, 'Used.');
148
+ });
149
+ }
150
+ /**
151
+ * Every dependency of the studio and client commands, faked: one host, one
152
+ * review store (one database: a second studio made on it is refused) and one
153
+ * file store. `review` is the test's own view of that store, on the same
154
+ * clock. Cloudflare is a DNS that records each host it is asked to add or
155
+ * remove.
156
+ */
157
+ export function fakeDeps(home, cwd = home) {
158
+ const fake = fakeHost();
159
+ const kv = memoryKV();
160
+ // The store's clock, which a test moves past REUSE_SECONDS instead of waiting.
161
+ const clock = { at: Date.now() };
162
+ const review = createReview(kv, { now: () => new Date(clock.at) });
163
+ const files = memoryFiles();
164
+ // The gate's answer: the host's client, then the proof's answer in its scope.
165
+ fake.gate = async (host, ask) => {
166
+ const client = await review.clientOfHost(host);
167
+ return client ? review.proofAnswer({ client: client.slug }, ask) : null;
168
+ };
169
+ const ensured = [];
170
+ const released = [];
171
+ const perHost = {
172
+ mode: 'per-host',
173
+ ensure: async (host) => {
174
+ ensured.push(host);
175
+ return 'added';
176
+ },
177
+ release: async (host) => {
178
+ released.push(host);
179
+ return { removed: [`CNAME ${host} abc123.vercel-dns-016.com`], left: [] };
180
+ },
181
+ };
182
+ /**
183
+ * The host's DNS as its zone's nameservers and this machine's resolver see
184
+ * it: every host served and seen unless a test empties a set and adds its
185
+ * own, or makes the nameservers unreachable. Each ask goes in `calls`, so a
186
+ * test can say what came before the proof.
187
+ */
188
+ const dns = {
189
+ served: 'all',
190
+ seen: 'all',
191
+ negative: 1800,
192
+ };
193
+ const has = (set, host) => set === 'all' || set.has(host);
194
+ const names = {
195
+ async served(host) {
196
+ fake.calls.push(`served ${host}`);
197
+ if (dns.served === 'unreachable')
198
+ return { state: 'unreachable', zone: 'example.com', negative: null };
199
+ return {
200
+ state: has(dns.served, host) ? 'served' : 'missing',
201
+ zone: 'example.com',
202
+ negative: dns.negative,
203
+ };
204
+ },
205
+ async seen(host) {
206
+ fake.calls.push(`lookup ${host}`);
207
+ return has(dns.seen, host);
208
+ },
209
+ };
210
+ const hosts = [];
211
+ /** pnpm runs, by their first argument (install, update, run), that fail as pnpm does. */
212
+ const pnpmFails = new Set();
213
+ const deps = {
214
+ home,
215
+ cwd,
216
+ host: (dir) => {
217
+ hosts.push(dir);
218
+ return fake.host;
219
+ },
220
+ dns: ({ config }, host) => config.dns.provider === 'vercel'
221
+ ? wildcardDns()
222
+ : config.dns.provider === 'manual'
223
+ ? manualDns((h) => host.recordFor(h))
224
+ : perHost,
225
+ names,
226
+ store: () => kv,
227
+ now: () => new Date(clock.at),
228
+ files: () => files,
229
+ // Installs nothing: an install that passes writes the lockfile, as pnpm does.
230
+ pnpm: (dir, args) => {
231
+ fake.calls.push(`pnpm ${args.join(' ')}`);
232
+ if (pnpmFails.has(args[0]))
233
+ return {
234
+ status: 1,
235
+ stdout: '',
236
+ stderr: 'ERR_PNPM_META_FETCH_FAIL GET https://registry.npmjs.org/react',
237
+ };
238
+ if (args[0] === 'install')
239
+ writeFileSync(join(dir, 'pnpm-lock.yaml'), "lockfileVersion: '9.0'\n");
240
+ return { status: 0, stdout: '', stderr: '' };
241
+ },
242
+ };
243
+ return { deps, fake, kv, review, files, ensured, released, hosts, clock, pnpmFails, dns };
244
+ }
@@ -0,0 +1,87 @@
1
+ import { type Run } from './vercel.ts';
2
+ export type Deployment = {
3
+ id: string;
4
+ state: string;
5
+ };
6
+ export type Verification = {
7
+ type: string;
8
+ domain: string;
9
+ value: string;
10
+ };
11
+ export type DomainState = {
12
+ verified: boolean;
13
+ verification: Verification[];
14
+ };
15
+ /** A host proof from the review store (startProof): the ask goes out, and only the studio's gate sends the answer back. */
16
+ export type Proof = {
17
+ ask: string;
18
+ answer: string;
19
+ };
20
+ export interface Host {
21
+ /** Links the studio folder to its project, creating the project when `create` and it is missing. */
22
+ link(create: boolean): Promise<'created' | 'linked' | 'kept'>;
23
+ /** Sets a production secret that is not there yet, its value on stdin. */
24
+ setSecret(name: string, value: string): Promise<'set' | 'kept'>;
25
+ /** Whether Production has an env var of this name; its value is never read. */
26
+ hasEnv(name: string): Promise<boolean>;
27
+ /** Production values of these names, read into memory, never into a file left behind. */
28
+ pullEnv(names: readonly string[]): Promise<Record<string, string>>;
29
+ /**
30
+ * The review store (Upstash for Redis) when production has none: the
31
+ * team's existing database of the name given, connected, or else a new
32
+ * one, only ever on the free plan. A database serves one studio only:
33
+ * `studio create` checks the one connected is no other studio's before it
34
+ * writes to it (server/mark.ts).
35
+ */
36
+ provisionDatabase(existing?: string): Promise<'created' | 'connected' | 'kept'>;
37
+ /**
38
+ * Takes the team's database of this name off the studio project, its
39
+ * env vars with it, for a `studio create` that connected another studio's.
40
+ * True once Production holds neither store name; false when the
41
+ * disconnect failed or left them, so the refusal can say what is left.
42
+ */
43
+ disconnectDatabase(name: string): Promise<boolean>;
44
+ /** The private file store (Vercel Blob), made and connected when production has none. */
45
+ provisionFiles(region: string): Promise<'created' | 'kept'>;
46
+ /** Puts a host on the studio project, or asks Vercel to verify one already there. */
47
+ addDomain(host: string): Promise<DomainState>;
48
+ removeDomain(host: string): Promise<'removed' | 'absent'>;
49
+ /** The CNAME target Vercel asks a host under another DNS provider to point at. */
50
+ recordFor(host: string): Promise<string>;
51
+ /** Whether the host answers the proof over HTTPS from this studio's own gate, waiting up to a minute. */
52
+ reaches(host: string, proof: Proof): Promise<boolean>;
53
+ /** A production deployment of the studio folder, once it is ready. */
54
+ deploy(): Promise<Deployment>;
55
+ /** Every deployment of the studio project, of any target or state. */
56
+ deployments(): Promise<Deployment[]>;
57
+ removeDeployment(id: string): Promise<void>;
58
+ }
59
+ /**
60
+ * Deploys the studio site and then puts every older deployment out of reach,
61
+ * since a request can name a deployment by its id and be served by that
62
+ * deployment's gate and functions (studios-findings.md, section 1, pinning):
63
+ * a fix, or a rotated secret, is in force only once no older one answers.
64
+ * It deletes nothing unless the project's list holds the new deployment, since
65
+ * one made on another project would leave the list without a live one. It
66
+ * deletes them, then lists the project again and fails unless the new
67
+ * deployment is the only one left.
68
+ */
69
+ export declare function deployAlone(host: Host): Promise<{
70
+ deployment: string;
71
+ removed: string[];
72
+ }>;
73
+ type VercelOptions = {
74
+ dir: string;
75
+ team: string;
76
+ project: string;
77
+ run?: (cwd: string, args: string[], input?: string) => Run | null;
78
+ fetcher?: typeof fetch;
79
+ wait?: (ms: number) => Promise<unknown>;
80
+ };
81
+ /** The env vars the Vercel CLI (59.x) reads to pick a project or team over the folder's link and `--scope`. */
82
+ export declare const PICKERS: string[];
83
+ /** The Vercel adapter. Every call goes through `call`, which adds `--scope <team>`. */
84
+ export declare function vercelHost({ dir, team, project, run, fetcher, wait, }: VercelOptions): Host;
85
+ /** Vercel's first-ranked CNAME for a host, without its trailing dot. */
86
+ export declare function cnameTarget(config: Record<string, any> | null): string;
87
+ export {};