从 ssw-mvp/boot/ssw-tdc(Spring Boot/JNPF,37 Controller)剥离重底座的本地优先实现: - axum 单进程承担控制面/数据面/静态托管/TDC→TDS HTTP 契约网关 - pglite-rs 0.2 内嵌原生 PostgreSQL(进程内,非浏览器 WASM),19 张 tdc_* 表 - 静态 HTML + 原生 JS 控制台:schema 自省驱动 CRUD + 按表生命周期动作 - 红线:TDC↔TDS 只走 HTTP 契约,未配 SSW_TDS_BASE_URL 时 Noop 降级 - AGENTS.md 记录架构红线、命令速查与踩坑清单;docs/ 记录跨方契约细节
85 lines
4.2 KiB
JavaScript
85 lines
4.2 KiB
JavaScript
#!/usr/bin/env node
|
||
// 最小但忠实的 TDS(可信数据空间运营方)契约桩。
|
||
// 仅实现 ssw-tdc 经 TdcTdsClient 实际依赖的端点(/api/ssw/tds/Bridge/* + Connector/Register + NodeReport),
|
||
// 用于让 Rust 连接器在本地拥有真实 HTTP 对端,进行跨方契约调试/优化。
|
||
// 非产品代码;零依赖,仅用 Node 内置 http。
|
||
const http = require('http');
|
||
|
||
const PORT = Number(process.env.PORT || 30100);
|
||
const connectors = new Map(); // code -> record
|
||
|
||
function json(res, status, obj) {
|
||
const body = JSON.stringify(obj);
|
||
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) });
|
||
res.end(body);
|
||
}
|
||
|
||
function hash(s) {
|
||
// 非加密占位哈希,仅用于演示链/存证
|
||
let h = 0x811c9dc5;
|
||
for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 0x01000193); }
|
||
return '0x' + (h >>> 0).toString(16).padStart(8, '0') + (h >>> 0).toString(16).padStart(8, '0');
|
||
}
|
||
|
||
const server = http.createServer((req, res) => {
|
||
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
|
||
const p = url.pathname;
|
||
const method = req.method;
|
||
const token = req.headers['x-internal-token'] || '';
|
||
let raw = '';
|
||
req.on('data', (c) => (raw += c));
|
||
req.on('end', () => {
|
||
let body = {};
|
||
try { if (raw) body = JSON.parse(raw); } catch (_) {}
|
||
const log = `→ TDS ${method} ${p} token=${token ? 'set' : 'none'} bodyLen=${raw.length}`;
|
||
console.log(log);
|
||
|
||
// ---- Connector 注册(TDC 注册入口)----
|
||
if (p === '/api/ssw/tds/Connector/Register' && method === 'POST') {
|
||
const code = body.connectorCode || ('TDC-' + Date.now());
|
||
const rec = {
|
||
connectorId: 'conn-' + hash(code),
|
||
connectorCode: code,
|
||
name: body.name,
|
||
orgName: body.orgName,
|
||
publicKey: body.publicKey,
|
||
endpoint: body.endpoint,
|
||
registerStatus: 'APPROVED',
|
||
approvedAt: new Date().toISOString(),
|
||
};
|
||
connectors.set(code, rec);
|
||
console.log(` ✓ 注册连接器 ${code} -> ${rec.connectorId}`);
|
||
return json(res, 200, rec);
|
||
}
|
||
|
||
// ---- Bridge 只读概览(GET 与 POST 都支持)----
|
||
const bridgeMatch = p.match(/^\/api\/ssw\/tds\/Bridge\/(Contract|Connector|DataProduct)\/(.+)$/);
|
||
if (bridgeMatch && (method === 'GET' || method === 'POST')) {
|
||
const kind = bridgeMatch[1];
|
||
const id = decodeURIComponent(bridgeMatch[2]);
|
||
const sample = {
|
||
Contract: { id, name: '示例合约', status: 'ACTIVE', parties: ['provider', 'consumer'], spaceId: 'sp-dev' },
|
||
Connector: { id, code: id, name: '示例连接器', status: 'ONLINE', registerStatus: 'APPROVED' },
|
||
DataProduct: { id, name: '示例数据产品', status: 'PUBLISHED', ownerConnector: 'TDC-demo' },
|
||
}[kind];
|
||
return json(res, 200, sample);
|
||
}
|
||
|
||
// ---- Bridge 批量审计(POST {bizIds:[...]})----
|
||
if (p === '/api/ssw/tds/Bridge/AuditChain' && method === 'POST') return json(res, 200, { bizIds: body.bizIds || [], chainHash: hash(JSON.stringify(body.bizIds || [])), height: 1024 });
|
||
if (p === '/api/ssw/tds/Bridge/ChainEvidence' && method === 'POST') return json(res, 200, { bizIds: body.bizIds || [], evidences: (body.bizIds || []).map((b) => ({ bizId: b, txHash: hash(b), confirmed: true })) });
|
||
if (p === '/api/ssw/tds/Bridge/AuditRecord' && method === 'POST') return json(res, 200, { bizIds: body.bizIds || [], records: (body.bizIds || []).map((b) => ({ bizId: b, actor: 'tdc', ts: new Date().toISOString() })) });
|
||
if (p === '/api/ssw/tds/Bridge/AuditTrail' && method === 'POST') { console.log(' ✓ 留痕上报', JSON.stringify(body).slice(0, 120)); return json(res, 200, { ok: true }); }
|
||
|
||
// ---- NodeReport(TDC 心跳/上报)----
|
||
if (p === '/api/ssw/tds/NodeReport' && method === 'POST') return json(res, 200, { ok: true, reportedAt: new Date().toISOString() });
|
||
|
||
console.log(` ✗ 404 未实现: ${method} ${p}`);
|
||
json(res, 404, { error: 'not implemented by tds-stub', path: p });
|
||
});
|
||
});
|
||
|
||
server.listen(PORT, '127.0.0.1', () => {
|
||
console.log(`[tds-stub] 监听 http://127.0.0.1:${PORT} (实现 /api/ssw/tds/Bridge/*, Connector/Register, NodeReport)`);
|
||
});
|