-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GET request body failing to parse #59
Comments
Im seeing the same issue with response body: Exampleasync function getText (url) {
const response = await fetch(url)
const clone = response.clone()
const text = await clone.text()
// Hangs
}
getText('https://google.com').then(console.log).catch(console.error) |
Hey! 👋 @jonathannorris, I'm pretty confused what you're trying to do here. GET or HEAD requests cannot have bodies. When I try run the following code on a Cloudflare Worker I get a
Would you be able to explain your scenario a little more? @SupremeTechnopriest, if I run your code with Miniflare, but add a |
@mrbbot Thanks for getting back to me! Im using miniflare The actual code is a bit more complicated. The example I provided is just a minimal reproduction. Here is the actual code: export async function rewriteContent (request, response) {
const type = response.headers.get('Content-Type')
if (!type) return response
// Rewrite html content
if (type.startsWith('text/html')) {
const clone = response.clone()
const text = await clone.text()
// Requests hangs here.
// The promise never resolves and the request never times out.
const amp = isAmp(text)
const rewriter = createRewriter(config, amp)
response = await rewriter.transform(response)
}
// Rewrite other types (omitted)
return response
} |
If you could try it out on v16 that would be great. 🙂 I'll have a bit more of a think on what's causing this... |
@mrbbot Tried it on node |
@mrbbot were you able to reproduce this on your end? |
@SupremeTechnopriest no, I haven't been able to reproduce this. I have a potential workaround which I'll try tomorrow and report back to you 👍 |
Appreciate it! |
Ok, here it is. It would be great to check if this issue still occurs with // runner.mjs
import { ConsoleLog, Miniflare } from "miniflare";
import { fetch, Headers, Response, Request, FormData } from "undici";
Headers.prototype.raw = function () {
return Object.fromEntries(
[...this.entries()].map(([key, value]) => [key, [value]])
);
};
const mf = new Miniflare({
scriptPath: "src/index.js",
log: new ConsoleLog(true),
bindings: {
fetch,
Headers,
Response,
Request,
FormData,
},
});
mf.createServer().listen(8787, () => {
mf.log.info("Listening on :8787");
}); With a recent version of Node 16, run Whilst you could use this for developing workers, I wouldn't suggest it. This is more to check whether using Note that if you are returning the result of a addEventListener("fetch", (event) => {
event.respondWith(handleRequest());
});
async function handleRequest() {
const res = await fetch("...");
return new Response(res.body, res);
} This will only work for |
@mrbbot I will give this a go tomorrow and let you know how it works out. Whats the ETA on v2? |
The full release will probably be in a month or so, but hopefully a beta will be ready before then. |
@mrbbot That definitely fixes the clone issue, but some other issues crop up with sub requests getting a 520. Im not 100% convinced that its a problem with miniflare yet, but Im going to continue investigating and I'll get back to you. Looking forward to v2. Let me know when a beta is ready! |
Hey @mrbbot, Been going through this and it seems that when I have caching enabled some sub requests start failing. Im getting the error This is with the Just thought I'd bring it to your attention. I feel like we are almost there. |
Ah whoops, I forgot I do an Line 19 in 31d0579
...so cache won't work when you're using this undici -hack.
When you're getting |
@mrbbot I caught the error and found the cause.
I am mutating the Host header, but I'm also mutating it in other requests as well that don't throw. Cloudflare workers allow you to edit the host header as long as it's the same host as the request url. Any ideas? |
Maybe this is related, I am using // this does not work (hangs forever):
await ky.get('/api').json()
// this works:
const response = await ky.get('/api')
const result = await response.json() Digging further I found that internally they call |
@dan-lee Yeah its the same bug with cloned response. Try shimming with:
|
Thanks for hinting at that @SupremeTechnopriest. Then I get another problem:
There's no more info or stack trace, not sure where this is coming from. |
@dan-lee Hmmm, to me that means that ky isnt setting a credential field and undici requires it to be explicitly set... Have you tested this worker at cloudflare? |
@SupremeTechnopriest Yes, this worker is already in production. It was used with |
Yep me too. Using vanilla fetch I don't have to manually set the credentials field for undici to work... So the problem is likely with the ky wrapper. Seeing that your code works at cloudflare, it could be a bad interaction between ky and undici. Is it possible you are setting |
Wow! Thanks, @SupremeTechnopriest it didn't occur to me that this could actually be the case 🤦 This must be a remnant from earlier development. PS: Some node warnings I get, which might be very well expected
|
@dan-lee Yeah I think those are to be expected and safe to ignore. Glad we figured it out! |
@mrbbot Any thoughts on this one? Im going to start familiarizing myself with the miniflare codebase so I can help out where I can.
|
No, I'm not sure what's going on here. If I think of anything I'll let you know. Just a heads up too, the codebase is currently going through a major restructure for v2. |
Thanks for heads up! |
Looks like |
@mrbbot Awesome! Looking forward to it. Nice catch! |
Hey! 👋 The first pre-release of Miniflare 2 has just been released, using |
@mrbbot Awesome! In testing it out, I'm getting an error decoding brotli responses. The browser is throwing |
This is with cache disabled btw. I tried enabling cache and the file names look good now. Just cant decode the response in the browser. |
@SupremeTechnopriest Let's open a new issue. 👍 |
With an API we are building on Cloudflare Workers we support setting a body on a GET request. It works when it's deployed on a Cloudflare Worker, however we don't get any response from
await event.request.json()
withminiflare
.Thanks for building this awesome tool!
The text was updated successfully, but these errors were encountered: