Skip to content

fix: hoist TDZ-bound disconnect-handler vars + x-api-key fallback for Anthropic SDK - #3

Merged
MAXeaglet merged 2 commits into
MAXeaglet:masterfrom
ouones:pr/upstream-fixes
Aug 4, 2026
Merged

fix: hoist TDZ-bound disconnect-handler vars + x-api-key fallback for Anthropic SDK#3
MAXeaglet merged 2 commits into
MAXeaglet:masterfrom
ouones:pr/upstream-fixes

Conversation

@ouones

@ouones ouones commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

摘要

proxy.mjs 的两个独立修复:

  1. 提升被断连回调/超时 catch 引用的 TDZ 变量

    • handleChatCompletionsstartTime / bytesReceived / lastCcEvent / keepaliveCount 声明在 try
      块内、await forwardToCC() 之后,却被紧接着注册的 res.on('close') 回调引用。当上游响应慢、客户端先断连时,close
      回调在声明执行前触发,访问暂时性死区 → ReferenceError。非流式空闲超时的 catch 分支同样有此风险。
    • 修复:把变量提升到函数作用域(try 之前)。handleMessages 同样处理(startTime / messageId / reader /
      bytesReceived / lastCcEvent / fullText),并补声明非流式 Anthropic 分支缺失的 finishReason / usage /
      toolCalls(同样是不稳定 ReferenceError 来源)。
  2. 为 Anthropic SDK 鉴权回退到 x-api-key header

    • getApiKey 只读 Authorization: Bearer,而 Anthropic TS/JS SDK 不发送该 header——它用 x-api-key 鉴权,导致
      Anthropic SDK 客户端拿不到 key、鉴权失败。
    • 修复:无 Bearer 时回退读 x-api-key(大小写不敏感),用同样的正则提取 user_ key。OpenAI 请求行为不变。

改动范围

  • proxy.mjs——两个 commit 合计 +23 / −11 行。
  • 无 docker-compose.yml、无配置、无依赖改动。

验证

  • node --check proxy.mjs 通过。
  • 每个 commit 仅改 proxy.mjs,两个 commit 相互独立(无重叠行)。

ouones added 2 commits August 1, 2026 11:02
getApiKey only read the Authorization: Bearer header, which the Anthropic
TypeScript/JS SDK does not send — it authenticates via the x-api-key
header. Fall back to x-api-key (case-insensitive) when no Bearer token is
present, extracting the user_ key with the same regex. OpenAI-style
requests keep working unchanged.
In handleChatCompletions, startTime/bytesReceived/lastCcEvent/keepaliveCount
were declared with const/let inside the try block after the await
forwardToCC() call, but the res.on('close') callback (registered later)
references them. If the client disconnects while the upstream response is
slow, the close callback runs before those declarations execute, hitting
the temporal dead zone and throwing ReferenceError. The non-stream idle
timeout catch branch has the same risk.

Hoist these to function scope before the try block; keep only the
branch-local non-stream vars (finishReason/usage/toolCalls) where they
are. Same fix applied to handleMessages for startTime/messageId/reader/
bytesReceived/lastCcEvent/fullText, and declare the missing
finishReason/usage/toolCalls in the non-stream Anthropic branch (were
undefined, also a ReferenceError source).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants