fix: resolve binary file corruption when committing images and binary files#210
Open
Nr18 wants to merge 2 commits intoryancyq:mainfrom
Open
fix: resolve binary file corruption when committing images and binary files#210Nr18 wants to merge 2 commits intoryancyq:mainfrom
Nr18 wants to merge 2 commits intoryancyq:mainfrom
Conversation
**Location:** `src/blob.ts`, line 28
The code was reading files with UTF-8 text encoding:
```typescript
fs.createReadStream(this.absolutePath, { encoding: 'utf8' })
```
Images are **binary files** containing arbitrary byte values (0-255). The UTF-8 encoding option tells Node.js to:
1. Interpret the binary data as UTF-8 text
2. Drop any byte sequences that are not valid UTF-8
This causes data loss because:
- Valid UTF-8 sequences are 1-4 bytes depending on the character
- Many byte combinations in binary files are invalid UTF-8 sequences
- These invalid bytes are silently dropped by the UTF-8 decoder
- The resulting file is corrupted (missing bytes)
The base64-encoder was converting base64 strings back to buffers before pushing them to the stream, which caused them to be re-encoded as UTF-8 bytes. This corrupted binary files (images, PDFs, etc.) when they were committed and pushed to GitHub. Changes: - base64-encoder.ts: Push base64 strings directly instead of converting them back to buffers. This prevents the double-encoding that was corrupting binary data. - blob.ts: Remove UTF-8 encoding from file stream reading. Files are now read as raw buffers, which is necessary for the base64 encoder to work correctly with both text and binary files. The fix ensures that: - Binary files (PNG, JPG, PDF, etc.) are encoded without corruption - Text files continue to work correctly - The action properly base64 encodes file content for GitHub API Fixes image corruption when committing and pushing images to GitHub
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.
Overview
This PR fixes a critical issue where binary files (images, PDFs, etc.) were being corrupted when committed and pushed to GitHub through this action. The root cause was improper handling of file encoding in both the file reading and base64 encoding streams.
Problem Statement
When users committed image files (PNG, JPG, etc.) using this action, the images would become corrupted and unable to open after being pushed to the repository. This was caused by two issues:
File Reading: Files were being read with UTF-8 text encoding, which drops invalid UTF-8 byte sequences. Binary files contain arbitrary byte values that aren't valid UTF-8, resulting in data loss.
Base64 Encoding: The Base64Encoder was converting base64 strings back to Buffers before pushing them to the stream, causing double-encoding that further corrupted the data.
Root Cause Analysis
encoding: 'utf8'increateReadStream(), which corrupted binary dataBuffer.from(base64String)which re-encoded the base64 string as UTF-8 bytes, breaking the encoded contentChanges Made
1. src/blob.ts
fs.createReadStream()2. src/stream/base64-encoder.ts
this.push(Buffer.from(base64String))this.push(base64String)Testing
npm run build- dist folder updatedImpact
Files Changed
Related Issues
Closes the image corruption issue when using this action to commit binary files