From 63bceabcaf0085fa46aa2d5c976fed709c63ef9d Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Date: Mon, 23 Sep 2024 13:49:20 -0700 Subject: [PATCH] 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 --- .../providers/s3/utils/md5.native.test.ts | 18 +++++++++--------- .../__tests__/providers/s3/utils/md5.test.ts | 18 +++++++++--------- .../src/providers/s3/utils/md5.native.ts | 12 ++---------- packages/storage/src/providers/s3/utils/md5.ts | 12 ++---------- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/packages/storage/__tests__/providers/s3/utils/md5.native.test.ts b/packages/storage/__tests__/providers/s3/utils/md5.native.test.ts index aea1eab7743..ec70d0a8e14 100644 --- a/packages/storage/__tests__/providers/s3/utils/md5.native.test.ts +++ b/packages/storage/__tests__/providers/s3/utils/md5.native.test.ts @@ -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(); diff --git a/packages/storage/__tests__/providers/s3/utils/md5.test.ts b/packages/storage/__tests__/providers/s3/utils/md5.test.ts index 2de7f7dfd0e..7412d58e571 100644 --- a/packages/storage/__tests__/providers/s3/utils/md5.test.ts +++ b/packages/storage/__tests__/providers/s3/utils/md5.test.ts @@ -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(); diff --git a/packages/storage/src/providers/s3/utils/md5.native.ts b/packages/storage/src/providers/s3/utils/md5.native.ts index 6c43cad24b0..a0c5a2365d8 100644 --- a/packages/storage/src/providers/s3/utils/md5.native.ts +++ b/packages/storage/src/providers/s3/utils/md5.native.ts @@ -14,16 +14,8 @@ export const calculateContentMd5 = async ( content: Blob | string | ArrayBuffer | ArrayBufferView, ): Promise => { 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); diff --git a/packages/storage/src/providers/s3/utils/md5.ts b/packages/storage/src/providers/s3/utils/md5.ts index 80292d95eea..98e04fdaf99 100644 --- a/packages/storage/src/providers/s3/utils/md5.ts +++ b/packages/storage/src/providers/s3/utils/md5.ts @@ -9,16 +9,8 @@ export const calculateContentMd5 = async ( content: Blob | string | ArrayBuffer | ArrayBufferView, ): Promise => { 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);