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,108 +1,119 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Node = exports.PATH_ERROR = void 0;
4
- const LABEL_REG_EXP_STR = '[^/]+';
5
- const ONLY_WILDCARD_REG_EXP_STR = '.*';
6
- const TAIL_WILDCARD_REG_EXP_STR = '(?:|/.*)';
7
- exports.PATH_ERROR = Symbol();
8
- /**
9
- * Sort order:
10
- * 1. literal
11
- * 2. special pattern (e.g. :label{[0-9]+})
12
- * 3. common label pattern (e.g. :label)
13
- * 4. wildcard
14
- */
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var node_exports = {};
20
+ __export(node_exports, {
21
+ Node: () => Node,
22
+ PATH_ERROR: () => PATH_ERROR
23
+ });
24
+ module.exports = __toCommonJS(node_exports);
25
+ const LABEL_REG_EXP_STR = "[^/]+";
26
+ const ONLY_WILDCARD_REG_EXP_STR = ".*";
27
+ const TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
28
+ const PATH_ERROR = Symbol();
15
29
  function compareKey(a, b) {
16
- if (a.length === 1) {
17
- return b.length === 1 ? (a < b ? -1 : 1) : -1;
18
- }
19
- if (b.length === 1) {
20
- return 1;
21
- }
22
- // wildcard
23
- if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
24
- return 1;
25
- }
26
- else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
27
- return -1;
28
- }
29
- // label
30
- if (a === LABEL_REG_EXP_STR) {
31
- return 1;
32
- }
33
- else if (b === LABEL_REG_EXP_STR) {
34
- return -1;
35
- }
36
- return a.length === b.length ? (a < b ? -1 : 1) : b.length - a.length;
30
+ if (a.length === 1) {
31
+ return b.length === 1 ? a < b ? -1 : 1 : -1;
32
+ }
33
+ if (b.length === 1) {
34
+ return 1;
35
+ }
36
+ if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
37
+ return 1;
38
+ } else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
39
+ return -1;
40
+ }
41
+ if (a === LABEL_REG_EXP_STR) {
42
+ return 1;
43
+ } else if (b === LABEL_REG_EXP_STR) {
44
+ return -1;
45
+ }
46
+ return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
37
47
  }
38
48
  class Node {
39
- constructor() {
40
- this.children = {};
49
+ constructor() {
50
+ this.children = {};
51
+ }
52
+ insert(tokens, index, paramMap, context) {
53
+ if (tokens.length === 0) {
54
+ if (this.index !== void 0) {
55
+ throw PATH_ERROR;
56
+ }
57
+ this.index = index;
58
+ return;
41
59
  }
42
- insert(tokens, index, paramMap, context) {
43
- if (tokens.length === 0) {
44
- if (this.index !== undefined) {
45
- throw exports.PATH_ERROR;
46
- }
47
- this.index = index;
48
- return;
60
+ const [token, ...restTokens] = tokens;
61
+ const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
62
+ let node;
63
+ if (pattern) {
64
+ const name = pattern[1];
65
+ const regexpStr = pattern[2] || LABEL_REG_EXP_STR;
66
+ node = this.children[regexpStr];
67
+ if (!node) {
68
+ if (Object.keys(this.children).some(
69
+ (k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
70
+ )) {
71
+ throw PATH_ERROR;
49
72
  }
50
- const [token, ...restTokens] = tokens;
51
- const pattern = token === '*'
52
- ? restTokens.length === 0
53
- ? ['', '', ONLY_WILDCARD_REG_EXP_STR] // '*' matches to all the trailing paths
54
- : ['', '', LABEL_REG_EXP_STR]
55
- : token === '/*'
56
- ? ['', '', TAIL_WILDCARD_REG_EXP_STR] // '/path/to/*' is /\/path\/to(?:|/.*)$
57
- : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
58
- let node;
59
- if (pattern) {
60
- const name = pattern[1];
61
- const regexpStr = pattern[2] || LABEL_REG_EXP_STR;
62
- node = this.children[regexpStr];
63
- if (!node) {
64
- if (Object.keys(this.children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
65
- throw exports.PATH_ERROR;
66
- }
67
- node = this.children[regexpStr] = new Node();
68
- if (name !== '') {
69
- node.varIndex = context.varIndex++;
70
- }
71
- }
72
- if (name !== '') {
73
- if (paramMap.some((p) => p[0] === name)) {
74
- throw new Error('Duplicate param name');
75
- }
76
- paramMap.push([name, node.varIndex]);
77
- }
73
+ node = this.children[regexpStr] = new Node();
74
+ if (name !== "") {
75
+ node.varIndex = context.varIndex++;
78
76
  }
79
- else {
80
- node = this.children[token];
81
- if (!node) {
82
- if (Object.keys(this.children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
83
- throw exports.PATH_ERROR;
84
- }
85
- node = this.children[token] = new Node();
86
- }
77
+ }
78
+ if (name !== "") {
79
+ if (paramMap.some((p) => p[0] === name)) {
80
+ throw new Error("Duplicate param name");
87
81
  }
88
- node.insert(restTokens, index, paramMap, context);
89
- }
90
- buildRegExpStr() {
91
- const childKeys = Object.keys(this.children).sort(compareKey);
92
- const strList = childKeys.map((k) => {
93
- const c = this.children[k];
94
- return (typeof c.varIndex === 'number' ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr();
95
- });
96
- if (typeof this.index === 'number') {
97
- strList.unshift(`#${this.index}`);
98
- }
99
- if (strList.length === 0) {
100
- return '';
101
- }
102
- if (strList.length === 1) {
103
- return strList[0];
82
+ paramMap.push([name, node.varIndex]);
83
+ }
84
+ } else {
85
+ node = this.children[token];
86
+ if (!node) {
87
+ if (Object.keys(this.children).some(
88
+ (k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
89
+ )) {
90
+ throw PATH_ERROR;
104
91
  }
105
- return '(?:' + strList.join('|') + ')';
92
+ node = this.children[token] = new Node();
93
+ }
94
+ }
95
+ node.insert(restTokens, index, paramMap, context);
96
+ }
97
+ buildRegExpStr() {
98
+ const childKeys = Object.keys(this.children).sort(compareKey);
99
+ const strList = childKeys.map((k) => {
100
+ const c = this.children[k];
101
+ return (typeof c.varIndex === "number" ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr();
102
+ });
103
+ if (typeof this.index === "number") {
104
+ strList.unshift(`#${this.index}`);
105
+ }
106
+ if (strList.length === 0) {
107
+ return "";
108
+ }
109
+ if (strList.length === 1) {
110
+ return strList[0];
106
111
  }
112
+ return "(?:" + strList.join("|") + ")";
113
+ }
107
114
  }
108
- exports.Node = Node;
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ Node,
118
+ PATH_ERROR
119
+ });
@@ -1,161 +1,178 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RegExpRouter = void 0;
4
- const router_1 = require("../../router");
5
- const url_1 = require("../../utils/url");
6
- const node_1 = require("./node");
7
- const trie_1 = require("./trie");
8
- const METHOD_NAMES = [router_1.METHOD_NAME_ALL, ...router_1.METHODS].map((method) => method.toUpperCase());
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var router_exports = {};
20
+ __export(router_exports, {
21
+ RegExpRouter: () => RegExpRouter
22
+ });
23
+ module.exports = __toCommonJS(router_exports);
24
+ var import_router = require("../../router");
25
+ var import_url = require("../../utils/url");
26
+ var import_node = require("./node");
27
+ var import_trie = require("./trie");
28
+ const METHOD_NAMES = [import_router.METHOD_NAME_ALL, ...import_router.METHODS].map((method) => method.toUpperCase());
9
29
  const emptyParam = {};
10
30
  const nullMatcher = [/^$/, []];
11
31
  function buildWildcardRegExp(path) {
12
- return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`);
32
+ return new RegExp(path === "*" ? "" : `^${path.replace(/\/\*/, "(?:|/.*)")}$`);
13
33
  }
14
34
  function buildMatcherFromPreprocessedRoutes(routes) {
15
- const trie = new trie_1.Trie();
16
- const handlers = [];
17
- if (routes.length === 0) {
18
- return nullMatcher;
35
+ const trie = new import_trie.Trie();
36
+ const handlers = [];
37
+ if (routes.length === 0) {
38
+ return nullMatcher;
39
+ }
40
+ for (let i = 0, len = routes.length; i < len; i++) {
41
+ let paramMap;
42
+ try {
43
+ paramMap = trie.insert(routes[i][0], i);
44
+ } catch (e) {
45
+ throw e === import_node.PATH_ERROR ? new import_router.UnsupportedPathError(routes[i][0]) : e;
19
46
  }
20
- for (let i = 0, len = routes.length; i < len; i++) {
21
- let paramMap;
22
- try {
23
- paramMap = trie.insert(routes[i][0], i);
24
- }
25
- catch (e) {
26
- throw e === node_1.PATH_ERROR ? new router_1.UnsupportedPathError(routes[i][0]) : e;
27
- }
28
- handlers[i] = [routes[i][1], paramMap.length !== 0 ? paramMap : null];
29
- }
30
- const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
31
- for (let i = 0, len = handlers.length; i < len; i++) {
32
- const paramMap = handlers[i][1];
33
- if (paramMap) {
34
- for (let j = 0, len = paramMap.length; j < len; j++) {
35
- paramMap[j][1] = paramReplacementMap[paramMap[j][1]];
36
- }
37
- }
47
+ handlers[i] = [routes[i][1], paramMap.length !== 0 ? paramMap : null];
48
+ }
49
+ const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
50
+ for (let i = 0, len = handlers.length; i < len; i++) {
51
+ const paramMap = handlers[i][1];
52
+ if (paramMap) {
53
+ for (let j = 0, len2 = paramMap.length; j < len2; j++) {
54
+ paramMap[j][1] = paramReplacementMap[paramMap[j][1]];
55
+ }
38
56
  }
39
- const handlerMap = [];
40
- // using `in` because indexReplacementMap is a sparse array
41
- for (const i in indexReplacementMap) {
42
- handlerMap[i] = handlers[indexReplacementMap[i]];
43
- }
44
- return [regexp, handlerMap];
57
+ }
58
+ const handlerMap = [];
59
+ for (const i in indexReplacementMap) {
60
+ handlerMap[i] = handlers[indexReplacementMap[i]];
61
+ }
62
+ return [regexp, handlerMap];
45
63
  }
46
64
  function findMiddleware(middleware, path) {
47
- if (!middleware) {
48
- return undefined;
49
- }
50
- for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
51
- if (buildWildcardRegExp(k).test(path)) {
52
- return [...middleware[k]];
53
- }
65
+ if (!middleware) {
66
+ return void 0;
67
+ }
68
+ for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
69
+ if (buildWildcardRegExp(k).test(path)) {
70
+ return [...middleware[k]];
54
71
  }
55
- return undefined;
72
+ }
73
+ return void 0;
56
74
  }
57
75
  class RegExpRouter {
58
- constructor() {
59
- this.middleware = { [router_1.METHOD_NAME_ALL]: {} };
60
- this.routes = { [router_1.METHOD_NAME_ALL]: {} };
76
+ constructor() {
77
+ this.middleware = { [import_router.METHOD_NAME_ALL]: {} };
78
+ this.routes = { [import_router.METHOD_NAME_ALL]: {} };
79
+ }
80
+ add(method, path, handler) {
81
+ var _a;
82
+ const { middleware, routes } = this;
83
+ if (!middleware || !routes) {
84
+ throw new Error("Can not add a route since the matcher is already built.");
61
85
  }
62
- add(method, path, handler) {
63
- var _a;
64
- const { middleware, routes } = this;
65
- if (!middleware || !routes) {
66
- throw new Error('Can not add a route since the matcher is already built.');
67
- }
68
- if (path === '/*') {
69
- path = '*';
70
- }
71
- if (/\*$/.test(path)) {
72
- middleware[method] || (middleware[method] = {});
73
- const re = buildWildcardRegExp(path);
74
- (_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[router_1.METHOD_NAME_ALL], path) || []);
75
- Object.keys(middleware).forEach((m) => {
76
- if (method === router_1.METHOD_NAME_ALL || method === m) {
77
- Object.keys(middleware[m]).forEach((p) => {
78
- ;
79
- (path === '*' || path === p) && middleware[m][p].push(handler);
80
- });
81
- }
82
- });
83
- Object.keys(routes).forEach((m) => {
84
- if (method === router_1.METHOD_NAME_ALL || method === m) {
85
- Object.keys(routes[m]).forEach((p) => (path === '*' || re.test(p)) && routes[m][p].push(handler));
86
- }
87
- });
88
- return;
86
+ if (path === "/*") {
87
+ path = "*";
88
+ }
89
+ if (/\*$/.test(path)) {
90
+ middleware[method] || (middleware[method] = {});
91
+ const re = buildWildcardRegExp(path);
92
+ (_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || []);
93
+ Object.keys(middleware).forEach((m) => {
94
+ if (method === import_router.METHOD_NAME_ALL || method === m) {
95
+ Object.keys(middleware[m]).forEach((p) => {
96
+ ;
97
+ (path === "*" || path === p) && middleware[m][p].push(handler);
98
+ });
89
99
  }
90
- const paths = (0, url_1.checkOptionalParameter)(path) || [path];
91
- for (let i = 0, len = paths.length; i < len; i++) {
92
- const path = paths[i];
93
- routes[method] || (routes[method] = {});
94
- Object.keys(routes).forEach((m) => {
95
- var _a;
96
- if (method === router_1.METHOD_NAME_ALL || method === m) {
97
- (_a = routes[m])[path] || (_a[path] = [
98
- ...(routes[router_1.METHOD_NAME_ALL][path] ||
99
- findMiddleware(middleware[method], path) ||
100
- findMiddleware(middleware[router_1.METHOD_NAME_ALL], path) ||
101
- []),
102
- ]);
103
- routes[m][path].push(handler);
104
- }
105
- });
100
+ });
101
+ Object.keys(routes).forEach((m) => {
102
+ if (method === import_router.METHOD_NAME_ALL || method === m) {
103
+ Object.keys(routes[m]).forEach(
104
+ (p) => (path === "*" || re.test(p)) && routes[m][p].push(handler)
105
+ );
106
106
  }
107
+ });
108
+ return;
107
109
  }
108
- match(method, path) {
109
- const matchers = this.buildAllMatchers();
110
- this.match = (method, path) => {
111
- const matcher = matchers[method];
112
- const match = path.match(matcher[0]);
113
- if (!match) {
114
- return null;
115
- }
116
- const index = match.indexOf('', 1);
117
- const [handlers, paramMap] = matcher[1][index];
118
- if (!paramMap) {
119
- return { handlers, params: emptyParam };
120
- }
121
- const params = {};
122
- for (let i = 0, len = paramMap.length; i < len; i++) {
123
- params[paramMap[i][0]] = match[paramMap[i][1]];
124
- }
125
- return { handlers, params };
126
- };
127
- return this.match(method, path);
128
- }
129
- buildAllMatchers() {
130
- const matchers = {};
131
- METHOD_NAMES.forEach((method) => {
132
- matchers[method] = this.buildMatcher(method) || matchers[router_1.METHOD_NAME_ALL];
133
- });
134
- // Release cache
135
- this.middleware = this.routes = undefined;
136
- return matchers;
137
- }
138
- buildMatcher(method) {
139
- const routes = [];
140
- let hasOwnRoute = method === router_1.METHOD_NAME_ALL;
141
- [this.middleware, this.routes].forEach((r) => {
142
- const ownRoute = r[method]
143
- ? Object.keys(r[method]).map((path) => [path, r[method][path]])
144
- : [];
145
- if (ownRoute.length !== 0) {
146
- hasOwnRoute || (hasOwnRoute = true);
147
- routes.push(...ownRoute);
148
- }
149
- else if (method !== router_1.METHOD_NAME_ALL) {
150
- routes.push(...Object.keys(r[router_1.METHOD_NAME_ALL]).map((path) => [path, r[router_1.METHOD_NAME_ALL][path]]));
151
- }
152
- });
153
- if (!hasOwnRoute) {
154
- return null;
155
- }
156
- else {
157
- return buildMatcherFromPreprocessedRoutes(routes);
110
+ const paths = (0, import_url.checkOptionalParameter)(path) || [path];
111
+ for (let i = 0, len = paths.length; i < len; i++) {
112
+ const path2 = paths[i];
113
+ routes[method] || (routes[method] = {});
114
+ Object.keys(routes).forEach((m) => {
115
+ var _a2;
116
+ if (method === import_router.METHOD_NAME_ALL || method === m) {
117
+ (_a2 = routes[m])[path2] || (_a2[path2] = [
118
+ ...routes[import_router.METHOD_NAME_ALL][path2] || findMiddleware(middleware[method], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || []
119
+ ]);
120
+ routes[m][path2].push(handler);
158
121
  }
122
+ });
123
+ }
124
+ }
125
+ match(method, path) {
126
+ const matchers = this.buildAllMatchers();
127
+ this.match = (method2, path2) => {
128
+ const matcher = matchers[method2];
129
+ const match = path2.match(matcher[0]);
130
+ if (!match) {
131
+ return null;
132
+ }
133
+ const index = match.indexOf("", 1);
134
+ const [handlers, paramMap] = matcher[1][index];
135
+ if (!paramMap) {
136
+ return { handlers, params: emptyParam };
137
+ }
138
+ const params = {};
139
+ for (let i = 0, len = paramMap.length; i < len; i++) {
140
+ params[paramMap[i][0]] = match[paramMap[i][1]];
141
+ }
142
+ return { handlers, params };
143
+ };
144
+ return this.match(method, path);
145
+ }
146
+ buildAllMatchers() {
147
+ const matchers = {};
148
+ METHOD_NAMES.forEach((method) => {
149
+ matchers[method] = this.buildMatcher(method) || matchers[import_router.METHOD_NAME_ALL];
150
+ });
151
+ this.middleware = this.routes = void 0;
152
+ return matchers;
153
+ }
154
+ buildMatcher(method) {
155
+ const routes = [];
156
+ let hasOwnRoute = method === import_router.METHOD_NAME_ALL;
157
+ [this.middleware, this.routes].forEach((r) => {
158
+ const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
159
+ if (ownRoute.length !== 0) {
160
+ hasOwnRoute || (hasOwnRoute = true);
161
+ routes.push(...ownRoute);
162
+ } else if (method !== import_router.METHOD_NAME_ALL) {
163
+ routes.push(
164
+ ...Object.keys(r[import_router.METHOD_NAME_ALL]).map((path) => [path, r[import_router.METHOD_NAME_ALL][path]])
165
+ );
166
+ }
167
+ });
168
+ if (!hasOwnRoute) {
169
+ return null;
170
+ } else {
171
+ return buildMatcherFromPreprocessedRoutes(routes);
159
172
  }
173
+ }
160
174
  }
161
- exports.RegExpRouter = RegExpRouter;
175
+ // Annotate the CommonJS export names for ESM import in node:
176
+ 0 && (module.exports = {
177
+ RegExpRouter
178
+ });
@@ -1,42 +1,58 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Trie = void 0;
4
- const node_1 = require("./node");
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var trie_exports = {};
20
+ __export(trie_exports, {
21
+ Trie: () => Trie
22
+ });
23
+ module.exports = __toCommonJS(trie_exports);
24
+ var import_node = require("./node");
5
25
  class Trie {
6
- constructor() {
7
- this.context = { varIndex: 0 };
8
- this.root = new node_1.Node();
9
- }
10
- insert(path, index) {
11
- const paramMap = [];
12
- /**
13
- * - pattern (:label, :label{0-9]+}, ...)
14
- * - /* wildcard
15
- * - character
16
- */
17
- const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g);
18
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
19
- // @ts-ignore
20
- this.root.insert(tokens, index, paramMap, this.context);
21
- return paramMap;
22
- }
23
- buildRegExp() {
24
- let regexp = this.root.buildRegExpStr();
25
- let captureIndex = 0;
26
- const indexReplacementMap = [];
27
- const paramReplacementMap = [];
28
- regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
29
- if (typeof handlerIndex !== 'undefined') {
30
- indexReplacementMap[++captureIndex] = Number(handlerIndex);
31
- return '$()';
32
- }
33
- if (typeof paramIndex !== 'undefined') {
34
- paramReplacementMap[Number(paramIndex)] = ++captureIndex;
35
- return '';
36
- }
37
- return '';
38
- });
39
- return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
40
- }
26
+ constructor() {
27
+ this.context = { varIndex: 0 };
28
+ this.root = new import_node.Node();
29
+ }
30
+ insert(path, index) {
31
+ const paramMap = [];
32
+ const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g);
33
+ this.root.insert(tokens, index, paramMap, this.context);
34
+ return paramMap;
35
+ }
36
+ buildRegExp() {
37
+ let regexp = this.root.buildRegExpStr();
38
+ let captureIndex = 0;
39
+ const indexReplacementMap = [];
40
+ const paramReplacementMap = [];
41
+ regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
42
+ if (typeof handlerIndex !== "undefined") {
43
+ indexReplacementMap[++captureIndex] = Number(handlerIndex);
44
+ return "$()";
45
+ }
46
+ if (typeof paramIndex !== "undefined") {
47
+ paramReplacementMap[Number(paramIndex)] = ++captureIndex;
48
+ return "";
49
+ }
50
+ return "";
51
+ });
52
+ return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
53
+ }
41
54
  }
42
- exports.Trie = Trie;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ Trie
58
+ });