Repository navigation
fix(response): keep the Blob content type instead of forcing octet-stream - #1987
Conversation
…ream
Assigning a Blob to ctx.body forced Content-Type: application/octet-stream
and ignored the Blob's own type. A typed Blob such as
new Blob(data, { type: 'image/png' }) was sent with the wrong content type,
so browsers download the payload instead of rendering it. The Response body
path already copies the body's own Content-Type, and the original feature
request (koajs#1777) specified ctx.type = blob.type.
Use val.type when present and fall back to application/octet-stream for an
untyped Blob. An explicitly preset Content-Type still wins.
📝 WalkthroughWalkthroughThe Compact metadata:
Related issues: None specified Related PRs: None specified Suggested labels: bug, tests Suggested reviewers: None specified Poem: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts Koa’s response body handling so that when a Blob is assigned to ctx.body, its own MIME type is preserved (if present) instead of always defaulting to application/octet-stream, and adds tests to lock in this behavior and ensure explicit content types remain authoritative. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1987 +/- ##
=======================================
Coverage 99.90% 99.90%
=======================================
Files 9 9
Lines 2127 2127
=======================================
Hits 2125 2125
Misses 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Ziiyodullayevv
left a comment
There was a problem hiding this comment.
Good fix for Blob content type handling. Keeping the original content type instead of forcing octet-stream preserves the intended MIME type.
|
Nudge on this one — approved on 2 August, still mergeable, no open threads. Happy to rebase if that helps. |
yowainwright
left a comment
There was a problem hiding this comment.
LGTM! Thanks for the thoughtful work and the nudge @spokodev! 🥇
When a Blob is assigned to
ctx.body, Koa forcesContent-Type: application/octet-streamand ignores the Blob's owntype. A Blob carries its MIME type inblob.type, so a typed Blob such asnew Blob(data, { type: 'image/png' })(or a Blob returned byfetch(...).then(r => r.blob())) is sent with the wrong content type. Browsers then download the payload instead of rendering it, and any client that negotiates onContent-Typesees the wrong value.The sibling
Responsebody path already copies the body's ownContent-Type, and the original feature request (#1777) specifiedctx.type = blob.type, so the Blob path is the outlier.Cause
In
lib/response.js, the Blob branch of the body setter is:'bin'maps throughmime-typestoapplication/octet-stream, discardingval.type. ThesetTypeguard (an explicitly preset Content-Type wins) is correct; only the fallback value is wrong.Fix
Honor the Blob's own MIME type when present, and fall back to
application/octet-streamfor an untyped Blob (the existing default). An explicitctx.typestill wins.Added two tests to
__tests__/response/body.test.js: a typed Blob keeps its type, and an explicit content type is not overridden. The first fails before this change; the full suite (442) stays green.Summary by Sourcery
Preserve Blob MIME types in response headers instead of defaulting typed Blobs to application/octet-stream.
Bug Fixes:
Tests:
Summary by CodeRabbit