Skip to content

Commit 63bceab

Browse files
ashwinkumar6Ashwin Kumar
andauthored
fix(storage): md5 calculation for react native (#13836)
* fix(storage): md5 calculation for react native * chore: update unit tests * code cleanup --------- Co-authored-by: Ashwin Kumar <[email protected]>
1 parent fab71a7 commit 63bceab

File tree

4 files changed

+22
-38
lines changed

4 files changed

+22
-38
lines changed

packages/storage/__tests__/providers/s3/utils/md5.native.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,23 @@ describe('calculateContentMd5 (native)', () => {
6969
mockMd5.mockReset();
7070
});
7171

72-
it('calculates MD5 for content type: string', async () => {
73-
await calculateContentMd5(stringContent);
74-
const [mockMd5Instance] = mockMd5.mock.instances;
75-
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(stringContent);
76-
expect(mockToBase64).toHaveBeenCalled();
77-
});
78-
7972
it.each([
73+
{ type: 'string', content: stringContent },
8074
{ type: 'ArrayBuffer view', content: new Uint8Array() },
8175
{ type: 'ArrayBuffer', content: new ArrayBuffer(8) },
82-
{ type: 'Blob', content: new Blob([stringContent]) },
8376
])('calculates MD5 for content type: $type', async ({ content }) => {
77+
await calculateContentMd5(content);
78+
const [mockMd5Instance] = mockMd5.mock.instances;
79+
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(content);
80+
expect(mockToBase64).toHaveBeenCalled();
81+
});
82+
83+
it('calculates MD5 for content type: blob', async () => {
8484
Object.defineProperty(global, 'FileReader', {
8585
writable: true,
8686
value: jest.fn(() => mockSuccessfulFileReader),
8787
});
88-
await calculateContentMd5(content);
88+
await calculateContentMd5(new Blob([stringContent]));
8989
const [mockMd5Instance] = mockMd5.mock.instances;
9090
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(fileReaderResult);
9191
expect(mockSuccessfulFileReader.readAsArrayBuffer).toHaveBeenCalled();

packages/storage/__tests__/providers/s3/utils/md5.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ describe('calculateContentMd5', () => {
4646
mockMd5.mockReset();
4747
});
4848

49-
it('calculates MD5 for content type: string', async () => {
50-
await calculateContentMd5(stringContent);
51-
const [mockMd5Instance] = mockMd5.mock.instances;
52-
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(stringContent);
53-
expect(mockToBase64).toHaveBeenCalled();
54-
});
55-
5649
it.each([
50+
{ type: 'string', content: stringContent },
5751
{ type: 'ArrayBuffer view', content: new Uint8Array() },
5852
{ type: 'ArrayBuffer', content: new ArrayBuffer(8) },
59-
{ type: 'Blob', content: new Blob([stringContent]) },
6053
])('calculates MD5 for content type: $type', async ({ content }) => {
54+
await calculateContentMd5(content);
55+
const [mockMd5Instance] = mockMd5.mock.instances;
56+
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(content);
57+
expect(mockToBase64).toHaveBeenCalled();
58+
});
59+
60+
it('calculates MD5 for content type: blob', async () => {
6161
Object.defineProperty(global, 'FileReader', {
6262
writable: true,
6363
value: jest.fn(() => mockSuccessfulFileReader),
6464
});
65-
await calculateContentMd5(content);
65+
await calculateContentMd5(new Blob([stringContent]));
6666
const [mockMd5Instance] = mockMd5.mock.instances;
6767
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(fileReaderResult);
6868
expect(mockSuccessfulFileReader.readAsArrayBuffer).toHaveBeenCalled();

packages/storage/src/providers/s3/utils/md5.native.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@ export const calculateContentMd5 = async (
1414
content: Blob | string | ArrayBuffer | ArrayBufferView,
1515
): Promise<string> => {
1616
const hasher = new Md5();
17-
if (typeof content === 'string') {
18-
hasher.update(content);
19-
} else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
20-
const blob = new Blob([content]);
21-
const buffer = await readFile(blob);
22-
hasher.update(buffer);
23-
} else {
24-
const buffer = await readFile(content);
25-
hasher.update(buffer);
26-
}
17+
const buffer = content instanceof Blob ? await readFile(content) : content;
18+
hasher.update(buffer);
2719
const digest = await hasher.digest();
2820

2921
return toBase64(digest);

packages/storage/src/providers/s3/utils/md5.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@ export const calculateContentMd5 = async (
99
content: Blob | string | ArrayBuffer | ArrayBufferView,
1010
): Promise<string> => {
1111
const hasher = new Md5();
12-
if (typeof content === 'string') {
13-
hasher.update(content);
14-
} else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
15-
const blob = new Blob([content]);
16-
const buffer = await readFile(blob);
17-
hasher.update(buffer);
18-
} else {
19-
const buffer = await readFile(content);
20-
hasher.update(buffer);
21-
}
12+
const buffer = content instanceof Blob ? await readFile(content) : content;
13+
hasher.update(buffer);
2214
const digest = await hasher.digest();
2315

2416
return toBase64(digest);

0 commit comments

Comments
 (0)