@crombolo/server 0.23.0 → 0.25.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 +13 -13
- package/src/adapters/dictionary.ts +20 -7
- package/src/adapters/documents.ts +11 -0
- package/src/adapters/health.ts +96 -1
- package/src/adapters/media.ts +63 -23
- package/src/adapters/redirects.ts +17 -5
- package/src/approvals.ts +17 -3
- package/src/assistant.ts +7 -2
- package/src/bundle-install.ts +22 -5
- package/src/config.ts +11 -1
- package/src/content-dispatch.ts +87 -0
- package/src/content-events.ts +49 -36
- package/src/content-notifications.ts +101 -56
- package/src/content-operations.ts +19 -4
- package/src/index.ts +1 -0
- package/src/jobs.ts +104 -75
- package/src/ports.ts +43 -10
- package/src/redirects.ts +10 -1
- package/src/schema.ts +66 -2
- package/src/server.ts +52 -8
- package/src/settings.ts +8 -0
- package/src/url-settings.ts +12 -1
- package/src/webhooks.ts +36 -42
package/src/settings.ts
CHANGED
|
@@ -194,6 +194,14 @@ const DECLARED = [
|
|
|
194
194
|
section: 'Database',
|
|
195
195
|
description: 'How many days the audit log keeps an entry. `0` keeps them all.',
|
|
196
196
|
},
|
|
197
|
+
{
|
|
198
|
+
env: 'CROMBOLO_EVENT_RETENTION_DAYS',
|
|
199
|
+
kind: 'number',
|
|
200
|
+
default: '30',
|
|
201
|
+
section: 'Database',
|
|
202
|
+
description:
|
|
203
|
+
'How many days delivered webhooks, their log, and sent change events are kept. Failed ones stay until retried. `0` keeps them all.',
|
|
204
|
+
},
|
|
197
205
|
|
|
198
206
|
// Sign-in and secrets
|
|
199
207
|
{
|
package/src/url-settings.ts
CHANGED
|
@@ -43,8 +43,19 @@ export async function redirectAfterUrlChange(
|
|
|
43
43
|
ctx: UrlChangeContext,
|
|
44
44
|
before: UrlOptions,
|
|
45
45
|
): Promise<void> {
|
|
46
|
+
const withBefore = (source: PublishedContentSource): PublishedContentSource => ({
|
|
47
|
+
...source,
|
|
48
|
+
loadUrlOptions: async () => before,
|
|
49
|
+
})
|
|
50
|
+
// The snapshot hands the build a source of its own, so the old setting goes on that too.
|
|
51
|
+
const snapshot = ctx.source.withSnapshot?.bind(ctx.source)
|
|
46
52
|
const old = new PublishedCache(
|
|
47
|
-
{
|
|
53
|
+
{
|
|
54
|
+
...withBefore(ctx.source),
|
|
55
|
+
withSnapshot: snapshot
|
|
56
|
+
? (fn) => snapshot((scoped, checkpoint) => fn(withBefore(scoped), checkpoint))
|
|
57
|
+
: undefined,
|
|
58
|
+
},
|
|
48
59
|
ctx.cacheOptions,
|
|
49
60
|
)
|
|
50
61
|
const captured: DocumentRoute[] = []
|
package/src/webhooks.ts
CHANGED
|
@@ -8,7 +8,6 @@ import { join } from 'node:path'
|
|
|
8
8
|
import { type RaisedWebhookEvent, WEBHOOK_EVENT_ALIASES } from '@crombolo/api-management'
|
|
9
9
|
import {
|
|
10
10
|
type Db,
|
|
11
|
-
fromDbBool,
|
|
12
11
|
type WebhookDelivery,
|
|
13
12
|
WebhookDeliveryRepository,
|
|
14
13
|
type WebhookLogEntry,
|
|
@@ -143,6 +142,11 @@ export interface WebhookServiceOptions {
|
|
|
143
142
|
now?: () => Date
|
|
144
143
|
}
|
|
145
144
|
|
|
145
|
+
/** An event as a webhook delivers it, with the item's type as it was when it happened. */
|
|
146
|
+
export interface WebhookFacts extends RaisedWebhookEvent {
|
|
147
|
+
contentTypeKey?: string
|
|
148
|
+
}
|
|
149
|
+
|
|
146
150
|
export interface WebhookService {
|
|
147
151
|
/** Which stores a new or moved webhook may use on this node. */
|
|
148
152
|
stores(): { files: boolean; database: boolean }
|
|
@@ -152,7 +156,11 @@ export interface WebhookService {
|
|
|
152
156
|
update(key: string, definition: WebhookDefinition): Promise<WebhookWriteResult>
|
|
153
157
|
remove(key: string): Promise<boolean>
|
|
154
158
|
move(key: string, store: WebhookStore): Promise<WebhookWriteResult>
|
|
155
|
-
/**
|
|
159
|
+
/** What a delivery needs to know about the item, read on `db` — the change's transaction. */
|
|
160
|
+
factsFor(db: Db, event: RaisedWebhookEvent): Promise<WebhookFacts>
|
|
161
|
+
/** Queues a delivery per subscriber, once per event however often it is repeated. */
|
|
162
|
+
enqueueFor(db: Db, eventId: string | undefined, facts: WebhookFacts): Promise<number>
|
|
163
|
+
/** Facts and queue at once, outside any outbox. Never throws into the caller. */
|
|
156
164
|
raise(event: RaisedWebhookEvent): Promise<number>
|
|
157
165
|
runDue(now?: Date, limit?: number): Promise<WorkflowRunReport>
|
|
158
166
|
logs(
|
|
@@ -288,6 +296,8 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
288
296
|
'Webhook-Retry-Count': String(delivery.attempts - 1),
|
|
289
297
|
'Webhook-Id': webhook.key,
|
|
290
298
|
'Webhook-Delivery': delivery.id,
|
|
299
|
+
// The change itself, the same for every webhook it reaches (story 171).
|
|
300
|
+
...(delivery.eventId ? { 'Webhook-Event-Id': delivery.eventId } : {}),
|
|
291
301
|
}
|
|
292
302
|
// The log shows which headers were sent, never what their values were.
|
|
293
303
|
const logged = JSON.stringify({
|
|
@@ -458,21 +468,27 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
458
468
|
else await rows.remove(existing.key)
|
|
459
469
|
return written
|
|
460
470
|
},
|
|
471
|
+
async factsFor(db, event) {
|
|
472
|
+
const contentTypeKey = await itemType(db, event.key)
|
|
473
|
+
return contentTypeKey ? { ...event, contentTypeKey } : { ...event }
|
|
474
|
+
},
|
|
475
|
+
async enqueueFor(db, eventId, facts) {
|
|
476
|
+
const subscribers = subscribersFor(facts, facts.contentTypeKey, await all(), bundles)
|
|
477
|
+
if (subscribers.length === 0) return 0
|
|
478
|
+
const queued = await new WebhookDeliveryRepository(db).enqueue(
|
|
479
|
+
subscribers.map((subscriber) => ({
|
|
480
|
+
subscriber,
|
|
481
|
+
eventAlias: facts.alias,
|
|
482
|
+
payload: { Id: facts.key },
|
|
483
|
+
eventId,
|
|
484
|
+
})),
|
|
485
|
+
now(),
|
|
486
|
+
)
|
|
487
|
+
return queued.length
|
|
488
|
+
},
|
|
461
489
|
async raise(event) {
|
|
462
490
|
try {
|
|
463
|
-
|
|
464
|
-
if (!eventHappened(event.alias, facts.published)) return 0
|
|
465
|
-
const subscribers = subscribersFor(event, facts.contentTypeKey, await all(), bundles)
|
|
466
|
-
if (subscribers.length === 0) return 0
|
|
467
|
-
await deliveries.enqueue(
|
|
468
|
-
subscribers.map((subscriber) => ({
|
|
469
|
-
subscriber,
|
|
470
|
-
eventAlias: event.alias,
|
|
471
|
-
payload: { Id: event.key },
|
|
472
|
-
})),
|
|
473
|
-
now(),
|
|
474
|
-
)
|
|
475
|
-
return subscribers.length
|
|
491
|
+
return await this.enqueueFor(options.db, undefined, await this.factsFor(options.db, event))
|
|
476
492
|
} catch (error) {
|
|
477
493
|
log.error('Could not queue {event} for {key}: {error}', { ...event, error })
|
|
478
494
|
return 0
|
|
@@ -509,36 +525,14 @@ export function createWebhookService(options: WebhookServiceOptions): WebhookSer
|
|
|
509
525
|
}
|
|
510
526
|
}
|
|
511
527
|
|
|
512
|
-
/**
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
* that is gone or not a document.
|
|
517
|
-
*/
|
|
518
|
-
export function eventHappened(alias: string, published: boolean | undefined): boolean {
|
|
519
|
-
if (alias === 'Umbraco.ContentPublish') return published === true
|
|
520
|
-
if (alias === 'Umbraco.ContentUnpublish') return published !== true
|
|
521
|
-
return true
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/** An item's type and whether it is published, while it still exists. */
|
|
525
|
-
async function itemFacts(
|
|
526
|
-
db: Db,
|
|
527
|
-
key: string,
|
|
528
|
-
): Promise<{ contentTypeKey: string | undefined; published: boolean | undefined }> {
|
|
529
|
-
const rows = await db.query<{ type_key: string; published: unknown }>(
|
|
530
|
-
`SELECT tn.unique_id AS type_key, d.published FROM node n
|
|
528
|
+
/** An item's type, while it still exists. */
|
|
529
|
+
async function itemType(db: Db, key: string): Promise<string | undefined> {
|
|
530
|
+
const rows = await db.query<{ type_key: string }>(
|
|
531
|
+
`SELECT tn.unique_id AS type_key FROM node n
|
|
531
532
|
JOIN content c ON c.node_id = n.id
|
|
532
533
|
JOIN node tn ON tn.id = c.content_type_id
|
|
533
|
-
LEFT JOIN document d ON d.node_id = n.id
|
|
534
534
|
WHERE n.unique_id = ?`,
|
|
535
535
|
[key],
|
|
536
536
|
)
|
|
537
|
-
|
|
538
|
-
if (!row) return { contentTypeKey: undefined, published: undefined }
|
|
539
|
-
return {
|
|
540
|
-
contentTypeKey: String(row.type_key).toLowerCase(),
|
|
541
|
-
published:
|
|
542
|
-
row.published === null || row.published === undefined ? undefined : fromDbBool(row.published),
|
|
543
|
-
}
|
|
537
|
+
return rows[0] ? String(rows[0].type_key).toLowerCase() : undefined
|
|
544
538
|
}
|