@tinyhttp/type-is 2.2.2 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +9 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -4
- package/vite.config.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -6,8 +6,7 @@ function normalizeType(value) {
|
|
|
6
6
|
return typer.format(type);
|
|
7
7
|
}
|
|
8
8
|
function tryNormalizeType(value) {
|
|
9
|
-
if (!value)
|
|
10
|
-
return null;
|
|
9
|
+
if (!value) return null;
|
|
11
10
|
try {
|
|
12
11
|
return normalizeType(value);
|
|
13
12
|
} catch (err) {
|
|
@@ -15,40 +14,32 @@ function tryNormalizeType(value) {
|
|
|
15
14
|
}
|
|
16
15
|
}
|
|
17
16
|
function mimeMatch(expected, actual) {
|
|
18
|
-
if (expected === false)
|
|
19
|
-
return false;
|
|
17
|
+
if (expected === false) return false;
|
|
20
18
|
const actualParts = actual.split("/");
|
|
21
19
|
const expectedParts = expected.split("/");
|
|
22
|
-
if (actualParts.length !== 2 || expectedParts.length !== 2)
|
|
23
|
-
|
|
24
|
-
if (expectedParts[0] !== "*" && expectedParts[0] !== actualParts[0])
|
|
25
|
-
return false;
|
|
20
|
+
if (actualParts.length !== 2 || expectedParts.length !== 2) return false;
|
|
21
|
+
if (expectedParts[0] !== "*" && expectedParts[0] !== actualParts[0]) return false;
|
|
26
22
|
if (expectedParts[1].slice(0, 2) === "*+")
|
|
27
23
|
return expectedParts[1].length <= actualParts[1].length + 1 && expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length);
|
|
28
|
-
if (expectedParts[1] !== "*" && expectedParts[1] !== actualParts[1])
|
|
29
|
-
return false;
|
|
24
|
+
if (expectedParts[1] !== "*" && expectedParts[1] !== actualParts[1]) return false;
|
|
30
25
|
return true;
|
|
31
26
|
}
|
|
32
27
|
function normalize(type) {
|
|
33
|
-
if (typeof type !== "string")
|
|
34
|
-
return false;
|
|
28
|
+
if (typeof type !== "string") return false;
|
|
35
29
|
switch (type) {
|
|
36
30
|
case "urlencoded":
|
|
37
31
|
return "application/x-www-form-urlencoded";
|
|
38
32
|
case "multipart":
|
|
39
33
|
return "multipart/*";
|
|
40
34
|
}
|
|
41
|
-
if (type[0] === "+")
|
|
42
|
-
return "*/*" + type;
|
|
35
|
+
if (type[0] === "+") return `*/*${type}`;
|
|
43
36
|
return type.indexOf("/") === -1 ? mime.getType(type) : type;
|
|
44
37
|
}
|
|
45
38
|
const typeIs = (value, ...types) => {
|
|
46
39
|
let i;
|
|
47
40
|
const val = tryNormalizeType(value);
|
|
48
|
-
if (!val)
|
|
49
|
-
|
|
50
|
-
if (!types || !types.length)
|
|
51
|
-
return val;
|
|
41
|
+
if (!val) return false;
|
|
42
|
+
if (!types || !types.length) return val;
|
|
52
43
|
let type;
|
|
53
44
|
for (i = 0; i < types.length; i++) {
|
|
54
45
|
if (mimeMatch(normalize(type = types[i]), val)) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import * as typer from '@tinyhttp/content-type'\nimport mime from 'mime'\n\nfunction normalizeType(value: string) {\n // parse the type\n const type = typer.parse(value)\n type.parameters = {}\n // reformat it\n return typer.format(type)\n}\n\nfunction tryNormalizeType(value: string) {\n if (!value) return null\n\n try {\n return normalizeType(value)\n } catch (err) {\n return null\n }\n}\n\nfunction mimeMatch(expected: string | boolean, actual: string | boolean): boolean {\n // invalid type\n if (expected === false) return false\n\n // split types\n const actualParts = (actual as string).split('/')\n const expectedParts = (expected as string).split('/')\n\n // invalid format\n if (actualParts.length !== 2 || expectedParts.length !== 2) return false\n\n // validate type\n if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) return false\n\n // validate suffix wildcard\n if (expectedParts[1].slice(0, 2) === '*+')\n return (\n expectedParts[1].length <= actualParts[1].length + 1 &&\n expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length)\n )\n\n // validate subtype\n if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) return false\n\n return true\n}\n\nfunction normalize(type: string | unknown) {\n // invalid type\n if (typeof type !== 'string') return false\n\n switch (type) {\n case 'urlencoded':\n return 'application/x-www-form-urlencoded'\n case 'multipart':\n return 'multipart/*'\n }\n // \"+json\" -> \"*/*+json\" expando\n if (type[0] === '+') return
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import * as typer from '@tinyhttp/content-type'\nimport mime from 'mime'\n\nfunction normalizeType(value: string) {\n // parse the type\n const type = typer.parse(value)\n type.parameters = {}\n // reformat it\n return typer.format(type)\n}\n\nfunction tryNormalizeType(value: string) {\n if (!value) return null\n\n try {\n return normalizeType(value)\n } catch (err) {\n return null\n }\n}\n\nfunction mimeMatch(expected: string | boolean, actual: string | boolean): boolean {\n // invalid type\n if (expected === false) return false\n\n // split types\n const actualParts = (actual as string).split('/')\n const expectedParts = (expected as string).split('/')\n\n // invalid format\n if (actualParts.length !== 2 || expectedParts.length !== 2) return false\n\n // validate type\n if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) return false\n\n // validate suffix wildcard\n if (expectedParts[1].slice(0, 2) === '*+')\n return (\n expectedParts[1].length <= actualParts[1].length + 1 &&\n expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length)\n )\n\n // validate subtype\n if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) return false\n\n return true\n}\n\nfunction normalize(type: string | unknown) {\n // invalid type\n if (typeof type !== 'string') return false\n\n switch (type) {\n case 'urlencoded':\n return 'application/x-www-form-urlencoded'\n case 'multipart':\n return 'multipart/*'\n }\n // \"+json\" -> \"*/*+json\" expando\n if (type[0] === '+') return `*/*${type}`\n\n return type.indexOf('/') === -1 ? mime.getType(type) : type\n}\n\n/**\n * Compare a `value` content-type with `types`.\n * Each `type` can be an extension like `html`,\n * a special shortcut like `multipart` or `urlencoded`,\n * or a mime type.\n */\nexport const typeIs = (value: string, ...types: string[]) => {\n let i: number\n // remove parameters and normalize\n const val = tryNormalizeType(value)\n\n // no type or invalid\n if (!val) return false\n\n // no types, return the content type\n if (!types || !types.length) return val\n\n let type: string\n for (i = 0; i < types.length; i++) {\n if (mimeMatch(normalize((type = types[i])), val)) {\n return type[0] === '+' || type.indexOf('*') !== -1 ? val : type\n }\n }\n\n // no matches\n return false\n}\n"],"names":[],"mappings":";;AAGA,SAAS,cAAc,OAAe;AAE9B,QAAA,OAAO,MAAM,MAAM,KAAK;AAC9B,OAAK,aAAa;AAEX,SAAA,MAAM,OAAO,IAAI;AAC1B;AAEA,SAAS,iBAAiB,OAAe;AACnC,MAAA,CAAC,MAAc,QAAA;AAEf,MAAA;AACF,WAAO,cAAc,KAAK;AAAA,WACnB,KAAK;AACL,WAAA;AAAA,EACT;AACF;AAEA,SAAS,UAAU,UAA4B,QAAmC;AAE5E,MAAA,aAAa,MAAc,QAAA;AAGzB,QAAA,cAAe,OAAkB,MAAM,GAAG;AAC1C,QAAA,gBAAiB,SAAoB,MAAM,GAAG;AAGpD,MAAI,YAAY,WAAW,KAAK,cAAc,WAAW,EAAU,QAAA;AAG/D,MAAA,cAAc,CAAC,MAAM,OAAO,cAAc,CAAC,MAAM,YAAY,CAAC,EAAU,QAAA;AAG5E,MAAI,cAAc,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM;AAEjC,WAAA,cAAc,CAAC,EAAE,UAAU,YAAY,CAAC,EAAE,SAAS,KACnD,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,YAAY,CAAC,EAAE,MAAM,IAAI,cAAc,CAAC,EAAE,MAAM;AAI9E,MAAA,cAAc,CAAC,MAAM,OAAO,cAAc,CAAC,MAAM,YAAY,CAAC,EAAU,QAAA;AAErE,SAAA;AACT;AAEA,SAAS,UAAU,MAAwB;AAErC,MAAA,OAAO,SAAS,SAAiB,QAAA;AAErC,UAAQ,MAAM;AAAA,IACZ,KAAK;AACI,aAAA;AAAA,IACT,KAAK;AACI,aAAA;AAAA,EACX;AAEA,MAAI,KAAK,CAAC,MAAM,IAAK,QAAO,MAAM,IAAI;AAE/B,SAAA,KAAK,QAAQ,GAAG,MAAM,KAAK,KAAK,QAAQ,IAAI,IAAI;AACzD;AAQa,MAAA,SAAS,CAAC,UAAkB,UAAoB;AACvD,MAAA;AAEE,QAAA,MAAM,iBAAiB,KAAK;AAG9B,MAAA,CAAC,IAAY,QAAA;AAGjB,MAAI,CAAC,SAAS,CAAC,MAAM,OAAe,QAAA;AAEhC,MAAA;AACJ,OAAK,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AAC7B,QAAA,UAAU,UAAW,OAAO,MAAM,CAAC,CAAE,GAAG,GAAG,GAAG;AACzC,aAAA,KAAK,CAAC,MAAM,OAAO,KAAK,QAAQ,GAAG,MAAM,KAAK,MAAM;AAAA,IAC7D;AAAA,EACF;AAGO,SAAA;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyhttp/type-is",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TypeScript rewrite of type-is with CJS and ESM targets",
|
|
6
6
|
"homepage": "https://tinyhttp.v1rtl.site",
|
|
@@ -31,9 +31,6 @@
|
|
|
31
31
|
"@tinyhttp/content-type": "^0.1.4",
|
|
32
32
|
"mime": "4.0.1"
|
|
33
33
|
},
|
|
34
|
-
"files": [
|
|
35
|
-
"dist"
|
|
36
|
-
],
|
|
37
34
|
"scripts": {
|
|
38
35
|
"dev": "vite",
|
|
39
36
|
"build": "vite build"
|
package/vite.config.ts
ADDED