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,157 +1,155 @@
1
- import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router';
2
- import { checkOptionalParameter } from '../../utils/url';
3
- import { PATH_ERROR } from './node';
4
- import { Trie } from './trie';
5
- const METHOD_NAMES = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase());
6
- const emptyParam = {};
7
- const nullMatcher = [/^$/, []];
1
+ // src/router/reg-exp-router/router.ts
2
+ import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from "../../router.js";
3
+ import { checkOptionalParameter } from "../../utils/url.js";
4
+ import { PATH_ERROR } from "./node.js";
5
+ import { Trie } from "./trie.js";
6
+ var METHOD_NAMES = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase());
7
+ var emptyParam = {};
8
+ var nullMatcher = [/^$/, []];
8
9
  function buildWildcardRegExp(path) {
9
- return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`);
10
+ return new RegExp(path === "*" ? "" : `^${path.replace(/\/\*/, "(?:|/.*)")}$`);
10
11
  }
11
12
  function buildMatcherFromPreprocessedRoutes(routes) {
12
- const trie = new Trie();
13
- const handlers = [];
14
- if (routes.length === 0) {
15
- return nullMatcher;
13
+ const trie = new Trie();
14
+ const handlers = [];
15
+ if (routes.length === 0) {
16
+ return nullMatcher;
17
+ }
18
+ for (let i = 0, len = routes.length; i < len; i++) {
19
+ let paramMap;
20
+ try {
21
+ paramMap = trie.insert(routes[i][0], i);
22
+ } catch (e) {
23
+ throw e === PATH_ERROR ? new UnsupportedPathError(routes[i][0]) : e;
16
24
  }
17
- for (let i = 0, len = routes.length; i < len; i++) {
18
- let paramMap;
19
- try {
20
- paramMap = trie.insert(routes[i][0], i);
21
- }
22
- catch (e) {
23
- throw e === PATH_ERROR ? new UnsupportedPathError(routes[i][0]) : e;
24
- }
25
- handlers[i] = [routes[i][1], paramMap.length !== 0 ? paramMap : null];
26
- }
27
- const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
28
- for (let i = 0, len = handlers.length; i < len; i++) {
29
- const paramMap = handlers[i][1];
30
- if (paramMap) {
31
- for (let j = 0, len = paramMap.length; j < len; j++) {
32
- paramMap[j][1] = paramReplacementMap[paramMap[j][1]];
33
- }
34
- }
25
+ handlers[i] = [routes[i][1], paramMap.length !== 0 ? paramMap : null];
26
+ }
27
+ const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
28
+ for (let i = 0, len = handlers.length; i < len; i++) {
29
+ const paramMap = handlers[i][1];
30
+ if (paramMap) {
31
+ for (let j = 0, len2 = paramMap.length; j < len2; j++) {
32
+ paramMap[j][1] = paramReplacementMap[paramMap[j][1]];
33
+ }
35
34
  }
36
- const handlerMap = [];
37
- // using `in` because indexReplacementMap is a sparse array
38
- for (const i in indexReplacementMap) {
39
- handlerMap[i] = handlers[indexReplacementMap[i]];
40
- }
41
- return [regexp, handlerMap];
35
+ }
36
+ const handlerMap = [];
37
+ for (const i in indexReplacementMap) {
38
+ handlerMap[i] = handlers[indexReplacementMap[i]];
39
+ }
40
+ return [regexp, handlerMap];
42
41
  }
43
42
  function findMiddleware(middleware, path) {
44
- if (!middleware) {
45
- return undefined;
46
- }
47
- for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
48
- if (buildWildcardRegExp(k).test(path)) {
49
- return [...middleware[k]];
50
- }
43
+ if (!middleware) {
44
+ return void 0;
45
+ }
46
+ for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
47
+ if (buildWildcardRegExp(k).test(path)) {
48
+ return [...middleware[k]];
51
49
  }
52
- return undefined;
50
+ }
51
+ return void 0;
53
52
  }
54
- export class RegExpRouter {
55
- constructor() {
56
- this.middleware = { [METHOD_NAME_ALL]: {} };
57
- this.routes = { [METHOD_NAME_ALL]: {} };
53
+ var RegExpRouter = class {
54
+ constructor() {
55
+ this.middleware = { [METHOD_NAME_ALL]: {} };
56
+ this.routes = { [METHOD_NAME_ALL]: {} };
57
+ }
58
+ add(method, path, handler) {
59
+ var _a;
60
+ const { middleware, routes } = this;
61
+ if (!middleware || !routes) {
62
+ throw new Error("Can not add a route since the matcher is already built.");
58
63
  }
59
- add(method, path, handler) {
60
- var _a;
61
- const { middleware, routes } = this;
62
- if (!middleware || !routes) {
63
- throw new Error('Can not add a route since the matcher is already built.');
64
- }
65
- if (path === '/*') {
66
- path = '*';
67
- }
68
- if (/\*$/.test(path)) {
69
- middleware[method] || (middleware[method] = {});
70
- const re = buildWildcardRegExp(path);
71
- (_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
72
- Object.keys(middleware).forEach((m) => {
73
- if (method === METHOD_NAME_ALL || method === m) {
74
- Object.keys(middleware[m]).forEach((p) => {
75
- ;
76
- (path === '*' || path === p) && middleware[m][p].push(handler);
77
- });
78
- }
79
- });
80
- Object.keys(routes).forEach((m) => {
81
- if (method === METHOD_NAME_ALL || method === m) {
82
- Object.keys(routes[m]).forEach((p) => (path === '*' || re.test(p)) && routes[m][p].push(handler));
83
- }
84
- });
85
- return;
64
+ if (path === "/*") {
65
+ path = "*";
66
+ }
67
+ if (/\*$/.test(path)) {
68
+ middleware[method] || (middleware[method] = {});
69
+ const re = buildWildcardRegExp(path);
70
+ (_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
71
+ Object.keys(middleware).forEach((m) => {
72
+ if (method === METHOD_NAME_ALL || method === m) {
73
+ Object.keys(middleware[m]).forEach((p) => {
74
+ ;
75
+ (path === "*" || path === p) && middleware[m][p].push(handler);
76
+ });
86
77
  }
87
- const paths = checkOptionalParameter(path) || [path];
88
- for (let i = 0, len = paths.length; i < len; i++) {
89
- const path = paths[i];
90
- routes[method] || (routes[method] = {});
91
- Object.keys(routes).forEach((m) => {
92
- var _a;
93
- if (method === METHOD_NAME_ALL || method === m) {
94
- (_a = routes[m])[path] || (_a[path] = [
95
- ...(routes[METHOD_NAME_ALL][path] ||
96
- findMiddleware(middleware[method], path) ||
97
- findMiddleware(middleware[METHOD_NAME_ALL], path) ||
98
- []),
99
- ]);
100
- routes[m][path].push(handler);
101
- }
102
- });
78
+ });
79
+ Object.keys(routes).forEach((m) => {
80
+ if (method === METHOD_NAME_ALL || method === m) {
81
+ Object.keys(routes[m]).forEach(
82
+ (p) => (path === "*" || re.test(p)) && routes[m][p].push(handler)
83
+ );
103
84
  }
85
+ });
86
+ return;
104
87
  }
105
- match(method, path) {
106
- const matchers = this.buildAllMatchers();
107
- this.match = (method, path) => {
108
- const matcher = matchers[method];
109
- const match = path.match(matcher[0]);
110
- if (!match) {
111
- return null;
112
- }
113
- const index = match.indexOf('', 1);
114
- const [handlers, paramMap] = matcher[1][index];
115
- if (!paramMap) {
116
- return { handlers, params: emptyParam };
117
- }
118
- const params = {};
119
- for (let i = 0, len = paramMap.length; i < len; i++) {
120
- params[paramMap[i][0]] = match[paramMap[i][1]];
121
- }
122
- return { handlers, params };
123
- };
124
- return this.match(method, path);
125
- }
126
- buildAllMatchers() {
127
- const matchers = {};
128
- METHOD_NAMES.forEach((method) => {
129
- matchers[method] = this.buildMatcher(method) || matchers[METHOD_NAME_ALL];
130
- });
131
- // Release cache
132
- this.middleware = this.routes = undefined;
133
- return matchers;
134
- }
135
- buildMatcher(method) {
136
- const routes = [];
137
- let hasOwnRoute = method === METHOD_NAME_ALL;
138
- [this.middleware, this.routes].forEach((r) => {
139
- const ownRoute = r[method]
140
- ? Object.keys(r[method]).map((path) => [path, r[method][path]])
141
- : [];
142
- if (ownRoute.length !== 0) {
143
- hasOwnRoute || (hasOwnRoute = true);
144
- routes.push(...ownRoute);
145
- }
146
- else if (method !== METHOD_NAME_ALL) {
147
- routes.push(...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]]));
148
- }
149
- });
150
- if (!hasOwnRoute) {
151
- return null;
152
- }
153
- else {
154
- return buildMatcherFromPreprocessedRoutes(routes);
88
+ const paths = checkOptionalParameter(path) || [path];
89
+ for (let i = 0, len = paths.length; i < len; i++) {
90
+ const path2 = paths[i];
91
+ routes[method] || (routes[method] = {});
92
+ Object.keys(routes).forEach((m) => {
93
+ var _a2;
94
+ if (method === METHOD_NAME_ALL || method === m) {
95
+ (_a2 = routes[m])[path2] || (_a2[path2] = [
96
+ ...routes[METHOD_NAME_ALL][path2] || findMiddleware(middleware[method], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
97
+ ]);
98
+ routes[m][path2].push(handler);
155
99
  }
100
+ });
156
101
  }
157
- }
102
+ }
103
+ match(method, path) {
104
+ const matchers = this.buildAllMatchers();
105
+ this.match = (method2, path2) => {
106
+ const matcher = matchers[method2];
107
+ const match = path2.match(matcher[0]);
108
+ if (!match) {
109
+ return null;
110
+ }
111
+ const index = match.indexOf("", 1);
112
+ const [handlers, paramMap] = matcher[1][index];
113
+ if (!paramMap) {
114
+ return { handlers, params: emptyParam };
115
+ }
116
+ const params = {};
117
+ for (let i = 0, len = paramMap.length; i < len; i++) {
118
+ params[paramMap[i][0]] = match[paramMap[i][1]];
119
+ }
120
+ return { handlers, params };
121
+ };
122
+ return this.match(method, path);
123
+ }
124
+ buildAllMatchers() {
125
+ const matchers = {};
126
+ METHOD_NAMES.forEach((method) => {
127
+ matchers[method] = this.buildMatcher(method) || matchers[METHOD_NAME_ALL];
128
+ });
129
+ this.middleware = this.routes = void 0;
130
+ return matchers;
131
+ }
132
+ buildMatcher(method) {
133
+ const routes = [];
134
+ let hasOwnRoute = method === METHOD_NAME_ALL;
135
+ [this.middleware, this.routes].forEach((r) => {
136
+ const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
137
+ if (ownRoute.length !== 0) {
138
+ hasOwnRoute || (hasOwnRoute = true);
139
+ routes.push(...ownRoute);
140
+ } else if (method !== METHOD_NAME_ALL) {
141
+ routes.push(
142
+ ...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]])
143
+ );
144
+ }
145
+ });
146
+ if (!hasOwnRoute) {
147
+ return null;
148
+ } else {
149
+ return buildMatcherFromPreprocessedRoutes(routes);
150
+ }
151
+ }
152
+ };
153
+ export {
154
+ RegExpRouter
155
+ };
@@ -1,38 +1,35 @@
1
- import { Node } from './node';
2
- export class Trie {
3
- constructor() {
4
- this.context = { varIndex: 0 };
5
- this.root = new Node();
6
- }
7
- insert(path, index) {
8
- const paramMap = [];
9
- /**
10
- * - pattern (:label, :label{0-9]+}, ...)
11
- * - /* wildcard
12
- * - character
13
- */
14
- const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g);
15
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
16
- // @ts-ignore
17
- this.root.insert(tokens, index, paramMap, this.context);
18
- return paramMap;
19
- }
20
- buildRegExp() {
21
- let regexp = this.root.buildRegExpStr();
22
- let captureIndex = 0;
23
- const indexReplacementMap = [];
24
- const paramReplacementMap = [];
25
- regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
26
- if (typeof handlerIndex !== 'undefined') {
27
- indexReplacementMap[++captureIndex] = Number(handlerIndex);
28
- return '$()';
29
- }
30
- if (typeof paramIndex !== 'undefined') {
31
- paramReplacementMap[Number(paramIndex)] = ++captureIndex;
32
- return '';
33
- }
34
- return '';
35
- });
36
- return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
37
- }
38
- }
1
+ // src/router/reg-exp-router/trie.ts
2
+ import { Node } from "./node.js";
3
+ var Trie = class {
4
+ constructor() {
5
+ this.context = { varIndex: 0 };
6
+ this.root = new Node();
7
+ }
8
+ insert(path, index) {
9
+ const paramMap = [];
10
+ const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g);
11
+ this.root.insert(tokens, index, paramMap, this.context);
12
+ return paramMap;
13
+ }
14
+ buildRegExp() {
15
+ let regexp = this.root.buildRegExpStr();
16
+ let captureIndex = 0;
17
+ const indexReplacementMap = [];
18
+ const paramReplacementMap = [];
19
+ regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
20
+ if (typeof handlerIndex !== "undefined") {
21
+ indexReplacementMap[++captureIndex] = Number(handlerIndex);
22
+ return "$()";
23
+ }
24
+ if (typeof paramIndex !== "undefined") {
25
+ paramReplacementMap[Number(paramIndex)] = ++captureIndex;
26
+ return "";
27
+ }
28
+ return "";
29
+ });
30
+ return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
31
+ }
32
+ };
33
+ export {
34
+ Trie
35
+ };
@@ -1 +1,5 @@
1
- export { SmartRouter } from './router';
1
+ // src/router/smart-router/index.ts
2
+ import { SmartRouter } from "./router.js";
3
+ export {
4
+ SmartRouter
5
+ };
@@ -1,53 +1,55 @@
1
- import { UnsupportedPathError } from '../../router';
2
- export class SmartRouter {
3
- constructor(init) {
4
- this.routers = [];
5
- this.routes = [];
6
- Object.assign(this, init);
1
+ // src/router/smart-router/router.ts
2
+ import { UnsupportedPathError } from "../../router.js";
3
+ var SmartRouter = class {
4
+ constructor(init) {
5
+ this.routers = [];
6
+ this.routes = [];
7
+ Object.assign(this, init);
8
+ }
9
+ add(method, path, handler) {
10
+ if (!this.routes) {
11
+ throw new Error("Can not add a route since the matcher is already built.");
7
12
  }
8
- add(method, path, handler) {
9
- if (!this.routes) {
10
- throw new Error('Can not add a route since the matcher is already built.');
11
- }
12
- this.routes.push([method, path, handler]);
13
+ this.routes.push([method, path, handler]);
14
+ }
15
+ match(method, path) {
16
+ if (!this.routes) {
17
+ throw new Error("Fatal error");
13
18
  }
14
- match(method, path) {
15
- if (!this.routes) {
16
- throw new Error('Fatal error');
17
- }
18
- const { routers, routes } = this;
19
- const len = routers.length;
20
- let i = 0;
21
- let res;
22
- for (; i < len; i++) {
23
- const router = routers[i];
24
- try {
25
- routes.forEach((args) => {
26
- router.add(...args);
27
- });
28
- res = router.match(method, path);
29
- }
30
- catch (e) {
31
- if (e instanceof UnsupportedPathError) {
32
- continue;
33
- }
34
- throw e;
35
- }
36
- this.match = router.match.bind(router);
37
- this.routers = [router];
38
- this.routes = undefined;
39
- break;
40
- }
41
- if (i === len) {
42
- // not found
43
- throw new Error('Fatal error');
19
+ const { routers, routes } = this;
20
+ const len = routers.length;
21
+ let i = 0;
22
+ let res;
23
+ for (; i < len; i++) {
24
+ const router = routers[i];
25
+ try {
26
+ routes.forEach((args) => {
27
+ router.add(...args);
28
+ });
29
+ res = router.match(method, path);
30
+ } catch (e) {
31
+ if (e instanceof UnsupportedPathError) {
32
+ continue;
44
33
  }
45
- return res || null;
34
+ throw e;
35
+ }
36
+ this.match = router.match.bind(router);
37
+ this.routers = [router];
38
+ this.routes = void 0;
39
+ break;
46
40
  }
47
- get activeRouter() {
48
- if (this.routes || this.routers.length !== 1) {
49
- throw new Error('No active router has been determined yet.');
50
- }
51
- return this.routers[0];
41
+ if (i === len) {
42
+ throw new Error("Fatal error");
43
+ }
44
+ return res || null;
45
+ }
46
+ get activeRouter() {
47
+ if (this.routes || this.routers.length !== 1) {
48
+ throw new Error("No active router has been determined yet.");
52
49
  }
53
- }
50
+ return this.routers[0];
51
+ }
52
+ };
53
+ export {
54
+ SmartRouter
55
+ };
@@ -1 +1,5 @@
1
- export { StaticRouter } from './router';
1
+ // src/router/static-router/index.ts
2
+ import { StaticRouter } from "./router.js";
3
+ export {
4
+ StaticRouter
5
+ };
@@ -1,68 +1,63 @@
1
- import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router';
2
- export class StaticRouter {
3
- constructor() {
4
- this.middleware = {};
5
- this.routes = {};
6
- [METHOD_NAME_ALL, ...METHODS].forEach((method) => {
7
- this.routes[method.toUpperCase()] = {};
8
- });
1
+ // src/router/static-router/router.ts
2
+ import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from "../../router.js";
3
+ var StaticRouter = class {
4
+ constructor() {
5
+ this.middleware = {};
6
+ this.routes = {};
7
+ [METHOD_NAME_ALL, ...METHODS].forEach((method) => {
8
+ this.routes[method.toUpperCase()] = {};
9
+ });
10
+ }
11
+ add(method, path, handler) {
12
+ var _a, _b;
13
+ const { middleware, routes } = this;
14
+ if (path === "/*") {
15
+ path = "*";
9
16
  }
10
- add(method, path, handler) {
11
- var _a;
12
- const { middleware, routes } = this;
13
- if (path === '/*') {
14
- path = '*';
15
- }
16
- if (path === '*') {
17
- if (method === METHOD_NAME_ALL) {
18
- middleware[METHOD_NAME_ALL] || (middleware[METHOD_NAME_ALL] = { handlers: [], params: {} });
19
- Object.keys(middleware).forEach((m) => {
20
- middleware[m].handlers.push(handler);
21
- });
22
- Object.keys(routes).forEach((m) => {
23
- Object.values(routes[m]).forEach((matchRes) => matchRes.handlers.push(handler));
24
- });
25
- }
26
- else {
27
- middleware[method] || (middleware[method] = {
28
- handlers: [...(middleware[METHOD_NAME_ALL]?.handlers || [])],
29
- params: {},
30
- });
31
- middleware[method].handlers.push(handler);
32
- if (routes[method]) {
33
- Object.values(routes[method]).forEach((matchRes) => matchRes.handlers.push(handler));
34
- }
35
- }
36
- return;
37
- }
38
- if (/\*|\/:/.test(path)) {
39
- throw new UnsupportedPathError(path);
40
- }
41
- (_a = routes[method])[path] || (_a[path] = {
42
- handlers: [
43
- ...(routes[METHOD_NAME_ALL][path]?.handlers ||
44
- middleware[method]?.handlers ||
45
- middleware[METHOD_NAME_ALL]?.handlers ||
46
- []),
47
- ],
48
- params: {},
17
+ if (path === "*") {
18
+ if (method === METHOD_NAME_ALL) {
19
+ middleware[_a = METHOD_NAME_ALL] || (middleware[_a] = { handlers: [], params: {} });
20
+ Object.keys(middleware).forEach((m) => {
21
+ middleware[m].handlers.push(handler);
49
22
  });
50
- if (method === METHOD_NAME_ALL) {
51
- Object.keys(routes).forEach((m) => {
52
- routes[m][path]?.handlers?.push(handler);
53
- });
54
- }
55
- else {
56
- routes[method][path].handlers.push(handler);
23
+ Object.keys(routes).forEach((m) => {
24
+ Object.values(routes[m]).forEach((matchRes) => matchRes.handlers.push(handler));
25
+ });
26
+ } else {
27
+ middleware[method] || (middleware[method] = {
28
+ handlers: [...middleware[METHOD_NAME_ALL]?.handlers || []],
29
+ params: {}
30
+ });
31
+ middleware[method].handlers.push(handler);
32
+ if (routes[method]) {
33
+ Object.values(routes[method]).forEach((matchRes) => matchRes.handlers.push(handler));
57
34
  }
35
+ }
36
+ return;
37
+ }
38
+ if (/\*|\/:/.test(path)) {
39
+ throw new UnsupportedPathError(path);
58
40
  }
59
- match(method, path) {
60
- const { routes, middleware } = this;
61
- this.match = (method, path) => routes[method][path] ||
62
- routes[METHOD_NAME_ALL][path] ||
63
- middleware[method] ||
64
- middleware[METHOD_NAME_ALL] ||
65
- null;
66
- return this.match(method, path);
41
+ (_b = routes[method])[path] || (_b[path] = {
42
+ handlers: [
43
+ ...routes[METHOD_NAME_ALL][path]?.handlers || middleware[method]?.handlers || middleware[METHOD_NAME_ALL]?.handlers || []
44
+ ],
45
+ params: {}
46
+ });
47
+ if (method === METHOD_NAME_ALL) {
48
+ Object.keys(routes).forEach((m) => {
49
+ routes[m][path]?.handlers?.push(handler);
50
+ });
51
+ } else {
52
+ routes[method][path].handlers.push(handler);
67
53
  }
68
- }
54
+ }
55
+ match(method, path) {
56
+ const { routes, middleware } = this;
57
+ this.match = (method2, path2) => routes[method2][path2] || routes[METHOD_NAME_ALL][path2] || middleware[method2] || middleware[METHOD_NAME_ALL] || null;
58
+ return this.match(method, path);
59
+ }
60
+ };
61
+ export {
62
+ StaticRouter
63
+ };
@@ -1 +1,5 @@
1
- export { TrieRouter } from './router';
1
+ // src/router/trie-router/index.ts
2
+ import { TrieRouter } from "./router.js";
3
+ export {
4
+ TrieRouter
5
+ };