Skip to content

Commit 1374e43

Browse files
committed
fix: prevent binary file corruption when encoding to base64
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
1 parent d2f2197 commit 1374e43

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.envrc
2+
.idea/
23

34
# Logs
45
logs

dist/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29884,8 +29884,11 @@ class Blob {
2988429884
if (!fs.existsSync(this.absolutePath)) {
2988529885
throw new Error(`File does not exist, path: ${this.absolutePath}`);
2988629886
}
29887+
// Always read files as raw buffers without encoding
29888+
// The Base64Encoder works with buffers for both text and binary files
29889+
// Using any encoding (like 'utf8') corrupts the data
2988729890
return fs
29888-
.createReadStream(this.absolutePath, { encoding: 'utf8' })
29891+
.createReadStream(this.absolutePath)
2988929892
.pipe(new base64_encoder_1.default());
2989029893
}
2989129894
load() {
@@ -30591,12 +30594,12 @@ class Base64Encoder extends node_stream_1.Transform {
3059130594
chunk = chunk.subarray(0, chunk.length - overflowSize);
3059230595
}
3059330596
const base64String = chunk.toString('base64');
30594-
this.push(node_buffer_1.Buffer.from(base64String));
30597+
this.push(base64String);
3059530598
callback();
3059630599
}
3059730600
_flush(callback) {
3059830601
if (this.overflow) {
30599-
this.push(node_buffer_1.Buffer.from(this.overflow.toString('base64')));
30602+
this.push(this.overflow.toString('base64'));
3060030603
}
3060130604
callback();
3060230605
}

src/stream/base64-encoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export default class Base64Encoder extends Transform {
2525
}
2626

2727
const base64String = chunk.toString('base64')
28-
this.push(Buffer.from(base64String))
28+
this.push(base64String)
2929
callback()
3030
}
3131

3232
_flush(callback: TransformCallback): void {
3333
if (this.overflow) {
34-
this.push(Buffer.from(this.overflow.toString('base64')))
34+
this.push(this.overflow.toString('base64'))
3535
}
3636
callback()
3737
}

0 commit comments

Comments
 (0)