// Изменено: 2026-03-07 // HTTP-обёртка для serverless функций на Node.js 20. // Загружает handler.js из /app/function/ и вызывает handle(event) на каждый запрос. // Соглашение: handler.js должен экспортировать async функцию handle(event). // // Пример handler.js: // exports.handle = async (event) => { // return { message: `Hello, ${event.name || 'World'}!` }; // }; 'use strict'; const http = require('http'); const path = require('path'); const HANDLER_PATH = '/app/function/handler.js'; const PORT = 8080; // Загружаем модуль пользователя один раз при старте — не на каждый запрос let userHandle; try { const mod = require(HANDLER_PATH); if (typeof mod.handle !== 'function') { throw new Error('handler.js must export a handle(event) function'); } userHandle = mod.handle; } catch (err) { console.error('Failed to load handler:', err.message); process.exit(1); } const server = http.createServer(async (req, res) => { // Health check — используется readinessProbe оператора if (req.method === 'GET' && req.url === '/health') { return sendJSON(res, 200, { status: 'ok' }); } // Читаем тело запроса let body = ''; req.on('data', chunk => { body += chunk; }); req.on('end', async () => { let event = {}; if (body) { try { event = JSON.parse(body); } catch { // Не JSON — передаём как строку, не ломаем вызов event = { body }; } } try { const result = await userHandle(event); sendJSON(res, 200, result); } catch (err) { console.error('Handler error:', err); sendJSON(res, 500, { error: err.message }); } }); }); function sendJSON(res, status, data) { const body = JSON.stringify(data); res.writeHead(status, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body), }); res.end(body); } server.listen(PORT, '0.0.0.0', () => { console.log(`sless runtime (nodejs20) listening on :${PORT}`); });