@tinyhttp/type-is
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -1,84 +0,62 @@ | ||
import { lookup } from 'es-mime-types'; | ||
import * as typer from 'es-content-type'; | ||
import { lookup } from "es-mime-types"; | ||
import * as typer from "es-content-type"; | ||
function normalizeType(value) { | ||
// parse the type | ||
const type = typer.parse(value); | ||
type.parameters = {}; | ||
// reformat it | ||
return typer.format(type); | ||
const type = typer.parse(value); | ||
type.parameters = {}; | ||
return typer.format(type); | ||
} | ||
function tryNormalizeType(value) { | ||
if (!value) | ||
return null; | ||
try { | ||
return normalizeType(value); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
if (!value) | ||
return null; | ||
try { | ||
return normalizeType(value); | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function mimeMatch(expected, actual) { | ||
// invalid type | ||
if (expected === false) | ||
return false; | ||
// split types | ||
const actualParts = actual.split('/'); | ||
const expectedParts = expected.split('/'); | ||
// invalid format | ||
if (actualParts.length !== 2 || expectedParts.length !== 2) | ||
return false; | ||
// validate type | ||
if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) | ||
return false; | ||
// validate suffix wildcard | ||
if (expectedParts[1].substr(0, 2) === '*+') | ||
return (expectedParts[1].length <= actualParts[1].length + 1 && | ||
expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)); | ||
// validate subtype | ||
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) | ||
return false; | ||
return true; | ||
if (expected === false) | ||
return false; | ||
const actualParts = actual.split("/"); | ||
const expectedParts = expected.split("/"); | ||
if (actualParts.length !== 2 || expectedParts.length !== 2) | ||
return false; | ||
if (expectedParts[0] !== "*" && expectedParts[0] !== actualParts[0]) | ||
return false; | ||
if (expectedParts[1].substr(0, 2) === "*+") | ||
return expectedParts[1].length <= actualParts[1].length + 1 && expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length); | ||
if (expectedParts[1] !== "*" && expectedParts[1] !== actualParts[1]) | ||
return false; | ||
return true; | ||
} | ||
function normalize(type) { | ||
// invalid type | ||
if (typeof type !== 'string') | ||
return false; | ||
switch (type) { | ||
case 'urlencoded': | ||
return 'application/x-www-form-urlencoded'; | ||
case 'multipart': | ||
return 'multipart/*'; | ||
} | ||
// "+json" -> "*/*+json" expando | ||
if (type[0] === '+') | ||
return '*/*' + type; | ||
return type.indexOf('/') === -1 ? lookup(type) : type; | ||
if (typeof type !== "string") | ||
return false; | ||
switch (type) { | ||
case "urlencoded": | ||
return "application/x-www-form-urlencoded"; | ||
case "multipart": | ||
return "multipart/*"; | ||
} | ||
if (type[0] === "+") | ||
return "*/*" + type; | ||
return type.indexOf("/") === -1 ? lookup(type) : type; | ||
} | ||
/** | ||
* Compare a `value` content-type with `types`. | ||
* Each `type` can be an extension like `html`, | ||
* a special shortcut like `multipart` or `urlencoded`, | ||
* or a mime type. | ||
*/ | ||
const typeIs = (value, ...types) => { | ||
let i; | ||
// remove parameters and normalize | ||
const val = tryNormalizeType(value); | ||
// no type or invalid | ||
if (!val) | ||
return false; | ||
// no types, return the content type | ||
if (!types || !types.length) | ||
return val; | ||
let type; | ||
for (i = 0; i < types.length; i++) { | ||
if (mimeMatch(normalize((type = types[i])), val)) { | ||
return type[0] === '+' || type.indexOf('*') !== -1 ? val : type; | ||
} | ||
let i; | ||
const val = tryNormalizeType(value); | ||
if (!val) | ||
return false; | ||
if (!types || !types.length) | ||
return val; | ||
let type; | ||
for (i = 0; i < types.length; i++) { | ||
if (mimeMatch(normalize(type = types[i]), val)) { | ||
return type[0] === "+" || type.indexOf("*") !== -1 ? val : type; | ||
} | ||
// no matches | ||
return false; | ||
} | ||
return false; | ||
}; | ||
export { typeIs }; | ||
export { | ||
typeIs | ||
}; |
{ | ||
"name": "@tinyhttp/type-is", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"type": "module", | ||
@@ -35,4 +35,6 @@ "description": "TypeScript rewrite of type-is with CJS and ESM targets", | ||
"scripts": { | ||
"build": "rollup -c" | ||
"dev": "vite", | ||
"build": "vite build", | ||
"postbuild": "tsc --emitDeclarationOnly" | ||
} | ||
} |
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
6
4164
72