UNPKG

2.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = isMimeType;
7
8var _assertString = _interopRequireDefault(require("./util/assertString"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12/*
13 Checks if the provided string matches to a correct Media type format (MIME type)
14
15 This function only checks is the string format follows the
16 etablished rules by the according RFC specifications.
17 This function supports 'charset' in textual media types
18 (https://tools.ietf.org/html/rfc6657).
19
20 This function does not check against all the media types listed
21 by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)
22 because of lightness purposes : it would require to include
23 all these MIME types in this librairy, which would weigh it
24 significantly. This kind of effort maybe is not worth for the use that
25 this function has in this entire librairy.
26
27 More informations in the RFC specifications :
28 - https://tools.ietf.org/html/rfc2045
29 - https://tools.ietf.org/html/rfc2046
30 - https://tools.ietf.org/html/rfc7231#section-3.1.1.1
31 - https://tools.ietf.org/html/rfc7231#section-3.1.1.5
32*/
33// Match simple MIME types
34// NB :
35// Subtype length must not exceed 100 characters.
36// This rule does not comply to the RFC specs (what is the max length ?).
37var mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len
38// Handle "charset" in "text/*"
39
40var 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
41// Handle "boundary" in "multipart/*"
42
43var 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
44
45function isMimeType(str) {
46 (0, _assertString.default)(str);
47 return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str);
48}
49
50module.exports = exports.default;
\No newline at end of file