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);