isMimeType.js 2.2 KB

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