Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storage): md5 calculation for react native #13836

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why take this test out?

Copy link
Member Author

@ashwinkumar6 ashwinkumar6 Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just refactored the test. we're testing all 4 data input types in unit tests,

])('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]) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Member Author

@ashwinkumar6 ashwinkumar6 Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replied on this comment

])('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
10 changes: 5 additions & 5 deletions packages/storage/src/providers/s3/utils/md5.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const calculateContentMd5 = async (
content: Blob | string | ArrayBuffer | ArrayBufferView,
): Promise<string> => {
const hasher = new Md5();
if (typeof content === 'string') {
if (
typeof content === 'string' ||
ArrayBuffer.isView(content) ||
content instanceof ArrayBuffer
ashwinkumar6 marked this conversation as resolved.
Show resolved Hide resolved
) {
hasher.update(content);
} else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
const blob = new Blob([content]);
Copy link
Member Author

@ashwinkumar6 ashwinkumar6 Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blob([arrayBuffer]) and Blob([arrayBufferView]) works in web but not RN. This can be removed for both platforms

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed for both platforms?

Copy link
Member Author

@ashwinkumar6 ashwinkumar6 Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, since it's un-necessary code removing it from both web and RN

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF this works for web then why remove it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, since it's un-necessary code at this point removing it from both web and RN

const buffer = await readFile(blob);
hasher.update(buffer);
} else {
const buffer = await readFile(content);
hasher.update(buffer);
Expand Down
Loading