From 1eec1496b86cbe3a0f5614e91ec2482993e3039c Mon Sep 17 00:00:00 2001 From: owizdom Date: Fri, 27 Nov 2026 15:58:55 +0200 Subject: [PATCH] Round down a fractional challenge list limit instead of crashing GET /api/v1/challenges?limit=2.5 passed 2.5 to D1 as the LIMIT, which SQLite refuses with "datatype mismatch", and the Worker threw. The limit is now rounded down after the existing clamp, so every value that worked before gives the same result. --- api/src/index.ts | 3 ++- api/test/api.test.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/api/src/index.ts b/api/src/index.ts index 49142fc..5777d87 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -50,7 +50,8 @@ async function api(req: Request, env: Env, path: string[]): Promise { const route = `${req.method} ${path.join("/")}`; if (route === "GET challenges") { - const limit = Math.min(100, Math.max(1, Number(new URL(req.url).searchParams.get("limit")) || 20)); + // SQLite refuses a fractional LIMIT, so take the whole part after clamping. + const limit = Math.floor(Math.min(100, Math.max(1, Number(new URL(req.url).searchParams.get("limit")) || 20))); return json({ challenges: await recent(env.DB, limit) }); } if (req.method === "GET" && path[0] === "challenges" && path.length === 2) { diff --git a/api/test/api.test.ts b/api/test/api.test.ts index e047574..d9ab339 100644 --- a/api/test/api.test.ts +++ b/api/test/api.test.ts @@ -49,6 +49,15 @@ describe("challenges", () => { expect(jobs?.n).toBe(1); }); + it("lists the whole part of a fractional limit", async () => { + for (let i = 0; i < 3; i++) expect((await challenge(uuid())).status).toBe(201); + for (const [limit, count] of [["2.5", 2], ["1.5", 1]] as const) { + const r = await get(`challenges?limit=${limit}`); + expect(r.status).toBe(200); + expect((await r.json<{ challenges: unknown[] }>()).challenges).toHaveLength(count); + } + }); + it("holds 10 challenges per key per hour", async () => { const key = "one-busy-holder-key"; for (let i = 0; i < 10; i++) expect((await challenge(uuid(), key)).status).toBe(201);