Skip to content

Commit 86a2863

Browse files
committed
fix(update): cancel npm registry error bodies
1 parent 90ba9fc commit 86a2863

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/infra/update-check.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ describe("resolveNpmChannelTag", () => {
219219
);
220220
});
221221

222+
it("cancels public registry HTTP failure bodies", async () => {
223+
const cancel = vi.fn(async () => undefined);
224+
const fetch = vi.fn(
225+
async () => ({ ok: false, status: 503, body: { cancel } }) as unknown as Response,
226+
);
227+
vi.stubGlobal("fetch", fetch);
228+
229+
await expect(
230+
fetchNpmPackageTargetStatus({ target: "latest", timeoutMs: 1000 }),
231+
).resolves.toEqual({
232+
target: "latest",
233+
version: null,
234+
nodeEngine: null,
235+
error: "HTTP 503",
236+
});
237+
expect(cancel).toHaveBeenCalledTimes(1);
238+
});
239+
222240
it("falls back to latest when beta is older", async () => {
223241
versionByTag.beta = "1.0.0-beta.1";
224242
versionByTag.latest = "1.0.1-1";

src/infra/update-check.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ async function fetchPublicNpmPackageTargetStatus(params: {
110110
target: string;
111111
timeoutMs: number;
112112
}): Promise<NpmPackageTargetStatus> {
113+
let res: Response | undefined;
113114
try {
114-
const res = await fetchWithTimeout(
115+
res = await fetchWithTimeout(
115116
`https://registry.npmjs.org/openclaw/${encodeURIComponent(params.target)}`,
116117
{},
117118
Math.max(250, params.timeoutMs),
@@ -135,6 +136,10 @@ async function fetchPublicNpmPackageTargetStatus(params: {
135136
};
136137
} catch (err) {
137138
return { target: params.target, version: null, nodeEngine: null, error: String(err) };
139+
} finally {
140+
if (res?.bodyUsed !== true) {
141+
await res?.body?.cancel().catch(() => undefined);
142+
}
138143
}
139144
}
140145

0 commit comments

Comments
 (0)