fix(sdk): cancel Copilot token error bodies · openclaw/openclaw@5b3d652
vincentkoc
·
2026-06-19
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -318,6 +318,30 @@ describe("provider auth profile helpers", () => {
|
318 | 318 | ).rejects.toThrow("Copilot token response has invalid expires_at"); |
319 | 319 | }); |
320 | 320 | |
| 321 | +it("cancels Copilot token exchange error bodies", async () => { |
| 322 | +vi.resetModules(); |
| 323 | + |
| 324 | +const response = new Response("bad credentials", { status: 401 }); |
| 325 | +const cancel = vi.spyOn(response.body!, "cancel").mockResolvedValue(undefined); |
| 326 | +const fetchImpl = vi.fn(async () => response); |
| 327 | + |
| 328 | +const { resolveCopilotApiToken } = await import("./provider-auth.js"); |
| 329 | + |
| 330 | +await expect( |
| 331 | +resolveCopilotApiToken({ |
| 332 | +githubToken: "github-token", |
| 333 | + fetchImpl, |
| 334 | +cachePath: "/tmp/copilot-token.json", |
| 335 | +loadJsonFileImpl: () => undefined, |
| 336 | +saveJsonFileImpl: () => { |
| 337 | +throw new Error("should not save failed token"); |
| 338 | +}, |
| 339 | +}), |
| 340 | +).rejects.toThrow("Copilot token exchange failed: HTTP 401"); |
| 341 | + |
| 342 | +expect(cancel).toHaveBeenCalledOnce(); |
| 343 | +}); |
| 344 | + |
321 | 345 | it("refreshes cached Copilot tokens with out-of-range expiry values", async () => { |
322 | 346 | vi.resetModules(); |
323 | 347 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -200,6 +200,12 @@ function parseCopilotTokenResponse(value: unknown): {
|
200 | 200 | return { token, expiresAt: expiresAtMs }; |
201 | 201 | } |
202 | 202 | |
| 203 | +async function cancelUnreadResponseBody(response: Response): Promise<void> { |
| 204 | +if (!response.bodyUsed) { |
| 205 | +await response.body?.cancel().catch(() => undefined); |
| 206 | +} |
| 207 | +} |
| 208 | + |
203 | 209 | function resolveCopilotProxyHost(proxyEp: string): string | null { |
204 | 210 | const trimmed = proxyEp.trim(); |
205 | 211 | if (!trimmed) { |
@@ -300,6 +306,7 @@ export async function resolveCopilotApiToken(params: {
|
300 | 306 | }); |
301 | 307 | |
302 | 308 | if (!res.ok) { |
| 309 | +await cancelUnreadResponseBody(res); |
303 | 310 | throw new Error(`Copilot token exchange failed: HTTP ${res.status}`); |
304 | 311 | } |
305 | 312 | |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。