mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
bbaf619f63
swc_bundler: - [x] Fix wrapped esms. (denoland/deno#9307) - [x] Make test secure.
58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
// Loaded from https://deno.land/x/segno@v1.1.0/lib/validations/isMimeType.ts
|
|
|
|
|
|
/*
|
|
Checks if the provided string matches to a correct Media type format (MIME type)
|
|
|
|
This function only checks is the string format follows the
|
|
etablished rules by the according RFC specifications.
|
|
This function supports 'charset' in textual media types
|
|
(https://tools.ietf.org/html/rfc6657).
|
|
|
|
This function does not check against all the media types listed
|
|
by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)
|
|
because of lightness purposes : it would require to include
|
|
all these MIME types in this librairy, which would weigh it
|
|
significantly. This kind of effort maybe is not worth for the use that
|
|
this function has in this entire librairy.
|
|
|
|
More informations in the RFC specifications :
|
|
- https://tools.ietf.org/html/rfc2045
|
|
- https://tools.ietf.org/html/rfc2046
|
|
- https://tools.ietf.org/html/rfc7231#section-3.1.1.1
|
|
- https://tools.ietf.org/html/rfc7231#section-3.1.1.5
|
|
*/
|
|
|
|
// @ts-ignore allowing typedoc to build
|
|
import { assertString } from '../helpers/assertString.ts';
|
|
|
|
// Match simple MIME types
|
|
// NB :
|
|
// Subtype length must not exceed 100 characters.
|
|
// This rule does not comply to the RFC specs (what is the max length ?).
|
|
/**
|
|
* @ignore
|
|
*/
|
|
const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len
|
|
|
|
// Handle "charset" in "text/*"
|
|
/**
|
|
* @ignore
|
|
*/
|
|
const mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len
|
|
|
|
// Handle "boundary" in "multipart/*"
|
|
/**
|
|
* @ignore
|
|
*/
|
|
const mimeTypeMultipart = /^multipart\/[a-zA-Z0-9\.\-\+]{1,100}(;\s?(boundary|charset)=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?){0,2}$/i; // eslint-disable-line max-len
|
|
|
|
export const isMimeType = (str: string) => {
|
|
assertString(str);
|
|
return (
|
|
mimeTypeSimple.test(str) ||
|
|
mimeTypeText.test(str) ||
|
|
mimeTypeMultipart.test(str)
|
|
);
|
|
};
|