logigate/server/utils/library.ts

71 lines
3.0 KiB
TypeScript

// The public component library, as JSON files under data/library:
// entries:<id> holds a published component with everything it uses, index
// the list everyone browses. Publishers have no account here, just the
// random player id kept in their browser (as on the leaderboards) and an
// edit key per entry, which only their browser has; only its hash is stored.
import type { H3Event } from 'h3'
import { cleanBundle, LIBRARY_LIMITS, type LibraryBundle, type LibraryMeta } from '../../app/lib/logic/library'
/** An entry in the index: its meta with the publisher's player id instead of their name. */
export type LibraryItem = Omit<LibraryMeta, 'author'> & { player: string }
export interface LibraryRecord {
meta: LibraryItem
bundle: LibraryBundle
keyHash: string
}
/** Entries the library holds at most, so the index stays small enough to read on every visit. */
export const MAX_ENTRIES = 20_000
export const libraryStorage = () => useStorage('library')
export const entryKey = (id: string) => `entries:${id}`
export async function libraryIndex(): Promise<LibraryItem[]> {
return (await libraryStorage().getItem<LibraryItem[]>('index')) ?? []
}
/** An entry as everyone sees it: the author by the name they chose, never their player id. */
export function publicMeta(item: LibraryItem, names: Record<string, string>): LibraryMeta {
const { player, ...meta } = item
return { ...meta, author: names[player] ?? '' }
}
/** Entries whose name, description, group or author contain every word of the query, newest first. */
export function searchLibrary(list: LibraryMeta[], query: string): LibraryMeta[] {
const words = query.toLowerCase().split(/\s+/).filter(Boolean)
return list
.filter(m => {
const text = `${m.name} ${m.description} ${m.group} ${m.author}`.toLowerCase()
return words.every(w => text.includes(w))
})
.sort((a, b) => b.updated - a.updated)
}
/** The publisher and their component, checked and cleaned like any project. */
export async function readLibraryBody(event: H3Event): Promise<{ player: string; bundle: LibraryBundle }> {
const raw = await readRawBody(event, 'utf8')
if (!raw) throw createError({ statusCode: 400, message: 'Empty request' })
if (raw.length > LIBRARY_LIMITS.bytes) throw createError({ statusCode: 413, message: 'Too large for the library (1 MB at most)' })
let body: { player?: unknown }
try {
body = JSON.parse(raw)
} catch {
throw createError({ statusCode: 400, message: 'Not valid JSON' })
}
if (!isPlayer(body.player)) throw createError({ statusCode: 400, message: 'Missing player' })
try {
return { player: body.player, bundle: cleanBundle(body) }
} catch (e) {
throw createError({ statusCode: 400, message: (e as Error).message })
}
}
/** The entry behind an API id, or a 404. */
export async function findEntry(id: string | undefined): Promise<LibraryRecord> {
const record = isShareId(id) ? await libraryStorage().getItem<LibraryRecord>(entryKey(id)) : null
if (!record) throw createError({ statusCode: 404, message: 'Not in the library (any more)' })
return record
}