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.
Files changed (153) hide show
  1. package/dist/cjs/compose.js +82 -66
  2. package/dist/cjs/context.js +159 -144
  3. package/dist/cjs/hono.js +173 -150
  4. package/dist/cjs/index.js +31 -11
  5. package/dist/cjs/middleware/basic-auth/index.js +70 -39
  6. package/dist/cjs/middleware/bearer-auth/index.js +69 -51
  7. package/dist/cjs/middleware/cache/index.js +49 -28
  8. package/dist/cjs/middleware/compress/index.js +39 -16
  9. package/dist/cjs/middleware/cors/index.js +89 -71
  10. package/dist/cjs/middleware/etag/index.js +46 -24
  11. package/dist/cjs/middleware/html/index.js +50 -30
  12. package/dist/cjs/middleware/jsx/index.js +178 -166
  13. package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
  14. package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
  15. package/dist/cjs/middleware/jwt/index.js +74 -54
  16. package/dist/cjs/middleware/logger/index.js +59 -38
  17. package/dist/cjs/middleware/powered-by/index.js +30 -7
  18. package/dist/cjs/middleware/pretty-json/index.js +31 -8
  19. package/dist/cjs/middleware/serve-static/bun.js +56 -38
  20. package/dist/cjs/middleware/serve-static/index.js +27 -4
  21. package/dist/cjs/middleware/serve-static/module.js +42 -0
  22. package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
  23. package/dist/cjs/middleware/validator/index.js +27 -4
  24. package/dist/cjs/middleware/validator/middleware.js +125 -91
  25. package/dist/cjs/package.json +3 -0
  26. package/dist/cjs/request.js +129 -116
  27. package/dist/cjs/router/reg-exp-router/index.js +27 -4
  28. package/dist/cjs/router/reg-exp-router/node.js +108 -97
  29. package/dist/cjs/router/reg-exp-router/router.js +158 -141
  30. package/dist/cjs/router/reg-exp-router/trie.js +55 -39
  31. package/dist/cjs/router/smart-router/index.js +27 -4
  32. package/dist/cjs/router/smart-router/router.js +70 -49
  33. package/dist/cjs/router/static-router/index.js +27 -4
  34. package/dist/cjs/router/static-router/router.js +78 -64
  35. package/dist/cjs/router/trie-router/index.js +27 -4
  36. package/dist/cjs/router/trie-router/node.js +167 -159
  37. package/dist/cjs/router/trie-router/router.js +43 -20
  38. package/dist/cjs/router.js +35 -6
  39. package/dist/cjs/types.js +16 -0
  40. package/dist/cjs/utils/body.js +36 -15
  41. package/dist/cjs/utils/buffer.js +56 -31
  42. package/dist/cjs/utils/cloudflare.js +53 -34
  43. package/dist/cjs/utils/cookie.js +59 -35
  44. package/dist/cjs/utils/crypto.js +69 -43
  45. package/dist/cjs/utils/encode.js +92 -65
  46. package/dist/cjs/utils/filepath.js +39 -22
  47. package/dist/cjs/utils/html.js +54 -33
  48. package/dist/cjs/utils/http-status.js +68 -45
  49. package/dist/cjs/utils/json.js +91 -76
  50. package/dist/cjs/utils/jwt/index.js +32 -25
  51. package/dist/cjs/utils/jwt/jwt.js +124 -93
  52. package/dist/cjs/utils/jwt/types.js +65 -38
  53. package/dist/cjs/utils/mime.js +110 -87
  54. package/dist/cjs/utils/object.js +53 -35
  55. package/dist/cjs/utils/types.js +16 -0
  56. package/dist/cjs/utils/url.js +89 -78
  57. package/dist/cjs/validator/rule.js +80 -0
  58. package/dist/cjs/validator/sanitizer.js +30 -0
  59. package/dist/cjs/validator/schema.js +16 -0
  60. package/dist/cjs/validator/validator.js +439 -0
  61. package/dist/compose.d.ts +4 -3
  62. package/dist/compose.js +61 -64
  63. package/dist/context.d.ts +10 -9
  64. package/dist/context.js +139 -143
  65. package/dist/hono.d.ts +27 -42
  66. package/dist/hono.js +153 -149
  67. package/dist/index.d.ts +4 -1
  68. package/dist/index.js +9 -9
  69. package/dist/middleware/basic-auth/index.d.ts +1 -1
  70. package/dist/middleware/basic-auth/index.js +52 -40
  71. package/dist/middleware/bearer-auth/index.d.ts +1 -1
  72. package/dist/middleware/bearer-auth/index.js +48 -49
  73. package/dist/middleware/cache/index.d.ts +1 -1
  74. package/dist/middleware/cache/index.js +28 -26
  75. package/dist/middleware/compress/index.d.ts +1 -1
  76. package/dist/middleware/compress/index.js +18 -14
  77. package/dist/middleware/cors/index.d.ts +1 -1
  78. package/dist/middleware/cors/index.js +68 -69
  79. package/dist/middleware/etag/index.d.ts +1 -1
  80. package/dist/middleware/etag/index.js +25 -22
  81. package/dist/middleware/html/index.js +29 -28
  82. package/dist/middleware/jsx/index.js +164 -172
  83. package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
  84. package/dist/middleware/jsx/jsx-runtime.js +7 -2
  85. package/dist/middleware/jwt/index.d.ts +1 -1
  86. package/dist/middleware/jwt/index.js +53 -52
  87. package/dist/middleware/logger/index.d.ts +1 -1
  88. package/dist/middleware/logger/index.js +35 -39
  89. package/dist/middleware/powered-by/index.d.ts +1 -1
  90. package/dist/middleware/powered-by/index.js +9 -5
  91. package/dist/middleware/pretty-json/index.d.ts +1 -1
  92. package/dist/middleware/pretty-json/index.js +10 -6
  93. package/dist/middleware/serve-static/bun.d.ts +1 -1
  94. package/dist/middleware/serve-static/bun.js +35 -36
  95. package/dist/middleware/serve-static/index.js +5 -1
  96. package/dist/middleware/serve-static/module.d.ts +3 -0
  97. package/dist/middleware/serve-static/module.js +13 -0
  98. package/dist/middleware/serve-static/serve-static.d.ts +2 -3
  99. package/dist/middleware/serve-static/serve-static.js +36 -36
  100. package/dist/middleware/validator/index.js +5 -2
  101. package/dist/middleware/validator/middleware.d.ts +9 -15
  102. package/dist/middleware/validator/middleware.js +104 -89
  103. package/dist/request.d.ts +2 -2
  104. package/dist/request.js +108 -114
  105. package/dist/router/reg-exp-router/index.js +5 -1
  106. package/dist/router/reg-exp-router/node.js +87 -96
  107. package/dist/router/reg-exp-router/router.js +140 -142
  108. package/dist/router/reg-exp-router/trie.js +35 -38
  109. package/dist/router/smart-router/index.js +5 -1
  110. package/dist/router/smart-router/router.js +50 -48
  111. package/dist/router/static-router/index.js +5 -1
  112. package/dist/router/static-router/router.js +58 -63
  113. package/dist/router/trie-router/index.js +5 -1
  114. package/dist/router/trie-router/node.js +147 -158
  115. package/dist/router/trie-router/router.js +23 -19
  116. package/dist/router.js +12 -5
  117. package/dist/types.d.ts +22 -0
  118. package/dist/types.js +0 -0
  119. package/dist/utils/body.js +15 -13
  120. package/dist/utils/buffer.js +35 -29
  121. package/dist/utils/cloudflare.js +32 -32
  122. package/dist/utils/cookie.js +38 -33
  123. package/dist/utils/crypto.js +48 -41
  124. package/dist/utils/encode.js +70 -62
  125. package/dist/utils/filepath.js +18 -20
  126. package/dist/utils/html.js +34 -32
  127. package/dist/utils/http-status.js +48 -44
  128. package/dist/utils/json.js +71 -75
  129. package/dist/utils/jwt/index.js +5 -1
  130. package/dist/utils/jwt/jwt.js +95 -90
  131. package/dist/utils/jwt/types.js +47 -41
  132. package/dist/utils/mime.js +90 -86
  133. package/dist/utils/object.js +31 -32
  134. package/dist/utils/types.d.ts +3 -0
  135. package/dist/utils/types.js +0 -0
  136. package/dist/utils/url.js +69 -77
  137. package/dist/{middleware/validator → validator}/rule.d.ts +0 -0
  138. package/dist/validator/rule.js +57 -0
  139. package/dist/{middleware/validator → validator}/sanitizer.d.ts +0 -0
  140. package/dist/validator/sanitizer.js +7 -0
  141. package/dist/validator/schema.d.ts +7 -0
  142. package/dist/validator/schema.js +0 -0
  143. package/dist/{middleware/validator → validator}/validator.d.ts +10 -7
  144. package/dist/validator/validator.js +406 -0
  145. package/package.json +14 -8
  146. package/dist/cjs/middleware/validator/rule.js +0 -66
  147. package/dist/cjs/middleware/validator/sanitizer.js +0 -6
  148. package/dist/cjs/middleware/validator/validator.js +0 -417
  149. package/dist/middleware/serve-static/module.d.mts +0 -3
  150. package/dist/middleware/serve-static/module.mjs +0 -13
  151. package/dist/middleware/validator/rule.js +0 -63
  152. package/dist/middleware/validator/sanitizer.js +0 -3
  153. package/dist/middleware/validator/validator.js +0 -403
@@ -1,35 +1,34 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- export const isObject = (val) => val && typeof val === 'object' && !Array.isArray(val);
3
- export const mergeObjects = (target, source) => {
4
- const merged = Object.assign({}, target);
5
- if (isObject(target) && isObject(source)) {
6
- for (const key of Object.keys(source)) {
7
- if (isObject(source[key])) {
8
- if (target[key] === undefined)
9
- Object.assign(merged, { [key]: source[key] });
10
- else
11
- merged[key] = mergeObjects(target[key], source[key]);
12
- }
13
- else if (Array.isArray(source[key]) && Array.isArray(target[key])) {
14
- const srcArr = source[key];
15
- const tgtArr = target[key];
16
- const outArr = [];
17
- for (let i = 0; i < srcArr.length; i += 1) {
18
- // If corresponding index for both arrays is an object, then merge them
19
- // Otherwise just copy src arr index into out arr index
20
- if (isObject(srcArr[i]) && isObject(tgtArr[i])) {
21
- outArr[i] = mergeObjects(tgtArr[i], srcArr[i]);
22
- }
23
- else {
24
- outArr[i] = srcArr[i];
25
- }
26
- }
27
- Object.assign(merged, { [key]: outArr });
28
- }
29
- else {
30
- Object.assign(merged, { [key]: source[key] });
31
- }
1
+ // src/utils/object.ts
2
+ var isObject = (val) => val && typeof val === "object" && !Array.isArray(val);
3
+ var mergeObjects = (target, source) => {
4
+ const merged = Object.assign({}, target);
5
+ if (isObject(target) && isObject(source)) {
6
+ for (const key of Object.keys(source)) {
7
+ if (isObject(source[key])) {
8
+ if (target[key] === void 0)
9
+ Object.assign(merged, { [key]: source[key] });
10
+ else
11
+ merged[key] = mergeObjects(target[key], source[key]);
12
+ } else if (Array.isArray(source[key]) && Array.isArray(target[key])) {
13
+ const srcArr = source[key];
14
+ const tgtArr = target[key];
15
+ const outArr = [];
16
+ for (let i = 0; i < srcArr.length; i += 1) {
17
+ if (isObject(srcArr[i]) && isObject(tgtArr[i])) {
18
+ outArr[i] = mergeObjects(tgtArr[i], srcArr[i]);
19
+ } else {
20
+ outArr[i] = srcArr[i];
21
+ }
32
22
  }
23
+ Object.assign(merged, { [key]: outArr });
24
+ } else {
25
+ Object.assign(merged, { [key]: source[key] });
26
+ }
33
27
  }
34
- return merged;
28
+ }
29
+ return merged;
30
+ };
31
+ export {
32
+ isObject,
33
+ mergeObjects
35
34
  };
@@ -0,0 +1,3 @@
1
+ export declare type Expect<T extends true> = T;
2
+ export declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
3
+ export declare type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
File without changes
package/dist/utils/url.js CHANGED
@@ -1,85 +1,77 @@
1
- export const splitPath = (path) => {
2
- const paths = path.split(/\//); // faster than path.split('/')
3
- if (paths[0] === '') {
4
- paths.shift();
5
- }
6
- return paths;
1
+ // src/utils/url.ts
2
+ var splitPath = (path) => {
3
+ const paths = path.split(/\//);
4
+ if (paths[0] === "") {
5
+ paths.shift();
6
+ }
7
+ return paths;
7
8
  };
8
- const patternCache = {};
9
- export const getPattern = (label) => {
10
- // * => wildcard
11
- // :id{[0-9]+} => ([0-9]+)
12
- // :id => (.+)
13
- //const name = ''
14
- if (label === '*') {
15
- return '*';
16
- }
17
- const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
18
- if (match) {
19
- if (!patternCache[label]) {
20
- if (match[2]) {
21
- patternCache[label] = [label, match[1], new RegExp('^' + match[2] + '$')];
22
- }
23
- else {
24
- patternCache[label] = [label, match[1], true];
25
- }
26
- }
27
- return patternCache[label];
9
+ var patternCache = {};
10
+ var getPattern = (label) => {
11
+ if (label === "*") {
12
+ return "*";
13
+ }
14
+ const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
15
+ if (match) {
16
+ if (!patternCache[label]) {
17
+ if (match[2]) {
18
+ patternCache[label] = [label, match[1], new RegExp("^" + match[2] + "$")];
19
+ } else {
20
+ patternCache[label] = [label, match[1], true];
21
+ }
28
22
  }
29
- return null;
23
+ return patternCache[label];
24
+ }
25
+ return null;
30
26
  };
31
- export const getPathFromURL = (url, strict = true) => {
32
- const queryIndex = url.indexOf('?');
33
- const result = url.substring(url.indexOf('/', 8), queryIndex === -1 ? url.length : queryIndex);
34
- // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
35
- // default is true
36
- if (strict === false && result.endsWith('/')) {
37
- return result.slice(0, -1);
38
- }
39
- return result;
27
+ var getPathFromURL = (url, strict = true) => {
28
+ const queryIndex = url.indexOf("?");
29
+ const result = url.substring(url.indexOf("/", 8), queryIndex === -1 ? url.length : queryIndex);
30
+ if (strict === false && result.endsWith("/")) {
31
+ return result.slice(0, -1);
32
+ }
33
+ return result;
40
34
  };
41
- export const getQueryStringFromURL = (url) => {
42
- const queryIndex = url.indexOf('?');
43
- const result = queryIndex !== -1 ? url.substring(queryIndex) : '';
44
- return result;
35
+ var getQueryStringFromURL = (url) => {
36
+ const queryIndex = url.indexOf("?");
37
+ const result = queryIndex !== -1 ? url.substring(queryIndex) : "";
38
+ return result;
45
39
  };
46
- export const mergePath = (...paths) => {
47
- let p = '';
48
- let endsWithSlash = false;
49
- for (let path of paths) {
50
- /* ['/hey/','/say'] => ['/hey', '/say'] */
51
- if (p.endsWith('/')) {
52
- p = p.slice(0, -1);
53
- endsWithSlash = true;
54
- }
55
- /* ['/hey','say'] => ['/hey', '/say'] */
56
- if (!path.startsWith('/')) {
57
- path = `/${path}`;
58
- }
59
- /* ['/hey/', '/'] => `/hey/` */
60
- if (path === '/' && endsWithSlash) {
61
- p = `${p}/`;
62
- }
63
- else if (path !== '/') {
64
- p = `${p}${path}`;
65
- }
66
- /* ['/', '/'] => `/` */
67
- if (path === '/' && p === '') {
68
- p = '/';
69
- }
40
+ var mergePath = (...paths) => {
41
+ let p = "";
42
+ let endsWithSlash = false;
43
+ for (let path of paths) {
44
+ if (p.endsWith("/")) {
45
+ p = p.slice(0, -1);
46
+ endsWithSlash = true;
47
+ }
48
+ if (!path.startsWith("/")) {
49
+ path = `/${path}`;
70
50
  }
71
- return p;
51
+ if (path === "/" && endsWithSlash) {
52
+ p = `${p}/`;
53
+ } else if (path !== "/") {
54
+ p = `${p}${path}`;
55
+ }
56
+ if (path === "/" && p === "") {
57
+ p = "/";
58
+ }
59
+ }
60
+ return p;
61
+ };
62
+ var checkOptionalParameter = (path) => {
63
+ const match = path.match(/(^.+)(\/\:[^\/]+)\?$/);
64
+ if (!match)
65
+ return null;
66
+ const base = match[1];
67
+ const optional = base + match[2];
68
+ return [base, optional];
72
69
  };
73
- export const checkOptionalParameter = (path) => {
74
- /*
75
- If path is `/api/animals/:type?` it will return:
76
- [`/api/animals`, `/api/animals/:type`]
77
- in other cases it will return null
78
- */
79
- const match = path.match(/(^.+)(\/\:[^\/]+)\?$/);
80
- if (!match)
81
- return null;
82
- const base = match[1];
83
- const optional = base + match[2];
84
- return [base, optional];
70
+ export {
71
+ checkOptionalParameter,
72
+ getPathFromURL,
73
+ getPattern,
74
+ getQueryStringFromURL,
75
+ mergePath,
76
+ splitPath
85
77
  };
@@ -0,0 +1,57 @@
1
+ // src/validator/rule.ts
2
+ var rule = {
3
+ isEmpty(value, options = { ignore_whitespace: false }) {
4
+ return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
5
+ },
6
+ isLength: (value, options, arg2) => {
7
+ let min;
8
+ let max;
9
+ if (typeof options === "object") {
10
+ min = options.min || 0;
11
+ max = options.max;
12
+ } else {
13
+ min = options || 0;
14
+ max = arg2;
15
+ }
16
+ const presentationSequences = value.match(/(\uFE0F|\uFE0E)/g) || [];
17
+ const surrogatePairs = value.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
18
+ const len = value.length - presentationSequences.length - surrogatePairs.length;
19
+ return len >= min && (typeof max === "undefined" || len <= max);
20
+ },
21
+ isAlpha: (value) => {
22
+ return /^[A-Z]+$/i.test(value);
23
+ },
24
+ isNumeric: (value) => {
25
+ return /^[0-9]+$/.test(value);
26
+ },
27
+ contains: (value, elem, options = {
28
+ ignoreCase: false,
29
+ minOccurrences: 1
30
+ }) => {
31
+ options.ignoreCase || (options.ignoreCase = false);
32
+ options.minOccurrences || (options.minOccurrences = 1);
33
+ if (options.ignoreCase) {
34
+ return value.toLowerCase().split(elem.toLowerCase()).length > options.minOccurrences;
35
+ }
36
+ return value.split(elem).length > options.minOccurrences;
37
+ },
38
+ isIn: (value, options) => {
39
+ if (typeof options === "object") {
40
+ for (const elem of options) {
41
+ if (elem === value)
42
+ return true;
43
+ }
44
+ }
45
+ return false;
46
+ },
47
+ match: (value, regExp) => {
48
+ return regExp.test(value);
49
+ },
50
+ isGte: (value, min) => min <= value,
51
+ isLte: (value, max) => value <= max,
52
+ isTrue: (value) => value === true,
53
+ isFalse: (value) => value === false
54
+ };
55
+ export {
56
+ rule
57
+ };
@@ -0,0 +1,7 @@
1
+ // src/validator/sanitizer.ts
2
+ var sanitizer = {
3
+ trim: (value) => value.trim()
4
+ };
5
+ export {
6
+ sanitizer
7
+ };
@@ -0,0 +1,7 @@
1
+ import type { VString, VNumber, VBoolean, VObject, VNumberArray, VStringArray, VBooleanArray, VArray, VObjectBase } from './validator';
2
+ export declare type Schema = {
3
+ [key: string]: VString | VNumber | VBoolean | VStringArray | VNumberArray | VBooleanArray | Schema | VObject<Schema> | VArray<Schema>;
4
+ };
5
+ export declare type SchemaToProp<T> = {
6
+ [K in keyof T]: T[K] extends VNumberArray ? number[] : T[K] extends VBooleanArray ? boolean[] : T[K] extends VStringArray ? string[] : T[K] extends VNumber ? number : T[K] extends VBoolean ? boolean : T[K] extends VString ? string : T[K] extends VObjectBase<Schema> ? T[K]['container'] extends VNumber ? number : T[K]['container'] extends VString ? string : T[K]['container'] extends VBoolean ? boolean : T[K] extends VArray<Schema> ? SchemaToProp<ReadonlyArray<T[K]['container']>> : T[K] extends VObject<Schema> ? SchemaToProp<T[K]['container']> : T[K] extends Schema ? SchemaToProp<T[K]> : never : SchemaToProp<T[K]>;
7
+ };
File without changes
@@ -1,6 +1,6 @@
1
- import type { JSONObject, JSONPrimitive, JSONArray } from '../../utils/json';
2
- import type { Schema } from './middleware';
3
- declare type Target = 'query' | 'header' | 'body' | 'json';
1
+ import type { JSONObject, JSONPrimitive, JSONArray } from './../utils/json';
2
+ import type { Schema } from './schema';
3
+ declare type Target = 'query' | 'queries' | 'header' | 'body' | 'json';
4
4
  declare type Type = JSONPrimitive | JSONObject | JSONArray | File;
5
5
  declare type RuleFunc = (value: Type) => boolean;
6
6
  declare type Rule = {
@@ -13,9 +13,9 @@ declare type Sanitizer = (value: Type) => Type;
13
13
  export declare type ValidateResult = {
14
14
  isValid: boolean;
15
15
  message: string | undefined;
16
- target: Target;
17
- key: string;
18
- value: Type;
16
+ target: Target | 'unknown';
17
+ key: string | null;
18
+ value: Type | null;
19
19
  ruleName: string;
20
20
  ruleType: 'type' | 'value';
21
21
  jsonData?: JSONObject;
@@ -38,6 +38,7 @@ export declare class VArray<T extends Schema> extends VObjectBase<T> {
38
38
  export declare class Validator {
39
39
  isArray: boolean;
40
40
  query: (key: string) => VString;
41
+ queries: (key: string) => VStringArray;
41
42
  header: (key: string) => VString;
42
43
  body: (key: string) => VString;
43
44
  json: (key: string) => VString;
@@ -71,7 +72,9 @@ export declare abstract class VBase {
71
72
  asNumber: () => VNumber | VNumberArray;
72
73
  asBoolean: () => VBoolean | VBooleanArray;
73
74
  get(value: string): this;
74
- validate: (req: Request) => Promise<ValidateResult[]>;
75
+ validate: <R extends Request<string, {
76
+ [x: string]: any;
77
+ }>>(req: R) => Promise<ValidateResult[]>;
75
78
  protected getTypeRuleName(): string;
76
79
  private validateRule;
77
80
  private validateType;