@milesroxas/design-hub 0.12.0 → 0.13.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 (51) hide show
  1. package/README.md +4 -4
  2. package/core/review/service.test.ts +39 -38
  3. package/core/review/store.ts +2 -2
  4. package/core/review/suite-kv.ts +17 -0
  5. package/dist/core/review/store.js +2 -2
  6. package/dist/core/review/suite-kv.d.ts +7 -0
  7. package/dist/core/review/suite-kv.js +12 -0
  8. package/dist/scripts/client.js +3 -3
  9. package/dist/scripts/lib/cli.js +5 -0
  10. package/dist/scripts/lib/fake-host.d.ts +1 -2
  11. package/dist/scripts/lib/fake-host.js +20 -9
  12. package/dist/scripts/lib/host.d.ts +13 -25
  13. package/dist/scripts/lib/host.js +15 -324
  14. package/dist/scripts/lib/studio-deps.d.ts +16 -0
  15. package/dist/scripts/lib/studio-deps.js +41 -16
  16. package/dist/scripts/lib/studios.d.ts +14 -4
  17. package/dist/scripts/lib/studios.js +23 -5
  18. package/dist/scripts/lib/vercel-host.d.ts +17 -0
  19. package/dist/scripts/lib/vercel-host.js +410 -0
  20. package/dist/scripts/share.js +3 -2
  21. package/dist/scripts/studio.d.ts +8 -2
  22. package/dist/scripts/studio.js +38 -14
  23. package/dist/server/api.d.ts +6 -5
  24. package/dist/server/api.js +12 -10
  25. package/dist/server/gate.d.ts +97 -0
  26. package/dist/server/gate.js +537 -0
  27. package/dist/server/mark.d.ts +17 -0
  28. package/dist/server/mark.js +46 -1
  29. package/dist/server/session.d.ts +2 -1
  30. package/dist/server/session.js +2 -2
  31. package/dist/server/stores/backends.d.ts +22 -0
  32. package/dist/server/stores/backends.js +96 -0
  33. package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
  34. package/dist/server/stores/index.d.ts +41 -0
  35. package/dist/server/stores/index.js +63 -0
  36. package/dist/server/stores/pglite.d.ts +16 -0
  37. package/dist/server/stores/pglite.js +98 -0
  38. package/dist/server/stores/postgres.d.ts +63 -0
  39. package/dist/server/stores/postgres.js +153 -0
  40. package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
  41. package/dist/vercel/files.js +1 -1
  42. package/dist/vercel/proxy.d.ts +6 -82
  43. package/dist/vercel/proxy.js +18 -525
  44. package/dist/vite/plugin.js +1 -1
  45. package/package.json +3 -1
  46. package/skills/miles-deploy/SKILL.md +2 -1
  47. package/templates/studio/.env.example +3 -0
  48. package/templates/studio/README.md +13 -0
  49. package/templates/studio/package.json +1 -1
  50. /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
  51. /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
package/README.md CHANGED
@@ -72,9 +72,9 @@ A speculation rule in `shell/index.html` prefetches a prototype's page when its
72
72
 
73
73
  ## The studio site, and signing in
74
74
 
75
- `miles-deploy` owns the steps; this is what they rest on. A project has no deployment of its own. A **studio** is one site on one hosting account, with one database (Upstash for Redis), one private file store (Vercel Blob), one `GATE_SECRET` and one apex domain, that serves every client of the studio; two studios share none of these. Its folder, made by `design-hub studio create` from `templates/studio/`, is thin like a project: one-line `proxy.ts`, `api/review.ts` and `api/files.ts` from `./vercel`, a `vercel.json`, a `studio.json` (id, slug, apex, team, DNS; no secret) and a gitignored `.env`. A machine finds its studios by slug in `~/.design-hub/studios.json`. `design-hub signin` starts `vercel login`'s device sign-in in the background and returns its link, so the user clicks instead of typing.
75
+ `miles-deploy` owns the steps; this is what they rest on. A project has no deployment of its own. A **studio** is one site on one hosting account, with one database (Upstash for Redis, or Postgres: Neon, Supabase or any other, chosen at `studio create --kv`), one private file store (Vercel Blob), one `GATE_SECRET` and one apex domain, that serves every client of the studio; two studios share none of these. Its folder, made by `design-hub studio create` from `templates/studio/`, is thin like a project: one-line `proxy.ts`, `api/review.ts` and `api/files.ts` from `./vercel`, a `vercel.json`, a `studio.json` (id, slug, apex, team, DNS, the database's kind and the host; no secret) and a gitignored `.env`. A machine finds its studios by slug in `~/.design-hub/studios.json`. `design-hub signin` starts `vercel login`'s device sign-in in the background and returns its link, so the user clicks instead of typing.
76
76
 
77
- A client's address is `<client>.<apex>` (or a codename under the apex, `client add --host`), and a project is `/<project>/` below it. `design-hub project link` writes `.design-hub/studio.json` in the project (studio, its id, client, project; committed). `design-hub share` builds the project with base `/<project>/`, uploads `dist/` under `c/<client>/<project>/<build>/` in the file store with a new random build id, checks every file is there, and only then points the project's record at that build, so a half-finished upload is never served; the live build and the two before it are kept. Every path on every client's host sits behind the gate, enforced on the server by Vercel Routing Middleware (`createStudioProxy` in `vercel/proxy.ts`, which the studio's `vercel.json` names as `proxy.entrypoint`). The host names the client, from one store lookup kept a minute per instance; a host no client has is the plain 404 before anything else is read. The files function and the review function run the gate's checks again themselves, in the order host, session, project, so neither trusts anything the gate set. A client-side gate was rejected because the bundle and the state behind it would still download.
77
+ A client's address is `<client>.<apex>` (or a codename under the apex, `client add --host`), and a project is `/<project>/` below it. `design-hub project link` writes `.design-hub/studio.json` in the project (studio, its id, client, project; committed). `design-hub share` builds the project with base `/<project>/`, uploads `dist/` under `c/<client>/<project>/<build>/` in the file store with a new random build id, checks every file is there, and only then points the project's record at that build, so a half-finished upload is never served; the live build and the two before it are kept. Every path on every client's host sits behind the gate, enforced on the server by the gate (`server/gate.ts`, free of any host: it answers each request with a decision), run as Vercel Routing Middleware by its adapter (`createStudioProxy` in `vercel/proxy.ts`, which the studio's `vercel.json` names as `proxy.entrypoint`). The host names the client, from one store lookup kept a minute per instance; a host no client has is the plain 404 before anything else is read. The files function and the review function run the gate's checks again themselves, in the order host, session, project, so neither trusts anything the gate set. A client-side gate was rejected because the bundle and the state behind it would still download.
78
78
 
79
79
  Studio people have a host of their own, `studio.<apex>`, which `studio create` (or `studio deploy`, for an older studio) adds and proves as it proves a client's. Only Studio people sign in there; a Client, a cookie of a client's host, or none gets that host's sign-in page, word for word a client host's. Its page is the studio hub (`shell/studio/StudioHub.tsx`, built apart from the client hub under `_studio/`, so no client's host serves it): every client with its projects, an **Open** that hands the person off to the client's host (a one-time record in the client's keys, claimed once there within 60 seconds, which sets that host's own Studio cookie), and a **People** panel per client and one for the studio's own people, the review panel's People controls. Its review API answers `me`, `clients` and the people ops, each naming the client it is for with `client`, which the service checks as the handoff does (known, added, not archived) and answers as an unknown client otherwise; without `client` an op is the studio's own people. No client's host reads a `client` parameter.
80
80
 
@@ -95,6 +95,6 @@ The studio site reads its env from the Vercel project, never committed: `GATE_SE
95
95
 
96
96
  Every signed-in person can comment as themselves. The People tab lists each person with their team, kind and when their access ends, and says how links behave where Studio shares them.
97
97
 
98
- The panel (`shell/review/`) mounts on every prototype page in its own React root, so a prototype that throws never takes it down. A comment carries the prototype, the URL, every dial panel's values (`snapshotDials`), and the element it was pinned to as a selector path with an offset; Show state reopens the URL and sets the dials back. Pins draw over their elements and below the Controls panel. The API is one endpoint, `<project>/api/review` (`server/api.ts`, reached on a studio site through its one-line `api/review.ts`, scoped by the host's client and the project the path names): queries by GET, commands by POST with a JSON body, same-origin only. The rules (who may delete, what a valid comment is, which client a person may act in) live once, in `core/review/`, behind a small storage port: the studio's Upstash Redis over REST, or a file in `.design-hub/review.json` under `design-hub dev`, where whoever runs the server is a local Studio person in one fixed scope. Every key is built by one function from a scope, under the prefix `hub4:`: the studio's (`studio:people`, `clients`, `hosts`, …), a client's (`c:<client>:people`, `:links`, `:projects`, …) and a project's (`c:<client>:p:<project>:comments:<prototype>`, …), so no op reaches another client's records.
98
+ The panel (`shell/review/`) mounts on every prototype page in its own React root, so a prototype that throws never takes it down. A comment carries the prototype, the URL, every dial panel's values (`snapshotDials`), and the element it was pinned to as a selector path with an offset; Show state reopens the URL and sets the dials back. Pins draw over their elements and below the Controls panel. The API is one endpoint, `<project>/api/review` (`server/api.ts`, reached on a studio site through its one-line `api/review.ts`, scoped by the host's client and the project the path names): queries by GET, commands by POST with a JSON body, same-origin only. The rules (who may delete, what a valid comment is, which client a person may act in) live once, in `core/review/`, behind a small storage port: the studio's database (`server/stores/`: Upstash Redis over REST, or Postgres in one table of key, field and value), or a file in `.design-hub/review.json` under `design-hub dev`, where whoever runs the server is a local Studio person in one fixed scope. Every key is built by one function from a scope, under the prefix `hub4:`: the studio's (`studio:people`, `clients`, `hosts`, …), a client's (`c:<client>:people`, `:links`, `:projects`, …) and a project's (`c:<client>:p:<project>:comments:<prototype>`, …), so no op reaches another client's records.
99
99
 
100
- `design-hub dev` serves the prototypes open, with the review panel on the local store. The gate and the studio's functions are covered by `pnpm test` (`vercel/proxy.test.ts`, against an in-memory Upstash and file store).
100
+ `design-hub dev` serves the prototypes open, with the review panel on the local store. The gate and the studio's functions are covered by `pnpm test` (`vercel/gate.test.ts`, against an in-memory Upstash and file store, and again in `vercel/gate.postgres.test.ts` on an embedded Postgres).
@@ -2,7 +2,8 @@ import assert from 'node:assert/strict';
2
2
  import { beforeEach, test } from 'node:test';
3
3
  import { type Anchor, accessEnd, type Comment, type Person, type Scope, threadLabel } from './model.ts';
4
4
  import { createReview, REUSE_SECONDS, type Review, ReviewError } from './service.ts';
5
- import { type KV, keyOf, memoryKV, type Snapshot } from './store.ts';
5
+ import { type KV, keyOf, type Snapshot } from './store.ts';
6
+ import { suiteKV } from './suite-kv.ts';
6
7
 
7
8
  const anchor: Anchor = {
8
9
  prototype: 'sample-list',
@@ -55,7 +56,7 @@ const LIVE = live('acme', 'bravo');
55
56
 
56
57
  beforeEach(async () => {
57
58
  clock = Date.parse('2030-01-01T00:00:00Z');
58
- review = createReview(memoryKV(LIVE), {
59
+ review = createReview(suiteKV(LIVE), {
59
60
  now: () => {
60
61
  clock += 1000;
61
62
  return new Date(clock);
@@ -144,7 +145,7 @@ test('a revoke ends every session and link, and a revoked person gets no new lin
144
145
  });
145
146
 
146
147
  test('a store failure before the claim leaves the link usable', async () => {
147
- const kv = memoryKV(LIVE);
148
+ const kv = suiteKV(LIVE);
148
149
  let failPeople = false;
149
150
  const flaky: KV = {
150
151
  ...kv,
@@ -165,7 +166,7 @@ test('a store failure before the claim leaves the link usable', async () => {
165
166
  test('the store never holds a link token in clear', async () => {
166
167
  let seen: Snapshot = { hashes: {}, sets: {} };
167
168
  const r = createReview(
168
- memoryKV(LIVE, (snapshot) => {
169
+ suiteKV(LIVE, (snapshot) => {
169
170
  seen = snapshot;
170
171
  }),
171
172
  );
@@ -183,7 +184,7 @@ test('the store never holds a link token in clear', async () => {
183
184
 
184
185
  test('a revoke writes revocation and epoch, never the profile, before it drops their links', async () => {
185
186
  const writes: string[] = [];
186
- const kv = memoryKV(LIVE);
187
+ const kv = suiteKV(LIVE);
187
188
  const watched: KV = {
188
189
  ...kv,
189
190
  hset: async (key, field, value) => {
@@ -216,7 +217,7 @@ test('a revoke writes revocation and epoch, never the profile, before it drops t
216
217
  * until the test releases it: a real interleaving, not two calls in a row.
217
218
  */
218
219
  function pausingKV() {
219
- const kv = memoryKV(LIVE);
220
+ const kv = suiteKV(LIVE);
220
221
  let arm = false;
221
222
  let reached = () => {};
222
223
  let release = () => {};
@@ -330,7 +331,7 @@ for (const [what, run] of [
330
331
  ['a sign out everywhere', (r: Review, root: Person, id: string) => r.signOutEverywhere(A, root, id)],
331
332
  ] as const)
332
333
  test(`a link used while ${what} lands signs nobody in`, async () => {
333
- const { store, arm, pauses } = pausing(memoryKV(LIVE), {
334
+ const { store, arm, pauses } = pausing(suiteKV(LIVE), {
334
335
  claim: { method: 'hdel', key: keyOf(A, 'links') },
335
336
  drop: { method: 'hgetall', key: keyOf(A, 'links') },
336
337
  });
@@ -353,7 +354,7 @@ for (const [what, run] of [
353
354
  });
354
355
 
355
356
  test('any revocation record means revoked, and an access end that does not parse means no access', async () => {
356
- const kv = memoryKV(LIVE);
357
+ const kv = suiteKV(LIVE);
357
358
  const r = createReview(kv);
358
359
  const root = { ...studio, id: 'cli' };
359
360
  const a = (await r.addPerson(A, root, { name: 'Rae', team: 'Acme' })).person;
@@ -369,7 +370,7 @@ test('any revocation record means revoked, and an access end that does not parse
369
370
  });
370
371
 
371
372
  test('a profile that is not a well-formed person is no person, and listPeople skips it', async () => {
372
- const kv = memoryKV(LIVE);
373
+ const kv = suiteKV(LIVE);
373
374
  const r = createReview(kv);
374
375
  const root = { ...studio, id: 'cli' };
375
376
  const { person } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
@@ -411,7 +412,7 @@ test('two sign outs everywhere at once both end the earlier sessions', async ()
411
412
  });
412
413
 
413
414
  test('a profile write never carries revocation or epoch', async () => {
414
- const kv = memoryKV(LIVE);
415
+ const kv = suiteKV(LIVE);
415
416
  const r = createReview(kv);
416
417
  const root = { ...studio, id: 'cli' };
417
418
  const { person } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
@@ -543,7 +544,7 @@ test('records under an older key prefix are never read: the unscoped hub3 store
543
544
  );
544
545
  const link = JSON.stringify({ person: 'old', expiresAt: '2999-01-01T00:00:00Z' });
545
546
  const comment = JSON.stringify({ id: 'x', thread: 'x', audience: 'client', anchor });
546
- const kv = memoryKV({
547
+ const kv = suiteKV({
547
548
  hashes: {
548
549
  ...LIVE.hashes,
549
550
  'hub3:people': { old: JSON.stringify(profile) },
@@ -615,7 +616,7 @@ test('bad input names every problem', async () => {
615
616
 
616
617
  test('triage runs on add, and a failing triager never loses the comment', async () => {
617
618
  const seen: string[][] = [];
618
- const triaged = createReview(memoryKV(LIVE), {
619
+ const triaged = createReview(suiteKV(LIVE), {
619
620
  triage: async ({ states }) => {
620
621
  seen.push(states);
621
622
  return { kind: 'bug', state: states[0] ?? null, by: 'jev', confidence: 0.9 };
@@ -629,7 +630,7 @@ test('triage runs on add, and a failing triager never loses the comment', async
629
630
  });
630
631
  assert.deepEqual(seen, [['Empty', 'Real list']]);
631
632
  assert.equal(c.triage?.kind, 'bug');
632
- const failing = createReview(memoryKV(LIVE), { triage: async () => Promise.reject(new Error('down')) });
633
+ const failing = createReview(suiteKV(LIVE), { triage: async () => Promise.reject(new Error('down')) });
633
634
  await failing.setAudience(A, studio, anchor.prototype, 'client');
634
635
  assert.equal((await failing.add(A, client, { body: 'Still saved', anchor })).triage, null);
635
636
  });
@@ -795,7 +796,7 @@ test('numbers count per prototype', async () => {
795
796
  });
796
797
 
797
798
  test('the internal count starts past internal threads numbered while the two shared one count', async () => {
798
- const kv = memoryKV(LIVE);
799
+ const kv = suiteKV(LIVE);
799
800
  const r = createReview(kv);
800
801
  const root = { ...studio, id: 'cli' };
801
802
  // Under one shared count: internal 1, client-facing 2, internal 3.
@@ -830,7 +831,7 @@ test('the internal count starts past internal threads numbered while the two sha
830
831
  });
831
832
 
832
833
  test('a stored comment without an audience is internal, and one without a number has none', async () => {
833
- const kv = memoryKV(LIVE);
834
+ const kv = suiteKV(LIVE);
834
835
  const r = createReview(kv);
835
836
  const root = { ...studio, id: 'cli' };
836
837
  const rae = (await r.addPerson(A, root, { name: 'Rae', team: 'Acme' })).person;
@@ -878,7 +879,7 @@ test('a prototype is internal until Studio marks it client-facing, and internal
878
879
  });
879
880
 
880
881
  test('only client-facing is stored: internal deletes the field, and absent or garbage reads as internal', async () => {
881
- const kv = memoryKV(LIVE);
882
+ const kv = suiteKV(LIVE);
882
883
  const own = createReview(kv);
883
884
  await own.setAudience(A, studio, 'secret-plan', 'client');
884
885
  assert.deepEqual(await kv.hgetall(keyOf(A, 'audiences')), { 'secret-plan': 'client' });
@@ -1011,7 +1012,7 @@ test('a Client acting on a client-facing thread of an internal prototype is told
1011
1012
 
1012
1013
  // Slice 6: what Studio marks on a prototype's dials and variants.
1013
1014
  test('controls: Studio marks a dial either way and a variant on or off, one field each', async () => {
1014
- const kv = memoryKV(LIVE);
1015
+ const kv = suiteKV(LIVE);
1015
1016
  const own = createReview(kv);
1016
1017
  const mark = (kind: string, id: string, audience: string) =>
1017
1018
  own.setControl(A, studio, { prototype: 'sample-list', kind, id, audience });
@@ -1234,13 +1235,13 @@ const ACTORLESS = [
1234
1235
 
1235
1236
  test('every op of the review service is in the cross-client test or named as having no actor', () => {
1236
1237
  const listed = [...OPS.map(([name]) => name), ...STUDIO_WIDE.map(([name]) => name), ...ACTORLESS].sort();
1237
- assert.deepEqual(Object.keys(createReview(memoryKV())).sort(), listed);
1238
+ assert.deepEqual(Object.keys(createReview(suiteKV())).sort(), listed);
1238
1239
  });
1239
1240
 
1240
1241
  test('every op from client A’s person on client B’s scope answers as an unknown id does, and changes nothing', async () => {
1241
1242
  let snapshot = '';
1242
1243
  review = createReview(
1243
- memoryKV(live('acme'), (s) => {
1244
+ suiteKV(live('acme'), (s) => {
1244
1245
  snapshot = JSON.stringify(s);
1245
1246
  }),
1246
1247
  );
@@ -1274,7 +1275,7 @@ const ON_RECORD = new Set(['addClient', 'clientRecord', 'archiveClient', 'forget
1274
1275
  test('every op in a client archived, forgotten or never added answers as an unknown id, and writes nothing', async () => {
1275
1276
  let snapshot = '';
1276
1277
  review = createReview(
1277
- memoryKV(undefined, (s) => {
1278
+ suiteKV(undefined, (s) => {
1278
1279
  snapshot = JSON.stringify(s);
1279
1280
  }),
1280
1281
  );
@@ -1301,7 +1302,7 @@ test('every op in a client archived, forgotten or never added answers as an unkn
1301
1302
 
1302
1303
  test('a client added over keys an earlier client of its slug left starts clean, every kind of key', async () => {
1303
1304
  let seen: Snapshot = { hashes: {}, sets: {} };
1304
- const kv = memoryKV(undefined, (s) => {
1305
+ const kv = suiteKV(undefined, (s) => {
1305
1306
  seen = s;
1306
1307
  });
1307
1308
  const r = createReview(kv);
@@ -1373,7 +1374,7 @@ test('a client added over keys an earlier client of its slug left starts clean,
1373
1374
  });
1374
1375
 
1375
1376
  test('the local store’s client, which has no record, runs every op as before, and no studio may add a client by its name', async () => {
1376
- const kv = memoryKV();
1377
+ const kv = suiteKV();
1377
1378
  const r = createReview(kv);
1378
1379
  const SITE: Scope = { client: 'site', project: 'site' };
1379
1380
  const ed = (
@@ -1454,7 +1455,7 @@ test('a project’s comments, numbers, marks and audiences are its own, the same
1454
1455
  test('every record lives under its own scope’s keys, and Studio’s under the studio’s', async () => {
1455
1456
  let seen: Snapshot = { hashes: {}, sets: {} };
1456
1457
  review = createReview(
1457
- memoryKV(undefined, (s) => {
1458
+ suiteKV(undefined, (s) => {
1458
1459
  seen = s;
1459
1460
  }),
1460
1461
  );
@@ -1498,7 +1499,7 @@ test('every record lives under its own scope’s keys, and Studio’s under the
1498
1499
  });
1499
1500
 
1500
1501
  test('a profile stored at the wrong home is no person: a Client with the studio, Studio with a client', async () => {
1501
- const kv = memoryKV(LIVE);
1502
+ const kv = suiteKV(LIVE);
1502
1503
  const r = createReview(kv);
1503
1504
  const rae = (await r.addPerson(A, studio, { name: 'Rae', team: 'Acme' })).person;
1504
1505
  const profile = JSON.parse((await kv.hget(keyOf(A, 'people'), rae.id)) ?? '{}');
@@ -1515,7 +1516,7 @@ test('a profile stored at the wrong home is no person: a Client with the studio,
1515
1516
  });
1516
1517
 
1517
1518
  test('a host names its client while the client is live and its records agree; anything else is no client', async () => {
1518
- review = createReview(memoryKV());
1519
+ review = createReview(suiteKV());
1519
1520
  const acme = await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1520
1521
  assert.deepEqual(acme, {
1521
1522
  slug: 'acme',
@@ -1531,7 +1532,7 @@ test('a host names its client while the client is live and its records agree; an
1531
1532
  for (const host of ['', 'nope.studio.test', 'studio.test', 'acme.studio.test.evil', 'a b'])
1532
1533
  assert.equal(await review.clientOfHost(host), null, host);
1533
1534
  // A host table entry the client's own record does not agree with, or an archived client, is no client.
1534
- const kv = memoryKV();
1535
+ const kv = suiteKV();
1535
1536
  const r = createReview(kv);
1536
1537
  await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1537
1538
  await kv.hset(keyOf('studio', 'hosts'), 'alias.studio.test', 'acme');
@@ -1546,7 +1547,7 @@ test('a host names its client while the client is live and its records agree; an
1546
1547
  });
1547
1548
 
1548
1549
  test('a client is Studio’s to add, with a slug and a host nobody has, and never a name the studio keeps', async () => {
1549
- review = createReview(memoryKV());
1550
+ review = createReview(suiteKV());
1550
1551
  await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1551
1552
  await rejects(review.addClient(studio, { slug: 'acme', host: 'other.studio.test', name: 'A' }), 'invalid');
1552
1553
  await rejects(review.addClient(studio, { slug: 'other', host: 'acme.studio.test', name: 'A' }), 'invalid');
@@ -1562,7 +1563,7 @@ test('a client is Studio’s to add, with a slug and a host nobody has, and neve
1562
1563
  });
1563
1564
 
1564
1565
  test('a project is its client’s: listed with a build, opened only from its own client', async () => {
1565
- review = createReview(memoryKV());
1566
+ review = createReview(suiteKV());
1566
1567
  const acme = { client: 'acme' };
1567
1568
  await rejects(review.addProject(acme, studio, { slug: 'rebrand', name: 'Rebrand' }), 'not_found');
1568
1569
  await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
@@ -1613,7 +1614,7 @@ test('a project is its client’s: listed with a build, opened only from its own
1613
1614
  test('a Client is refused every studio-wide op, and a signed-out caller is unauthorized, with nothing written', async () => {
1614
1615
  let snapshot = '';
1615
1616
  review = createReview(
1616
- memoryKV(LIVE, (x) => {
1617
+ suiteKV(LIVE, (x) => {
1617
1618
  snapshot = JSON.stringify(x);
1618
1619
  }),
1619
1620
  );
@@ -1698,7 +1699,7 @@ test('the People tab’s default access reads and sets the same value: the clien
1698
1699
  });
1699
1700
 
1700
1701
  test('of two adds at once, one gets the slug and one the host; the other is refused and holds nothing', async () => {
1701
- review = createReview(memoryKV());
1702
+ review = createReview(suiteKV());
1702
1703
  const make = (slug: string, host: string) => outcome(review.addClient(studio, { slug, host, name: slug }));
1703
1704
  const sameSlug = await Promise.all([make('acme', 'one.studio.test'), make('acme', 'two.studio.test')]);
1704
1705
  assert.equal(sameSlug.filter((o) => 'value' in o).length, 1);
@@ -1720,7 +1721,7 @@ test('of two adds at once, one gets the slug and one the host; the other is refu
1720
1721
  });
1721
1722
 
1722
1723
  test('archiving a client removes every key it has, so the slug and host start clean when added again', async () => {
1723
- const kv = memoryKV();
1724
+ const kv = suiteKV();
1724
1725
  const tick = () => {
1725
1726
  clock += 1000;
1726
1727
  return new Date(clock);
@@ -1811,7 +1812,7 @@ test('archiving a client removes every key it has, so the slug and host start cl
1811
1812
  });
1812
1813
 
1813
1814
  test('an archive keeps a host another client has, and frees a slug or host whose add stopped part way', async () => {
1814
- const kv = memoryKV();
1815
+ const kv = suiteKV();
1815
1816
  let now = Date.parse('2030-01-01T00:00:00Z');
1816
1817
  const r = createReview(kv, { now: () => new Date(now) });
1817
1818
  const record = (slug: string, host: string) =>
@@ -1871,7 +1872,7 @@ test('an archive keeps a host another client has, and frees a slug or host whose
1871
1872
  });
1872
1873
 
1873
1874
  test('a host proof is Studio’s to start and end, answers only its own ask in its own client, and keeps no ask', async () => {
1874
- const kv = memoryKV();
1875
+ const kv = suiteKV();
1875
1876
  const r = createReview(kv);
1876
1877
  await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1877
1878
  await r.addClient(studio, { slug: 'bravo', host: 'bravo.studio.test', name: 'Bravo' });
@@ -1949,7 +1950,7 @@ for (const lands of ['during the archive', 'after the forget'] as const)
1949
1950
  return new Date(at);
1950
1951
  },
1951
1952
  };
1952
- const kv = memoryKV();
1953
+ const kv = suiteKV();
1953
1954
  const held = holdingKV(kv);
1954
1955
  const site = createReview(held.kv, deps);
1955
1956
  const cli = createReview(kv, deps);
@@ -1993,7 +1994,7 @@ for (const lands of ['during the archive', 'after the forget'] as const)
1993
1994
  test('a sweep reaches its own scope’s keys only: never acme-two’s, a sibling project’s, the studio’s or the directory’s', async () => {
1994
1995
  let at = Date.parse('2030-01-01T00:00:00Z');
1995
1996
  let seen: Snapshot = { hashes: {}, sets: {} };
1996
- const kv = memoryKV(undefined, (s) => {
1997
+ const kv = suiteKV(undefined, (s) => {
1997
1998
  seen = s;
1998
1999
  });
1999
2000
  const r = createReview(kv, {
@@ -2078,7 +2079,7 @@ test('a sweep reaches its own scope’s keys only: never acme-two’s, a sibling
2078
2079
 
2079
2080
  test('the local store is never swept: archive and forget of its client change nothing, and a project added in it deletes nothing', async () => {
2080
2081
  let snapshot = '';
2081
- const kv = memoryKV(undefined, (s) => {
2082
+ const kv = suiteKV(undefined, (s) => {
2082
2083
  snapshot = JSON.stringify(s);
2083
2084
  });
2084
2085
  const r = createReview(kv);
@@ -2104,7 +2105,7 @@ test('the local store is never swept: archive and forget of its client change no
2104
2105
  });
2105
2106
 
2106
2107
  test('a client default set while the client’s record goes is refused, and never lands as the studio’s', async () => {
2107
- const kv = memoryKV();
2108
+ const kv = suiteKV();
2108
2109
  const r = createReview(kv);
2109
2110
  await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
2110
2111
  // The record is there for the live check, and gone by the time the default is written: a forget landed between.
@@ -2155,7 +2156,7 @@ test('a project points only at a build started for it, never one started before
2155
2156
 
2156
2157
  /** A store with acme added (its host proven) and bravo still waiting for its host, on a clock a test moves. */
2157
2158
  async function handoffStudio() {
2158
- const kv = memoryKV();
2159
+ const kv = suiteKV();
2159
2160
  let t = Date.parse('2030-01-01T00:00:00Z');
2160
2161
  const r = createReview(kv, { now: () => new Date(t) });
2161
2162
  const ed = await r.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
@@ -1,6 +1,6 @@
1
1
  // The one storage port the review service needs: a few Redis-shaped hash
2
- // and set operations. Production is Upstash Redis over REST
3
- // (server/upstash.ts); dev and tests use the in-memory store below, which
2
+ // and set operations. A studio's is Upstash Redis over REST or Postgres
3
+ // (server/stores/); dev and tests use the in-memory store below, which
4
4
  // the dev server persists to a file (server/file-kv.ts).
5
5
  import { SLUG } from '../feature.ts';
6
6
  import { isClientSlug, isProjectSlug } from '../prototype.ts';
@@ -0,0 +1,17 @@
1
+ // The store the service suite (service.test.ts) runs on: the memory store,
2
+ // unless a test file picks another before it loads the suite, as
3
+ // server/stores/service.postgres.test.ts picks Postgres. One signature for
4
+ // both: the records to start with, and what to call after each write.
5
+ import { type KV, memoryKV, type Snapshot } from './store.ts';
6
+
7
+ type Make = (initial?: Snapshot, onChange?: (s: Snapshot) => void) => KV;
8
+
9
+ let make: Make = memoryKV;
10
+
11
+ /** A new store for one test, holding `initial`. */
12
+ export const suiteKV: Make = (initial, onChange) => make(initial, onChange);
13
+
14
+ /** For a test file that runs the service suite on another store: call before the suite loads. */
15
+ export function runSuiteOn(other: Make): void {
16
+ make = other;
17
+ }
@@ -1,6 +1,6 @@
1
1
  // The one storage port the review service needs: a few Redis-shaped hash
2
- // and set operations. Production is Upstash Redis over REST
3
- // (server/upstash.ts); dev and tests use the in-memory store below, which
2
+ // and set operations. A studio's is Upstash Redis over REST or Postgres
3
+ // (server/stores/); dev and tests use the in-memory store below, which
4
4
  // the dev server persists to a file (server/file-kv.ts).
5
5
  import { SLUG } from '../feature.js';
6
6
  import { isClientSlug, isProjectSlug } from '../prototype.js';
@@ -0,0 +1,7 @@
1
+ import { type KV, type Snapshot } from './store.ts';
2
+ type Make = (initial?: Snapshot, onChange?: (s: Snapshot) => void) => KV;
3
+ /** A new store for one test, holding `initial`. */
4
+ export declare const suiteKV: Make;
5
+ /** For a test file that runs the service suite on another store: call before the suite loads. */
6
+ export declare function runSuiteOn(other: Make): void;
7
+ export {};
@@ -0,0 +1,12 @@
1
+ // The store the service suite (service.test.ts) runs on: the memory store,
2
+ // unless a test file picks another before it loads the suite, as
3
+ // server/stores/service.postgres.test.ts picks Postgres. One signature for
4
+ // both: the records to start with, and what to call after each write.
5
+ import { memoryKV } from './store.js';
6
+ let make = memoryKV;
7
+ /** A new store for one test, holding `initial`. */
8
+ export const suiteKV = (initial, onChange) => make(initial, onChange);
9
+ /** For a test file that runs the service suite on another store: call before the suite loads. */
10
+ export function runSuiteOn(other) {
11
+ make = other;
12
+ }
@@ -28,7 +28,7 @@ import { REUSE_SECONDS } from '../core/review/service.js';
28
28
  import { localStudio } from '../server/local.js';
29
29
  import { CliError } from './lib/cli.js';
30
30
  import { pointHost, waitServed } from './lib/dns.js';
31
- import { openReview, realDeps, recordOf } from './lib/studio-deps.js';
31
+ import { openFiles, openReview, realDeps, recordOf } from './lib/studio-deps.js';
32
32
  import { clientHost, findStudio, studioHost } from './lib/studios.js';
33
33
  /** S9: what a per-host or manual record makes public. */
34
34
  const PUBLIC_HOST = 'With per-host DNS a client’s host is public in certificate logs from the moment it is added. To keep the client’s name out of them, add it with --host <codename>.';
@@ -171,7 +171,7 @@ async function add(ctx) {
171
171
  // while this one waited on the host, and its projects' builds are live.
172
172
  const current = await recordOf(review, slug);
173
173
  if (current && !isAdded(current))
174
- await clearPrefix(deps.files(studio), clientPrefix(slug));
174
+ await clearPrefix(await openFiles(deps, studio), clientPrefix(slug));
175
175
  await prove(ctx, host, client, dns);
176
176
  const at = (token) => `https://${client.host}/invite/${token}`;
177
177
  try {
@@ -234,7 +234,7 @@ async function archive(ctx) {
234
234
  // leaves the client readable; the record stays, archived, until they are done, so a run again
235
235
  // still knows the host and each step finds what is left.
236
236
  const archived = await review.archiveClient(scope, actor());
237
- const files = await clearPrefix(deps.files(studio), clientPrefix(slug));
237
+ const files = await clearPrefix(await openFiles(deps, studio), clientPrefix(slug));
238
238
  const record = archived?.client ?? null;
239
239
  const wildcard = studio.config.dns.mode === 'wildcard';
240
240
  // Another client's host, or one a live client's record names, is never taken off.
@@ -6,6 +6,7 @@
6
6
  // schema, so an agent discovers the commands without reading their source.
7
7
  import { parseArgs } from 'node:util';
8
8
  import { MarkError } from '../../server/mark.js';
9
+ import { closeStores } from '../../server/stores/index.js';
9
10
  /** A failure the caller can act on: the message, and the fix when there is one. */
10
11
  export class CliError extends Error {
11
12
  detail;
@@ -87,4 +88,8 @@ export async function runCommand(spec, main, argv = process.argv.slice(2)) {
87
88
  print({ ok: false, error: error instanceof Error ? error.message : String(error), ...detail });
88
89
  process.exitCode = 1;
89
90
  }
91
+ finally {
92
+ // A Postgres store's idle connections would keep the process up after its one JSON object.
93
+ await closeStores();
94
+ }
90
95
  }
@@ -21,8 +21,7 @@ export type FakeHost = {
21
21
  export declare function fakeHost(older?: string[]): FakeHost;
22
22
  /**
23
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
24
+ * store, answering as the gate does (server/gate.ts), in short: a host no client has is the plain 404, the
26
25
  * host proof's path answers the proof's answer for its ask, a link page is
27
26
  * 200 while the link is live and a POST uses it up, anything else is the
28
27
  * signed-out 401. It keeps each host's answer a minute, unknown hosts too,
@@ -13,6 +13,15 @@ import { createReview } from '../../core/review/service.js';
13
13
  import { memoryKV } from '../../core/review/store.js';
14
14
  import { hostCache } from '../../server/hosts.js';
15
15
  import { manualDns, wildcardDns } from './dns.js';
16
+ import { hasStore, STORE_NAMES } from './studios.js';
17
+ /** What a new or connected database of each kind puts on the fake team's Production env. */
18
+ const DATABASES = {
19
+ upstash: { KV_REST_API_URL: 'https://kv.studio.test', KV_REST_API_TOKEN: 'kv-token-xxxxxxxx' },
20
+ postgres: {
21
+ DATABASE_URL: 'postgres://user:pg-secret-xxxxxxxx@db.studio.test/hub?sslmode=require',
22
+ POSTGRES_URL: 'postgres://user:pg-secret-xxxxxxxx@db.studio.test/hub?sslmode=require',
23
+ },
24
+ };
16
25
  export function fakeHost(older = ['dpl_old1', 'dpl_old2']) {
17
26
  let next = 0;
18
27
  const fake = {
@@ -55,17 +64,17 @@ export function fakeHost(older = ['dpl_old1', 'dpl_old2']) {
55
64
  log(`pullEnv ${names.join(',')}`);
56
65
  return Object.fromEntries(names.filter((n) => fake.env.has(n)).map((n) => [n, fake.env.get(n)]));
57
66
  },
58
- async provisionDatabase(existing) {
59
- log(`provisionDatabase${existing ? ` ${existing}` : ''}`);
60
- if (fake.env.has('KV_REST_API_URL'))
67
+ async provisionDatabase(kind, existing) {
68
+ log(`provisionDatabase${kind === 'upstash' ? '' : ` ${kind}`}${existing ? ` ${existing}` : ''}`);
69
+ if (hasStore(kind, (n) => fake.env.has(n)))
61
70
  return 'kept';
62
- store({ KV_REST_API_URL: 'https://kv.studio.test', KV_REST_API_TOKEN: 'kv-token-xxxxxxxx' });
71
+ store(DATABASES[kind]);
63
72
  return existing ? 'connected' : 'created';
64
73
  },
65
- async disconnectDatabase(name) {
74
+ async disconnectDatabase(kind, name) {
66
75
  log(`disconnectDatabase ${name}`);
67
- fake.env.delete('KV_REST_API_URL');
68
- fake.env.delete('KV_REST_API_TOKEN');
76
+ for (const n of STORE_NAMES[kind])
77
+ fake.env.delete(n);
69
78
  return true;
70
79
  },
71
80
  async provisionFiles(region) {
@@ -119,8 +128,7 @@ export function fakeHost(older = ['dpl_old1', 'dpl_old2']) {
119
128
  const text = (status, body) => new Response(body, { status, headers: { 'content-type': 'text/plain; charset=utf-8' } });
120
129
  /**
121
130
  * The studio site as a fetch from outside meets it, over the commands' own
122
- * store, answering as the gate does (vercel/proxy.ts, which `pnpm quality`
123
- * keeps scripts from importing): a host no client has is the plain 404, the
131
+ * store, answering as the gate does (server/gate.ts), in short: a host no client has is the plain 404, the
124
132
  * host proof's path answers the proof's answer for its ask, a link page is
125
133
  * 200 while the link is live and a POST uses it up, anything else is the
126
134
  * signed-out 401. It keeps each host's answer a minute, unknown hosts too,
@@ -227,6 +235,9 @@ export function fakeDeps(home, cwd = home) {
227
235
  : perHost,
228
236
  names,
229
237
  store: () => kv,
238
+ prepareStore: async () => {
239
+ fake.calls.push('prepareStore');
240
+ },
230
241
  now: () => new Date(clock.at),
231
242
  files: () => files,
232
243
  // Installs nothing: an install that passes writes the lockfile, as pnpm does.
@@ -1,4 +1,5 @@
1
- import { type Run } from './vercel.ts';
1
+ import type { StoreKind } from '../../server/stores/index.ts';
2
+ import type { StudioConfig } from './studios.ts';
2
3
  export type Deployment = {
3
4
  id: string;
4
5
  state: string;
@@ -27,20 +28,20 @@ export interface Host {
27
28
  /** Production values of these names, read into memory, never into a file left behind. */
28
29
  pullEnv(names: readonly string[]): Promise<Record<string, string>>;
29
30
  /**
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).
31
+ * The review store of this kind (Upstash for Redis, or Postgres) when
32
+ * production has none: the team's existing database of the name given,
33
+ * connected, or else a new one, only ever on the free plan. A database
34
+ * serves one studio only: `studio create` checks the one connected is no
35
+ * other studio's before it writes to it (server/mark.ts).
35
36
  */
36
- provisionDatabase(existing?: string): Promise<'created' | 'connected' | 'kept'>;
37
+ provisionDatabase(kind: StoreKind, existing?: string): Promise<'created' | 'connected' | 'kept'>;
37
38
  /**
38
39
  * Takes the team's database of this name off the studio project, its
39
40
  * 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.
41
+ * True once Production holds none of the kind's store names; false when
42
+ * the disconnect failed or left them, so the refusal can say what is left.
42
43
  */
43
- disconnectDatabase(name: string): Promise<boolean>;
44
+ disconnectDatabase(kind: StoreKind, name: string): Promise<boolean>;
44
45
  /** The private file store (Vercel Blob), made and connected when production has none. */
45
46
  provisionFiles(region: string): Promise<'created' | 'kept'>;
46
47
  /** Puts a host on the studio project, or asks Vercel to verify one already there. */
@@ -70,18 +71,5 @@ export declare function deployAlone(host: Host): Promise<{
70
71
  deployment: string;
71
72
  removed: string[];
72
73
  }>;
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 {};
74
+ /** The host adapter a studio names, or a refusal that names the ones there are. */
75
+ export declare function hostFor(dir: string, config: StudioConfig, hosts?: Record<string, (dir: string, config: StudioConfig) => Host>): Host;