123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var path = require('path');
  2. var fs = require('fs');
  3. function Mime() {
  4. // Map of extension -> mime type
  5. this.types = Object.create(null);
  6. // Map of mime type -> extension
  7. this.extensions = Object.create(null);
  8. }
  9. /**
  10. * Define mimetype -> extension mappings. Each key is a mime-type that maps
  11. * to an array of extensions associated with the type. The first extension is
  12. * used as the default extension for the type.
  13. *
  14. * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
  15. *
  16. * @param map (Object) type definitions
  17. */
  18. Mime.prototype.define = function (map) {
  19. for (var type in map) {
  20. var exts = map[type];
  21. for (var i = 0; i < exts.length; i++) {
  22. if (process.env.DEBUG_MIME && this.types[exts[i]]) {
  23. console.warn((this._loading || "define()").replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
  24. this.types[exts[i]] + ' to ' + type);
  25. }
  26. this.types[exts[i]] = type;
  27. }
  28. // Default extension is the first one we encounter
  29. if (!this.extensions[type]) {
  30. this.extensions[type] = exts[0];
  31. }
  32. }
  33. };
  34. /**
  35. * Load an Apache2-style ".types" file
  36. *
  37. * This may be called multiple times (it's expected). Where files declare
  38. * overlapping types/extensions, the last file wins.
  39. *
  40. * @param file (String) path of file to load.
  41. */
  42. Mime.prototype.load = function(file) {
  43. this._loading = file;
  44. // Read file and split into lines
  45. var map = {},
  46. content = fs.readFileSync(file, 'ascii'),
  47. lines = content.split(/[\r\n]+/);
  48. lines.forEach(function(line) {
  49. // Clean up whitespace/comments, and split into fields
  50. var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
  51. map[fields.shift()] = fields;
  52. });
  53. this.define(map);
  54. this._loading = null;
  55. };
  56. /**
  57. * Lookup a mime type based on extension
  58. */
  59. Mime.prototype.lookup = function(path, fallback) {
  60. var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase();
  61. return this.types[ext] || fallback || this.default_type;
  62. };
  63. /**
  64. * Return file extension associated with a mime type
  65. */
  66. Mime.prototype.extension = function(mimeType) {
  67. var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase();
  68. return this.extensions[type];
  69. };
  70. // Default instance
  71. var mime = new Mime();
  72. // Define built-in types
  73. mime.define(require('./types.json'));
  74. // Default type
  75. mime.default_type = mime.lookup('bin');
  76. //
  77. // Additional API specific to the default instance
  78. //
  79. mime.Mime = Mime;
  80. /**
  81. * Lookup a charset based on mime type.
  82. */
  83. mime.charsets = {
  84. lookup: function(mimeType, fallback) {
  85. // Assume text types are utf8
  86. return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback;
  87. }
  88. };
  89. module.exports = mime;