|
| 1 | +import { createReadStream } from 'node:fs' |
| 2 | +import { stat } from 'node:fs/promises' |
| 3 | +import { Readable } from 'node:stream' |
| 4 | +import { extname, join, normalize, resolve, sep } from 'pathe' |
| 5 | +import { ensureMinimalNextDevframeHub, getStaticMount } from '../../../devframe/minimal-next-devframe-hub' |
| 6 | + |
| 7 | +export const runtime = 'nodejs' |
| 8 | +export const dynamic = 'force-dynamic' |
| 9 | + |
| 10 | +const CONTENT_TYPES: Record<string, string> = { |
| 11 | + '.html': 'text/html; charset=utf-8', |
| 12 | + '.htm': 'text/html; charset=utf-8', |
| 13 | + '.css': 'text/css; charset=utf-8', |
| 14 | + '.js': 'application/javascript; charset=utf-8', |
| 15 | + '.mjs': 'application/javascript; charset=utf-8', |
| 16 | + '.json': 'application/json; charset=utf-8', |
| 17 | + '.svg': 'image/svg+xml', |
| 18 | + '.png': 'image/png', |
| 19 | + '.jpg': 'image/jpeg', |
| 20 | + '.jpeg': 'image/jpeg', |
| 21 | + '.gif': 'image/gif', |
| 22 | + '.webp': 'image/webp', |
| 23 | + '.ico': 'image/x-icon', |
| 24 | +} |
| 25 | + |
| 26 | +interface ResolvedFile { |
| 27 | + abs: string |
| 28 | + size: number |
| 29 | + mtime: Date |
| 30 | +} |
| 31 | + |
| 32 | +async function statFile(abs: string): Promise<ResolvedFile | null> { |
| 33 | + try { |
| 34 | + const s = await stat(abs) |
| 35 | + if (!s.isFile()) |
| 36 | + return null |
| 37 | + return { abs, size: s.size, mtime: s.mtime } |
| 38 | + } |
| 39 | + catch { |
| 40 | + return null |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function resolveTarget(absDir: string, urlPath: string): Promise<ResolvedFile | null> { |
| 45 | + let cleaned = decodeURIComponent(urlPath || '/').replace(/[?#].*$/, '') |
| 46 | + if (cleaned.endsWith('/')) |
| 47 | + cleaned = cleaned.slice(0, -1) |
| 48 | + if (cleaned.startsWith('/')) |
| 49 | + cleaned = cleaned.slice(1) |
| 50 | + |
| 51 | + const abs = normalize(join(absDir, cleaned)) |
| 52 | + if (abs !== absDir && !abs.startsWith(absDir + sep)) |
| 53 | + return null |
| 54 | + |
| 55 | + const direct = await statFile(abs) |
| 56 | + if (direct) |
| 57 | + return direct |
| 58 | + |
| 59 | + // Directory → index.html |
| 60 | + try { |
| 61 | + const s = await stat(abs) |
| 62 | + if (s.isDirectory()) { |
| 63 | + const candidate = await statFile(join(abs, 'index.html')) |
| 64 | + if (candidate) |
| 65 | + return candidate |
| 66 | + } |
| 67 | + } |
| 68 | + catch { |
| 69 | + // not found / not a directory — continue |
| 70 | + } |
| 71 | + |
| 72 | + // SPA fallback for extensionless paths |
| 73 | + if (!/\.[a-z0-9]+$/i.test(cleaned)) { |
| 74 | + const fallback = await statFile(join(absDir, 'index.html')) |
| 75 | + if (fallback) |
| 76 | + return fallback |
| 77 | + } |
| 78 | + |
| 79 | + return null |
| 80 | +} |
| 81 | + |
| 82 | +export async function GET(request: Request): Promise<Response> { |
| 83 | + await ensureMinimalNextDevframeHub() |
| 84 | + |
| 85 | + const pathname = new URL(request.url).pathname |
| 86 | + const hit = getStaticMount(pathname) |
| 87 | + if (!hit) |
| 88 | + return new Response(null, { status: 404 }) |
| 89 | + |
| 90 | + const file = await resolveTarget(resolve(hit.distDir), hit.relative) |
| 91 | + if (!file) |
| 92 | + return new Response(null, { status: 404 }) |
| 93 | + |
| 94 | + return new Response(Readable.toWeb(createReadStream(file.abs)) as ReadableStream, { |
| 95 | + status: 200, |
| 96 | + headers: { |
| 97 | + 'Content-Type': CONTENT_TYPES[extname(file.abs).toLowerCase()] ?? 'application/octet-stream', |
| 98 | + 'Content-Length': String(file.size), |
| 99 | + 'Last-Modified': file.mtime.toUTCString(), |
| 100 | + 'Cache-Control': 'no-store', |
| 101 | + }, |
| 102 | + }) |
| 103 | +} |
0 commit comments