@crombolo/server 0.28.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 +3 -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 +22 -9
- package/src/seed/export.ts +334 -67
- package/src/seed/keys.ts +24 -16
- 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/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) => {
|