hono 2.3.1 → 2.4.0
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/cjs/compose.js +82 -66
- package/dist/cjs/context.js +159 -144
- package/dist/cjs/hono.js +173 -150
- package/dist/cjs/index.js +31 -11
- package/dist/cjs/middleware/basic-auth/index.js +70 -39
- package/dist/cjs/middleware/bearer-auth/index.js +69 -51
- package/dist/cjs/middleware/cache/index.js +49 -28
- package/dist/cjs/middleware/compress/index.js +39 -16
- package/dist/cjs/middleware/cors/index.js +89 -71
- package/dist/cjs/middleware/etag/index.js +46 -24
- package/dist/cjs/middleware/html/index.js +50 -30
- package/dist/cjs/middleware/jsx/index.js +178 -166
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
- package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
- package/dist/cjs/middleware/jwt/index.js +74 -54
- package/dist/cjs/middleware/logger/index.js +59 -38
- package/dist/cjs/middleware/powered-by/index.js +30 -7
- package/dist/cjs/middleware/pretty-json/index.js +31 -8
- package/dist/cjs/middleware/serve-static/bun.js +56 -38
- package/dist/cjs/middleware/serve-static/index.js +27 -4
- package/dist/cjs/middleware/serve-static/module.js +42 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
- package/dist/cjs/middleware/validator/index.js +27 -4
- package/dist/cjs/middleware/validator/middleware.js +125 -91
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/request.js +129 -116
- package/dist/cjs/router/reg-exp-router/index.js +27 -4
- package/dist/cjs/router/reg-exp-router/node.js +108 -97
- package/dist/cjs/router/reg-exp-router/router.js +158 -141
- package/dist/cjs/router/reg-exp-router/trie.js +55 -39
- package/dist/cjs/router/smart-router/index.js +27 -4
- package/dist/cjs/router/smart-router/router.js +70 -49
- package/dist/cjs/router/static-router/index.js +27 -4
- package/dist/cjs/router/static-router/router.js +78 -64
- package/dist/cjs/router/trie-router/index.js +27 -4
- package/dist/cjs/router/trie-router/node.js +167 -159
- package/dist/cjs/router/trie-router/router.js +43 -20
- package/dist/cjs/router.js +35 -6
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/utils/body.js +36 -15
- package/dist/cjs/utils/buffer.js +56 -31
- package/dist/cjs/utils/cloudflare.js +53 -34
- package/dist/cjs/utils/cookie.js +59 -35
- package/dist/cjs/utils/crypto.js +69 -43
- package/dist/cjs/utils/encode.js +92 -65
- package/dist/cjs/utils/filepath.js +39 -22
- package/dist/cjs/utils/html.js +54 -33
- package/dist/cjs/utils/http-status.js +68 -45
- package/dist/cjs/utils/json.js +91 -76
- package/dist/cjs/utils/jwt/index.js +32 -25
- package/dist/cjs/utils/jwt/jwt.js +124 -93
- package/dist/cjs/utils/jwt/types.js +65 -38
- package/dist/cjs/utils/mime.js +110 -87
- package/dist/cjs/utils/object.js +53 -35
- package/dist/cjs/utils/types.js +16 -0
- package/dist/cjs/utils/url.js +89 -78
- package/dist/cjs/validator/rule.js +80 -0
- package/dist/cjs/validator/sanitizer.js +30 -0
- package/dist/cjs/validator/schema.js +16 -0
- package/dist/cjs/validator/validator.js +439 -0
- package/dist/compose.d.ts +4 -3
- package/dist/compose.js +61 -64
- package/dist/context.d.ts +10 -9
- package/dist/context.js +139 -143
- package/dist/hono.d.ts +27 -42
- package/dist/hono.js +153 -149
- package/dist/index.d.ts +4 -1
- package/dist/index.js +9 -9
- package/dist/middleware/basic-auth/index.d.ts +1 -1
- package/dist/middleware/basic-auth/index.js +52 -40
- package/dist/middleware/bearer-auth/index.d.ts +1 -1
- package/dist/middleware/bearer-auth/index.js +48 -49
- package/dist/middleware/cache/index.d.ts +1 -1
- package/dist/middleware/cache/index.js +28 -26
- package/dist/middleware/compress/index.d.ts +1 -1
- package/dist/middleware/compress/index.js +18 -14
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/cors/index.js +68 -69
- package/dist/middleware/etag/index.d.ts +1 -1
- package/dist/middleware/etag/index.js +25 -22
- package/dist/middleware/html/index.js +29 -28
- package/dist/middleware/jsx/index.js +164 -172
- package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
- package/dist/middleware/jsx/jsx-runtime.js +7 -2
- package/dist/middleware/jwt/index.d.ts +1 -1
- package/dist/middleware/jwt/index.js +53 -52
- package/dist/middleware/logger/index.d.ts +1 -1
- package/dist/middleware/logger/index.js +35 -39
- package/dist/middleware/powered-by/index.d.ts +1 -1
- package/dist/middleware/powered-by/index.js +9 -5
- package/dist/middleware/pretty-json/index.d.ts +1 -1
- package/dist/middleware/pretty-json/index.js +10 -6
- package/dist/middleware/serve-static/bun.d.ts +1 -1
- package/dist/middleware/serve-static/bun.js +35 -36
- package/dist/middleware/serve-static/index.js +5 -1
- package/dist/middleware/serve-static/module.d.ts +3 -0
- package/dist/middleware/serve-static/module.js +13 -0
- package/dist/middleware/serve-static/serve-static.d.ts +2 -3
- package/dist/middleware/serve-static/serve-static.js +36 -36
- package/dist/middleware/validator/index.js +5 -2
- package/dist/middleware/validator/middleware.d.ts +9 -15
- package/dist/middleware/validator/middleware.js +104 -89
- package/dist/request.d.ts +2 -2
- package/dist/request.js +108 -114
- package/dist/router/reg-exp-router/index.js +5 -1
- package/dist/router/reg-exp-router/node.js +87 -96
- package/dist/router/reg-exp-router/router.js +140 -142
- package/dist/router/reg-exp-router/trie.js +35 -38
- package/dist/router/smart-router/index.js +5 -1
- package/dist/router/smart-router/router.js +50 -48
- package/dist/router/static-router/index.js +5 -1
- package/dist/router/static-router/router.js +58 -63
- package/dist/router/trie-router/index.js +5 -1
- package/dist/router/trie-router/node.js +147 -158
- package/dist/router/trie-router/router.js +23 -19
- package/dist/router.js +12 -5
- package/dist/types.d.ts +22 -0
- package/dist/types.js +0 -0
- package/dist/utils/body.js +15 -13
- package/dist/utils/buffer.js +35 -29
- package/dist/utils/cloudflare.js +32 -32
- package/dist/utils/cookie.js +38 -33
- package/dist/utils/crypto.js +48 -41
- package/dist/utils/encode.js +70 -62
- package/dist/utils/filepath.js +18 -20
- package/dist/utils/html.js +34 -32
- package/dist/utils/http-status.js +48 -44
- package/dist/utils/json.js +71 -75
- package/dist/utils/jwt/index.js +5 -1
- package/dist/utils/jwt/jwt.js +95 -90
- package/dist/utils/jwt/types.js +47 -41
- package/dist/utils/mime.js +90 -86
- package/dist/utils/object.js +31 -32
- package/dist/utils/types.d.ts +3 -0
- package/dist/utils/types.js +0 -0
- package/dist/utils/url.js +69 -77
- package/dist/{middleware/validator → validator}/rule.d.ts +0 -0
- package/dist/validator/rule.js +57 -0
- package/dist/{middleware/validator → validator}/sanitizer.d.ts +0 -0
- package/dist/validator/sanitizer.js +7 -0
- package/dist/validator/schema.d.ts +7 -0
- package/dist/validator/schema.js +0 -0
- package/dist/{middleware/validator → validator}/validator.d.ts +10 -7
- package/dist/validator/validator.js +406 -0
- package/package.json +14 -8
- package/dist/cjs/middleware/validator/rule.js +0 -66
- package/dist/cjs/middleware/validator/sanitizer.js +0 -6
- package/dist/cjs/middleware/validator/validator.js +0 -417
- package/dist/middleware/serve-static/module.d.mts +0 -3
- package/dist/middleware/serve-static/module.mjs +0 -13
- package/dist/middleware/validator/rule.js +0 -63
- package/dist/middleware/validator/sanitizer.js +0 -3
- package/dist/middleware/validator/validator.js +0 -403
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
// src/validator/validator.ts
|
|
2
|
+
import { JSONPathCopy } from "./../utils/json.js";
|
|
3
|
+
import { rule } from "./rule.js";
|
|
4
|
+
import { sanitizer } from "./sanitizer.js";
|
|
5
|
+
var VObjectBase = class {
|
|
6
|
+
constructor(container, key) {
|
|
7
|
+
this.keys = [];
|
|
8
|
+
this._isOptional = false;
|
|
9
|
+
this.getValidators = () => {
|
|
10
|
+
const validators = [];
|
|
11
|
+
const thisKeys = [];
|
|
12
|
+
Object.assign(thisKeys, this.keys);
|
|
13
|
+
const walk = (container, keys, isOptional) => {
|
|
14
|
+
for (const v of Object.values(container)) {
|
|
15
|
+
if (v instanceof VArray || v instanceof VObject) {
|
|
16
|
+
isOptional || (isOptional = v._isOptional);
|
|
17
|
+
keys.push(...v.keys);
|
|
18
|
+
walk(v.container, keys, isOptional);
|
|
19
|
+
const tmp = [];
|
|
20
|
+
Object.assign(tmp, thisKeys);
|
|
21
|
+
keys = tmp;
|
|
22
|
+
} else if (v instanceof VBase) {
|
|
23
|
+
if (isOptional)
|
|
24
|
+
v.isOptional();
|
|
25
|
+
v.baseKeys.push(...keys);
|
|
26
|
+
validators.push(v);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
walk(this.container, this.keys, this._isOptional);
|
|
31
|
+
return validators;
|
|
32
|
+
};
|
|
33
|
+
this.container = container;
|
|
34
|
+
if (this instanceof VArray) {
|
|
35
|
+
this.keys.push(key, "[*]");
|
|
36
|
+
} else if (this instanceof VObject) {
|
|
37
|
+
this.keys.push(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
isOptional() {
|
|
41
|
+
this._isOptional = true;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var VObject = class extends VObjectBase {
|
|
46
|
+
constructor(container, key) {
|
|
47
|
+
super(container, key);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var VArray = class extends VObjectBase {
|
|
51
|
+
constructor(container, key) {
|
|
52
|
+
super(container, key);
|
|
53
|
+
this.type = "array";
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var Validator = class {
|
|
57
|
+
constructor() {
|
|
58
|
+
this.isArray = false;
|
|
59
|
+
this.query = (key) => new VString({ target: "query", key });
|
|
60
|
+
this.queries = (key) => new VStringArray({ target: "queries", key });
|
|
61
|
+
this.header = (key) => new VString({ target: "header", key });
|
|
62
|
+
this.body = (key) => new VString({ target: "body", key });
|
|
63
|
+
this.json = (key) => {
|
|
64
|
+
if (this.isArray) {
|
|
65
|
+
return new VStringArray({ target: "json", key });
|
|
66
|
+
} else {
|
|
67
|
+
return new VString({ target: "json", key });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
this.array = (path, validator) => {
|
|
71
|
+
this.isArray = true;
|
|
72
|
+
const res = validator(this);
|
|
73
|
+
const arr = new VArray(res, path);
|
|
74
|
+
return arr;
|
|
75
|
+
};
|
|
76
|
+
this.object = (path, validator) => {
|
|
77
|
+
this.isArray = false;
|
|
78
|
+
const res = validator(this);
|
|
79
|
+
const obj = new VObject(res, path);
|
|
80
|
+
return obj;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var VBase = class {
|
|
85
|
+
constructor(options) {
|
|
86
|
+
this.baseKeys = [];
|
|
87
|
+
this._nested = () => this.baseKeys.length ? true : false;
|
|
88
|
+
this.addSanitizer = (sanitizer2) => {
|
|
89
|
+
this.sanitizers.push(sanitizer2);
|
|
90
|
+
return this;
|
|
91
|
+
};
|
|
92
|
+
this.message = (text) => {
|
|
93
|
+
const len = this.rules.length;
|
|
94
|
+
if (len >= 1) {
|
|
95
|
+
this.rules[len - 1].customMessage = text;
|
|
96
|
+
}
|
|
97
|
+
return this;
|
|
98
|
+
};
|
|
99
|
+
this.isRequired = () => {
|
|
100
|
+
return this.addRule("isRequired", (value) => {
|
|
101
|
+
if (value !== void 0 && value !== null && value !== "")
|
|
102
|
+
return true;
|
|
103
|
+
return false;
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
this.isOptional = () => {
|
|
107
|
+
this._optional = true;
|
|
108
|
+
return this.addRule("isOptional", () => true);
|
|
109
|
+
};
|
|
110
|
+
this.isEqual = (comparison) => {
|
|
111
|
+
return this.addRule("isEqual", (value) => {
|
|
112
|
+
return value === comparison;
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
this.asNumber = () => {
|
|
116
|
+
const newVNumber = new VNumber({ ...this, type: "number" });
|
|
117
|
+
if (this.isArray)
|
|
118
|
+
return newVNumber.asArray();
|
|
119
|
+
return newVNumber;
|
|
120
|
+
};
|
|
121
|
+
this.asBoolean = () => {
|
|
122
|
+
const newVBoolean = new VBoolean({ ...this, type: "boolean" });
|
|
123
|
+
if (this.isArray)
|
|
124
|
+
return newVBoolean.asArray();
|
|
125
|
+
return newVBoolean;
|
|
126
|
+
};
|
|
127
|
+
this.validate = async (req) => {
|
|
128
|
+
let value = void 0;
|
|
129
|
+
let jsonData = void 0;
|
|
130
|
+
if (this.target === "query") {
|
|
131
|
+
value = req.query(this.key);
|
|
132
|
+
}
|
|
133
|
+
if (this.target === "queries") {
|
|
134
|
+
value = req.queries(this.key);
|
|
135
|
+
}
|
|
136
|
+
if (this.target === "header") {
|
|
137
|
+
value = req.header(this.key);
|
|
138
|
+
}
|
|
139
|
+
if (this.target === "body") {
|
|
140
|
+
const body = await req.parseBody();
|
|
141
|
+
value = body[this.key];
|
|
142
|
+
}
|
|
143
|
+
if (this.target === "json") {
|
|
144
|
+
if (this._nested()) {
|
|
145
|
+
this.key = `${this.baseKeys.join(".")}.${this.key}`;
|
|
146
|
+
}
|
|
147
|
+
let obj = {};
|
|
148
|
+
try {
|
|
149
|
+
obj = await req.json();
|
|
150
|
+
} catch (e) {
|
|
151
|
+
throw new Error("Malformed JSON in request body");
|
|
152
|
+
}
|
|
153
|
+
const dst = {};
|
|
154
|
+
value = JSONPathCopy(obj, dst, this.key);
|
|
155
|
+
if (this._nested())
|
|
156
|
+
jsonData = dst;
|
|
157
|
+
}
|
|
158
|
+
const results = [];
|
|
159
|
+
let typeRule = this.rules.shift();
|
|
160
|
+
for (const rule2 of this.rules) {
|
|
161
|
+
if (rule2.type === "type") {
|
|
162
|
+
typeRule = rule2;
|
|
163
|
+
} else if (rule2.type === "value") {
|
|
164
|
+
const result = this.validateRule(rule2, value);
|
|
165
|
+
result.jsonData || (result.jsonData = jsonData);
|
|
166
|
+
results.push(result);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (typeRule) {
|
|
170
|
+
const typeResult = this.validateRule(typeRule, value);
|
|
171
|
+
typeResult.jsonData || (typeResult.jsonData = jsonData);
|
|
172
|
+
results.unshift(typeResult);
|
|
173
|
+
this.rules.unshift(typeRule);
|
|
174
|
+
}
|
|
175
|
+
return results;
|
|
176
|
+
};
|
|
177
|
+
this.validateType = (value) => {
|
|
178
|
+
if (this.isArray) {
|
|
179
|
+
if (!Array.isArray(value)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
for (const val of value) {
|
|
183
|
+
if (typeof val === "undefined" && this._nested()) {
|
|
184
|
+
value.pop();
|
|
185
|
+
}
|
|
186
|
+
for (const val2 of value) {
|
|
187
|
+
if (typeof val2 !== this.type) {
|
|
188
|
+
if (!this._optional || typeof val2 !== "undefined")
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
if (typeof value !== this.type) {
|
|
195
|
+
if (this._optional && (typeof value === "undefined" || Array.isArray(value))) {
|
|
196
|
+
} else {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
203
|
+
this.validateValue = (func, value) => {
|
|
204
|
+
if (this._optional && typeof value === "undefined")
|
|
205
|
+
return true;
|
|
206
|
+
if (Array.isArray(value)) {
|
|
207
|
+
if (value.length === 0 && !this._optional)
|
|
208
|
+
return false;
|
|
209
|
+
for (const sanitizer2 of this.sanitizers) {
|
|
210
|
+
value = value.map((innerVal) => sanitizer2(innerVal));
|
|
211
|
+
}
|
|
212
|
+
for (const val of value) {
|
|
213
|
+
if (!func(val)) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
} else {
|
|
219
|
+
for (const sanitizer2 of this.sanitizers) {
|
|
220
|
+
value = sanitizer2(value);
|
|
221
|
+
}
|
|
222
|
+
if (!func(value)) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
this.getMessage = (opts) => {
|
|
229
|
+
let keyText;
|
|
230
|
+
const valueText = Array.isArray(opts.value) ? `${opts.value.map(
|
|
231
|
+
(val) => val === void 0 ? "undefined" : typeof val === "string" ? `"${val}"` : val
|
|
232
|
+
).join(", ")}` : opts.value;
|
|
233
|
+
switch (this.target) {
|
|
234
|
+
case "query":
|
|
235
|
+
keyText = `the query parameter "${this.key}"`;
|
|
236
|
+
break;
|
|
237
|
+
case "queries":
|
|
238
|
+
keyText = `the query parameters "${this.key}"`;
|
|
239
|
+
break;
|
|
240
|
+
case "header":
|
|
241
|
+
keyText = `the request header "${this.key}"`;
|
|
242
|
+
break;
|
|
243
|
+
case "body":
|
|
244
|
+
keyText = `the request body "${this.key}"`;
|
|
245
|
+
break;
|
|
246
|
+
case "json":
|
|
247
|
+
keyText = `the JSON body "${this.key}"`;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
return `Invalid Value [${valueText}]: ${keyText} is invalid - ${opts.ruleName}`;
|
|
251
|
+
};
|
|
252
|
+
this.target = options.target;
|
|
253
|
+
this.key = options.key;
|
|
254
|
+
this.type = options.type || "string";
|
|
255
|
+
this.rules = [
|
|
256
|
+
{
|
|
257
|
+
name: this.getTypeRuleName(),
|
|
258
|
+
type: "type",
|
|
259
|
+
func: this.validateType
|
|
260
|
+
}
|
|
261
|
+
];
|
|
262
|
+
this.sanitizers = [];
|
|
263
|
+
this._optional = false;
|
|
264
|
+
this.isArray = options.isArray || false;
|
|
265
|
+
}
|
|
266
|
+
addRule(arg, func) {
|
|
267
|
+
if (typeof arg === "string" && func) {
|
|
268
|
+
this.rules.push({ name: arg, func, type: "value" });
|
|
269
|
+
} else if (arg instanceof Function) {
|
|
270
|
+
this.rules.push({ name: arg.name, func: arg, type: "value" });
|
|
271
|
+
}
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
get(value) {
|
|
275
|
+
const len = this.rules.length;
|
|
276
|
+
if (len > 0) {
|
|
277
|
+
this.rules[this.rules.length - 1].customMessage = value;
|
|
278
|
+
}
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
getTypeRuleName() {
|
|
282
|
+
const prefix = "should be";
|
|
283
|
+
return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`;
|
|
284
|
+
}
|
|
285
|
+
validateRule(rule2, value) {
|
|
286
|
+
let isValid = false;
|
|
287
|
+
if (this._nested() && this.target != "json") {
|
|
288
|
+
isValid = false;
|
|
289
|
+
} else if (rule2.type === "value") {
|
|
290
|
+
isValid = this.validateValue(rule2.func, value);
|
|
291
|
+
} else if (rule2.type === "type") {
|
|
292
|
+
isValid = this.validateType(value);
|
|
293
|
+
}
|
|
294
|
+
const message = isValid ? void 0 : rule2.customMessage || this.getMessage({ ruleName: rule2.name, value });
|
|
295
|
+
const result = {
|
|
296
|
+
isValid,
|
|
297
|
+
message,
|
|
298
|
+
target: this.target,
|
|
299
|
+
key: this.key,
|
|
300
|
+
value,
|
|
301
|
+
ruleName: rule2.name,
|
|
302
|
+
ruleType: rule2.type
|
|
303
|
+
};
|
|
304
|
+
return result;
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
var VString = class extends VBase {
|
|
308
|
+
constructor(options) {
|
|
309
|
+
super(options);
|
|
310
|
+
this.asArray = () => {
|
|
311
|
+
return new VStringArray(this);
|
|
312
|
+
};
|
|
313
|
+
this.isEmpty = (options = { ignore_whitespace: false }) => {
|
|
314
|
+
return this.addRule("isEmpty", (value) => rule.isEmpty(value, options));
|
|
315
|
+
};
|
|
316
|
+
this.isLength = (options, arg2) => {
|
|
317
|
+
return this.addRule("isLength", (value) => rule.isLength(value, options, arg2));
|
|
318
|
+
};
|
|
319
|
+
this.isAlpha = () => {
|
|
320
|
+
return this.addRule("isAlpha", (value) => rule.isAlpha(value));
|
|
321
|
+
};
|
|
322
|
+
this.isNumeric = () => {
|
|
323
|
+
return this.addRule("isNumeric", (value) => rule.isNumeric(value));
|
|
324
|
+
};
|
|
325
|
+
this.contains = (elem, options = {
|
|
326
|
+
ignoreCase: false,
|
|
327
|
+
minOccurrences: 1
|
|
328
|
+
}) => {
|
|
329
|
+
return this.addRule("contains", (value) => rule.contains(value, elem, options));
|
|
330
|
+
};
|
|
331
|
+
this.isIn = (options) => {
|
|
332
|
+
return this.addRule("isIn", (value) => rule.isIn(value, options));
|
|
333
|
+
};
|
|
334
|
+
this.match = (regExp) => {
|
|
335
|
+
return this.addRule("match", (value) => rule.match(value, regExp));
|
|
336
|
+
};
|
|
337
|
+
this.trim = () => {
|
|
338
|
+
return this.addSanitizer((value) => sanitizer.trim(value));
|
|
339
|
+
};
|
|
340
|
+
this.type = "string";
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
var VNumber = class extends VBase {
|
|
344
|
+
constructor(options) {
|
|
345
|
+
super(options);
|
|
346
|
+
this.asArray = () => {
|
|
347
|
+
return new VNumberArray(this);
|
|
348
|
+
};
|
|
349
|
+
this.isGte = (min) => {
|
|
350
|
+
return this.addRule("isGte", (value) => rule.isGte(value, min));
|
|
351
|
+
};
|
|
352
|
+
this.isLte = (min) => {
|
|
353
|
+
return this.addRule("isLte", (value) => rule.isLte(value, min));
|
|
354
|
+
};
|
|
355
|
+
this.type = "number";
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
var VBoolean = class extends VBase {
|
|
359
|
+
constructor(options) {
|
|
360
|
+
super(options);
|
|
361
|
+
this.asArray = () => {
|
|
362
|
+
return new VBooleanArray(this);
|
|
363
|
+
};
|
|
364
|
+
this.isTrue = () => {
|
|
365
|
+
return this.addRule("isTrue", (value) => rule.isTrue(value));
|
|
366
|
+
};
|
|
367
|
+
this.isFalse = () => {
|
|
368
|
+
return this.addRule("isFalse", (value) => rule.isFalse(value));
|
|
369
|
+
};
|
|
370
|
+
this.type = "boolean";
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
var VNumberArray = class extends VNumber {
|
|
374
|
+
constructor(options) {
|
|
375
|
+
super(options);
|
|
376
|
+
this.isArray = true;
|
|
377
|
+
this.rules[0].name = this.getTypeRuleName();
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
var VStringArray = class extends VString {
|
|
381
|
+
constructor(options) {
|
|
382
|
+
super(options);
|
|
383
|
+
this.isArray = true;
|
|
384
|
+
this.rules[0].name = this.getTypeRuleName();
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var VBooleanArray = class extends VBoolean {
|
|
388
|
+
constructor(options) {
|
|
389
|
+
super(options);
|
|
390
|
+
this.isArray = true;
|
|
391
|
+
this.rules[0].name = this.getTypeRuleName();
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
export {
|
|
395
|
+
VArray,
|
|
396
|
+
VBase,
|
|
397
|
+
VBoolean,
|
|
398
|
+
VBooleanArray,
|
|
399
|
+
VNumber,
|
|
400
|
+
VNumberArray,
|
|
401
|
+
VObject,
|
|
402
|
+
VObjectBase,
|
|
403
|
+
VString,
|
|
404
|
+
VStringArray,
|
|
405
|
+
Validator
|
|
406
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
|
-
"main": "dist/index.js",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"module": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
8
9
|
"files": [
|
|
@@ -13,11 +14,12 @@
|
|
|
13
14
|
"test:deno": "deno test --allow-read deno_test",
|
|
14
15
|
"test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx",
|
|
15
16
|
"test:all": "yarn test && yarn test:deno && yarn test:bun",
|
|
16
|
-
"lint": "eslint --ext js,ts src .eslintrc.
|
|
17
|
-
"lint:fix": "eslint --ext js,ts src .eslintrc.
|
|
17
|
+
"lint": "eslint --ext js,ts src .eslintrc.cjs",
|
|
18
|
+
"lint:fix": "eslint --ext js,ts src .eslintrc.cjs --fix",
|
|
18
19
|
"denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
20
|
+
"copy:package.cjs.json": "cp ./package.cjs.json ./dist/cjs/package.json",
|
|
21
|
+
"build": "rimraf dist && tsx ./build.ts && yarn copy:package.cjs.json",
|
|
22
|
+
"watch": "rimraf dist && tsx ./build.ts --watch && yarn copy:package.cjs.json",
|
|
21
23
|
"prerelease": "yarn denoify && yarn test:deno && yarn build",
|
|
22
24
|
"release": "np"
|
|
23
25
|
},
|
|
@@ -91,7 +93,7 @@
|
|
|
91
93
|
"require": "./dist/cjs/middleware/serve-static/bun.js"
|
|
92
94
|
},
|
|
93
95
|
"./serve-static.module": {
|
|
94
|
-
"import": "./dist/middleware/serve-static/module.
|
|
96
|
+
"import": "./dist/middleware/serve-static/module.js"
|
|
95
97
|
},
|
|
96
98
|
"./validator": {
|
|
97
99
|
"import": "./dist/middleware/validator/index.js",
|
|
@@ -173,7 +175,7 @@
|
|
|
173
175
|
"./dist/middleware/serve-static/bun.d.ts"
|
|
174
176
|
],
|
|
175
177
|
"serve-static.module": [
|
|
176
|
-
"./dist/middleware/serve-static/module.d.
|
|
178
|
+
"./dist/middleware/serve-static/module.d.ts"
|
|
177
179
|
],
|
|
178
180
|
"validator": [
|
|
179
181
|
"./dist/middleware/validator"
|
|
@@ -226,12 +228,15 @@
|
|
|
226
228
|
"devDependencies": {
|
|
227
229
|
"@cloudflare/workers-types": "^3.16.0",
|
|
228
230
|
"@types/crypto-js": "^4.1.1",
|
|
231
|
+
"@types/glob": "^8.0.0",
|
|
229
232
|
"@types/jest": "^29.0.2",
|
|
230
233
|
"@types/node": "^17.0.29",
|
|
231
234
|
"@typescript-eslint/eslint-plugin": "^5.21.0",
|
|
232
235
|
"@typescript-eslint/parser": "^5.21.0",
|
|
236
|
+
"arg": "^5.0.2",
|
|
233
237
|
"crypto-js": "^4.1.1",
|
|
234
238
|
"denoify": "^1.1.1",
|
|
239
|
+
"esbuild": "^0.15.12",
|
|
235
240
|
"eslint": "^8.14.0",
|
|
236
241
|
"eslint-config-prettier": "^8.5.0",
|
|
237
242
|
"eslint-define-config": "^1.4.0",
|
|
@@ -247,6 +252,7 @@
|
|
|
247
252
|
"prettier": "^2.6.2",
|
|
248
253
|
"rimraf": "^3.0.2",
|
|
249
254
|
"ts-jest": "^29.0.1",
|
|
255
|
+
"tsx": "^3.11.0",
|
|
250
256
|
"typescript": "^4.8.3"
|
|
251
257
|
},
|
|
252
258
|
"engines": {
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Some validation rules is are based on Validator.js
|
|
3
|
-
// Validator.js
|
|
4
|
-
// License (MIT)
|
|
5
|
-
// Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
|
|
6
|
-
// https://github.com/validatorjs/validator.js
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.rule = void 0;
|
|
9
|
-
exports.rule = {
|
|
10
|
-
// string
|
|
11
|
-
isEmpty(value, options = { ignore_whitespace: false }) {
|
|
12
|
-
return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
|
|
13
|
-
},
|
|
14
|
-
isLength: (value, options, arg2) => {
|
|
15
|
-
let min;
|
|
16
|
-
let max;
|
|
17
|
-
if (typeof options === 'object') {
|
|
18
|
-
min = options.min || 0;
|
|
19
|
-
max = options.max;
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
// backwards compatibility: isLength(str, min [, max])
|
|
23
|
-
min = options || 0;
|
|
24
|
-
max = arg2;
|
|
25
|
-
}
|
|
26
|
-
const presentationSequences = value.match(/(\uFE0F|\uFE0E)/g) || [];
|
|
27
|
-
const surrogatePairs = value.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
|
|
28
|
-
const len = value.length - presentationSequences.length - surrogatePairs.length;
|
|
29
|
-
return len >= min && (typeof max === 'undefined' || len <= max);
|
|
30
|
-
},
|
|
31
|
-
isAlpha: (value) => {
|
|
32
|
-
return /^[A-Z]+$/i.test(value);
|
|
33
|
-
},
|
|
34
|
-
isNumeric: (value) => {
|
|
35
|
-
return /^[0-9]+$/.test(value);
|
|
36
|
-
},
|
|
37
|
-
contains: (value, elem, options = {
|
|
38
|
-
ignoreCase: false,
|
|
39
|
-
minOccurrences: 1,
|
|
40
|
-
}) => {
|
|
41
|
-
options.ignoreCase || (options.ignoreCase = false);
|
|
42
|
-
options.minOccurrences || (options.minOccurrences = 1);
|
|
43
|
-
if (options.ignoreCase) {
|
|
44
|
-
return value.toLowerCase().split(elem.toLowerCase()).length > options.minOccurrences;
|
|
45
|
-
}
|
|
46
|
-
return value.split(elem).length > options.minOccurrences;
|
|
47
|
-
},
|
|
48
|
-
isIn: (value, options) => {
|
|
49
|
-
if (typeof options === 'object') {
|
|
50
|
-
for (const elem of options) {
|
|
51
|
-
if (elem === value)
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
},
|
|
57
|
-
match: (value, regExp) => {
|
|
58
|
-
return regExp.test(value);
|
|
59
|
-
},
|
|
60
|
-
// number
|
|
61
|
-
isGte: (value, min) => min <= value,
|
|
62
|
-
isLte: (value, max) => value <= max,
|
|
63
|
-
// boolean
|
|
64
|
-
isTrue: (value) => value === true,
|
|
65
|
-
isFalse: (value) => value === false,
|
|
66
|
-
};
|