Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretToken, string>();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
63 changes: 63 additions & 0 deletions tests/http.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down