167 lines
4.2 KiB
JavaScript
167 lines
4.2 KiB
JavaScript
import http from 'node:http';
|
|
|
|
const moduleId = 'license-limits-core';
|
|
const port = Number.parseInt(process.env.PORT || '8787', 10);
|
|
const capabilities = ['licenseLimits'];
|
|
const allowedActions = new Set([
|
|
'licenseLimits:check',
|
|
'licenseLimits:read',
|
|
'licenseLimits:validate',
|
|
'licenseLimits:enforce',
|
|
]);
|
|
|
|
function sendJson(res, statusCode, payload) {
|
|
const body = JSON.stringify(payload);
|
|
res.writeHead(statusCode, {
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
'Content-Length': Buffer.byteLength(body),
|
|
});
|
|
res.end(body);
|
|
}
|
|
|
|
function readJsonBody(req) {
|
|
return new Promise((resolve, reject) => {
|
|
let body = '';
|
|
|
|
req.on('data', (chunk) => {
|
|
body += chunk;
|
|
if (body.length > 1024 * 1024) {
|
|
req.destroy();
|
|
reject(new Error('request_body_too_large'));
|
|
}
|
|
});
|
|
|
|
req.on('end', () => {
|
|
if (!body.trim()) {
|
|
resolve({});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
resolve(JSON.parse(body));
|
|
} catch {
|
|
reject(new Error('invalid_json'));
|
|
}
|
|
});
|
|
|
|
req.on('error', reject);
|
|
});
|
|
}
|
|
|
|
function sendAuthorization(res, action) {
|
|
const allowed = typeof action === 'string' && allowedActions.has(action);
|
|
sendJson(res, allowed ? 200 : 403, {
|
|
moduleId,
|
|
allowed,
|
|
action,
|
|
capabilities,
|
|
});
|
|
}
|
|
|
|
function normalizeLimitValue(value) {
|
|
return Number.isInteger(value) && value >= -1 ? value : null;
|
|
}
|
|
|
|
function buildLimitDecision(body) {
|
|
const type = typeof body.type === 'string' ? body.type : '';
|
|
if (type !== 'users' && type !== 'materials') {
|
|
return { valid: false, error: 'invalid_limit_type' };
|
|
}
|
|
|
|
const current = Number.isInteger(body.current) && body.current >= 0 ? body.current : null;
|
|
if (current === null) {
|
|
return { valid: false, error: 'invalid_current_count' };
|
|
}
|
|
|
|
const limits = body.limits && typeof body.limits === 'object' ? body.limits : {};
|
|
const maxUsers = normalizeLimitValue(limits.maxUsers);
|
|
const maxMaterials = normalizeLimitValue(limits.maxMaterials);
|
|
if (maxUsers === null || maxMaterials === null) {
|
|
return { valid: false, error: 'invalid_limits' };
|
|
}
|
|
|
|
const max = type === 'users' ? maxUsers : maxMaterials;
|
|
return {
|
|
valid: true,
|
|
type,
|
|
current,
|
|
max,
|
|
allowed: max === -1 || current < max,
|
|
};
|
|
}
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const url = new URL(req.url || '/', `http://${req.headers.host || '127.0.0.1'}`);
|
|
|
|
if (req.method === 'GET' && url.pathname === '/health') {
|
|
sendJson(res, 200, { moduleId, status: 'healthy' });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && url.pathname === '/status') {
|
|
sendJson(res, 200, {
|
|
moduleId,
|
|
status: 'ready',
|
|
capabilities,
|
|
note: '具体人数和素材数上限以验证系统下发的实例授权为准',
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && url.pathname === '/capabilities') {
|
|
sendJson(res, 200, {
|
|
moduleId,
|
|
capabilities,
|
|
actions: Array.from(allowedActions),
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && url.pathname === '/limits') {
|
|
sendJson(res, 200, {
|
|
moduleId,
|
|
status: 'entitlement_required',
|
|
error: '此模块只提供限额能力合同,具体限额必须来自已签名的实例授权',
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && url.pathname === '/limits/enforce') {
|
|
readJsonBody(req)
|
|
.then((body) => {
|
|
const decision = buildLimitDecision(body);
|
|
if (!decision.valid) {
|
|
sendJson(res, 400, { moduleId, allowed: false, error: decision.error });
|
|
return;
|
|
}
|
|
|
|
sendJson(res, 200, {
|
|
moduleId,
|
|
capability: 'licenseLimits',
|
|
...decision,
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
sendJson(res, 400, { moduleId, allowed: false, error: error.message });
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && url.pathname === '/authorize') {
|
|
readJsonBody(req)
|
|
.then((body) => {
|
|
sendAuthorization(res, body.action);
|
|
})
|
|
.catch((error) => {
|
|
sendJson(res, 400, { moduleId, allowed: false, error: error.message });
|
|
});
|
|
return;
|
|
}
|
|
|
|
sendJson(res, 404, { moduleId, error: '接口不存在' });
|
|
});
|
|
|
|
server.listen(port, '0.0.0.0', () => {
|
|
console.log(`${moduleId} listening on ${port}`);
|
|
});
|