diff --git a/e2e/clawdhub.e2e.test.ts b/e2e/clawdhub.e2e.test.ts index 436510d..bf0b2db 100644 --- a/e2e/clawdhub.e2e.test.ts +++ b/e2e/clawdhub.e2e.test.ts @@ -47,7 +47,7 @@ async function makeTempConfig(registry: string, token: string | null) { async function fetchWithTimeout(input: RequestInfo | URL, init?: RequestInit) { const controller = new AbortController() - const timeout = setTimeout(() => controller.abort('Timeout'), REQUEST_TIMEOUT_MS) + const timeout = setTimeout(() => controller.abort(new Error('Timeout')), REQUEST_TIMEOUT_MS) try { return await fetch(input, { ...init, signal: controller.signal }) } finally { diff --git a/packages/clawdhub/src/http.ts b/packages/clawdhub/src/http.ts index 9c72a59..1a6b668 100644 --- a/packages/clawdhub/src/http.ts +++ b/packages/clawdhub/src/http.ts @@ -54,23 +54,26 @@ export async function apiRequest( body = JSON.stringify(args.body ?? {}) } const controller = new AbortController() - const timeout = setTimeout(() => controller.abort('Timeout'), REQUEST_TIMEOUT_MS) - const response = await fetch(url, { - method: args.method, - headers, - body, - signal: controller.signal, - }) - clearTimeout(timeout) - if (!response.ok) { - const text = await response.text().catch(() => '') - const message = text || `HTTP ${response.status}` - if (response.status === 429 || response.status >= 500) { - throw new Error(message) + const timeout = setTimeout(() => controller.abort(new Error('Timeout')), REQUEST_TIMEOUT_MS) + try { + const response = await fetch(url, { + method: args.method, + headers, + body, + signal: controller.signal, + }) + if (!response.ok) { + const text = await response.text().catch(() => '') + const message = text || `HTTP ${response.status}` + if (response.status === 429 || response.status >= 500) { + throw new Error(message) + } + throw new AbortError(message) } - throw new AbortError(message) + return (await response.json()) as unknown + } finally { + clearTimeout(timeout) } - return (await response.json()) as unknown }, { retries: 2 }, ) @@ -103,23 +106,26 @@ export async function apiRequestForm( const headers: Record = { Accept: 'application/json' } if (args.token) headers.Authorization = `Bearer ${args.token}` const controller = new AbortController() - const timeout = setTimeout(() => controller.abort('Timeout'), REQUEST_TIMEOUT_MS) - const response = await fetch(url, { - method: args.method, - headers, - body: args.form, - signal: controller.signal, - }) - clearTimeout(timeout) - if (!response.ok) { - const text = await response.text().catch(() => '') - const message = text || `HTTP ${response.status}` - if (response.status === 429 || response.status >= 500) { - throw new Error(message) + const timeout = setTimeout(() => controller.abort(new Error('Timeout')), REQUEST_TIMEOUT_MS) + try { + const response = await fetch(url, { + method: args.method, + headers, + body: args.form, + signal: controller.signal, + }) + if (!response.ok) { + const text = await response.text().catch(() => '') + const message = text || `HTTP ${response.status}` + if (response.status === 429 || response.status >= 500) { + throw new Error(message) + } + throw new AbortError(message) } - throw new AbortError(message) + return (await response.json()) as unknown + } finally { + clearTimeout(timeout) } - return (await response.json()) as unknown }, { retries: 2 }, ) @@ -138,17 +144,20 @@ export async function downloadZip(registry: string, args: { slug: string; versio } const controller = new AbortController() - const timeout = setTimeout(() => controller.abort('Timeout'), REQUEST_TIMEOUT_MS) - const response = await fetch(url.toString(), { method: 'GET', signal: controller.signal }) - clearTimeout(timeout) - if (!response.ok) { - const message = (await response.text().catch(() => '')) || `HTTP ${response.status}` - if (response.status === 429 || response.status >= 500) { - throw new Error(message) + const timeout = setTimeout(() => controller.abort(new Error('Timeout')), REQUEST_TIMEOUT_MS) + try { + const response = await fetch(url.toString(), { method: 'GET', signal: controller.signal }) + if (!response.ok) { + const message = (await response.text().catch(() => '')) || `HTTP ${response.status}` + if (response.status === 429 || response.status >= 500) { + throw new Error(message) + } + throw new AbortError(message) } - throw new AbortError(message) + return new Uint8Array(await response.arrayBuffer()) + } finally { + clearTimeout(timeout) } - return new Uint8Array(await response.arrayBuffer()) }, { retries: 2 }, )