/** * 服务端 HTTP API 公共工具 * * 为什么不用 RPC(connection.rpc.intercept('/api')): * DSH 的 /api 通道只能有一个 interceptor(api-gateway 已占用), * 再 intercept 会抛 "already has an interceptor" 导致插件 apply 失败。 * 因此改用 webServer 自开路由(与 dsh-better-sidebar 的 /sidebar/api 同模式)。 */ export const API_PREFIX = '/odl/api/'; /** 响应 JSON */ export function writeJson(res, status, data) { res.writeHead(status, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(data)); } /** 读取 JSON body */ export async function readJsonBody(req) { const chunks = []; for await (const chunk of req) chunks.push(chunk); const raw = Buffer.concat(chunks).toString('utf8'); if (!raw) return {}; try { return JSON.parse(raw); } catch { return {}; } } /** 从激活配置解析 baseUrl(连接列表为空时回退数据源当前 baseUrl) */ export function resolveActiveBaseUrl(settings, dataSource) { const state = getQmtConnections(settings); const active = getActiveQmtConnection(state); return String(active?.baseUrl ?? dataSource?.baseUrl ?? '').replace(/\/+$/, ''); } // 延迟引入 settings 函数避免循环依赖 import { getQmtConnections, getActiveQmtConnection, } from '../settings.js';