@crombolo/server 0.46.1 → 0.48.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 +26 -13
- package/src/adapters/documents.ts +60 -10
- package/src/adapters/elements.ts +20 -3
- package/src/adapters/health.ts +44 -0
- package/src/adapters/public-access.ts +14 -3
- package/src/component-snippets.ts +48 -0
- package/src/config.ts +53 -3
- package/src/core.ts +17 -3
- package/src/database.ts +29 -0
- package/src/index.ts +1 -0
- package/src/jobs.ts +46 -7
- package/src/logging.ts +6 -1
- package/src/member-auth.ts +6 -0
- package/src/owner-jobs.ts +18 -1
- package/src/ports.ts +1 -0
- package/src/server.ts +515 -55
- package/src/settings.ts +117 -0
- package/src/site-assets.ts +72 -0
- package/src/site-search.ts +2 -2
- package/src/telemetry.ts +541 -0
- package/src/webhooks.ts +24 -2
- package/src/worker.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The crombolo HTTP server: routing, configuration, media stores and background jobs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -31,19 +31,32 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@crombolo/api-delivery": "0.
|
|
35
|
-
"@crombolo/api-management": "0.
|
|
36
|
-
"@crombolo/auth": "0.
|
|
37
|
-
"@crombolo/backoffice-host": "0.
|
|
38
|
-
"@crombolo/common": "0.
|
|
39
|
-
"@crombolo/content": "0.
|
|
40
|
-
"@crombolo/contracts": "0.
|
|
41
|
-
"@crombolo/core": "0.
|
|
42
|
-
"@crombolo/data": "0.
|
|
43
|
-
"@crombolo/render": "0.
|
|
44
|
-
"@crombolo/schema": "0.
|
|
45
|
-
"@crombolo/transfer": "0.
|
|
34
|
+
"@crombolo/api-delivery": "0.48.0",
|
|
35
|
+
"@crombolo/api-management": "0.48.0",
|
|
36
|
+
"@crombolo/auth": "0.48.0",
|
|
37
|
+
"@crombolo/backoffice-host": "0.48.0",
|
|
38
|
+
"@crombolo/common": "0.48.0",
|
|
39
|
+
"@crombolo/content": "0.48.0",
|
|
40
|
+
"@crombolo/contracts": "0.48.0",
|
|
41
|
+
"@crombolo/core": "0.48.0",
|
|
42
|
+
"@crombolo/data": "0.48.0",
|
|
43
|
+
"@crombolo/render": "0.48.0",
|
|
44
|
+
"@crombolo/schema": "0.48.0",
|
|
45
|
+
"@crombolo/transfer": "0.48.0",
|
|
46
46
|
"@logtape/logtape": "^2.3.8",
|
|
47
|
+
"@opentelemetry/api": "1.10.0",
|
|
48
|
+
"@opentelemetry/context-async-hooks": "3.0.0",
|
|
49
|
+
"@opentelemetry/core": "3.0.0",
|
|
50
|
+
"@opentelemetry/exporter-logs-otlp-http": "0.300.0",
|
|
51
|
+
"@opentelemetry/exporter-logs-otlp-proto": "0.300.0",
|
|
52
|
+
"@opentelemetry/exporter-metrics-otlp-http": "0.300.0",
|
|
53
|
+
"@opentelemetry/exporter-metrics-otlp-proto": "0.300.0",
|
|
54
|
+
"@opentelemetry/exporter-trace-otlp-http": "0.300.0",
|
|
55
|
+
"@opentelemetry/exporter-trace-otlp-proto": "0.300.0",
|
|
56
|
+
"@opentelemetry/resources": "3.0.0",
|
|
57
|
+
"@opentelemetry/sdk-logs": "3.0.0",
|
|
58
|
+
"@opentelemetry/sdk-metrics": "3.0.0",
|
|
59
|
+
"@opentelemetry/sdk-trace": "3.0.0",
|
|
47
60
|
"@sigstore/bundle": "^3.1.0",
|
|
48
61
|
"@sigstore/core": "^2.0.0",
|
|
49
62
|
"@sigstore/protobuf-specs": "^0.4.3",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** Adapters binding the document repository to the API port and the renderer. */
|
|
2
2
|
import type {
|
|
3
|
+
DocumentCollectionItem,
|
|
3
4
|
DocumentPort,
|
|
4
5
|
PortFailure,
|
|
5
6
|
Principal,
|
|
@@ -7,7 +8,6 @@ import type {
|
|
|
7
8
|
SkipTake,
|
|
8
9
|
} from '@crombolo/api-management/domain'
|
|
9
10
|
import {
|
|
10
|
-
type DocumentAggregate,
|
|
11
11
|
normaliseUuid,
|
|
12
12
|
ObjectTypes,
|
|
13
13
|
type Page,
|
|
@@ -19,8 +19,10 @@ import {
|
|
|
19
19
|
ContentTypeRepository,
|
|
20
20
|
type Db,
|
|
21
21
|
DictionaryRepository,
|
|
22
|
+
DocumentListingRepository,
|
|
22
23
|
DocumentRepository,
|
|
23
24
|
type DocumentRepositoryOptions,
|
|
25
|
+
type DocumentSortField,
|
|
24
26
|
DomainRepository,
|
|
25
27
|
InvalidParentError,
|
|
26
28
|
LanguageRepository,
|
|
@@ -74,6 +76,17 @@ async function toTreeItems(
|
|
|
74
76
|
return items
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
/** The list view's built-in columns, by the alias the client sends; any other is a property. */
|
|
80
|
+
const COLLECTION_SORTS: Record<string, DocumentSortField> = {
|
|
81
|
+
name: 'name',
|
|
82
|
+
createDate: 'createDate',
|
|
83
|
+
updateDate: 'updateDate',
|
|
84
|
+
sortOrder: 'sortOrder',
|
|
85
|
+
creator: 'creator',
|
|
86
|
+
owner: 'creator',
|
|
87
|
+
updater: 'updater',
|
|
88
|
+
}
|
|
89
|
+
|
|
77
90
|
export function createDocumentPort(
|
|
78
91
|
db: Db,
|
|
79
92
|
cache: PublishedCache,
|
|
@@ -477,16 +490,53 @@ export function createDocumentPort(
|
|
|
477
490
|
return toTreeItems(repo, await repo.nodes.byKeys(keys))
|
|
478
491
|
},
|
|
479
492
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
)
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
493
|
+
/**
|
|
494
|
+
* The list view (story 269): searched, sorted and paged in the database, so
|
|
495
|
+
* a large folder costs a page; then only the page's items are loaded.
|
|
496
|
+
*/
|
|
497
|
+
async collection(parentKey, query, excludeKeys = []) {
|
|
498
|
+
const parent = parentKey ? await repo.nodes.byKey(parentKey) : undefined
|
|
499
|
+
if (parentKey && !parent) return { total: 0, items: [] }
|
|
500
|
+
const listing = new DocumentListingRepository(db)
|
|
501
|
+
const filter = query.filter?.trim()
|
|
502
|
+
const page = await listing.children({
|
|
503
|
+
parentId: parent?.id ?? -1,
|
|
504
|
+
names: filter ? [filter] : [],
|
|
505
|
+
// Anything but a built-in column is a property the list view shows (story 276).
|
|
506
|
+
sort: {
|
|
507
|
+
...(COLLECTION_SORTS[query.orderBy ?? '']
|
|
508
|
+
? { field: COLLECTION_SORTS[query.orderBy ?? ''] as DocumentSortField }
|
|
509
|
+
: query.orderBy
|
|
510
|
+
? { property: query.orderBy }
|
|
511
|
+
: { field: 'sortOrder' as const }),
|
|
512
|
+
ascending: query.orderDirection !== 'Descending',
|
|
513
|
+
},
|
|
514
|
+
culture: query.orderCulture ?? null,
|
|
515
|
+
excludeKeys,
|
|
516
|
+
skip: query.skip,
|
|
517
|
+
take: query.take,
|
|
518
|
+
})
|
|
519
|
+
const found = new Map((await repo.nodes.byKeys(page.keys)).map((row) => [row.key, row]))
|
|
520
|
+
const rows = page.keys.flatMap((key) => found.get(key) ?? [])
|
|
521
|
+
const [guarded, people, counts] = await Promise.all([
|
|
522
|
+
repo.protectedKeys(rows),
|
|
523
|
+
listing.people(page.keys),
|
|
524
|
+
repo.nodes.childCounts(
|
|
525
|
+
rows.map((row) => row.id),
|
|
526
|
+
ObjectTypes.Document,
|
|
527
|
+
),
|
|
528
|
+
])
|
|
529
|
+
const items: DocumentCollectionItem[] = []
|
|
530
|
+
for (const row of rows) {
|
|
488
531
|
const aggregate = await repo.load(row)
|
|
489
|
-
if (aggregate)
|
|
532
|
+
if (!aggregate) continue
|
|
533
|
+
items.push({
|
|
534
|
+
...aggregate,
|
|
535
|
+
isProtected: guarded.has(row.key),
|
|
536
|
+
hasChildren: (counts.get(row.id) ?? 0) > 0,
|
|
537
|
+
creator: people.get(row.key)?.creator ?? null,
|
|
538
|
+
updater: people.get(row.key)?.updater ?? null,
|
|
539
|
+
})
|
|
490
540
|
}
|
|
491
541
|
return { total: page.total, items }
|
|
492
542
|
},
|
package/src/adapters/elements.ts
CHANGED
|
@@ -267,12 +267,29 @@ export function createElementPort(
|
|
|
267
267
|
}
|
|
268
268
|
},
|
|
269
269
|
|
|
270
|
-
async publish(key, cultures) {
|
|
270
|
+
async publish(key, cultures, schedules = []) {
|
|
271
|
+
const now = Date.now()
|
|
272
|
+
const later = (s: { publishTime: Date | null }) =>
|
|
273
|
+
s.publishTime !== null && s.publishTime.getTime() > now
|
|
274
|
+
const immediate = schedules.filter((s) => !later(s))
|
|
275
|
+
const scheduledOnly = schedules.length > 0 && immediate.length === 0
|
|
276
|
+
const publishing = immediate.some((s) => s.culture !== null)
|
|
277
|
+
? immediate.flatMap((s) => (s.culture === null ? [] : [s.culture]))
|
|
278
|
+
: cultures
|
|
271
279
|
try {
|
|
272
|
-
|
|
280
|
+
// The schedule and the publish are one change: neither lands without the other.
|
|
281
|
+
const published = await repo.atomically(async (tx) => {
|
|
282
|
+
if (schedules.length > 0)
|
|
283
|
+
await tx.setSchedule(
|
|
284
|
+
key,
|
|
285
|
+
schedules.map((s) => ({ ...s, publishTime: later(s) ? s.publishTime : null })),
|
|
286
|
+
)
|
|
287
|
+
if (scheduledOnly) return true
|
|
288
|
+
return tx.publish(key, publishing)
|
|
289
|
+
})
|
|
273
290
|
if (!published) return notFound()
|
|
274
291
|
await changed()
|
|
275
|
-
return { ok: true as const }
|
|
292
|
+
return { ok: true as const, scheduledOnly }
|
|
276
293
|
} catch (error) {
|
|
277
294
|
return failure(error)
|
|
278
295
|
}
|
package/src/adapters/health.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
type QueueCounts,
|
|
23
23
|
queueCounts,
|
|
24
24
|
RemoteTrustRepository,
|
|
25
|
+
searchProvidersOf,
|
|
25
26
|
sql,
|
|
26
27
|
UserGroupRepository,
|
|
27
28
|
UserRepository,
|
|
@@ -32,6 +33,7 @@ import { type EmailPort, emailAvailability } from '../email.ts'
|
|
|
32
33
|
import type { MediaStore } from '../media-store.ts'
|
|
33
34
|
import { restartPending } from '../restart-pending.ts'
|
|
34
35
|
import { backofficeVersion } from '../status.ts'
|
|
36
|
+
import { type TelemetryStatus, telemetryStatus } from '../telemetry.ts'
|
|
35
37
|
|
|
36
38
|
export const UPGRADE_PACKAGE = '@crombolo/crombolo'
|
|
37
39
|
|
|
@@ -48,6 +50,7 @@ const CHECKS = {
|
|
|
48
50
|
routes: { id: '6f1d4a8e-0047-4c1a-9e7d-000000000007', name: 'Routes and pages' },
|
|
49
51
|
settings: { id: '6f1d4a8e-0047-4c1a-9e7d-000000000008', name: 'Site settings' },
|
|
50
52
|
code: { id: '6f1d4a8e-0047-4c1a-9e7d-000000000009', name: 'Installing code' },
|
|
53
|
+
search: { id: '6f1d4a8e-0047-4c1a-9e7d-00000000000a', name: 'Search providers' },
|
|
51
54
|
} as const
|
|
52
55
|
|
|
53
56
|
/** Waiting longer than this means nothing is sending: no server is running its jobs, or one is stuck. */
|
|
@@ -86,6 +89,15 @@ const GROUPS: HealthCheckGroup[] = [
|
|
|
86
89
|
},
|
|
87
90
|
],
|
|
88
91
|
},
|
|
92
|
+
{
|
|
93
|
+
name: 'Search',
|
|
94
|
+
checks: [
|
|
95
|
+
{
|
|
96
|
+
...CHECKS.search,
|
|
97
|
+
description: 'Which search the back office and the site use, and whether each answers.',
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
},
|
|
89
101
|
{
|
|
90
102
|
name: 'Site',
|
|
91
103
|
checks: [
|
|
@@ -385,6 +397,29 @@ export function createDiagnosticsPort(options: HealthCheckOptions): DiagnosticsP
|
|
|
385
397
|
),
|
|
386
398
|
)
|
|
387
399
|
},
|
|
400
|
+
[CHECKS.search.id]: async () => {
|
|
401
|
+
const { backoffice, site } = searchProvidersOf(db)
|
|
402
|
+
const checked = async (
|
|
403
|
+
what: string,
|
|
404
|
+
provider: { name: string; health?(): Promise<{ ok: boolean; detail?: string }> },
|
|
405
|
+
) => {
|
|
406
|
+
try {
|
|
407
|
+
const health = (await provider.health?.()) ?? { ok: true }
|
|
408
|
+
return health.ok
|
|
409
|
+
? result('Success', `${what} uses ${provider.name}, which is answering.`)
|
|
410
|
+
: result(
|
|
411
|
+
'Error',
|
|
412
|
+
`${what} uses ${provider.name}, which is not answering${health.detail ? `: ${health.detail}` : '.'}`,
|
|
413
|
+
)
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return result(
|
|
416
|
+
'Error',
|
|
417
|
+
`${what} uses ${provider.name}, which is not answering: ${failure(error)}`,
|
|
418
|
+
)
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return [await checked('Back-office search', backoffice), await checked('Site search', site)]
|
|
422
|
+
},
|
|
388
423
|
[CHECKS.code.id]: async () => {
|
|
389
424
|
const results: HealthCheckResult[] = []
|
|
390
425
|
const verb = RemotePermissions.InstallCode
|
|
@@ -475,7 +510,10 @@ export function createDiagnosticsPort(options: HealthCheckOptions): DiagnosticsP
|
|
|
475
510
|
'E-mail',
|
|
476
511
|
availability.available ? (availability.description ?? undefined) : 'Not configured',
|
|
477
512
|
],
|
|
513
|
+
['Back-office search', searchProvidersOf(db).backoffice.name],
|
|
514
|
+
['Site search', searchProvidersOf(db).site.name],
|
|
478
515
|
['Log level', config.logLevel],
|
|
516
|
+
['Telemetry', describeTelemetry(telemetryStatus())],
|
|
479
517
|
['Time zone', Intl.DateTimeFormat().resolvedOptions().timeZone],
|
|
480
518
|
['Server time', now().toISOString()],
|
|
481
519
|
]
|
|
@@ -525,3 +563,9 @@ export function createDiagnosticsPort(options: HealthCheckOptions): DiagnosticsP
|
|
|
525
563
|
},
|
|
526
564
|
}
|
|
527
565
|
}
|
|
566
|
+
|
|
567
|
+
/** Where traces, metrics and logs go, or that they go nowhere (story 262); never the headers. */
|
|
568
|
+
export function describeTelemetry(status: TelemetryStatus): string {
|
|
569
|
+
if (!status.enabled) return 'Not sent anywhere'
|
|
570
|
+
return `Sent to ${status.endpoint} as ${status.serviceName} (${status.role})`
|
|
571
|
+
}
|
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
} from '@crombolo/api-management/domain'
|
|
18
18
|
import { ObjectTypes } from '@crombolo/core'
|
|
19
19
|
import {
|
|
20
|
+
appendCacheInstruction,
|
|
20
21
|
type Db,
|
|
21
22
|
MemberRepository,
|
|
22
23
|
NodeRepository,
|
|
@@ -26,9 +27,19 @@ import {
|
|
|
26
27
|
|
|
27
28
|
export function createPublicAccessPort(
|
|
28
29
|
db: Db,
|
|
29
|
-
options: { onChange?: () => void | Promise<void> } = {},
|
|
30
|
+
options: { nodeId?: string; onChange?: () => void | Promise<void> } = {},
|
|
30
31
|
): PublicAccessPort {
|
|
31
32
|
const access = new PublicAccessRepository(db)
|
|
33
|
+
// A page that leaves out what a visitor may not open changes with the rules,
|
|
34
|
+
// so they move the checkpoint, and with it every page's ETag (story 258).
|
|
35
|
+
const changed = async () => {
|
|
36
|
+
await appendCacheInstruction(db, {
|
|
37
|
+
kind: 'content',
|
|
38
|
+
payload: { publicAccess: true },
|
|
39
|
+
by: options.nodeId,
|
|
40
|
+
})
|
|
41
|
+
await options.onChange?.()
|
|
42
|
+
}
|
|
32
43
|
const members = new MemberRepository(db)
|
|
33
44
|
const nodes = new NodeRepository(db)
|
|
34
45
|
|
|
@@ -104,14 +115,14 @@ export function createPublicAccessPort(
|
|
|
104
115
|
memberUserNames: input.memberUserNames,
|
|
105
116
|
})
|
|
106
117
|
if (!saved) return 'content-not-found'
|
|
107
|
-
await
|
|
118
|
+
await changed()
|
|
108
119
|
return 'ok'
|
|
109
120
|
},
|
|
110
121
|
|
|
111
122
|
async remove(documentKey): Promise<PublicAccessStatus> {
|
|
112
123
|
if (!(await isDocument(documentKey))) return 'content-not-found'
|
|
113
124
|
if (!(await access.remove(documentKey))) return 'entry-not-found'
|
|
114
|
-
await
|
|
125
|
+
await changed()
|
|
115
126
|
return 'ok'
|
|
116
127
|
},
|
|
117
128
|
}
|
|
@@ -158,6 +158,54 @@ export default function SiteMap({ nav }: Pick<PageProps, 'nav'>) {
|
|
|
158
158
|
)
|
|
159
159
|
return <div class="sitemap">{branch(nav.root())}</div>
|
|
160
160
|
}
|
|
161
|
+
`,
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
id: 'PersonalisedPart',
|
|
165
|
+
name: 'Personalised part',
|
|
166
|
+
// Story 264: a part fetched for each visitor, so the page around it is shared.
|
|
167
|
+
content: `'use deferred'
|
|
168
|
+
// A personalised part: it shows the signed-in visitor, and is fetched for each
|
|
169
|
+
// visitor after the page loads, so the rest of the page can be the same for
|
|
170
|
+
// everyone and kept by a CDN. Use one for a greeting, a basket or an account
|
|
171
|
+
// link. For something that runs in the browser, such as a counter or a map,
|
|
172
|
+
// start from "Interactive part" instead.
|
|
173
|
+
//
|
|
174
|
+
// Use it with what to show until it arrives, which is what is shared:
|
|
175
|
+
// <Greeting fallback={<a href="/sign-in">Sign in</a>} />
|
|
176
|
+
// Its props travel in its address, so pass data only: text, numbers, lists.
|
|
177
|
+
import { usePage } from '@crombolo/crombolo'
|
|
178
|
+
|
|
179
|
+
export default function Greeting() {
|
|
180
|
+
const { member } = usePage()
|
|
181
|
+
if (!member) return <a href="/sign-in">Sign in</a>
|
|
182
|
+
return <a href="/account">Hi {member.name}</a>
|
|
183
|
+
}
|
|
184
|
+
`,
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
id: 'InteractivePart',
|
|
188
|
+
name: 'Interactive part',
|
|
189
|
+
// Story 264: an island, which React runs in the browser.
|
|
190
|
+
content: `/** @jsxImportSource react */
|
|
191
|
+
'use client'
|
|
192
|
+
// An interactive part: React runs it in the browser, so it can respond to
|
|
193
|
+
// clicks and typing. The site needs react and react-dom installed. For
|
|
194
|
+
// something that shows the signed-in visitor, start from "Personalised part"
|
|
195
|
+
// instead: an interactive part sees only the props it is given.
|
|
196
|
+
//
|
|
197
|
+
// Use it with what to show until it runs:
|
|
198
|
+
// <Counter start={0} fallback={<p>0</p>} />
|
|
199
|
+
import { useState } from 'react'
|
|
200
|
+
|
|
201
|
+
export default function Counter({ start = 0 }: { start?: number }) {
|
|
202
|
+
const [count, setCount] = useState(start)
|
|
203
|
+
return (
|
|
204
|
+
<button type="button" onClick={() => setCount(count + 1)}>
|
|
205
|
+
{count}
|
|
206
|
+
</button>
|
|
207
|
+
)
|
|
208
|
+
}
|
|
161
209
|
`,
|
|
162
210
|
},
|
|
163
211
|
]
|
package/src/config.ts
CHANGED
|
@@ -3,10 +3,13 @@ import { join, resolve } from 'node:path'
|
|
|
3
3
|
import type { DeliveryApiOptions } from '@crombolo/api-delivery'
|
|
4
4
|
import type { MiddlewareChain, SiteRoute } from '@crombolo/common'
|
|
5
5
|
import {
|
|
6
|
+
type BackofficeSearchProvider,
|
|
6
7
|
DEFAULT_BACKOFFICE_PATH,
|
|
7
8
|
DEFAULT_PASSWORD_CONFIGURATION,
|
|
8
9
|
isValidPassword,
|
|
10
|
+
type SiteSearchProvider,
|
|
9
11
|
} from '@crombolo/core'
|
|
12
|
+
import type { Db } from '@crombolo/data'
|
|
10
13
|
import {
|
|
11
14
|
type CacheOptions,
|
|
12
15
|
DEFAULT_RENDER_TIMEOUT_MS,
|
|
@@ -15,6 +18,7 @@ import {
|
|
|
15
18
|
type RequestMember,
|
|
16
19
|
type SiteServices,
|
|
17
20
|
type SnapshotLimits,
|
|
21
|
+
type Validator,
|
|
18
22
|
} from '@crombolo/render'
|
|
19
23
|
import type { ServerBundle } from './bundles.ts'
|
|
20
24
|
import type { EmailPort } from './email.ts'
|
|
@@ -27,6 +31,7 @@ import type { ProvenancePolicy, SignaturePolicy } from './provenance.ts'
|
|
|
27
31
|
import type { SchemaStore } from './schema-store.ts'
|
|
28
32
|
import { setting } from './settings.ts'
|
|
29
33
|
import type { SiteDatabaseConfig, SiteQueryModules } from './site-queries.ts'
|
|
34
|
+
import type { TelemetryOptions } from './telemetry.ts'
|
|
30
35
|
|
|
31
36
|
/**
|
|
32
37
|
* Runtime settings for a site. Umbraco's equivalents live in appsettings.json;
|
|
@@ -136,6 +141,15 @@ export function listeningBodyLimit(config: CromboloConfig): number {
|
|
|
136
141
|
: config.maxRequestBodyBytes
|
|
137
142
|
}
|
|
138
143
|
|
|
144
|
+
/** Makes a search provider for a database; called once, when the database is opened. */
|
|
145
|
+
export type SearchProviderFactory<P> = (context: { db: Db }) => P
|
|
146
|
+
|
|
147
|
+
export interface SearchConfig {
|
|
148
|
+
backoffice?: SearchProviderFactory<BackofficeSearchProvider>
|
|
149
|
+
site?: SearchProviderFactory<SiteSearchProvider>
|
|
150
|
+
matchLimit?: number
|
|
151
|
+
}
|
|
152
|
+
|
|
139
153
|
export interface CromboloConfig {
|
|
140
154
|
port: number
|
|
141
155
|
/** The largest request body the server will read, media uploads included. */
|
|
@@ -346,6 +360,13 @@ export interface CromboloConfig {
|
|
|
346
360
|
* A site swaps in blob storage with `s3MediaStore` or `azureMediaStore`.
|
|
347
361
|
*/
|
|
348
362
|
mediaStore: MediaStore | undefined
|
|
363
|
+
/**
|
|
364
|
+
* The search providers (story 273): backoffice search and site search, each
|
|
365
|
+
* a factory given the database, the database's own when unset. `matchLimit`
|
|
366
|
+
* caps the matches a provider outside the database gives a query that
|
|
367
|
+
* filters with them in SQL. docs/search.md, "Providers".
|
|
368
|
+
*/
|
|
369
|
+
search: SearchConfig
|
|
349
370
|
/** Where log files are written, and read by the backoffice's log viewer. */
|
|
350
371
|
logsDir: string
|
|
351
372
|
/** The least severe level logged: trace, debug, info, warning, error or fatal. */
|
|
@@ -455,10 +476,24 @@ export interface CromboloConfig {
|
|
|
455
476
|
/**
|
|
456
477
|
* How long pages, Delivery API answers and media may be kept by browsers and a
|
|
457
478
|
* CDN (story 057; `docs/operations.md`, "Behind a CDN"). Nothing is kept unless
|
|
458
|
-
* set. `CROMBOLO_CACHE_MAX_AGE`, `CROMBOLO_CACHE_STALE_WHILE_REVALIDATE
|
|
459
|
-
* `CROMBOLO_CACHE_TAG_HEADER
|
|
479
|
+
* set. `CROMBOLO_CACHE_MAX_AGE`, `CROMBOLO_CACHE_STALE_WHILE_REVALIDATE`,
|
|
480
|
+
* `CROMBOLO_CACHE_TAG_HEADER`, `CROMBOLO_CACHE_VALIDATOR`, `CROMBOLO_CACHE_MODE`,
|
|
481
|
+
* `CROMBOLO_CACHE_STALE_IF_ERROR` and `CROMBOLO_CACHE_CHECK_FLOOR`; per type
|
|
482
|
+
* alias in code only.
|
|
460
483
|
*/
|
|
461
484
|
cache: CacheOptions
|
|
485
|
+
/**
|
|
486
|
+
* Where traces, metrics and logs are sent over OpenTelemetry (story 262;
|
|
487
|
+
* `docs/operations.md`, "Telemetry"). `OTEL_EXPORTER_OTLP_ENDPOINT` and
|
|
488
|
+
* `OTEL_SERVICE_NAME` win; unset, nothing is sent.
|
|
489
|
+
*/
|
|
490
|
+
telemetry: TelemetryOptions
|
|
491
|
+
/**
|
|
492
|
+
* The load balancers or CDN in front of the site, as addresses or IPv4
|
|
493
|
+
* ranges (`10.0.0.0/8`). A request's `traceparent` is followed only from one
|
|
494
|
+
* of them. `CROMBOLO_TRUSTED_PROXIES`.
|
|
495
|
+
*/
|
|
496
|
+
trustedProxies: string[]
|
|
462
497
|
/**
|
|
463
498
|
* Bundles whose server half this site runs, each the result of calling the
|
|
464
499
|
* factory its package exports. Imported and listed here by hand on purpose:
|
|
@@ -812,6 +847,7 @@ export function loadConfig(
|
|
|
812
847
|
DEFAULTS.mediaCacheMegabytes,
|
|
813
848
|
),
|
|
814
849
|
mediaStore: undefined,
|
|
850
|
+
search: {},
|
|
815
851
|
logsDir: setting('CROMBOLO_LOGS_DIR') ?? DEFAULTS.logsDir,
|
|
816
852
|
logLevel: parseLogLevel(setting('CROMBOLO_LOG_LEVEL')),
|
|
817
853
|
logToConsole: setting('CROMBOLO_LOG_TO_CONSOLE') !== 'false',
|
|
@@ -898,10 +934,19 @@ export function loadConfig(
|
|
|
898
934
|
: Number(setting('CROMBOLO_DELIVERY_API_PREVIEW_TOKEN_MINUTES')),
|
|
899
935
|
}),
|
|
900
936
|
}),
|
|
937
|
+
telemetry: {},
|
|
938
|
+
trustedProxies: (setting('CROMBOLO_TRUSTED_PROXIES') ?? '')
|
|
939
|
+
.split(',')
|
|
940
|
+
.map((proxy) => proxy.trim())
|
|
941
|
+
.filter(Boolean),
|
|
901
942
|
cache: stripUndefined({
|
|
902
943
|
maxAge: numberSetting('CROMBOLO_CACHE_MAX_AGE'),
|
|
903
944
|
staleWhileRevalidate: numberSetting('CROMBOLO_CACHE_STALE_WHILE_REVALIDATE'),
|
|
904
945
|
tagHeader: setting('CROMBOLO_CACHE_TAG_HEADER') || undefined,
|
|
946
|
+
validator: validatorSetting('CROMBOLO_CACHE_VALIDATOR'),
|
|
947
|
+
mode: setting('CROMBOLO_CACHE_MODE') === 'check' ? ('check' as const) : undefined,
|
|
948
|
+
staleIfError: numberSetting('CROMBOLO_CACHE_STALE_IF_ERROR'),
|
|
949
|
+
checkFloor: numberSetting('CROMBOLO_CACHE_CHECK_FLOOR'),
|
|
905
950
|
}),
|
|
906
951
|
// No environment variable: a bundle is an import, which is code.
|
|
907
952
|
bundles: [],
|
|
@@ -1041,7 +1086,7 @@ export function describeDatabase(
|
|
|
1041
1086
|
}
|
|
1042
1087
|
}
|
|
1043
1088
|
|
|
1044
|
-
export const VERSION = '0.
|
|
1089
|
+
export const VERSION = '0.48.0'
|
|
1045
1090
|
|
|
1046
1091
|
/** A whole number of seconds from the environment; undefined when unset or not one. */
|
|
1047
1092
|
/** A positive number from a setting, else the default. */
|
|
@@ -1050,6 +1095,11 @@ function positiveNumber(raw: string | undefined, fallback: number): number {
|
|
|
1050
1095
|
return raw !== undefined && raw !== '' && Number.isFinite(value) && value > 0 ? value : fallback
|
|
1051
1096
|
}
|
|
1052
1097
|
|
|
1098
|
+
function validatorSetting(name: Parameters<typeof setting>[0]): Validator | undefined {
|
|
1099
|
+
const raw = setting(name)?.trim()
|
|
1100
|
+
return raw === 'published' || raw === 'body' ? raw : undefined
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1053
1103
|
function numberSetting(name: Parameters<typeof setting>[0]): number | undefined {
|
|
1054
1104
|
const raw = setting(name)
|
|
1055
1105
|
if (raw === undefined || raw.trim() === '') return undefined
|
package/src/core.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import {
|
|
20
20
|
DEFAULT_MEDIA_CACHE_MEGABYTES,
|
|
21
21
|
DEVELOPMENT_LIMITS,
|
|
22
|
+
deferredTransform,
|
|
22
23
|
enableComponentStack,
|
|
23
24
|
ISLANDS_PATH,
|
|
24
25
|
islandTransform,
|
|
@@ -79,6 +80,7 @@ import { bootSchema, CacheInstructionPoller } from './schema.ts'
|
|
|
79
80
|
import { publishSchema, type SchemaStore, useSchemaStore } from './schema-store.ts'
|
|
80
81
|
import { createSiteQueries, queryProblems } from './site-queries.ts'
|
|
81
82
|
import { createSiteServices } from './site-services.ts'
|
|
83
|
+
import { recordCacheLookup, startTelemetry, tracedDb, tracedEmail } from './telemetry.ts'
|
|
82
84
|
import { redirectAfterUrlChange, type UrlChangeContext } from './url-settings.ts'
|
|
83
85
|
import { assertViewRuntime } from './view-runtime.ts'
|
|
84
86
|
import { bundleEventProblems, createWebhookService } from './webhooks.ts'
|
|
@@ -161,9 +163,19 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
161
163
|
// bundle's; a worker runs bundles' jobs, so it opens every owner's.
|
|
162
164
|
const siteChains = chains.filter((chain) => chain.id === SITE_OWNER)
|
|
163
165
|
const opened = config.role === 'web' ? siteChains : chains
|
|
164
|
-
const
|
|
166
|
+
const booted = owns
|
|
165
167
|
? await bootstrapDatabase(config, chains)
|
|
166
168
|
: await attachDatabase(config, opened)
|
|
169
|
+
const { seededAdminPassword } = booted
|
|
170
|
+
// Story 262: on every role, before anything reads, so each statement is a span.
|
|
171
|
+
await startTelemetry({
|
|
172
|
+
telemetry: config.telemetry,
|
|
173
|
+
siteName: config.siteName,
|
|
174
|
+
role: config.role,
|
|
175
|
+
nodeId: config.nodeId,
|
|
176
|
+
db: booted.db,
|
|
177
|
+
})
|
|
178
|
+
const db = tracedDb(booted.db)
|
|
167
179
|
await openTables(db, config, opened, { provision: owns })
|
|
168
180
|
// Stories 222 and 225: wiring a bundle approves it; an administrator can disable it.
|
|
169
181
|
const bundleGrants = createBundleGrants({
|
|
@@ -241,6 +253,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
241
253
|
const cache = new PublishedCache(publishedSource, {
|
|
242
254
|
...cacheOptions,
|
|
243
255
|
onInvalidate: () => hooks.onCacheInvalidate?.(),
|
|
256
|
+
onRead: recordCacheLookup,
|
|
244
257
|
})
|
|
245
258
|
// A sync or a Settings change that alters how URLs are built moves every URL:
|
|
246
259
|
// the old ones redirect, as a rename's do.
|
|
@@ -292,7 +305,8 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
292
305
|
},
|
|
293
306
|
// Story 209: a `'use client'` view is a stub on the server, and its browser
|
|
294
307
|
// code is built with the generation, unless a site's Vite build supplies it.
|
|
295
|
-
|
|
308
|
+
// Story 263: a `'use deferred'` view is a placeholder in the page, its source kept beside it.
|
|
309
|
+
transform: (key, bytes) => islandTransform(key, bytes) ?? deferredTransform(key, bytes),
|
|
296
310
|
beforeSwap: async (generation) => {
|
|
297
311
|
if (!hooks.buildIslands) return
|
|
298
312
|
if (config.islands?.manifest || (config.development && config.islands?.devServer)) return
|
|
@@ -334,7 +348,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
334
348
|
)
|
|
335
349
|
// Resolved once: constructing it twice would warn twice about a provider that
|
|
336
350
|
// is half-configured, and the adapters are cheap but not free of side effects.
|
|
337
|
-
const email = resolveEmailPort(config)
|
|
351
|
+
const email = tracedEmail(resolveEmailPort(config))
|
|
338
352
|
// Story 045: a change queues the e-mails its subscribers asked for. Nothing is
|
|
339
353
|
// queued on a site that cannot send; development sends to the console.
|
|
340
354
|
const notifier = createContentNotifier(db, { canSend: Boolean(email) })
|
package/src/database.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
connect,
|
|
10
10
|
cromboloPlan,
|
|
11
11
|
type Db,
|
|
12
|
+
DEFAULT_MATCH_LIMIT,
|
|
12
13
|
ensureBuiltInDataTypes,
|
|
13
14
|
ensureSystemContentTypes,
|
|
14
15
|
ensureSystemMemberTypes,
|
|
@@ -19,6 +20,7 @@ import {
|
|
|
19
20
|
readState,
|
|
20
21
|
seedContent,
|
|
21
22
|
seedIdentity,
|
|
23
|
+
useSearchProviders,
|
|
22
24
|
} from '@crombolo/data'
|
|
23
25
|
import { ensureApiClient } from './api-client.ts'
|
|
24
26
|
import { type CromboloConfig, runsOnLocalhost } from './config.ts'
|
|
@@ -87,9 +89,35 @@ export async function attachDatabase(
|
|
|
87
89
|
await db.close()
|
|
88
90
|
throw error
|
|
89
91
|
}
|
|
92
|
+
useSiteSearchProviders(db, config)
|
|
90
93
|
return { db, seededAdminPassword: undefined }
|
|
91
94
|
}
|
|
92
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Registers the site's search providers on `db` (story 273), so every reader
|
|
98
|
+
* of it, the indexer and the CLI among them, finds them. Without a factory,
|
|
99
|
+
* the database's own.
|
|
100
|
+
*/
|
|
101
|
+
export function useSiteSearchProviders(db: Db, config: Pick<CromboloConfig, 'search'>): void {
|
|
102
|
+
const search = config.search ?? {}
|
|
103
|
+
const log = logger('search')
|
|
104
|
+
useSearchProviders(db, {
|
|
105
|
+
backoffice: search.backoffice?.({ db }),
|
|
106
|
+
site: search.site?.({ db }),
|
|
107
|
+
matchLimit: search.matchLimit,
|
|
108
|
+
onError: (error, context) =>
|
|
109
|
+
log.warning('The search provider {provider} failed; the item stays queued', {
|
|
110
|
+
provider: context.provider,
|
|
111
|
+
key: context.key,
|
|
112
|
+
error,
|
|
113
|
+
}),
|
|
114
|
+
onCapped: () =>
|
|
115
|
+
log.warning('A search took {limit} matches from the provider, so its total may be capped', {
|
|
116
|
+
limit: search.matchLimit ?? DEFAULT_MATCH_LIMIT,
|
|
117
|
+
}),
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
93
121
|
/**
|
|
94
122
|
* `chains` are the custom tables: the site's and the wired bundles'. Their
|
|
95
123
|
* steps pending in production refuse the boot just as core's do, and a
|
|
@@ -118,6 +146,7 @@ export async function bootstrapDatabase(
|
|
|
118
146
|
await db.close()
|
|
119
147
|
throw error
|
|
120
148
|
}
|
|
149
|
+
useSiteSearchProviders(db, config)
|
|
121
150
|
return { db, seededAdminPassword: await installDatabase(db, config, chains) }
|
|
122
151
|
}
|
|
123
152
|
|
package/src/index.ts
CHANGED
|
@@ -115,6 +115,7 @@ export * from './site-sections.ts'
|
|
|
115
115
|
export * from './status.ts'
|
|
116
116
|
export * from './status-pages.ts'
|
|
117
117
|
export * from './tar.ts'
|
|
118
|
+
export * from './telemetry.ts'
|
|
118
119
|
export * from './transfer-dashboard.ts'
|
|
119
120
|
export * from './two-factor.ts'
|
|
120
121
|
export * from './udt.ts'
|