@crombolo/server 0.37.0 → 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 -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 +265 -0
- package/src/bundle-install.ts +13 -2
- package/src/bundle-media.ts +12 -11
- package/src/bundles.ts +126 -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 +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 +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/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 +333 -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
package/src/imaging.ts
CHANGED
|
@@ -216,7 +216,7 @@ export function padPixels(
|
|
|
216
216
|
// ------------------------------------------------------------ the request
|
|
217
217
|
|
|
218
218
|
export type ResizeMode = 'crop' | 'max' | 'stretch' | 'pad' | 'boxpad' | 'min'
|
|
219
|
-
export type OutputFormat = 'jpg' | 'png' | 'webp'
|
|
219
|
+
export type OutputFormat = 'jpg' | 'png' | 'webp' | 'avif'
|
|
220
220
|
|
|
221
221
|
export interface ImagingRequest {
|
|
222
222
|
width?: number
|
|
@@ -231,6 +231,20 @@ export interface ImagingRequest {
|
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
const MODES: readonly ResizeMode[] = ['crop', 'max', 'stretch', 'pad', 'boxpad', 'min']
|
|
234
|
+
const FORMATS: readonly OutputFormat[] = ['jpg', 'png', 'webp', 'avif']
|
|
235
|
+
|
|
236
|
+
/** ImageSharp's `ranchor`, as the focal point a crop is cut around. */
|
|
237
|
+
const ANCHORS: Record<string, { left: number; top: number }> = {
|
|
238
|
+
center: { left: 0.5, top: 0.5 },
|
|
239
|
+
top: { left: 0.5, top: 0 },
|
|
240
|
+
bottom: { left: 0.5, top: 1 },
|
|
241
|
+
left: { left: 0, top: 0.5 },
|
|
242
|
+
right: { left: 1, top: 0.5 },
|
|
243
|
+
topleft: { left: 0, top: 0 },
|
|
244
|
+
topright: { left: 1, top: 0 },
|
|
245
|
+
bottomleft: { left: 0, top: 1 },
|
|
246
|
+
bottomright: { left: 1, top: 1 },
|
|
247
|
+
}
|
|
234
248
|
const MAX_SIDE = 5000
|
|
235
249
|
|
|
236
250
|
/** The imaging request a query asks for, or undefined when it asks for the original. */
|
|
@@ -253,11 +267,13 @@ export function parseImagingQuery(params: URLSearchParams): ImagingRequest | und
|
|
|
253
267
|
const quality = size('quality')
|
|
254
268
|
if (!width && !height && !cc && !format) return undefined
|
|
255
269
|
const mode = (params.get('rmode') ?? '').toLowerCase() as ResizeMode
|
|
270
|
+
// A focal point wins over an anchor, as it does in ImageSharp.
|
|
271
|
+
const anchor = ANCHORS[(params.get('ranchor') ?? '').toLowerCase()]
|
|
256
272
|
return {
|
|
257
273
|
width,
|
|
258
274
|
height,
|
|
259
275
|
mode: MODES.includes(mode) ? mode : 'crop',
|
|
260
|
-
focalPoint: rxy ? { left: rxy[0] as number, top: rxy[1] as number } :
|
|
276
|
+
focalPoint: rxy ? { left: rxy[0] as number, top: rxy[1] as number } : anchor,
|
|
261
277
|
crop: cc
|
|
262
278
|
? {
|
|
263
279
|
left: cc[0] as number,
|
|
@@ -266,11 +282,44 @@ export function parseImagingQuery(params: URLSearchParams): ImagingRequest | und
|
|
|
266
282
|
bottom: cc[3] as number,
|
|
267
283
|
}
|
|
268
284
|
: undefined,
|
|
269
|
-
format:
|
|
285
|
+
format: FORMATS.includes(format as OutputFormat) ? (format as OutputFormat) : undefined,
|
|
270
286
|
quality: quality ? Math.min(quality, 100) : undefined,
|
|
271
287
|
}
|
|
272
288
|
}
|
|
273
289
|
|
|
290
|
+
let avif: Promise<boolean> | undefined
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Whether this machine can encode AVIF. Bun uses the operating system's codec:
|
|
294
|
+
* macOS has one, the usual Linux image has no AV1 encoder. Asked once.
|
|
295
|
+
*/
|
|
296
|
+
export function canEncodeAvif(): Promise<boolean> {
|
|
297
|
+
avif ??= new Bun.Image(encodePng({ width: 1, height: 1, data: new Uint8Array(4) }))
|
|
298
|
+
.avif()
|
|
299
|
+
.bytes()
|
|
300
|
+
.then(
|
|
301
|
+
() => true,
|
|
302
|
+
() => false,
|
|
303
|
+
)
|
|
304
|
+
return avif
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** The format written for a request: AVIF only where it can be, WebP where not. */
|
|
308
|
+
export async function outputFormat(
|
|
309
|
+
requested: OutputFormat | undefined,
|
|
310
|
+
source: string,
|
|
311
|
+
): Promise<OutputFormat> {
|
|
312
|
+
const format = requested ?? formatOf(source)
|
|
313
|
+
return format === 'avif' && !(await canEncodeAvif()) ? 'webp' : format
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const CONTENT_TYPES: Record<OutputFormat, string> = {
|
|
317
|
+
jpg: 'image/jpeg',
|
|
318
|
+
png: 'image/png',
|
|
319
|
+
webp: 'image/webp',
|
|
320
|
+
avif: 'image/avif',
|
|
321
|
+
}
|
|
322
|
+
|
|
274
323
|
const formatOf = (source: string): OutputFormat => {
|
|
275
324
|
const extension = source.split('.').pop()?.toLowerCase()
|
|
276
325
|
return extension === 'png' || extension === 'gif' ? 'png' : extension === 'webp' ? 'webp' : 'jpg'
|
|
@@ -297,7 +346,7 @@ export async function processImage(
|
|
|
297
346
|
request: ImagingRequest,
|
|
298
347
|
sourceName: string,
|
|
299
348
|
): Promise<{ bytes: Uint8Array; contentType: string }> {
|
|
300
|
-
const format = request.format
|
|
349
|
+
const format = await outputFormat(request.format, sourceName)
|
|
301
350
|
let pixels: Pixels | undefined
|
|
302
351
|
const decode = async () => {
|
|
303
352
|
pixels ??= decodePng(await new Bun.Image(bytes).png().bytes())
|
|
@@ -374,16 +423,16 @@ export async function processImage(
|
|
|
374
423
|
let image = new Bun.Image(pixels ? encodePng(pixels) : bytes)
|
|
375
424
|
if (resize) image = image.resize(resize.width, resize.height, { fit: resize.fit })
|
|
376
425
|
const quality = request.quality
|
|
426
|
+
const options = quality ? { quality } : undefined
|
|
377
427
|
const encoded =
|
|
378
428
|
format === 'png'
|
|
379
429
|
? image.png()
|
|
380
430
|
: format === 'webp'
|
|
381
|
-
? image.webp(
|
|
382
|
-
:
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
}
|
|
431
|
+
? image.webp(options)
|
|
432
|
+
: format === 'avif'
|
|
433
|
+
? image.avif(options)
|
|
434
|
+
: image.jpeg(options)
|
|
435
|
+
return { bytes: await encoded.bytes(), contentType: CONTENT_TYPES[format] }
|
|
387
436
|
}
|
|
388
437
|
|
|
389
438
|
/**
|
|
@@ -405,16 +454,17 @@ export class ImageProcessor {
|
|
|
405
454
|
source: StoredFile,
|
|
406
455
|
request: ImagingRequest,
|
|
407
456
|
): Promise<{ key: string; bytes: Uint8Array; contentType: string }> {
|
|
408
|
-
|
|
457
|
+
// Keyed on what is written, so a node without AVIF never serves another's AVIF as WebP.
|
|
458
|
+
const format = await outputFormat(request.format, source.key)
|
|
459
|
+
const written = { ...request, format }
|
|
409
460
|
const hash = new Bun.CryptoHasher('sha1')
|
|
410
|
-
.update(`${source.key}|${source.etag}|${JSON.stringify(
|
|
461
|
+
.update(`${source.key}|${source.etag}|${JSON.stringify(written)}`)
|
|
411
462
|
.digest('hex')
|
|
412
463
|
const key = `.cache/${hash.slice(0, 2)}/${hash}.${format}`
|
|
413
|
-
const contentType =
|
|
414
|
-
format === 'png' ? 'image/png' : format === 'webp' ? 'image/webp' : 'image/jpeg'
|
|
464
|
+
const contentType = CONTENT_TYPES[format]
|
|
415
465
|
const cached = await this.#store.get(key)
|
|
416
466
|
if (cached) return { key, bytes: await cached.bytes(), contentType }
|
|
417
|
-
const result = await processImage(await source.bytes(),
|
|
467
|
+
const result = await processImage(await source.bytes(), written, source.key)
|
|
418
468
|
await this.#store.put(key, result.bytes, { contentType: result.contentType })
|
|
419
469
|
return { key, bytes: result.bytes, contentType: result.contentType }
|
|
420
470
|
}
|
package/src/index.ts
CHANGED
|
@@ -20,11 +20,11 @@ export * from './backup.ts'
|
|
|
20
20
|
export * from './boot-failure.ts'
|
|
21
21
|
export * from './bundle-build.ts'
|
|
22
22
|
export * from './bundle-code.ts'
|
|
23
|
+
export * from './bundle-grants.ts'
|
|
23
24
|
export * from './bundle-install.ts'
|
|
24
25
|
export * from './bundle-media.ts'
|
|
25
26
|
export * from './bundle-reload.ts'
|
|
26
27
|
export * from './bundle-sections.ts'
|
|
27
|
-
export * from './bundle-tables.ts'
|
|
28
28
|
export * from './bundles.ts'
|
|
29
29
|
export * from './busy.ts'
|
|
30
30
|
export * from './components.ts'
|
|
@@ -35,6 +35,8 @@ export * from './content-notifications.ts'
|
|
|
35
35
|
export * from './content-operations.ts'
|
|
36
36
|
export * from './content-query.ts'
|
|
37
37
|
export * from './content-type-udt.ts'
|
|
38
|
+
export * from './cron.ts'
|
|
39
|
+
export * from './custom-tables.ts'
|
|
38
40
|
export * from './database.ts'
|
|
39
41
|
export * from './diagnostics.ts'
|
|
40
42
|
export * from './dictionary-transfer.ts'
|
|
@@ -64,8 +66,10 @@ export * from './identity-providers.ts'
|
|
|
64
66
|
export * from './image-metadata.ts'
|
|
65
67
|
export * from './imaging.ts'
|
|
66
68
|
export * from './islands.ts'
|
|
69
|
+
export * from './job-wake.ts'
|
|
67
70
|
export * from './jobs.ts'
|
|
68
71
|
export * from './listen.ts'
|
|
72
|
+
export * from './live-reload.ts'
|
|
69
73
|
export * from './log-expressions.ts'
|
|
70
74
|
export * from './log-viewer.ts'
|
|
71
75
|
export * from './logging.ts'
|
|
@@ -80,6 +84,7 @@ export * from './middleware.ts'
|
|
|
80
84
|
export * from './models-builder.ts'
|
|
81
85
|
export * from './no-nodes.ts'
|
|
82
86
|
export * from './oembed.ts'
|
|
87
|
+
export * from './owner-jobs.ts'
|
|
83
88
|
export * from './ports.ts'
|
|
84
89
|
export * from './rate-limit.ts'
|
|
85
90
|
export * from './redirects.ts'
|
|
@@ -96,7 +101,9 @@ export * from './server.ts'
|
|
|
96
101
|
export * from './server-events.ts'
|
|
97
102
|
export * from './settings.ts'
|
|
98
103
|
export * from './setup.ts'
|
|
104
|
+
export * from './site-data.ts'
|
|
99
105
|
export * from './site-file.ts'
|
|
106
|
+
export * from './site-queries.ts'
|
|
100
107
|
export * from './site-replace.ts'
|
|
101
108
|
export * from './site-sections.ts'
|
|
102
109
|
export * from './status.ts'
|
|
@@ -110,3 +117,5 @@ export * from './url-settings.ts'
|
|
|
110
117
|
export * from './view-runtime.ts'
|
|
111
118
|
export * from './webhook-files.ts'
|
|
112
119
|
export * from './webhooks.ts'
|
|
120
|
+
export * from './worker.ts'
|
|
121
|
+
export * from './worker-watch.ts'
|
package/src/job-wake.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the background jobs run (story 223): on an owning node unless it is
|
|
3
|
+
* told to leave them (`CROMBOLO_JOBS=off`), and on a worker always. A node that
|
|
4
|
+
* leaves them wakes the worker over `NOTIFY` when a write leaves it work.
|
|
5
|
+
*/
|
|
6
|
+
import { type Db, JOBS_CHANNEL, type JobWake } from '@crombolo/data'
|
|
7
|
+
import type { CromboloConfig } from './config.ts'
|
|
8
|
+
|
|
9
|
+
export function runsJobs(config: Pick<CromboloConfig, 'role' | 'runJobs'>): boolean {
|
|
10
|
+
if (config.role === 'worker') return true
|
|
11
|
+
return config.role !== 'web' && config.runJobs
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Calls `onWork` for each wake a writer sends; resolves to the way to stop listening. */
|
|
15
|
+
export async function listenForJobs(
|
|
16
|
+
db: Db,
|
|
17
|
+
onWork: (work: JobWake) => void,
|
|
18
|
+
): Promise<(() => Promise<void>) | undefined> {
|
|
19
|
+
if (!db.listen) return undefined
|
|
20
|
+
return db.listen(JOBS_CHANNEL, (payload) => {
|
|
21
|
+
if (payload === 'events' || payload === 'search' || payload === 'jobs') onWork(payload)
|
|
22
|
+
})
|
|
23
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live reload in development (story 214): the event stream open pages listen
|
|
3
|
+
* on, the changes it carries, and the background type check whose findings it
|
|
4
|
+
* shows. docs/writing-components.md, "The development loop".
|
|
5
|
+
*/
|
|
6
|
+
import { watch } from 'node:fs'
|
|
7
|
+
import type { LiveChange, LiveTypes } from '@crombolo/render'
|
|
8
|
+
|
|
9
|
+
export interface LiveReloadOptions {
|
|
10
|
+
/** This process's identity; a page that reconnects to a different one reloads. */
|
|
11
|
+
bootId?: string
|
|
12
|
+
/** A burst of changes within this long is one reload. */
|
|
13
|
+
collectMs?: number
|
|
14
|
+
/** A comment this often keeps a quiet stream open through proxies. */
|
|
15
|
+
heartbeatMs?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LiveReload {
|
|
19
|
+
readonly bootId: string
|
|
20
|
+
/** Something open pages show has changed. */
|
|
21
|
+
changed(what: LiveChange): void
|
|
22
|
+
/** The background type check's latest findings, sent now and to each page that connects. */
|
|
23
|
+
types(findings: LiveTypes): void
|
|
24
|
+
/** The `text/event-stream` answer for a page's `EventSource`. */
|
|
25
|
+
stream(request: Request): Response
|
|
26
|
+
/** How many pages are listening. */
|
|
27
|
+
listeners(): number
|
|
28
|
+
close(): void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const encoder = new TextEncoder()
|
|
32
|
+
const frame = (event: string, data: unknown) =>
|
|
33
|
+
encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
|
|
34
|
+
|
|
35
|
+
export function createLiveReload(options: LiveReloadOptions = {}): LiveReload {
|
|
36
|
+
const bootId = options.bootId ?? crypto.randomUUID()
|
|
37
|
+
const collectMs = options.collectMs ?? 200
|
|
38
|
+
const listeners = new Set<ReadableStreamDefaultController<Uint8Array>>()
|
|
39
|
+
let latestTypes: LiveTypes = { count: 0 }
|
|
40
|
+
const pending = new Set<LiveChange>()
|
|
41
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
42
|
+
|
|
43
|
+
const send = (chunk: Uint8Array) => {
|
|
44
|
+
for (const listener of listeners) {
|
|
45
|
+
try {
|
|
46
|
+
listener.enqueue(chunk)
|
|
47
|
+
} catch {
|
|
48
|
+
listeners.delete(listener)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const heartbeat = setInterval(
|
|
53
|
+
() => send(encoder.encode(': still here\n\n')),
|
|
54
|
+
options.heartbeatMs ?? 15_000,
|
|
55
|
+
)
|
|
56
|
+
heartbeat.unref?.()
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
bootId,
|
|
60
|
+
changed(what) {
|
|
61
|
+
pending.add(what)
|
|
62
|
+
if (timer) return
|
|
63
|
+
timer = setTimeout(() => {
|
|
64
|
+
timer = undefined
|
|
65
|
+
const reasons = [...pending]
|
|
66
|
+
pending.clear()
|
|
67
|
+
send(frame('reload', { reasons }))
|
|
68
|
+
}, collectMs)
|
|
69
|
+
},
|
|
70
|
+
types(findings) {
|
|
71
|
+
latestTypes = findings
|
|
72
|
+
send(frame('types', findings))
|
|
73
|
+
},
|
|
74
|
+
stream(request) {
|
|
75
|
+
let self: ReadableStreamDefaultController<Uint8Array> | undefined
|
|
76
|
+
const body = new ReadableStream<Uint8Array>({
|
|
77
|
+
start(controller) {
|
|
78
|
+
self = controller
|
|
79
|
+
listeners.add(controller)
|
|
80
|
+
// A short retry, so a page finds a restarted server quickly.
|
|
81
|
+
controller.enqueue(encoder.encode('retry: 500\n\n'))
|
|
82
|
+
controller.enqueue(frame('hello', { boot: bootId }))
|
|
83
|
+
controller.enqueue(frame('types', latestTypes))
|
|
84
|
+
request.signal.addEventListener('abort', () => {
|
|
85
|
+
listeners.delete(controller)
|
|
86
|
+
try {
|
|
87
|
+
controller.close()
|
|
88
|
+
} catch {}
|
|
89
|
+
})
|
|
90
|
+
},
|
|
91
|
+
cancel() {
|
|
92
|
+
if (self) listeners.delete(self)
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
return new Response(body, {
|
|
96
|
+
headers: {
|
|
97
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
98
|
+
'cache-control': 'no-store',
|
|
99
|
+
connection: 'keep-alive',
|
|
100
|
+
},
|
|
101
|
+
})
|
|
102
|
+
},
|
|
103
|
+
listeners: () => listeners.size,
|
|
104
|
+
close() {
|
|
105
|
+
clearInterval(heartbeat)
|
|
106
|
+
clearTimeout(timer)
|
|
107
|
+
for (const listener of listeners) {
|
|
108
|
+
try {
|
|
109
|
+
listener.close()
|
|
110
|
+
} catch {}
|
|
111
|
+
}
|
|
112
|
+
listeners.clear()
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface TypeWatchOptions {
|
|
118
|
+
/** One check; aborted when a newer change starts another. */
|
|
119
|
+
check(signal: AbortSignal): Promise<{ checked: boolean; problems: string[] }>
|
|
120
|
+
/** A finished check's findings, only when they differ from the last. */
|
|
121
|
+
onFindings(findings: { problems: string[]; types: LiveTypes }): void
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Runs the type check one at a time: a change while one runs stops it and
|
|
126
|
+
* starts again, so the findings are always of the latest files.
|
|
127
|
+
*/
|
|
128
|
+
export function createTypeWatch(options: TypeWatchOptions) {
|
|
129
|
+
let running: AbortController | undefined
|
|
130
|
+
let last: string | undefined
|
|
131
|
+
|
|
132
|
+
const run = async () => {
|
|
133
|
+
running?.abort()
|
|
134
|
+
const controller = new AbortController()
|
|
135
|
+
running = controller
|
|
136
|
+
try {
|
|
137
|
+
const result = await options.check(controller.signal)
|
|
138
|
+
if (controller.signal.aborted || !result.checked) return
|
|
139
|
+
const key = result.problems.join('\n')
|
|
140
|
+
if (key === last) return
|
|
141
|
+
last = key
|
|
142
|
+
options.onFindings({ problems: result.problems, types: typesOf(result.problems) })
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (!controller.signal.aborted) throw error
|
|
145
|
+
} finally {
|
|
146
|
+
if (running === controller) running = undefined
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
/** Checks again, stopping a check already running. */
|
|
152
|
+
schedule(): Promise<void> {
|
|
153
|
+
return run()
|
|
154
|
+
},
|
|
155
|
+
stop() {
|
|
156
|
+
running?.abort()
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const TSC_LINE = /^(.+?)\((\d+),\d+\):\s*error\s+TS\d+:\s*(.*)$/
|
|
162
|
+
|
|
163
|
+
/** The banner's summary of `tsc`'s lines: how many errors, and the first. */
|
|
164
|
+
export function typesOf(problems: readonly string[]): LiveTypes {
|
|
165
|
+
const errors = problems.filter((line) => TSC_LINE.test(line))
|
|
166
|
+
const match = errors[0] ? TSC_LINE.exec(errors[0]) : null
|
|
167
|
+
return {
|
|
168
|
+
count: errors.length,
|
|
169
|
+
...(match
|
|
170
|
+
? {
|
|
171
|
+
first: {
|
|
172
|
+
file: match[1] as string,
|
|
173
|
+
line: Number(match[2]),
|
|
174
|
+
message: match[3] as string,
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
: {}),
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Calls `onChange` once a burst of changes under `dir` has settled; development only. */
|
|
182
|
+
export function watchDirectory(dir: string, onChange: () => void, settleMs = 100) {
|
|
183
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
184
|
+
const watcher = watch(dir, { recursive: true }, () => {
|
|
185
|
+
clearTimeout(timer)
|
|
186
|
+
timer = setTimeout(onChange, settleMs)
|
|
187
|
+
})
|
|
188
|
+
watcher.unref()
|
|
189
|
+
return {
|
|
190
|
+
close() {
|
|
191
|
+
clearTimeout(timer)
|
|
192
|
+
watcher.close()
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
}
|
package/src/log-viewer.ts
CHANGED
|
@@ -7,7 +7,12 @@
|
|
|
7
7
|
import { existsSync } from 'node:fs'
|
|
8
8
|
import { readdir, stat } from 'node:fs/promises'
|
|
9
9
|
import { join } from 'node:path'
|
|
10
|
-
import type {
|
|
10
|
+
import type {
|
|
11
|
+
LogEntry,
|
|
12
|
+
LogLevelName,
|
|
13
|
+
LogRange,
|
|
14
|
+
LogViewerPort,
|
|
15
|
+
} from '@crombolo/api-management/domain'
|
|
11
16
|
import { type Db, LogSearchRepository } from '@crombolo/data'
|
|
12
17
|
import type { CromboloConfig } from './config.ts'
|
|
13
18
|
import { type LogEvent, logFilter } from './log-expressions.ts'
|
package/src/marketplace.ts
CHANGED
|
@@ -21,11 +21,13 @@ import { readFileSync } from 'node:fs'
|
|
|
21
21
|
import { join } from 'node:path'
|
|
22
22
|
import {
|
|
23
23
|
type BundlePackage,
|
|
24
|
+
describePermissions,
|
|
24
25
|
EXTENSION_KEYWORD,
|
|
25
26
|
type ExtensionRegistry,
|
|
26
27
|
type PackageJson,
|
|
27
28
|
validateBundlePackage,
|
|
28
29
|
} from '@crombolo/backoffice-host'
|
|
30
|
+
import { type BundleGrants, declaredPermissions } from './bundle-grants.ts'
|
|
29
31
|
import { logger } from './logging.ts'
|
|
30
32
|
|
|
31
33
|
const log = logger('packages')
|
|
@@ -62,6 +64,14 @@ export interface InstalledListing {
|
|
|
62
64
|
description: string
|
|
63
65
|
/** The crombolo releases it is written for. */
|
|
64
66
|
peerRange: string
|
|
67
|
+
/** What its server half may do with the site, each with what it allows (story 222). */
|
|
68
|
+
permissions: { permission: string; allows: string }[]
|
|
69
|
+
/** Whether it is turned on: nothing is declared, or an administrator granted all of it. */
|
|
70
|
+
on: boolean
|
|
71
|
+
/** What it declares and has not been granted. */
|
|
72
|
+
missing: string[]
|
|
73
|
+
/** Who granted it, and when; absent when nobody has. */
|
|
74
|
+
granted?: { by: string | null; at: string; version: string }
|
|
65
75
|
}
|
|
66
76
|
|
|
67
77
|
/** What became of the content half of an install. */
|
|
@@ -84,6 +94,8 @@ export interface MarketplaceOptions {
|
|
|
84
94
|
install?: PackageInstaller
|
|
85
95
|
/** The ids of the bundles whose server half this node has mounted. */
|
|
86
96
|
wired?: () => readonly string[]
|
|
97
|
+
/** Where an install's grant is recorded, and where the listing reads it (story 222). */
|
|
98
|
+
grants?: Pick<BundleGrants, 'state' | 'grant'>
|
|
87
99
|
/** The version of a package whose content has been applied here. */
|
|
88
100
|
contentApplied?: (packageName: string) => Promise<string | null>
|
|
89
101
|
/**
|
|
@@ -118,6 +130,8 @@ export interface InstallOutcome {
|
|
|
118
130
|
* accept that (story 194): the screen asks, and sends the install again.
|
|
119
131
|
*/
|
|
120
132
|
needsServerConsent?: boolean
|
|
133
|
+
/** With `needsServerConsent`: what it asks to do with the site, to grant as a whole (story 222). */
|
|
134
|
+
permissions?: { permission: string; allows: string }[]
|
|
121
135
|
}
|
|
122
136
|
|
|
123
137
|
export type PackageInstaller = (
|
|
@@ -160,7 +174,14 @@ export interface Marketplace {
|
|
|
160
174
|
install(
|
|
161
175
|
name: string,
|
|
162
176
|
version: string | undefined,
|
|
163
|
-
options?: {
|
|
177
|
+
options?: {
|
|
178
|
+
applyContent?: boolean
|
|
179
|
+
acceptServerCode?: boolean
|
|
180
|
+
/** The permissions the administrator granted: the bundle's whole list (story 222). */
|
|
181
|
+
grant?: readonly string[]
|
|
182
|
+
/** Who is installing, recorded on the grant. */
|
|
183
|
+
by?: string | null
|
|
184
|
+
},
|
|
164
185
|
): Promise<InstallOutcome>
|
|
165
186
|
uninstall(name: string): Promise<InstallOutcome>
|
|
166
187
|
}
|
|
@@ -236,7 +257,9 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
236
257
|
const declaresAt = async (
|
|
237
258
|
name: string,
|
|
238
259
|
wanted?: string,
|
|
239
|
-
): Promise<
|
|
260
|
+
): Promise<
|
|
261
|
+
(BundlePackage['carries'] & { permissions: string[] }) | 'not-a-bundle' | 'unreachable'
|
|
262
|
+
> => {
|
|
240
263
|
try {
|
|
241
264
|
const response = await call(`${options.registry}/${encodeURIComponent(name)}`, {
|
|
242
265
|
headers: { accept: 'application/json' },
|
|
@@ -263,11 +286,13 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
263
286
|
extensions?: unknown[]
|
|
264
287
|
content?: boolean
|
|
265
288
|
server?: string
|
|
289
|
+
permissions?: string[]
|
|
266
290
|
}
|
|
267
291
|
return {
|
|
268
292
|
extensions: Array.isArray(field.extensions) && field.extensions.length > 0,
|
|
269
293
|
content: field.content === true,
|
|
270
294
|
server: field.server !== undefined,
|
|
295
|
+
permissions: Array.isArray(field.permissions) ? field.permissions : [],
|
|
271
296
|
}
|
|
272
297
|
} catch {
|
|
273
298
|
return 'unreachable'
|
|
@@ -276,8 +301,32 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
276
301
|
|
|
277
302
|
/** What the registry says this package's latest version carries, when it is a valid bundle. */
|
|
278
303
|
const declares = async (name: string): Promise<BundlePackage['carries'] | undefined> => {
|
|
279
|
-
const
|
|
280
|
-
|
|
304
|
+
const declared = await declaresAt(name)
|
|
305
|
+
if (typeof declared === 'string') return undefined
|
|
306
|
+
const { permissions: _, ...carries } = declared
|
|
307
|
+
return carries
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** A bundle's permissions and where its grant stands, for the listing. */
|
|
311
|
+
const grantOf = (bundle: BundlePackage) => {
|
|
312
|
+
const declared = describePermissions(declaredPermissions(bundle))
|
|
313
|
+
if (!bundle.carries.server || !options.grants)
|
|
314
|
+
return { permissions: declared, on: true, missing: [] as string[] }
|
|
315
|
+
const state = options.grants.state(bundle)
|
|
316
|
+
return {
|
|
317
|
+
permissions: state.declared,
|
|
318
|
+
on: state.on,
|
|
319
|
+
missing: state.missing,
|
|
320
|
+
...(state.grant
|
|
321
|
+
? {
|
|
322
|
+
granted: {
|
|
323
|
+
by: state.grant.grantedBy,
|
|
324
|
+
at: state.grant.grantedAt.toISOString(),
|
|
325
|
+
version: state.grant.version,
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
: {}),
|
|
329
|
+
}
|
|
281
330
|
}
|
|
282
331
|
|
|
283
332
|
return {
|
|
@@ -356,6 +405,7 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
356
405
|
contentApplied: (await options.contentApplied?.(bundle.packageName)) ?? null,
|
|
357
406
|
description: descriptionOf(bundle.directory),
|
|
358
407
|
peerRange: bundle.peerRange,
|
|
408
|
+
...grantOf(bundle),
|
|
359
409
|
})),
|
|
360
410
|
)
|
|
361
411
|
},
|
|
@@ -381,7 +431,8 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
381
431
|
ok: false,
|
|
382
432
|
message: `${name} is not a bundle for this site, so it was not installed.`,
|
|
383
433
|
}
|
|
384
|
-
if (declared.server && request.
|
|
434
|
+
if (declared.server && !consented(request, declared.permissions))
|
|
435
|
+
return serverConsentNeeded(name, declared.permissions)
|
|
385
436
|
|
|
386
437
|
const specifier = version ? `${name}@${version}` : name
|
|
387
438
|
const result = await install('add', specifier, options.siteDir)
|
|
@@ -404,14 +455,17 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
404
455
|
}
|
|
405
456
|
// The version that landed is what counts: a release published between
|
|
406
457
|
// reading the registry and installing may have brought server code.
|
|
407
|
-
if (found.carries.server && request
|
|
458
|
+
if (found.carries.server && !consented(request, declaredPermissions(found))) {
|
|
408
459
|
await install('remove', name, options.siteDir)
|
|
409
460
|
options.extensions.invalidate()
|
|
410
461
|
return serverConsentNeeded(
|
|
411
462
|
name,
|
|
412
|
-
|
|
463
|
+
declaredPermissions(found),
|
|
464
|
+
`${name}@${found.version} turned out to run code on the server, or to ask for more than was granted, so it was removed again.`,
|
|
413
465
|
)
|
|
414
466
|
}
|
|
467
|
+
// What was granted is recorded against the version that landed.
|
|
468
|
+
if (found.carries.server) await options.grants?.grant(found, request.by ?? null)
|
|
415
469
|
|
|
416
470
|
// The content half lands after the dependency, so a refused check leaves
|
|
417
471
|
// the package installed and says why, rather than undoing an install that
|
|
@@ -475,12 +529,30 @@ function descriptionOf(directory: string): string {
|
|
|
475
529
|
/** Said of every bundle installed here: its screens are someone else's code in our browsers. */
|
|
476
530
|
export const SCREENS_NOTICE = 'Its screens run in the browser of everyone who uses the back office.'
|
|
477
531
|
|
|
478
|
-
/**
|
|
479
|
-
|
|
532
|
+
/**
|
|
533
|
+
* Whether an install request said yes to server code and granted exactly what
|
|
534
|
+
* the bundle asks for (stories 194, 222): all of it, and nothing it did not ask.
|
|
535
|
+
*/
|
|
536
|
+
function consented(
|
|
537
|
+
request: { acceptServerCode?: boolean; grant?: readonly string[] },
|
|
538
|
+
permissions: readonly string[],
|
|
539
|
+
): boolean {
|
|
540
|
+
if (request.acceptServerCode !== true) return false
|
|
541
|
+
const granted = new Set(request.grant ?? [])
|
|
542
|
+
return granted.size === new Set(permissions).size && permissions.every((p) => granted.has(p))
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** A refusal the screen answers by asking for an explicit yes, and the grant (stories 194, 222). */
|
|
546
|
+
function serverConsentNeeded(
|
|
547
|
+
name: string,
|
|
548
|
+
permissions: readonly string[],
|
|
549
|
+
before = '',
|
|
550
|
+
): InstallOutcome {
|
|
480
551
|
return {
|
|
481
552
|
ok: false,
|
|
482
553
|
needsServerConsent: true,
|
|
483
|
-
|
|
554
|
+
permissions: describePermissions(permissions),
|
|
555
|
+
message: `${before ? `${before} ` : ''}${name} runs code on the server, with access to the site's data, files and settings. Confirm that you trust its publisher, and approve what it asks to do, to install it.`,
|
|
484
556
|
}
|
|
485
557
|
}
|
|
486
558
|
|
package/src/media-access.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* reads and sets rules through `handle`, under
|
|
9
9
|
* `<backoffice>/crombolo/api/media-access/{key}`, which is not in the contract.
|
|
10
10
|
*/
|
|
11
|
-
import { hasSection, type Principal } from '@crombolo/api-management'
|
|
11
|
+
import { hasSection, type Principal } from '@crombolo/api-management/domain'
|
|
12
12
|
import {
|
|
13
13
|
fallbackPermissions,
|
|
14
14
|
hasPathAccess,
|
|
@@ -180,10 +180,12 @@ export function createMediaAccess(deps: MediaAccessDeps) {
|
|
|
180
180
|
const isProtected = (item: MediaByUrl) => item.trashed || isProtectedMedia(item.access, item.rule)
|
|
181
181
|
|
|
182
182
|
const memberGroups = async (): Promise<string[]> => {
|
|
183
|
-
const rows = await db.
|
|
184
|
-
'
|
|
185
|
-
|
|
186
|
-
|
|
183
|
+
const rows = await db.q
|
|
184
|
+
.selectFrom('node')
|
|
185
|
+
.select('text')
|
|
186
|
+
.where('node_object_type', '=', ObjectTypes.MemberGroup)
|
|
187
|
+
.orderBy('text')
|
|
188
|
+
.execute()
|
|
187
189
|
return rows.flatMap((row) => (row.text ? [String(row.text)] : []))
|
|
188
190
|
}
|
|
189
191
|
|
|
@@ -222,10 +224,11 @@ export function createMediaAccess(deps: MediaAccessDeps) {
|
|
|
222
224
|
writePolicy: 'inherit' as const,
|
|
223
225
|
creatorMemberKey: null,
|
|
224
226
|
}
|
|
225
|
-
const fileRows = await db.
|
|
226
|
-
'
|
|
227
|
-
|
|
228
|
-
|
|
227
|
+
const fileRows = await db.q
|
|
228
|
+
.selectFrom('content')
|
|
229
|
+
.select('file_url')
|
|
230
|
+
.where('node_id', '=', node.id)
|
|
231
|
+
.execute()
|
|
229
232
|
const from = inherited ? await nodes.byKey(inherited.nodeKey) : undefined
|
|
230
233
|
const creator = item.creatorMemberKey
|
|
231
234
|
? await new MemberRepository(db).byKey(item.creatorMemberKey)
|