/** * Serve pre-cached store covers, screenshots, and demo clips from local disk only. * * The local server listens strictly on loopback and never initiates outbound network * requests: files present in the player's offline asset pack or local media cache are * served directly (with HTTP byte-range support for video playback), while uncached * paths immediately return 404 so the browser UI falls back without stalling. */ import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import type { ServiceResponse } from '../transport/exchange.ts'; export const STORE_MEDIA_PREFIX = 'store-media/'; const STORE_HOST = 'static-store.gaijin.net'; const MANIFEST = new URL('./web/official-store-media.json', import.meta.url); const CONTENT_TYPES: Readonly> = { jpg: 'image/jpeg', webm: 'video/webm', mp4: 'video/mp4', }; interface StoreMediaEntry { readonly cover: string; readonly screenshots?: readonly string[]; readonly videos?: readonly string[]; } function knownAssets(): ReadonlyMap { const { byProduct } = JSON.parse(readFileSync(MANIFEST, 'utf8')) as { byProduct: Record }; const assets = new Map(); for (const entry of Object.values(byProduct)) { for (const path of [entry.cover, ...entry.screenshots ?? [], ...entry.videos ?? []]) { const type = CONTENT_TYPES[path.slice(path.lastIndexOf('.') + 1)]; if (type && /^(?:img\/|video\/)[a-z0-9/_-]+\.(?:jpg|mp4|webm)$/i.test(path)) assets.set(path, type); } } return assets; } export class StoreMediaCache { readonly #assets = knownAssets(); readonly #roots: readonly string[]; constructor(dataFolder: string) { this.#roots = [ join(dataFolder, 'asset-pack', STORE_HOST), join(dataFolder, 'cache', 'store-media'), join(dataFolder, 'store-media-cache'), join(dataFolder, 'store-assets'), ]; } serve(path: string, range: string | undefined): ServiceResponse { const type = this.#assets.get(path); if (!type) return { status: 404, logNotes: { summary: 'unknown store media' } }; const bytes = this.#readLocal(path); if (!bytes) { return { status: 404, headers: { 'cache-control': 'no-store' }, logNotes: { summary: 'store media not cached: ' + path }, }; } const logNotes = { summary: 'store media: ' + path, omitResponseBody: true }; const headers = { 'content-type': type, 'cache-control': 'public, max-age=86400', 'accept-ranges': 'bytes' }; if (!range) return { status: 200, headers: { ...headers, 'content-length': String(bytes.length) }, body: bytes, logNotes }; const span = parseRange(range, bytes.length); if (!span) return { status: 416, headers: { ...headers, 'content-range': `bytes */${bytes.length}` }, logNotes }; const [first, last] = span; const body = bytes.subarray(first, last + 1); return { status: 206, headers: { ...headers, 'content-range': `bytes ${first}-${last}/${bytes.length}`, 'content-length': String(body.length) }, body, logNotes, }; } #readLocal(path: string): Buffer | undefined { for (const root of this.#roots) { const bytes = readCached(join(root, path)); if (bytes) return bytes; } return undefined; } } function readCached(path: string): Buffer | undefined { if (!existsSync(path)) return undefined; try { const bytes = readFileSync(path); return bytes.length ? bytes : undefined; } catch { return undefined; } } function parseRange(range: string, size: number): readonly [number, number] | undefined { const match = /^bytes=(\d*)-(\d*)$/.exec(range); if (!match || (!match[1] && !match[2])) return undefined; if (!match[1]) { const count = Number(match[2]); return Number.isSafeInteger(count) && count > 0 ? [Math.max(0, size - count), size - 1] : undefined; } const from = Number(match[1]); const to = match[2] ? Number(match[2]) : size - 1; if (!Number.isSafeInteger(from) || !Number.isSafeInteger(to) || from >= size || to < from) return undefined; return [from, Math.min(to, size - 1)]; }