@crombolo/server 0.37.0 → 0.39.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 +15 -14
- 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 +10 -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-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 +250 -0
- package/src/bundle-install.ts +13 -2
- package/src/bundle-media.ts +12 -11
- package/src/bundles.ts +131 -43
- package/src/component-snippets.ts +1 -1
- package/src/components.ts +6 -1
- package/src/config.ts +61 -3
- 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 +675 -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 +10 -1
- 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/models-builder.ts +1 -1
- package/src/node-operations.ts +122 -0
- package/src/owner-jobs.ts +428 -0
- package/src/ports.ts +36 -154
- package/src/preview-tokens.ts +6 -12
- package/src/remote.ts +3 -1
- package/src/routes.ts +18 -1
- 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 +328 -452
- package/src/settings.ts +18 -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 +23 -6
- package/src/status-pages.ts +19 -5
- package/src/status.ts +11 -9
- package/src/url-settings.ts +1 -2
- package/src/webhook-files.ts +1 -1
- package/src/webhooks.ts +31 -23
- package/src/worker-watch.ts +73 -0
- package/src/worker.ts +138 -0
- package/src/bundle-tables.ts +0 -161
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundles ask for permissions (story 222). A bundle's package declares what it
|
|
3
|
+
* may do with the site, and each capability serves only those. Wiring it in the
|
|
4
|
+
* config approves it, recorded at boot (story 225); an administrator can
|
|
5
|
+
* disable it.
|
|
6
|
+
* docs/bundles.md, "Permissions".
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
type BundlePackage,
|
|
10
|
+
type BundlePermission,
|
|
11
|
+
describePermissions,
|
|
12
|
+
} from '@crombolo/backoffice-host/registry'
|
|
13
|
+
import { type BundleGrant, BundleGrantRepository, type Db } from '@crombolo/data'
|
|
14
|
+
|
|
15
|
+
/** The permissions each capability serves; `log` needs none. */
|
|
16
|
+
export const CAPABILITY_PERMISSIONS: Record<string, readonly BundlePermission[]> = {
|
|
17
|
+
redirects: ['redirects.read', 'redirects.write'],
|
|
18
|
+
documents: ['documents.read'],
|
|
19
|
+
identityProviders: ['identityProviders.read', 'identityProviders.write'],
|
|
20
|
+
media: ['media.read', 'media.write', 'member.delegate'],
|
|
21
|
+
log: [],
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The permission each capability method needs. */
|
|
25
|
+
const METHOD_PERMISSIONS: Record<string, Record<string, BundlePermission>> = {
|
|
26
|
+
redirects: {
|
|
27
|
+
list: 'redirects.read',
|
|
28
|
+
byKey: 'redirects.read',
|
|
29
|
+
save: 'redirects.write',
|
|
30
|
+
replace: 'redirects.write',
|
|
31
|
+
remove: 'redirects.write',
|
|
32
|
+
},
|
|
33
|
+
documents: { url: 'documents.read' },
|
|
34
|
+
identityProviders: {
|
|
35
|
+
list: 'identityProviders.read',
|
|
36
|
+
test: 'identityProviders.read',
|
|
37
|
+
groups: 'identityProviders.read',
|
|
38
|
+
update: 'identityProviders.write',
|
|
39
|
+
reset: 'identityProviders.write',
|
|
40
|
+
},
|
|
41
|
+
media: {
|
|
42
|
+
get: 'media.read',
|
|
43
|
+
list: 'media.read',
|
|
44
|
+
crop: 'media.read',
|
|
45
|
+
create: 'media.write',
|
|
46
|
+
update: 'media.write',
|
|
47
|
+
delete: 'media.write',
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The permissions a package declares. */
|
|
52
|
+
export const declaredPermissions = (pkg: BundlePackage | undefined): string[] =>
|
|
53
|
+
Array.isArray(pkg?.field.permissions) ? [...(pkg?.field.permissions ?? [])] : []
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Why a bundle's capabilities and its declared permissions disagree: a
|
|
57
|
+
* capability with none of its permissions, `media` without `member.delegate`
|
|
58
|
+
* (it always acts as the calling member), or a permission no capability uses.
|
|
59
|
+
*/
|
|
60
|
+
export function permissionProblems(
|
|
61
|
+
label: string,
|
|
62
|
+
capabilities: readonly string[],
|
|
63
|
+
declared: readonly string[],
|
|
64
|
+
): string[] {
|
|
65
|
+
const problems: string[] = []
|
|
66
|
+
const has = new Set(declared)
|
|
67
|
+
for (const capability of capabilities) {
|
|
68
|
+
const serves = CAPABILITY_PERMISSIONS[capability]
|
|
69
|
+
if (!serves || serves.length === 0) continue
|
|
70
|
+
const writes = serves.filter((p) => p !== 'member.delegate')
|
|
71
|
+
if (!writes.some((p) => has.has(p)))
|
|
72
|
+
problems.push(
|
|
73
|
+
`Bundle "${label}" uses "${capability}" but declares none of its permissions (${writes.join(', ')}) in "crombolo.permissions".`,
|
|
74
|
+
)
|
|
75
|
+
if (capability === 'media' && !has.has('member.delegate'))
|
|
76
|
+
problems.push(
|
|
77
|
+
`Bundle "${label}" uses "media", which acts as the calling member, and so needs "member.delegate" in "crombolo.permissions".`,
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
for (const permission of declared) {
|
|
81
|
+
const used = capabilities.some((c) => CAPABILITY_PERMISSIONS[c]?.includes(permission as never))
|
|
82
|
+
if (!used)
|
|
83
|
+
problems.push(
|
|
84
|
+
`Bundle "${label}" declares "${permission}", which none of its capabilities uses; take it out of "crombolo.permissions".`,
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
return problems
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A capability call the bundle was not given the permission for. */
|
|
91
|
+
export class PermissionRefusedError extends Error {
|
|
92
|
+
constructor(
|
|
93
|
+
readonly bundle: string,
|
|
94
|
+
readonly permission: string,
|
|
95
|
+
) {
|
|
96
|
+
super(`Bundle "${bundle}" may not do this: it does not declare "${permission}".`)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A capability with only the methods its declared permissions allow: the rest
|
|
102
|
+
* throw `PermissionRefusedError`, naming the permission.
|
|
103
|
+
*/
|
|
104
|
+
export function gated<T extends object>(
|
|
105
|
+
bundle: string,
|
|
106
|
+
capability: string,
|
|
107
|
+
host: T,
|
|
108
|
+
declared: readonly string[],
|
|
109
|
+
): T {
|
|
110
|
+
const methods = METHOD_PERMISSIONS[capability]
|
|
111
|
+
if (!methods) return host
|
|
112
|
+
const has = new Set(declared)
|
|
113
|
+
const out: Record<string, unknown> = {}
|
|
114
|
+
for (const [name, value] of Object.entries(host)) {
|
|
115
|
+
const needs = methods[name]
|
|
116
|
+
out[name] =
|
|
117
|
+
typeof value === 'function' && needs && !has.has(needs)
|
|
118
|
+
? () => {
|
|
119
|
+
throw new PermissionRefusedError(bundle, needs)
|
|
120
|
+
}
|
|
121
|
+
: typeof value === 'function'
|
|
122
|
+
? (value as (...args: unknown[]) => unknown).bind(host)
|
|
123
|
+
: value
|
|
124
|
+
}
|
|
125
|
+
return Object.freeze(out) as T
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Where a bundle stands: what it may do, who approved it, and whether it is disabled. */
|
|
129
|
+
export interface BundleGrantState {
|
|
130
|
+
bundleId: string
|
|
131
|
+
packageName: string
|
|
132
|
+
version: string
|
|
133
|
+
declared: { permission: string; allows: string }[]
|
|
134
|
+
/** On unless an administrator disabled it: the site's code is its approval. */
|
|
135
|
+
on: boolean
|
|
136
|
+
grant: BundleGrant | undefined
|
|
137
|
+
/** Who disabled it, when it is off. */
|
|
138
|
+
disabledBy: string | null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Whether a bundle may run, what it was approved for, and the switch to stop one. */
|
|
142
|
+
export interface BundleGrants {
|
|
143
|
+
/** Reads the rows again, as a node elsewhere may have changed them. */
|
|
144
|
+
refresh(): Promise<void>
|
|
145
|
+
isOn(bundleId: string): boolean
|
|
146
|
+
state(pkg: BundlePackage): BundleGrantState
|
|
147
|
+
/** Records what a bundle is approved for, and by whom; true when that changed. */
|
|
148
|
+
grant(pkg: BundlePackage, by: string | null): Promise<boolean>
|
|
149
|
+
disable(pkg: BundlePackage, by: string | null): Promise<BundleGrantState>
|
|
150
|
+
enable(pkg: BundlePackage): Promise<BundleGrantState>
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Whether a recorded approval already holds this version and list. */
|
|
154
|
+
export function approvalHolds(pkg: BundlePackage, grant: BundleGrant | undefined): boolean {
|
|
155
|
+
if (!grant || grant.version !== pkg.version) return false
|
|
156
|
+
const declared = declaredPermissions(pkg)
|
|
157
|
+
return (
|
|
158
|
+
grant.permissions.length === declared.length &&
|
|
159
|
+
declared.every((permission) => grant.permissions.includes(permission))
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Bundles as this node sees them (stories 222 and 225): read at boot, read
|
|
165
|
+
* again at most every `refreshMs` (a node elsewhere disabling one), and at once
|
|
166
|
+
* after a change made here.
|
|
167
|
+
*/
|
|
168
|
+
export function createBundleGrants(options: {
|
|
169
|
+
db: Db
|
|
170
|
+
/** The wired bundles' packages, by bundle id. */
|
|
171
|
+
packages: () => ReadonlyMap<string, BundlePackage>
|
|
172
|
+
refreshMs?: number
|
|
173
|
+
}): BundleGrants {
|
|
174
|
+
const repository = new BundleGrantRepository(options.db)
|
|
175
|
+
let grants = new Map<string, BundleGrant>()
|
|
176
|
+
let readAt = 0
|
|
177
|
+
const refreshMs = options.refreshMs ?? 5_000
|
|
178
|
+
|
|
179
|
+
const read = async () => {
|
|
180
|
+
grants = new Map((await repository.all()).map((grant) => [grant.bundleId, grant]))
|
|
181
|
+
readAt = Date.now()
|
|
182
|
+
}
|
|
183
|
+
const stale = () => {
|
|
184
|
+
if (Date.now() - readAt > refreshMs) void read().catch(() => {})
|
|
185
|
+
}
|
|
186
|
+
const stateOf = (pkg: BundlePackage): BundleGrantState => {
|
|
187
|
+
const grant = grants.get(pkg.id)
|
|
188
|
+
return {
|
|
189
|
+
bundleId: pkg.id,
|
|
190
|
+
packageName: pkg.packageName,
|
|
191
|
+
version: pkg.version,
|
|
192
|
+
declared: describePermissions(declaredPermissions(pkg)),
|
|
193
|
+
on: !grant?.disabledAt,
|
|
194
|
+
grant,
|
|
195
|
+
disabledBy: grant?.disabledAt ? grant.disabledBy : null,
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
refresh: read,
|
|
201
|
+
isOn(bundleId) {
|
|
202
|
+
stale()
|
|
203
|
+
return options.packages().has(bundleId) && !grants.get(bundleId)?.disabledAt
|
|
204
|
+
},
|
|
205
|
+
state(pkg) {
|
|
206
|
+
stale()
|
|
207
|
+
return stateOf(pkg)
|
|
208
|
+
},
|
|
209
|
+
async grant(pkg, by) {
|
|
210
|
+
if (approvalHolds(pkg, grants.get(pkg.id))) return false
|
|
211
|
+
await repository.grant({
|
|
212
|
+
bundleId: pkg.id,
|
|
213
|
+
packageName: pkg.packageName,
|
|
214
|
+
version: pkg.version,
|
|
215
|
+
permissions: declaredPermissions(pkg),
|
|
216
|
+
grantedBy: by,
|
|
217
|
+
})
|
|
218
|
+
await read()
|
|
219
|
+
return true
|
|
220
|
+
},
|
|
221
|
+
async disable(pkg, by) {
|
|
222
|
+
await repository.disable(
|
|
223
|
+
{ bundleId: pkg.id, packageName: pkg.packageName, version: pkg.version },
|
|
224
|
+
by,
|
|
225
|
+
)
|
|
226
|
+
await read()
|
|
227
|
+
return stateOf(pkg)
|
|
228
|
+
},
|
|
229
|
+
async enable(pkg) {
|
|
230
|
+
await repository.enable(pkg.id)
|
|
231
|
+
await read()
|
|
232
|
+
return stateOf(pkg)
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Who a bundle's approval names when it came from being wired in the config. */
|
|
238
|
+
export const APPROVED_BY_CODE = "the site's code"
|
|
239
|
+
|
|
240
|
+
/** What a route of a bundle that is off answers. */
|
|
241
|
+
export function disabledResponse(bundleId: string): Response {
|
|
242
|
+
return Response.json(
|
|
243
|
+
{
|
|
244
|
+
title: 'Turned off',
|
|
245
|
+
detail: `The bundle "${bundleId}" has been disabled by an administrator, in Packages → Installed.`,
|
|
246
|
+
status: 503,
|
|
247
|
+
},
|
|
248
|
+
{ status: 503, headers: { 'content-type': 'application/problem+json' } },
|
|
249
|
+
)
|
|
250
|
+
}
|
package/src/bundle-install.ts
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
peerRangeProblem,
|
|
23
23
|
readBundlePackage,
|
|
24
24
|
readPackageJson,
|
|
25
|
-
} from '@crombolo/backoffice-host'
|
|
25
|
+
} from '@crombolo/backoffice-host/registry'
|
|
26
26
|
import { ObjectTypes } from '@crombolo/core'
|
|
27
27
|
import {
|
|
28
28
|
answersByNode,
|
|
@@ -84,6 +84,7 @@ import { syncDomainsFile } from './domains.ts'
|
|
|
84
84
|
import { mediaStoreFor, placeBlobs } from './media-files.ts'
|
|
85
85
|
import { componentAliasesIn, syncSchemaDirectory } from './schema.ts'
|
|
86
86
|
import { publishSchema } from './schema-store.ts'
|
|
87
|
+
import { applySiteData, type SiteDataApplied } from './site-data.ts'
|
|
87
88
|
import {
|
|
88
89
|
carriesAdministrator,
|
|
89
90
|
prunePeople,
|
|
@@ -204,6 +205,7 @@ export function readBundle(
|
|
|
204
205
|
let opened: ReturnType<typeof openBundle> = {
|
|
205
206
|
set: loaded.set,
|
|
206
207
|
people: undefined,
|
|
208
|
+
siteData: undefined,
|
|
207
209
|
openBlob: (_key, bytes) => bytes,
|
|
208
210
|
}
|
|
209
211
|
const peopleOnly = options.withoutPeople && loaded.set.manifest.content !== 'sealed'
|
|
@@ -432,6 +434,8 @@ export interface ContentImportResult {
|
|
|
432
434
|
published: number
|
|
433
435
|
publishFailures: { key: string; reason: string }[]
|
|
434
436
|
problems: string[]
|
|
437
|
+
/** What the file's site data did here, when it carried any (story 189). */
|
|
438
|
+
siteData?: SiteDataApplied
|
|
435
439
|
}
|
|
436
440
|
|
|
437
441
|
/**
|
|
@@ -697,6 +701,9 @@ export async function installBundleContent(
|
|
|
697
701
|
await publishSchema(config.schemaStore, config.schemaDir)
|
|
698
702
|
}
|
|
699
703
|
const kept = promoteStaged(config, runDir, runId)
|
|
704
|
+
// On the owners' own connections, so after crombolo's transaction rather than
|
|
705
|
+
// in it: each owner is replaced in a transaction of its own.
|
|
706
|
+
const siteData = set.siteData ? await applySiteData(db, config, set.siteData) : undefined
|
|
700
707
|
// Domains belong to where the site runs, never to the file: rebound by the
|
|
701
708
|
// site's own `domains.toml` onto the pages now in its tree.
|
|
702
709
|
if (replace) await syncDomainsFile(db, config.siteDir)
|
|
@@ -717,7 +724,11 @@ export async function installBundleContent(
|
|
|
717
724
|
blobs: placed,
|
|
718
725
|
published: result.published.length,
|
|
719
726
|
publishFailures: result.publishFailures,
|
|
720
|
-
problems:
|
|
727
|
+
problems: [
|
|
728
|
+
...prepared.problems,
|
|
729
|
+
...(siteData?.failed ?? []).map((failure) => `site data not written for ${failure}`),
|
|
730
|
+
],
|
|
731
|
+
...(siteData ? { siteData } : {}),
|
|
721
732
|
sections: staged
|
|
722
733
|
? {
|
|
723
734
|
written: staged.written,
|
package/src/bundle-media.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* list, and its size, counted as it is read. An image is turned upright and
|
|
16
16
|
* stored without its metadata, and what that metadata said is returned.
|
|
17
17
|
*/
|
|
18
|
-
import { hasSection } from '@crombolo/api-management'
|
|
18
|
+
import { hasSection } from '@crombolo/api-management/domain'
|
|
19
19
|
import { type MediaActor, type MediaOperations, memberActor, userActor } from '@crombolo/content'
|
|
20
20
|
import {
|
|
21
21
|
checkMemberUpload,
|
|
@@ -314,20 +314,21 @@ export function createBundleMedia(deps: BundleMediaDeps) {
|
|
|
314
314
|
return { at, missing: [] as string[] }
|
|
315
315
|
}
|
|
316
316
|
const childFolder = async (parentId: number, name: string) => {
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
317
|
+
const siblings = () =>
|
|
318
|
+
db.q
|
|
319
|
+
.selectFrom('node')
|
|
320
|
+
.select(['id', 'unique_id', 'path', 'text'])
|
|
321
|
+
.where('parent_id', '=', parentId)
|
|
322
|
+
.where('node_object_type', '=', ObjectTypes.Media)
|
|
323
|
+
.where('trashed', '=', db.dialect.boolValue(false) as boolean)
|
|
324
|
+
const rows = await siblings()
|
|
325
|
+
.where((eb) => eb(eb.fn('lower', ['text']), '=', lower(name)))
|
|
326
|
+
.execute()
|
|
321
327
|
const row =
|
|
322
328
|
rows[0] ??
|
|
323
329
|
// SQLite lowers only ASCII.
|
|
324
330
|
(/[^ -~]/.test(name)
|
|
325
|
-
? (
|
|
326
|
-
await db.query<{ id: number; unique_id: string; path: string; text: string }>(
|
|
327
|
-
'SELECT id, unique_id, path, text FROM node WHERE parent_id = ? AND node_object_type = ? AND trashed = ?',
|
|
328
|
-
[parentId, ObjectTypes.Media, db.dialect.boolValue(false)],
|
|
329
|
-
)
|
|
330
|
-
).find((r) => lower(String(r.text)) === lower(name))
|
|
331
|
+
? (await siblings().execute()).find((r) => lower(String(r.text)) === lower(name))
|
|
331
332
|
: undefined)
|
|
332
333
|
return row
|
|
333
334
|
? { id: Number(row.id), key: normaliseUuid(String(row.unique_id)), path: String(row.path) }
|
package/src/bundles.ts
CHANGED
|
@@ -38,20 +38,22 @@
|
|
|
38
38
|
* release refuses the boot.
|
|
39
39
|
*
|
|
40
40
|
* - A bundle that declares `tables` owns them: its own ledgered chain, written
|
|
41
|
-
* with the declarative builder in `@crombolo/data`,
|
|
42
|
-
*
|
|
41
|
+
* with the declarative builder in `@crombolo/data`, in a schema or file of
|
|
42
|
+
* its own, and `context.bundle.database`, a Kysely handle on the connection
|
|
43
|
+
* that reaches only them (`docs/bundles.md`, Part 4; story 189).
|
|
43
44
|
*
|
|
44
|
-
* Deliberately not offered:
|
|
45
|
+
* Deliberately not offered: crombolo's own `Db`, and anything that writes the
|
|
45
46
|
* site's files.
|
|
46
47
|
*/
|
|
47
48
|
|
|
48
|
-
import { hasCultureAccess, hasSection, type Principal } from '@crombolo/api-management'
|
|
49
|
+
import { hasCultureAccess, hasSection, type Principal } from '@crombolo/api-management/domain'
|
|
49
50
|
import {
|
|
50
51
|
BUNDLE_ID,
|
|
51
52
|
BUNDLE_SCHEMA_VERSION,
|
|
52
53
|
type BundlePackage,
|
|
53
54
|
peerRangeProblem,
|
|
54
|
-
|
|
55
|
+
schemaProblem,
|
|
56
|
+
} from '@crombolo/backoffice-host/registry'
|
|
55
57
|
import type { Middleware, RouteMethod, SiteRoute } from '@crombolo/common'
|
|
56
58
|
import {
|
|
57
59
|
type AppAlias,
|
|
@@ -62,14 +64,15 @@ import {
|
|
|
62
64
|
} from '@crombolo/core'
|
|
63
65
|
import {
|
|
64
66
|
type BundleTables,
|
|
65
|
-
type BundleTablesCapability,
|
|
66
67
|
bundleTablesCompatibility,
|
|
67
|
-
|
|
68
|
+
compileTables,
|
|
68
69
|
type Db,
|
|
69
70
|
type RedirectMatchKind,
|
|
70
71
|
RedirectRepository,
|
|
71
72
|
type RedirectRow,
|
|
72
73
|
type RedirectTargetKind,
|
|
74
|
+
SITE_OWNER,
|
|
75
|
+
type TablesDatabase,
|
|
73
76
|
} from '@crombolo/data'
|
|
74
77
|
import {
|
|
75
78
|
normaliseRedirectPath,
|
|
@@ -78,9 +81,17 @@ import {
|
|
|
78
81
|
type RequestMember,
|
|
79
82
|
type SiteServices,
|
|
80
83
|
} from '@crombolo/render'
|
|
84
|
+
import { anonymiseProblems } from './anonymise-rules.ts'
|
|
85
|
+
import {
|
|
86
|
+
declaredPermissions,
|
|
87
|
+
disabledResponse,
|
|
88
|
+
gated,
|
|
89
|
+
permissionProblems,
|
|
90
|
+
} from './bundle-grants.ts'
|
|
81
91
|
import type { BundleMedia, BundleMediaDeclaration } from './bundle-media.ts'
|
|
82
92
|
import type { BundleForms } from './form-types.ts'
|
|
83
93
|
import type { BundleIdentityProviders } from './identity-provider-overrides.ts'
|
|
94
|
+
import { type JobInfo, type JobSchedule, jobProblems, type OwnerJobs } from './owner-jobs.ts'
|
|
84
95
|
import type { DeclaredRoute } from './routes.ts'
|
|
85
96
|
import type { BundleEvents } from './webhooks.ts'
|
|
86
97
|
|
|
@@ -164,8 +175,6 @@ export interface BundleHost {
|
|
|
164
175
|
log: BundleLog
|
|
165
176
|
/** The identity providers the config declares, and an administrator's overrides of their settings (story 033). */
|
|
166
177
|
identityProviders: BundleIdentityProviders
|
|
167
|
-
/** The bundle's own tables, and only for a bundle that declares some. */
|
|
168
|
-
tables: BundleTablesCapability
|
|
169
178
|
/**
|
|
170
179
|
* Media items in the folders the bundle declares, as the member a route was
|
|
171
180
|
* given or the back-office user; refused on a public route (story 198).
|
|
@@ -208,19 +217,20 @@ export const BUNDLE_CAPABILITIES: readonly BundleCapability[] = [
|
|
|
208
217
|
'redirects',
|
|
209
218
|
'documents',
|
|
210
219
|
'log',
|
|
211
|
-
'tables',
|
|
212
220
|
'identityProviders',
|
|
213
221
|
'media',
|
|
214
222
|
]
|
|
215
223
|
|
|
216
|
-
/**
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
> &
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
+
/**
|
|
225
|
+
* What a handler is handed: the declared capabilities, and for a bundle that
|
|
226
|
+
* declares tables, `database`, typed from the declaration (story 189).
|
|
227
|
+
*/
|
|
228
|
+
export type HostFor<C extends BundleCapability, T extends BundleTables> = Pick<BundleHost, C> & {
|
|
229
|
+
/** Only for a bundle that declares tables: a Kysely handle on them and nothing else. */
|
|
230
|
+
readonly database: TablesDatabase<T>
|
|
231
|
+
/** Only for a bundle that declares jobs: queue one, or read how one is going (story 187). */
|
|
232
|
+
readonly jobs: OwnerJobs
|
|
233
|
+
}
|
|
224
234
|
|
|
225
235
|
/** A signed-in member as a member route is given them: nothing of the back office. */
|
|
226
236
|
export interface BundleMember {
|
|
@@ -240,6 +250,8 @@ export type BundleCaller =
|
|
|
240
250
|
| { readonly kind: 'backoffice'; readonly principal: Principal }
|
|
241
251
|
| { readonly kind: 'member'; readonly member: BundleMember; readonly via: 'cookie' | 'token' }
|
|
242
252
|
| { readonly kind: 'public' }
|
|
253
|
+
/** A background job (story 187): nobody, treated as the public. */
|
|
254
|
+
| { readonly kind: 'job' }
|
|
243
255
|
|
|
244
256
|
/**
|
|
245
257
|
* What a bundle's route handler is given besides 211's context: its declared
|
|
@@ -306,8 +318,24 @@ export interface ServerBundle<
|
|
|
306
318
|
* to the chain by itself.
|
|
307
319
|
*/
|
|
308
320
|
middleware?: Middleware<SiteServices, PublishedContent, RequestMember>
|
|
321
|
+
/**
|
|
322
|
+
* Its background jobs, by name (story 187): each `(payload, context)`, given
|
|
323
|
+
* its capabilities for no caller and its own `database`, as a public route
|
|
324
|
+
* would be, with `context.job` for its progress and its signal.
|
|
325
|
+
*/
|
|
326
|
+
jobs?: Record<string, BundleJob<C, T>>
|
|
327
|
+
/** When its jobs run by themselves: cron expressions in a timezone. */
|
|
328
|
+
schedules?: readonly JobSchedule[]
|
|
309
329
|
}
|
|
310
330
|
|
|
331
|
+
/** A bundle's background job (story 187). */
|
|
332
|
+
export type BundleJob<
|
|
333
|
+
C extends BundleCapability = BundleCapability,
|
|
334
|
+
T extends BundleTables = BundleTables,
|
|
335
|
+
// biome-ignore lint/suspicious/noExplicitAny: a payload is the bundle's to type
|
|
336
|
+
P = any,
|
|
337
|
+
> = (payload: P, context: { bundle: HostFor<C, T>; job: JobInfo }) => unknown
|
|
338
|
+
|
|
311
339
|
/** A bundle as mounted, with the prefix its routes answer below. */
|
|
312
340
|
export interface MountedBundle {
|
|
313
341
|
id: string
|
|
@@ -369,9 +397,13 @@ export function validateServerBundles(
|
|
|
369
397
|
problems.push(
|
|
370
398
|
`Bundle "${label}" asked for the capability "${capability}", which no host offers.`,
|
|
371
399
|
)
|
|
372
|
-
if (bundle.capabilities.includes('tables')
|
|
400
|
+
if ((bundle.capabilities as readonly string[]).includes('tables'))
|
|
401
|
+
problems.push(
|
|
402
|
+
`Bundle "${label}" asks for "tables", which is gone: declaring tables gives its handlers context.bundle.database (docs/bundles.md, Part 4).`,
|
|
403
|
+
)
|
|
404
|
+
if (bundle.tables && bundle.id === SITE_OWNER)
|
|
373
405
|
problems.push(
|
|
374
|
-
`Bundle "${label}"
|
|
406
|
+
`Bundle "${label}" declares tables under the id "${SITE_OWNER}", which names the site's own tables.`,
|
|
375
407
|
)
|
|
376
408
|
if (bundle.tables) {
|
|
377
409
|
if (bundle.tables.id !== bundle.id)
|
|
@@ -379,8 +411,12 @@ export function validateServerBundles(
|
|
|
379
411
|
`Bundle "${label}" declares its tables under the id "${bundle.tables.id}"; a bundle's tables are under its own id.`,
|
|
380
412
|
)
|
|
381
413
|
const owner = packages.find((pkg) => pkg.id === bundle.id)
|
|
382
|
-
|
|
414
|
+
const compiled = compileTables(bundle.tables, owner?.version)
|
|
415
|
+
for (const problem of compiled.problems)
|
|
383
416
|
problems.push(`Bundle "${label}" tables: ${problem}.`)
|
|
417
|
+
if (owner?.field.anonymise !== undefined && compiled.problems.length === 0)
|
|
418
|
+
for (const problem of anonymiseProblems(owner.field.anonymise, compiled.schema))
|
|
419
|
+
problems.push(`Bundle "${label}"'s package.json: crombolo.${problem}.`)
|
|
384
420
|
// Read by an operator's tooling before anything is installed, so it must
|
|
385
421
|
// say what the plan does — derived, not asserted, as core's is.
|
|
386
422
|
if (owner) {
|
|
@@ -391,13 +427,21 @@ export function validateServerBundles(
|
|
|
391
427
|
)
|
|
392
428
|
}
|
|
393
429
|
}
|
|
430
|
+
if (!bundle.tables && packages.find((pkg) => pkg.id === bundle.id)?.field.anonymise)
|
|
431
|
+
problems.push(
|
|
432
|
+
`Bundle "${label}"'s package.json declares "crombolo.anonymise", and the bundle declares no tables for it to describe.`,
|
|
433
|
+
)
|
|
394
434
|
for (const problem of mediaProblems(bundle))
|
|
395
435
|
problems.push(`Bundle "${label}" media: ${problem}.`)
|
|
436
|
+
problems.push(...jobProblems(`Bundle "${label}"`, bundle.jobs, bundle.schedules))
|
|
437
|
+
{
|
|
438
|
+
const pkg = packages.find((candidate) => candidate.id === bundle.id)
|
|
439
|
+
if (pkg?.carries.server && Array.isArray(pkg.field.permissions))
|
|
440
|
+
problems.push(...permissionProblems(label, bundle.capabilities, declaredPermissions(pkg)))
|
|
441
|
+
}
|
|
396
442
|
const owner = packages.find((pkg) => pkg.id === bundle.id)
|
|
397
443
|
if (owner?.carries.server && (owner.field.schemaVersion ?? 1) < BUNDLE_SCHEMA_VERSION)
|
|
398
|
-
problems.push(
|
|
399
|
-
`Bundle "${label}" is written for bundle schema ${owner.field.schemaVersion ?? 1}; this release reads ${BUNDLE_SCHEMA_VERSION}. Its routes are route handlers since 0.37: see "Routes" in docs/bundles.md, then set "crombolo.schemaVersion": ${BUNDLE_SCHEMA_VERSION}.`,
|
|
400
|
-
)
|
|
444
|
+
problems.push(`Bundle "${label}": ${schemaProblem(owner.field.schemaVersion ?? 1)}.`)
|
|
401
445
|
const routes = new Set<string>()
|
|
402
446
|
for (const route of bundle.routes ?? []) {
|
|
403
447
|
if (!route || typeof route !== 'object' || !route.handlers || !route.options) {
|
|
@@ -634,10 +678,7 @@ export function createBundleRedirects(options: {
|
|
|
634
678
|
* refuses a public caller. It is told the bundle too, for what it declared.
|
|
635
679
|
*/
|
|
636
680
|
export type CallerCapabilities = {
|
|
637
|
-
[K in
|
|
638
|
-
caller: BundleCaller,
|
|
639
|
-
bundle: ServerBundle,
|
|
640
|
-
) => BundleHost[K]
|
|
681
|
+
[K in BundleCapability]?: (caller: BundleCaller, bundle: ServerBundle) => BundleHost[K]
|
|
641
682
|
}
|
|
642
683
|
|
|
643
684
|
export interface ServerBundlesOptions {
|
|
@@ -648,13 +689,20 @@ export interface ServerBundlesOptions {
|
|
|
648
689
|
coreVersion: string
|
|
649
690
|
/** `<backoffice>/crombolo/api`, from `createBackOfficePaths`. */
|
|
650
691
|
pluginApiPath: string
|
|
651
|
-
host: Omit<BundleHost, '
|
|
692
|
+
host: Omit<BundleHost, 'media'> & { media?: BundleMedia }
|
|
652
693
|
/** Capabilities built per request from the caller, in place of the ones in `host`. */
|
|
653
694
|
callerHost?: CallerCapabilities
|
|
654
695
|
/** Named per bundle, so a log line says which one wrote it. */
|
|
655
696
|
log(bundleId: string): BundleLog
|
|
656
|
-
/** A bundle's own tables,
|
|
657
|
-
|
|
697
|
+
/** A bundle's own tables, on the connection that reaches only them; absent where none are. */
|
|
698
|
+
database?(bundle: ServerBundle): TablesDatabase | undefined
|
|
699
|
+
/**
|
|
700
|
+
* Whether a bundle is turned on: its grant covers what it declares (story
|
|
701
|
+
* 222). A bundle that is off answers every route as waiting. Absent, all are on.
|
|
702
|
+
*/
|
|
703
|
+
isOn?(bundleId: string): boolean
|
|
704
|
+
/** A bundle's queue, recording who queued from the caller (story 187). */
|
|
705
|
+
jobs?(bundleId: string, queuedBy: string | null): OwnerJobs
|
|
658
706
|
}
|
|
659
707
|
|
|
660
708
|
/** Runs a route through 211's pipeline: the server's `routes.invoke`. */
|
|
@@ -683,8 +731,22 @@ export interface ServerBundles {
|
|
|
683
731
|
): Promise<Response | undefined>
|
|
684
732
|
/** The bundles' site routes, for the server's route table, each given its bundle's capabilities. */
|
|
685
733
|
siteRoutes(): DeclaredRoute[]
|
|
734
|
+
/** What a bundle's job is given: its capabilities for no caller, as on a public route (story 187). */
|
|
735
|
+
jobContext(bundleId: string): Pick<BundleHost, BundleCapability> | undefined
|
|
686
736
|
}
|
|
687
737
|
|
|
738
|
+
const JOB_CALLER: BundleCaller = Object.freeze({ kind: 'job' })
|
|
739
|
+
|
|
740
|
+
/** Who queued a job, for administrators to read: it grants nothing. */
|
|
741
|
+
const queuedByOf = (caller: BundleCaller): string | null =>
|
|
742
|
+
caller.kind === 'member'
|
|
743
|
+
? `member ${caller.member.username}`
|
|
744
|
+
: caller.kind === 'backoffice'
|
|
745
|
+
? `user ${caller.principal.email}`
|
|
746
|
+
: caller.kind === 'job'
|
|
747
|
+
? 'a job'
|
|
748
|
+
: 'a visitor'
|
|
749
|
+
|
|
688
750
|
const freezeMember = (member: RequestMember): BundleMember =>
|
|
689
751
|
Object.freeze({
|
|
690
752
|
key: member.key,
|
|
@@ -728,39 +790,64 @@ export function createServerBundles(options: ServerBundlesOptions): ServerBundle
|
|
|
728
790
|
}
|
|
729
791
|
})
|
|
730
792
|
|
|
731
|
-
//
|
|
732
|
-
const
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
793
|
+
// Made once per bundle, at its first request, each on its own tables alone.
|
|
794
|
+
const databases = new Map<string, TablesDatabase>()
|
|
795
|
+
const databaseOf = (bundle: ServerBundle): TablesDatabase | undefined => {
|
|
796
|
+
if (!bundle.tables) return undefined
|
|
797
|
+
let opened = databases.get(bundle.id)
|
|
798
|
+
if (!opened) {
|
|
799
|
+
opened = options.database?.(bundle)
|
|
800
|
+
if (!opened) return undefined
|
|
801
|
+
databases.set(bundle.id, opened)
|
|
802
|
+
}
|
|
803
|
+
return opened
|
|
804
|
+
}
|
|
737
805
|
/** Only the declared capabilities, frozen, so a handler cannot reach further. */
|
|
738
806
|
const contextFor = (
|
|
739
807
|
bundle: ServerBundle,
|
|
740
808
|
caller: BundleCaller,
|
|
741
809
|
): Pick<BundleHost, BundleCapability> => {
|
|
742
810
|
const host: Record<string, unknown> = {}
|
|
811
|
+
const declared = declaredPermissions(owners.get(bundle.id))
|
|
743
812
|
for (const capability of bundle.capabilities) {
|
|
744
|
-
const forCaller =
|
|
745
|
-
|
|
813
|
+
const forCaller = options.callerHost?.[capability]
|
|
814
|
+
const given = forCaller
|
|
746
815
|
? forCaller(caller, bundle)
|
|
747
816
|
: capability === 'log'
|
|
748
817
|
? (logs.get(bundle.id) as BundleLog)
|
|
749
|
-
: capability
|
|
750
|
-
|
|
751
|
-
|
|
818
|
+
: options.host[capability]
|
|
819
|
+
// Only what its declared permissions allow (story 222).
|
|
820
|
+
host[capability] = given ? gated(bundle.id, capability, given, declared) : given
|
|
752
821
|
}
|
|
822
|
+
const database = databaseOf(bundle)
|
|
823
|
+
if (database) host.database = database
|
|
824
|
+
if (bundle.jobs && options.jobs) host.jobs = options.jobs(bundle.id, queuedByOf(caller))
|
|
753
825
|
return Object.freeze(host) as Pick<BundleHost, BundleCapability>
|
|
754
826
|
}
|
|
755
827
|
|
|
828
|
+
/** A site route that answers as waiting while its bundle is off. */
|
|
829
|
+
const whileOn = (bundleId: string, route: SiteRoute): SiteRoute => {
|
|
830
|
+
const isOn = options.isOn
|
|
831
|
+
if (!isOn) return route
|
|
832
|
+
const handlers: SiteRoute['handlers'] = {}
|
|
833
|
+
for (const [method, handler] of Object.entries(route.handlers))
|
|
834
|
+
handlers[method as RouteMethod] = (request, context) =>
|
|
835
|
+
isOn(bundleId) ? handler(request, context) : disabledResponse(bundleId)
|
|
836
|
+
return { ...route, handlers }
|
|
837
|
+
}
|
|
838
|
+
|
|
756
839
|
return {
|
|
757
840
|
mounted,
|
|
758
841
|
basePath,
|
|
842
|
+
jobContext(bundleId) {
|
|
843
|
+
const bundle = byId.get(bundleId)
|
|
844
|
+
return bundle ? contextFor(bundle, JOB_CALLER) : undefined
|
|
845
|
+
},
|
|
759
846
|
siteRoutes() {
|
|
760
847
|
return options.bundles.flatMap((bundle) =>
|
|
761
848
|
(bundle.routes ?? []).filter(onSite).map(
|
|
762
849
|
(route): DeclaredRoute => ({
|
|
763
|
-
route: route as SiteRoute,
|
|
850
|
+
route: whileOn(bundle.id, route as SiteRoute),
|
|
764
851
|
owner: `the bundle ${bundle.id}`,
|
|
765
852
|
extra: ({ member, via }) => {
|
|
766
853
|
const caller: BundleCaller = member
|
|
@@ -792,6 +879,7 @@ export function createServerBundles(options: ServerBundlesOptions): ServerBundle
|
|
|
792
879
|
for (const route of (bundle.routes ?? []).filter((candidate) => !onSite(candidate))) {
|
|
793
880
|
const params = matchRoute(route.path, requested)
|
|
794
881
|
if (!params) continue
|
|
882
|
+
if (options.isOn && !options.isOn(bundle.id)) return disabledResponse(bundle.id)
|
|
795
883
|
const caller: BundleCaller = Object.freeze({ kind: 'backoffice', principal })
|
|
796
884
|
return invoke(
|
|
797
885
|
{ route: route as SiteRoute, owner: `the bundle ${bundle.id}` },
|