Diffinery

homeflare/celldpublic

fix(runtime): Response.clone() tees a streaming body

celld threw for any unread fetched response; workerd and Request.clone() tee it.
Jason Schwarzenberger authored 1 parent c23c516commit 5189149b307a54a4aaa56da1bdd63fd4825b19a6Browse files
5 files changed+85−3
Files (5)
modifiedHOMEFLARE.md+1−0View file
@@ -20,6 +20,7 @@
2020 | Patch | Why |
2121 |---|---|
2222 | `node:net` `isIP` / `isIPv4` / `isIPv6`; `node:dns` and `node:dns/promises` `lookup()` | Fedify checks a URL's host with both before fetching it, so palm's remote lookups failed with `node:net.isIP is not implemented in celld`. `lookup()` uses the node's resolver through a new `__dns_lookup` op, with `connect()`'s egress rules. |
23+| `Response.clone()` on a streaming body | celld threw `Cannot clone a streaming response before consumption`; workerd tees the body, as celld's `Request.clone()` already did. palm's media proxy returns a fetched image and caches `response.clone()`. |
2324
2425 ## Building
2526
modifiedcrates/celld/js/harness.js+12−3View file
@@ -360,10 +360,19 @@
360360 }
361361 clone() {
362362 if (this.bodyUsed) throw new TypeError("Body has already been consumed");
363- if (this._bodyBytes === null)
364- throw new TypeError("Cannot clone a streaming response before consumption");
363+ // Streaming body (Homeflare patch): tee it as Request.clone() does and
364+ // as workerd does, keeping one branch and giving the clone the other.
365+ // `cache.put(key, response.clone())` on a fetched response is the usual
366+ // case.
367+ let clonedBody;
368+ if (this.body === null) clonedBody = null;
369+ else if (this._bodyBytes === null) {
370+ const [left, right] = this.body.tee();
371+ this.body = left;
372+ clonedBody = right;
373+ } else clonedBody = this._bodyBytes;
365374 const response = new Response(
366- this.body === null ? null : this._bodyBytes,
375+ clonedBody,
367376 {
368377 status: this.status, statusText: this.statusText, headers: this.headers,
369378 webSocket: this.webSocket, __wsTarget: this._wsTarget, cf: this.cf,
addedhomeflare/smoke/response-clone/index.js+66−0View file
@@ -1,0 +1,66 @@
1+// Smoke test for Response.clone() on a streaming body. GET / answers a JSON
2+// report; GET /served returns a streamed response after handing a clone to
3+// cache.put() in waitUntil (palm's media proxy), checked by run.sh's
4+// paths.txt.
5+const streamOf = (...parts) => {
6+ const encoder = new TextEncoder();
7+ return new ReadableStream({
8+ async pull(controller) {
9+ const next = parts.shift();
10+ if (next === undefined) controller.close();
11+ else controller.enqueue(encoder.encode(next));
12+ },
13+ });
14+};
15+
16+const expectEqual = (name, got, want) => ({
17+ name,
18+ ok: JSON.stringify(got) === JSON.stringify(want),
19+ got,
20+});
21+
22+export default {
23+ async fetch(request, env, ctx) {
24+ const url = new URL(request.url);
25+ if (url.pathname === "/served") {
26+ const response = new Response(streamOf("streamed ", "through ", "the host"), {
27+ headers: { "content-type": "text/plain", "x-smoke": "1" },
28+ });
29+ ctx.waitUntil(caches.default.put(new Request("https://smoke.invalid/served"), response.clone()));
30+ return response;
31+ }
32+
33+ const checks = [];
34+ const original = new Response(streamOf("a", "b", "c"), { status: 201, headers: { "x-k": "v" } });
35+ const copy = original.clone();
36+ checks.push(expectEqual("original not used by clone", original.bodyUsed, false));
37+ checks.push(expectEqual("clone keeps status", copy.status, 201));
38+ checks.push(expectEqual("clone keeps headers", copy.headers.get("x-k"), "v"));
39+ checks.push(expectEqual("clone headers are separate", copy.headers === original.headers, false));
40+ // Read the clone first: a tee must buffer for the branch read later.
41+ checks.push(expectEqual("clone body", await copy.text(), "abc"));
42+ checks.push(expectEqual("original body", await original.text(), "abc"));
43+
44+ const twice = new Response(streamOf("x", "y"));
45+ const first = twice.clone();
46+ const second = twice.clone();
47+ const texts = await Promise.all([twice.text(), first.text(), second.text()]);
48+ checks.push(expectEqual("clone of a clone", texts, ["xy", "xy", "xy"]));
49+
50+ const used = new Response(streamOf("z"));
51+ await used.text();
52+ let threw = "";
53+ try { used.clone(); } catch (error) { threw = error.constructor.name; }
54+ checks.push(expectEqual("clone after use throws", threw, "TypeError"));
55+
56+ checks.push(expectEqual("null body clone", new Response(null, { status: 204 }).clone().body, null));
57+ checks.push(expectEqual("bytes body clone", await new Response("bytes").clone().text(), "bytes"));
58+
59+ const forCache = new Response(streamOf("cached ", "copy"));
60+ await caches.default.put(new Request("https://smoke.invalid/x"), forCache.clone());
61+ checks.push(expectEqual("original readable after cache.put(clone)", await forCache.text(), "cached copy"));
62+
63+ const failed = checks.filter((c) => !c.ok);
64+ return Response.json({ ok: failed.length === 0, failed, checks }, { status: failed.length ? 500 : 200 });
65+ },
66+};
addedhomeflare/smoke/response-clone/paths.txt+1−0View file
@@ -1,0 +1,1 @@
1+/served streamed through the host
addedhomeflare/smoke/response-clone/wrangler.jsonc+5−0View file
@@ -1,0 +1,5 @@
1+{
2+ "name": "response-clone",
3+ "main": "index.js",
4+ "compatibility_date": "2026-01-01"
5+}