Skip to content

Commit

Permalink
fix: 🐛 loadFromURL for application/octet-stream Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Apr 24, 2024
1 parent f1a4e63 commit eeda1d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/dotlottie-js/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ export async function loadFromURL(src: string): Promise<Uint8Array> {

const contentType = response.headers.get('content-type');

if (!contentType?.includes('application/zip')) {
if (!['application/zip', 'application/octet-stream'].includes(contentType || '')) {
throw new DotLottieError(
'Invalid content type provided for .lottie file, expected application/zip',
'Invalid content type provided for .lottie file, expected application/zip or application/octet-stream',
ErrorCodes.INVALID_DOTLOTTIE,
);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/dotlottie-js/src/tests/utils-browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe('loadFromUrl', () => {
);
});

it('loads a dotlottie from a url with content-type application/octet-stream', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/octet-stream' } })),
);

const dotLottieURL = 'https://lottiefiles.fake/animation/animation.lottie';

const dotLottie = await loadFromURL(dotLottieURL);

expect(dotLottie).toBeDefined();
expect(dotLottie).toBeInstanceOf(Uint8Array);

expect(fetchSpy).toHaveBeenCalledWith(dotLottieURL);
});

it('loads a dotlottie from a url', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/zip' } })),
Expand Down
15 changes: 15 additions & 0 deletions packages/dotlottie-js/src/tests/utils-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe('loadFromUrl', () => {
);
});

it('loads a dotlottie from a url with content-type application/octet-stream', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/octet-stream' } })),
);

const dotLottieURL = 'https://lottiefiles.fake/animation/animation.lottie';

const dotLottie = await loadFromURL(dotLottieURL);

expect(dotLottie).toBeDefined();
expect(dotLottie).toBeInstanceOf(Uint8Array);

expect(fetchSpy).toHaveBeenCalledWith(dotLottieURL);
});

it('loads a dotlottie from a url', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/zip' } })),
Expand Down

0 comments on commit eeda1d3

Please sign in to comment.