@crombolo/server 0.63.0 → 0.65.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -14
- package/src/access.ts +6 -2
- package/src/adapters/content.ts +66 -11
- package/src/adapters/documents.ts +6 -2
- package/src/adapters/schema-files.ts +19 -27
- package/src/approvals.ts +31 -17
- package/src/bundle-admin.ts +39 -3
- package/src/bundle-reload.ts +2 -1
- package/src/bundles.ts +60 -25
- package/src/config.ts +47 -8
- package/src/content-notifications.ts +6 -3
- package/src/content-operations.ts +4 -3
- package/src/core.ts +362 -136
- package/src/email-notice.ts +2 -2
- package/src/email-settings.ts +394 -0
- package/src/email.ts +9 -4
- package/src/extension-components.ts +20 -4
- package/src/extension-discovery.ts +84 -65
- package/src/form-workflows.ts +3 -3
- package/src/grant-trust.ts +6 -4
- package/src/index.ts +2 -0
- package/src/ports.ts +19 -17
- package/src/routes.ts +22 -3
- package/src/schema-transfer.ts +472 -0
- package/src/seed/values.ts +126 -85
- package/src/server.ts +173 -14
- package/src/settings.ts +14 -10
- package/src/site-public.ts +2 -1
- package/src/status.ts +3 -0
- package/src/telemetry.ts +9 -2
- package/src/trusted-signers-admin.ts +39 -3
- package/src/view-as.ts +95 -0
- package/src/zip.ts +86 -3
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E-mail set up in Settings (story 340): kept in `key_value`, its key sealed by
|
|
3
|
+
* `CROMBOLO_SECRET_KEY`, and used only when the site's configuration sets no
|
|
4
|
+
* e-mail. The configuration wins, because whoever sets e-mail decides where
|
|
5
|
+
* password-reset links go. docs/configuration.md, "E-mail".
|
|
6
|
+
*/
|
|
7
|
+
import { isValidEmail } from '@crombolo/core'
|
|
8
|
+
import { type Db, now } from '@crombolo/data'
|
|
9
|
+
import { recordAudit } from './adapters/audit.ts'
|
|
10
|
+
import {
|
|
11
|
+
type EmailAvailability,
|
|
12
|
+
type EmailMessage,
|
|
13
|
+
type EmailPort,
|
|
14
|
+
emailAvailability,
|
|
15
|
+
emailFromEnvironment,
|
|
16
|
+
logEmail,
|
|
17
|
+
resolveEmailPort,
|
|
18
|
+
} from './email.ts'
|
|
19
|
+
import { logger } from './logging.ts'
|
|
20
|
+
import { openSecret, sealSecret, usableSecretKey } from './secrets.ts'
|
|
21
|
+
|
|
22
|
+
const KEY = 'crombolo:email'
|
|
23
|
+
/** How stale another node's copy may be before it reads again. */
|
|
24
|
+
export const EMAIL_SETTINGS_TTL_MS = 30_000
|
|
25
|
+
|
|
26
|
+
export const STORED_PROVIDERS = ['resend', 'postmark', 'ses', 'custom'] as const
|
|
27
|
+
export type StoredProvider = (typeof STORED_PROVIDERS)[number]
|
|
28
|
+
|
|
29
|
+
interface Stored {
|
|
30
|
+
provider: StoredProvider
|
|
31
|
+
from: string
|
|
32
|
+
fromName?: string
|
|
33
|
+
url?: string
|
|
34
|
+
postmarkStream?: string
|
|
35
|
+
sesRegion?: string
|
|
36
|
+
sesAccessKeyId?: string
|
|
37
|
+
/** The API key, or SES's secret access key. */
|
|
38
|
+
keySealed?: string
|
|
39
|
+
/** Where the key was entered for; it goes nowhere else (as story 305's client secret). */
|
|
40
|
+
keyFor?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** What an administrator sends; `key` is set only when a new one is entered. */
|
|
44
|
+
export type EmailSettingsInput = Partial<Omit<Stored, 'keySealed' | 'keyFor'>> & { key?: string }
|
|
45
|
+
|
|
46
|
+
interface Site {
|
|
47
|
+
email?: EmailPort | null
|
|
48
|
+
development?: boolean
|
|
49
|
+
secretKey?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Env = Record<string, string | undefined>
|
|
53
|
+
|
|
54
|
+
/** `config` when the site's configuration sets e-mail, `off` when it turns it off. */
|
|
55
|
+
export function configuredEmail(config: Site, env: Env = Bun.env): 'config' | 'off' | undefined {
|
|
56
|
+
if (config.email === null) return 'off'
|
|
57
|
+
if (config.email) return 'config'
|
|
58
|
+
const kind = (env.CROMBOLO_EMAIL_PROVIDER ?? '').trim().toLowerCase()
|
|
59
|
+
if (kind === 'none') return 'off'
|
|
60
|
+
return kind ? 'config' : undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const targetOf = (s: Pick<Stored, 'provider' | 'url' | 'sesAccessKeyId'>) =>
|
|
64
|
+
[s.provider, s.url ?? '', s.sesAccessKeyId ?? ''].join('|')
|
|
65
|
+
|
|
66
|
+
async function read(db: Db): Promise<Stored | undefined> {
|
|
67
|
+
const row = await db.q
|
|
68
|
+
.selectFrom('key_value')
|
|
69
|
+
.select('value')
|
|
70
|
+
.where('key', '=', KEY)
|
|
71
|
+
.executeTakeFirst()
|
|
72
|
+
try {
|
|
73
|
+
return row?.value ? (JSON.parse(row.value) as Stored) : undefined
|
|
74
|
+
} catch {
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function write(db: Db, stored: Stored | undefined): Promise<void> {
|
|
80
|
+
if (!stored) {
|
|
81
|
+
await db.q.deleteFrom('key_value').where('key', '=', KEY).execute()
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
const value = JSON.stringify(stored)
|
|
85
|
+
const updated = await db.q
|
|
86
|
+
.updateTable('key_value')
|
|
87
|
+
.set({ value, update_date: now(db) })
|
|
88
|
+
.where('key', '=', KEY)
|
|
89
|
+
.returning('key')
|
|
90
|
+
.execute()
|
|
91
|
+
if (updated.length === 0)
|
|
92
|
+
await db.q
|
|
93
|
+
.insertInto('key_value')
|
|
94
|
+
.values({ key: KEY, value, update_date: now(db) })
|
|
95
|
+
.execute()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function keyOf(stored: Stored, secretKey: string | undefined): string {
|
|
99
|
+
const key = usableSecretKey(secretKey)
|
|
100
|
+
if (!stored.keySealed || !key || stored.keyFor !== targetOf(stored)) return ''
|
|
101
|
+
try {
|
|
102
|
+
return openSecret(stored.keySealed, key)
|
|
103
|
+
} catch {
|
|
104
|
+
return ''
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The adapter stored settings describe, through the same checks as the environment's. */
|
|
109
|
+
function portOf(
|
|
110
|
+
stored: Stored | undefined,
|
|
111
|
+
secretKey: string | undefined,
|
|
112
|
+
log: { warning(message: string, properties?: Record<string, unknown>): void } = logger('email'),
|
|
113
|
+
): EmailPort | undefined {
|
|
114
|
+
if (!stored) return undefined
|
|
115
|
+
const key = keyOf(stored, secretKey)
|
|
116
|
+
return emailFromEnvironment(
|
|
117
|
+
{
|
|
118
|
+
CROMBOLO_EMAIL_PROVIDER: stored.provider,
|
|
119
|
+
CROMBOLO_EMAIL_FROM: stored.from,
|
|
120
|
+
CROMBOLO_EMAIL_FROM_NAME: stored.fromName,
|
|
121
|
+
CROMBOLO_EMAIL_URL: stored.url,
|
|
122
|
+
CROMBOLO_EMAIL_POSTMARK_STREAM: stored.postmarkStream,
|
|
123
|
+
CROMBOLO_EMAIL_SES_REGION: stored.sesRegion,
|
|
124
|
+
CROMBOLO_EMAIL_SES_ACCESS_KEY_ID: stored.sesAccessKeyId,
|
|
125
|
+
...(stored.provider === 'ses'
|
|
126
|
+
? { CROMBOLO_EMAIL_SES_SECRET_ACCESS_KEY: key }
|
|
127
|
+
: { CROMBOLO_EMAIL_API_KEY: key }),
|
|
128
|
+
},
|
|
129
|
+
log,
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The site's e-mail, read again from Settings as it changes. */
|
|
134
|
+
export interface LiveEmail extends EmailPort {
|
|
135
|
+
/** Reads the settings now, on the node that saved them. */
|
|
136
|
+
refresh(): Promise<void>
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The port a site sends through. The configuration's when it sets e-mail;
|
|
141
|
+
* otherwise one that follows Settings, re-read when older than the TTL, so a
|
|
142
|
+
* change reaches every node without a restart. With nothing set up it reports
|
|
143
|
+
* `none`, or is the console in development.
|
|
144
|
+
*/
|
|
145
|
+
export async function siteEmail(
|
|
146
|
+
db: Db,
|
|
147
|
+
config: Site,
|
|
148
|
+
options: { env?: Env; ttlMs?: number } = {},
|
|
149
|
+
): Promise<EmailPort | undefined> {
|
|
150
|
+
const env = options.env ?? Bun.env
|
|
151
|
+
if (configuredEmail(config, env)) return resolveEmailPort(config, env)
|
|
152
|
+
const ttl = options.ttlMs ?? EMAIL_SETTINGS_TTL_MS
|
|
153
|
+
const fallback = config.development ? logEmail() : undefined
|
|
154
|
+
let current: EmailPort | undefined
|
|
155
|
+
let readAt = 0
|
|
156
|
+
let reading: Promise<void> | undefined
|
|
157
|
+
const load = () => {
|
|
158
|
+
reading ??= read(db)
|
|
159
|
+
.then((stored) => {
|
|
160
|
+
current = portOf(stored, config.secretKey) ?? fallback
|
|
161
|
+
readAt = Date.now()
|
|
162
|
+
})
|
|
163
|
+
.catch(() => {})
|
|
164
|
+
.finally(() => {
|
|
165
|
+
reading = undefined
|
|
166
|
+
})
|
|
167
|
+
return reading
|
|
168
|
+
}
|
|
169
|
+
// A stale copy answers at once while the next read is under way.
|
|
170
|
+
const fresh = () => {
|
|
171
|
+
if (Date.now() - readAt >= ttl) void load()
|
|
172
|
+
return current
|
|
173
|
+
}
|
|
174
|
+
await load()
|
|
175
|
+
const live: LiveEmail = {
|
|
176
|
+
get description() {
|
|
177
|
+
return fresh()?.description ?? 'not set up'
|
|
178
|
+
},
|
|
179
|
+
get provider() {
|
|
180
|
+
return fresh()?.provider ?? 'none'
|
|
181
|
+
},
|
|
182
|
+
async send(message: EmailMessage) {
|
|
183
|
+
if (Date.now() - readAt >= ttl) await load()
|
|
184
|
+
return current
|
|
185
|
+
? current.send(message)
|
|
186
|
+
: { ok: false as const, error: 'This site has no e-mail set up.' }
|
|
187
|
+
},
|
|
188
|
+
refresh: load,
|
|
189
|
+
}
|
|
190
|
+
return live
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** What the card shows: never a key, only whether one is kept. */
|
|
194
|
+
export interface EmailSettingsView {
|
|
195
|
+
/** `config`/`off`: set by the site's configuration, read-only here. */
|
|
196
|
+
source: 'config' | 'off' | 'settings' | 'none'
|
|
197
|
+
provider: string | null
|
|
198
|
+
from: string | null
|
|
199
|
+
fromName: string | null
|
|
200
|
+
url: string | null
|
|
201
|
+
postmarkStream: string | null
|
|
202
|
+
sesRegion: string | null
|
|
203
|
+
sesAccessKeyId: string | null
|
|
204
|
+
keyKept: boolean
|
|
205
|
+
/** Whether a key can be kept at all: the site has a secret key to seal it with. */
|
|
206
|
+
canKeepKeys: boolean
|
|
207
|
+
status: EmailAvailability
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export interface EmailActor {
|
|
211
|
+
id: string | null
|
|
212
|
+
name: string
|
|
213
|
+
email: string | null
|
|
214
|
+
ipAddress?: string | null
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export type EmailOutcome = { ok: true } | { ok: false; status: number; message: string }
|
|
218
|
+
|
|
219
|
+
const text = (value: unknown) => (typeof value === 'string' ? value.trim() : '')
|
|
220
|
+
|
|
221
|
+
/** Settings → E-mail, as an administrator uses it. */
|
|
222
|
+
export function createEmailSettings(
|
|
223
|
+
db: Db,
|
|
224
|
+
config: Site,
|
|
225
|
+
options: { port: () => EmailPort | undefined; env?: Env },
|
|
226
|
+
) {
|
|
227
|
+
const env = options.env ?? Bun.env
|
|
228
|
+
const audit = (actor: EmailActor, action: string, detail: string) =>
|
|
229
|
+
recordAudit(db, {
|
|
230
|
+
category: 'settings',
|
|
231
|
+
action,
|
|
232
|
+
entityType: 'E-mail',
|
|
233
|
+
entityName: 'E-mail',
|
|
234
|
+
detail,
|
|
235
|
+
source: 'request',
|
|
236
|
+
userKey: actor.id,
|
|
237
|
+
userName: actor.name,
|
|
238
|
+
ipAddress: actor.ipAddress ?? null,
|
|
239
|
+
forwardedFor: null,
|
|
240
|
+
})
|
|
241
|
+
const refresh = async () => {
|
|
242
|
+
const port = options.port() as Partial<LiveEmail> | undefined
|
|
243
|
+
await port?.refresh?.()
|
|
244
|
+
}
|
|
245
|
+
const locked = (): EmailOutcome | undefined =>
|
|
246
|
+
configuredEmail(config, env)
|
|
247
|
+
? {
|
|
248
|
+
ok: false,
|
|
249
|
+
status: 409,
|
|
250
|
+
message: "E-mail is set by the site's configuration, so it can't be changed here.",
|
|
251
|
+
}
|
|
252
|
+
: undefined
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
async view(): Promise<EmailSettingsView> {
|
|
256
|
+
const status = emailAvailability(options.port())
|
|
257
|
+
const canKeepKeys = Boolean(usableSecretKey(config.secretKey))
|
|
258
|
+
const fixed = configuredEmail(config, env)
|
|
259
|
+
if (fixed) {
|
|
260
|
+
const fromEnv = !config.email && fixed === 'config'
|
|
261
|
+
return {
|
|
262
|
+
source: fixed,
|
|
263
|
+
provider:
|
|
264
|
+
fixed === 'off' ? null : (config.email?.provider ?? text(env.CROMBOLO_EMAIL_PROVIDER)),
|
|
265
|
+
from: fromEnv ? text(env.CROMBOLO_EMAIL_FROM) || null : null,
|
|
266
|
+
fromName: fromEnv ? text(env.CROMBOLO_EMAIL_FROM_NAME) || null : null,
|
|
267
|
+
url: fromEnv ? text(env.CROMBOLO_EMAIL_URL) || null : null,
|
|
268
|
+
postmarkStream: null,
|
|
269
|
+
sesRegion: fromEnv ? text(env.CROMBOLO_EMAIL_SES_REGION) || null : null,
|
|
270
|
+
sesAccessKeyId: null,
|
|
271
|
+
keyKept: false,
|
|
272
|
+
canKeepKeys,
|
|
273
|
+
status,
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const stored = await read(db)
|
|
277
|
+
return {
|
|
278
|
+
source: stored ? 'settings' : 'none',
|
|
279
|
+
provider: stored?.provider ?? null,
|
|
280
|
+
from: stored?.from ?? null,
|
|
281
|
+
fromName: stored?.fromName ?? null,
|
|
282
|
+
url: stored?.url ?? null,
|
|
283
|
+
postmarkStream: stored?.postmarkStream ?? null,
|
|
284
|
+
sesRegion: stored?.sesRegion ?? null,
|
|
285
|
+
sesAccessKeyId: stored?.sesAccessKeyId ?? null,
|
|
286
|
+
keyKept: Boolean(stored && keyOf(stored, config.secretKey)),
|
|
287
|
+
canKeepKeys,
|
|
288
|
+
status,
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
async save(actor: EmailActor, input: EmailSettingsInput): Promise<EmailOutcome> {
|
|
293
|
+
const refused = locked()
|
|
294
|
+
if (refused) return refused
|
|
295
|
+
const provider = text(input.provider) as StoredProvider
|
|
296
|
+
if (!STORED_PROVIDERS.includes(provider))
|
|
297
|
+
return { ok: false, status: 400, message: 'Choose a provider.' }
|
|
298
|
+
const from = text(input.from)
|
|
299
|
+
if (!isValidEmail(from))
|
|
300
|
+
return { ok: false, status: 400, message: 'Enter the address e-mail is sent from.' }
|
|
301
|
+
const next: Stored = {
|
|
302
|
+
provider,
|
|
303
|
+
from,
|
|
304
|
+
...(text(input.fromName) ? { fromName: text(input.fromName) } : {}),
|
|
305
|
+
}
|
|
306
|
+
if (provider === 'custom' || text(input.url)) {
|
|
307
|
+
const url = text(input.url)
|
|
308
|
+
if (provider === 'custom' && !url)
|
|
309
|
+
return { ok: false, status: 400, message: 'Enter the address messages are sent to.' }
|
|
310
|
+
if (url) next.url = url
|
|
311
|
+
}
|
|
312
|
+
if (provider === 'postmark' && text(input.postmarkStream))
|
|
313
|
+
next.postmarkStream = text(input.postmarkStream)
|
|
314
|
+
if (provider === 'ses') {
|
|
315
|
+
next.sesRegion = text(input.sesRegion)
|
|
316
|
+
next.sesAccessKeyId = text(input.sesAccessKeyId)
|
|
317
|
+
if (!next.sesRegion)
|
|
318
|
+
return { ok: false, status: 400, message: 'Enter the Amazon SES region.' }
|
|
319
|
+
if (!next.sesAccessKeyId)
|
|
320
|
+
return { ok: false, status: 400, message: 'Enter the access key ID.' }
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const key = text(input.key)
|
|
324
|
+
const secretKey = usableSecretKey(config.secretKey)
|
|
325
|
+
if (key) {
|
|
326
|
+
if (!secretKey)
|
|
327
|
+
return {
|
|
328
|
+
ok: false,
|
|
329
|
+
status: 400,
|
|
330
|
+
message:
|
|
331
|
+
"This site can't keep e-mail keys until whoever hosts it gives it a secret key.",
|
|
332
|
+
}
|
|
333
|
+
next.keySealed = sealSecret(key, secretKey)
|
|
334
|
+
next.keyFor = targetOf(next)
|
|
335
|
+
} else {
|
|
336
|
+
const before = await read(db)
|
|
337
|
+
// A kept key stays only while it is for the same provider and address.
|
|
338
|
+
if (before?.keySealed && before.keyFor === targetOf(next)) {
|
|
339
|
+
next.keySealed = before.keySealed
|
|
340
|
+
next.keyFor = before.keyFor
|
|
341
|
+
} else if (provider !== 'custom')
|
|
342
|
+
return {
|
|
343
|
+
ok: false,
|
|
344
|
+
status: 400,
|
|
345
|
+
message: before?.keySealed
|
|
346
|
+
? 'Enter the key again: a key is only used with the provider and address it was entered for.'
|
|
347
|
+
: 'Enter the key from your provider.',
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const problems: string[] = []
|
|
352
|
+
const built = portOf(next, config.secretKey, {
|
|
353
|
+
warning: (_message, properties) => problems.push(String(properties?.detail ?? '')),
|
|
354
|
+
})
|
|
355
|
+
if (!built)
|
|
356
|
+
return {
|
|
357
|
+
ok: false,
|
|
358
|
+
status: 400,
|
|
359
|
+
message: `Those settings can't be used. ${problems.filter(Boolean).join(' ')}`.trim(),
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
await write(db, next)
|
|
363
|
+
await audit(actor, 'Save', `${provider}, from ${from}${key ? ', key changed' : ''}`)
|
|
364
|
+
await refresh()
|
|
365
|
+
return { ok: true }
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
async clear(actor: EmailActor): Promise<EmailOutcome> {
|
|
369
|
+
const refused = locked()
|
|
370
|
+
if (refused) return refused
|
|
371
|
+
await write(db, undefined)
|
|
372
|
+
await audit(actor, 'Clear', 'e-mail settings removed')
|
|
373
|
+
await refresh()
|
|
374
|
+
return { ok: true }
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
/** A message to the administrator, through whatever is in force now. */
|
|
378
|
+
async test(actor: EmailActor): Promise<EmailOutcome> {
|
|
379
|
+
if (!actor.email || !isValidEmail(actor.email))
|
|
380
|
+
return { ok: false, status: 400, message: 'Your account has no e-mail address to send to.' }
|
|
381
|
+
const port = options.port()
|
|
382
|
+
if (!port || port.provider === 'none')
|
|
383
|
+
return { ok: false, status: 409, message: 'Set up e-mail first.' }
|
|
384
|
+
const result = await port.send({
|
|
385
|
+
to: [{ email: actor.email, name: actor.name }],
|
|
386
|
+
subject: 'A test e-mail from your site',
|
|
387
|
+
text: 'This message shows that your site can send e-mail.',
|
|
388
|
+
})
|
|
389
|
+
return result.ok
|
|
390
|
+
? { ok: true }
|
|
391
|
+
: { ok: false, status: 502, message: `The test e-mail was not sent: ${result.error}` }
|
|
392
|
+
},
|
|
393
|
+
}
|
|
394
|
+
}
|
package/src/email.ts
CHANGED
|
@@ -52,7 +52,8 @@ export interface EmailMessage {
|
|
|
52
52
|
export type EmailResult = { ok: true; id: string | null } | { ok: false; error: string }
|
|
53
53
|
|
|
54
54
|
/** Which adapter is in play; named in logs and in the availability report. */
|
|
55
|
-
|
|
55
|
+
/** `none`: a port set up in Settings (story 340) that has nothing set up yet. */
|
|
56
|
+
export type EmailProvider = 'resend' | 'postmark' | 'ses' | 'custom' | 'log' | 'none'
|
|
56
57
|
|
|
57
58
|
export interface EmailPort {
|
|
58
59
|
/** Named in the boot banner and in logs: `resend`, `postmark (broadcast)`, … */
|
|
@@ -224,13 +225,17 @@ export const EMAIL_DEPENDENT_FEATURES = [
|
|
|
224
225
|
export const EMAIL_SETUP_HINT =
|
|
225
226
|
'Set CROMBOLO_EMAIL_PROVIDER to resend, postmark or custom with its key and CROMBOLO_EMAIL_FROM, or pass `email` in crombolo.config.ts. See docs/configuration.md.'
|
|
226
227
|
|
|
228
|
+
/** Whether there is a port that would take a message, the console's included. */
|
|
229
|
+
export const emailReady = (port: EmailPort | undefined): port is EmailPort =>
|
|
230
|
+
port !== undefined && port.provider !== 'none'
|
|
231
|
+
|
|
227
232
|
export function emailAvailability(port: EmailPort | undefined): EmailAvailability {
|
|
228
|
-
if (!port)
|
|
233
|
+
if (!emailReady(port))
|
|
229
234
|
return {
|
|
230
235
|
available: false,
|
|
231
236
|
provider: null,
|
|
232
237
|
description: null,
|
|
233
|
-
reason:
|
|
238
|
+
reason: 'No e-mail provider is configured. Set one up in Settings, under E-mail.',
|
|
234
239
|
affects: [...EMAIL_DEPENDENT_FEATURES],
|
|
235
240
|
}
|
|
236
241
|
if (port.provider === 'log')
|
|
@@ -239,7 +244,7 @@ export function emailAvailability(port: EmailPort | undefined): EmailAvailabilit
|
|
|
239
244
|
provider: 'log',
|
|
240
245
|
description: port.description,
|
|
241
246
|
reason:
|
|
242
|
-
|
|
247
|
+
"E-mail is written to the server's log in development, not sent. Set up a provider before relying on it.",
|
|
243
248
|
affects: [],
|
|
244
249
|
}
|
|
245
250
|
return {
|
|
@@ -32,6 +32,9 @@ export interface ExtensionComponents {
|
|
|
32
32
|
layouts(): { id: string; layout: string }[]
|
|
33
33
|
/** Every component the extensions offer, as `<id>/<name>` with the extension's name: for the template picker. */
|
|
34
34
|
listed(): { alias: string; extension: string }[]
|
|
35
|
+
/** An extension started while the site runs (story 346), in place of one with its id. */
|
|
36
|
+
add(source: ExtensionComponentSource): void
|
|
37
|
+
remove(id: string): void
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
const BLOCKS = 'blocks/'
|
|
@@ -70,7 +73,8 @@ export function createExtensionComponents(
|
|
|
70
73
|
sources: readonly ExtensionComponentSource[],
|
|
71
74
|
options: { timeoutMs?: number } = {},
|
|
72
75
|
): ExtensionComponents {
|
|
73
|
-
const
|
|
76
|
+
const live = [...sources]
|
|
77
|
+
const byId = new Map(live.map((source) => [source.process.id, source]))
|
|
74
78
|
const warned = new Set<string>()
|
|
75
79
|
/** Components whose removals were logged this boot, by extension and name. */
|
|
76
80
|
const stripped = new Set<string>()
|
|
@@ -138,13 +142,19 @@ export function createExtensionComponents(
|
|
|
138
142
|
source.process.description?.components?.find((component) => component.name === name)?.layout,
|
|
139
143
|
})
|
|
140
144
|
|
|
145
|
+
const remove = (id: string) => {
|
|
146
|
+
const at = live.findIndex((source) => source.process.id === id)
|
|
147
|
+
if (at >= 0) live.splice(at, 1)
|
|
148
|
+
byId.delete(id)
|
|
149
|
+
}
|
|
150
|
+
|
|
141
151
|
return {
|
|
142
152
|
module(alias) {
|
|
143
153
|
// A block's component is found by its element type's alias, in whichever
|
|
144
154
|
// extension offers it.
|
|
145
155
|
if (alias.startsWith(BLOCKS)) {
|
|
146
156
|
const name = alias
|
|
147
|
-
const source =
|
|
157
|
+
const source = live.find((candidate) => offers(candidate, name))
|
|
148
158
|
return source ? moduleFor(source, name) : undefined
|
|
149
159
|
}
|
|
150
160
|
const slash = alias.indexOf('/')
|
|
@@ -154,13 +164,13 @@ export function createExtensionComponents(
|
|
|
154
164
|
return source && offers(source, name) ? moduleFor(source, name) : undefined
|
|
155
165
|
},
|
|
156
166
|
layouts() {
|
|
157
|
-
return
|
|
167
|
+
return live.flatMap((source) => {
|
|
158
168
|
const layout = source.layout()
|
|
159
169
|
return layout ? [{ id: source.process.id, layout }] : []
|
|
160
170
|
})
|
|
161
171
|
},
|
|
162
172
|
listed() {
|
|
163
|
-
return
|
|
173
|
+
return live.flatMap((source) =>
|
|
164
174
|
(source.process.description?.components ?? [])
|
|
165
175
|
.filter((component) => !component.name.startsWith(BLOCKS))
|
|
166
176
|
.map((component) => ({
|
|
@@ -169,5 +179,11 @@ export function createExtensionComponents(
|
|
|
169
179
|
})),
|
|
170
180
|
)
|
|
171
181
|
},
|
|
182
|
+
add(source) {
|
|
183
|
+
remove(source.process.id)
|
|
184
|
+
live.push(source)
|
|
185
|
+
byId.set(source.process.id, source)
|
|
186
|
+
},
|
|
187
|
+
remove,
|
|
172
188
|
}
|
|
173
189
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Installed extensions run once granted, with nothing imported by the site
|
|
3
|
-
* (story 310), each in a Deno process of its own (story 311). At boot,
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
3
|
+
* (story 310), each in a Deno process of its own (story 311). At boot, and as
|
|
4
|
+
* it is granted while the site runs (story 346), each dependency with a server
|
|
5
|
+
* half that the config's `extensions` entry grants, or that a recorded grant
|
|
6
|
+
* covers, is built, started with its settings, and mounted from what it
|
|
7
|
+
* describes. One that cannot start is left off, and the site runs without it.
|
|
8
|
+
* docs/bundles.md, "Extensions run once granted".
|
|
8
9
|
*/
|
|
9
10
|
import { join } from 'node:path'
|
|
10
11
|
import type { BundlePackage } from '@crombolo/backoffice-host'
|
|
@@ -125,6 +126,75 @@ export interface FailedExtension {
|
|
|
125
126
|
reason: string
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
/** How starting one extension went: running, refused or broken, or not granted. */
|
|
130
|
+
export type ExtensionStart =
|
|
131
|
+
| { status: 'started'; bundle: ServerBundle; process: ExtensionProcess }
|
|
132
|
+
| { status: 'failed'; reason: string }
|
|
133
|
+
| { status: 'not-granted' }
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Starts one installed server half, at boot or while the site runs (story 346):
|
|
137
|
+
* when the config's entry or its recorded grant covers it, built, started with
|
|
138
|
+
* its settings, checked that it can run apart, and given its stand-in.
|
|
139
|
+
*/
|
|
140
|
+
export async function startExtension(
|
|
141
|
+
config: CromboloConfig,
|
|
142
|
+
pkg: BundlePackage,
|
|
143
|
+
recorded: { grant?: BundleGrant; settings?: Record<string, unknown> },
|
|
144
|
+
log: BundleLog,
|
|
145
|
+
activity?: ActivityRecorder,
|
|
146
|
+
): Promise<ExtensionStart> {
|
|
147
|
+
// Story 326: an extension runs from a root of its own, never as a dependency of the site.
|
|
148
|
+
const refused =
|
|
149
|
+
pkg.linked && Bun.env.NODE_ENV === 'production'
|
|
150
|
+
? LINKED_IN_PRODUCTION
|
|
151
|
+
: pkg.dependency
|
|
152
|
+
? INSTALLED_AS_DEPENDENCY
|
|
153
|
+
: undefined
|
|
154
|
+
if (refused) {
|
|
155
|
+
log.error('{package} is not started: {reason}', { package: pkg.packageName, reason: refused })
|
|
156
|
+
return { status: 'failed', reason: refused }
|
|
157
|
+
}
|
|
158
|
+
const entry = config.extensions?.[pkg.id]
|
|
159
|
+
const { grant } = recorded
|
|
160
|
+
const byConfig = entryCovers(pkg, entry)
|
|
161
|
+
const granted =
|
|
162
|
+
byConfig || Boolean(grant?.recorded && !grant.disabledAt && grantGaps(pkg, grant).length === 0)
|
|
163
|
+
// Nothing of an extension that is not granted is built, let alone run.
|
|
164
|
+
if (!granted) return { status: 'not-granted' }
|
|
165
|
+
const process = new ExtensionProcess({
|
|
166
|
+
pkg,
|
|
167
|
+
siteDir: config.siteDir,
|
|
168
|
+
home: join(config.siteDir, '.crombolo', 'extensions', pkg.id),
|
|
169
|
+
settings: settingsOf(pkg, recorded.settings, entry, config.secretKey),
|
|
170
|
+
dialect: config.dialect,
|
|
171
|
+
origins: [
|
|
172
|
+
...((byConfig ? entry?.origins : grant?.origins) ?? []),
|
|
173
|
+
...(entry?.dev?.allowNet ?? []),
|
|
174
|
+
],
|
|
175
|
+
reads: entry?.dev?.allowRead,
|
|
176
|
+
timeoutMs: entry?.timeoutMs,
|
|
177
|
+
log,
|
|
178
|
+
activity,
|
|
179
|
+
})
|
|
180
|
+
try {
|
|
181
|
+
const description = await process.start()
|
|
182
|
+
const problems = isolationProblems(description)
|
|
183
|
+
if (description.id !== pkg.id)
|
|
184
|
+
problems.push(`it calls itself "${description.id}", where its package.json says "${pkg.id}"`)
|
|
185
|
+
if (problems.length > 0) throw new Error(`It cannot run: ${problems.join('; ')}.`)
|
|
186
|
+
return { status: 'started', bundle: isolatedBundle(process), process }
|
|
187
|
+
} catch (error) {
|
|
188
|
+
await process.stop()
|
|
189
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
190
|
+
log.error('{package} could not be started, and the site runs without it: {reason}', {
|
|
191
|
+
package: pkg.packageName,
|
|
192
|
+
reason,
|
|
193
|
+
})
|
|
194
|
+
return { status: 'failed', reason }
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
128
198
|
/**
|
|
129
199
|
* The installed server halves to start, each in its own process with its
|
|
130
200
|
* settings: those the config grants and those a recorded grant covers. Ones the
|
|
@@ -147,68 +217,17 @@ export async function startExtensions(
|
|
|
147
217
|
const failed: FailedExtension[] = []
|
|
148
218
|
for (const pkg of packages) {
|
|
149
219
|
if (!pkg.carries.server || wired.has(pkg.id)) continue
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
failed.push({ id: pkg.id, reason: LINKED_IN_PRODUCTION })
|
|
153
|
-
log(pkg.id).error('{package} is not started: {reason}', {
|
|
154
|
-
package: pkg.packageName,
|
|
155
|
-
reason: LINKED_IN_PRODUCTION,
|
|
156
|
-
})
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
if (pkg.dependency) {
|
|
160
|
-
failed.push({ id: pkg.id, reason: INSTALLED_AS_DEPENDENCY })
|
|
161
|
-
log(pkg.id).error('{package} is not started: {reason}', {
|
|
162
|
-
package: pkg.packageName,
|
|
163
|
-
reason: INSTALLED_AS_DEPENDENCY,
|
|
164
|
-
})
|
|
165
|
-
continue
|
|
166
|
-
}
|
|
167
|
-
const entry = config.extensions?.[pkg.id]
|
|
168
|
-
const grant = recorded.grants.get(pkg.id)
|
|
169
|
-
const byConfig = entryCovers(pkg, entry)
|
|
170
|
-
const granted =
|
|
171
|
-
byConfig ||
|
|
172
|
-
Boolean(grant?.recorded && !grant.disabledAt && grantGaps(pkg, grant).length === 0)
|
|
173
|
-
// Nothing of an extension that is not granted is built, let alone run.
|
|
174
|
-
if (!granted) continue
|
|
175
|
-
const process = new ExtensionProcess({
|
|
220
|
+
const started = await startExtension(
|
|
221
|
+
config,
|
|
176
222
|
pkg,
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
settings: settingsOf(pkg, recorded.settings.get(pkg.id), entry, config.secretKey),
|
|
180
|
-
dialect: config.dialect,
|
|
181
|
-
origins: [
|
|
182
|
-
...((byConfig ? entry?.origins : grant?.origins) ?? []),
|
|
183
|
-
...(entry?.dev?.allowNet ?? []),
|
|
184
|
-
],
|
|
185
|
-
reads: entry?.dev?.allowRead,
|
|
186
|
-
timeoutMs: entry?.timeoutMs,
|
|
187
|
-
log: log(pkg.id),
|
|
223
|
+
{ grant: recorded.grants.get(pkg.id), settings: recorded.settings.get(pkg.id) },
|
|
224
|
+
log(pkg.id),
|
|
188
225
|
activity,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
problems.push(
|
|
195
|
-
`it calls itself "${description.id}", where its package.json says "${pkg.id}"`,
|
|
196
|
-
)
|
|
197
|
-
if (problems.length > 0) throw new Error(`It cannot run: ${problems.join('; ')}.`)
|
|
198
|
-
bundles.push(isolatedBundle(process))
|
|
199
|
-
processes.push(process)
|
|
200
|
-
} catch (error) {
|
|
201
|
-
await process.stop()
|
|
202
|
-
const reason = error instanceof Error ? error.message : String(error)
|
|
203
|
-
failed.push({ id: pkg.id, reason })
|
|
204
|
-
log(pkg.id).error(
|
|
205
|
-
'{package} could not be started, and the site starts without it: {reason}',
|
|
206
|
-
{
|
|
207
|
-
package: pkg.packageName,
|
|
208
|
-
reason,
|
|
209
|
-
},
|
|
210
|
-
)
|
|
211
|
-
}
|
|
226
|
+
)
|
|
227
|
+
if (started.status === 'started') {
|
|
228
|
+
bundles.push(started.bundle)
|
|
229
|
+
processes.push(started.process)
|
|
230
|
+
} else if (started.status === 'failed') failed.push({ id: pkg.id, reason: started.reason })
|
|
212
231
|
}
|
|
213
232
|
return { bundles, processes, failed }
|
|
214
233
|
}
|