-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed for both platforms? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
const buffer = content instanceof Blob ? await readFile(content) : content; | ||
hasher.update(buffer); | ||
const digest = await hasher.digest(); | ||
|
||
return toBase64(digest); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IF this works for web then why remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
const buffer = content instanceof Blob ? await readFile(content) : content; | ||
hasher.update(buffer); | ||
const digest = await hasher.digest(); | ||
|
||
return toBase64(digest); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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,