diff --git a/pkgs/id/hosted-worker/src/user-do.ts b/pkgs/id/hosted-worker/src/user-do.ts index bfab8219ab..373010439f 100644 --- a/pkgs/id/hosted-worker/src/user-do.ts +++ b/pkgs/id/hosted-worker/src/user-do.ts @@ -149,7 +149,11 @@ export class UserDO { * reference is what each user's storage pays for). */ private usage() { const sql = this.state.storage.sql; - const kv = sql.exec("SELECT COUNT(*) AS n, COALESCE(SUM(LENGTH(value)), 0) AS bytes FROM kv").toArray()[0]; + // LENGTH on TEXT counts characters; casting to BLOB counts the + // UTF-8 bytes the ceiling is defined in (#11349). + const kv = sql + .exec("SELECT COUNT(*) AS n, COALESCE(SUM(LENGTH(CAST(value AS BLOB))), 0) AS bytes FROM kv") + .toArray()[0]; const blobs = sql.exec("SELECT COUNT(*) AS n, COALESCE(SUM(size), 0) AS bytes FROM blobs").toArray()[0]; const kvBytes = Number(kv.bytes); const blobBytes = Number(blobs.bytes); @@ -331,7 +335,7 @@ export class UserDO { const kvMatch = url.pathname.match(/^\/v1\/kv\/([A-Za-z0-9._-]{1,128})$/); if (kvMatch && req.method === "PUT") { const prior = this.state.storage.sql - .exec("SELECT LENGTH(value) AS n FROM kv WHERE key = ?", kvMatch[1]) + .exec("SELECT LENGTH(CAST(value AS BLOB)) AS n FROM kv WHERE key = ?", kvMatch[1]) .toArray(); const priorBytes = prior.length === 0 ? 0 : Number(prior[0].n); const headroom = this.kvHeadroom(priorBytes); diff --git a/pkgs/id/hosted-worker/test/usage.test.ts b/pkgs/id/hosted-worker/test/usage.test.ts index e1a32f9c9c..112da27727 100644 --- a/pkgs/id/hosted-worker/test/usage.test.ts +++ b/pkgs/id/hosted-worker/test/usage.test.ts @@ -170,6 +170,31 @@ test("kv overwrite headroom counts the prior value", async () => { assert.equal((await getUsage(doA)).storage_bytes, 10); }); +test("kv usage and the ceiling count UTF-8 bytes, not characters (#11349)", async () => { + const { doA } = makeDO({ USER_CEILING_BYTES: "100" }); + // 5 astral characters = 20 UTF-8 bytes; SQLite LENGTH on TEXT says 5. + const value = "\u{1F600}".repeat(5); + assert.equal(new TextEncoder().encode(value).byteLength, 20); + let accepted = 0; + for (let i = 0; i < 17; i++) { + const r = await doA.fetch(routed(`/v1/kv/k${i}`, { method: "PUT", body: value })); + if (r.status === 200) accepted++; + else assert.equal(r.status, 413); + } + assert.equal(accepted, 5, "only 100 bytes of 20-byte values fit"); + const u = await getUsage(doA); + assert.equal(u.kv_bytes, 100); + assert.equal(u.storage_bytes, 100); + assert.equal(u.at_cap, true); + const read = await doA.fetch(routed("/v1/kv/k0")); + assert.equal(((await read.json()) as { value: string }).value, value, "multibyte values round-trip"); + // Overwriting a 20-byte value with 2 ASCII bytes frees 18 bytes of headroom. + assert.equal((await doA.fetch(routed("/v1/kv/k0", { method: "PUT", body: "ab" }))).status, 200); + assert.equal((await getUsage(doA)).kv_bytes, 82); + assert.equal((await doA.fetch(routed("/v1/kv/k1", { method: "PUT", body: "\u00e9".repeat(19) }))).status, 200, "38 bytes fits 18 freed + 20 prior"); + assert.equal((await getUsage(doA)).kv_bytes, 100); +}); + test("a deduped re-PUT of the same blob does not double-charge", async () => { const { doA } = makeDO({ USER_CEILING_BYTES: "10" }); const blob = new TextEncoder().encode("0123456789");