@alshdavid/mime-types
Version:
The ultimate javascript content-type utility.
117 lines (116 loc) • 4.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MimeTypesParser = void 0;
const index_js_1 = require("./path/index.js");
const preference_js_1 = require("./preference.js");
const mime_databse_js_1 = require("./mime-databse.js");
const EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/;
const TEXT_TYPE_REGEXP = /^text\//i;
class MimeTypesParser {
#mimeTypes;
#mimeDatabase;
#extensions;
constructor({ mimeDatabase = mime_databse_js_1.mimeDatabase, preference = preference_js_1.preference, } = {}) {
this.#mimeDatabase = mimeDatabase;
this.#mimeTypes = {};
this.#extensions = {};
for (const [mimeKey, mime] of Object.entries(this.#mimeDatabase)) {
const exts = mime.extensions;
if (!exts || !exts.length) {
continue;
}
this.#extensions[mimeKey] = exts;
for (const extension of exts) {
if (this.#mimeTypes[extension]) {
const from = preference.indexOf(this.#mimeDatabase[this.#mimeTypes[extension]].source);
const to = preference.indexOf(mime.source);
if (this.#mimeTypes[extension] !== 'application/octet-stream' &&
(from > to || (from === to && this.#mimeTypes[extension].substring(0, 12) === 'application/'))) {
continue;
}
}
this.#mimeTypes[extension] = mimeKey;
}
}
}
lookup(path) {
if (!path || typeof path !== 'string') {
return ['', false];
}
// get the extension ("ext" or ".ext" or full path)
const extension = (0, index_js_1.extname)('x.' + path)
.toLowerCase()
.substring(1);
if (!extension) {
return ['', false];
}
const found = this.#mimeTypes[extension];
if (!found) {
return ['', false];
}
return [found, true];
}
contentType(query) {
if (!query || typeof query !== 'string') {
return ['', false];
}
let mime = query;
if (!query.includes('/')) {
const extension = (0, index_js_1.extname)('x.' + query)
.toLowerCase()
.substring(1);
if (!extension || !this.#mimeTypes[extension]) {
return ['', false];
}
mime = this.#mimeTypes[extension];
}
if (!mime.includes('charset')) {
let cs;
const textMatch = EXTRACT_TYPE_REGEXP.exec(mime);
const dbMime = textMatch && this.#mimeDatabase[textMatch[1].toLowerCase()];
if (dbMime && dbMime.charset) {
cs = dbMime.charset;
}
if (textMatch && TEXT_TYPE_REGEXP.test(textMatch[1])) {
cs = 'UTF-8';
}
if (!cs) {
return [query, false];
}
if (cs) {
mime += '; charset=' + cs.toLowerCase();
}
}
return [mime, true];
}
extension(mimeType) {
if (!mimeType || typeof mimeType !== 'string') {
return ['', false];
}
// TODO: use media-typer
const match = EXTRACT_TYPE_REGEXP.exec(mimeType);
// get extensions
const exts = match && this.#extensions[match[1].toLowerCase()];
if (!exts || !exts.length) {
return ['', false];
}
return [exts[0], true];
}
charset(mimeType) {
if (!mimeType || typeof mimeType !== 'string') {
return ['', false];
}
// TODO: use media-typer
const match = EXTRACT_TYPE_REGEXP.exec(mimeType);
const mime = match && this.#mimeDatabase[match[1].toLowerCase()];
if (mime && mime.charset) {
return [mime.charset, true];
}
// default text/* to utf-8
if (match && TEXT_TYPE_REGEXP.test(match[1])) {
return ['UTF-8', true];
}
return ['', false];
}
}
exports.MimeTypesParser = MimeTypesParser;