Skip to content

LIBERATED AI CHAT

TypeScript

fix: bounded streaming body read + update stale fallback comments

faber6b2cd9da5d82
diff --git a/functions/api/telemetry.ts b/functions/api/telemetry.ts
index 0d785a0..5857f83 100644
--- a/functions/api/telemetry.ts
+++ b/functions/api/telemetry.ts
@@ -17,9 +17,9 @@
* KV Binding (Cloudflare Pages Dashboard → Settings → Bindings):
* TELEMETRY_KV — KV namespace for buffering
*
- * IMPORTANT: TELEMETRY_KV must be bound in production. Without it,
- * every request falls back to a direct HF commit — which will
- * recreate the commit-explosion problem at scale.
+ * Without TELEMETRY_KV bound, telemetry accumulates in an
+ * in-memory isolate buffer and flushes to HF in batches.
+ * KV is preferred (survives isolate eviction) but not required.
*
* Cloudflare-level protections (configure in Dashboard → Security):
* - WAF rule: block POST /api/telemetry with Content-Length > 524288
@@ -200,18 +200,36 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
return jsonResponse({ error: 'Rate limit exceeded — try again later' }, 429)
}
- // ── Read body with size cap ───────────────────────────────────────
+ // ── Bounded body read (cancels stream at 512 KiB) ──────────────────
+ // Content-Length can be spoofed or absent (chunked), so we enforce
+ // the cap at the byte level while streaming — never buffer more than
+ // MAX_REQUEST_BYTES regardless of what the client claims.
let bodyText: string
try {
- bodyText = await request.text()
+ const reader = request.body?.getReader()
+ if (!reader) {
+ return jsonResponse({ error: 'No body' }, 400)
+ }
+ const chunks: Uint8Array[] = []
+ let totalBytes = 0
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+ totalBytes += value.byteLength
+ if (totalBytes > MAX_REQUEST_BYTES) {
+ reader.cancel()
+ return jsonResponse({ error: 'Request too large', limit: MAX_REQUEST_BYTES }, 413)
+ }
+ chunks.push(value)
+ }
+ const merged = new Uint8Array(totalBytes)
+ let offset = 0
+ for (const c of chunks) { merged.set(c, offset); offset += c.byteLength }
+ bodyText = new TextDecoder().decode(merged)
} catch {
return jsonResponse({ error: 'Failed to read body' }, 400)
}
- if (new TextEncoder().encode(bodyText).byteLength > MAX_REQUEST_BYTES) {
- return jsonResponse({ error: 'Request too large', limit: MAX_REQUEST_BYTES }, 413)
- }
-
let payload: TelemetryPayload
try {
payload = JSON.parse(bodyText) as TelemetryPayload
diff --git a/wrangler.toml b/wrangler.toml
index b9a51d0..7541f11 100644
--- a/wrangler.toml
+++ b/wrangler.toml
@@ -8,8 +8,9 @@
# LOCAL DEV: Create a preview namespace and set the id below,
# then run `wrangler pages dev` to test locally.
#
-# If TELEMETRY_KV is not bound, telemetry falls back to direct
-# per-request HF commits (the old behavior).
+# If TELEMETRY_KV is not bound, telemetry buffers in the Worker
+# isolate's memory and flushes to HF in batches (zero-config fallback).
+# KV is preferred because it survives isolate eviction.
# Uncomment and set a real namespace ID for local dev:
# [[kv_namespaces]]