fix: hoist TDZ-bound disconnect-handler vars + x-api-key fallback for Anthropic SDK - #3
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
proxy.mjs的两个独立修复:提升被断连回调/超时 catch 引用的 TDZ 变量
handleChatCompletions里startTime/bytesReceived/lastCcEvent/keepaliveCount声明在 try块内、
await forwardToCC()之后,却被紧接着注册的res.on('close')回调引用。当上游响应慢、客户端先断连时,close回调在声明执行前触发,访问暂时性死区 →
ReferenceError。非流式空闲超时的 catch 分支同样有此风险。handleMessages同样处理(startTime/messageId/reader/bytesReceived/lastCcEvent/fullText),并补声明非流式 Anthropic 分支缺失的finishReason/usage/toolCalls(同样是不稳定ReferenceError来源)。为 Anthropic SDK 鉴权回退到
x-api-keyheadergetApiKey只读Authorization: Bearer,而 Anthropic TS/JS SDK 不发送该 header——它用x-api-key鉴权,导致Anthropic SDK 客户端拿不到 key、鉴权失败。
x-api-key(大小写不敏感),用同样的正则提取user_key。OpenAI 请求行为不变。改动范围
proxy.mjs——两个 commit 合计 +23 / −11 行。验证
node --check proxy.mjs通过。