diff --git a/src/transport.ts b/src/transport.ts index 53c9bd4..16cc8c1 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -5,6 +5,7 @@ const DEFAULT_BASE_URL = "https://proofline.sh"; const MAX_RESPONSE_BYTES = 8 * 1024 * 1024; const MAX_RETRIES = 3; const MAX_RETRY_SECONDS = 60; +const RETRYABLE_STATUSES = [429, 503]; const inspect = Symbol.for("nodejs.util.inspect.custom"); const secrets = new WeakMap(); @@ -226,7 +227,13 @@ async function decoded( } } catch (error) { if (error instanceof ProoflineError) throw error; - if (response.ok && (signal.aborted || performance.now() >= deadline)) + // A retryable refusal cut off at the wait deadline reports the wait timing + // out, as the Python SDK's check_stream does. Other refusals keep their + // status. + const timeoutOnExpiry = + response.ok || + (waitTimeout && RETRYABLE_STATUSES.includes(response.status)); + if (timeoutOnExpiry && (signal.aborted || performance.now() >= deadline)) throw new ProoflineError( 0, waitTimeout @@ -533,7 +540,7 @@ export class Transport { const delay = retryDelay(response.headers.get("Retry-After"), attempt); if ( method !== "GET" || - ![429, 503].includes(response.status) || + !RETRYABLE_STATUSES.includes(response.status) || attempt >= MAX_RETRIES || delay === undefined ) diff --git a/tests/http.test.mjs b/tests/http.test.mjs index c1263ee..901b6a1 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -1060,6 +1060,69 @@ test("refusal retries preserve response bounds and the caller deadline", async ( assert.equal(fixture.calls.length - before, 1); }); +test("a retryable refusal times the wait out only when cut off at the wait deadline", async (t) => { + for (const status of [429, 503, 403]) { + for (const clock of ["elapsed", "native-abort", "interrupted"]) { + await t.test(`${status} ${clock}`, async (t) => { + const real = performance.now.bind(performance); + let skew = 0; + const fixture = await server(t, (_, response) => { + response.writeHead(status, { + "Content-Type": "application/problem+json", + "Retry-After": "1", + }); + // The headers and a first chunk arrive; the rest never does. + response.write('{"type":"about:blank","detail":"', () => { + if (clock === "native-abort") return; + // "elapsed": the observed clock passes the wait deadline, then the + // read fails while the request's abort signal, 20 seconds away, has + // not fired. "interrupted": the read fails long before the deadline. + setTimeout(() => { + if (clock === "elapsed") skew = 600_000; + response.socket.end(); + }, 20); + }); + }); + const client = new ProoflineClient({ baseUrl: fixture.baseUrl, token }); + const reviewUrl = `${fixture.baseUrl}/orgs/by-id/${orgId}/reviews/${reviewId}`; + // "elapsed" reports the expiry only through the observed clock; + // "native-abort" freezes that clock before the deadline, so only the + // request's abort signal reports that the wait deadline expired; + // "interrupted" ends the body while the deadline is still far away. + const observed = real(); + const mocked = t.mock.method(performance, "now", () => + clock === "native-abort" ? observed : real() + skew, + ); + try { + const waiting = client.waitForReview(orgId, reviewId, { + // Long enough for the headers to arrive before the abort fires. + timeoutMs: clock === "native-abort" ? 500 : 20_000, + }); + if (status === 403 || clock === "interrupted") { + // A refusal that is not retried, or a body cut off before the + // deadline, keeps its status and error, as before. + await assert.rejects( + waiting, + (error) => + error instanceof ProoflineError && + error.status === status && + error.type === "urn:proofline:sdk:invalid-response", + ); + } else { + const result = await waiting; + assert.equal(result.status, "TIMED_OUT"); + assert.equal(result.review, null); + assert.equal(result.reviewUrl, reviewUrl); + } + } finally { + mocked.mock.restore(); + } + assert.equal(fixture.calls.length, 1); + }); + } + } +}); + test("invalid UTF-8 never hides successful-body or incomplete-body failures", async (t) => { for (const status of [200, 429, 503]) { for (const mode of [