INNER CODE UNIT · JavaScript
timer
wangshengithub/staticshield · src/server.js:88
var timer = setTimeout(() => { finish(reject, new Error('请求超时')); req.destroy(); }, 30000);
req.on('data', c => { size += c.length; if (size > maxBytes) { finish(reject, new Error('请求体过大(上限 ' + Math.round(maxBytes / 1048576) + 'MB)')); req.destroy(); return; } chunks.push(c); });
req.on('end', () => { try { var raw = Buffer.concat(chunks).toString('utf8'); finish(resolve, raw ? JSON.parse(raw) : {}); } catch (e) { finish(reject, new Error('JSON 解析失败')); } });
req.on('error', e => finish(reject, e));
});
}
function sendJson(res, code, obj) { var body = JSON.stringify(obj); res.writeHead(code, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' }); res.end(body); }
function sendHtml(res, code, html) { res.writeHead(code, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' }); res.end(html); }
function makeHandler(guiHtml, ctx) {
return async function handle(req, res) {
try {
var url = (req.url || '/').split('?')[0];
if (ctx && ctx.port) {
var host = String(req.headers.host || ''), p = ctx.port;
if (host !== '127.0.0.1:' + p && host !== 'localhost:' + p && host !== '[::1]:' + p) return sendJson(res, 403, { ok: false, error: 'Forbidden' });
}
if (req.method === 'GET' && (url === '/' || url === '/index.html')) {
if (!guiHtml) return sendJson(res, 503, { ok: false, error: 'GUI 未生成,请先运行 node src/build-gui.js' });