Skip to content

Commit

Permalink
fix(storage): md5 calculation for react native (aws-amplify#13836)
Browse files Browse the repository at this point in the history
* fix(storage): md5 calculation for react native

* chore: update unit tests

* code cleanup

---------

Co-authored-by: Ashwin Kumar <ashwsrir@amazon.com>
  • Loading branch information
ashwinkumar6 and Ashwin Kumar committed Sep 23, 2024
1 parent fab71a7 commit 63bceab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
18 changes: 9 additions & 9 deletions packages/storage/__tests__/providers/s3/utils/md5.native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ describe('calculateContentMd5 (native)', () => {
mockMd5.mockReset();
});

it('calculates MD5 for content type: string', async () => {
await calculateContentMd5(stringContent);
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(stringContent);
expect(mockToBase64).toHaveBeenCalled();
});

it.each([
{ type: 'string', content: stringContent },
{ type: 'ArrayBuffer view', content: new Uint8Array() },
{ type: 'ArrayBuffer', content: new ArrayBuffer(8) },
{ type: 'Blob', content: new Blob([stringContent]) },
])('calculates MD5 for content type: $type', async ({ content }) => {
await calculateContentMd5(content);
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(content);
expect(mockToBase64).toHaveBeenCalled();
});

it('calculates MD5 for content type: blob', async () => {
Object.defineProperty(global, 'FileReader', {
writable: true,
value: jest.fn(() => mockSuccessfulFileReader),
});
await calculateContentMd5(content);
await calculateContentMd5(new Blob([stringContent]));
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(fileReaderResult);
expect(mockSuccessfulFileReader.readAsArrayBuffer).toHaveBeenCalled();
Expand Down
18 changes: 9 additions & 9 deletions packages/storage/__tests__/providers/s3/utils/md5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ describe('calculateContentMd5', () => {
mockMd5.mockReset();
});

it('calculates MD5 for content type: string', async () => {
await calculateContentMd5(stringContent);
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(stringContent);
expect(mockToBase64).toHaveBeenCalled();
});

it.each([
{ type: 'string', content: stringContent },
{ type: 'ArrayBuffer view', content: new Uint8Array() },
{ type: 'ArrayBuffer', content: new ArrayBuffer(8) },
{ type: 'Blob', content: new Blob([stringContent]) },
])('calculates MD5 for content type: $type', async ({ content }) => {
await calculateContentMd5(content);
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(content);
expect(mockToBase64).toHaveBeenCalled();
});

it('calculates MD5 for content type: blob', async () => {
Object.defineProperty(global, 'FileReader', {
writable: true,
value: jest.fn(() => mockSuccessfulFileReader),
});
await calculateContentMd5(content);
await calculateContentMd5(new Blob([stringContent]));
const [mockMd5Instance] = mockMd5.mock.instances;
expect(mockMd5Instance.update.mock.calls[0][0]).toBe(fileReaderResult);
expect(mockSuccessfulFileReader.readAsArrayBuffer).toHaveBeenCalled();
Expand Down
12 changes: 2 additions & 10 deletions packages/storage/src/providers/s3/utils/md5.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ export const calculateContentMd5 = async (
content: Blob | string | ArrayBuffer | ArrayBufferView,
): Promise<string> => {
const hasher = new Md5();
if (typeof content === 'string') {
hasher.update(content);
} else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
const blob = new Blob([content]);
const buffer = await readFile(blob);
hasher.update(buffer);
} else {
const buffer = await readFile(content);
hasher.update(buffer);
}
const buffer = content instanceof Blob ? await readFile(content) : content;
hasher.update(buffer);
const digest = await hasher.digest();

return toBase64(digest);
Expand Down
12 changes: 2 additions & 10 deletions packages/storage/src/providers/s3/utils/md5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ export const calculateContentMd5 = async (
content: Blob | string | ArrayBuffer | ArrayBufferView,
): Promise<string> => {
const hasher = new Md5();
if (typeof content === 'string') {
hasher.update(content);
} else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
const blob = new Blob([content]);
const buffer = await readFile(blob);
hasher.update(buffer);
} else {
const buffer = await readFile(content);
hasher.update(buffer);
}
const buffer = content instanceof Blob ? await readFile(content) : content;
hasher.update(buffer);
const digest = await hasher.digest();

return toBase64(digest);
Expand Down

0 comments on commit 63bceab

Please sign in to comment.