@crombolo/server 0.27.0 → 0.29.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/package.json +12 -12
- package/src/bundle-build.ts +139 -0
- package/src/bundle-reload.ts +84 -0
- package/src/config.ts +17 -1
- package/src/database.ts +4 -4
- package/src/index.ts +4 -0
- package/src/marketplace.ts +14 -3
- package/src/media-folders.ts +32 -0
- package/src/schema.ts +0 -6
- package/src/seed/compile.ts +745 -0
- package/src/seed/export.ts +416 -0
- package/src/seed/index.ts +5 -0
- package/src/seed/keys.ts +35 -0
- package/src/seed/lines.ts +58 -0
- package/src/seed/read.ts +175 -0
- package/src/server.ts +23 -1
- package/src/settings.ts +13 -0
- package/src/webhook-files.ts +50 -13
- package/src/webhooks.ts +59 -6
package/src/server.ts
CHANGED
|
@@ -122,6 +122,7 @@ import { createPublishedContentSource, loadDraftNode } from './adapters/document
|
|
|
122
122
|
import type { Approvals } from './approvals.ts'
|
|
123
123
|
import { handleApprovalsApi } from './approvals-api.ts'
|
|
124
124
|
import { CommandError, installBundleContent } from './bundle-install.ts'
|
|
125
|
+
import { BUNDLES_CHANGED_EVENT, type BundleReload, createBundleReloader } from './bundle-reload.ts'
|
|
125
126
|
import { bundleChains, bundleTablesReport } from './bundle-tables.ts'
|
|
126
127
|
import { createBundleRedirects, createServerBundles, validateServerBundles } from './bundles.ts'
|
|
127
128
|
import { busyResponse } from './busy.ts'
|
|
@@ -270,6 +271,12 @@ export interface ServerHandle {
|
|
|
270
271
|
}
|
|
271
272
|
/** Scheduled publishing and version cleanup; each run can be called directly. */
|
|
272
273
|
jobs: BackgroundJobs
|
|
274
|
+
/**
|
|
275
|
+
* Reads the site's `package.json` and `node_modules` again, after a host has
|
|
276
|
+
* changed them (story 181), and says what changed. Back-office halves only:
|
|
277
|
+
* nothing is mounted, and a `web` node, which serves no back office, does nothing.
|
|
278
|
+
*/
|
|
279
|
+
reloadBundles(): Promise<BundleReload>
|
|
273
280
|
/** Stops the watcher and the poll, then closes the database. */
|
|
274
281
|
close(): Promise<void>
|
|
275
282
|
}
|
|
@@ -301,17 +308,26 @@ export async function createServer(config: CromboloConfig = loadConfig()): Promi
|
|
|
301
308
|
// Extensions are the site's own npm dependencies (`docs/bundles.md`).
|
|
302
309
|
// Uncached in development so an author editing one sees a reload pick it up.
|
|
303
310
|
const extensions = createExtensionRegistry(config.siteDir, { cache: !config.development })
|
|
311
|
+
// A host that installs packages itself calls `reloadBundles()` (story 181); the
|
|
312
|
+
// Marketplace's own install goes through it too, so either way open back
|
|
313
|
+
// offices hear about it. `events` is created further down, before any reload.
|
|
314
|
+
const bundleReloader = createBundleReloader({
|
|
315
|
+
extensions,
|
|
316
|
+
wired: () => bundles?.mounted.map((bundle) => bundle.id) ?? [],
|
|
317
|
+
changed: () => events.broadcast('notify', [BUNDLES_CHANGED_EVENT]),
|
|
318
|
+
})
|
|
304
319
|
const marketplace = createMarketplace({
|
|
305
320
|
registry: config.npmRegistry,
|
|
306
321
|
keyword: config.packageKeyword,
|
|
307
322
|
marketplaceUrl: config.marketplaceUrl,
|
|
308
|
-
extensions,
|
|
323
|
+
extensions: { ...extensions, invalidate: () => void bundleReloader.reload() },
|
|
309
324
|
siteDir: config.siteDir,
|
|
310
325
|
wired: () => bundles?.mounted.map((bundle) => bundle.id) ?? [],
|
|
311
326
|
contentApplied: async (packageName) =>
|
|
312
327
|
(await new TransferRunRepository(db).appliedForPackage(packageName))?.packageVersion ?? null,
|
|
313
328
|
applyContentOnInstall: config.applyBundleContentOnInstall,
|
|
314
329
|
installs: config.runtimeInstalls,
|
|
330
|
+
installsElsewhere: config.installsElsewhere,
|
|
315
331
|
applyContent: async (packageName) => {
|
|
316
332
|
// A package from the marketplace is for anyone, so nothing in it can be
|
|
317
333
|
// sealed; one that is is refused rather than half applied.
|
|
@@ -1438,6 +1454,7 @@ export async function createServer(config: CromboloConfig = loadConfig()): Promi
|
|
|
1438
1454
|
keyword: config.packageKeyword,
|
|
1439
1455
|
appliesContent: config.applyBundleContentOnInstall,
|
|
1440
1456
|
installs: config.runtimeInstalls,
|
|
1457
|
+
installsElsewhere: config.runtimeInstalls ? null : (config.installsElsewhere ?? null),
|
|
1441
1458
|
items: await marketplace.search(url.searchParams.get('q') ?? undefined),
|
|
1442
1459
|
})
|
|
1443
1460
|
}
|
|
@@ -1461,6 +1478,7 @@ export async function createServer(config: CromboloConfig = loadConfig()): Promi
|
|
|
1461
1478
|
if (action === 'installed' && request.method === 'GET') {
|
|
1462
1479
|
return Response.json({
|
|
1463
1480
|
installs: config.runtimeInstalls,
|
|
1481
|
+
installsElsewhere: config.runtimeInstalls ? null : (config.installsElsewhere ?? null),
|
|
1464
1482
|
items: await marketplace.installed(),
|
|
1465
1483
|
})
|
|
1466
1484
|
}
|
|
@@ -2492,6 +2510,10 @@ export async function createServer(config: CromboloConfig = loadConfig()): Promi
|
|
|
2492
2510
|
health,
|
|
2493
2511
|
events,
|
|
2494
2512
|
jobs,
|
|
2513
|
+
reloadBundles: async () =>
|
|
2514
|
+
servesBackOffice
|
|
2515
|
+
? bundleReloader.reload()
|
|
2516
|
+
: { added: [], removed: [], changed: [], problems: [] },
|
|
2495
2517
|
serveOptions: {
|
|
2496
2518
|
port: config.port,
|
|
2497
2519
|
maxRequestBodySize: listeningBodyLimit(config),
|
package/src/settings.ts
CHANGED
|
@@ -306,6 +306,19 @@ const DECLARED = [
|
|
|
306
306
|
description:
|
|
307
307
|
'The Packages section installs and removes packages on the running site. Off, they are added to `package.json` and deployed.',
|
|
308
308
|
},
|
|
309
|
+
{
|
|
310
|
+
env: 'CROMBOLO_INSTALLS_ELSEWHERE_MESSAGE',
|
|
311
|
+
kind: 'string',
|
|
312
|
+
section: 'Back office and bundles',
|
|
313
|
+
description:
|
|
314
|
+
'With runtime installs off, what the Marketplace says about where bundles are added, in place of "add it to package.json and deploy". For a host that installs them itself.',
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
env: 'CROMBOLO_INSTALLS_ELSEWHERE_URL',
|
|
318
|
+
kind: 'string',
|
|
319
|
+
section: 'Back office and bundles',
|
|
320
|
+
description: 'A link shown with that message: the host’s own place for installing bundles.',
|
|
321
|
+
},
|
|
309
322
|
{
|
|
310
323
|
env: 'CROMBOLO_MARKETPLACE_URL',
|
|
311
324
|
kind: 'string',
|
package/src/webhook-files.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A webhook as a file, `schema/webhooks/<
|
|
3
|
-
* is held to. Pure: reading and writing the directory is `webhooks.ts`'s
|
|
4
|
-
* docs/webhooks.md.
|
|
2
|
+
* A webhook as a file, `schema/webhooks/<alias>.toml`, and the rules every
|
|
3
|
+
* webhook is held to. Pure: reading and writing the directory is `webhooks.ts`'s
|
|
4
|
+
* job. docs/webhooks.md.
|
|
5
|
+
*
|
|
6
|
+
* Since story 161 a webhook file names itself by alias, carries no key unless
|
|
7
|
+
* one differs from the one its alias derives, and names its content types by
|
|
8
|
+
* alias. A legacy `<key>.toml` with keys still reads.
|
|
5
9
|
*/
|
|
6
10
|
import { WEBHOOK_EVENT_ALIASES } from '@crombolo/api-management'
|
|
11
|
+
import { derivedKey } from '@crombolo/core'
|
|
7
12
|
|
|
8
13
|
export type WebhookStore = 'files' | 'database'
|
|
9
14
|
|
|
10
15
|
export interface WebhookDefinition {
|
|
11
16
|
key: string
|
|
17
|
+
/** A file's name for it (story 161); absent on a webhook kept in the database. */
|
|
18
|
+
alias?: string
|
|
12
19
|
name: string | null
|
|
13
20
|
description: string | null
|
|
14
21
|
url: string
|
|
15
22
|
enabled: boolean
|
|
16
23
|
events: string[]
|
|
24
|
+
/** In memory, keys; in a file, aliases (a uuid-shaped value is a key). */
|
|
17
25
|
contentTypeKeys: string[]
|
|
18
26
|
/** A value is either `env:NAME` or, kept in the database only, the secret itself. */
|
|
19
27
|
headers: Record<string, string>
|
|
@@ -62,16 +70,38 @@ const quote = (value: string) => JSON.stringify(value)
|
|
|
62
70
|
const list = (values: readonly string[]) => `[${values.map(quote).join(', ')}]`
|
|
63
71
|
const key = (name: string) => (/^[A-Za-z0-9_-]+$/.test(name) ? name : quote(name))
|
|
64
72
|
|
|
65
|
-
/**
|
|
66
|
-
export
|
|
67
|
-
|
|
73
|
+
/** The key a webhook's alias derives, so a file need not carry one (story 161). */
|
|
74
|
+
export const webhookKeyFor = (alias: string) => derivedKey('webhook', alias)
|
|
75
|
+
|
|
76
|
+
/** An alias from a name, as a file takes one: `Order placed` → `orderPlaced`. */
|
|
77
|
+
export function webhookAliasFor(name: string | null, key: string): string {
|
|
78
|
+
const words = (name ?? '').split(/[^A-Za-z0-9]+/).filter(Boolean)
|
|
79
|
+
const camel = words
|
|
80
|
+
.map((w, i) =>
|
|
81
|
+
i === 0 ? w.charAt(0).toLowerCase() + w.slice(1) : w.charAt(0).toUpperCase() + w.slice(1),
|
|
82
|
+
)
|
|
83
|
+
.join('')
|
|
84
|
+
return /^[A-Za-z]/.test(camel) ? camel : `webhook${key.replaceAll('-', '').slice(0, 8)}`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Canonical TOML, so a backoffice edit is a small diff. `contentTypes` is what
|
|
89
|
+
* the file names them by — aliases — and defaults to the keys.
|
|
90
|
+
*/
|
|
91
|
+
export function writeWebhookFile(
|
|
92
|
+
webhook: WebhookDefinition,
|
|
93
|
+
contentTypes: readonly string[] = webhook.contentTypeKeys,
|
|
94
|
+
): string {
|
|
95
|
+
const alias = webhook.alias ?? webhookAliasFor(webhook.name, webhook.key)
|
|
96
|
+
const out = ['[webhook]']
|
|
97
|
+
if (webhook.key.toLowerCase() !== webhookKeyFor(alias)) out.push(`key = ${quote(webhook.key)}`)
|
|
98
|
+
out.push(`alias = ${quote(alias)}`)
|
|
68
99
|
if (webhook.name) out.push(`name = ${quote(webhook.name)}`)
|
|
69
100
|
if (webhook.description) out.push(`description = ${quote(webhook.description)}`)
|
|
70
101
|
out.push(`url = ${quote(webhook.url)}`)
|
|
71
102
|
if (!webhook.enabled) out.push('enabled = false')
|
|
72
103
|
out.push(`events = ${list(webhook.events)}`)
|
|
73
|
-
if (
|
|
74
|
-
out.push(`content-types = ${list(webhook.contentTypeKeys)}`)
|
|
104
|
+
if (contentTypes.length > 0) out.push(`content-types = ${list(contentTypes)}`)
|
|
75
105
|
const headers = Object.keys(webhook.headers).sort()
|
|
76
106
|
if (headers.length > 0) {
|
|
77
107
|
out.push('', '[webhook.headers]')
|
|
@@ -82,6 +112,7 @@ export function writeWebhookFile(webhook: WebhookDefinition): string {
|
|
|
82
112
|
|
|
83
113
|
const KEYS = new Set([
|
|
84
114
|
'key',
|
|
115
|
+
'alias',
|
|
85
116
|
'name',
|
|
86
117
|
'description',
|
|
87
118
|
'url',
|
|
@@ -120,17 +151,23 @@ export function parseWebhookFile(
|
|
|
120
151
|
for (const [name, value] of Object.entries(table.headers as Record<string, unknown>))
|
|
121
152
|
if (typeof value === 'string') headers[name] = value
|
|
122
153
|
else problems.push(`${file}: webhook.headers.${name} must be text`)
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
154
|
+
const webhookAlias = text('alias') ?? undefined
|
|
155
|
+
const written = text('key')
|
|
156
|
+
if (written !== null && !/^[0-9a-f-]{36}$/i.test(written))
|
|
157
|
+
problems.push(`${file}: webhook.key must be a key`)
|
|
158
|
+
if (!written && !webhookAlias) problems.push(`${file}: webhook.alias is required`)
|
|
159
|
+
const webhookKey = written ?? (webhookAlias ? webhookKeyFor(webhookAlias) : '')
|
|
126
160
|
const webhook: WebhookDefinition = {
|
|
127
|
-
key:
|
|
161
|
+
key: webhookKey.toLowerCase(),
|
|
162
|
+
...(webhookAlias ? { alias: webhookAlias } : {}),
|
|
128
163
|
name: text('name'),
|
|
129
164
|
description: text('description'),
|
|
130
165
|
url: text('url') ?? '',
|
|
131
166
|
enabled: table.enabled !== false,
|
|
132
167
|
events: strings('events'),
|
|
133
|
-
contentTypeKeys: strings('content-types').map((value) =>
|
|
168
|
+
contentTypeKeys: strings('content-types').map((value) =>
|
|
169
|
+
/^[0-9a-f-]{36}$/i.test(value) ? value.toLowerCase() : value,
|
|
170
|
+
),
|
|
134
171
|
headers,
|
|
135
172
|
}
|
|
136
173
|
problems.push(...webhookProblems(webhook, 'files').map((message) => `${file}: ${message}`))
|
package/src/webhooks.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync
|
|
|
7
7
|
import { join } from 'node:path'
|
|
8
8
|
import { type RaisedWebhookEvent, WEBHOOK_EVENT_ALIASES } from '@crombolo/api-management'
|
|
9
9
|
import {
|
|
10
|
+
ContentTypeRepository,
|
|
10
11
|
type Db,
|
|
11
12
|
type WebhookDelivery,
|
|
12
13
|
WebhookDeliveryRepository,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
parseWebhookFile,
|
|
23
24
|
type WebhookDefinition,
|
|
24
25
|
type WebhookStore,
|
|
26
|
+
webhookAliasFor,
|
|
25
27
|
webhookProblems,
|
|
26
28
|
writeWebhookFile,
|
|
27
29
|
} from './webhook-files.ts'
|
|
@@ -188,9 +190,10 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
188
190
|
|
|
189
191
|
const stores = () => ({ files: options.filesWritable, database: secret !== undefined })
|
|
190
192
|
|
|
191
|
-
|
|
193
|
+
/** Each webhook file, by its file name, as written: content types still aliases. */
|
|
194
|
+
const filesOnDisk = (): Array<{ name: string; webhook: WebhookDefinition }> => {
|
|
192
195
|
if (!existsSync(dir)) return []
|
|
193
|
-
const out: WebhookDefinition
|
|
196
|
+
const out: Array<{ name: string; webhook: WebhookDefinition }> = []
|
|
194
197
|
for (const name of readdirSync(dir)
|
|
195
198
|
.filter((file) => file.endsWith('.toml'))
|
|
196
199
|
.sort()) {
|
|
@@ -198,12 +201,41 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
198
201
|
`schema/webhooks/${name}`,
|
|
199
202
|
readFileSync(join(dir, name), 'utf8'),
|
|
200
203
|
)
|
|
201
|
-
if (parsed.webhook) out.push(parsed.webhook)
|
|
204
|
+
if (parsed.webhook) out.push({ name, webhook: parsed.webhook })
|
|
202
205
|
else log.warning('A webhook file was skipped: {problems}', { problems: parsed.problems })
|
|
203
206
|
}
|
|
204
207
|
return out
|
|
205
208
|
}
|
|
206
209
|
|
|
210
|
+
/**
|
|
211
|
+
* The files' webhooks with their content types as keys (story 161). An alias
|
|
212
|
+
* no type has is logged and kept as a filter nothing matches, so a webhook
|
|
213
|
+
* meant for some types never widens to every type.
|
|
214
|
+
*/
|
|
215
|
+
const fromFiles = async (): Promise<WebhookDefinition[]> => {
|
|
216
|
+
const types = new ContentTypeRepository(options.db)
|
|
217
|
+
const out: WebhookDefinition[] = []
|
|
218
|
+
for (const { name, webhook } of filesOnDisk()) {
|
|
219
|
+
const keys: string[] = []
|
|
220
|
+
for (const value of webhook.contentTypeKeys) {
|
|
221
|
+
if (/^[0-9a-f-]{36}$/i.test(value)) keys.push(value)
|
|
222
|
+
else {
|
|
223
|
+
const found = await types.byAlias(value)
|
|
224
|
+
if (found) keys.push(found.key.toLowerCase())
|
|
225
|
+
else {
|
|
226
|
+
log.warning('Webhook {file} names a content type {alias} this site has not got', {
|
|
227
|
+
file: `schema/webhooks/${name}`,
|
|
228
|
+
alias: value,
|
|
229
|
+
})
|
|
230
|
+
keys.push(`unknown:${value}`)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
out.push({ ...webhook, contentTypeKeys: keys })
|
|
235
|
+
}
|
|
236
|
+
return out
|
|
237
|
+
}
|
|
238
|
+
|
|
207
239
|
const headersOf = (row: WebhookRow): Record<string, string> => {
|
|
208
240
|
if (!row.sealedHeaders) return {}
|
|
209
241
|
if (!secret)
|
|
@@ -226,17 +258,38 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
226
258
|
})
|
|
227
259
|
|
|
228
260
|
const all = async (): Promise<WebhookView[]> => [
|
|
229
|
-
...fromFiles().map((webhook) => ({ ...webhook, store: 'files' as const })),
|
|
261
|
+
...(await fromFiles()).map((webhook) => ({ ...webhook, store: 'files' as const })),
|
|
230
262
|
...(await fromDatabase()).map((webhook) => ({ ...webhook, store: 'database' as const })),
|
|
231
263
|
]
|
|
232
264
|
|
|
265
|
+
/** Content types by alias, as a file names them; a key no type has stays a key. */
|
|
266
|
+
const contentTypeAliases = async (keys: readonly string[]) => {
|
|
267
|
+
const types = new ContentTypeRepository(options.db)
|
|
268
|
+
const out: string[] = []
|
|
269
|
+
for (const key of keys) out.push((await types.byKey(key))?.alias ?? key)
|
|
270
|
+
return out
|
|
271
|
+
}
|
|
272
|
+
/** The files that hold a webhook, legacy `<key>.toml` among them. */
|
|
273
|
+
const filesFor = (key: string) =>
|
|
274
|
+
filesOnDisk()
|
|
275
|
+
.filter(({ webhook }) => webhook.key === key.toLowerCase())
|
|
276
|
+
.map(({ name }) => name)
|
|
233
277
|
const writeFile = async (webhook: WebhookDefinition) => {
|
|
234
278
|
mkdirSync(dir, { recursive: true })
|
|
235
|
-
|
|
279
|
+
const alias = webhook.alias ?? webhookAliasFor(webhook.name, webhook.key)
|
|
280
|
+
const name = `${alias}.toml`
|
|
281
|
+
// Renamed, or a legacy `<key>.toml`: the old file goes, so one webhook is one file.
|
|
282
|
+
for (const old of filesFor(webhook.key))
|
|
283
|
+
if (old !== name) rmSync(join(dir, old), { force: true })
|
|
284
|
+
writeFileSync(
|
|
285
|
+
join(dir, name),
|
|
286
|
+
writeWebhookFile({ ...webhook, alias }, await contentTypeAliases(webhook.contentTypeKeys)),
|
|
287
|
+
'utf8',
|
|
288
|
+
)
|
|
236
289
|
await options.onFilesChanged?.()
|
|
237
290
|
}
|
|
238
291
|
const removeFile = async (key: string) => {
|
|
239
|
-
rmSync(join(dir,
|
|
292
|
+
for (const name of filesFor(key)) rmSync(join(dir, name), { force: true })
|
|
240
293
|
await options.onFilesChanged?.()
|
|
241
294
|
}
|
|
242
295
|
const writeRow = async (webhook: WebhookDefinition) => {
|