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: webp detection #63

Merged
merged 5 commits into from
Feb 20, 2024
Merged

fix: webp detection #63

merged 5 commits into from
Feb 20, 2024

Conversation

samuelOsborne
Copy link
Collaborator

No description provided.

Copy link

changeset-bot bot commented Feb 19, 2024

🦋 Changeset detected

Latest commit: 753fc9d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@dotlottie/dotlottie-js Patch
next Patch
node Patch
nuxt-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Feb 19, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
dotlottie-js 21.23 KB (+0.92% 🔺) 425 ms (+0.92% 🔺) 300 ms (+2.11% 🔺) 725 ms

Copy link
Member

@theashraf theashraf left a comment

Choose a reason for hiding this comment

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

Overall, it looks good, but it raises a question I have in mind: should we fail or throw an error instead of assuming the file type is PNG if we can't detect its mime_code against the map of all known mime_codes?

Also It would also be good to conduct a quick verification of the existing mime_code signatures to include the full signature of the file we're verifying, rather than just the first few bytes of the entire signature.

What do you think?

packages/dotlottie-js/src/common/utils.ts Outdated Show resolved Hide resolved
@@ -38,10 +38,10 @@ export const MIME_CODES: MimeCodes = {
png: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
gif: [0x47, 0x49, 0x46],
bmp: [0x42, 0x4d],
webp: [0x52, 0x49, 0x46, 0x46, 0x57, 0x45, 0x42, 0x50],
webp: [0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50],
Copy link
Member

Choose a reason for hiding this comment

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

[suggestion]

Replace 0x00 with another placeholder to explicitly indicate that these values are not necessary for the MIME type check. For example:

webp: [0x52, 0x49, 0x46, 0x46, "?", "?", "?", "?", 0x57, 0x45, 0x42, 0x50],

Comment on lines 182 to 183
const riff = Array.from(bufData.subarray(0, 4));
const webpCheck = Array.from(bufData.subarray(8, 12));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const riff = Array.from(bufData.subarray(0, 4));
const webpCheck = Array.from(bufData.subarray(8, 12));
const riffHeader = Array.from(bufData.subarray(0, 4));
const webpFormatMarker = Array.from(bufData.subarray(8, 12));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done @theashraf

@@ -358,17 +358,17 @@ export class DotLottie extends DotLottieCommon {
}
}
}
} catch (err) {
} catch (err: any) {
Copy link
Member

Choose a reason for hiding this comment

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

isn't suppose to be instance of Error ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done @theashraf

'image_4.png',
'image_5.png',
'image_9.png',
]);
expect(uniqueImages.map((image) => image.id)).toEqual(['image_1', 'image_2', 'image_4', 'image_5', 'image_9']);
});
});

it('Properly detects mimetype of images.', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

[suggestion]
Add unit tests for getMimeTypeFromBase64 to cover the logic for detecting all supported MIME types. The responsibility for preventing similar issues in the future lies with getMimeTypeFromBase64.

Since the logic for inlining assets is already covered by other tests, I don't see the value in adding more integration tests to verify if getMimeTypeFromBase64 is functioning as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added test for all supported file type @theashraf

Comment on lines +264 to +281
try {
let videoDotLottie = new DotLottie();

videoDotLottie = await videoDotLottie.fromArrayBuffer(VIDEO_DOTLOTTIE);

const videoAnimation = videoDotLottie.getAnimations();

if (videoAnimation) {
videoAnimation.map(async (animation) => {
await animation[1].toJSON({
inlineAssets: true,
});
});
}
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
Copy link
Member

Choose a reason for hiding this comment

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

[suggestion]
Consider moving this assertion to an independent test case, as it checks for an error when an unsupported MIME type is used. This differs from the primary intention of the test case, which is to assert the happy path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done @theashraf

@samuelOsborne samuelOsborne merged commit 94a9a93 into main Feb 20, 2024
2 checks passed
@samuelOsborne samuelOsborne deleted the fix/webp-detection branch February 20, 2024 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants