@crombolo/server 0.34.0 → 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.
@@ -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) => name.replace(/[/\\:*?"<>|]+/g, '-').trim() || 'item'
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 = join(siteDir, 'seed', relative)
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(join(siteDir, 'seed', 'media', path), { recursive: true })
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 = join(siteDir, 'seed', 'media', path)
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
- const snapshot = await cache.snapshot()
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
- ? snapshot.media.get(effective.defaultImage.trim().toLowerCase())
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(),