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.

124 lines
3.3 KiB

4 years ago
  1. /*!
  2. * negotiator
  3. * Copyright(c) 2012 Federico Romero
  4. * Copyright(c) 2012-2014 Isaac Z. Schlueter
  5. * Copyright(c) 2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict';
  9. /**
  10. * Cached loaded submodules.
  11. * @private
  12. */
  13. var modules = Object.create(null);
  14. /**
  15. * Module exports.
  16. * @public
  17. */
  18. module.exports = Negotiator;
  19. module.exports.Negotiator = Negotiator;
  20. /**
  21. * Create a Negotiator instance from a request.
  22. * @param {object} request
  23. * @public
  24. */
  25. function Negotiator(request) {
  26. if (!(this instanceof Negotiator)) {
  27. return new Negotiator(request);
  28. }
  29. this.request = request;
  30. }
  31. Negotiator.prototype.charset = function charset(available) {
  32. var set = this.charsets(available);
  33. return set && set[0];
  34. };
  35. Negotiator.prototype.charsets = function charsets(available) {
  36. var preferredCharsets = loadModule('charset').preferredCharsets;
  37. return preferredCharsets(this.request.headers['accept-charset'], available);
  38. };
  39. Negotiator.prototype.encoding = function encoding(available) {
  40. var set = this.encodings(available);
  41. return set && set[0];
  42. };
  43. Negotiator.prototype.encodings = function encodings(available) {
  44. var preferredEncodings = loadModule('encoding').preferredEncodings;
  45. return preferredEncodings(this.request.headers['accept-encoding'], available);
  46. };
  47. Negotiator.prototype.language = function language(available) {
  48. var set = this.languages(available);
  49. return set && set[0];
  50. };
  51. Negotiator.prototype.languages = function languages(available) {
  52. var preferredLanguages = loadModule('language').preferredLanguages;
  53. return preferredLanguages(this.request.headers['accept-language'], available);
  54. };
  55. Negotiator.prototype.mediaType = function mediaType(available) {
  56. var set = this.mediaTypes(available);
  57. return set && set[0];
  58. };
  59. Negotiator.prototype.mediaTypes = function mediaTypes(available) {
  60. var preferredMediaTypes = loadModule('mediaType').preferredMediaTypes;
  61. return preferredMediaTypes(this.request.headers.accept, available);
  62. };
  63. // Backwards compatibility
  64. Negotiator.prototype.preferredCharset = Negotiator.prototype.charset;
  65. Negotiator.prototype.preferredCharsets = Negotiator.prototype.charsets;
  66. Negotiator.prototype.preferredEncoding = Negotiator.prototype.encoding;
  67. Negotiator.prototype.preferredEncodings = Negotiator.prototype.encodings;
  68. Negotiator.prototype.preferredLanguage = Negotiator.prototype.language;
  69. Negotiator.prototype.preferredLanguages = Negotiator.prototype.languages;
  70. Negotiator.prototype.preferredMediaType = Negotiator.prototype.mediaType;
  71. Negotiator.prototype.preferredMediaTypes = Negotiator.prototype.mediaTypes;
  72. /**
  73. * Load the given module.
  74. * @private
  75. */
  76. function loadModule(moduleName) {
  77. var module = modules[moduleName];
  78. if (module !== undefined) {
  79. return module;
  80. }
  81. // This uses a switch for static require analysis
  82. switch (moduleName) {
  83. case 'charset':
  84. module = require('./lib/charset');
  85. break;
  86. case 'encoding':
  87. module = require('./lib/encoding');
  88. break;
  89. case 'language':
  90. module = require('./lib/language');
  91. break;
  92. case 'mediaType':
  93. module = require('./lib/mediaType');
  94. break;
  95. default:
  96. throw new Error('Cannot find module \'' + moduleName + '\'');
  97. }
  98. // Store to prevent invoking require()
  99. modules[moduleName] = module;
  100. return module;
  101. }