Support emulator HEAD requests

This commit is contained in:
Christian Johannesen 2026-10-07 21:53:53 +02:00
parent d828652e9a
commit fbaf88941c

View file

@ -8,14 +8,14 @@ const programDirectory = resolve(process.env.PROGRAM_DIRECTORY || fileURLToPath(
const manifestPath = join(programDirectory, 'programs.json'); const manifestPath = join(programDirectory, 'programs.json');
const publicDirectory = fileURLToPath(new URL('./public/', import.meta.url)); const publicDirectory = fileURLToPath(new URL('./public/', import.meta.url));
function sendJson(response, status, value) { function sendJson(response, status, value, headOnly = false) {
const body = JSON.stringify(value); const body = JSON.stringify(value);
response.writeHead(status, { response.writeHead(status, {
'Content-Type': 'application/json; charset=utf-8', 'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(body), 'Content-Length': Buffer.byteLength(body),
'Cache-Control': 'no-store' 'Cache-Control': 'no-store'
}); });
response.end(body); response.end(headOnly ? undefined : body);
} }
function fetchPrograms() { function fetchPrograms() {
@ -37,7 +37,7 @@ function fetchPrograms() {
}); });
} }
function sendProgram(response, programPath) { function sendProgram(response, programPath, headOnly = false) {
const programs = fetchPrograms(); const programs = fetchPrograms();
if (!programs.some(program => program.path === programPath)) { if (!programs.some(program => program.path === programPath)) {
sendJson(response, 404, { error: 'Program not found' }); sendJson(response, 404, { error: 'Program not found' });
@ -51,10 +51,11 @@ function sendProgram(response, programPath) {
'Content-Disposition': `inline; filename="${programPath.split('/').at(-1)}"`, 'Content-Disposition': `inline; filename="${programPath.split('/').at(-1)}"`,
'Cache-Control': 'no-store' 'Cache-Control': 'no-store'
}); });
createReadStream(filePath).pipe(response); if (headOnly) response.end();
else createReadStream(filePath).pipe(response);
} }
function sendSource(response, programPath) { function sendSource(response, programPath, headOnly = false) {
const program = fetchPrograms().find(entry => entry.path === programPath); const program = fetchPrograms().find(entry => entry.path === programPath);
if (!program?.source) { if (!program?.source) {
sendJson(response, 404, { error: 'Program source not found' }); sendJson(response, 404, { error: 'Program source not found' });
@ -66,10 +67,11 @@ function sendSource(response, programPath) {
'Content-Length': statSync(filePath).size, 'Content-Length': statSync(filePath).size,
'Cache-Control': 'no-store' 'Cache-Control': 'no-store'
}); });
createReadStream(filePath).pipe(response); if (headOnly) response.end();
else createReadStream(filePath).pipe(response);
} }
function serveStatic(response, pathname) { function serveStatic(response, pathname, headOnly = false) {
const requested = pathname === '/' ? 'index.html' : pathname.slice(1); const requested = pathname === '/' ? 'index.html' : pathname.slice(1);
const filePath = normalize(join(publicDirectory, requested)); const filePath = normalize(join(publicDirectory, requested));
if (!filePath.startsWith(publicDirectory) || !existsSync(filePath) || !statSync(filePath).isFile()) { if (!filePath.startsWith(publicDirectory) || !existsSync(filePath) || !statSync(filePath).isFile()) {
@ -81,23 +83,26 @@ function serveStatic(response, pathname) {
'Content-Length': statSync(filePath).size, 'Content-Length': statSync(filePath).size,
'Cache-Control': 'no-cache' 'Cache-Control': 'no-cache'
}); });
createReadStream(filePath).pipe(response); if (headOnly) response.end();
else createReadStream(filePath).pipe(response);
return true; return true;
} }
createServer(async (request, response) => { createServer(async (request, response) => {
try { try {
const url = new URL(request.url, `http://${request.headers.host || 'localhost'}`); const url = new URL(request.url, `http://${request.headers.host || 'localhost'}`);
if (request.method !== 'GET') { const headOnly = request.method === 'HEAD';
if (request.method !== 'GET' && !headOnly) {
response.setHeader('Allow', 'GET, HEAD');
sendJson(response, 405, { error: 'Method not allowed' }); sendJson(response, 405, { error: 'Method not allowed' });
} else if (url.pathname === '/api/programs') { } else if (url.pathname === '/api/programs') {
sendJson(response, 200, { programs: fetchPrograms() }); sendJson(response, 200, { programs: fetchPrograms() }, headOnly);
} else if (url.pathname === '/api/program') { } else if (url.pathname === '/api/program') {
sendProgram(response, url.searchParams.get('path') || ''); sendProgram(response, url.searchParams.get('path') || '', headOnly);
} else if (url.pathname === '/api/source') { } else if (url.pathname === '/api/source') {
sendSource(response, url.searchParams.get('path') || ''); sendSource(response, url.searchParams.get('path') || '', headOnly);
} else if (!serveStatic(response, url.pathname)) { } else if (!serveStatic(response, url.pathname, headOnly)) {
sendJson(response, 404, { error: 'Not found' }); sendJson(response, 404, { error: 'Not found' }, headOnly);
} }
} catch (error) { } catch (error) {
console.error(error); console.error(error);