fix(studio-server): preserve binary bodies in file PUT/POST#1914
Open
yegor177 wants to merge 1 commit into
Open
fix(studio-server): preserve binary bodies in file PUT/POST#1914yegor177 wants to merge 1 commit into
yegor177 wants to merge 1 commit into
Conversation
c.req.text() decodes the request body as UTF-8, replacing invalid bytes with U+FFFD — binary uploads (fonts, images) were silently corrupted on disk. Read the raw bytes and write the buffer instead; byte-exact for text bodies too.
miguel-heygen
approved these changes
Jul 3, 2026
| .arrayBuffer() | ||
| .then((b) => Buffer.from(b)) | ||
| .catch(() => Buffer.alloc(0)); | ||
| writeFileSync(res.absPath, body); |
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
PUT/POST /api/projects/:id/files/*in@hyperframes/studio-serverread the request body withc.req.text()and write it withwriteFileSync(path, body, "utf-8"). Any body that is not valid UTF-8 — fonts, images, any binary asset — gets its non-UTF-8 bytes replaced with U+FFFD during the decode, so the file on disk is silently corrupted (and typically inflated). TheContent-Typeheader is ignored.Repro
Observed with a 20 828-byte OTF font arriving as 37 041 bytes on disk;
@font-facethen falls back silently, which makes this painful to diagnose.Fix
Read the raw bytes (
Buffer.from(await c.req.arrayBuffer())) and write the buffer without an encoding. This is byte-exact for text bodies too (UTF-8 text round-trips unchanged), so existing text uploads keep working — no content-type sniffing needed.Testing
files.test.ts: POST + PUT with bodies containing invalid-UTF-8 bytes round-trip byte-for-byte.