Skip to content

Commit 91b13b4

Browse files
committed
fix: add binary regression tests to Base64Encoder and Blob.load() using a fixture with non-UTF-8 byte sequences to guard against silent data corruption
1 parent 96ffeb0 commit 91b13b4

6 files changed

Lines changed: 26 additions & 7 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.envrc
2-
.idea/
32

43
# Logs
54
logs

__tests__/blob.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ describe('Blob', () => {
7272
)
7373
})
7474

75+
it('binary file without corruption', async () => {
76+
const blob = getBlob('fixtures/blob.bin')
77+
const fileAddition = await blob.load()
78+
expect(fileAddition.contents).toEqual(
79+
fs
80+
.readFileSync(join(__dirname, 'fixtures/blob.bin.base64.txt'))
81+
.toString()
82+
)
83+
})
84+
7585
it('file with string', async () => {
7686
const blob = getBlob('fixtures/error.txt')
7787
const mockStream = new PassThrough()

__tests__/fixtures/blob.bin

12 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iVBORw0KGgr//gAB

__tests__/stream/base64-encoder.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,18 @@ describe('Base64 Encoder', () => {
1515
const streamedContent = Buffer.concat(chunks).toString('utf8')
1616
expect(streamedContent).toEqual(Buffer.from(content).toString('base64'))
1717
})
18+
19+
it('binary stream without corruption', async () => {
20+
const binary = Buffer.from([
21+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0xff, 0xfe, 0x00, 0x01,
22+
])
23+
const stream = Readable.from(binary).pipe(new Base64Encoder())
24+
25+
const chunks: Buffer[] = []
26+
for await (const chunk of stream) {
27+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
28+
}
29+
const streamedContent = Buffer.concat(chunks).toString('utf8')
30+
expect(streamedContent).toEqual(binary.toString('base64'))
31+
})
1832
})

src/blob.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ export class Blob {
2626
throw new Error(`File does not exist, path: ${this.absolutePath}`)
2727
}
2828

29-
// Always read files as raw buffers without encoding
30-
// The Base64Encoder works with buffers for both text and binary files
31-
// Using any encoding (like 'utf8') corrupts the data
32-
return fs
33-
.createReadStream(this.absolutePath)
34-
.pipe(new Base64Encoder())
29+
return fs.createReadStream(this.absolutePath).pipe(new Base64Encoder())
3530
}
3631

3732
async load(): Promise<FileAddition> {

0 commit comments

Comments
 (0)