index.js 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 compressibleTypeRegExp = /^text\/|\+json$|\+text$|\+xml$/i
  19. var extractTypeRegExp = /^\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 = extractTypeRegExp.exec(type)
  38. var mime = match && match[1].toLowerCase()
  39. var data = db[mime]
  40. if ((data && data.compressible) || compressibleTypeRegExp.test(mime)) {
  41. return true
  42. }
  43. return data
  44. ? data.compressible
  45. : undefined
  46. }