Skip to content

Commit 8a661bb

Browse files
committed
refactor(proxy): 文档纠偏 + inflateWithCap 去重 + userId 门控对齐 + parseSSEChunk 最小放宽
- CL-16: handler.ts 顶部注释重写为 v2.6 现实(两套餐均 Anthropic 上游, OpenAI 客户端 O→A 翻译,Anthropic 客户端原生 passthrough); passthroughResponse docstring 改为如实描述 accept-encoding 转发; claim/client.ts 引用去掉不存在的 NOTEPAD 章节名 - R2-11: manager.ts 'refreshing if necessary' 过度承诺改为如实描述 (凭据过期由上游 401 暴露,守卫保留备未来填充 expiresAt) - CL-17: 抽共享 src/proxy/inflate.ts inflateWithCap(DecompressionStream + 上限 + cancel + releaseLock);proxy/handler.ts 与 async/handler.ts 两处解压循环改调,64MiB/4MiB 上限与错误契约(throw vs errorResponse) 保留在各自调用方 - CL-15: /v1/responses userId 门控对齐 handler.ts(startPlan 不注入 metadata.user_id),留档 bundle 为 presence-gated 无套餐概念 - R2-18: parseSSEChunk data: 前缀放宽(至多剥一个前导空格,'data: x' 输出逐字节不变);帧分割容忍 \r\n\r\n(导出 SSE_FRAME_SPLIT,与 3.11.2 bundle Kxo 正则一致),anthropicSseToOpenaiSse 与 async 流适配器 的缓冲分割同步切换——CRLF 流不再永久缓冲;:496 第二解析器与其余解析点 不动(TriDefender#32)
1 parent 477b7a4 commit 8a661bb

10 files changed

Lines changed: 177 additions & 73 deletions

File tree

‎src/async/handler.ts‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { Credential } from "../auth/types.js";
2525
import { credentialString } from "../auth/types.js";
2626
import { errorResponse } from "../proxy/handler.js";
2727
import { transformRequestBody } from "../proxy/body-transformer.js";
28+
import { inflateWithCap } from "../proxy/inflate.js";
2829
import { translateRequestOpenAIToAnthropic, translateResponseAnthropicToOpenAI } from "../translator/openai-to-anthropic.js";
2930
import { anthropicSseToOpenaiSseWithKeepalive } from "./openai-stream-adapter.js";
3031
import type { AnthropicMessagesRequest, OpenAIChatRequest, AnthropicMessagesResponse } from "../translator/types.js";
@@ -104,33 +105,14 @@ async function readBody(req: Request): Promise<{ ok: true; body: string } | { ok
104105
const encoding = req.headers.get("content-encoding")?.toLowerCase().trim() ?? "";
105106
let bytes: Uint8Array = Buffer.concat(chunks);
106107
if (encoding === "gzip" || encoding === "x-gzip") {
107-
const gunzip = new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>;
108-
const source = new ReadableStream<Uint8Array>({
109-
start(controller) {
110-
controller.enqueue(bytes);
111-
controller.close();
112-
},
113-
});
114-
const inflateReader = source.pipeThrough(gunzip).getReader();
115-
const inflated: Uint8Array[] = [];
116-
let inflatedTotal = 0;
117-
try {
118-
for (;;) {
119-
const { done, value } = await inflateReader.read();
120-
if (done) break;
121-
inflatedTotal += value.byteLength;
122-
if (inflatedTotal > MAX_REQUEST_BODY_BYTES) {
123-
await inflateReader.cancel().catch(() => {});
124-
return { ok: false, response: errorResponse(413, "request_too_large", `decompressed body exceeds ${MAX_REQUEST_BODY_BYTES} byte cap`) };
125-
}
126-
inflated.push(value);
108+
const inflated = await inflateWithCap(bytes, MAX_REQUEST_BODY_BYTES);
109+
if (!inflated.ok) {
110+
if (inflated.reason === "too_large") {
111+
return { ok: false, response: errorResponse(413, "request_too_large", `decompressed body exceeds ${MAX_REQUEST_BODY_BYTES} byte cap`) };
127112
}
128-
bytes = Buffer.concat(inflated);
129-
} catch {
130113
return { ok: false, response: errorResponse(400, "invalid_request_error", "could not decompress gzip request body") };
131-
} finally {
132-
inflateReader.releaseLock?.();
133114
}
115+
bytes = inflated.bytes;
134116
}
135117
const body = new TextDecoder().decode(bytes);
136118
if (!body || body.length === 0) {

‎src/async/openai-stream-adapter.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* or downstream clients see inconsistent IDs, missing tool arguments, and
2020
* duplicate finish reasons.
2121
*/
22-
import { initState, parseSSEChunk, translateEvent, type TranslationState, type ParsedSSE } from "../translator/sse-translator.js";
22+
import { initState, parseSSEChunk, translateEvent, SSE_FRAME_SPLIT, type TranslationState, type ParsedSSE } from "../translator/sse-translator.js";
2323

2424
export function anthropicSseToOpenaiSseWithKeepalive(
2525
upstream: ReadableStream<Uint8Array>,
@@ -60,7 +60,7 @@ export function anthropicSseToOpenaiSseWithKeepalive(
6060
return;
6161
}
6262
buffer += decoder.decode(value, { stream: true });
63-
const blocks = buffer.split("\n\n");
63+
const blocks = buffer.split(SSE_FRAME_SPLIT);
6464
buffer = blocks.pop() ?? "";
6565
for (const block of blocks) {
6666
processBlock(block, state);

‎src/auth/manager.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import type { Credential } from "./types.js";
1414
export class AuthManager {
1515
private oauthCred: Credential | null = null;
1616

17-
/** Returns the current credential, refreshing if necessary. */
17+
/**
18+
* Returns the current credential or throws when none is stored.
19+
*
20+
* There is no proactive refresh: the login flows never populate
21+
* `expiresAt`, so expiry surfaces as an upstream 401, not here. The guard
22+
* below is retained for the day a flow starts filling `expiresAt`.
23+
*/
1824
async getCredential(): Promise<Credential> {
1925
if (this.oauthCred) {
2026
if (this.oauthCred.expiresAt && Date.now() >= this.oauthCred.expiresAt) {

‎src/claim/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* carry the full identity set minus `X-ZCode-Agent` — same header set the
1818
* endpoint-routing config fetch uses for zcode.z.ai control-plane calls.
1919
*
20-
* @see _reverse/NOTEPAD.md "Manual Claim Plan" section.
20+
* @see _reverse/NOTEPAD.md (billing/claim endpoints section).
2121
*/
2222
import type { ClaimablePlan, ClaimOutcome, PlanEntitlement } from "./types.js";
2323
import { classifyClaimCode } from "./types.js";

‎src/proxy/handler.ts‎

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/**
22
* Main proxy handler — routes requests, injects auth, forwards, and streams responses.
33
*
4-
* **Translation mode**:
5-
* - coding-plan and start-plan both use an OpenAI-compatible upstream.
6-
* - Anthropic clients are translated Anthropic → OpenAI upstream → Anthropic
7-
* response. OpenAI clients are already in the upstream's native format and
8-
* pass through.
4+
* **v2.6 upstream reality (post-PR #34)**: BOTH plan tiers post an
5+
* Anthropic-format upstream — coding-plan mirrors the real ZCode client
6+
* (api.z.ai/api/anthropic → ultra via endpoint routing); start-plan posts to
7+
* zcode.z.ai's Anthropic gateway with the plan JWT. Consequently:
8+
* - OpenAI clients are translated OpenAI→Anthropic on the way up and
9+
* Anthropic→OpenAI on the way down ("translation" mode).
10+
* - Anthropic clients speak the upstream's native format — requests are
11+
* forwarded with body transforms only ("passthrough" mode,
12+
* `decompress: false`).
913
*
1014
* @see .omo/plans/zcode-proxy.md Task 6
1115
*/
@@ -40,6 +44,7 @@ import { translateRequestAnthropicToOpenAI, translateResponseOpenAIToAnthropic }
4044
import { anthropicSseToOpenaiSse, openaiSseToAnthropicSse } from "../translator/sse-translator.js";
4145
import type { OpenAIChatRequest, OpenAIChatResponse, AnthropicMessagesRequest, AnthropicMessagesResponse } from "../translator/types.js";
4246
import { dumpPhase, dumpHeaders, dumpBody, dumpEnabled } from "./dump.js";
47+
import { inflateWithCap } from "./inflate.js";
4348

4449
/** Options for the proxy handler. */
4550
export interface ProxyHandlerOptions {
@@ -508,48 +513,28 @@ export class InflatedBodyTooLargeError extends Error {
508513
}
509514

510515
async function inflateGzipBody(bytes: Uint8Array): Promise<Uint8Array> {
511-
const gunzip = new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>;
512-
const source = new ReadableStream<Uint8Array>({
513-
start(controller) {
514-
controller.enqueue(bytes);
515-
controller.close();
516-
},
517-
});
518-
const reader = source.pipeThrough(gunzip).getReader();
519-
const parts: Uint8Array[] = [];
520-
let total = 0;
521-
try {
522-
for (;;) {
523-
const { done, value } = await reader.read();
524-
if (done) break;
525-
total += value.byteLength;
526-
if (total > MAX_INFLATED_BODY_BYTES) {
527-
await reader.cancel().catch(() => {});
528-
throw new InflatedBodyTooLargeError(MAX_INFLATED_BODY_BYTES);
529-
}
530-
parts.push(value);
531-
}
532-
} catch (err) {
533-
if (err instanceof InflatedBodyTooLargeError) throw err;
534-
throw new Error(`request body is marked content-encoding: gzip but failed to decompress: ${(err as Error).message}`);
535-
} finally {
536-
reader.releaseLock?.();
516+
const result = await inflateWithCap(bytes, MAX_INFLATED_BODY_BYTES);
517+
if (!result.ok) {
518+
if (result.reason === "too_large") throw new InflatedBodyTooLargeError(MAX_INFLATED_BODY_BYTES);
519+
throw new Error(`request body is marked content-encoding: gzip but failed to decompress: ${result.detail}`);
537520
}
538-
return Buffer.concat(parts);
521+
return result.bytes;
539522
}
540523

541524
/**
542525
* Create a passthrough response that streams the upstream body to the client.
543526
* Preserves status and the allowlisted headers, and honors the client's
544527
* `Accept-Encoding` for gzip.
545528
*
546-
* The upstream request always advertises `accept-encoding: gzip` (see
547-
* `buildUpstreamHeaderPairs`), so the upstream typically returns a gzip body.
548-
* If THIS client did not advertise gzip, we decompress before forwarding and
549-
* drop the now-mismatched `content-encoding`/`content-length` headers —
550-
* otherwise clients whose HTTP stack does not auto-decompress (e.g. some
551-
* Tauri-based clients) receive raw gzip bytes and fail to parse the JSON
552-
* body with "non-JSON body" errors despite a 200 status.
529+
* The upstream request FORWARDS the client's `accept-encoding` (only
530+
* defaulting to "gzip" when the client sent none — see
531+
* `buildUpstreamHeaderPairs`), so the upstream compresses only when the
532+
* client can decode it. If THIS client did not advertise gzip but the body
533+
* arrived gzip-compressed anyway, we decompress before forwarding and drop
534+
* the now-mismatched `content-encoding`/`content-length` headers — otherwise
535+
* clients whose HTTP stack does not auto-decompress (e.g. some Tauri-based
536+
* clients) receive raw gzip bytes and fail to parse the JSON body with
537+
* "non-JSON body" errors despite a 200 status.
553538
*/
554539
function passthroughResponse(
555540
upstream: Response,

‎src/proxy/inflate.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Shared bounded gzip inflation for request bodies (audit CL-17).
3+
*
4+
* Two call sites used to carry byte-for-byte copies of this
5+
* DecompressionStream+reader+cancel loop (proxy/handler.ts and
6+
* async/handler.ts); fixes to one never reached the other. The callers keep
7+
* their own limits and error contracts — this helper only performs the
8+
* bounded inflation and reports the outcome.
9+
*/
10+
11+
export type InflateResult =
12+
| { ok: true; bytes: Uint8Array }
13+
| { ok: false; reason: "too_large" }
14+
| { ok: false; reason: "corrupt"; detail: string };
15+
16+
/**
17+
* Inflate `bytes` as gzip, reading at most `limitBytes` of DECOMPRESSED
18+
* output. The stream is cancelled and the reader lock released as soon as
19+
* the limit trips (a small wire payload cannot expand into unbounded
20+
* memory), and on any decode error.
21+
*
22+
* `too_large` carries no detail by design — callers embed their own limit in
23+
* the user-facing message.
24+
*/
25+
export async function inflateWithCap(bytes: Uint8Array, limitBytes: number): Promise<InflateResult> {
26+
const gunzip = new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>;
27+
const source = new ReadableStream<Uint8Array>({
28+
start(controller) {
29+
controller.enqueue(bytes);
30+
controller.close();
31+
},
32+
});
33+
const reader = source.pipeThrough(gunzip).getReader();
34+
const parts: Uint8Array[] = [];
35+
let total = 0;
36+
try {
37+
for (;;) {
38+
const { done, value } = await reader.read();
39+
if (done) break;
40+
total += value.byteLength;
41+
if (total > limitBytes) {
42+
await reader.cancel().catch(() => {});
43+
return { ok: false, reason: "too_large" };
44+
}
45+
parts.push(value);
46+
}
47+
return { ok: true, bytes: Buffer.concat(parts) };
48+
} catch (err) {
49+
return { ok: false, reason: "corrupt", detail: (err as Error).message };
50+
} finally {
51+
reader.releaseLock?.();
52+
}
53+
}

‎src/proxy/responses-handler.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ describe("handleResponses captcha (start-plan)", () => {
275275
expect(seen).toEqual([null]);
276276
expect(captcha.minted()).toBe(0);
277277
});
278+
279+
it("injects metadata.user_id on coding-plan but NOT on start-plan (CL-15, mirrors handler.ts)", async () => {
280+
const START_PLAN: ProxyConfig = { ...CONFIG, plan: "start-plan", provider: "bigmodel" };
281+
let codingBody = "";
282+
let startBody = "";
283+
const codingFetch = (async (request: Request): Promise<Response> => {
284+
codingBody = await request.text();
285+
return new Response(anthropicMsg("ok"), { status: 200, headers: { "content-type": "application/json" } });
286+
}) as unknown as typeof fetch;
287+
const startFetch = (async (request: Request): Promise<Response> => {
288+
startBody = await request.text();
289+
return new Response(anthropicMsg("ok"), { status: 200, headers: { "content-type": "application/json" } });
290+
}) as unknown as typeof fetch;
291+
292+
await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), { config: CONFIG, auth, fetchImpl: codingFetch });
293+
await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), { config: START_PLAN, auth, fetchImpl: startFetch });
294+
295+
expect(JSON.parse(codingBody).metadata?.user_id).toBe("u1");
296+
expect(JSON.parse(startBody).metadata?.user_id).toBeUndefined();
297+
});
278298
});
279299

280300
// CL-08: /v1/responses used to give up on the FIRST connect-level failure and

‎src/proxy/responses-handler.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,13 @@ export async function handleResponses(
172172
} catch (err) {
173173
return errorResponse(400, "translation_failed", `Chat→Anthropic translation failed: ${(err as Error).message}`);
174174
}
175+
// userId gating mirrors handler.ts: coding-plan injects the OAuth userId,
176+
// start-plan omits it (internal consistency; the bundle is presence-gated
177+
// with no plan concept — whether start-plan should carry it is not
178+
// determinable from _reverse, kept aligned with the sibling path).
175179
upstreamRequestBody = transformRequestBody(JSON.stringify(anthropicReq), {
176180
format: "anthropic",
177-
userId: cred.userId,
181+
userId: startPlan ? undefined : cred.userId,
178182
startPlan,
179183
}) ?? JSON.stringify(anthropicReq);
180184
}

‎src/translator/sse-translator.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @see .omo/plans/zcode-proxy.md Task 12
44
*/
55
import { describe, it, expect } from "bun:test";
6-
import { anthropicSseToOpenaiSse, openaiSseToAnthropicSse } from "./sse-translator.js";
6+
import { anthropicSseToOpenaiSse, openaiSseToAnthropicSse, parseSSEChunk } from "./sse-translator.js";
77

88
function makeStream(text: string): ReadableStream<Uint8Array> {
99
const encoder = new TextEncoder();
@@ -94,6 +94,47 @@ const ANTHROPIC_SSE = [
9494
'',
9595
].join('\n');
9696

97+
describe("parseSSEChunk — lenient frame/data parsing (R2-18)", () => {
98+
it("parses a data line without the optional space after the colon", () => {
99+
const parsed = parseSSEChunk('event: message_stop\ndata:{"type":"message_stop"}\n\n');
100+
expect(parsed).toHaveLength(1);
101+
expect(parsed[0].event).toBe("message_stop");
102+
expect(parsed[0].data).toEqual({ type: "message_stop" });
103+
});
104+
105+
it("keeps byte-identical output for classic 'data: x' (one space) payloads", () => {
106+
const parsed = parseSSEChunk('data: {"a":1}\n\n');
107+
expect(parsed[0].data).toEqual({ a: 1 });
108+
// two spaces after the colon: only ONE is stripped (SSE spec); the
109+
// remaining leading space is JSON whitespace, so it still parses.
110+
const twoSpaces = parseSSEChunk('data: {"a":1}\n\n');
111+
expect(twoSpaces[0].data).toEqual({ a: 1 });
112+
});
113+
114+
it("tolerates CRLF frame boundaries and CRLF line endings", () => {
115+
const crlf = 'event: message_start\r\ndata: {"type":"message_start"}\r\n\r\nevent: message_stop\r\ndata: {"type":"message_stop"}\r\n\r\n';
116+
const parsed = parseSSEChunk(crlf);
117+
expect(parsed).toHaveLength(2);
118+
expect(parsed[0].event).toBe("message_start");
119+
expect(parsed[1].event).toBe("message_stop");
120+
expect(parsed[1].data).toEqual({ type: "message_stop" });
121+
});
122+
123+
it("anthropicSseToOpenaiSse no longer buffers forever on a CRLF-only stream", async () => {
124+
const crlfStream = [
125+
'event: message_start',
126+
'data: {"type":"message_start","message":{"id":"msg_crlf","role":"assistant","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}',
127+
'',
128+
'event: message_stop',
129+
'data: {"type":"message_stop"}',
130+
'',
131+
].join("\r\n") + "\r\n";
132+
const out = await collectStream(anthropicSseToOpenaiSse(makeStream(crlfStream), "glm-4.6"));
133+
expect(out).toContain('"role":"assistant"');
134+
expect(out).toContain("data: [DONE]");
135+
});
136+
});
137+
97138
describe("anthropicSseToOpenaiSse", () => {
98139
it("translates message_start to first chunk with role", async () => {
99140
const input = makeStream(ANTHROPIC_SSE);

‎src/translator/sse-translator.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ export interface ParsedSSE {
1313
data: unknown;
1414
}
1515

16+
/**
17+
* SSE frame boundary splitter: a blank line in any of the three line-ending
18+
* styles. Mirrors the 3.11.2 bundle's `Kxo` frame regex — a CRLF-only
19+
* upstream (`\r\n\r\n`) never produces a `\n\n` boundary and would buffer
20+
* forever under a plain `indexOf("\n\n")` / `split("\n\n")`.
21+
*/
22+
export const SSE_FRAME_SPLIT = /\r\n\r\n|\n\n|\r\r/;
23+
1624
export function parseSSEChunk(raw: string): ParsedSSE[] {
1725
const results: ParsedSSE[] = [];
18-
const blocks = raw.split("\n\n");
26+
const blocks = raw.split(SSE_FRAME_SPLIT);
1927

2028
for (const block of blocks) {
2129
const lines = block.trim().split("\n").filter(Boolean);
@@ -27,8 +35,13 @@ export function parseSSEChunk(raw: string): ParsedSSE[] {
2735
for (const line of lines) {
2836
if (line.startsWith("event: ")) {
2937
eventType = line.slice(7).trim();
30-
} else if (line.startsWith("data: ")) {
31-
dataStr = line.slice(6);
38+
} else if (line.startsWith("data:")) {
39+
// SSE spec: the colon may be followed by at most ONE space. Strip at
40+
// most one leading space so both `data: x` and `data:x` parse while
41+
// existing `data: x` payloads stay byte-identical.
42+
let data = line.slice(5);
43+
if (data.startsWith(" ")) data = data.slice(1);
44+
dataStr = data;
3245
}
3346
}
3447

@@ -127,7 +140,7 @@ export function anthropicSseToOpenaiSse(
127140
if (done) break;
128141

129142
buffer += decoder.decode(value, { stream: true });
130-
const blocks = buffer.split("\n\n");
143+
const blocks = buffer.split(SSE_FRAME_SPLIT);
131144
buffer = blocks.pop() ?? "";
132145

133146
for (const block of blocks) {

0 commit comments

Comments
 (0)