-
Notifications
You must be signed in to change notification settings - Fork 829
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
miniflare: compress responses more like Cloudflare FL #5798
Conversation
🦋 Changeset detectedLatest commit: e78938d The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
a145d09
to
094e47b
Compare
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-wrangler-5798 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5798/npm-package-wrangler-5798 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-wrangler-5798 dev path/to/script.js Additional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-create-cloudflare-5798 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-cloudflare-kv-asset-handler-5798 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-miniflare-5798 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-cloudflare-pages-shared-5798 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9033226953/npm-package-cloudflare-vitest-pool-workers-5798 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
}); | ||
t.is(await res.text(), testBody, encoding); | ||
t.is(res.headers.get('Content-Encoding'), encoding, encoding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this was test wasn't actually testing all encodings – the encoding was always "gzip". I added this assertion and it was failing – I had to add the "Accept-Encoding" header above so we are now actually testing each encoding
const contentType = response.headers.get("Content-Type"); | ||
|
||
// if cloudflare's FL does not compress this mime-type, then don't compress locally either | ||
if (!isCompressedByCloudflareFL(contentType)) { | ||
return response; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main fix, in miniflare's entry worker
// Check `Content-Type: text/html` is compressed (FL always compresses html) | ||
res = await fetch(defaultCompressedUrl); | ||
t.is(res.headers.get("Content-Type"), "text/html"); | ||
t.is(res.headers.get("Content-Encoding"), 'gzip'); | ||
t.is(await res.text(), testBody); | ||
|
||
// Check `Content-Type: text/event-stream` is not compressed (FL does not compress this mime type) | ||
res = await fetch(defaultUncompressedUrl); | ||
t.is(res.headers.get("Content-Type"), "text/event-stream"); | ||
t.is(res.headers.get("Content-Encoding"), null); | ||
t.is(await res.text(), testBody); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were the tests confirmed to fail without the fix, passing with the fix
@RamIdeas It works! At least, in this fairly common case of Workers AI use: const body = await env.AI.run('@cf/meta/llama-2-7b-chat-int8', {
prompt: 'Say hi',
stream: true,
});
return new Response(body, { headers: { 'content-type': 'text/event-stream' } }); Tried with some other content types like So, users will still see issues in use cases like streaming HTML rendering – but I suspect there's nothing more for miniflare to do here. 🚀 |
No luck. A simple ![]()
From the original issue:
Not sure if |
since undici has already decompressed the response body
Ah I thought my change to
Since you first posted this, I've thought the response to be malformed. If the body is decompressed, the Content-Encoding header shouldn't imply it is compressed. So I agree we should remove the header. I have done in a following commit. Would you mind trying again? This actually means you will be proxying an uncompressed response. Maybe your proxy will apply its own compression? Not the end of the world locally, but that seems wasteful so, in fact, I think miniflare should use undici.request not undici.fetch but that seemed like a larger change (the options don't match up) so will leave it uncompressed for now. (Also, thanks for your prompt response!) |
b683d6c
to
660d82e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved pending confirmation
Thanks. I've tried adding this commit 660d82e locally and it works well ✅
Yeah, definitely better uncompressed than broken! Whenever we want compressed responses I'll use undici.request manually. And I agree Miniflare should probably do that internally but there might be a good reason why it doesn't already 🤔 |
Thanks for confirming. Will merge now and it'll go out in tomorrow's release |
What this PR solves / how to test
Fixes DEVX-1283
This PR includes the mime-types from the docs and limits miniflare's default compression behaviour to only mime-types that the Cloudflare platform itself compresses
This should make local development more closely match production.
Previous issue comments:
Accept-Encoding
header #5409 (comment)Accept-Encoding: identity
for AI API requests #5713 (comment)Note for reviewers: I updated the whitespacing within the tagged template literals which were still 2-space rather than tabs, please use the 2nd commit to review which is much more minimal (I've also highlighted the important parts below)
Author has addressed the following