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(core): Case insensitive MIME type comparison #2956

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions modules/core/src/lib/api/select-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {compareArrayBuffers, path} from '@loaders.gl/loader-utils';
import {normalizeLoader} from '../loader-utils/normalize-loader';
import {log} from '../utils/log';
import {getResourceUrl, getResourceMIMEType} from '../utils/resource-utils';
import {compareMIMETypes} from '../utils/mime-type-utils';
import {getRegisteredLoaders} from './register-loaders';
import {isBlob} from '../../javascript-utils/is-type';
import {stripQueryString} from '../utils/url-utils';
Expand Down Expand Up @@ -216,13 +217,13 @@ function findLoaderByExtension(loaders: Loader[], extension: string): Loader | n

function findLoaderByMIMEType(loaders: Loader[], mimeType: string): Loader | null {
for (const loader of loaders) {
if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
if (loader.mimeTypes?.some((mimeType1) => compareMIMETypes(mimeType, mimeType1))) {
return loader;
}

// Support referring to loaders using the "unregistered tree"
// https://en.wikipedia.org/wiki/Media_type#Unregistered_tree
if (mimeType === `application/x.${loader.id}`) {
if (compareMIMETypes(mimeType, `application/x.${loader.id}`)) {
return loader;
}
}
Expand Down
14 changes: 14 additions & 0 deletions modules/core/src/lib/utils/mime-type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
const DATA_URL_PATTERN = /^data:([-\w.]+\/[-\w.+]+)(;|,)/;
const MIME_TYPE_PATTERN = /^([-\w.]+\/[-\w.+]+)/;

/**
* Compare two MIME types, case insensitively etc.
* @param mimeType1
* @param mimeType2
* @returns true if the MIME types are equivalent
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type
*/
export function compareMIMETypes(mimeType1: string, mimeType2: string): boolean {
if (mimeType1.toLowerCase() === mimeType2.toLowerCase()) {
return true;
}
return false;
}

/**
* Remove extra data like `charset` from MIME types
* @param mimeString
Expand Down
8 changes: 8 additions & 0 deletions modules/core/test/lib/utils/mime-type-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

import test from 'tape-promise/tape';
import {parseMIMEType, parseMIMETypeFromURL} from '@loaders.gl/core/lib/utils/mime-type-utils';
import {compareMIMETypes} from '@loaders.gl/core/lib/utils/mime-type-utils';

const DATA_URL =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAQMAAABIeJ9nAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAGUExURf///wAAAFXC034AAAAMSURBVAjXY3BgaAAAAUQAwetZAwkAAAAASUVORK5CYII=';

test('compareMIMETypes', (t) => {
t.equal(compareMIMETypes('image/png', 'image/jpeg'), false);
t.equal(compareMIMETypes('image/png', 'image/png'), true);
t.equal(compareMIMETypes('image/png', 'image/PNG'), true);
t.end();
});

test('parseMIMEType', (t) => {
t.equal(parseMIMEType('image/png;'), 'image/png');
t.equal(parseMIMEType('image/png'), 'image/png');
Expand Down
Loading