es-mime-types
Advanced tools
Comparing version 0.0.14 to 0.0.15
declare const extensions: any; | ||
declare const types: any; | ||
export { extensions, types }; | ||
/** | ||
* Get the default charset for a MIME type. | ||
*/ | ||
declare function charset(type: string): boolean | string; | ||
export declare function charset(type: string): boolean | string; | ||
/** | ||
* Create a full Content-Type header given a MIME type or extension. | ||
*/ | ||
declare function contentType(str: string): any; | ||
export declare function contentType(str: string): any; | ||
/** | ||
* Get the default extension for a MIME type. | ||
*/ | ||
declare function extension(type: string | unknown): any; | ||
export declare function extension(type: string | unknown): any; | ||
/** | ||
* Lookup the MIME type for a file path/extension. | ||
*/ | ||
declare function lookup(path: string | unknown): any; | ||
export { charset, contentType, extension, extensions, lookup, types }; | ||
export declare function lookup(path: string | unknown): any; |
@@ -1,1 +0,109 @@ | ||
var __defineProperty=Object.defineProperty;var __commonJS=(callback,module)=>()=>{if(!module){module={exports:{}};callback(module.exports,module)}return module.exports};var __markAsModule=target=>{return __defineProperty(target,"__esModule",{value:!0})};var __export=(target,all)=>{__markAsModule(target);for(var name in all)__defineProperty(target,name,{get:all[name],enumerable:!0})};import db from"mime-db";import{extname}from"path";var require_src=__commonJS(exports=>{__export(exports,{charset:()=>charset,contentType:()=>contentType,extension:()=>extension,extensions:()=>extensions,lookup:()=>lookup,types:()=>types});const EXTRACT_TYPE_REGEXP=/^\s*([^;\s]*)(?:;|\s|$)/;const TEXT_TYPE_REGEXP=/^text\//i;function populateMaps(extensions2,types2){const preference=["nginx","apache",void 0,"iana"];Object.keys(db).forEach(type=>{const mime=db[type];const exts=mime.extensions;if(!exts||!exts.length){return}extensions2[type]=exts;for(const extension2 of exts){if(types2[extension2]){const from=preference.indexOf(db[types2[extension2]].source);const to=preference.indexOf(mime.source);if(types2[extension2]!=="application/octet-stream"&&(from>to||from===to&&types2[extension2].substr(0,12)==="application/")){continue}}types2[extension2]=type}})}const extensions=Object.create(null);const types=Object.create(null);populateMaps(extensions,types);function charset(type){if(!type||typeof type!=="string"){return!1}const match=EXTRACT_TYPE_REGEXP.exec(type);const mime=match&&db[match[1].toLowerCase()];if(mime&&mime.charset){return mime.charset}if(match&&TEXT_TYPE_REGEXP.test(match[1])){return"UTF-8"}return!1}function contentType(str){if(!str||typeof str!=="string"){return!1}let mime=str.indexOf("/")===-1?exports.lookup(str):str;if(!mime){return!1}if(mime.indexOf("charset")===-1){const charset2=exports.charset(mime);if(charset2)mime+="; charset="+charset2.toLowerCase()}return mime}function extension(type){if(!type||typeof type!=="string"){return!1}const match=EXTRACT_TYPE_REGEXP.exec(type);const exts=match&&exports.extensions[match[1].toLowerCase()];if(!exts||!exts.length){return!1}return exts[0]}function lookup(path2){if(!path2||typeof path2!=="string"){return!1}const extension2=extname("x."+path2).toLowerCase().substr(1);if(!extension2){return!1}return exports.types[extension2]||!1}});export default require_src(); | ||
import db from 'mime-db'; | ||
import { extname } from 'path'; | ||
const EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/; | ||
const TEXT_TYPE_REGEXP = /^text\//i; | ||
function populateMaps(extensions, types) { | ||
// source preference (least -> most) | ||
const preference = ['nginx', 'apache', undefined, 'iana']; | ||
Object.keys(db).forEach((type) => { | ||
const mime = db[type]; | ||
const exts = mime.extensions; | ||
if (!exts || !exts.length) { | ||
return; | ||
} | ||
// mime -> extensions | ||
extensions[type] = exts; | ||
// extension -> mime | ||
for (const extension of exts) { | ||
if (types[extension]) { | ||
const from = preference.indexOf(db[types[extension]].source); | ||
const to = preference.indexOf(mime.source); | ||
if (types[extension] !== 'application/octet-stream' && | ||
(from > to || | ||
(from === to && types[extension].substr(0, 12) === 'application/'))) { | ||
// skip the remapping | ||
continue; | ||
} | ||
} | ||
// set the extension -> mime | ||
types[extension] = type; | ||
} | ||
}); | ||
} | ||
const extensions = Object.create(null); | ||
const types = Object.create(null); | ||
populateMaps(extensions, types); | ||
/** | ||
* Get the default charset for a MIME type. | ||
*/ | ||
function charset(type) { | ||
if (!type || typeof type !== 'string') { | ||
return false; | ||
} | ||
// TODO: use media-typer | ||
const match = EXTRACT_TYPE_REGEXP.exec(type); | ||
const mime = match && db[match[1].toLowerCase()]; | ||
if (mime && mime.charset) { | ||
return mime.charset; | ||
} | ||
// default text/* to utf-8 | ||
if (match && TEXT_TYPE_REGEXP.test(match[1])) { | ||
return 'UTF-8'; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Create a full Content-Type header given a MIME type or extension. | ||
*/ | ||
function contentType(str) { | ||
// TODO: should this even be in this module? | ||
if (!str || typeof str !== 'string') { | ||
return false; | ||
} | ||
let mime = str.indexOf('/') === -1 ? exports.lookup(str) : str; | ||
if (!mime) { | ||
return false; | ||
} | ||
// TODO: use content-type or other module | ||
if (mime.indexOf('charset') === -1) { | ||
const charset = exports.charset(mime); | ||
if (charset) | ||
mime += '; charset=' + charset.toLowerCase(); | ||
} | ||
return mime; | ||
} | ||
/** | ||
* Get the default extension for a MIME type. | ||
*/ | ||
function extension(type) { | ||
if (!type || typeof type !== 'string') { | ||
return false; | ||
} | ||
// TODO: use media-typer | ||
const match = EXTRACT_TYPE_REGEXP.exec(type); | ||
// get extensions | ||
const exts = match && exports.extensions[match[1].toLowerCase()]; | ||
if (!exts || !exts.length) { | ||
return false; | ||
} | ||
return exts[0]; | ||
} | ||
/** | ||
* Lookup the MIME type for a file path/extension. | ||
*/ | ||
function lookup(path) { | ||
if (!path || typeof path !== 'string') { | ||
return false; | ||
} | ||
// get the extension ("ext" or ".ext" or full path) | ||
const extension = extname('x.' + path) | ||
.toLowerCase() | ||
.substr(1); | ||
if (!extension) { | ||
return false; | ||
} | ||
return exports.types[extension] || false; | ||
} | ||
export { charset, contentType, extension, extensions, lookup, types }; |
{ | ||
"name": "es-mime-types", | ||
"description": "mime-types rewrite in TypeScript with ESM and CommonJS targets", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"repository": "https://github.com/talentlessguy/es-mime-types.git", | ||
@@ -39,3 +39,4 @@ "keywords": [ | ||
"@types/node": "^14.6.0", | ||
"tsup": "^3.6.1", | ||
"rollup": "^2.26.5", | ||
"rollup-plugin-typescript2": "^0.27.2", | ||
"typescript": "^4.0.2" | ||
@@ -48,4 +49,4 @@ }, | ||
"prepare": "pnpm build", | ||
"build": "tsup src/index.ts --minify-whitespace --format cjs,esm --dts" | ||
"build": "rollup -c" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
16229
350
2
5