templates/worker-tinygo: make /echo work under wrangler dev - #204
Merged
Conversation
io.Copy(w, req.Body) triggers readableStreamToReadCloser.WriteTo's fast path, which hands the request's raw ReadableStream to the JS Response object. miniflare's proxy worker cannot relay such a response and fails with 'Body has already been used', so the template's /echo endpoint breaks out of the box under wrangler dev (it works when deployed to production workerd). Buffer the body before writing, matching the worker-go template. Fixes syumai#176
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
worker-tinygotemplate's/echoendpoint fails underwrangler devwith:This PR fixes the template so it works out of the box, and documents why in a comment. Fixes #176.
Root cause
io.Copy(w, req.Body)triggersreadableStreamToReadCloser.WriteTo's fast path ininternal/jsutil/stream.go, which passes the request's raw ReadableStream through to the JSResponseobject viaWriteRawJSBody.This zero-copy passthrough is valid on production workerd, but miniflare routes every request through a proxy worker (
miniflare/dist/src/workers/core/entry.worker.js), and that layer cannot relay a response whose body is the incoming request's own stream —await service.fetch(request)throws the error above.This is an environment limitation, not a bug in this library. A plain JavaScript worker reproduces it under
wrangler devwith no Go involved:(Related: cloudflare/workers-sdk#4373, another "streams work in production but not in miniflare" case.)
Since
wrangler devis the first thing every new user of the template runs, the template shouldn't rely on the passthrough. Theworker-gotemplate already buffers the body in/echo, which is why only the tinygo template was affected; this change aligns the two.Testing
Verified with TinyGo 0.41.1 + wrangler 4.124.0 (and wrangler 3.109) on macOS:
/hellounaffected.