diff --git a/package.json b/package.json index 6bedcf6..0be3c5c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "bun test", "typecheck": "tsc --noEmit", "check:boundaries": "bun scripts/check-boundaries.ts", - "build": "bun build src/index.ts --target node --outdir dist && bun build bin/filaxis.ts --target node --outfile dist/bin/filaxis.js && bun build bin/filaxis-mcp.ts --target node --outfile dist/bin/filaxis-mcp.js && bunx tsc --emitDeclarationOnly --declaration --outDir dist --target es2022 --module esnext --moduleResolution bundler --skipLibCheck src/index.ts" + "build": "bun build src/index.ts --target node --outdir dist && bun build bin/filaxis.ts --target node --outfile dist/bin/filaxis.js && bun build bin/filaxis-mcp.ts --target node --outfile dist/bin/filaxis-mcp.js && bunx tsc --emitDeclarationOnly --declaration --rootDir src --outDir dist --target es2022 --module esnext --moduleResolution bundler --skipLibCheck src/index.ts" }, "devDependencies": { "@types/bun": "latest", diff --git a/src/daemon-server.ts b/src/daemon-server.ts index eba055b..a7fd142 100644 --- a/src/daemon-server.ts +++ b/src/daemon-server.ts @@ -334,8 +334,7 @@ export class DaemonServer { try { payload = await readJsonBody(req); } catch (err) { - const status = err instanceof HttpStatusError ? err.statusCode : 400; - this.sendJson(res, status, { ok: false, error: err instanceof Error ? err.message : String(err) }); + this.sendJsonBodyError(res, err); return; } const method = typeof payload.method === "string" ? payload.method : ""; @@ -422,8 +421,7 @@ export class DaemonServer { try { body = await readRawJsonBody(req); } catch (err) { - const status = err instanceof HttpStatusError ? err.statusCode : 400; - this.sendJson(res, status, { ok: false, error: err instanceof Error ? err.message : String(err) }); + this.sendJsonBodyError(res, err); return; } @@ -450,6 +448,14 @@ export class DaemonServer { } } + private sendJsonBodyError(res: http.ServerResponse, err: unknown): void { + if (err instanceof HttpStatusError) { + this.sendJson(res, err.statusCode, { ok: false, error: err.message }); + return; + } + this.sendJson(res, 400, { ok: false, error: "读取 JSON 请求体失败" }); + } + private sendJson(res: http.ServerResponse, status: number, body: unknown): void { if (res.writableEnded) return; const json = JSON.stringify(body); @@ -1039,8 +1045,8 @@ async function readRawJsonBody(req: http.IncomingMessage): Promise { if (!raw) throw new HttpStatusError(400, "空 JSON 请求体"); try { return JSON.parse(raw); - } catch (e) { - throw new HttpStatusError(400, `JSON 解析失败: ${(e as Error).message}`); + } catch { + throw new HttpStatusError(400, "JSON 解析失败"); } } diff --git a/tests/daemon.test.ts b/tests/daemon.test.ts index ef49a3f..13496c8 100644 --- a/tests/daemon.test.ts +++ b/tests/daemon.test.ts @@ -200,6 +200,24 @@ describe("DaemonServer & DaemonClient RPC", () => { } }); + test("returns stable validation messages for invalid HTTP JSON bodies", async () => { + const srv = new DaemonServer({ writeFiles: false }); + try { + const addr = await srv.start(); + for (const path of ["/rpc", "/mcp/message"]) { + const res = await fetch(`${addr}${path}`, { + method: "POST", + headers: { "Content-Type": "application/json", Authorization: `Bearer ${srv.token}` }, + body: "{invalid JSON", + }); + expect(res.status).toBe(400); + expect(await res.json()).toEqual({ ok: false, error: "JSON 解析失败" }); + } + } finally { + await srv.stop(); + } + }); + test("shares a pending listen and does not reopen a stopped server instance", async () => { const srv = new DaemonServer({ writeFiles: false }); const starting = srv.start();