@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/src/server.ts CHANGED
@@ -85,6 +85,7 @@ import {
85
85
  import {
86
86
  appendCacheInstruction,
87
87
  type Db,
88
+ ExtensionActivityLog,
88
89
  ensureFormLibraryTypes,
89
90
  ensureKeyValue,
90
91
  FormEntryRepository,
@@ -168,7 +169,7 @@ import { createPublishedContentSource, loadDraftNode } from './adapters/document
168
169
  import type { Approvals } from './approvals.ts'
169
170
  import { handleApprovalsApi } from './approvals-api.ts'
170
171
  import { type BundleActor, createBundleAdmin, settingsIn } from './bundle-admin.ts'
171
- import { actAsChoice, actingPrincipalOf } from './bundle-api.ts'
172
+ import { actingPrincipalOf } from './bundle-api.ts'
172
173
  import { type BundleGrants, declaredOrigins } from './bundle-grants.ts'
173
174
  import { CommandError, installBundleContent } from './bundle-install.ts'
174
175
  import { BUNDLES_CHANGED_EVENT, type BundleReload, createBundleReloader } from './bundle-reload.ts'
@@ -194,6 +195,7 @@ import { syncDomainsFile } from './domains.ts'
194
195
  import { editorTypeLibs, siteJsxImportSource } from './editor-types.ts'
195
196
  import { emailAvailability } from './email.ts'
196
197
  import { noticeAboutEmail } from './email-notice.ts'
198
+ import { activityCsv, readActivityQuery } from './extension-activity.ts'
197
199
  import { createBackOfficeBuilds } from './extension-backoffice.ts'
198
200
  import { warnAboutAppPlugins } from './extensions-notice.ts'
199
201
  import { coerceForm, deleteForm, saveForm } from './form-files.ts'
@@ -214,6 +216,7 @@ import {
214
216
  parseEntryState,
215
217
  redactSensitive,
216
218
  } from './forms.ts'
219
+ import { mayGrant } from './grant-policy.ts'
217
220
  import { HOST_TOKEN_SECONDS, issueHostToken } from './host-token.ts'
218
221
  import { canEncodeAvif, ImageProcessor, parseImagingQuery } from './imaging.ts'
219
222
  import type { BackgroundJobs } from './jobs.ts'
@@ -459,7 +462,13 @@ export async function createServer(
459
462
  } = core
460
463
  /** The extensions that are on: granted, and not turned off (story 309). */
461
464
  const onExtensions = () =>
462
- extensions.list().filter((extension) => bundleGrants.isOn(extension.bundle.id))
465
+ extensions.list().filter(
466
+ (extension) =>
467
+ bundleGrants.isOn(extension.bundle.id) &&
468
+ // Story 326: one installed as a dependency shows only when the site's code wires it.
469
+ (!extension.bundle.dependency ||
470
+ config.bundles.some((bundle) => bundle.id === extension.bundle.id)),
471
+ )
463
472
  // Their back-office code, built when first asked for (story 320).
464
473
  const backOfficeBuilds = createBackOfficeBuilds(config.siteDir, (id) => {
465
474
  const named = logger('bundle', id)
@@ -495,6 +504,7 @@ export async function createServer(
495
504
  contentApplied: async (packageName) =>
496
505
  (await new TransferRunRepository(db).appliedForPackage(packageName))?.packageVersion ?? null,
497
506
  applyContentOnInstall: config.applyBundleContentOnInstall,
507
+ unrestrictedFrom: config.unrestrictedExtensions,
498
508
  installs: config.runtimeInstalls,
499
509
  installsElsewhere: config.installsElsewhere,
500
510
  policy: config.bundlePolicy,
@@ -846,11 +856,13 @@ export async function createServer(
846
856
  extensions,
847
857
  bundleGrants,
848
858
  failed: () => [...core.failedExtensions, ...backOfficeBuilds.failed()],
859
+ stopExtension: (id) => core.stopExtension(id),
849
860
  })
850
861
  const actorOf = (principal: Principal, request: Request): BundleActor => ({
851
862
  id: principal.id,
852
863
  name: principal.name,
853
864
  email: principal.email,
865
+ isAdmin: principal.isAdmin,
854
866
  source: 'request',
855
867
  ...clientAddress(request),
856
868
  })
@@ -2142,6 +2154,7 @@ export async function createServer(
2142
2154
  url: marketplace.url(),
2143
2155
  keyword: config.packageKeyword,
2144
2156
  appliesContent: config.applyBundleContentOnInstall,
2157
+ mayGrant: mayGrant(principal.isAdmin, config.unrestrictedExtensions),
2145
2158
  installs: config.runtimeInstalls,
2146
2159
  installsElsewhere: config.runtimeInstalls ? null : (config.installsElsewhere ?? null),
2147
2160
  items: await marketplace.search(url.searchParams.get('q') ?? undefined),
@@ -2203,8 +2216,56 @@ export async function createServer(
2203
2216
  }
2204
2217
  }
2205
2218
 
2219
+ // Story 328: the data a removed extension left, deleted on its own, an administrator's.
2220
+ if (action === 'drop-left-data' && request.method === 'POST') {
2221
+ if (!principal.isAdmin)
2222
+ return Response.json(
2223
+ { ok: false, message: 'An administrator deletes what an extension left.' },
2224
+ { status: 403 },
2225
+ )
2226
+ const body = (await request.json().catch(() => ({}))) as { id?: unknown }
2227
+ const { status, ...outcome } = await bundleAdmin.dropLeftData(
2228
+ actorOf(principal, request),
2229
+ typeof body.id === 'string' ? body.id : '',
2230
+ )
2231
+ return Response.json(outcome, { status })
2232
+ }
2233
+
2234
+ // Story 327: what one extension did, newest first, its counts, and the same as a file.
2235
+ const watching = /^installed\/([a-z0-9][a-z0-9-]*)\/activity(\/summary|\.csv)?$/.exec(action)
2236
+ if (watching && request.method === 'GET') {
2237
+ const [, id, part] = watching as unknown as [string, string, string | undefined]
2238
+ const read = readActivityQuery(url.searchParams)
2239
+ if (typeof read === 'string')
2240
+ return Response.json(
2241
+ { title: 'Invalid request', status: 400, detail: read },
2242
+ { status: 400 },
2243
+ )
2244
+ await core.activity.flush()
2245
+ const store = new ExtensionActivityLog(db)
2246
+ if (part === '/summary')
2247
+ return Response.json({
2248
+ counts: await store.counts(
2249
+ id,
2250
+ new Date(Date.now() - config.extensionActivityDays * 86_400_000),
2251
+ ),
2252
+ people: await store.people(id),
2253
+ days: config.extensionActivityDays,
2254
+ })
2255
+ if (part === '.csv')
2256
+ return new Response(await activityCsv(store, id, read.filter), {
2257
+ headers: {
2258
+ 'content-type': 'text/csv; charset=utf-8',
2259
+ 'content-disposition': `attachment; filename="${id}-activity.csv"`,
2260
+ },
2261
+ })
2262
+ return Response.json(await store.page(id, read.filter, read.cursor))
2263
+ }
2264
+
2265
+ // What this person may grant at each level here (story 324): the panel shows the button, the sentence or the snippet.
2266
+ const granting = mayGrant(principal.isAdmin, config.unrestrictedExtensions)
2206
2267
  if (action === 'installed' && request.method === 'GET')
2207
- return Response.json(await bundleAdmin.installed())
2268
+ return Response.json({ ...(await bundleAdmin.installed()), mayGrant: granting })
2208
2269
 
2209
2270
  // Where a version was built, for the Install panel before anything is
2210
2271
  // installed (story 060), and whether this site would take it.
@@ -2223,13 +2284,20 @@ export async function createServer(
2223
2284
  }
2224
2285
 
2225
2286
  if ((action === 'install' || action === 'uninstall') && request.method === 'POST') {
2287
+ // Installing or removing code is an administrator's, as granting it is (story 324).
2288
+ if (!principal.isAdmin)
2289
+ return Response.json(
2290
+ { ok: false, message: 'An administrator installs and removes extensions.' },
2291
+ { status: 403 },
2292
+ )
2226
2293
  const body = (await request.json().catch(() => ({}))) as {
2227
2294
  name?: unknown
2228
2295
  version?: unknown
2229
2296
  applyContent?: unknown
2230
2297
  grant?: unknown
2231
- actAs?: unknown
2232
2298
  settings?: unknown
2299
+ dropData?: unknown
2300
+ dropContent?: unknown
2233
2301
  }
2234
2302
  const name = typeof body.name === 'string' ? body.name : ''
2235
2303
  const actor = actorOf(principal, request)
@@ -2239,13 +2307,16 @@ export async function createServer(
2239
2307
  name,
2240
2308
  version: typeof body.version === 'string' ? body.version : undefined,
2241
2309
  applyContent: body.applyContent !== false,
2242
- actAs: actAsChoice(body.actAs, principal.id),
2243
2310
  grant: Array.isArray(body.grant)
2244
2311
  ? body.grant.filter((p): p is string => typeof p === 'string')
2245
2312
  : [],
2246
2313
  settings: settingsIn(body.settings),
2247
2314
  })
2248
- : await bundleAdmin.uninstall(actor, name)
2315
+ : // Story 328: its data and the content it added are kept unless asked.
2316
+ await bundleAdmin.uninstall(actor, name, {
2317
+ dropData: body.dropData === true,
2318
+ dropContent: body.dropContent === true,
2319
+ })
2249
2320
  log.info('{user} {action} the package {name}: {outcome}', {
2250
2321
  user: principal.email,
2251
2322
  action: action === 'install' ? 'installed' : 'removed',
@@ -2314,17 +2385,12 @@ export async function createServer(
2314
2385
  : Response.json({ ok: false, message: outcome.message }, { status: outcome.status })
2315
2386
  }
2316
2387
 
2317
- // Story 309: granting an installed extension what it asks for, administrators only.
2388
+ // Story 309: granting an installed extension what it asks for, administrators only: the
2389
+ // admin's one refusal says so, for every level and every way in (story 324).
2318
2390
  if (action === 'grant' && request.method === 'POST') {
2319
- if (!principal.isAdmin)
2320
- return Response.json(
2321
- { ok: false, message: 'Only an administrator grants an extension.' },
2322
- { status: 403 },
2323
- )
2324
2391
  const body = (await request.json().catch(() => ({}))) as {
2325
2392
  id?: unknown
2326
2393
  grant?: unknown
2327
- actAs?: unknown
2328
2394
  }
2329
2395
  const { status, ...outcome } = await bundleAdmin.grant(
2330
2396
  actorOf(principal, request),
@@ -2333,7 +2399,6 @@ export async function createServer(
2333
2399
  grant: Array.isArray(body.grant)
2334
2400
  ? body.grant.filter((p): p is string => typeof p === 'string')
2335
2401
  : [],
2336
- actAs: actAsChoice(body.actAs, principal.id),
2337
2402
  },
2338
2403
  )
2339
2404
  return Response.json(outcome, { status })
package/src/settings.ts CHANGED
@@ -204,6 +204,14 @@ const DECLARED = [
204
204
  section: 'Database',
205
205
  description: 'How many days the audit log keeps an entry. `0` keeps them all.',
206
206
  },
207
+ {
208
+ env: 'CROMBOLO_EXTENSION_ACTIVITY_DAYS',
209
+ kind: 'number',
210
+ default: '14',
211
+ section: 'Database',
212
+ description:
213
+ 'How many days what each extension did is kept for its Activity view in Settings, under Extensions. `0` keeps nothing.',
214
+ },
207
215
  {
208
216
  env: 'CROMBOLO_EVENT_RETENTION_DAYS',
209
217
  kind: 'number',
@@ -347,6 +355,15 @@ const DECLARED = [
347
355
  description:
348
356
  'Where a signed bundle has to come from, separated by commas: `org/*`, `org/repo`, or `org/repo@v*` for its tags. Empty takes signed extensions from anywhere. Each is also a trusted CI build for site bundles, locked on Settings → Trusted signers.',
349
357
  },
358
+ {
359
+ env: 'CROMBOLO_UNRESTRICTED_EXTENSIONS',
360
+ kind: 'enum',
361
+ values: ['backoffice', 'config'],
362
+ default: 'backoffice',
363
+ section: 'Back office and bundles',
364
+ description:
365
+ "Where an Unrestricted extension, one whose own code runs in the back office, may be turned on: by an administrator in Settings, or only in the site's configuration, so a commit someone reviews is the only way.",
366
+ },
350
367
  {
351
368
  env: 'CROMBOLO_INSTALLS_ELSEWHERE_MESSAGE',
352
369
  kind: 'string',
package/src/site-file.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * failed export never leaves a file that looks whole.
11
11
  */
12
12
  import { rename, rm } from 'node:fs/promises'
13
- import { discoverBundles } from '@crombolo/backoffice-host/registry'
13
+ import { discoverBundles, readRecord } from '@crombolo/backoffice-host/registry'
14
14
  import { type Db, NodeRepository, type NodeRow, ObjectTypes } from '@crombolo/data'
15
15
  import {
16
16
  BUNDLE_SECTIONS,
@@ -125,11 +125,14 @@ export function unknownHostProvided(
125
125
  }
126
126
 
127
127
  function requiredPackages(config: CromboloConfig): Record<string, string> {
128
- if (config.bundles.length === 0) return {}
129
128
  const wired = new Set(config.bundles.map((bundle) => bundle.id))
130
129
  // The installation's own packages are not the site's to need (story 177).
131
130
  const hosts = new Set(config.hostProvided)
132
131
  const required: Record<string, string> = {}
132
+ // Story 326: what the record says is installed, each in a root of its own.
133
+ for (const line of Object.values(readRecord(config.siteDir)))
134
+ if (!hosts.has(line.name)) required[line.name] = line.version
135
+ if (config.bundles.length === 0) return required
133
136
  for (const found of discoverBundles(config.siteDir).bundles)
134
137
  if (wired.has(found.id) && !hosts.has(found.packageName))
135
138
  required[found.packageName] = found.version