hono 2.5.6 → 2.5.8
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/context.js +2 -2
- package/dist/cjs/middleware/cors/index.js +3 -2
- package/dist/cjs/router/reg-exp-router/router.js +13 -5
- package/dist/cjs/validator/validator.js +11 -17
- package/dist/context.js +2 -2
- package/dist/middleware/cors/index.js +3 -2
- package/dist/middleware/html/index.d.ts +2 -2
- package/dist/middleware/jsx/index.d.ts +7 -6
- package/dist/middleware/jsx/jsx-dev-runtime.d.ts +1 -1
- package/dist/request.d.ts +1 -1
- package/dist/router/reg-exp-router/router.js +13 -5
- package/dist/types.d.ts +1 -1
- package/dist/validator/validator.d.ts +5 -4
- package/dist/validator/validator.js +11 -17
- package/package.json +1 -1
package/dist/cjs/context.js
CHANGED
|
@@ -170,10 +170,10 @@ class Context {
|
|
|
170
170
|
if (global?.fastly !== void 0) {
|
|
171
171
|
return "fastly";
|
|
172
172
|
}
|
|
173
|
-
if (typeof global?.EdgeRuntime
|
|
173
|
+
if (typeof global?.EdgeRuntime === "string") {
|
|
174
174
|
return "vercel";
|
|
175
175
|
}
|
|
176
|
-
if (global?.process?.
|
|
176
|
+
if (global?.process?.release?.name === "node") {
|
|
177
177
|
return "node";
|
|
178
178
|
}
|
|
179
179
|
return "other";
|
|
@@ -42,7 +42,6 @@ const cors = (options) => {
|
|
|
42
42
|
}
|
|
43
43
|
})(opts.origin);
|
|
44
44
|
return async (c, next) => {
|
|
45
|
-
await next();
|
|
46
45
|
function set(key, value) {
|
|
47
46
|
c.res.headers.append(key, value);
|
|
48
47
|
}
|
|
@@ -59,7 +58,9 @@ const cors = (options) => {
|
|
|
59
58
|
if (opts.exposeHeaders?.length) {
|
|
60
59
|
set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
|
61
60
|
}
|
|
62
|
-
if (c.req.method
|
|
61
|
+
if (c.req.method !== "OPTIONS") {
|
|
62
|
+
await next();
|
|
63
|
+
} else {
|
|
63
64
|
if (opts.maxAge != null) {
|
|
64
65
|
set("Access-Control-Max-Age", opts.maxAge.toString());
|
|
65
66
|
}
|
|
@@ -37,6 +37,7 @@ function buildMatcherFromPreprocessedRoutes(routes) {
|
|
|
37
37
|
if (routes.length === 0) {
|
|
38
38
|
return nullMatcher;
|
|
39
39
|
}
|
|
40
|
+
routes = routes.sort(([a], [b]) => a.length - b.length);
|
|
40
41
|
for (let i = 0, len = routes.length; i < len; i++) {
|
|
41
42
|
let paramMap;
|
|
42
43
|
try {
|
|
@@ -85,18 +86,26 @@ class RegExpRouter {
|
|
|
85
86
|
}
|
|
86
87
|
if (!methodNames.includes(method))
|
|
87
88
|
methodNames.push(method);
|
|
89
|
+
if (!middleware[method]) {
|
|
90
|
+
;
|
|
91
|
+
[middleware, routes].forEach((handlerMap) => {
|
|
92
|
+
handlerMap[method] = {};
|
|
93
|
+
Object.keys(handlerMap[import_router.METHOD_NAME_ALL]).forEach((p) => {
|
|
94
|
+
handlerMap[method][p] = [...handlerMap[import_router.METHOD_NAME_ALL][p]];
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
88
98
|
if (path === "/*") {
|
|
89
99
|
path = "*";
|
|
90
100
|
}
|
|
91
101
|
if (/\*$/.test(path)) {
|
|
92
|
-
middleware[method] || (middleware[method] = {});
|
|
93
102
|
const re = buildWildcardRegExp(path);
|
|
94
|
-
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || []);
|
|
103
|
+
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[method], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || []);
|
|
95
104
|
Object.keys(middleware).forEach((m) => {
|
|
96
105
|
if (method === import_router.METHOD_NAME_ALL || method === m) {
|
|
97
106
|
Object.keys(middleware[m]).forEach((p) => {
|
|
98
107
|
;
|
|
99
|
-
(path === "*" ||
|
|
108
|
+
(path === "*" || re.test(p)) && middleware[m][p].push(handler);
|
|
100
109
|
});
|
|
101
110
|
}
|
|
102
111
|
});
|
|
@@ -112,12 +121,11 @@ class RegExpRouter {
|
|
|
112
121
|
const paths = (0, import_url.checkOptionalParameter)(path) || [path];
|
|
113
122
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
114
123
|
const path2 = paths[i];
|
|
115
|
-
routes[method] || (routes[method] = {});
|
|
116
124
|
Object.keys(routes).forEach((m) => {
|
|
117
125
|
var _a2;
|
|
118
126
|
if (method === import_router.METHOD_NAME_ALL || method === m) {
|
|
119
127
|
(_a2 = routes[m])[path2] || (_a2[path2] = [
|
|
120
|
-
...
|
|
128
|
+
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || []
|
|
121
129
|
]);
|
|
122
130
|
routes[m][path2].push(handler);
|
|
123
131
|
}
|
|
@@ -40,17 +40,11 @@ class VObjectBase {
|
|
|
40
40
|
this._isOptional = false;
|
|
41
41
|
this.getValidators = () => {
|
|
42
42
|
const validators = [];
|
|
43
|
-
const thisKeys = [];
|
|
44
|
-
Object.assign(thisKeys, this.keys);
|
|
45
43
|
const walk = (container, keys, isOptional) => {
|
|
46
44
|
for (const v of Object.values(container)) {
|
|
47
45
|
if (v instanceof VArray || v instanceof VObject) {
|
|
48
46
|
isOptional || (isOptional = v._isOptional);
|
|
49
|
-
|
|
50
|
-
walk(v.container, keys, isOptional);
|
|
51
|
-
const tmp = [];
|
|
52
|
-
Object.assign(tmp, thisKeys);
|
|
53
|
-
keys = tmp;
|
|
47
|
+
walk(v.container, [...keys, ...v.keys], isOptional);
|
|
54
48
|
} else if (v instanceof VBase) {
|
|
55
49
|
if (isOptional)
|
|
56
50
|
v.isOptional();
|
|
@@ -86,28 +80,28 @@ class VArray extends VObjectBase {
|
|
|
86
80
|
}
|
|
87
81
|
}
|
|
88
82
|
class Validator {
|
|
89
|
-
constructor() {
|
|
90
|
-
this.
|
|
83
|
+
constructor(inArray = false) {
|
|
84
|
+
this.inArray = inArray;
|
|
91
85
|
this.query = (key) => new VString({ target: "query", key });
|
|
92
86
|
this.queries = (key) => new VStringArray({ target: "queries", key });
|
|
93
87
|
this.header = (key) => new VString({ target: "header", key });
|
|
94
88
|
this.body = (key) => new VString({ target: "body", key });
|
|
95
89
|
this.json = (key) => {
|
|
96
|
-
if (this.
|
|
90
|
+
if (this.inArray) {
|
|
97
91
|
return new VStringArray({ target: "json", key });
|
|
98
92
|
} else {
|
|
99
93
|
return new VString({ target: "json", key });
|
|
100
94
|
}
|
|
101
95
|
};
|
|
102
|
-
this.array = (path,
|
|
103
|
-
|
|
104
|
-
const res = validator
|
|
96
|
+
this.array = (path, validatorFn) => {
|
|
97
|
+
const validator = new Validator(true);
|
|
98
|
+
const res = validatorFn(validator);
|
|
105
99
|
const arr = new VArray(res, path);
|
|
106
100
|
return arr;
|
|
107
101
|
};
|
|
108
|
-
this.object = (path,
|
|
109
|
-
|
|
110
|
-
const res = validator
|
|
102
|
+
this.object = (path, validatorFn) => {
|
|
103
|
+
const validator = new Validator(this.inArray);
|
|
104
|
+
const res = validatorFn(validator);
|
|
111
105
|
const obj = new VObject(res, path);
|
|
112
106
|
return obj;
|
|
113
107
|
};
|
|
@@ -214,7 +208,7 @@ class VBase {
|
|
|
214
208
|
this.validateType = (value) => {
|
|
215
209
|
if (this.isArray) {
|
|
216
210
|
if (!Array.isArray(value)) {
|
|
217
|
-
return
|
|
211
|
+
return this._optional && typeof value === "undefined";
|
|
218
212
|
}
|
|
219
213
|
for (const val of value) {
|
|
220
214
|
if (typeof val === "undefined" && this._nested()) {
|
package/dist/context.js
CHANGED
|
@@ -148,10 +148,10 @@ var Context = class {
|
|
|
148
148
|
if (global?.fastly !== void 0) {
|
|
149
149
|
return "fastly";
|
|
150
150
|
}
|
|
151
|
-
if (typeof global?.EdgeRuntime
|
|
151
|
+
if (typeof global?.EdgeRuntime === "string") {
|
|
152
152
|
return "vercel";
|
|
153
153
|
}
|
|
154
|
-
if (global?.process?.
|
|
154
|
+
if (global?.process?.release?.name === "node") {
|
|
155
155
|
return "node";
|
|
156
156
|
}
|
|
157
157
|
return "other";
|
|
@@ -20,7 +20,6 @@ var cors = (options) => {
|
|
|
20
20
|
}
|
|
21
21
|
})(opts.origin);
|
|
22
22
|
return async (c, next) => {
|
|
23
|
-
await next();
|
|
24
23
|
function set(key, value) {
|
|
25
24
|
c.res.headers.append(key, value);
|
|
26
25
|
}
|
|
@@ -37,7 +36,9 @@ var cors = (options) => {
|
|
|
37
36
|
if (opts.exposeHeaders?.length) {
|
|
38
37
|
set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
|
39
38
|
}
|
|
40
|
-
if (c.req.method
|
|
39
|
+
if (c.req.method !== "OPTIONS") {
|
|
40
|
+
await next();
|
|
41
|
+
} else {
|
|
41
42
|
if (opts.maxAge != null) {
|
|
42
43
|
set("Access-Control-Max-Age", opts.maxAge.toString());
|
|
43
44
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { HtmlEscapedString } from '../../utils/html';
|
|
2
|
-
export declare const raw: (value:
|
|
3
|
-
export declare const html: (strings: TemplateStringsArray, ...values:
|
|
2
|
+
export declare const raw: (value: unknown) => HtmlEscapedString;
|
|
3
|
+
export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => HtmlEscapedString;
|
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
import type { StringBuffer, HtmlEscaped, HtmlEscapedString } from '../../utils/html';
|
|
2
|
+
declare type Props = Record<string, any>;
|
|
2
3
|
declare global {
|
|
3
4
|
namespace jsx.JSX {
|
|
4
5
|
interface IntrinsicElements {
|
|
5
|
-
[tagName: string]:
|
|
6
|
+
[tagName: string]: Props;
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
10
|
declare type Child = string | number | JSXNode | Child[];
|
|
10
11
|
export declare class JSXNode implements HtmlEscaped {
|
|
11
12
|
tag: string | Function;
|
|
12
|
-
props:
|
|
13
|
+
props: Props;
|
|
13
14
|
children: Child[];
|
|
14
15
|
isEscaped: true;
|
|
15
|
-
constructor(tag: string | Function, props:
|
|
16
|
+
constructor(tag: string | Function, props: Props, children: Child[]);
|
|
16
17
|
toString(): string;
|
|
17
18
|
toStringToBuffer(buffer: StringBuffer): void;
|
|
18
19
|
}
|
|
19
20
|
export { jsxFn as jsx };
|
|
20
|
-
declare const jsxFn: (tag: string | Function, props:
|
|
21
|
-
declare type FC<T =
|
|
21
|
+
declare const jsxFn: (tag: string | Function, props: Props, ...children: (string | HtmlEscapedString)[]) => JSXNode;
|
|
22
|
+
declare type FC<T = Props> = (props: T) => HtmlEscapedString;
|
|
22
23
|
export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>;
|
|
23
24
|
export declare const Fragment: (props: {
|
|
24
25
|
key?: string;
|
|
25
|
-
children?:
|
|
26
|
+
children?: Child[];
|
|
26
27
|
}) => JSXNode;
|
package/dist/request.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BodyData } from './utils/body';
|
|
2
2
|
import type { Cookie } from './utils/cookie';
|
|
3
|
-
declare type ValidatedData = Record<string,
|
|
3
|
+
declare type ValidatedData = Record<string, unknown>;
|
|
4
4
|
declare global {
|
|
5
5
|
interface Request<CfHostMetadata = unknown, ParamKeyType extends string = string, Data extends ValidatedData = ValidatedData> {
|
|
6
6
|
paramData?: Record<ParamKeyType, string>;
|
|
@@ -15,6 +15,7 @@ function buildMatcherFromPreprocessedRoutes(routes) {
|
|
|
15
15
|
if (routes.length === 0) {
|
|
16
16
|
return nullMatcher;
|
|
17
17
|
}
|
|
18
|
+
routes = routes.sort(([a], [b]) => a.length - b.length);
|
|
18
19
|
for (let i = 0, len = routes.length; i < len; i++) {
|
|
19
20
|
let paramMap;
|
|
20
21
|
try {
|
|
@@ -63,18 +64,26 @@ var RegExpRouter = class {
|
|
|
63
64
|
}
|
|
64
65
|
if (!methodNames.includes(method))
|
|
65
66
|
methodNames.push(method);
|
|
67
|
+
if (!middleware[method]) {
|
|
68
|
+
;
|
|
69
|
+
[middleware, routes].forEach((handlerMap) => {
|
|
70
|
+
handlerMap[method] = {};
|
|
71
|
+
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
72
|
+
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
66
76
|
if (path === "/*") {
|
|
67
77
|
path = "*";
|
|
68
78
|
}
|
|
69
79
|
if (/\*$/.test(path)) {
|
|
70
|
-
middleware[method] || (middleware[method] = {});
|
|
71
80
|
const re = buildWildcardRegExp(path);
|
|
72
|
-
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
|
|
81
|
+
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
|
|
73
82
|
Object.keys(middleware).forEach((m) => {
|
|
74
83
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
75
84
|
Object.keys(middleware[m]).forEach((p) => {
|
|
76
85
|
;
|
|
77
|
-
(path === "*" ||
|
|
86
|
+
(path === "*" || re.test(p)) && middleware[m][p].push(handler);
|
|
78
87
|
});
|
|
79
88
|
}
|
|
80
89
|
});
|
|
@@ -90,12 +99,11 @@ var RegExpRouter = class {
|
|
|
90
99
|
const paths = checkOptionalParameter(path) || [path];
|
|
91
100
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
92
101
|
const path2 = paths[i];
|
|
93
|
-
routes[method] || (routes[method] = {});
|
|
94
102
|
Object.keys(routes).forEach((m) => {
|
|
95
103
|
var _a2;
|
|
96
104
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
97
105
|
(_a2 = routes[m])[path2] || (_a2[path2] = [
|
|
98
|
-
...
|
|
106
|
+
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
99
107
|
]);
|
|
100
108
|
routes[m][path2].push(handler);
|
|
101
109
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface CustomHandler<P extends string | Partial<Environment> | Schema
|
|
|
20
20
|
(c: Context<P extends string ? P : P extends Partial<Environment> ? string : P extends Partial<Schema> ? string : never, P extends Partial<Environment> ? P : P extends Partial<Schema> ? Partial<Environment> : E extends Partial<Environment> ? E extends Partial<Schema> ? Environment : E : E extends Partial<Schema> ? Partial<Environment> : Environment, S extends Schema ? S : P extends Schema ? P : E extends Schema ? E : any>, next: Next): Response | Promise<Response | undefined | void>;
|
|
21
21
|
}
|
|
22
22
|
export interface ExecutionContext {
|
|
23
|
-
waitUntil(promise: Promise<
|
|
23
|
+
waitUntil(promise: Promise<void>): void;
|
|
24
24
|
passThroughOnException(): void;
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
@@ -36,14 +36,15 @@ export declare class VArray<T extends Schema> extends VObjectBase<T> {
|
|
|
36
36
|
constructor(container: T, key: string);
|
|
37
37
|
}
|
|
38
38
|
export declare class Validator {
|
|
39
|
-
|
|
39
|
+
private inArray;
|
|
40
|
+
constructor(inArray?: boolean);
|
|
40
41
|
query: (key: string) => VString;
|
|
41
42
|
queries: (key: string) => VStringArray;
|
|
42
43
|
header: (key: string) => VString;
|
|
43
44
|
body: (key: string) => VString;
|
|
44
45
|
json: (key: string) => VString;
|
|
45
|
-
array: <T extends Schema>(path: string,
|
|
46
|
-
object: <T extends Schema>(path: string,
|
|
46
|
+
array: <T extends Schema>(path: string, validatorFn: (v: Validator) => T) => VArray<T>;
|
|
47
|
+
object: <T extends Schema>(path: string, validatorFn: (v: Validator) => T) => VObject<T>;
|
|
47
48
|
}
|
|
48
49
|
declare type VOptions = {
|
|
49
50
|
target: Target;
|
|
@@ -73,7 +74,7 @@ export declare abstract class VBase {
|
|
|
73
74
|
asBoolean: () => VBoolean | VBooleanArray;
|
|
74
75
|
get(value: string): this;
|
|
75
76
|
validate: <R extends Request<unknown, string, {
|
|
76
|
-
[x: string]:
|
|
77
|
+
[x: string]: unknown;
|
|
77
78
|
}>>(req: R) => Promise<ValidateResult[]>;
|
|
78
79
|
protected getTypeRuleName(): string;
|
|
79
80
|
private sanitizeValue;
|
|
@@ -8,17 +8,11 @@ var VObjectBase = class {
|
|
|
8
8
|
this._isOptional = false;
|
|
9
9
|
this.getValidators = () => {
|
|
10
10
|
const validators = [];
|
|
11
|
-
const thisKeys = [];
|
|
12
|
-
Object.assign(thisKeys, this.keys);
|
|
13
11
|
const walk = (container, keys, isOptional) => {
|
|
14
12
|
for (const v of Object.values(container)) {
|
|
15
13
|
if (v instanceof VArray || v instanceof VObject) {
|
|
16
14
|
isOptional || (isOptional = v._isOptional);
|
|
17
|
-
|
|
18
|
-
walk(v.container, keys, isOptional);
|
|
19
|
-
const tmp = [];
|
|
20
|
-
Object.assign(tmp, thisKeys);
|
|
21
|
-
keys = tmp;
|
|
15
|
+
walk(v.container, [...keys, ...v.keys], isOptional);
|
|
22
16
|
} else if (v instanceof VBase) {
|
|
23
17
|
if (isOptional)
|
|
24
18
|
v.isOptional();
|
|
@@ -54,28 +48,28 @@ var VArray = class extends VObjectBase {
|
|
|
54
48
|
}
|
|
55
49
|
};
|
|
56
50
|
var Validator = class {
|
|
57
|
-
constructor() {
|
|
58
|
-
this.
|
|
51
|
+
constructor(inArray = false) {
|
|
52
|
+
this.inArray = inArray;
|
|
59
53
|
this.query = (key) => new VString({ target: "query", key });
|
|
60
54
|
this.queries = (key) => new VStringArray({ target: "queries", key });
|
|
61
55
|
this.header = (key) => new VString({ target: "header", key });
|
|
62
56
|
this.body = (key) => new VString({ target: "body", key });
|
|
63
57
|
this.json = (key) => {
|
|
64
|
-
if (this.
|
|
58
|
+
if (this.inArray) {
|
|
65
59
|
return new VStringArray({ target: "json", key });
|
|
66
60
|
} else {
|
|
67
61
|
return new VString({ target: "json", key });
|
|
68
62
|
}
|
|
69
63
|
};
|
|
70
|
-
this.array = (path,
|
|
71
|
-
|
|
72
|
-
const res = validator
|
|
64
|
+
this.array = (path, validatorFn) => {
|
|
65
|
+
const validator = new Validator(true);
|
|
66
|
+
const res = validatorFn(validator);
|
|
73
67
|
const arr = new VArray(res, path);
|
|
74
68
|
return arr;
|
|
75
69
|
};
|
|
76
|
-
this.object = (path,
|
|
77
|
-
|
|
78
|
-
const res = validator
|
|
70
|
+
this.object = (path, validatorFn) => {
|
|
71
|
+
const validator = new Validator(this.inArray);
|
|
72
|
+
const res = validatorFn(validator);
|
|
79
73
|
const obj = new VObject(res, path);
|
|
80
74
|
return obj;
|
|
81
75
|
};
|
|
@@ -182,7 +176,7 @@ var VBase = class {
|
|
|
182
176
|
this.validateType = (value) => {
|
|
183
177
|
if (this.isArray) {
|
|
184
178
|
if (!Array.isArray(value)) {
|
|
185
|
-
return
|
|
179
|
+
return this._optional && typeof value === "undefined";
|
|
186
180
|
}
|
|
187
181
|
for (const val of value) {
|
|
188
182
|
if (typeof val === "undefined" && this._nested()) {
|