@tinyhttp/type-is
Advanced tools
Comparing version 2.2.3 to 2.2.4
@@ -1,54 +0,82 @@ | ||
import * as typer from "@tinyhttp/content-type"; | ||
import mime from "mime"; | ||
import * as typer from '@tinyhttp/content-type'; | ||
import mime from 'mime'; | ||
function normalizeType(value) { | ||
const type = typer.parse(value); | ||
type.parameters = {}; | ||
return typer.format(type); | ||
// parse the type | ||
const type = typer.parse(value); | ||
type.parameters = {}; | ||
// reformat it | ||
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) { | ||
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].slice(0, 2) === "*+") | ||
return expectedParts[1].length <= actualParts[1].length + 1 && expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length); | ||
if (expectedParts[1] !== "*" && expectedParts[1] !== actualParts[1]) return false; | ||
return true; | ||
// 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].slice(0, 2) === '*+') | ||
return (expectedParts[1].length <= actualParts[1].length + 1 && | ||
expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length)); | ||
// validate subtype | ||
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) | ||
return false; | ||
return true; | ||
} | ||
function normalize(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 ? mime.getType(type) : 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 ? mime.getType(type) : type; | ||
} | ||
const typeIs = (value, ...types) => { | ||
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; | ||
/** | ||
* 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. | ||
*/ | ||
export 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; | ||
} | ||
} | ||
} | ||
return false; | ||
// no matches | ||
return false; | ||
}; | ||
export { | ||
typeIs | ||
}; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@tinyhttp/type-is", | ||
"version": "2.2.3", | ||
"version": "2.2.4", | ||
"type": "module", | ||
@@ -32,8 +32,7 @@ "description": "TypeScript rewrite of type-is with CJS and ESM targets", | ||
"@tinyhttp/content-type": "^0.1.4", | ||
"mime": "4.0.1" | ||
"mime": "4.0.4" | ||
}, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build" | ||
"build": "tsc" | ||
} | ||
} |
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
26438
88
+ Addedmime@4.0.4(transitive)
- Removedmime@4.0.1(transitive)
Updatedmime@4.0.4