@crombolo/server 0.60.0 → 0.61.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 +14 -14
- package/src/adapters/audit.ts +24 -4
- package/src/approvals-api.ts +2 -2
- package/src/bundle-admin.ts +194 -25
- package/src/bundle-api.ts +67 -16
- package/src/bundle-code-install.ts +18 -1
- package/src/bundle-grants.ts +78 -15
- package/src/bundles.ts +143 -4
- package/src/component-templates-api.ts +2 -2
- package/src/config.ts +20 -3
- package/src/core.ts +49 -19
- package/src/extension-activity.ts +237 -0
- package/src/extension-components.ts +17 -1
- package/src/extension-discovery.ts +37 -3
- package/src/extension-process.ts +140 -28
- package/src/extension-screens.ts +383 -0
- package/src/grant-policy.ts +57 -0
- package/src/index.ts +10 -1
- package/src/jobs.ts +23 -0
- package/src/marketplace.ts +190 -29
- package/src/ports.ts +1 -2
- package/src/remote.ts +12 -4
- package/src/server.ts +79 -14
- package/src/settings.ts +17 -0
- package/src/site-file.ts +5 -2
package/src/core.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
BUNDLE_PERMISSIONS,
|
|
12
12
|
createBackOfficePaths,
|
|
13
13
|
createExtensionRegistry,
|
|
14
|
+
USER_DELEGATE,
|
|
14
15
|
} from '@crombolo/backoffice-host/registry'
|
|
15
16
|
import { mediaJobHandlers } from '@crombolo/content'
|
|
16
17
|
import { ObjectTypes } from '@crombolo/core'
|
|
@@ -43,7 +44,7 @@ import { recordAudit } from './adapters/audit.ts'
|
|
|
43
44
|
import { createMediaSource, createPublishedContentSource } from './adapters/documents.ts'
|
|
44
45
|
import { backOfficeUrl, documentEditUrl } from './back-office-url.ts'
|
|
45
46
|
import { BACKOFFICE_EXTERNAL_PATH } from './backoffice-external.ts'
|
|
46
|
-
import {
|
|
47
|
+
import { createBundleApi, extensionPrincipal } from './bundle-api.ts'
|
|
47
48
|
import {
|
|
48
49
|
APPROVED_BY_CODE,
|
|
49
50
|
createBundleGrants,
|
|
@@ -66,6 +67,7 @@ import { databaseFor, openTables, siteTablesProblems, tablesChains } from './cus
|
|
|
66
67
|
import { attachDatabase, bootstrapDatabase } from './database.ts'
|
|
67
68
|
import { createDiagnostics } from './diagnostics.ts'
|
|
68
69
|
import { resolveEmailPort } from './email.ts'
|
|
70
|
+
import { createActivityRecorder, personOf, timed } from './extension-activity.ts'
|
|
69
71
|
import { createExtensionComponents } from './extension-components.ts'
|
|
70
72
|
import {
|
|
71
73
|
entryCovers,
|
|
@@ -195,11 +197,14 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
195
197
|
const entryProblems = extensionEntryProblems(config.extensions, bundlePackages)
|
|
196
198
|
if (entryProblems.length > 0)
|
|
197
199
|
throw new Error(`The installed extensions cannot be started:\n- ${entryProblems.join('\n- ')}`)
|
|
200
|
+
// Story 327: what each extension does, kept for the Activity view.
|
|
201
|
+
const activity = createActivityRecorder(() => db)
|
|
198
202
|
const extensionsStarted = await startExtensions(
|
|
199
203
|
config,
|
|
200
204
|
bundlePackages,
|
|
201
205
|
await readRecordedExtensions(config),
|
|
202
206
|
bundleLog,
|
|
207
|
+
activity,
|
|
203
208
|
)
|
|
204
209
|
const extensionProcesses = extensionsStarted.processes
|
|
205
210
|
const failedExtensions = extensionsStarted.failed
|
|
@@ -302,12 +307,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
302
307
|
)
|
|
303
308
|
const bundle = { id: pkg.id }
|
|
304
309
|
// Recorded where the database is written, and audited when it changes.
|
|
305
|
-
if (
|
|
306
|
-
owns &&
|
|
307
|
-
(await bundleGrants.grant(pkg, APPROVED_BY_CODE, {
|
|
308
|
-
actAs: entry?.actAs ? actAsChoice(entry.actAs) : undefined,
|
|
309
|
-
}))
|
|
310
|
-
)
|
|
310
|
+
if (owns && (await bundleGrants.grant(pkg, APPROVED_BY_CODE)))
|
|
311
311
|
await recordAudit(db, {
|
|
312
312
|
category: 'system',
|
|
313
313
|
action: 'Grant',
|
|
@@ -580,6 +580,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
580
580
|
const bundles =
|
|
581
581
|
config.bundles.length > 0
|
|
582
582
|
? createServerBundles({
|
|
583
|
+
activity,
|
|
583
584
|
bundles: config.bundles,
|
|
584
585
|
packages: bundlePackages,
|
|
585
586
|
coreVersion: VERSION,
|
|
@@ -624,12 +625,14 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
624
625
|
},
|
|
625
626
|
log: (id) => bundleLog(id),
|
|
626
627
|
callerHost: {
|
|
627
|
-
// Story 308: the site's areas, as the extension or
|
|
628
|
+
// Story 308: the site's areas, as the extension; story 325, or as the person using it.
|
|
628
629
|
api: (caller, bundle) => {
|
|
629
630
|
const pkg = bundlePackages.find((candidate) => candidate.id === bundle.id)
|
|
630
631
|
const granted = () => bundleGrants.granted(bundle.id)
|
|
631
632
|
return createBundleApi({
|
|
632
633
|
bundleId: bundle.id,
|
|
634
|
+
bundleName: pkg?.name ?? bundle.id,
|
|
635
|
+
activity: { recorder: activity, person: personOf(caller) },
|
|
633
636
|
origin: (config.applicationUrl || 'http://localhost').replace(/\/+$/, ''),
|
|
634
637
|
backOfficePath: config.backOfficePath,
|
|
635
638
|
granted,
|
|
@@ -639,12 +642,18 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
639
642
|
{ id: bundle.id, name: pkg?.name ?? bundle.id },
|
|
640
643
|
granted(),
|
|
641
644
|
)
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
645
|
+
if (!pkg || !granted().includes(USER_DELEGATE)) return itself
|
|
646
|
+
if (caller.kind === 'backoffice') return caller.principal
|
|
647
|
+
// A job or an event acts for whoever granted it; a visitor never borrows anyone.
|
|
648
|
+
const key =
|
|
649
|
+
caller.kind === 'job' ? bundleGrants.state(pkg).grant?.delegateKey : null
|
|
650
|
+
const installer = key ? await userPrincipalByKey(db, key) : undefined
|
|
651
|
+
if (key && !installer)
|
|
652
|
+
bundleLog(bundle.id).warn(
|
|
653
|
+
'It acts for whoever granted it, {key}, who is no longer a user: it acts as itself.',
|
|
654
|
+
{ key },
|
|
655
|
+
)
|
|
656
|
+
return installer ?? itself
|
|
648
657
|
},
|
|
649
658
|
})
|
|
650
659
|
},
|
|
@@ -654,14 +663,25 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
654
663
|
fetch: async (input, init) => {
|
|
655
664
|
const url = new URL(input instanceof Request ? input.url : String(input))
|
|
656
665
|
const origins = declaredOrigins(pkg)
|
|
657
|
-
|
|
666
|
+
const event = {
|
|
667
|
+
extensionId: bundle.id,
|
|
668
|
+
...personOf(_caller),
|
|
669
|
+
kind: 'request' as const,
|
|
670
|
+
area: url.host,
|
|
671
|
+
verb: (init?.method ?? 'GET').toUpperCase(),
|
|
672
|
+
}
|
|
673
|
+
if (!origins.includes(url.origin)) {
|
|
674
|
+
activity.record({ ...event, kind: 'refused', outcome: 'refused' })
|
|
658
675
|
throw new OutboundRefused(
|
|
659
676
|
`${bundle.id} may not reach ${url.origin}: its http.outbound names ${origins.join(', ') || 'nothing'}.`,
|
|
660
677
|
)
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
678
|
+
}
|
|
679
|
+
return timed(activity, event, () =>
|
|
680
|
+
pinnedFetch(input, init, {
|
|
681
|
+
allowPrivateAddresses:
|
|
682
|
+
config.webhooks?.allowPrivateAddresses ?? config.development,
|
|
683
|
+
}),
|
|
684
|
+
)
|
|
665
685
|
},
|
|
666
686
|
}
|
|
667
687
|
},
|
|
@@ -818,6 +838,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
818
838
|
nodeId: schema.nodeId,
|
|
819
839
|
versionCleanup: config.versionCleanup,
|
|
820
840
|
auditRetentionDays: config.auditRetentionDays,
|
|
841
|
+
extensionActivityDays: config.extensionActivityDays,
|
|
821
842
|
mediaFiles,
|
|
822
843
|
// The workflow queue drains here rather than in the request: a submission is
|
|
823
844
|
// stored first, and an e-mail that cannot be sent yet must not cost it.
|
|
@@ -854,6 +875,14 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
854
875
|
// Where jobs are left to a worker: whether one is running them (decision 4).
|
|
855
876
|
const workerWatch = createWorkerWatch({ db, config })
|
|
856
877
|
return {
|
|
878
|
+
/** What each extension does, recorded for the Activity view (story 327). */
|
|
879
|
+
activity,
|
|
880
|
+
/** Stops one extension's process on this node, as removing it does (story 328). */
|
|
881
|
+
async stopExtension(id: string) {
|
|
882
|
+
await Promise.all(
|
|
883
|
+
extensionProcesses.filter((process) => process.id === id).map((process) => process.stop()),
|
|
884
|
+
)
|
|
885
|
+
},
|
|
857
886
|
/** The server's own handler, which `api` calls through once the server is built (story 308). */
|
|
858
887
|
setBundleDispatch(dispatch: (request: Request) => Promise<Response>) {
|
|
859
888
|
bundleDispatch = dispatch
|
|
@@ -923,6 +952,7 @@ export async function createCore(config: CromboloConfig, hooks: CoreHooks) {
|
|
|
923
952
|
await jobRunner.stop()
|
|
924
953
|
await jobs.stop()
|
|
925
954
|
await Promise.all(extensionProcesses.map((process) => process.stop()))
|
|
955
|
+
await activity.stop()
|
|
926
956
|
},
|
|
927
957
|
/** The extensions that are granted but could not start, and why (story 311). */
|
|
928
958
|
failedExtensions,
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The extension activity recorder (story 327): every place that already knows
|
|
3
|
+
* what an extension's call is records it here, and rows are written in
|
|
4
|
+
* batches, a second apart or two hundred at a time. Reads of one kind, for one
|
|
5
|
+
* person, in one minute, fold into one row. A crash loses at most a second of
|
|
6
|
+
* activity; the audit log still has every write the API made.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
ACTIVITY_KINDS,
|
|
10
|
+
type ActivityFilter,
|
|
11
|
+
type ActivityInput,
|
|
12
|
+
type ActivityKind,
|
|
13
|
+
type ActivityOutcome,
|
|
14
|
+
type Db,
|
|
15
|
+
ExtensionActivityLog,
|
|
16
|
+
} from '@crombolo/data'
|
|
17
|
+
import type { BundleCaller } from './bundles.ts'
|
|
18
|
+
import { logger } from './logging.ts'
|
|
19
|
+
|
|
20
|
+
const log = logger('Activity')
|
|
21
|
+
|
|
22
|
+
/** Whom an extension acted for, as a row names them. */
|
|
23
|
+
export interface ActivityPerson {
|
|
24
|
+
personKey: string | null
|
|
25
|
+
personName: string | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What one call was, as a recording point knows it. */
|
|
29
|
+
export interface ActivityEvent extends ActivityPerson {
|
|
30
|
+
extensionId: string
|
|
31
|
+
kind: ActivityKind
|
|
32
|
+
area: string | null
|
|
33
|
+
verb?: string | null
|
|
34
|
+
entityKey?: string | null
|
|
35
|
+
count?: number
|
|
36
|
+
outcome: ActivityOutcome
|
|
37
|
+
ms?: number | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ActivityRecorder {
|
|
41
|
+
record(event: ActivityEvent): void
|
|
42
|
+
flush(): Promise<void>
|
|
43
|
+
stop(): Promise<void>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The person a caller is: a back-office user, a member by name, or nobody. */
|
|
47
|
+
export function personOf(caller: BundleCaller | undefined): ActivityPerson {
|
|
48
|
+
if (caller?.kind === 'backoffice')
|
|
49
|
+
return { personKey: caller.principal.id, personName: caller.principal.name }
|
|
50
|
+
if (caller?.kind === 'member')
|
|
51
|
+
return { personKey: null, personName: `member ${caller.member.username}` }
|
|
52
|
+
return { personKey: null, personName: null }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Times a call and records it, whether it answers, throws, or answers a promise. */
|
|
56
|
+
export function timed<T>(
|
|
57
|
+
recorder: ActivityRecorder | undefined,
|
|
58
|
+
event: Omit<ActivityEvent, 'outcome' | 'ms'>,
|
|
59
|
+
call: () => T,
|
|
60
|
+
outcomeOf: (error: unknown) => ActivityOutcome = () => 'failed',
|
|
61
|
+
): T {
|
|
62
|
+
if (!recorder) return call()
|
|
63
|
+
const started = performance.now()
|
|
64
|
+
const done = (outcome: ActivityOutcome) =>
|
|
65
|
+
recorder.record({ ...event, outcome, ms: performance.now() - started })
|
|
66
|
+
try {
|
|
67
|
+
const result = call()
|
|
68
|
+
if (result instanceof Promise)
|
|
69
|
+
return result.then(
|
|
70
|
+
(value) => {
|
|
71
|
+
done('ok')
|
|
72
|
+
return value
|
|
73
|
+
},
|
|
74
|
+
(error: unknown) => {
|
|
75
|
+
done(outcomeOf(error))
|
|
76
|
+
throw error
|
|
77
|
+
},
|
|
78
|
+
) as T
|
|
79
|
+
done('ok')
|
|
80
|
+
return result
|
|
81
|
+
} catch (error) {
|
|
82
|
+
done(outcomeOf(error))
|
|
83
|
+
throw error
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function createActivityRecorder(
|
|
88
|
+
/** The database, or how to reach it once it is open: extensions start before it is. */
|
|
89
|
+
db: Db | (() => Db),
|
|
90
|
+
options: { flushMs?: number; batch?: number } = {},
|
|
91
|
+
): ActivityRecorder {
|
|
92
|
+
let opened: ExtensionActivityLog | undefined
|
|
93
|
+
const store = () => (opened ??= new ExtensionActivityLog(typeof db === 'function' ? db() : db))
|
|
94
|
+
const batch = options.batch ?? 200
|
|
95
|
+
let queued: ActivityInput[] = []
|
|
96
|
+
/** This minute's reads, by extension, person, area and verb. */
|
|
97
|
+
const reads = new Map<string, ActivityInput>()
|
|
98
|
+
let writing: Promise<void> = Promise.resolve()
|
|
99
|
+
|
|
100
|
+
const minute = (at: Date) => new Date(Math.floor(at.getTime() / 60_000) * 60_000)
|
|
101
|
+
|
|
102
|
+
const flush = async () => {
|
|
103
|
+
const rows = [...queued, ...reads.values()]
|
|
104
|
+
queued = []
|
|
105
|
+
reads.clear()
|
|
106
|
+
if (rows.length === 0) return writing
|
|
107
|
+
writing = writing
|
|
108
|
+
.then(() => store().record(rows))
|
|
109
|
+
.catch((error: unknown) => {
|
|
110
|
+
log.warning('Could not keep {count} rows of extension activity: {error}', {
|
|
111
|
+
count: rows.length,
|
|
112
|
+
error,
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
return writing
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const timer = setInterval(() => void flush(), options.flushMs ?? 1000)
|
|
119
|
+
;(timer as { unref?: () => void }).unref?.()
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
record(event) {
|
|
123
|
+
const row: ActivityInput = {
|
|
124
|
+
extensionId: event.extensionId,
|
|
125
|
+
personKey: event.personKey,
|
|
126
|
+
personName: event.personName,
|
|
127
|
+
kind: event.kind,
|
|
128
|
+
area: event.area,
|
|
129
|
+
verb: event.verb ?? null,
|
|
130
|
+
entityKey: event.entityKey ?? null,
|
|
131
|
+
count: event.count ?? 1,
|
|
132
|
+
outcome: event.outcome,
|
|
133
|
+
ms: event.ms ?? null,
|
|
134
|
+
occurredAt: new Date(),
|
|
135
|
+
}
|
|
136
|
+
if (row.kind === 'read' && row.outcome === 'ok') {
|
|
137
|
+
const at = minute(row.occurredAt)
|
|
138
|
+
const key = [
|
|
139
|
+
row.extensionId,
|
|
140
|
+
row.personKey ?? '',
|
|
141
|
+
row.area ?? '',
|
|
142
|
+
row.verb ?? '',
|
|
143
|
+
at.getTime(),
|
|
144
|
+
].join('\u0000')
|
|
145
|
+
const held = reads.get(key)
|
|
146
|
+
if (held) {
|
|
147
|
+
held.count += row.count
|
|
148
|
+
held.ms = (held.ms ?? 0) + (row.ms ?? 0)
|
|
149
|
+
} else reads.set(key, { ...row, entityKey: null, occurredAt: at })
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
queued.push(row)
|
|
153
|
+
if (queued.length >= batch) void flush()
|
|
154
|
+
},
|
|
155
|
+
flush,
|
|
156
|
+
async stop() {
|
|
157
|
+
clearInterval(timer)
|
|
158
|
+
await flush()
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A page of activity as the Activity view asks for it: `before` and `limit`
|
|
165
|
+
* (1–500, default 100), and the filters `kind`, `person`, `since` and `until`.
|
|
166
|
+
* Answers the reason when the query is not one.
|
|
167
|
+
*/
|
|
168
|
+
export function readActivityQuery(
|
|
169
|
+
search: URLSearchParams,
|
|
170
|
+
): { filter: ActivityFilter; cursor: { before?: number; limit: number } } | string {
|
|
171
|
+
const limit = Number(search.get('limit') ?? 100)
|
|
172
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 500)
|
|
173
|
+
return 'limit must be between 1 and 500.'
|
|
174
|
+
const raw = search.get('before')
|
|
175
|
+
const before = raw === null ? undefined : Number(raw)
|
|
176
|
+
if (before !== undefined && (!Number.isInteger(before) || before < 0))
|
|
177
|
+
return 'before must be an entry id.'
|
|
178
|
+
const kind = search.get('kind') ?? undefined
|
|
179
|
+
if (kind && !ACTIVITY_KINDS.includes(kind as ActivityKind)) return `There is no kind "${kind}".`
|
|
180
|
+
const date = (name: string) => {
|
|
181
|
+
const value = search.get(name)
|
|
182
|
+
if (!value) return undefined
|
|
183
|
+
const parsed = new Date(value)
|
|
184
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed
|
|
185
|
+
}
|
|
186
|
+
const since = date('since')
|
|
187
|
+
const until = date('until')
|
|
188
|
+
if (since === null || until === null) return 'since and until must be dates.'
|
|
189
|
+
return {
|
|
190
|
+
filter: {
|
|
191
|
+
...(kind ? { kind: kind as ActivityKind } : {}),
|
|
192
|
+
...(search.get('person') ? { person: search.get('person') as string } : {}),
|
|
193
|
+
...(since ? { since } : {}),
|
|
194
|
+
...(until ? { until } : {}),
|
|
195
|
+
},
|
|
196
|
+
cursor: { ...(before !== undefined ? { before } : {}), limit },
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const csvCell = (value: unknown): string => {
|
|
201
|
+
const text = value === null || value === undefined ? '' : String(value)
|
|
202
|
+
// A cell a spreadsheet would read as a formula is kept as text.
|
|
203
|
+
const safe = /^[=+\-@\t\r]/.test(text) ? `'${text}` : text
|
|
204
|
+
return /[",\n]/.test(safe) ? `"${safe.replaceAll('"', '""')}"` : safe
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** All of an extension's activity the filter matches, as a CSV file, newest first. */
|
|
208
|
+
export async function activityCsv(
|
|
209
|
+
store: ExtensionActivityLog,
|
|
210
|
+
extensionId: string,
|
|
211
|
+
filter: ActivityFilter,
|
|
212
|
+
): Promise<string> {
|
|
213
|
+
const lines = ['when,person,kind,area,verb,item,count,outcome,ms']
|
|
214
|
+
let before: number | undefined
|
|
215
|
+
for (;;) {
|
|
216
|
+
const page = await store.page(extensionId, filter, { before, limit: 500 })
|
|
217
|
+
for (const row of page.items)
|
|
218
|
+
lines.push(
|
|
219
|
+
[
|
|
220
|
+
row.occurredAt.toISOString(),
|
|
221
|
+
row.personName ?? 'itself',
|
|
222
|
+
row.kind,
|
|
223
|
+
row.area,
|
|
224
|
+
row.verb,
|
|
225
|
+
row.entityKey,
|
|
226
|
+
row.count,
|
|
227
|
+
row.outcome,
|
|
228
|
+
row.ms,
|
|
229
|
+
]
|
|
230
|
+
.map(csvCell)
|
|
231
|
+
.join(','),
|
|
232
|
+
)
|
|
233
|
+
if (page.next === null) break
|
|
234
|
+
before = page.next
|
|
235
|
+
}
|
|
236
|
+
return `${lines.join('\n')}\n`
|
|
237
|
+
}
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
PublishedContent,
|
|
11
11
|
PublishedElement,
|
|
12
12
|
RawHtml,
|
|
13
|
+
sanitiseHtml,
|
|
13
14
|
} from '@crombolo/render'
|
|
14
15
|
import type { BundleLog } from './bundles.ts'
|
|
15
16
|
import type { ExtensionProcess } from './extension-process.ts'
|
|
@@ -71,6 +72,8 @@ export function createExtensionComponents(
|
|
|
71
72
|
): ExtensionComponents {
|
|
72
73
|
const byId = new Map(sources.map((source) => [source.process.id, source]))
|
|
73
74
|
const warned = new Set<string>()
|
|
75
|
+
/** Components whose removals were logged this boot, by extension and name. */
|
|
76
|
+
const stripped = new Set<string>()
|
|
74
77
|
const offers = (source: ExtensionComponentSource, name: string) =>
|
|
75
78
|
source.process.description?.components?.some((component) => component.name === name) ?? false
|
|
76
79
|
|
|
@@ -102,7 +105,20 @@ export function createExtensionComponents(
|
|
|
102
105
|
{ ...rest, readSearchParams: () => searchParams },
|
|
103
106
|
{ timeoutMs: options.timeoutMs ?? 10_000, extra: valuesSender() },
|
|
104
107
|
)
|
|
105
|
-
|
|
108
|
+
// Story 330: what can run script, by the Sanitizer API's safe baseline, never reaches the page.
|
|
109
|
+
const removed: string[] = []
|
|
110
|
+
const safe = sanitiseHtml(typeof value === 'string' ? value : '', (_kind, what) => {
|
|
111
|
+
removed.push(what)
|
|
112
|
+
})
|
|
113
|
+
const key = `${id}/${name}`
|
|
114
|
+
if (removed.length > 0 && !stripped.has(key)) {
|
|
115
|
+
stripped.add(key)
|
|
116
|
+
source.log.warn(
|
|
117
|
+
"The extension {id}'s component {name} carried {count} thing(s) a page may not: {names}. They were removed.",
|
|
118
|
+
{ id, name, count: removed.length, names: [...new Set(removed)].join(', ') },
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
return new RawHtml(safe)
|
|
106
122
|
} catch (error) {
|
|
107
123
|
source.log.error('The extension {id} could not render {name}: {error}', {
|
|
108
124
|
id,
|
|
@@ -8,7 +8,12 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { join } from 'node:path'
|
|
10
10
|
import type { BundlePackage } from '@crombolo/backoffice-host'
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type ExtensionSetting,
|
|
13
|
+
INSTALLED_AS_DEPENDENCY,
|
|
14
|
+
readSettings,
|
|
15
|
+
USER_DELEGATE,
|
|
16
|
+
} from '@crombolo/backoffice-host/registry'
|
|
12
17
|
import {
|
|
13
18
|
type BundleGrant,
|
|
14
19
|
BundleGrantRepository,
|
|
@@ -18,6 +23,7 @@ import {
|
|
|
18
23
|
import { declaredOrigins, declaredPermissions, grantGaps } from './bundle-grants.ts'
|
|
19
24
|
import type { BundleLog, ServerBundle } from './bundles.ts'
|
|
20
25
|
import type { CromboloConfig, ExtensionEntry } from './config.ts'
|
|
26
|
+
import type { ActivityRecorder } from './extension-activity.ts'
|
|
21
27
|
import { ExtensionProcess, isolatedBundle, isolationProblems } from './extension-process.ts'
|
|
22
28
|
import { openSecret } from './secrets.ts'
|
|
23
29
|
|
|
@@ -52,8 +58,7 @@ export function entryCovers(pkg: BundlePackage, entry: ExtensionEntry | undefine
|
|
|
52
58
|
const granted = new Set(entry.grant)
|
|
53
59
|
if (!declaredPermissions(pkg).every((permission) => granted.has(permission))) return false
|
|
54
60
|
const origins = new Set(entry.origins ?? [])
|
|
55
|
-
|
|
56
|
-
return !declaredPermissions(pkg).includes('actAs') || Boolean(entry.actAs)
|
|
61
|
+
return declaredOrigins(pkg).every((origin) => origins.has(origin))
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
/** Its settings: the manifest's defaults, then what the back office set, then the config's. */
|
|
@@ -95,6 +100,12 @@ export function extensionEntryProblems(
|
|
|
95
100
|
(id) =>
|
|
96
101
|
`The config's extensions names "${id}", which no extension the site depends on is: add it to package.json, or take it out.`,
|
|
97
102
|
)
|
|
103
|
+
// Acting for the person needs a person to have granted it, which a config has not (story 325).
|
|
104
|
+
for (const [id, entry] of Object.entries(entries ?? {}))
|
|
105
|
+
if (entry.grant?.includes(USER_DELEGATE))
|
|
106
|
+
problems.push(
|
|
107
|
+
`The config's extensions grants "${id}" ${USER_DELEGATE}, which only an administrator who is signed in may grant, so a job or an event has someone to act for: take it out of the config, and grant it in Settings, under Extensions, or with \`crombolo extension grant ${id} --delegate-to <user key>\`.`,
|
|
108
|
+
)
|
|
98
109
|
if (Bun.env.NODE_ENV === 'production')
|
|
99
110
|
for (const [id, entry] of Object.entries(entries ?? {}))
|
|
100
111
|
if (entry.dev)
|
|
@@ -104,6 +115,10 @@ export function extensionEntryProblems(
|
|
|
104
115
|
return problems
|
|
105
116
|
}
|
|
106
117
|
|
|
118
|
+
/** Why a linked extension does not run in production (story 326). */
|
|
119
|
+
export const LINKED_IN_PRODUCTION =
|
|
120
|
+
"it is linked from elsewhere on this machine, which is for a developer's machine only: install it with `crombolo extension install` so it has a root of its own"
|
|
121
|
+
|
|
107
122
|
/** An extension that could not start, and why: shown where it is listed. */
|
|
108
123
|
export interface FailedExtension {
|
|
109
124
|
id: string
|
|
@@ -120,6 +135,7 @@ export async function startExtensions(
|
|
|
120
135
|
packages: readonly BundlePackage[],
|
|
121
136
|
recorded: RecordedExtensions,
|
|
122
137
|
log: (id: string) => BundleLog,
|
|
138
|
+
activity?: ActivityRecorder,
|
|
123
139
|
): Promise<{
|
|
124
140
|
bundles: ServerBundle[]
|
|
125
141
|
processes: ExtensionProcess[]
|
|
@@ -131,6 +147,23 @@ export async function startExtensions(
|
|
|
131
147
|
const failed: FailedExtension[] = []
|
|
132
148
|
for (const pkg of packages) {
|
|
133
149
|
if (!pkg.carries.server || wired.has(pkg.id)) continue
|
|
150
|
+
// Story 326: an extension runs from a root of its own, never as a dependency of the site.
|
|
151
|
+
if (pkg.linked && Bun.env.NODE_ENV === 'production') {
|
|
152
|
+
failed.push({ id: pkg.id, reason: LINKED_IN_PRODUCTION })
|
|
153
|
+
log(pkg.id).error('{package} is not started: {reason}', {
|
|
154
|
+
package: pkg.packageName,
|
|
155
|
+
reason: LINKED_IN_PRODUCTION,
|
|
156
|
+
})
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
if (pkg.dependency) {
|
|
160
|
+
failed.push({ id: pkg.id, reason: INSTALLED_AS_DEPENDENCY })
|
|
161
|
+
log(pkg.id).error('{package} is not started: {reason}', {
|
|
162
|
+
package: pkg.packageName,
|
|
163
|
+
reason: INSTALLED_AS_DEPENDENCY,
|
|
164
|
+
})
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
134
167
|
const entry = config.extensions?.[pkg.id]
|
|
135
168
|
const grant = recorded.grants.get(pkg.id)
|
|
136
169
|
const byConfig = entryCovers(pkg, entry)
|
|
@@ -152,6 +185,7 @@ export async function startExtensions(
|
|
|
152
185
|
reads: entry?.dev?.allowRead,
|
|
153
186
|
timeoutMs: entry?.timeoutMs,
|
|
154
187
|
log: log(pkg.id),
|
|
188
|
+
activity,
|
|
155
189
|
})
|
|
156
190
|
try {
|
|
157
191
|
const description = await process.start()
|