Skip to content

fix: preserve binary response body as Buffer in transformFCResponse#200

Merged
Blankll merged 3 commits into
masterfrom
fix/binary-response-base64
Jun 30, 2026
Merged

fix: preserve binary response body as Buffer in transformFCResponse#200
Blankll merged 3 commits into
masterfrom
fix/binary-response-base64

Conversation

@Blankll

@Blankll Blankll commented Jun 30, 2026

Copy link
Copy Markdown
Member

Problem

When @geek-fun/serverless-adapter returns a response with isBase64Encoded: true for binary content types (e.g. image/png), transformFCResponse was converting the base64-decoded data to a UTF-8 string via Buffer.from(body, 'base64').toString('utf-8').

This corrupted binary data — bytes >= 0x80 became U+FFFD (0xEF 0xBF 0xBD). The result was then passed to respondRaw() which called res.end(string), sending corrupt UTF-8 instead of raw binary bytes.

Fix

Detect binary content types by checking the content-type header:

  • image/, audio/, video/, application/octet-stream, application/pdf, font/ → keep decoded body as Buffer
  • All other types (text, JSON, etc.) → convert to UTF-8 string as before

This allows respondRaw() to call res.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 preservation
  • tests/unit/stack/localStack/aliyunFc.unit.test.ts: Updated PNG test to verify Buffer output with Buffer.equals() byte equality check

Depends On

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

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.42%. Comparing base (f11e2a1) to head (106d50a).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Blankll and others added 2 commits June 30, 2026 14:54
…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>
@Blankll Blankll merged commit 012be75 into master Jun 30, 2026
6 checks passed
@Blankll Blankll deleted the fix/binary-response-base64 branch June 30, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant