holocron-trask-api / server.mjs
th3w1zard1's picture
Deploy from community-bots@6d7cf514f78b8f695b451f40a2aad50eca8c579c
d856122 verified
/**
* Deprecated HF mirror — bundled reference Q&A removed. Deploy holocron-trask-http for live research.
*/
import { createServer } from 'node:http';
function normalizeCorsOrigin(origin) {
return origin?.trim() ? origin.trim() : '*';
}
function jsonResponse(status, body, origin) {
const payload = JSON.stringify(body);
return {
status,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': String(Buffer.byteLength(payload)),
'Access-Control-Allow-Origin': normalizeCorsOrigin(origin),
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Trask-Api-Key',
Vary: 'Origin',
},
body: payload,
};
}
const LIVE_RESEARCH_REQUIRED = {
error:
'Bundled reference answers are disabled on this Space. Use OpenKotOR/holocron-trask-http (full trask-http-server) as TRASK_RESEARCHWIZARD_BASE_URL.',
};
async function handleRequest(request) {
const origin = request.headers.get('origin');
const url = new URL(request.url);
if (request.method === 'OPTIONS') {
return {
status: 204,
headers: {
'Access-Control-Allow-Origin': normalizeCorsOrigin(origin),
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Trask-Api-Key',
Vary: 'Origin',
},
body: '',
};
}
if (url.pathname === '/' && request.method === 'GET') {
return {
status: 200,
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
body: 'Holocron Trask API — live research only (bundled references removed).\n',
};
}
if (url.pathname === '/healthz' && request.method === 'GET') {
return jsonResponse(200, { ok: true, mode: 'live-trask-http-required', bundledReferenceApi: false }, origin);
}
if (url.pathname.startsWith('/reference') || url.pathname.startsWith('/api/trask')) {
return jsonResponse(503, LIVE_RESEARCH_REQUIRED, origin);
}
return jsonResponse(404, { error: 'Not found' }, origin);
}
const port = Number(process.env.PORT || 7860);
createServer(async (req, res) => {
const request = new Request(`http://${req.headers.host ?? 'localhost'}${req.url ?? '/'}`, {
method: req.method,
headers: req.headers,
body: req.method === 'GET' || req.method === 'HEAD' ? undefined : req,
duplex: 'half',
});
const response = await handleRequest(request);
res.writeHead(response.status, response.headers);
res.end(response.body);
}).listen(port, () => {
console.log(`holocron-trask-api stub listening on ${port}`);
});