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.
@@ -170,10 +170,10 @@ class Context {
170
170
  if (global?.fastly !== void 0) {
171
171
  return "fastly";
172
172
  }
173
- if (typeof global?.EdgeRuntime !== "string") {
173
+ if (typeof global?.EdgeRuntime === "string") {
174
174
  return "vercel";
175
175
  }
176
- if (global?.process?.title === "node") {
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 === "OPTIONS") {
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 === "*" || path === p) && middleware[m][p].push(handler);
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
- ...routes[import_router.METHOD_NAME_ALL][path2] || findMiddleware(middleware[method], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || []
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
- keys.push(...v.keys);
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.isArray = false;
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.isArray) {
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, validator) => {
103
- this.isArray = true;
104
- const res = validator(this);
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, validator) => {
109
- this.isArray = false;
110
- const res = validator(this);
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 false;
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 !== "string") {
151
+ if (typeof global?.EdgeRuntime === "string") {
152
152
  return "vercel";
153
153
  }
154
- if (global?.process?.title === "node") {
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 === "OPTIONS") {
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: any) => HtmlEscapedString;
3
- export declare const html: (strings: TemplateStringsArray, ...values: any[]) => HtmlEscapedString;
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]: Record<string, any>;
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: Record<string, any>;
13
+ props: Props;
13
14
  children: Child[];
14
15
  isEscaped: true;
15
- constructor(tag: string | Function, props: Record<string, any>, children: Child[]);
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: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => JSXNode;
21
- declare type FC<T = Record<string, any>> = (props: T) => HtmlEscapedString;
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?: any;
26
+ children?: Child[];
26
27
  }) => JSXNode;
@@ -1,3 +1,3 @@
1
1
  import type { JSXNode } from '.';
2
2
  export { Fragment } from '.';
3
- export declare function jsxDEV(tag: string | Function, props: Record<string, any>): JSXNode;
3
+ export declare function jsxDEV(tag: string | Function, props: Record<string, unknown>): 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, any>;
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 === "*" || path === p) && middleware[m][p].push(handler);
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
- ...routes[METHOD_NAME_ALL][path2] || findMiddleware(middleware[method], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
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<any>): void;
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
- isArray: boolean;
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, validator: (v: Validator) => T) => VArray<T>;
46
- object: <T extends Schema>(path: string, validator: (v: Validator) => T) => VObject<T>;
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]: any;
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
- keys.push(...v.keys);
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.isArray = false;
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.isArray) {
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, validator) => {
71
- this.isArray = true;
72
- const res = validator(this);
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, validator) => {
77
- this.isArray = false;
78
- const res = validator(this);
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 false;
179
+ return this._optional && typeof value === "undefined";
186
180
  }
187
181
  for (const val of value) {
188
182
  if (typeof val === "undefined" && this._nested()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "2.5.6",
3
+ "version": "2.5.8",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers, Deno, and Bun.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",