@tinyhttp/type-is 2.0.5 → 2.0.6
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 +51 -73
- package/package.json +4 -2
- package/vite.config.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -1,84 +1,62 @@
|
|
|
1
|
-
import { lookup } from
|
|
2
|
-
import * as typer from
|
|
3
|
-
|
|
1
|
+
import { lookup } from "es-mime-types";
|
|
2
|
+
import * as typer from "es-content-type";
|
|
4
3
|
function normalizeType(value) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// reformat it
|
|
9
|
-
return typer.format(type);
|
|
4
|
+
const type = typer.parse(value);
|
|
5
|
+
type.parameters = {};
|
|
6
|
+
return typer.format(type);
|
|
10
7
|
}
|
|
11
8
|
function tryNormalizeType(value) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
9
|
+
if (!value)
|
|
10
|
+
return null;
|
|
11
|
+
try {
|
|
12
|
+
return normalizeType(value);
|
|
13
|
+
} catch (err) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
20
16
|
}
|
|
21
17
|
function mimeMatch(expected, actual) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (expectedParts[1].substr(0, 2) === '*+')
|
|
36
|
-
return (expectedParts[1].length <= actualParts[1].length + 1 &&
|
|
37
|
-
expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length));
|
|
38
|
-
// validate subtype
|
|
39
|
-
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1])
|
|
40
|
-
return false;
|
|
41
|
-
return true;
|
|
18
|
+
if (expected === false)
|
|
19
|
+
return false;
|
|
20
|
+
const actualParts = actual.split("/");
|
|
21
|
+
const expectedParts = expected.split("/");
|
|
22
|
+
if (actualParts.length !== 2 || expectedParts.length !== 2)
|
|
23
|
+
return false;
|
|
24
|
+
if (expectedParts[0] !== "*" && expectedParts[0] !== actualParts[0])
|
|
25
|
+
return false;
|
|
26
|
+
if (expectedParts[1].substr(0, 2) === "*+")
|
|
27
|
+
return expectedParts[1].length <= actualParts[1].length + 1 && expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length);
|
|
28
|
+
if (expectedParts[1] !== "*" && expectedParts[1] !== actualParts[1])
|
|
29
|
+
return false;
|
|
30
|
+
return true;
|
|
42
31
|
}
|
|
43
32
|
function normalize(type) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return '*/*' + type;
|
|
56
|
-
return type.indexOf('/') === -1 ? lookup(type) : type;
|
|
33
|
+
if (typeof type !== "string")
|
|
34
|
+
return false;
|
|
35
|
+
switch (type) {
|
|
36
|
+
case "urlencoded":
|
|
37
|
+
return "application/x-www-form-urlencoded";
|
|
38
|
+
case "multipart":
|
|
39
|
+
return "multipart/*";
|
|
40
|
+
}
|
|
41
|
+
if (type[0] === "+")
|
|
42
|
+
return "*/*" + type;
|
|
43
|
+
return type.indexOf("/") === -1 ? lookup(type) : type;
|
|
57
44
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Compare a `value` content-type with `types`.
|
|
60
|
-
* Each `type` can be an extension like `html`,
|
|
61
|
-
* a special shortcut like `multipart` or `urlencoded`,
|
|
62
|
-
* or a mime type.
|
|
63
|
-
*/
|
|
64
45
|
const typeIs = (value, ...types) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// no type or invalid
|
|
69
|
-
if (!val)
|
|
70
|
-
return false;
|
|
71
|
-
// no types, return the content type
|
|
72
|
-
if (!types || !types.length)
|
|
73
|
-
return val;
|
|
74
|
-
let type;
|
|
75
|
-
for (i = 0; i < types.length; i++) {
|
|
76
|
-
if (mimeMatch(normalize((type = types[i])), val)) {
|
|
77
|
-
return type[0] === '+' || type.indexOf('*') !== -1 ? val : type;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// no matches
|
|
46
|
+
let i;
|
|
47
|
+
const val = tryNormalizeType(value);
|
|
48
|
+
if (!val)
|
|
81
49
|
return false;
|
|
50
|
+
if (!types || !types.length)
|
|
51
|
+
return val;
|
|
52
|
+
let type;
|
|
53
|
+
for (i = 0; i < types.length; i++) {
|
|
54
|
+
if (mimeMatch(normalize(type = types[i]), val)) {
|
|
55
|
+
return type[0] === "+" || type.indexOf("*") !== -1 ? val : type;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
};
|
|
60
|
+
export {
|
|
61
|
+
typeIs
|
|
82
62
|
};
|
|
83
|
-
|
|
84
|
-
export { typeIs };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyhttp/type-is",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TypeScript rewrite of type-is with CJS and ESM targets",
|
|
6
6
|
"homepage": "https://tinyhttp.v1rtl.site",
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"es-mime-types": "^0.1.4"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"
|
|
35
|
+
"dev": "vite",
|
|
36
|
+
"build": "vite build",
|
|
37
|
+
"postbuild": "tsc --emitDeclarationOnly"
|
|
36
38
|
}
|
|
37
39
|
}
|
package/vite.config.ts
ADDED