Skip to content

Commit 57bf0c9

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 58e439d commit 57bf0c9

6 files changed

Lines changed: 21 additions & 4 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ 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.readFileSync(join(__dirname, 'fixtures/blob.bin.base64.txt')).toString()
80+
)
81+
})
82+
7583
it('file with string', async () => {
7684
const blob = getBlob('fixtures/error.txt')
7785
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,16 @@ 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([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0xff, 0xfe, 0x00, 0x01])
21+
const stream = Readable.from(binary).pipe(new Base64Encoder())
22+
23+
const chunks: Buffer[] = []
24+
for await (const chunk of stream) {
25+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
26+
}
27+
const streamedContent = Buffer.concat(chunks).toString('utf8')
28+
expect(streamedContent).toEqual(binary.toString('base64'))
29+
})
1830
})

src/blob.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ 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
3229
return fs
3330
.createReadStream(this.absolutePath)
3431
.pipe(new Base64Encoder())

0 commit comments

Comments
 (0)