diff --git a/__tests__/blob.test.ts b/__tests__/blob.test.ts index c677b11..6ca73e1 100644 --- a/__tests__/blob.test.ts +++ b/__tests__/blob.test.ts @@ -72,6 +72,16 @@ describe('Blob', () => { ) }) + it('binary file without corruption', async () => { + const blob = getBlob('fixtures/blob.bin') + const fileAddition = await blob.load() + expect(fileAddition.contents).toEqual( + fs + .readFileSync(join(__dirname, 'fixtures/blob.bin.base64.txt')) + .toString() + ) + }) + it('file with string', async () => { const blob = getBlob('fixtures/error.txt') const mockStream = new PassThrough() diff --git a/__tests__/fixtures/blob.bin b/__tests__/fixtures/blob.bin new file mode 100644 index 0000000..e9dbd63 Binary files /dev/null and b/__tests__/fixtures/blob.bin differ diff --git a/__tests__/fixtures/blob.bin.base64.txt b/__tests__/fixtures/blob.bin.base64.txt new file mode 100644 index 0000000..3d73d73 --- /dev/null +++ b/__tests__/fixtures/blob.bin.base64.txt @@ -0,0 +1 @@ +iVBORw0KGgr//gAB \ No newline at end of file diff --git a/__tests__/stream/base64-encoder.test.ts b/__tests__/stream/base64-encoder.test.ts index 50933d4..bf6073d 100644 --- a/__tests__/stream/base64-encoder.test.ts +++ b/__tests__/stream/base64-encoder.test.ts @@ -15,4 +15,18 @@ describe('Base64 Encoder', () => { const streamedContent = Buffer.concat(chunks).toString('utf8') expect(streamedContent).toEqual(Buffer.from(content).toString('base64')) }) + + it('binary stream without corruption', async () => { + const binary = Buffer.from([ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0xff, 0xfe, 0x00, 0x01, + ]) + const stream = Readable.from(binary).pipe(new Base64Encoder()) + + const chunks: Buffer[] = [] + for await (const chunk of stream) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + } + const streamedContent = Buffer.concat(chunks).toString('utf8') + expect(streamedContent).toEqual(binary.toString('base64')) + }) }) diff --git a/dist/index.js b/dist/index.js index 9945afe..de6be05 100644 --- a/dist/index.js +++ b/dist/index.js @@ -23957,12 +23957,12 @@ var Base64Encoder = class extends Transform { chunk = chunk.subarray(0, chunk.length - overflowSize); } const base64String = chunk.toString("base64"); - this.push(Buffer2.from(base64String)); + this.push(base64String); callback(); } _flush(callback) { if (this.overflow) { - this.push(Buffer2.from(this.overflow.toString("base64"))); + this.push(this.overflow.toString("base64")); } callback(); } @@ -23979,7 +23979,7 @@ var Blob2 = class { if (!fs.existsSync(this.absolutePath)) { throw new Error(`File does not exist, path: ${this.absolutePath}`); } - return fs.createReadStream(this.absolutePath, { encoding: "utf8" }).pipe(new Base64Encoder()); + return fs.createReadStream(this.absolutePath).pipe(new Base64Encoder()); } async load() { const chunks = []; diff --git a/src/blob.ts b/src/blob.ts index b6d598b..2050f10 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -26,9 +26,7 @@ export class Blob { throw new Error(`File does not exist, path: ${this.absolutePath}`) } - return fs - .createReadStream(this.absolutePath, { encoding: 'utf8' }) - .pipe(new Base64Encoder()) + return fs.createReadStream(this.absolutePath).pipe(new Base64Encoder()) } async load(): Promise { diff --git a/src/stream/base64-encoder.ts b/src/stream/base64-encoder.ts index 671f87e..d8e4e2a 100644 --- a/src/stream/base64-encoder.ts +++ b/src/stream/base64-encoder.ts @@ -25,13 +25,13 @@ export default class Base64Encoder extends Transform { } const base64String = chunk.toString('base64') - this.push(Buffer.from(base64String)) + this.push(base64String) callback() } _flush(callback: TransformCallback): void { if (this.overflow) { - this.push(Buffer.from(this.overflow.toString('base64'))) + this.push(this.overflow.toString('base64')) } callback() }