Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Binary file added __tests__/fixtures/blob.bin
Binary file not shown.
1 change: 1 addition & 0 deletions __tests__/fixtures/blob.bin.base64.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iVBORw0KGgr//gAB
14 changes: 14 additions & 0 deletions __tests__/stream/base64-encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})
6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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 = [];
Expand Down
4 changes: 1 addition & 3 deletions src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileAddition> {
Expand Down
4 changes: 2 additions & 2 deletions src/stream/base64-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Loading