// Serves the locally-vendored React ESM (client/vendor/react/, // generated by scripts/build-vendor.ts) behind the // `/vendor/react///*` URLs the importmap in client/index.html // points at. This is what makes moi run offline — no esm.sh fetch. // // Both the version and the build mode are in the URL, which is what makes the // files safe to cache forever: a React bump moves the URL, and the development // and production builds (different bytes — prod's jsx-dev-runtime has no // `jsxDEV`) never share one, so a prebuilt `moi` and `bun run dev` on the same // port cannot poison each other's cache. The source HTML names the development // build; scripts/build-client.ts switches it to production in `dist/`. // `client/` ships in the published package (see package.json `files`), so this // resolves in a global install too. import { join } from 'node:path' const VENDOR_DIR = join(import.meta.dir, '..', 'client', 'vendor', 'react') const EMOJIBASE_DIR = join(import.meta.dir, '..', 'client', 'vendor', 'emojibase') // `//`: a version folder, the build mode, then a single // flat file (`react.js`) or one nested under `_impl/` — all `.js`, no // traversal. Matches exactly the layout build-vendor.ts emits. const ALLOWED = /^\d[\w.-]*\/(development|production)\/(_impl\/)?[a-zA-Z0-9_-]+\.js$/ export async function serveVendorReact(req: Request): Promise { const rel = new URL(req.url).pathname.replace(/^\/vendor\/react\//, '') if (!ALLOWED.test(rel) || rel.includes('..')) { return new Response('Not found', { status: 404 }) } const file = Bun.file(join(VENDOR_DIR, rel)) if (!(await file.exists())) { return new Response('Not found', { status: 404 }) } return new Response(file, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'public, max-age=31536000, immutable' } }) } // `/.json` only — mirrors the emojibase-data package layout the // emoji picker expects (`${emojibaseUrl}/${locale}/data.json`). const EMOJIBASE_ALLOWED = /^[a-z-]+\/[a-z]+\.json$/ // Serves the vendored emojibase-data JSON (client/vendor/emojibase) behind // `/vendor/emojibase/*`, so the settings emoji picker works offline — no // jsdelivr fetch at runtime. export async function serveVendorEmojibase(req: Request): Promise { const rel = new URL(req.url).pathname.replace(/^\/vendor\/emojibase\//, '') if (!EMOJIBASE_ALLOWED.test(rel) || rel.includes('..')) { return new Response('Not found', { status: 404 }) } const file = Bun.file(join(EMOJIBASE_DIR, rel)) if (!(await file.exists())) { return new Response('Not found', { status: 404 }) } return new Response(file, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'public, max-age=3600' } }) }