Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Keep only a recovery that shows the envelope, and publish two schema limits
Claude173b34d8a27c
diff --git a/src/lib/api/openapi.ts b/src/lib/api/openapi.tsindex b587f3b..5457c8f 100644--- a/src/lib/api/openapi.ts+++ b/src/lib/api/openapi.ts@@ -249,6 +249,9 @@ export function buildOpenApiDocument(properties: {groupId: {description: "Radio buttons in one group.",+ maxLength: 64,+ minLength: 1,+ pattern: "\\S",type: "string",},height: { exclusiveMinimum: 0, maximum: 1, type: "number" },@@ -387,7 +390,11 @@ export function buildOpenApiDocument(type: "integer",},},- { in: "query", name: "cursor", schema: { type: "string" } },+ {+ in: "query",+ name: "cursor",+ schema: { maxLength: 200, type: "string" },+ },],responses: {"200": {diff --git a/src/lib/api/route.dbtest.ts b/src/lib/api/route.dbtest.tsindex 17b7b0a..e694db1 100644--- a/src/lib/api/route.dbtest.ts+++ b/src/lib/api/route.dbtest.ts@@ -158,36 +158,50 @@ test(letRecoveryFinish = resolve;});let retry: Promise<Response> | undefined;+ let first: Response;- const first = await route.runIdempotently(- context,- "POST /api/v1/envelopes",- body,- async (remember) => {- await remember("envelope-1");-- // A retry comes in now. The envelope is noted and no response is- // kept yet, so it answers from the envelope, and finishes only after- // this request has kept its own response.- retry = route.runIdempotently(- context,- "POST /api/v1/envelopes",- body,- () => Promise.reject(new Error("A retry must not make an envelope.")),- async () => {- recoveryStarted();- await recoveryMayFinish;-- return { body: { from: "retry" }, status: 201 };- },- );- await recovering;-- return { body: { from: "first" }, status: 201 };- },- );-- letRecoveryFinish();+ try {+ first = await route.runIdempotently(+ context,+ "POST /api/v1/envelopes",+ body,+ async (remember) => {+ await remember("envelope-1");++ // A retry comes in now. The envelope is noted and no response is+ // kept yet, so it answers from the envelope, and finishes only+ // after this request has kept its own response.+ retry = route.runIdempotently(+ context,+ "POST /api/v1/envelopes",+ body,+ () =>+ Promise.reject(new Error("A retry must not make an envelope.")),+ async () => {+ recoveryStarted();+ await recoveryMayFinish;++ return { body: { from: "retry" }, status: 201 };+ },+ );++ // A retry that settles without answering from the envelope fails+ // the test now, with its own error, instead of at the timeout.+ const settledFirst = retry.then(() => {+ throw new Error(+ "The retry finished without answering from the envelope.",+ );+ });++ settledFirst.catch(() => undefined);+ await Promise.race([recovering, settledFirst]);++ return { body: { from: "first" }, status: 201 };+ },+ );+ } finally {+ letRecoveryFinish();+ }const second = await retry!;@@ -241,6 +255,48 @@ test("a first request that finishes after a retry answered for it gives the answassert.deepEqual(await first.json(), { from: "retry" });});+test("a retry that finds the envelope gone does not replace what the first request answers", async () => {+ const key = `order-${randomUUID()}`;+ const context = await createContext(key);+ const body = '{"title":"Lease"}';++ const first = await route.runIdempotently(+ context,+ "POST /api/v1/envelopes",+ body,+ async (remember) => {+ await remember("envelope-3");++ // Someone deleted the envelope just now, so the retry finds nothing.+ const retried = await route.runIdempotently(+ context,+ "POST /api/v1/envelopes",+ body,+ () => Promise.reject(new Error("A retry must not make an envelope.")),+ async () => ({ body: { error: "gone" }, status: 404 }),+ );++ assert.equal(retried.status, 404);++ return { body: { from: "first" }, status: 201 };+ },+ );++ assert.equal(first.status, 201);+ assert.deepEqual(await first.json(), { from: "first" });+ assert.deepEqual(+ await claimIdempotencyKey(+ {+ apiKeyId: context.actor.apiKeyId,+ key,+ requestHash: hashRequest("POST /api/v1/envelopes", body),+ },+ db(),+ ),+ { kind: "replay", response: { from: "first" }, statusCode: 201 },+ );+});+test("a request that fails before making anything lets its key go", async () => {const key = `order-${randomUUID()}`;const context = await createContext(key);diff --git a/src/lib/api/route.ts b/src/lib/api/route.tsindex 71dbe20..a5b8367 100644--- a/src/lib/api/route.ts+++ b/src/lib/api/route.ts@@ -260,17 +260,21 @@ export async function runIdempotently(}const recovered = await runCatching(() => recover(previous.resourceId));- // The first request may have finished meanwhile. Its response is then- // the one kept, and the answer here too.- const kept = isKept(recovered.status)- ? await completeIdempotencyKey({- apiKeyId: claim.apiKeyId,- key,- reservedAt: previous.reservedAt,- response: recovered.body,- statusCode: recovered.status,- })- : null;+ // Only an answer that shows the envelope is kept. One that finds it+ // gone (deleted meanwhile) is not: the first request may still be+ // finishing, and its own answer must not be replaced. And the first+ // request may have finished meanwhile: its response is then the one+ // kept, and the answer here too.+ const kept =+ recovered.status >= 200 && recovered.status < 300+ ? await completeIdempotencyKey({+ apiKeyId: claim.apiKeyId,+ key,+ reservedAt: previous.reservedAt,+ response: recovered.body,+ statusCode: recovered.status,+ })+ : null;return apiJson(kept ? kept.response : recovered.body, {headers: { "idempotent-replayed": "true" },