Files
sless/runtimes/nodejs20/server.js
T
“Naeel” e6abc490cd fix: imagePullSecrets, SLESS_ENTRYPOINT, registry secret propagation
- function_controller: добавить RegistrySecret + OperatorNamespace, копировать
  sless-registry-auth в sless-fn-<ns>, выставлять imagePullSecrets в Deployment,
  также обновлять imagePullSecrets при reconcile
- functionjob_controller: fnEnvVars включает SLESS_ENTRYPOINT, runner читает его
- server.js + server.py: читать SLESS_ENTRYPOINT вместо hardcoded handler.js/py
- rbac.yaml: добавить права на secrets
- operator.yaml: v0.1.8
- main.go: передать RegistrySecret + OperatorNamespace в FunctionReconciler
2026-03-08 11:15:48 +04:00

76 lines
2.6 KiB
JavaScript

// Изменено: 2026-03-08
// HTTP-обёртка для serverless функций на Node.js 20.
// Загружает модуль из SLESS_ENTRYPOINT или handler.js по умолчанию.
// Формат SLESS_ENTRYPOINT: "module-name.functionName" (например: handler-http.handle)
'use strict';
const http = require('http');
const path = require('path');
const PORT = 8080;
// Разбираем SLESS_ENTRYPOINT="module.func" → файл и имя функции.
// Fallback: handler.js + handle — для обратной совместимости.
const entrypoint = process.env.SLESS_ENTRYPOINT || 'handler.handle';
const dotIdx = entrypoint.lastIndexOf('.');
const moduleName = dotIdx >= 0 ? entrypoint.slice(0, dotIdx) : entrypoint;
const funcName = dotIdx >= 0 ? entrypoint.slice(dotIdx + 1) : 'handle';
const HANDLER_PATH = path.join('/app/function', moduleName);
// Загружаем модуль пользователя один раз при старте — не на каждый запрос
let userHandle;
try {
const mod = require(HANDLER_PATH);
if (typeof mod[funcName] !== 'function') {
throw new Error(`${moduleName} must export a ${funcName}(event) function`);
}
userHandle = mod[funcName];
} 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}`);
});