@crombolo/server 0.36.2 → 0.38.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 +15 -13
- package/src/access.ts +29 -27
- package/src/adapters/audit.ts +1 -1
- package/src/adapters/blueprints.ts +22 -13
- package/src/adapters/content.ts +1 -1
- package/src/adapters/dictionary.ts +1 -1
- package/src/adapters/documents.ts +17 -10
- package/src/adapters/elements.ts +47 -37
- package/src/adapters/file-system.ts +1 -1
- package/src/adapters/health.ts +33 -5
- package/src/adapters/media.ts +12 -8
- package/src/adapters/member-groups.ts +8 -6
- package/src/adapters/members.ts +6 -4
- package/src/adapters/public-access.ts +7 -5
- package/src/adapters/redirects.ts +1 -1
- package/src/adapters/references.ts +1 -1
- package/src/adapters/relations.ts +1 -1
- package/src/adapters/schema-files.ts +33 -1
- package/src/adapters/search-index.ts +1 -1
- package/src/adapters/template-files.ts +6 -1
- package/src/adapters/template-query.ts +1 -1
- package/src/adapters/users.ts +13 -13
- package/src/adapters/webhooks.ts +1 -1
- package/src/anonymise-rules.ts +83 -0
- package/src/approvals-api.ts +1 -1
- package/src/approvals.ts +75 -38
- package/src/back-office-url.ts +20 -0
- package/src/backup.ts +26 -2
- package/src/bundle-code.ts +1 -1
- package/src/bundle-grants.ts +265 -0
- package/src/bundle-install.ts +13 -2
- package/src/bundle-media.ts +12 -11
- package/src/bundles.ts +258 -444
- package/src/component-snippets.ts +1 -1
- package/src/components.ts +48 -7
- package/src/config.ts +141 -4
- package/src/content-notifications.ts +13 -9
- package/src/content-operations.ts +23 -65
- package/src/content-query.ts +1 -1
- package/src/core.ts +663 -0
- package/src/cron.ts +139 -0
- package/src/custom-tables.ts +274 -0
- package/src/database.ts +22 -14
- package/src/dictionary-transfer.ts +1 -1
- package/src/git-routes.ts +1 -1
- package/src/identity-provider-overrides.ts +18 -16
- package/src/imaging.ts +65 -15
- package/src/index.ts +14 -1
- package/src/islands.ts +197 -0
- package/src/job-wake.ts +23 -0
- package/src/live-reload.ts +195 -0
- package/src/log-viewer.ts +6 -1
- package/src/marketplace.ts +82 -10
- package/src/media-access.ts +12 -9
- package/src/member-auth.ts +6 -4
- package/src/middleware.ts +236 -0
- package/src/models-builder.ts +1 -1
- package/src/node-operations.ts +122 -0
- package/src/owner-jobs.ts +428 -0
- package/src/ports.ts +39 -154
- package/src/preview-tokens.ts +6 -12
- package/src/remote.ts +3 -1
- package/src/routes.ts +420 -0
- package/src/schema.ts +8 -3
- package/src/seed/compile.ts +7 -5
- package/src/seed/export.ts +6 -4
- package/src/seo.ts +2 -2
- package/src/server.ts +866 -483
- package/src/settings.ts +49 -2
- package/src/site-data.ts +195 -0
- package/src/site-file.ts +30 -4
- package/src/site-queries.ts +140 -0
- package/src/site-replace.ts +25 -19
- package/src/site-sections.ts +1 -1
- package/src/site-services.ts +116 -0
- package/src/status-pages.ts +142 -0
- package/src/status.ts +11 -9
- package/src/url-settings.ts +1 -2
- package/src/webhook-files.ts +18 -1
- package/src/webhooks.ts +83 -15
- package/src/worker-watch.ts +73 -0
- package/src/worker.ts +138 -0
- package/src/bundle-tables.ts +0 -161
package/src/worker.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A node that runs the background jobs and nothing else (story 223): core's,
|
|
3
|
+
* the site's and its bundles' jobs and schedules, on the core every node is
|
|
4
|
+
* built on. It answers `/health` alone; no page, back office or API is
|
|
5
|
+
* mounted. docs/operations.md, "Running a cluster".
|
|
6
|
+
*/
|
|
7
|
+
import type { WritesPaused } from '@crombolo/data'
|
|
8
|
+
import type { Server } from 'bun'
|
|
9
|
+
import type { CromboloConfig } from './config.ts'
|
|
10
|
+
import { VERSION } from './config.ts'
|
|
11
|
+
import { createCore } from './core.ts'
|
|
12
|
+
import { closeTables } from './custom-tables.ts'
|
|
13
|
+
import type { BackgroundJobs } from './jobs.ts'
|
|
14
|
+
import { logger } from './logging.ts'
|
|
15
|
+
import type { JobRunner } from './owner-jobs.ts'
|
|
16
|
+
import type { SchemaBoot } from './schema.ts'
|
|
17
|
+
import { createSeoHost } from './seo.ts'
|
|
18
|
+
|
|
19
|
+
export interface WorkerHealth {
|
|
20
|
+
/** False once the database is ahead of this node, as on any node that writes. */
|
|
21
|
+
ok: boolean
|
|
22
|
+
readOnly: boolean
|
|
23
|
+
paused: WritesPaused | null
|
|
24
|
+
version: string
|
|
25
|
+
revision: string
|
|
26
|
+
nodeId: string
|
|
27
|
+
role: 'worker'
|
|
28
|
+
/** The job this worker is running, and when it last claimed one. */
|
|
29
|
+
jobs: { running: string | null; lastClaimAt: string | null }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WorkerHandle {
|
|
33
|
+
config: CromboloConfig
|
|
34
|
+
db: Awaited<ReturnType<typeof createCore>>['db']
|
|
35
|
+
schema: SchemaBoot
|
|
36
|
+
/** A worker never seeds, so there is never a password to print. */
|
|
37
|
+
seededAdminPassword: undefined
|
|
38
|
+
health(): WorkerHealth
|
|
39
|
+
/** `/health`, and no answer to anything else. */
|
|
40
|
+
fetch(request: Request): Promise<Response>
|
|
41
|
+
serveOptions: {
|
|
42
|
+
port: number
|
|
43
|
+
maxRequestBodySize: number
|
|
44
|
+
fetch(request: Request, server: Server<unknown>): Promise<Response>
|
|
45
|
+
websocket: { message(): void }
|
|
46
|
+
}
|
|
47
|
+
/** The wired bundles that are off, waiting for an administrator to approve these permissions. */
|
|
48
|
+
waitingBundles(): { id: string; missing: string[] }[]
|
|
49
|
+
/** Core's jobs; each run can be called directly. */
|
|
50
|
+
jobs: BackgroundJobs
|
|
51
|
+
/** The site's and bundles' jobs (story 187). */
|
|
52
|
+
backgroundJobs: JobRunner
|
|
53
|
+
/** Applies cache instructions other nodes appended; the interval calls this too. */
|
|
54
|
+
poll(): Promise<number>
|
|
55
|
+
/** Finishes or releases the running job, then closes the database. */
|
|
56
|
+
close(): Promise<void>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function createWorker(config: CromboloConfig): Promise<WorkerHandle> {
|
|
60
|
+
if (config.role !== 'worker')
|
|
61
|
+
throw new Error(`createWorker needs the worker role, not '${config.role}'.`)
|
|
62
|
+
let seo: ReturnType<typeof createSeoHost> | undefined
|
|
63
|
+
const core = await createCore(config, {
|
|
64
|
+
// E-mails are rendered here, pages never: no island needs its browser code.
|
|
65
|
+
buildIslands: false,
|
|
66
|
+
siteName: async () => (await seo?.siteDefaults())?.siteName ?? config.siteName,
|
|
67
|
+
})
|
|
68
|
+
const { db, schema, poller, bundleGrants, bundlePackages, jobs, jobRunner } = core
|
|
69
|
+
// For the site's name in the e-mails a form sends, as its settings give it.
|
|
70
|
+
seo = createSeoHost({
|
|
71
|
+
db,
|
|
72
|
+
config,
|
|
73
|
+
cache: core.cache,
|
|
74
|
+
isProtected: async () => true,
|
|
75
|
+
draft: async () => undefined,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const health = (): WorkerHealth => {
|
|
79
|
+
const paused = poller.paused
|
|
80
|
+
return {
|
|
81
|
+
ok: !schema.compatibilityMode && !poller.behind,
|
|
82
|
+
readOnly: schema.compatibilityMode || poller.behind || paused !== undefined,
|
|
83
|
+
paused: paused ?? null,
|
|
84
|
+
version: schema.nodeState.version,
|
|
85
|
+
revision: schema.nodeState.revision,
|
|
86
|
+
nodeId: schema.nodeId,
|
|
87
|
+
role: 'worker',
|
|
88
|
+
jobs: jobRunner.state(),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const fetch = async (request: Request): Promise<Response> => {
|
|
92
|
+
if (new URL(request.url).pathname !== '/health') return new Response(null, { status: 404 })
|
|
93
|
+
const report = health()
|
|
94
|
+
return Response.json(report, {
|
|
95
|
+
status: report.ok ? 200 : 503,
|
|
96
|
+
headers: { 'cache-control': 'no-store' },
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
await core.startJobs()
|
|
101
|
+
logger('server').info('Crombolo {version} started on node {nodeId} as worker', {
|
|
102
|
+
version: VERSION,
|
|
103
|
+
nodeId: schema.nodeId,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
config,
|
|
108
|
+
db,
|
|
109
|
+
schema,
|
|
110
|
+
seededAdminPassword: undefined,
|
|
111
|
+
health,
|
|
112
|
+
fetch,
|
|
113
|
+
serveOptions: {
|
|
114
|
+
port: config.port,
|
|
115
|
+
maxRequestBodySize: 1024,
|
|
116
|
+
fetch: (request) => fetch(request),
|
|
117
|
+
websocket: { message() {} },
|
|
118
|
+
},
|
|
119
|
+
waitingBundles: () =>
|
|
120
|
+
config.bundles.flatMap((bundle) => {
|
|
121
|
+
const pkg = bundlePackages.find((candidate) => candidate.id === bundle.id)
|
|
122
|
+
if (!pkg || bundleGrants.isOn(bundle.id)) return []
|
|
123
|
+
return [{ id: bundle.id, missing: bundleGrants.state(pkg).missing }]
|
|
124
|
+
}),
|
|
125
|
+
jobs,
|
|
126
|
+
backgroundJobs: jobRunner,
|
|
127
|
+
poll: () => poller.poll(),
|
|
128
|
+
async close() {
|
|
129
|
+
core.diagnostics.stop()
|
|
130
|
+
await core.stopJobs()
|
|
131
|
+
// A dispatch a job started finishes before the connection goes.
|
|
132
|
+
await core.dispatcher.settled().catch(() => {})
|
|
133
|
+
poller.stop()
|
|
134
|
+
await closeTables(core.chains)
|
|
135
|
+
await db.close()
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
package/src/bundle-tables.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The chains of the bundles a site wires that own tables: worked out once from
|
|
3
|
-
* the configuration and the packages those bundles came from, and run after
|
|
4
|
-
* core's, in the order `bundles` lists them. `docs/bundles.md`, Part 4.
|
|
5
|
-
*
|
|
6
|
-
* Only a node that mounts bundles — one that serves the backoffice — applies
|
|
7
|
-
* them or checks them at boot. A `web` node reads nothing of a bundle's.
|
|
8
|
-
*/
|
|
9
|
-
import { type BundlePackage, discoverBundles } from '@crombolo/backoffice-host'
|
|
10
|
-
import {
|
|
11
|
-
bundlePlan,
|
|
12
|
-
type CompiledTables,
|
|
13
|
-
compileBundleTables,
|
|
14
|
-
type Db,
|
|
15
|
-
type Migration,
|
|
16
|
-
type MigrationPlan,
|
|
17
|
-
migrate,
|
|
18
|
-
type PlannedStep,
|
|
19
|
-
pendingMigrations,
|
|
20
|
-
planUpgrade,
|
|
21
|
-
readLedger,
|
|
22
|
-
readState,
|
|
23
|
-
} from '@crombolo/data'
|
|
24
|
-
import { type ServerBundle, validateServerBundles } from './bundles.ts'
|
|
25
|
-
import { type CromboloConfig, VERSION } from './config.ts'
|
|
26
|
-
|
|
27
|
-
/** One bundle's chain, with what it is called. */
|
|
28
|
-
export interface BundleChain {
|
|
29
|
-
id: string
|
|
30
|
-
name: string
|
|
31
|
-
compiled: CompiledTables
|
|
32
|
-
plan: MigrationPlan
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The chains of every wired bundle that declares tables, in wiring order.
|
|
37
|
-
* Throws when a declaration is invalid; `validateServerBundles` is what reports
|
|
38
|
-
* the reasons, and boot runs it first.
|
|
39
|
-
*/
|
|
40
|
-
export function bundleChains(
|
|
41
|
-
bundles: readonly ServerBundle[],
|
|
42
|
-
packages: readonly BundlePackage[],
|
|
43
|
-
): BundleChain[] {
|
|
44
|
-
return bundles.flatMap((bundle) => {
|
|
45
|
-
if (!bundle.tables) return []
|
|
46
|
-
const owner = packages.find((pkg) => pkg.id === bundle.id)
|
|
47
|
-
const compiled = compileBundleTables(bundle.tables, owner?.version)
|
|
48
|
-
return [{ id: bundle.id, name: owner?.name ?? bundle.id, compiled, plan: bundlePlan(compiled) }]
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The wired bundles cannot be used: every reason, at once. */
|
|
53
|
-
export class BundleConfigurationError extends Error {
|
|
54
|
-
constructor(readonly problems: string[]) {
|
|
55
|
-
super(`The configured bundles cannot be used:\n- ${problems.join('\n- ')}`)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The chains for a site's configuration, discovering its packages and checking
|
|
61
|
-
* the bundles exactly as boot checks them, so a command off the server refuses
|
|
62
|
-
* what the server would.
|
|
63
|
-
*/
|
|
64
|
-
export function bundleChainsFor(
|
|
65
|
-
config: Pick<CromboloConfig, 'bundles' | 'siteDir'>,
|
|
66
|
-
): BundleChain[] {
|
|
67
|
-
if (config.bundles.length === 0) return []
|
|
68
|
-
const packages = discoverBundles(config.siteDir).bundles
|
|
69
|
-
const problems = validateServerBundles(config.bundles, packages, VERSION)
|
|
70
|
-
if (problems.length > 0) throw new BundleConfigurationError(problems)
|
|
71
|
-
return bundleChains(config.bundles, packages)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export interface BundlePending {
|
|
75
|
-
id: string
|
|
76
|
-
name: string
|
|
77
|
-
state: string
|
|
78
|
-
pending: Migration[]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** What each chain still has to run. Throws `UpgradeStateError`, naming the bundle, when one is ahead. */
|
|
82
|
-
export async function pendingBundleSteps(
|
|
83
|
-
db: Db,
|
|
84
|
-
chains: readonly BundleChain[],
|
|
85
|
-
): Promise<BundlePending[]> {
|
|
86
|
-
const out: BundlePending[] = []
|
|
87
|
-
for (const chain of chains)
|
|
88
|
-
out.push({
|
|
89
|
-
id: chain.id,
|
|
90
|
-
name: chain.name,
|
|
91
|
-
state: await readState(db, chain.plan),
|
|
92
|
-
pending: await pendingMigrations(db, chain.plan),
|
|
93
|
-
})
|
|
94
|
-
return out
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Runs each chain's pending steps, in order, after core's. */
|
|
98
|
-
export async function migrateBundles(
|
|
99
|
-
db: Db,
|
|
100
|
-
chains: readonly BundleChain[],
|
|
101
|
-
options: { by?: string } = {},
|
|
102
|
-
): Promise<{ id: string; applied: string[] }[]> {
|
|
103
|
-
const out: { id: string; applied: string[] }[] = []
|
|
104
|
-
for (const chain of chains) {
|
|
105
|
-
const result = await migrate(db, chain.plan, { by: options.by })
|
|
106
|
-
out.push({ id: chain.id, applied: result.applied })
|
|
107
|
-
}
|
|
108
|
-
return out
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** What each chain would run, without running it. */
|
|
112
|
-
export async function planBundleUpgrades(
|
|
113
|
-
db: Db,
|
|
114
|
-
chains: readonly BundleChain[],
|
|
115
|
-
): Promise<{ id: string; name: string; steps: PlannedStep[] }[]> {
|
|
116
|
-
const out: { id: string; name: string; steps: PlannedStep[] }[] = []
|
|
117
|
-
for (const chain of chains)
|
|
118
|
-
out.push({ id: chain.id, name: chain.name, steps: await planUpgrade(db, chain.plan) })
|
|
119
|
-
return out
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export interface BundleTablesReport {
|
|
123
|
-
id: string
|
|
124
|
-
name: string
|
|
125
|
-
pending: { name: string; kind: string; release: string | null }[]
|
|
126
|
-
/** What has run here since the bundle was last purged, oldest first. */
|
|
127
|
-
applied: { name: string; kind: string; release: string | null; appliedAt: string }[]
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Each chain's steps, applied and pending, as the Changes dashboard shows them. */
|
|
131
|
-
export async function bundleTablesReport(
|
|
132
|
-
db: Db,
|
|
133
|
-
chains: readonly BundleChain[],
|
|
134
|
-
): Promise<BundleTablesReport[]> {
|
|
135
|
-
const ledger = await readLedger(db)
|
|
136
|
-
const out: BundleTablesReport[] = []
|
|
137
|
-
for (const chain of chains) {
|
|
138
|
-
const rows = ledger.filter((row) => row.bundle === chain.id)
|
|
139
|
-
const purged = rows.map((row) => row.kind).lastIndexOf('purge')
|
|
140
|
-
const releases = new Map(chain.compiled.steps.map((step) => [step.name, step.release]))
|
|
141
|
-
out.push({
|
|
142
|
-
id: chain.id,
|
|
143
|
-
name: chain.name,
|
|
144
|
-
pending: (await pendingMigrations(db, chain.plan)).map((step) => ({
|
|
145
|
-
name: step.name,
|
|
146
|
-
kind: step.kind ?? 'expand',
|
|
147
|
-
release: step.release ?? null,
|
|
148
|
-
})),
|
|
149
|
-
applied: rows
|
|
150
|
-
.slice(purged + 1)
|
|
151
|
-
.filter((row) => row.kind === 'expand' || row.kind === 'contract')
|
|
152
|
-
.map((row) => ({
|
|
153
|
-
name: row.name,
|
|
154
|
-
kind: row.kind,
|
|
155
|
-
release: releases.get(row.name) ?? null,
|
|
156
|
-
appliedAt: row.appliedAt,
|
|
157
|
-
})),
|
|
158
|
-
})
|
|
159
|
-
}
|
|
160
|
-
return out
|
|
161
|
-
}
|