diff --git a/pkgs/lab-ops-hub/src/index.ts b/pkgs/lab-ops-hub/src/index.ts
index a84147a3d0..7950b345da 100644
--- a/pkgs/lab-ops-hub/src/index.ts
+++ b/pkgs/lab-ops-hub/src/index.ts
@@ -1,6 +1,6 @@
import { Container, getContainer } from "@cloudflare/containers";
import { DurableObject } from "cloudflare:workers";
-import { computeBudget, fetchFollowingValidatedRedirects, validateCrawlUrl } from "./lib";
+import { computeBudget, fetchFollowingValidatedRedirects, readResponseTextCapped, validateCrawlUrl } from "./lib";
// ---------------------------------------------------------------------------
// Lab Ops Hub — one Worker exercising the whole Workers Paid surface at
@@ -164,7 +164,9 @@ async function crawlFallback(url: string) {
const res = await fetchFollowingValidatedRedirects(url, (u) =>
fetch(u, { redirect: "manual", headers: { "user-agent": "LabOpsHub/0.1 (+personal crawler)" } }),
);
- const html = await res.text();
+ // Cap the body read at the container's 2,000,000 bytes (#481): res.text()
+ // would buffer a huge page whole in the Worker's memory.
+ const html = await readResponseTextCapped(res);
const title = html.match(/
]*>([^<]*)<\/title>/i)?.[1]?.trim() ?? "";
const links = (html.match(/ {
+ if (!res.body) return "";
+ const reader = res.body.getReader();
+ const decoder = new TextDecoder("utf-8", { fatal: false });
+ let text = "";
+ let read = 0;
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ read += value.byteLength;
+ if (read > maxBytes) {
+ // Keep only the bytes up to the cap (a multi-byte character split at
+ // the boundary decodes as a replacement character, matching the
+ // container's errors="replace"), then cancel so the rest of the body
+ // is never pulled into memory.
+ text += decoder.decode(value.subarray(0, value.byteLength - (read - maxBytes)), { stream: true });
+ await reader.cancel();
+ break;
+ }
+ text += decoder.decode(value, { stream: true });
+ }
+ return text + decoder.decode();
+}
+
// --- Budget computation -------------------------------------------------------
// estimated_overage_usd is computed from the counted usage, never a
// literal: for each resource with an included amount and a rate,
diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts
index efaa943e75..8bd9482f46 100644
--- a/pkgs/lab-ops-hub/test/lib.test.ts
+++ b/pkgs/lab-ops-hub/test/lib.test.ts
@@ -2,7 +2,13 @@
// (type stripping): `npm test`. No Worker bindings are involved.
import assert from "node:assert/strict";
import { test } from "node:test";
-import { computeBudget, fetchFollowingValidatedRedirects, validateCrawlUrl } from "../src/lib.ts";
+import {
+ computeBudget,
+ fetchFollowingValidatedRedirects,
+ MAX_CRAWL_BODY_BYTES,
+ readResponseTextCapped,
+ validateCrawlUrl,
+} from "../src/lib.ts";
test("validateCrawlUrl accepts public http(s) targets", () => {
assert.equal(validateCrawlUrl("https://example.com/page").ok, true);
@@ -210,6 +216,41 @@ test("fetchFollowingValidatedRedirects bounds the hop count (#460)", async () =>
await assert.rejects(fetchFollowingValidatedRedirects("https://example.com/loop", fetchFn, 3), /too many redirects/);
});
+test("readResponseTextCapped returns a body under the cap whole (#481)", async () => {
+ const res = new Response("hi hello");
+ assert.equal(await readResponseTextCapped(res, 100), "hi hello");
+});
+
+test("readResponseTextCapped truncates at the cap and stops pulling the stream (#481)", async () => {
+ let pulled = 0;
+ let cancelled = false;
+ const chunk = new Uint8Array(100).fill(97); // "a" * 100
+ const body = new ReadableStream({
+ pull(controller) {
+ pulled += chunk.byteLength;
+ controller.enqueue(chunk);
+ },
+ cancel() {
+ cancelled = true;
+ },
+ });
+ const text = await readResponseTextCapped(new Response(body), 250);
+ assert.equal(text, "a".repeat(250));
+ assert.equal(cancelled, true);
+ // The reader stopped at the cap instead of draining the endless stream.
+ assert.ok(pulled <= 300, `pulled ${pulled} bytes for a 250-byte cap`);
+});
+
+test("readResponseTextCapped decodes a character split at the cap as a replacement (#481)", async () => {
+ // "é" is 0xC3 0xA9 in UTF-8; a 2-byte cap keeps only the first byte.
+ const res = new Response(new TextEncoder().encode("aébc"));
+ assert.equal(await readResponseTextCapped(res, 2), "a�");
+});
+
+test("the crawl body cap matches the container's 2,000,000-byte read (#481)", () => {
+ assert.equal(MAX_CRAWL_BODY_BYTES, 2_000_000);
+});
+
test("computeBudget reports $0 while usage is inside the included amounts", () => {
const b = computeBudget({ worker_request: 120, d1_write: 40, queue_msg: 3 });
assert.equal(b.estimated_overage_usd, 0);