@crombolo/server 0.33.1 → 0.35.2
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/README.md +14 -0
- package/package.json +13 -13
- package/src/adapters/audit.ts +1 -0
- package/src/adapters/bundles.ts +5 -4
- package/src/adapters/documents.ts +48 -22
- package/src/bundle-build.ts +7 -1
- package/src/bundle-code.ts +10 -71
- package/src/bundle-media.ts +613 -0
- package/src/bundles.ts +511 -57
- package/src/components.ts +58 -0
- package/src/config.ts +17 -1
- package/src/content-events.ts +11 -6
- package/src/delivery-media.ts +58 -0
- package/src/image-metadata.ts +355 -0
- package/src/imaging.ts +98 -0
- package/src/index.ts +3 -0
- package/src/marketplace.ts +88 -12
- package/src/media-access.ts +319 -0
- package/src/member-auth.ts +24 -3
- package/src/ports.ts +7 -2
- package/src/rate-limit.ts +54 -0
- package/src/schema.ts +78 -8
- package/src/seed/export.ts +16 -6
- package/src/seo.ts +5 -6
- package/src/server.ts +369 -73
- package/src/settings.ts +8 -0
- package/src/site-file.ts +2 -2
package/src/schema.ts
CHANGED
|
@@ -16,6 +16,8 @@ import {
|
|
|
16
16
|
ensureSystemContentTypes,
|
|
17
17
|
ensureSystemMemberTypes,
|
|
18
18
|
type Finding,
|
|
19
|
+
instructionEffects,
|
|
20
|
+
instructionsBetween,
|
|
19
21
|
Locks,
|
|
20
22
|
latestCacheInstructionId,
|
|
21
23
|
latestCheckpoint,
|
|
@@ -251,12 +253,30 @@ export interface CachePollerOptions {
|
|
|
251
253
|
content?: {
|
|
252
254
|
served(): { checkpoint: number | undefined; builtAt: Date } | undefined
|
|
253
255
|
behind(latest: number): void
|
|
256
|
+
/**
|
|
257
|
+
* Told that every change from the one it serves up to `checkpoint` left the
|
|
258
|
+
* pages alone — media only (story 197) — so its snapshot holds them too.
|
|
259
|
+
*/
|
|
260
|
+
covered?(checkpoint: number): void
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The media cache (story 197). Every change, this node's own included, is
|
|
264
|
+
* read in checkpoint order: a media change drops the items it names, and a
|
|
265
|
+
* change that empties caches empties it. A gap in the checkpoints, or being
|
|
266
|
+
* further behind than `MEDIA_REPLAY_LIMIT`, empties it too.
|
|
267
|
+
*/
|
|
268
|
+
media?: {
|
|
269
|
+
apply(change: { keys: string[]; moved: string[] }): void
|
|
270
|
+
clear(): void
|
|
254
271
|
}
|
|
255
272
|
}
|
|
256
273
|
|
|
257
274
|
/** A rebuild asked for and not yet served is asked for again after this long. */
|
|
258
275
|
const REASK_AFTER_MS = 30_000
|
|
259
276
|
|
|
277
|
+
/** Further behind than this many changes, the media cache is emptied rather than replayed. */
|
|
278
|
+
export const MEDIA_REPLAY_LIMIT = 5_000
|
|
279
|
+
|
|
260
280
|
/**
|
|
261
281
|
* Polls `cache_instruction` from where this node came in; own instructions are
|
|
262
282
|
* skipped. Where the database can notify, a write wakes it at once and the
|
|
@@ -283,6 +303,10 @@ export class CacheInstructionPoller {
|
|
|
283
303
|
/** The checkpoint this node last asked its cache to catch up to, and when. */
|
|
284
304
|
#asked: { checkpoint: number; at: number } | undefined
|
|
285
305
|
#latest: number | undefined
|
|
306
|
+
/** The checkpoint whose changes have all been read for the caches; undefined before the start. */
|
|
307
|
+
#followed: number | undefined
|
|
308
|
+
/** Checkpoints read whose changes left the pages alone. */
|
|
309
|
+
#pagesFree = new Set<number>()
|
|
286
310
|
|
|
287
311
|
/** The latest committed content checkpoint, as of the last poll. */
|
|
288
312
|
get latestCheckpoint(): number | undefined {
|
|
@@ -318,6 +342,8 @@ export class CacheInstructionPoller {
|
|
|
318
342
|
this.#paused = await readWritesPaused(this.#options.db)
|
|
319
343
|
this.#cursor = await latestCacheInstructionId(this.#options.db)
|
|
320
344
|
this.#origin = this.#cursor
|
|
345
|
+
// An empty media cache is current with whatever has committed so far.
|
|
346
|
+
this.#followed = await latestCheckpoint(this.#options.db)
|
|
321
347
|
if (intervalMs > 0) {
|
|
322
348
|
this.#timer = setInterval(() => void this.poll().catch(() => {}), intervalMs)
|
|
323
349
|
this.#timer.unref()
|
|
@@ -355,6 +381,8 @@ export class CacheInstructionPoller {
|
|
|
355
381
|
|
|
356
382
|
async #pollOnce(): Promise<number> {
|
|
357
383
|
const { db, nodeId, nodeState, onInstruction } = this.#options
|
|
384
|
+
// Read first: every instruction numbered up to it has committed, and is visible below.
|
|
385
|
+
const latest = await latestCheckpoint(db)
|
|
358
386
|
// A row can commit after one with a higher id (Postgres hands ids out
|
|
359
387
|
// before commit), so a short window behind the cursor is read again and
|
|
360
388
|
// the ids already seen are skipped.
|
|
@@ -371,7 +399,8 @@ export class CacheInstructionPoller {
|
|
|
371
399
|
onInstruction(instruction.kind, instruction.payload)
|
|
372
400
|
}
|
|
373
401
|
for (const id of this.#seen) if (id <= this.#cursor - CURSOR_WINDOW) this.#seen.delete(id)
|
|
374
|
-
|
|
402
|
+
await this.#follow(latest)
|
|
403
|
+
const served = this.#catchUp(latest)
|
|
375
404
|
await touchServer(db, {
|
|
376
405
|
nodeId,
|
|
377
406
|
...nodeState,
|
|
@@ -394,19 +423,60 @@ export class CacheInstructionPoller {
|
|
|
394
423
|
return applied
|
|
395
424
|
}
|
|
396
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Reads every change since the last checkpoint followed, in order: for the
|
|
428
|
+
* media cache, and to know which left the pages alone. Checkpoints count up
|
|
429
|
+
* by one per change, so a number with no instruction was pruned before this
|
|
430
|
+
* node read it, and the media cache cannot know what it missed.
|
|
431
|
+
*/
|
|
432
|
+
async #follow(latest: number): Promise<void> {
|
|
433
|
+
const from = this.#followed
|
|
434
|
+
if (from === undefined || latest === from) return
|
|
435
|
+
const media = this.#options.media
|
|
436
|
+
this.#followed = latest
|
|
437
|
+
// Behind a reset count, or too far behind to replay: start the media again.
|
|
438
|
+
if (latest < from || latest - from > MEDIA_REPLAY_LIMIT) {
|
|
439
|
+
media?.clear()
|
|
440
|
+
this.#pagesFree.clear()
|
|
441
|
+
return
|
|
442
|
+
}
|
|
443
|
+
const missed = await instructionsBetween(this.#options.db, from, latest)
|
|
444
|
+
const numbered = new Set(missed.map((instruction) => instruction.checkpoint))
|
|
445
|
+
let gap = false
|
|
446
|
+
for (let checkpoint = from + 1; checkpoint <= latest && !gap; checkpoint++)
|
|
447
|
+
gap = !numbered.has(checkpoint)
|
|
448
|
+
if (gap) media?.clear()
|
|
449
|
+
for (const instruction of missed) {
|
|
450
|
+
const effects = instructionEffects(instruction.kind, instruction.payload)
|
|
451
|
+
if (!effects.pages && instruction.checkpoint !== null)
|
|
452
|
+
this.#pagesFree.add(instruction.checkpoint)
|
|
453
|
+
if (gap || !media || effects.media === 'keep') continue
|
|
454
|
+
if (effects.media === 'clear') media.clear()
|
|
455
|
+
else media.apply(effects.media)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
397
459
|
/**
|
|
398
460
|
* Tells the cache it is behind when the latest committed checkpoint is past
|
|
399
|
-
* the one it serves
|
|
400
|
-
*
|
|
401
|
-
* failed rebuild is retried.
|
|
461
|
+
* the one it serves, and any changes between left pages alone. A cache that
|
|
462
|
+
* has not been built yet is left to build on its first read. A request not
|
|
463
|
+
* served within a while is made again, so a failed rebuild is retried.
|
|
402
464
|
*/
|
|
403
|
-
|
|
465
|
+
#catchUp(latest: number) {
|
|
404
466
|
const content = this.#options.content
|
|
405
467
|
if (!content) return undefined
|
|
406
|
-
const latest = await latestCheckpoint(this.#options.db)
|
|
407
468
|
this.#latest = latest
|
|
408
|
-
|
|
409
|
-
if (served?.checkpoint === undefined
|
|
469
|
+
let served = content.served()
|
|
470
|
+
if (served?.checkpoint === undefined) return served
|
|
471
|
+
let covered = served.checkpoint
|
|
472
|
+
while (this.#pagesFree.has(covered + 1)) covered += 1
|
|
473
|
+
for (const checkpoint of this.#pagesFree)
|
|
474
|
+
if (checkpoint <= covered) this.#pagesFree.delete(checkpoint)
|
|
475
|
+
if (covered > served.checkpoint) {
|
|
476
|
+
content.covered?.(covered)
|
|
477
|
+
served = content.served() ?? served
|
|
478
|
+
}
|
|
479
|
+
if (covered >= latest) return served
|
|
410
480
|
const asked = this.#asked
|
|
411
481
|
if (!asked || asked.checkpoint < latest || Date.now() - asked.at > REASK_AFTER_MS) {
|
|
412
482
|
this.#asked = { checkpoint: latest, at: Date.now() }
|
package/src/seed/export.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* would derive, so a round trip names the same nodes.
|
|
11
11
|
*/
|
|
12
12
|
import { mkdirSync, writeFileSync } from 'node:fs'
|
|
13
|
-
import { join } from 'node:path'
|
|
13
|
+
import { join, resolve, sep } from 'node:path'
|
|
14
14
|
import { mediaFileOf, normaliseUuid, ObjectTypes } from '@crombolo/core'
|
|
15
15
|
import {
|
|
16
16
|
ContentTypeRepository,
|
|
@@ -33,8 +33,11 @@ export const segmentOf = (name: string) =>
|
|
|
33
33
|
.replace(/[^a-z0-9]+/g, '-')
|
|
34
34
|
.replace(/^-+|-+$/g, '') || 'item'
|
|
35
35
|
|
|
36
|
-
/** A media file or folder name as it can sit on disk
|
|
37
|
-
const fileName = (name: string) =>
|
|
36
|
+
/** A media file or folder name as it can sit on disk: never `.` or `..`. */
|
|
37
|
+
const fileName = (name: string) => {
|
|
38
|
+
const safe = name.replace(/[/\\:*?"<>|]+/g, '-').trim()
|
|
39
|
+
return !safe || /^\.+$/.test(safe) ? 'item' : safe
|
|
40
|
+
}
|
|
38
41
|
|
|
39
42
|
const bareKey = (key: string) => (/^[A-Za-z0-9_-]+$/.test(key) ? key : JSON.stringify(key))
|
|
40
43
|
|
|
@@ -318,8 +321,15 @@ export async function exportSeed(
|
|
|
318
321
|
|
|
319
322
|
// --------------------------------------------------------- the files
|
|
320
323
|
const written: string[] = []
|
|
324
|
+
const seedRoot = resolve(siteDir, 'seed')
|
|
325
|
+
const under = (relative: string) => {
|
|
326
|
+
const file = resolve(seedRoot, relative)
|
|
327
|
+
if (!file.startsWith(seedRoot + sep))
|
|
328
|
+
throw new Error(`Refusing to write ${relative} outside the seed directory.`)
|
|
329
|
+
return file
|
|
330
|
+
}
|
|
321
331
|
const write = (relative: string, text: string) => {
|
|
322
|
-
const file =
|
|
332
|
+
const file = under(relative)
|
|
323
333
|
mkdirSync(join(file, '..'), { recursive: true })
|
|
324
334
|
writeFileSync(file, text)
|
|
325
335
|
written.push(`seed/${relative}`)
|
|
@@ -366,12 +376,12 @@ export async function exportSeed(
|
|
|
366
376
|
const src = mediaFile.get(node.key)
|
|
367
377
|
if (!src) {
|
|
368
378
|
// A folder: its directory says it, unless it has a name or key of its own.
|
|
369
|
-
mkdirSync(
|
|
379
|
+
mkdirSync(under(`media/${path}`), { recursive: true })
|
|
370
380
|
continue
|
|
371
381
|
}
|
|
372
382
|
const stored = options.files ? await options.files.open(src) : undefined
|
|
373
383
|
if (stored) {
|
|
374
|
-
const file =
|
|
384
|
+
const file = under(`media/${path}`)
|
|
375
385
|
mkdirSync(join(file, '..'), { recursive: true })
|
|
376
386
|
writeFileSync(file, await stored.bytes())
|
|
377
387
|
written.push(`seed/media/${path}`)
|
package/src/seo.ts
CHANGED
|
@@ -281,8 +281,7 @@ export function createSeoHost(options: SeoHostOptions): SeoHost {
|
|
|
281
281
|
const trimmed = value.trim()
|
|
282
282
|
if (!trimmed) return undefined
|
|
283
283
|
if (!UUID.test(trimmed)) return trimmed
|
|
284
|
-
|
|
285
|
-
return snapshot.media.get(trimmed.toLowerCase())?.url
|
|
284
|
+
return (await cache.media?.byKey(trimmed))?.url
|
|
286
285
|
}
|
|
287
286
|
|
|
288
287
|
const siteDefaults: SeoHost['siteDefaults'] = async () => {
|
|
@@ -320,7 +319,7 @@ export function createSeoHost(options: SeoHostOptions): SeoHost {
|
|
|
320
319
|
const details: SeoHost['details'] = async (content, request) => {
|
|
321
320
|
const snapshot = await cache.snapshot()
|
|
322
321
|
const { effective } = await settings()
|
|
323
|
-
const meta = effectiveMeta(content, {
|
|
322
|
+
const meta = await effectiveMeta(content, {
|
|
324
323
|
origin: publicOrigin(request, config),
|
|
325
324
|
alternates: urlsIn(snapshot, content.key),
|
|
326
325
|
site: await siteDefaults(),
|
|
@@ -328,9 +327,9 @@ export function createSeoHost(options: SeoHostOptions): SeoHost {
|
|
|
328
327
|
// A size only when the image is a media item: the page's own, or a site default given by key.
|
|
329
328
|
const media =
|
|
330
329
|
meta.image.source === 'page'
|
|
331
|
-
? content.media(SEO_ALIASES.image)[0]?.media
|
|
330
|
+
? (await content.media(SEO_ALIASES.image))[0]?.media
|
|
332
331
|
: meta.image.source === 'site' && UUID.test(effective.defaultImage.trim())
|
|
333
|
-
?
|
|
332
|
+
? await cache.media?.byKey(effective.defaultImage.trim())
|
|
334
333
|
: undefined
|
|
335
334
|
const size = (alias: string) => {
|
|
336
335
|
const value = Number(media?.value(alias))
|
|
@@ -413,7 +412,7 @@ export function createSeoHost(options: SeoHostOptions): SeoHost {
|
|
|
413
412
|
if (!draft) return Response.json({ message: 'That page could not be found.' }, { status: 404 })
|
|
414
413
|
const origin = publicOrigin(request, config)
|
|
415
414
|
const snapshot = await cache.snapshot()
|
|
416
|
-
const meta = effectiveMeta(draft.content, {
|
|
415
|
+
const meta = await effectiveMeta(draft.content, {
|
|
417
416
|
origin,
|
|
418
417
|
alternates: urlsIn(snapshot, key),
|
|
419
418
|
site: await siteDefaults(),
|