-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from Ilshidur/feature/isMimeType
Feature : isMimeType(str)
- Loading branch information
Showing
8 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = isMimeType; | ||
|
||
var _assertString = require('./util/assertString'); | ||
|
||
var _assertString2 = _interopRequireDefault(_assertString); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/* | ||
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 | ||
*/ | ||
|
||
// 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 ?). | ||
var 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/*" | ||
var 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/*" | ||
var 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 | ||
|
||
function isMimeType(str) { | ||
(0, _assertString2.default)(str); | ||
return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str); | ||
} | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import assertString from './util/assertString'; | ||
|
||
/* | ||
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 | ||
*/ | ||
|
||
// 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 ?). | ||
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/*" | ||
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/*" | ||
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 default function isMimeType(str) { | ||
assertString(str); | ||
return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.