index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*!
  2. * accepts
  3. * Copyright(c) 2014 Jonathan Ong
  4. * Copyright(c) 2015 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict'
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var Negotiator = require('negotiator')
  13. var mime = require('mime-types')
  14. /**
  15. * Module exports.
  16. * @public
  17. */
  18. module.exports = Accepts
  19. /**
  20. * Create a new Accepts object for the given req.
  21. *
  22. * @param {object} req
  23. * @public
  24. */
  25. function Accepts (req) {
  26. if (!(this instanceof Accepts)) {
  27. return new Accepts(req)
  28. }
  29. this.headers = req.headers
  30. this.negotiator = new Negotiator(req)
  31. }
  32. /**
  33. * Check if the given `type(s)` is acceptable, returning
  34. * the best match when true, otherwise `undefined`, in which
  35. * case you should respond with 406 "Not Acceptable".
  36. *
  37. * The `type` value may be a single mime type string
  38. * such as "application/json", the extension name
  39. * such as "json" or an array `["json", "html", "text/plain"]`. When a list
  40. * or array is given the _best_ match, if any is returned.
  41. *
  42. * Examples:
  43. *
  44. * // Accept: text/html
  45. * this.types('html');
  46. * // => "html"
  47. *
  48. * // Accept: text/*, application/json
  49. * this.types('html');
  50. * // => "html"
  51. * this.types('text/html');
  52. * // => "text/html"
  53. * this.types('json', 'text');
  54. * // => "json"
  55. * this.types('application/json');
  56. * // => "application/json"
  57. *
  58. * // Accept: text/*, application/json
  59. * this.types('image/png');
  60. * this.types('png');
  61. * // => undefined
  62. *
  63. * // Accept: text/*;q=.5, application/json
  64. * this.types(['html', 'json']);
  65. * this.types('html', 'json');
  66. * // => "json"
  67. *
  68. * @param {String|Array} types...
  69. * @return {String|Array|Boolean}
  70. * @public
  71. */
  72. Accepts.prototype.type =
  73. Accepts.prototype.types = function (types_) {
  74. var types = types_
  75. // support flattened arguments
  76. if (types && !Array.isArray(types)) {
  77. types = new Array(arguments.length)
  78. for (var i = 0; i < types.length; i++) {
  79. types[i] = arguments[i]
  80. }
  81. }
  82. // no types, return all requested types
  83. if (!types || types.length === 0) {
  84. return this.negotiator.mediaTypes()
  85. }
  86. // no accept header, return first given type
  87. if (!this.headers.accept) {
  88. return types[0]
  89. }
  90. var mimes = types.map(extToMime)
  91. var accepts = this.negotiator.mediaTypes(mimes.filter(validMime))
  92. var first = accepts[0]
  93. return first
  94. ? types[mimes.indexOf(first)]
  95. : false
  96. }
  97. /**
  98. * Return accepted encodings or best fit based on `encodings`.
  99. *
  100. * Given `Accept-Encoding: gzip, deflate`
  101. * an array sorted by quality is returned:
  102. *
  103. * ['gzip', 'deflate']
  104. *
  105. * @param {String|Array} encodings...
  106. * @return {String|Array}
  107. * @public
  108. */
  109. Accepts.prototype.encoding =
  110. Accepts.prototype.encodings = function (encodings_) {
  111. var encodings = encodings_
  112. // support flattened arguments
  113. if (encodings && !Array.isArray(encodings)) {
  114. encodings = new Array(arguments.length)
  115. for (var i = 0; i < encodings.length; i++) {
  116. encodings[i] = arguments[i]
  117. }
  118. }
  119. // no encodings, return all requested encodings
  120. if (!encodings || encodings.length === 0) {
  121. return this.negotiator.encodings()
  122. }
  123. return this.negotiator.encodings(encodings)[0] || false
  124. }
  125. /**
  126. * Return accepted charsets or best fit based on `charsets`.
  127. *
  128. * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
  129. * an array sorted by quality is returned:
  130. *
  131. * ['utf-8', 'utf-7', 'iso-8859-1']
  132. *
  133. * @param {String|Array} charsets...
  134. * @return {String|Array}
  135. * @public
  136. */
  137. Accepts.prototype.charset =
  138. Accepts.prototype.charsets = function (charsets_) {
  139. var charsets = charsets_
  140. // support flattened arguments
  141. if (charsets && !Array.isArray(charsets)) {
  142. charsets = new Array(arguments.length)
  143. for (var i = 0; i < charsets.length; i++) {
  144. charsets[i] = arguments[i]
  145. }
  146. }
  147. // no charsets, return all requested charsets
  148. if (!charsets || charsets.length === 0) {
  149. return this.negotiator.charsets()
  150. }
  151. return this.negotiator.charsets(charsets)[0] || false
  152. }
  153. /**
  154. * Return accepted languages or best fit based on `langs`.
  155. *
  156. * Given `Accept-Language: en;q=0.8, es, pt`
  157. * an array sorted by quality is returned:
  158. *
  159. * ['es', 'pt', 'en']
  160. *
  161. * @param {String|Array} langs...
  162. * @return {Array|String}
  163. * @public
  164. */
  165. Accepts.prototype.lang =
  166. Accepts.prototype.langs =
  167. Accepts.prototype.language =
  168. Accepts.prototype.languages = function (languages_) {
  169. var languages = languages_
  170. // support flattened arguments
  171. if (languages && !Array.isArray(languages)) {
  172. languages = new Array(arguments.length)
  173. for (var i = 0; i < languages.length; i++) {
  174. languages[i] = arguments[i]
  175. }
  176. }
  177. // no languages, return all requested languages
  178. if (!languages || languages.length === 0) {
  179. return this.negotiator.languages()
  180. }
  181. return this.negotiator.languages(languages)[0] || false
  182. }
  183. /**
  184. * Convert extnames to mime.
  185. *
  186. * @param {String} type
  187. * @return {String}
  188. * @private
  189. */
  190. function extToMime (type) {
  191. return type.indexOf('/') === -1
  192. ? mime.lookup(type)
  193. : type
  194. }
  195. /**
  196. * Check if mime is valid.
  197. *
  198. * @param {String} type
  199. * @return {String}
  200. * @private
  201. */
  202. function validMime (type) {
  203. return typeof type === 'string'
  204. }