You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.0 KiB

4 years ago
  1. /*!
  2. * compressible
  3. * Copyright(c) 2013 Jonathan Ong
  4. * Copyright(c) 2014 Jeremiah Senkpiel
  5. * Copyright(c) 2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict'
  9. /**
  10. * Module dependencies.
  11. * @private
  12. */
  13. var db = require('mime-db')
  14. /**
  15. * Module variables.
  16. * @private
  17. */
  18. var COMPRESSIBLE_TYPE_REGEXP = /^text\/|\+(?:json|text|xml)$/i
  19. var EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/
  20. /**
  21. * Module exports.
  22. * @public
  23. */
  24. module.exports = compressible
  25. /**
  26. * Checks if a type is compressible.
  27. *
  28. * @param {string} type
  29. * @return {Boolean} compressible
  30. * @public
  31. */
  32. function compressible (type) {
  33. if (!type || typeof type !== 'string') {
  34. return false
  35. }
  36. // strip parameters
  37. var match = EXTRACT_TYPE_REGEXP.exec(type)
  38. var mime = match && match[1].toLowerCase()
  39. var data = db[mime]
  40. // return database information
  41. if (data && data.compressible !== undefined) {
  42. return data.compressible
  43. }
  44. // fallback to regexp or unknown
  45. return COMPRESSIBLE_TYPE_REGEXP.test(mime) || undefined
  46. }