site-serve honours a rewrite's host condition, as Vercel does (the explorer host's root rewrite had turned the local home into /explorer and the scene parity gate found no scene on the home fold)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
igneum-labs 2026-10-08 10:51:23 +00:00
parent 5fc3d70ef8
commit 23f286d819

View file

@ -16,7 +16,10 @@ if (!process.env.DATABASE_URL) {
try { const m = /^DATABASE_URL=(.*)$/m.exec(readFileSync(`${homedir()}/.config/igneum/env`, 'utf8')); if (m) process.env.DATABASE_URL = m[1].trim().replace(/^['"]|['"]$/g, ''); } catch { }
}
const cfg = JSON.parse(readFileSync(join(site, 'vercel.json'), 'utf8'));
const rewrites = (cfg.rewrites || []).map(r => ({ re: new RegExp('^' + r.source.replace(/:[a-z]+/g, '[^/]+') + '$'), to: r.destination }));
// a rewrite with a host condition (vercel.json `has: [{type: 'host', value}]`, the explorer host's root since 8 October 2026) applies only
// when the request's Host matches, as on Vercel; without this the explorer's host rewrite turned the local "/" into /explorer and the
// scene parity gate found no scene on the home fold
const rewrites = (cfg.rewrites || []).map(r => ({ re: new RegExp('^' + r.source.replace(/:[a-z]+/g, '[^/]+') + '$'), to: r.destination, host: ((r.has || []).find(h => h.type === 'host') || {}).value || null }));
const TYPES = { '.html': 'text/html; charset=utf-8', '.css': 'text/css', '.js': 'text/javascript', '.mjs': 'text/javascript', '.json': 'application/json', '.png': 'image/png', '.ico': 'image/x-icon', '.svg': 'image/svg+xml', '.woff2': 'font/woff2', '.webp': 'image/webp', '.txt': 'text/plain', '.xml': 'application/xml', '.webmanifest': 'application/manifest+json' };
const handlers = new Map();
async function api(name) {
@ -29,7 +32,8 @@ function shim(res) {
}
createServer(async (req, res) => {
const url = new URL(req.url, 'http://x'); let p = decodeURIComponent(url.pathname);
for (const rw of rewrites) if (rw.re.test(p)) { p = rw.to; break; }
const host = String(req.headers.host || '').replace(/:\d+$/, '');
for (const rw of rewrites) { if (rw.host && rw.host !== host) continue; if (rw.re.test(p)) { p = rw.to; break; } }
if (p.startsWith('/api/')) {
const name = p.slice(5).replace(/[^a-z0-9_-]/gi, '');
if (!existsSync(join(site, 'api', name + '.mjs'))) { res.statusCode = 404; return res.end('no such function'); }