From 0fc97d5a0c4fdc4a97bc072d8080d8fdc26e2db5 Mon Sep 17 00:00:00 2001 From: William Schmitt Date: Mon, 16 Nov 2026 13:36:10 -0800 Subject: [PATCH] fix(client): apply the request timeout to reading the response body fetchWithTimeout() clears its timer as soon as fetch() resolves with headers. defaultParseResponse() then reads response.json()/text() with no timer, so a server that sends headers and stalls leaves the promise pending forever, despite the documented timeout and APIConnectionTimeoutError. defaultParseResponse() now arms the same timeout on the request's controller while it reads the body and maps that abort to APIConnectionTimeoutError (a caller abort still surfaces as before). The timeout is passed through the internal APIResponseProps. Scope: the body gets its own timeout window (like a read timeout), not the remainder of the header budget. Raw responses (__binaryResponse, asResponse()) are untouched, so long downloads the caller streams are not cut off by the request timeout. Measured: new test serves headers plus '{' and stalls, timeout 50 ms. On main it hits the 5 s jest timeout under native fetch and node-fetch; with this change both reject with APIConnectionTimeoutError in ~60 ms. src/core.ts is generated; this edit is sealed. Co-Authored-By: Claude Opus 5.5 --- src/core.ts | 16 +++++++++++++++- tests/index.test.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/core.ts b/src/core.ts index 08d2a2c6..fe127652 100644 --- a/src/core.ts +++ b/src/core.ts @@ -54,6 +54,7 @@ type APIResponseProps = { response: Response; options: FinalRequestOptions; controller: AbortController; + timeout: number; }; async function defaultParseResponse(props: APIResponseProps): Promise { @@ -67,6 +68,19 @@ async function defaultParseResponse(props: APIResponseProps): Promise { return response as unknown as T; } + const timeout = setTimeout(() => props.controller.abort(), props.timeout); + try { + return await parseBody(response); + } catch (err) { + if (props.controller.signal.aborted && !props.options.signal?.aborted) + throw new APIConnectionTimeoutError(); + throw err; + } finally { + clearTimeout(timeout); + } +} + +async function parseBody(response: Response): Promise { const contentType = response.headers.get('content-type'); const mediaType = contentType?.split(';')[0]?.trim(); const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json'); @@ -510,7 +524,7 @@ export abstract class APIClient { throw err; } - return { response, options, controller }; + return { response, options, controller, timeout }; } requestAPIList = AbstractPage>( diff --git a/tests/index.test.ts b/tests/index.test.ts index 03142eff..3e1feab6 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,9 +1,11 @@ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import Browserbase from '@browserbasehq/sdk'; -import { APIUserAbortError } from '@browserbasehq/sdk'; +import { APIConnectionTimeoutError, APIUserAbortError } from '@browserbasehq/sdk'; import { Headers } from '@browserbasehq/sdk/core'; import defaultFetch, { Response, type RequestInit, type RequestInfo } from 'node-fetch'; +import http from 'node:http'; +import { type AddressInfo } from 'node:net'; describe('instantiate client', () => { const env = process.env; @@ -505,4 +507,28 @@ describe('retries', () => { ).toEqual(JSON.stringify({ a: 1 })); expect(count).toEqual(3); }); + + test.each([ + ['native fetch', undefined], + ['node-fetch', defaultFetch], + ])('timeout covers a stalled response body with %s', async (_, fetch) => { + const server = http.createServer((req, res) => + res.writeHead(200, { 'content-type': 'application/json' }).write('{'), + ); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + const { port } = server.address() as AddressInfo; + const client = new Browserbase({ + apiKey: 'My API Key', + baseURL: `http://127.0.0.1:${port}`, + fetch, + timeout: 50, + maxRetries: 0, + }); + try { + await expect(client.get('/foo')).rejects.toThrow(APIConnectionTimeoutError); + } finally { + server.closeAllConnections(); + server.close(); + } + }); });