@milesroxas/design-hub 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/core/prototype.test.ts +12 -1
- package/core/prototype.ts +21 -2
- package/core/review/model.ts +71 -6
- package/core/review/service.test.ts +575 -39
- package/core/review/service.ts +452 -25
- package/core/review/store.ts +47 -6
- package/core/review/suite-kv.ts +17 -0
- package/dist/core/prototype.d.ts +20 -2
- package/dist/core/prototype.js +17 -2
- package/dist/core/review/model.d.ts +42 -3
- package/dist/core/review/model.js +52 -3
- package/dist/core/review/service.d.ts +81 -7
- package/dist/core/review/service.js +407 -23
- package/dist/core/review/store.d.ts +29 -2
- package/dist/core/review/store.js +6 -2
- package/dist/core/review/suite-kv.d.ts +7 -0
- package/dist/core/review/suite-kv.js +12 -0
- package/dist/scripts/client.d.ts +1 -1
- package/dist/scripts/client.js +4 -4
- package/dist/scripts/lib/cli.js +5 -0
- package/dist/scripts/lib/dns.d.ts +16 -2
- package/dist/scripts/lib/dns.js +66 -3
- package/dist/scripts/lib/fake-host.d.ts +17 -3
- package/dist/scripts/lib/fake-host.js +56 -10
- package/dist/scripts/lib/host.d.ts +15 -25
- package/dist/scripts/lib/host.js +15 -324
- package/dist/scripts/lib/resend.d.ts +36 -0
- package/dist/scripts/lib/resend.js +66 -0
- package/dist/scripts/lib/studio-deps.d.ts +23 -0
- package/dist/scripts/lib/studio-deps.js +46 -16
- package/dist/scripts/lib/studios.d.ts +16 -6
- package/dist/scripts/lib/studios.js +28 -10
- package/dist/scripts/lib/vercel-host.d.ts +17 -0
- package/dist/scripts/lib/vercel-host.js +416 -0
- package/dist/scripts/people.d.ts +16 -4
- package/dist/scripts/people.js +67 -17
- package/dist/scripts/share.js +3 -2
- package/dist/scripts/studio.d.ts +26 -4
- package/dist/scripts/studio.js +364 -19
- package/dist/server/api.d.ts +20 -7
- package/dist/server/api.js +83 -27
- package/dist/server/dev.js +21 -1
- package/dist/server/gate.d.ts +120 -0
- package/dist/server/gate.js +808 -0
- package/dist/server/mailer.d.ts +60 -0
- package/dist/server/mailer.js +104 -0
- package/dist/server/mailers/fake.d.ts +8 -0
- package/dist/server/mailers/fake.js +16 -0
- package/dist/server/mailers/resend.d.ts +21 -0
- package/dist/server/mailers/resend.js +49 -0
- package/dist/server/mark.d.ts +17 -0
- package/dist/server/mark.js +46 -1
- package/dist/server/session.d.ts +2 -1
- package/dist/server/session.js +6 -3
- package/dist/server/stores/backends.d.ts +22 -0
- package/dist/server/stores/backends.js +96 -0
- package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
- package/dist/server/stores/index.d.ts +41 -0
- package/dist/server/stores/index.js +63 -0
- package/dist/server/stores/pglite.d.ts +16 -0
- package/dist/server/stores/pglite.js +98 -0
- package/dist/server/stores/postgres.d.ts +63 -0
- package/dist/server/stores/postgres.js +153 -0
- package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
- package/dist/vercel/files.js +1 -1
- package/dist/vercel/proxy.d.ts +6 -82
- package/dist/vercel/proxy.js +24 -524
- package/dist/vite/plugin.js +4 -2
- package/kit/ui/Menu.tsx +29 -18
- package/kit/ui/layer.ts +41 -0
- package/package.json +3 -1
- package/shell/review/Bar.tsx +1 -0
- package/shell/review/Card.tsx +17 -7
- package/shell/review/People.tsx +908 -134
- package/shell/review/Review.tsx +2 -2
- package/shell/review/anchor.ts +6 -1
- package/shell/review/client.ts +41 -7
- package/shell/review/menu-place.test.ts +41 -0
- package/shell/review/place.test.ts +26 -1
- package/shell/review/place.ts +17 -0
- package/shell/review/review.css +97 -11
- package/shell/review/view.test.ts +115 -1
- package/shell/review/view.ts +92 -2
- package/shell/studio/StudioHub.tsx +1 -0
- package/shell/studio/hub.css +99 -13
- package/skills/miles-deploy/SKILL.md +26 -4
- package/templates/studio/.env.example +9 -0
- package/templates/studio/README.md +13 -0
- package/templates/studio/package.json +1 -1
- /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
- /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
package/dist/scripts/studio.js
CHANGED
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
// own host if it is not yet
|
|
14
14
|
// studio link lists a studio folder that already exists on this machine
|
|
15
15
|
// and links it to the studio's project on its team
|
|
16
|
+
// studio email turns email sign-in on (email.md, "Turning it on"): the
|
|
17
|
+
// sender's domain at Resend, its DNS records, a send-only
|
|
18
|
+
// key, a test mail, and only then the site's two secrets and
|
|
19
|
+
// a deploy; `--off` takes the two secrets off and deploys
|
|
16
20
|
//
|
|
17
21
|
// Every step checks before it acts, so a command stopped part way is safe to
|
|
18
22
|
// run again. Secrets go only to the folder's gitignored .env and the site's
|
|
@@ -20,16 +24,20 @@
|
|
|
20
24
|
import { randomBytes } from 'node:crypto';
|
|
21
25
|
import { copyFileSync, cpSync, existsSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
22
26
|
import { basename, join, resolve } from 'node:path';
|
|
27
|
+
import { escapeHtml } from '../core/html.js';
|
|
28
|
+
import { emailOf } from '../core/review/model.js';
|
|
23
29
|
import { readEnv, removeEnv, writeEnv } from '../server/env.js';
|
|
24
30
|
import { localStudio } from '../server/local.js';
|
|
25
|
-
import {
|
|
31
|
+
import { resendMailer } from '../server/mailers/resend.js';
|
|
32
|
+
import { claimDatabase, claimFiles, MarkError } from '../server/mark.js';
|
|
26
33
|
import { PACKAGE, TEMPLATES } from '../server/paths.js';
|
|
27
34
|
import { CliError } from './lib/cli.js';
|
|
28
35
|
import { keepingGitignore } from './lib/deploy.js';
|
|
29
36
|
import { pointHost, waitServed } from './lib/dns.js';
|
|
30
37
|
import { deployAlone } from './lib/host.js';
|
|
38
|
+
import { resendAccount } from './lib/resend.js';
|
|
31
39
|
import { openReview, realDeps } from './lib/studio-deps.js';
|
|
32
|
-
import { addToList, BLOB_NAME, CLOUDFLARE_NAME, DNS_PROVIDERS, findStudio, MODE_OF, readStudio, STORE_NAMES, STUDIO_FILE, studioHost, studioProblems, writeStudio, } from './lib/studios.js';
|
|
40
|
+
import { addToList, BLOB_NAME, CLOUDFLARE_NAME, DNS_PROVIDERS, findStudio, hasStore, KV_PROVIDERS, MODE_OF, readStudio, STORE_NAMES, STUDIO_FILE, studioHost, studioProblems, writeStudio, } from './lib/studios.js';
|
|
33
41
|
export const spec = {
|
|
34
42
|
name: 'studio',
|
|
35
43
|
summary: 'Create a studio (its folder, its site on a Vercel team, its stores), deploy it again, or list an existing studio folder on this machine.',
|
|
@@ -37,7 +45,7 @@ export const spec = {
|
|
|
37
45
|
{
|
|
38
46
|
name: 'action',
|
|
39
47
|
required: true,
|
|
40
|
-
description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, set up studio.<apex> (its DNS record and domain) and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again, remove every older deployment, and set up studio.<apex> if it is not yet. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project.',
|
|
48
|
+
description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, set up studio.<apex> (its DNS record and domain) and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again, remove every older deployment, and set up studio.<apex> if it is not yet. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project. email: turn email sign-in on, in this order, each step safe to run again: the full Resend key into .env; the sender’s domain at Resend (one already verified, or a verified parent, is used as it is, with no DNS change); else the domain made and its records added (Cloudflare by its API; Vercel DNS and manual print them and stop) and verified; a new send-only key for that domain; a test mail to --test; only then RESEND_API_KEY and MAIL_FROM on the site, a deploy, and the earlier send-only key removed. With --off: take the two off the site and deploy; links work as before.',
|
|
41
49
|
},
|
|
42
50
|
],
|
|
43
51
|
flags: {
|
|
@@ -78,9 +86,15 @@ export const spec = {
|
|
|
78
86
|
default: 'iad1',
|
|
79
87
|
description: 'For create: the file store’s region, the site’s function region.',
|
|
80
88
|
},
|
|
89
|
+
kv: {
|
|
90
|
+
type: 'string',
|
|
91
|
+
choices: KV_PROVIDERS,
|
|
92
|
+
default: 'upstash',
|
|
93
|
+
description: 'For create: the studio’s database. upstash: Upstash for Redis. postgres: Postgres, a new Neon database on its free plan, or with --database any Postgres the team has from the Vercel Marketplace (Neon, Supabase). Kept in studio.json; a rerun must give the same.',
|
|
94
|
+
},
|
|
81
95
|
database: {
|
|
82
96
|
type: 'string',
|
|
83
|
-
description: 'For create: the name of a database (
|
|
97
|
+
description: 'For create: the name of a database (of the --kv kind) the team already has and no studio uses, connected to the studio in place of a new one. A database serves one studio only: create marks it as this studio’s, and refuses one that is another studio’s or already holds a studio’s data, before writing to it, disconnecting it again and keeping none of its credentials. Absent, a new one is made, only ever on the free plan; a team refused the free plan stops here with nothing bought.',
|
|
84
98
|
},
|
|
85
99
|
package: {
|
|
86
100
|
type: 'string',
|
|
@@ -88,10 +102,23 @@ export const spec = {
|
|
|
88
102
|
},
|
|
89
103
|
studio: {
|
|
90
104
|
type: 'string',
|
|
91
|
-
description: 'For deploy: the studio’s slug. Absent, the studio folder this runs in.',
|
|
105
|
+
description: 'For deploy and email: the studio’s slug. Absent, the studio folder this runs in.',
|
|
106
|
+
},
|
|
107
|
+
from: {
|
|
108
|
+
type: 'string',
|
|
109
|
+
description: 'For email: who sign-in mail is from, "<Name> <address>", the address at the apex or below it, such as "North Studio <signin@mail.example.com>". The name is the studio’s in every mail.',
|
|
110
|
+
},
|
|
111
|
+
'resend-key-file': {
|
|
112
|
+
type: 'string',
|
|
113
|
+
description: 'For email: a file holding a full-access Resend API key, copied into the studio’s .env as RESEND_FULL_KEY; never pass a key itself. Absent, the one already in .env.',
|
|
114
|
+
},
|
|
115
|
+
test: { type: 'string', description: 'For email: the address the test mail goes to.' },
|
|
116
|
+
off: {
|
|
117
|
+
type: 'boolean',
|
|
118
|
+
description: 'For email: turn email sign-in off. Takes RESEND_API_KEY and MAIL_FROM off the site and deploys it.',
|
|
92
119
|
},
|
|
93
120
|
},
|
|
94
|
-
output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), dns, owner, deployment, removed, studioHost: { host, state: ready | waiting, error?, fix?, record? }, link? (the owner’s, on the studio’s host, the run it first answers), next }. deploy: { ok, studio, package, deployment, removed, studioHost, link? }. The deploy is done whatever studioHost says; while it is waiting, its fix says what to do (a record to add, a certificate still coming) and the next deploy finishes it. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }',
|
|
121
|
+
output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), filesMark: claimed | kept (the file store, the same), dns, owner, deployment, removed, studioHost: { host, state: ready | waiting, error?, fix?, record? }, link? (the owner’s, on the studio’s host, the run it first answers), next }. deploy: { ok, studio, package, deployment, removed, studioHost, link? }. The deploy is done whatever studioHost says; while it is waiting, its fix says what to do (a record to add, a certificate still coming) and the next deploy finishes it. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }. email: { ok, studio, from, domain, dns: none (already verified) | added | exists | printed, key (the send-only key’s name at Resend), test: sent, secrets, package, deployment, removed, keysRemoved, next }; while a step waits on a person it stops with ok: false, what to do (records to add, a domain not verified yet) and “run this again”. email --off: { ok, studio, secrets, package, deployment, removed, next }',
|
|
95
122
|
};
|
|
96
123
|
const need = (value, flag, action) => {
|
|
97
124
|
if (!value)
|
|
@@ -109,7 +136,7 @@ function configFrom(flags) {
|
|
|
109
136
|
vercel: { team: need(flags.team, 'team', 'create'), project: flags.project ?? flags.slug ?? '' },
|
|
110
137
|
dns: { provider, mode: MODE_OF[provider], zone: flags.zone ?? null },
|
|
111
138
|
owner: null,
|
|
112
|
-
providers: { kv: 'upstash', files: 'vercel-blob', host: 'vercel' },
|
|
139
|
+
providers: { kv: (flags.kv ?? 'upstash'), files: 'vercel-blob', host: 'vercel' },
|
|
113
140
|
};
|
|
114
141
|
need(flags.owner, 'owner', 'create');
|
|
115
142
|
const problems = studioProblems(config);
|
|
@@ -129,6 +156,7 @@ function sameStudio(kept, asked) {
|
|
|
129
156
|
['team', kept.vercel.team, asked.vercel.team],
|
|
130
157
|
['project', kept.vercel.project, asked.vercel.project],
|
|
131
158
|
['dns', kept.dns.provider, asked.dns.provider],
|
|
159
|
+
['kv', kept.providers.kv, asked.providers.kv],
|
|
132
160
|
];
|
|
133
161
|
const differs = pairs.filter(([, a, b]) => a !== b).map(([flag]) => flag);
|
|
134
162
|
if (differs.length)
|
|
@@ -140,6 +168,46 @@ function sameStudio(kept, asked) {
|
|
|
140
168
|
const HUB = '@milesroxas/design-hub';
|
|
141
169
|
const LOCKFILE = 'pnpm-lock.yaml';
|
|
142
170
|
const manifestOf = (dir) => JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
171
|
+
/** The first Design Hub whose gate has the email form and the code routes (email.md, slice 3). */
|
|
172
|
+
const EMAIL_SINCE = [0, 14, 0];
|
|
173
|
+
const versionOf = (v) => typeof v === 'string'
|
|
174
|
+
? (v
|
|
175
|
+
.match(/^\D*(\d+)\.(\d+)\.(\d+)/)
|
|
176
|
+
?.slice(1)
|
|
177
|
+
.map(Number) ?? null)
|
|
178
|
+
: null;
|
|
179
|
+
const olderThan = (v, than) => {
|
|
180
|
+
const i = v.findIndex((n, k) => n !== than[k]);
|
|
181
|
+
return i >= 0 && v[i] < than[i];
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Refuses a studio folder whose Design Hub has no email sign-in, before
|
|
185
|
+
* anything is written: a site on it would hold the mail secrets with no code
|
|
186
|
+
* form, and invites would promise one. It judges the version installed in the
|
|
187
|
+
* folder and the one its package.json names; a `file:` pack, by the installed
|
|
188
|
+
* one alone.
|
|
189
|
+
*/
|
|
190
|
+
function emailReady(dir, slug) {
|
|
191
|
+
const spec = manifestOf(dir).dependencies[HUB] ?? '';
|
|
192
|
+
let installed;
|
|
193
|
+
try {
|
|
194
|
+
installed = JSON.parse(readFileSync(join(dir, 'node_modules', HUB, 'package.json'), 'utf8')).version;
|
|
195
|
+
}
|
|
196
|
+
catch { }
|
|
197
|
+
const seen = [installed, spec.startsWith('file:') ? undefined : spec]
|
|
198
|
+
.map(versionOf)
|
|
199
|
+
.filter((v) => v !== null);
|
|
200
|
+
const old = seen.find((v) => olderThan(v, EMAIL_SINCE));
|
|
201
|
+
if (!old && seen.length > 0)
|
|
202
|
+
return;
|
|
203
|
+
const since = EMAIL_SINCE.join('.');
|
|
204
|
+
throw new CliError(old
|
|
205
|
+
? `The studio folder has ${HUB} ${old.join('.')}, which has no email sign-in; nothing was changed.`
|
|
206
|
+
: `The studio folder's ${HUB} version could not be read; nothing was changed.`, {
|
|
207
|
+
named: spec,
|
|
208
|
+
fix: `Move the studio folder to ${since} or newer first: pnpm add ${HUB}@<version> in ${dir}, or pnpm studio deploy --studio ${slug} --package <tgz>. Then run this again.`,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
143
211
|
/** The --package file, checked before anything is written. */
|
|
144
212
|
function packFile(tgz) {
|
|
145
213
|
if (tgz === undefined)
|
|
@@ -236,21 +304,30 @@ function tokenFrom(file) {
|
|
|
236
304
|
token = readFileSync(resolve(file), 'utf8').trim();
|
|
237
305
|
}
|
|
238
306
|
catch {
|
|
239
|
-
throw new CliError('--cloudflare-token-file could not be read.', {
|
|
307
|
+
throw new CliError('--cloudflare-token-file could not be read.', {
|
|
308
|
+
fix: 'Pass the path of a file that holds the token, never the token itself.',
|
|
309
|
+
});
|
|
240
310
|
}
|
|
241
311
|
if (!/^[\w-]{20,200}$/.test(token))
|
|
242
|
-
throw new CliError('--cloudflare-token-file does not hold one API token on one line.'
|
|
312
|
+
throw new CliError('--cloudflare-token-file does not hold one API token on one line.');
|
|
243
313
|
return token;
|
|
244
314
|
}
|
|
245
|
-
/**
|
|
246
|
-
|
|
315
|
+
/**
|
|
316
|
+
* The store and file credentials the CLI needs, read back from Production
|
|
317
|
+
* into .env where missing: the database's names of the studio's kind (for
|
|
318
|
+
* Postgres, whichever of DATABASE_URL and POSTGRES_URL the site has) and the
|
|
319
|
+
* file store's token.
|
|
320
|
+
*/
|
|
321
|
+
async function pullMissing(dir, host, kind) {
|
|
247
322
|
const env = readEnv(dir);
|
|
248
|
-
const
|
|
323
|
+
const store = hasStore(kind, (n) => !!env[n]) ? [] : STORE_NAMES[kind].filter((n) => !env[n]);
|
|
324
|
+
const missing = [...store, ...(env[BLOB_NAME] ? [] : [BLOB_NAME])];
|
|
249
325
|
if (!missing.length)
|
|
250
326
|
return [];
|
|
251
327
|
const values = await host.pullEnv(missing);
|
|
252
328
|
writeEnv(dir, values);
|
|
253
|
-
const
|
|
329
|
+
const now = { ...env, ...values };
|
|
330
|
+
const still = [...(hasStore(kind, (n) => !!now[n]) ? [] : store), ...(now[BLOB_NAME] ? [] : [BLOB_NAME])];
|
|
254
331
|
if (still.length)
|
|
255
332
|
throw new CliError(`Could not read ${still.join(', ')} back from the studio's Production env.`, {
|
|
256
333
|
fix: 'Copy them from the store’s page on Vercel into the studio’s .env, then run this again.',
|
|
@@ -294,14 +371,15 @@ async function wildcard(host, apex) {
|
|
|
294
371
|
* database of its own starts clean.
|
|
295
372
|
*/
|
|
296
373
|
async function claimOrLetGo(deps, host, studio, connected) {
|
|
374
|
+
const kind = studio.config.providers.kv;
|
|
297
375
|
try {
|
|
298
376
|
return await claimDatabase(deps.store(studio), studio);
|
|
299
377
|
}
|
|
300
378
|
catch (error) {
|
|
301
379
|
if (!(error instanceof MarkError))
|
|
302
380
|
throw error;
|
|
303
|
-
removeEnv(studio.dir, STORE_NAMES);
|
|
304
|
-
const gone = connected !== null && (await host.disconnectDatabase(connected));
|
|
381
|
+
removeEnv(studio.dir, STORE_NAMES[kind]);
|
|
382
|
+
const gone = connected !== null && (await host.disconnectDatabase(kind, connected));
|
|
305
383
|
const { dir, config } = studio;
|
|
306
384
|
const rerun = `then run design-hub studio create again in ${dir} with the same flags, without --database (a new free one) or with --database <a database no studio uses>.`;
|
|
307
385
|
throw new CliError(error.message, {
|
|
@@ -398,16 +476,21 @@ async function create(flags, deps) {
|
|
|
398
476
|
const pkg = installAndBuild(dir, deps);
|
|
399
477
|
const host = deps.host(dir, config);
|
|
400
478
|
const link = await keepingGitignore(dir, () => host.link(true));
|
|
479
|
+
const kind = config.providers.kv;
|
|
401
480
|
const env = {
|
|
402
481
|
GATE_SECRET: await gateSecret(dir, host),
|
|
403
|
-
database: await host.provisionDatabase(flags.database),
|
|
482
|
+
database: await host.provisionDatabase(kind, flags.database),
|
|
404
483
|
files: await host.provisionFiles(flags.region),
|
|
405
|
-
pulled: await pullMissing(dir, host),
|
|
484
|
+
pulled: await pullMissing(dir, host, kind),
|
|
406
485
|
};
|
|
407
486
|
const studio = { dir, config };
|
|
487
|
+
// Postgres's table, made here and only here, never by a visitor's request.
|
|
488
|
+
await deps.prepareStore(studio);
|
|
408
489
|
// Before anything is written to the database: it serves this studio only, or nothing goes on.
|
|
409
490
|
const connected = env.database === 'connected' ? (flags.database ?? null) : null;
|
|
410
491
|
const mark = await claimOrLetGo(deps, host, studio, connected);
|
|
492
|
+
// The file store too: one marked as another studio's is refused before a file is written.
|
|
493
|
+
const filesMark = await claimFiles(deps.files(studio), studio);
|
|
411
494
|
const dns = { provider: config.dns.provider, mode: config.dns.mode };
|
|
412
495
|
if (config.dns.mode === 'wildcard')
|
|
413
496
|
dns.wildcard = await wildcard(host, config.apex);
|
|
@@ -431,6 +514,7 @@ async function create(flags, deps) {
|
|
|
431
514
|
vercel: { team: config.vercel.team, project: config.vercel.project, link },
|
|
432
515
|
env,
|
|
433
516
|
mark,
|
|
517
|
+
filesMark,
|
|
434
518
|
dns,
|
|
435
519
|
owner: config.owner,
|
|
436
520
|
...deployed,
|
|
@@ -446,6 +530,9 @@ async function deploy(flags, deps) {
|
|
|
446
530
|
namePackage(dir, tgz);
|
|
447
531
|
const pkg = installAndBuild(dir, deps);
|
|
448
532
|
const studio = { dir, config };
|
|
533
|
+
// A Postgres studio's table, made or closed to other roles again, so an upgrade applies what a newer package needs.
|
|
534
|
+
if (config.providers.kv === 'postgres')
|
|
535
|
+
await deps.prepareStore(studio);
|
|
449
536
|
const host = deps.host(dir, config);
|
|
450
537
|
const deployed = await deployAlone(host);
|
|
451
538
|
// For a studio made before it had one, the owner's first link on the studio's host comes the run it answers.
|
|
@@ -469,7 +556,7 @@ async function link(flags, deps) {
|
|
|
469
556
|
// folder can hold a full .env and no link, and a deploy follows the link.
|
|
470
557
|
const host = deps.host(dir, config);
|
|
471
558
|
await keepingGitignore(dir, () => host.link(false));
|
|
472
|
-
const pulled = await pullMissing(dir, host);
|
|
559
|
+
const pulled = await pullMissing(dir, host, config.providers.kv);
|
|
473
560
|
const warnings = config.dns.provider === 'cloudflare' && !readEnv(dir)[CLOUDFLARE_NAME]
|
|
474
561
|
? [
|
|
475
562
|
`No ${CLOUDFLARE_NAME} in .env: client add needs it, and studio deploy until studio.${config.apex} answers. Run this again with --cloudflare-token-file <file>.`,
|
|
@@ -477,15 +564,273 @@ async function link(flags, deps) {
|
|
|
477
564
|
: [];
|
|
478
565
|
return { studio: config.slug, dir, list, env: { pulled }, warnings };
|
|
479
566
|
}
|
|
567
|
+
/** The names the site reads its mail by (chooseMailer), and the full key's, which stays in the folder. */
|
|
568
|
+
const MAIL_NAMES = ['RESEND_API_KEY', 'MAIL_FROM'];
|
|
569
|
+
const FULL_KEY = 'RESEND_FULL_KEY';
|
|
570
|
+
/** A Resend API key as a file holds it: `re_`, then letters, digits, `_` and `-`. */
|
|
571
|
+
const RESEND_KEY = /^re_[\w-]{8,200}$/;
|
|
572
|
+
/** Resend verified a new domain in about a minute (email-findings.md, b): looked at for up to two. */
|
|
573
|
+
const VERIFY_TRIES = 12;
|
|
574
|
+
const VERIFY_GAP_MS = 10_000;
|
|
575
|
+
/** The sender `--from` names, its address at the apex or below it. */
|
|
576
|
+
function senderFrom(given, apex) {
|
|
577
|
+
const raw = need(given, 'from', 'email');
|
|
578
|
+
const parts = raw.match(/^\s*"?([^"<>\\\r\n]*?)"?\s*<([^<>]+)>\s*$/);
|
|
579
|
+
const name = parts?.[1].trim() ?? '';
|
|
580
|
+
const address = emailOf(parts?.[2]);
|
|
581
|
+
if (!name || !address)
|
|
582
|
+
throw new CliError('--from takes a name and an address, such as "North Studio <signin@mail.example.com>".', {
|
|
583
|
+
got: raw,
|
|
584
|
+
});
|
|
585
|
+
const domain = address.slice(address.indexOf('@') + 1);
|
|
586
|
+
if (domain !== apex && !domain.endsWith(`.${apex}`))
|
|
587
|
+
throw new CliError(`The sender’s address must be at ${apex} or below it.`, {
|
|
588
|
+
got: address,
|
|
589
|
+
fix: `Use an address such as signin@mail.${apex}.`,
|
|
590
|
+
});
|
|
591
|
+
// A name with a comma, a period or another special goes in quotes, or Resend reads it as more than one sender.
|
|
592
|
+
const shown = /[()<>[\]:;@,."]/.test(name) ? `"${name}"` : name;
|
|
593
|
+
return { name, address, domain, from: `${shown} <${address}>` };
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Step 1: the full key, from the file into the folder's .env, or the one
|
|
597
|
+
* already there. Read from a file, so it never shows in a process list or a
|
|
598
|
+
* shell's history; no error repeats it.
|
|
599
|
+
*/
|
|
600
|
+
function fullKey(dir, file) {
|
|
601
|
+
if (!file) {
|
|
602
|
+
const kept = readEnv(dir)[FULL_KEY];
|
|
603
|
+
if (kept)
|
|
604
|
+
return kept;
|
|
605
|
+
throw new CliError(`No ${FULL_KEY} in the studio's .env.`, {
|
|
606
|
+
fix: 'Run this again with --resend-key-file <a file holding a full-access Resend API key>.',
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
let key;
|
|
610
|
+
try {
|
|
611
|
+
key = readFileSync(resolve(file), 'utf8').trim();
|
|
612
|
+
}
|
|
613
|
+
catch {
|
|
614
|
+
throw new CliError('--resend-key-file could not be read.', {
|
|
615
|
+
fix: 'Pass the path of a file that holds the key, never the key itself.',
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
if (!RESEND_KEY.test(key))
|
|
619
|
+
throw new CliError('--resend-key-file does not hold one Resend API key on one line.');
|
|
620
|
+
writeEnv(dir, { [FULL_KEY]: key });
|
|
621
|
+
return key;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* The sender's domain, and each of its parents under the apex, is no host
|
|
625
|
+
* the studio serves: not `studio.<apex>`, and no client's, even one named
|
|
626
|
+
* `mail` before that name was kept.
|
|
627
|
+
*/
|
|
628
|
+
async function notServed(deps, studio, domain) {
|
|
629
|
+
const review = await openReview(deps, studio);
|
|
630
|
+
const { apex } = studio.config;
|
|
631
|
+
for (let name = domain; name.endsWith(`.${apex}`); name = name.slice(name.indexOf('.') + 1))
|
|
632
|
+
if (await review.siteOfHost(name))
|
|
633
|
+
throw new CliError(`${name} is a host this studio serves, so mail cannot be sent from it.`, {
|
|
634
|
+
fix: `Send from another domain under the apex, such as signin@mail.${apex}, if no client is there.`,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
/** The account's domain for the sender: a verified one that is the sender's domain or a parent of it, the nearest; else the sender's own, as it stands. */
|
|
638
|
+
function domainFor(domains, domain) {
|
|
639
|
+
const covers = (d) => domain === d.name || domain.endsWith(`.${d.name}`);
|
|
640
|
+
const verified = domains
|
|
641
|
+
.filter((d) => d.status === 'verified' && covers(d))
|
|
642
|
+
.sort((a, b) => b.name.length - a.name.length);
|
|
643
|
+
return verified[0] ?? domains.find((d) => d.name === domain) ?? null;
|
|
644
|
+
}
|
|
645
|
+
/** Resend's records as the DNS port takes them, each name in full: Resend names them relative to the apex (email-findings.md, a). */
|
|
646
|
+
function wantedOf(records, apex) {
|
|
647
|
+
return records.map((r) => {
|
|
648
|
+
const type = r.type.toUpperCase();
|
|
649
|
+
if (type !== 'TXT' && type !== 'MX' && type !== 'CNAME')
|
|
650
|
+
throw new CliError(`Resend asks for a ${r.type} record, which this command cannot add.`, {
|
|
651
|
+
fix: 'Add the domain’s records at the DNS provider from its page at resend.com/domains, then run this again.',
|
|
652
|
+
});
|
|
653
|
+
const relative = r.name.toLowerCase().replace(/\.$/, '');
|
|
654
|
+
const name = relative === apex || relative.endsWith(`.${apex}`) ? relative : `${relative}.${apex}`;
|
|
655
|
+
return { type, name, value: r.value, ...(type === 'MX' ? { priority: Number(r.priority ?? 10) } : {}) };
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
/** Step 4: Resend asked to verify, then looked at until it says verified, for up to two minutes. */
|
|
659
|
+
async function verifiedDomain(deps, account, id) {
|
|
660
|
+
await account.verify(id);
|
|
661
|
+
let domain = await account.domain(id);
|
|
662
|
+
for (let i = 1; i < VERIFY_TRIES && domain.status !== 'verified'; i++) {
|
|
663
|
+
await deps.wait(VERIFY_GAP_MS);
|
|
664
|
+
domain = await account.domain(id);
|
|
665
|
+
}
|
|
666
|
+
return domain;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Steps 2 to 4: the domain the sender sends from. One the account has
|
|
670
|
+
* verified, or a verified parent, is used as it is: no DNS record is read or
|
|
671
|
+
* written. Else the sender's domain is made at Resend (once: a second make is
|
|
672
|
+
* refused, so the list is read first), its records go through the DNS port,
|
|
673
|
+
* and Resend verifies it, or the command stops to be run again.
|
|
674
|
+
*/
|
|
675
|
+
async function mailDomain(deps, studio, account, wanted) {
|
|
676
|
+
const found = domainFor(await account.domains(), wanted);
|
|
677
|
+
if (found?.status === 'verified')
|
|
678
|
+
return { domain: found, dns: 'none' };
|
|
679
|
+
const made = found ?? (await account.createDomain(wanted));
|
|
680
|
+
const records = made.records ?? (await account.domain(made.id)).records ?? [];
|
|
681
|
+
const dns = deps.dns(studio, deps.host(studio.dir, studio.config));
|
|
682
|
+
const recorded = await dns.records(wantedOf(records, studio.config.apex));
|
|
683
|
+
// Records only printed, for a domain this run made, cannot be there yet: show them now, wait for nothing.
|
|
684
|
+
if (typeof recorded === 'object' && !found)
|
|
685
|
+
throw new CliError('Add the records listed at the DNS provider, then run this again; nothing was set on the site.', { records: recorded.records, fix: 'Add each record at the DNS provider, then run this again.' });
|
|
686
|
+
const domain = await verifiedDomain(deps, account, made.id);
|
|
687
|
+
if (domain.status !== 'verified')
|
|
688
|
+
throw new CliError(`Resend has not verified ${made.name} yet; nothing was set on the site.`, {
|
|
689
|
+
status: domain.status,
|
|
690
|
+
...(typeof recorded === 'object'
|
|
691
|
+
? { records: recorded.records, fix: 'Add each record at the DNS provider, then run this again.' }
|
|
692
|
+
: { fix: 'New records can take some minutes to be served: run this again.' }),
|
|
693
|
+
});
|
|
694
|
+
return { domain, dns: recorded };
|
|
695
|
+
}
|
|
696
|
+
/** The send-only key's name at Resend: the studio's slug and id, so an earlier run's is found by it and no other studio's is. */
|
|
697
|
+
const keyName = (config) => `design-hub-${config.slug}-${String(config.id).slice(0, 8)}`;
|
|
698
|
+
/** Step 6's mail: it names the studio and the sender, so the inbox shows what sign-in mail will look like. */
|
|
699
|
+
function testMail(sender, to, key) {
|
|
700
|
+
const subject = `Email sign-in is ready for ${sender.name}`;
|
|
701
|
+
const line = `Sign-in codes and invites for ${sender.name} will come from ${sender.address}.`;
|
|
702
|
+
const text = `${subject}.\n\n${line}\n\nNothing to do: this mail only proves that mail is sent.\n`;
|
|
703
|
+
const html = `<!doctype html><html lang="en"><body><p>${escapeHtml(line)}</p><p>Nothing to do: this mail only proves that mail is sent.</p></body></html>`;
|
|
704
|
+
return { to, subject, text, html, key: `test-${key}` };
|
|
705
|
+
}
|
|
706
|
+
/** A fetch that keeps the status and message of what it last heard back, and nothing else (no header, no key). */
|
|
707
|
+
function heard(fetcher, said) {
|
|
708
|
+
return async (input, init) => {
|
|
709
|
+
const res = await fetcher(input, init);
|
|
710
|
+
const body = (await res
|
|
711
|
+
.clone()
|
|
712
|
+
.json()
|
|
713
|
+
.catch(() => null));
|
|
714
|
+
said.status = res.status;
|
|
715
|
+
said.message = typeof body?.message === 'string' ? body.message.slice(0, 300) : undefined;
|
|
716
|
+
return res;
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
/** Puts a secret on the site in place of the one there, which setSecret alone keeps. */
|
|
720
|
+
async function replaceSecret(host, name, value) {
|
|
721
|
+
await host.removeSecret(name);
|
|
722
|
+
await host.setSecret(name, value);
|
|
723
|
+
}
|
|
724
|
+
async function emailOn(flags, deps, keys) {
|
|
725
|
+
const studio = findStudio(deps.home, flags.studio, deps.cwd);
|
|
726
|
+
const { dir, config } = studio;
|
|
727
|
+
emailReady(dir, config.slug);
|
|
728
|
+
const sender = senderFrom(flags.from, config.apex);
|
|
729
|
+
const to = emailOf(need(flags.test, 'test', 'email'));
|
|
730
|
+
if (!to)
|
|
731
|
+
throw new CliError('--test takes an email address.', { got: flags.test });
|
|
732
|
+
// 1. The full key, into .env.
|
|
733
|
+
const full = fullKey(dir, flags['resend-key-file']);
|
|
734
|
+
keys.push(full);
|
|
735
|
+
await notServed(deps, studio, sender.domain);
|
|
736
|
+
const account = resendAccount({ key: full, fetcher: deps.fetcher });
|
|
737
|
+
// 2 to 4. The domain, verified.
|
|
738
|
+
const { domain, dns } = await mailDomain(deps, studio, account, sender.domain);
|
|
739
|
+
// 5. A new send-only key for it. An earlier run's goes only once the site holds this one.
|
|
740
|
+
const name = keyName(config);
|
|
741
|
+
const key = await account.createKey(name, domain.id);
|
|
742
|
+
keys.push(key.token);
|
|
743
|
+
// 6. The test mail, by the site's own adapter, with the key the site will hold.
|
|
744
|
+
const said = {};
|
|
745
|
+
const test = await resendMailer({
|
|
746
|
+
key: key.token,
|
|
747
|
+
from: sender.from,
|
|
748
|
+
fetcher: heard(deps.fetcher, said),
|
|
749
|
+
}).send(testMail(sender, to, key.id));
|
|
750
|
+
if (test !== 'sent')
|
|
751
|
+
throw new CliError(test === 'down'
|
|
752
|
+
? 'Resend did not answer the test mail; nothing was set on the site.'
|
|
753
|
+
: `Resend refused the test mail; nothing was set on the site. Resend said (${said.status ?? 'no status'}): ${said.message ?? 'no message'}`, {
|
|
754
|
+
to,
|
|
755
|
+
...(said.status === undefined ? {} : { status: said.status }),
|
|
756
|
+
fix: 'Check the --test address and that Resend sends from the sender’s domain, then run this again.',
|
|
757
|
+
});
|
|
758
|
+
// 7. Only now: the folder's .env, the site's two secrets, a deploy, and then the earlier keys removed.
|
|
759
|
+
writeEnv(dir, { RESEND_API_KEY: key.token, MAIL_FROM: sender.from });
|
|
760
|
+
const pkg = installAndBuild(dir, deps);
|
|
761
|
+
const host = deps.host(dir, config);
|
|
762
|
+
await replaceSecret(host, 'RESEND_API_KEY', key.token);
|
|
763
|
+
await replaceSecret(host, 'MAIL_FROM', sender.from);
|
|
764
|
+
const deployed = await deployAlone(host);
|
|
765
|
+
const older = (await account.keys()).filter((k) => k.name === name && k.id !== key.id);
|
|
766
|
+
for (const k of older)
|
|
767
|
+
await account.deleteKey(k.id);
|
|
768
|
+
return {
|
|
769
|
+
studio: config.slug,
|
|
770
|
+
from: sender.from,
|
|
771
|
+
domain: domain.name,
|
|
772
|
+
dns: typeof dns === 'object' ? 'printed' : dns,
|
|
773
|
+
key: name,
|
|
774
|
+
test,
|
|
775
|
+
secrets: [...MAIL_NAMES],
|
|
776
|
+
package: pkg,
|
|
777
|
+
...deployed,
|
|
778
|
+
keysRemoved: older.length,
|
|
779
|
+
next: `Email sign-in is on: a person with an email signs in alone with a code. A test mail went to ${to}.`,
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
async function emailOff(flags, deps) {
|
|
783
|
+
if (flags.from || flags.test || flags['resend-key-file'])
|
|
784
|
+
throw new CliError('--off takes no --from, --test or --resend-key-file.');
|
|
785
|
+
const { dir, config } = findStudio(deps.home, flags.studio, deps.cwd);
|
|
786
|
+
const pkg = installAndBuild(dir, deps);
|
|
787
|
+
const host = deps.host(dir, config);
|
|
788
|
+
const secrets = {};
|
|
789
|
+
for (const name of MAIL_NAMES)
|
|
790
|
+
secrets[name] = await host.removeSecret(name);
|
|
791
|
+
const deployed = await deployAlone(host);
|
|
792
|
+
// The folder too, so `people add` mails no more; the full key stays, for turning it on again.
|
|
793
|
+
removeEnv(dir, MAIL_NAMES);
|
|
794
|
+
return {
|
|
795
|
+
studio: config.slug,
|
|
796
|
+
secrets,
|
|
797
|
+
package: pkg,
|
|
798
|
+
...deployed,
|
|
799
|
+
next: `Email sign-in is off; links work as before. ${FULL_KEY} stays in the studio's .env, so turning it on again needs only --from and --test.`,
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
/** An error with every key this run knows put out of it, whatever made it. */
|
|
803
|
+
function withoutKeys(error, keys) {
|
|
804
|
+
const scrub = (text) => keys.reduce((t, k) => t.split(k).join('<key>'), text);
|
|
805
|
+
const problems = error?.problems;
|
|
806
|
+
const detail = error instanceof CliError || error instanceof MarkError
|
|
807
|
+
? error.detail
|
|
808
|
+
: Array.isArray(problems)
|
|
809
|
+
? { problems }
|
|
810
|
+
: {};
|
|
811
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
812
|
+
return new CliError(scrub(message), JSON.parse(scrub(JSON.stringify(detail))));
|
|
813
|
+
}
|
|
814
|
+
/** Email on or off. No key is ever in what it prints, an error's words and detail included. */
|
|
815
|
+
async function email(flags, deps) {
|
|
816
|
+
const keys = [];
|
|
817
|
+
try {
|
|
818
|
+
return await (flags.off ? emailOff(flags, deps) : emailOn(flags, deps, keys));
|
|
819
|
+
}
|
|
820
|
+
catch (error) {
|
|
821
|
+
throw withoutKeys(error, keys.filter(Boolean));
|
|
822
|
+
}
|
|
823
|
+
}
|
|
480
824
|
const ACTIONS = {
|
|
481
825
|
create,
|
|
482
826
|
deploy,
|
|
483
827
|
link,
|
|
828
|
+
email,
|
|
484
829
|
};
|
|
485
830
|
export async function run({ positionals: [action], flags }, deps) {
|
|
486
831
|
const act = Object.hasOwn(ACTIONS, action) ? ACTIONS[action] : undefined;
|
|
487
832
|
if (!act)
|
|
488
|
-
throw new CliError(`Unknown action ${action}.`, { fix: 'create, deploy or
|
|
833
|
+
throw new CliError(`Unknown action ${action}.`, { fix: 'create, deploy, link or email' });
|
|
489
834
|
return act(flags, deps);
|
|
490
835
|
}
|
|
491
836
|
export const main = (args) => run(args, realDeps());
|
package/dist/server/api.d.ts
CHANGED
|
@@ -2,13 +2,23 @@ import type { Checks } from '../core/checks.ts';
|
|
|
2
2
|
import { type Person, type Scope } from '../core/review/model.ts';
|
|
3
3
|
import { type HostScope, type Review } from '../core/review/service.ts';
|
|
4
4
|
import { type HostLookup } from './hosts.ts';
|
|
5
|
+
import { type Mailer } from './mailer.ts';
|
|
5
6
|
import { type Gate, type GateKeys } from './session.ts';
|
|
6
7
|
export declare const API_PATH = "/api/review";
|
|
7
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* One request: the service, who is signed in, and the scope every op runs
|
|
10
|
+
* in. `mailer` is the studio's, or none (no invite is mailed); `host` is the
|
|
11
|
+
* host the scope's Client people sign in on, named in their invite: on a
|
|
12
|
+
* studio site the client's own, from its record.
|
|
13
|
+
*/
|
|
8
14
|
export type ApiContext = {
|
|
9
15
|
review: Review;
|
|
10
16
|
actor: Person | null;
|
|
11
17
|
scope: Scope;
|
|
18
|
+
mailer?: Mailer | null;
|
|
19
|
+
host?: string | null;
|
|
20
|
+
/** pnpm dev with no DESIGN_HUB_DEV_MAIL: no studio is here to say how mail stands, so access answers mail: null and the People tab says nothing of it. */
|
|
21
|
+
mailUnknown?: boolean;
|
|
12
22
|
};
|
|
13
23
|
export declare const handleReview: (request: Request, ctx: ApiContext) => Promise<Response>;
|
|
14
24
|
/** The person a request's signed cookie names, while they may still sign in, where `at` is: a client's scope, or the studio's host. */
|
|
@@ -21,15 +31,18 @@ export type Deployed = {
|
|
|
21
31
|
error: string;
|
|
22
32
|
};
|
|
23
33
|
/**
|
|
24
|
-
* A studio site's review service:
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* gets
|
|
34
|
+
* A studio site's review service: the store its env names (chooseStore:
|
|
35
|
+
* Upstash or Postgres, never a guess between them), Jev for triage when its
|
|
36
|
+
* key is set and `checks` is given. `now` is the clock, so the gate and the
|
|
37
|
+
* service it asks run on one (the gate's tests move it). Without the gate's
|
|
38
|
+
* secret or one store, the cause goes to the site's log and a visitor gets
|
|
39
|
+
* SITE_NOT_SET_UP.
|
|
29
40
|
*/
|
|
30
41
|
export declare function studioReview(env: Record<string, string | undefined>, checks?: Checks | null, now?: () => Date): Deployed;
|
|
31
42
|
/**
|
|
32
43
|
* A studio site's review function: the host's client is the scope, and the
|
|
33
44
|
* gate's client, of every op; the studio's own host has the studio hub's.
|
|
45
|
+
* `mailer` is the studio's, chosen from the env as the gate chooses it
|
|
46
|
+
* (chooseMailer), unless a test gives one or null.
|
|
34
47
|
*/
|
|
35
|
-
export declare function studioHandler(request: Request, checks: Checks | null, hosts: HostLookup): Promise<Response>;
|
|
48
|
+
export declare function studioHandler(request: Request, checks: Checks | null, hosts: HostLookup, mailer?: Mailer | null): Promise<Response>;
|