Skip to content

Commit ac2ea76

Browse files
committed
fix(storage): honor byteOffset/byteLength in native and server toBase64
1 parent 46eacc1 commit ac2ea76

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

packages/storage/__tests__/client/utils/toBase64.native.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ describe('client/utils/toBase64.native (React Native)', () => {
2121
expect(toBase64(bytes)).toBe('aGVsbG8=');
2222
});
2323

24+
it('encodes a Uint8Array view over an offset into an ArrayBuffer', () => {
25+
const { buffer } = new Uint8Array([0, 0, 104, 101, 108, 108, 111, 0]);
26+
const view = new Uint8Array(buffer, 2, 5); // "hello"
27+
expect(toBase64(view)).toBe('aGVsbG8=');
28+
});
29+
2430
it('encodes an empty ArrayBufferView', () => {
2531
expect(toBase64(new Uint8Array())).toBe('');
2632
});

packages/storage/__tests__/server/utils/toBase64.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ describe('server/utils/toBase64', () => {
2121
expect(toBase64(bytes)).toBe('aGVsbG8=');
2222
});
2323

24+
it('encodes a Uint8Array view over an offset into an ArrayBuffer', () => {
25+
const { buffer } = new Uint8Array([0, 0, 104, 101, 108, 108, 111, 0]);
26+
const view = new Uint8Array(buffer, 2, 5); // "hello"
27+
expect(toBase64(view)).toBe('aGVsbG8=');
28+
});
29+
2430
it('encodes an empty ArrayBufferView', () => {
2531
expect(toBase64(new Uint8Array())).toBe('');
2632
});

packages/storage/src/client/utils/toBase64.native.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ export const toBase64: ToBase64 = input => {
1414
return Buffer.from(input, 'utf-8').toString('base64');
1515
}
1616

17-
return Buffer.from(input.buffer).toString('base64');
17+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength).toString(
18+
'base64',
19+
);
1820
};

packages/storage/src/server/utils/toBase64.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ export const toBase64: ToBase64 = input => {
1515
return Buffer.from(input, 'utf-8').toString('base64');
1616
}
1717

18-
return Buffer.from(input.buffer).toString('base64');
18+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength).toString(
19+
'base64',
20+
);
1921
};

0 commit comments

Comments
 (0)