const mimeDb = require('mime-db');
const defaultType = 'application/octet-stream';
+
+/**
+ * Expects mime-db source data as object:
+ * mime-type:
+ * source: <String>
+ * charset: <String>
+ * compressible: <Boolean>
+ * extensions: <String[]>
+ * Populate `extension` as an index from extensions to mime-types.
+ */
const extension = {};
for (const [mimeType, entry] of Object.entries(mimeDb)) {
- if (entry.extensions) {
- entry.extensions.forEach((ext) => {
- if (!(ext in extension)) {
- extension[ext] = [];
- }
- extension[ext].push(mimeType);
- });
+ for (const ext of entry?.extensions || []) {
+ if (!(ext in extension)) {
+ extension[ext] = [];
+ }
+ extension[ext].push(mimeType);
}
}
* Return a suitable type for a file extension.
* @param {string} ext file extension
* @param {string} def type to return if no match
+ * @returns {string}
*/
-const extensionToMime = (ext, def = defaultType) => extension[ext] && extension[ext][extension[ext].length - 1] || def;
+const extensionToMime = (ext, def = defaultType) => extension?.[ext]?.[0] || def;
module.exports = {
extensionToMime,