Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions src/daemon-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : "";
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -1039,8 +1045,8 @@ async function readRawJsonBody(req: http.IncomingMessage): Promise<unknown> {
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 解析失败");
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down