fix: preserve binary response body as Buffer in transformFCResponse#200
Merged
Conversation
When isBase64Encoded:true and the content type is binary (image/, audio/,
video/, application/octet-stream, application/pdf, font/), keep the
decoded body as a Buffer instead of converting to UTF-8 string.
Previously, Buffer.from(body, 'base64').toString('utf-8') always
converted the decoded data to a UTF-8 string, which corrupted binary
content (bytes >= 0x80 become U+FFFD). This caused image/png responses
to arrive mangled at the HTTP client.
Now binary content types stay as Buffer, allowing respondRaw() to send
raw bytes via res.end(buffer). Text content types continue to be
converted to UTF-8 string for JSON parsing compatibility.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #200 +/- ##
==========================================
+ Coverage 88.38% 88.42% +0.03%
==========================================
Files 145 145
Lines 7639 7643 +4
Branches 1998 2001 +3
==========================================
+ Hits 6752 6758 +6
Misses 397 397
+ Partials 490 488 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…onse - 7 new tests covering all binary content types (JPEG, audio, video, PDF) - Text content types verified to stay as UTF-8 string (not Buffer) - application/octet-stream excluded from binary detection (too generic) - application/json base64 body properly decoded then JSON-parsed Also synced binary detection list with serverless-adapter: removed application/octet-stream.
…=false branches - font/woff2 and font/ttf binary content-type tests (Buffer preservation) - headers=null with base64 body fallback to string - isBase64Encoded=false with binary Content-Type passthrough This brings transformFCResponse binary detection to 100% branch coverage. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
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.
Problem
When
@geek-fun/serverless-adapterreturns a response withisBase64Encoded: truefor binary content types (e.g.image/png),transformFCResponsewas converting the base64-decoded data to a UTF-8 string viaBuffer.from(body, 'base64').toString('utf-8').This corrupted binary data — bytes >= 0x80 became U+FFFD (
0xEF 0xBF 0xBD). The result was then passed torespondRaw()which calledres.end(string), sending corrupt UTF-8 instead of raw binary bytes.Fix
Detect binary content types by checking the
content-typeheader:image/,audio/,video/,application/octet-stream,application/pdf,font/→ keep decoded body as BufferThis allows
respondRaw()to callres.end(buffer)with raw bytes for binary content, while preserving backward compatibility for text/JSON responses.Changed Files
src/stack/localStack/aliyunFc.ts: Binary content-type detection + Buffer preservationtests/unit/stack/localStack/aliyunFc.unit.test.ts: Updated PNG test to verify Buffer output withBuffer.equals()byte equality checkDepends On