[Miniflare] Consider buffering all requests
This code fails in miniflare but works when deployed in a worker:
env.R2_BUCKET.put(key, request.body); // request.body is the `Request` from the worker handler.
It fails with Provided readable stream must have a known length (request/response body or readable half of FixedLengthStream). I've seen workarounds here with FixedLengthStream but since this is working in production I don't think it should fail in miniflare. Happy to give more information if needed.
wrangler version: 3.1.1
Hey! 👋 Thanks for raising this. I think this may only be a problem when the incoming request doesn't have a Content-Length header. Starting wrangler dev with the following worker...
export default {
async fetch(request, env, ctx) {
if (request.method !== "POST") return new Response(null, { status: 405 });
await env.BUCKET.put("key", request.body);
return new Response((await env.BUCKET.get("key"))?.body);
}
}
...then running the following script...
let res = await fetch("http://127.0.0.1:8787", {
method: "POST",
body: "body",
duplex: "half"
});
console.log(await res.text()); // "body"
const chunks = ["a", "b", "c"];
const stream = new ReadableStream({
pull(controller) {
const chunk = chunks.shift();
if (chunk) {
controller.enqueue(chunk);
} else {
controller.close();
}
}
});
res = await fetch("http://127.0.0.1:8787", {
method: "POST",
body: stream,
duplex: "half"
});
console.log(await res.text()); // throws as above
...only throws with a streaming body of unknown length. Would you be able to check the incoming headers on the request to see if Content-Length is present, or whether the request is using Transfer-Encoding: chunked?
I suspect this is working in production as I believe we buffer all requests first.
Hey! 👋 I'm going to transfer this to workers-sdk as that's the new home for Miniflare 3, and that will make it easier for us to track this. 👍
Hi,
I get the same error when using sveltekit with the cloudflare adapter. It works in production but not when running the worker locally. The adapter uses getPlatformProxy under the hood so maybe it is a bug with that. I don't know if it is a problem with sveltekit in particular or with wrangler. My incoming request does hava a Content-Length but no Transfer-Encoding header.
@mrbbot running your script worked as you described. My only guess is that sveltekit does something with the request or that it has something to do with getPlatformProxy
happy to help debug this further
Running into the same with remix as well. The request does include a content length header
Buffering the body manually works though
const buffered = await request.arrayBuffer();
await env.BUCKET.put(`key`, buffered);
Related to #6425, but not a duplicate — worth recording the distinction since the two surface the same error string.
Both hit Provided readable stream must have a known length (request/response body or readable half of FixedLengthStream), which is enforced by workerd's R2 put() binding. The asks differ:
- This issue is a local/production parity claim:
put(key, request.body)reportedly works deployed but fails under Miniflare, so the request is that Miniflare match production. - #6425 reports
put()rejecting anR2ObjectBody.bodystream outright, against the documented types.
If the parity claim here holds, the fix is in this repo. #6425 is tagged upstream:workerd.
[!NOTE] This is an automated, advisory triage report generated by workers-devprod. It is not an official maintainer response — a maintainer will follow up.
🤖 Automated triage report
Issue Triage: cloudflare/workers-sdk#4373
Summary
R2Bucket.put(key, request.body) fails in local dev (miniflare) with "Provided readable stream must have a known length" when the incoming request body is a stream of unknown length, even though the same code works when deployed to production.
Classification
- Type: Bug
- Component:
packages/miniflare(R2 simulation) /packages/wrangler(local dev) - Severity: Medium
- Has Reproduction: Yes (a maintainer supplied a minimal repro in the comments)
- Quality: Complete
Findings
- Created: not specified in context
- Author: vvo (Vincent Voyer)
- Version: wrangler 3.1.1
- Labels: none captured in context
- Comments: 1 (from
mrbbot, a contributor/maintainer)
Analysis
- The reporter hit a parity gap between local dev and production:
env.R2_BUCKET.put(key, request.body)works when deployed but throwsProvided readable stream must have a known length (request/response body or readable half of FixedLengthStream)in miniflare. - A maintainer (
mrbbot) engaged and narrowed the root cause: the error only occurs when the incoming request lacks aContent-Lengthheader, i.e. when the body is a streaming body of unknown length. He posted a clean minimal reproduction (a worker that pipesrequest.bodyinto R2, driven by twofetchcalls — one with a fixed string body that succeeds, one with aReadableStreamof unknown length that throws). - This is a genuine local-dev vs. production divergence in miniflare's R2
putimplementation, which appears to reject unknown-length streams that the real R2 accepts. It is a tooling/simulation bug and belongs in workers-sdk, not workerd or a third-party framework. - The trailing part of the maintainer's comment is truncated in the pre-fetched context (it ends with "...Would you be able to check the ..."), so it is unclear whether he requested additional info from the reporter or the reporter responded. There is no evidence the issue was resolved, duplicated, or closed.
- The workaround mentioned by the reporter is wrapping the stream in a
FixedLengthStream, but that shouldn't be required in local dev to match production.
Recommendation
Status: KEEP OPEN
Reasoning: This is a well-described, reproducible local-dev/production parity bug in miniflare's R2 put handling of unknown-length streams. A maintainer has already confirmed the behaviour and produced a minimal repro, so it is actionable and should stay open for a fix rather than being closed or bounced back for more info.
Action: Route to the miniflare R2 code path that validates stream length on put. Verify against the reporter's repro that unknown-length (chunked) request bodies are accepted in local dev, matching production R2 behaviour. Confirm whether the maintainer's truncated follow-up asked the reporter for anything before treating this as fully diagnosed.
Suggested Labels: (none — repo label set not present in context; omitting rather than guessing)
Suggested Comment
Thanks for the detailed report, and thanks @mrbbot for the minimal repro. This looks like a genuine local-dev vs. production parity gap: miniflare's R2
putrejects a streaming request body of unknown length (noContent-Length), whereas production R2 accepts it. We'll look into aligning the local behaviour so thatenv.R2_BUCKET.put(key, request.body)works without needing aFixedLengthStreamworkaround. If you're still hitting this on the latest version, let us know and we can dig in further.
Structured summary (JSON)
{
"issueNumber": 4373,
"title": "R2 put with request.body fails in miniflare but works in production",
"githubUrl": "https://github.com/cloudflare/workers-sdk/issues/4373",
"recommendation": "KEEP OPEN",
"difficulty": "medium",
"reasoning": "Reproducible local-dev/production parity bug: miniflare's R2 put rejects an unknown-length streaming request body that production R2 accepts. A maintainer already confirmed the behaviour and provided a minimal repro.",
"suggestedAction": "Fix miniflare's R2 put to accept unknown-length streams (no Content-Length), verifying against the maintainer's minimal repro so local dev matches production.",
"hasSuggestedComment": true,
"suggestedLabels": []
}
Closing this as stale, since there's been no activity here for a long time. Locally, a request that has a Content-Length header can now have its request.body passed straight to R2Bucket#put(), and the getPlatformProxy() case from the SvelteKit and Remix comments is tracked in #6504. If you still see this on the latest version, please reply here with details and we'll reopen it.