hono 4.13.3 → 4.13.5

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 (40) hide show
  1. package/dist/cjs/client/client.js +25 -11
  2. package/dist/cjs/client/utils.js +3 -0
  3. package/dist/cjs/helper/accepts/accepts.js +36 -2
  4. package/dist/cjs/helper/ssg/ssg.js +1 -1
  5. package/dist/cjs/helper/ssg/utils.js +30 -10
  6. package/dist/cjs/jsx/dom/render.js +2 -0
  7. package/dist/cjs/middleware/cache/index.js +1 -1
  8. package/dist/cjs/middleware/etag/index.js +1 -1
  9. package/dist/cjs/request.js +8 -4
  10. package/dist/cjs/router/reg-exp-router/node.js +10 -3
  11. package/dist/cjs/router/reg-exp-router/router.js +47 -83
  12. package/dist/cjs/router/reg-exp-router/trie.js +2 -1
  13. package/dist/cjs/router/trie-router/node.js +4 -3
  14. package/dist/cjs/router/utils.js +27 -0
  15. package/dist/cjs/utils/body.js +15 -3
  16. package/dist/cjs/utils/cookie.js +1 -1
  17. package/dist/cjs/utils/stream.js +7 -1
  18. package/dist/cjs/utils/url.js +9 -1
  19. package/dist/client/client.js +25 -11
  20. package/dist/client/utils.js +3 -0
  21. package/dist/helper/accepts/accepts.js +36 -2
  22. package/dist/helper/ssg/ssg.js +1 -1
  23. package/dist/helper/ssg/utils.js +30 -10
  24. package/dist/jsx/dom/render.js +2 -0
  25. package/dist/middleware/cache/index.js +1 -1
  26. package/dist/middleware/etag/index.js +1 -1
  27. package/dist/request.js +8 -4
  28. package/dist/router/reg-exp-router/node.js +6 -2
  29. package/dist/router/reg-exp-router/router.js +53 -84
  30. package/dist/router/reg-exp-router/trie.js +2 -1
  31. package/dist/router/trie-router/node.js +4 -3
  32. package/dist/router/utils.js +5 -0
  33. package/dist/types/router/reg-exp-router/node.d.ts +3 -0
  34. package/dist/types/router/utils.d.ts +1 -0
  35. package/dist/types/utils/url.d.ts +4 -0
  36. package/dist/utils/body.js +15 -3
  37. package/dist/utils/cookie.js +1 -1
  38. package/dist/utils/stream.js +7 -1
  39. package/dist/utils/url.js +9 -1
  40. package/package.json +1 -1
@@ -40,6 +40,10 @@ const createProxy = (callback, path) => {
40
40
  });
41
41
  return proxy;
42
42
  };
43
+ const appendQueryParams = (url, searchParams) => {
44
+ const queryString = searchParams.toString();
45
+ return queryString ? `${url}?${queryString}` : url;
46
+ };
43
47
  class ClientRequestImpl {
44
48
  url;
45
49
  method;
@@ -66,6 +70,9 @@ class ClientRequestImpl {
66
70
  }
67
71
  if (Array.isArray(v)) {
68
72
  for (const v2 of v) {
73
+ if (v2 === void 0) {
74
+ continue;
75
+ }
69
76
  form.append(k, v2);
70
77
  }
71
78
  } else {
@@ -90,19 +97,29 @@ class ClientRequestImpl {
90
97
  if (args?.cookie) {
91
98
  const cookies = [];
92
99
  for (const [key, value] of Object.entries(args.cookie)) {
100
+ if (value === void 0) {
101
+ continue;
102
+ }
93
103
  cookies.push((0, import_cookie.serialize)(key, value));
94
104
  }
95
- headerValues["Cookie"] = cookies.join("; ");
105
+ if (cookies.length > 0) {
106
+ headerValues["Cookie"] = cookies.join("; ");
107
+ }
96
108
  }
97
109
  if (this.cType) {
98
110
  headerValues["Content-Type"] = this.cType;
99
111
  }
100
- const headers = new Headers(headerValues ?? void 0);
112
+ const headers = new Headers();
113
+ for (const [key, value] of Object.entries(headerValues)) {
114
+ if (value !== void 0) {
115
+ headers.set(key, value);
116
+ }
117
+ }
101
118
  let url = this.url;
102
119
  url = (0, import_utils.removeIndexString)(url);
103
120
  url = (0, import_utils.replaceUrlParam)(url, this.pathParams);
104
121
  if (this.queryParams) {
105
- url = url + "?" + this.queryParams.toString();
122
+ url = appendQueryParams(url, this.queryParams);
106
123
  }
107
124
  methodUpperCase = this.method.toUpperCase();
108
125
  const setBody = !(methodUpperCase === "GET" || methodUpperCase === "HEAD");
@@ -146,7 +163,7 @@ const hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
146
163
  result = (0, import_utils.replaceUrlParam)(url, opts.args[0].param);
147
164
  }
148
165
  if (opts.args[0].query) {
149
- result = result + "?" + buildSearchParamsOption(opts.args[0].query).toString();
166
+ result = appendQueryParams(result, buildSearchParamsOption(opts.args[0].query));
150
167
  }
151
168
  }
152
169
  result = (0, import_utils.removeIndexString)(result);
@@ -157,18 +174,15 @@ const hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
157
174
  }
158
175
  if (method === "ws") {
159
176
  const webSocketUrl = (0, import_utils.replaceUrlProtocol)(
160
- opts.args[0] && opts.args[0].param ? (0, import_utils.replaceUrlParam)(url, opts.args[0].param) : url,
177
+ opts.args[0]?.param ? (0, import_utils.replaceUrlParam)(url, opts.args[0].param) : url,
161
178
  "ws"
162
179
  );
163
180
  const targetUrl = new URL(webSocketUrl);
164
181
  const queryParams = opts.args[0]?.query;
165
182
  if (queryParams) {
166
- Object.entries(queryParams).forEach(([key, value]) => {
167
- if (Array.isArray(value)) {
168
- value.forEach((item) => targetUrl.searchParams.append(key, item));
169
- } else {
170
- targetUrl.searchParams.set(key, value);
171
- }
183
+ const searchParams = buildSearchParamsOption(queryParams);
184
+ searchParams.forEach((value, key) => {
185
+ targetUrl.searchParams.append(key, value);
172
186
  });
173
187
  }
174
188
  const establishWebSocket = (...args) => {
@@ -49,6 +49,9 @@ const buildSearchParams = (query) => {
49
49
  }
50
50
  if (Array.isArray(v)) {
51
51
  for (const v2 of v) {
52
+ if (v2 === void 0) {
53
+ continue;
54
+ }
52
55
  searchParams.append(k, v2);
53
56
  }
54
57
  } else {
@@ -22,10 +22,44 @@ __export(accepts_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(accepts_exports);
24
24
  var import_accept = require("../../utils/accept");
25
+ const matchType = (acceptType, supportedType) => {
26
+ if (acceptType === supportedType) {
27
+ return true;
28
+ }
29
+ if (acceptType === "*/*" || acceptType === "*") {
30
+ return false;
31
+ }
32
+ if (acceptType.endsWith("/*")) {
33
+ const [acceptMain] = acceptType.split("/");
34
+ const [supportedMain] = supportedType.split("/");
35
+ return acceptMain === supportedMain;
36
+ }
37
+ return false;
38
+ };
39
+ const getSpecificity = (type) => {
40
+ if (type === "*/*" || type === "*") {
41
+ return 1;
42
+ }
43
+ if (type.endsWith("/*")) {
44
+ return 2;
45
+ }
46
+ return 3;
47
+ };
25
48
  const defaultMatch = (accepts2, config) => {
26
49
  const { supports, default: defaultSupport } = config;
27
- const accept = accepts2.sort((a, b) => b.q - a.q).find((accept2) => supports.includes(accept2.type));
28
- return accept ? accept.type : defaultSupport;
50
+ const sortedAccepts = accepts2.slice().sort((a, b) => {
51
+ if (b.q !== a.q) {
52
+ return b.q - a.q;
53
+ }
54
+ return getSpecificity(b.type) - getSpecificity(a.type);
55
+ });
56
+ for (const accept of sortedAccepts) {
57
+ const matched = supports.find((supported) => matchType(accept.type, supported));
58
+ if (matched) {
59
+ return matched;
60
+ }
61
+ }
62
+ return defaultSupport;
29
63
  };
30
64
  const accepts = (c, options) => {
31
65
  const acceptHeader = c.req.header(options.header);
@@ -206,7 +206,7 @@ const saveContentToFile = async (data, fsModule, outDir, extensionMap) => {
206
206
  const { routePath, content, mimeType } = awaitedData;
207
207
  const filePath = generateFilePath(routePath, outDir, mimeType, extensionMap);
208
208
  const dirPath = (0, import_utils2.dirname)(filePath);
209
- if (!createdDirs.has(dirPath)) {
209
+ if (dirPath !== "" && !createdDirs.has(dirPath)) {
210
210
  await fsModule.mkdir(dirPath, { recursive: true });
211
211
  createdDirs.add(dirPath);
212
212
  }
@@ -33,8 +33,14 @@ const dirname = (path) => {
33
33
  const normalizePath = (path) => {
34
34
  return path.replace(/(\\)/g, "/").replace(/\/$/g, "");
35
35
  };
36
- const handleParent = (resultPaths, beforeParentFlag) => {
37
- if (resultPaths.length === 0 || beforeParentFlag) {
36
+ const getUncRoot = (path) => {
37
+ const uncRoot = path.replace(/\\/g, "/").match(/^\/\/([^/]+)\/([^/]+)/);
38
+ if (uncRoot) {
39
+ return `${uncRoot[1].toLowerCase()}/${uncRoot[2].toLowerCase()}`;
40
+ }
41
+ };
42
+ const handleParent = (resultPaths) => {
43
+ if (resultPaths.length === 0 || resultPaths[resultPaths.length - 1] === "..") {
38
44
  resultPaths.push("..");
39
45
  } else {
40
46
  resultPaths.pop();
@@ -47,22 +53,20 @@ const handleNonDot = (path, resultPaths) => {
47
53
  }
48
54
  };
49
55
  const handleSegments = (paths, resultPaths) => {
50
- let beforeParentFlag = false;
51
56
  for (const path of paths) {
52
57
  if (path === "..") {
53
- handleParent(resultPaths, beforeParentFlag);
54
- beforeParentFlag = true;
58
+ handleParent(resultPaths);
55
59
  } else {
56
60
  handleNonDot(path, resultPaths);
57
- beforeParentFlag = false;
58
61
  }
59
62
  }
60
63
  };
61
64
  const joinPaths = (...paths) => {
65
+ const hasUncPrefix = getUncRoot(paths[0]) !== void 0;
62
66
  paths = paths.map(normalizePath);
63
67
  const resultPaths = [];
64
68
  handleSegments(paths.join("/").split("/"), resultPaths);
65
- return (paths[0][0] === "/" ? "/" : "") + resultPaths.join("/");
69
+ return (hasUncPrefix ? "//" : paths[0][0] === "/" ? "/" : "") + resultPaths.join("/");
66
70
  };
67
71
  const filterStaticGenerateRoutes = (hono) => {
68
72
  return hono.routes.reduce((acc, { method, handler, path }) => {
@@ -76,10 +80,26 @@ const filterStaticGenerateRoutes = (hono) => {
76
80
  const isDynamicRoute = (path) => {
77
81
  return path.split("/").some((segment) => segment.startsWith(":") || segment.includes("*"));
78
82
  };
83
+ const toSegments = (path) => path === "" ? [] : path.split("/");
84
+ const getPathRoot = (path) => {
85
+ const normalizedPath = path.replace(/\\/g, "/");
86
+ const uncRoot = getUncRoot(normalizedPath);
87
+ if (uncRoot) {
88
+ return `unc:${uncRoot}`;
89
+ }
90
+ const driveRoot = normalizedPath.match(/^([A-Za-z]):/);
91
+ if (driveRoot) {
92
+ const kind = normalizedPath[2] === "/" ? "drive-absolute" : "drive-relative";
93
+ return `${kind}:${driveRoot[1].toLowerCase()}`;
94
+ }
95
+ return normalizedPath.startsWith("/") ? "absolute" : "relative";
96
+ };
79
97
  const ensureWithinOutDir = (outDir, filePath) => {
80
- const normalizedOutDir = joinPaths("/", outDir);
81
- const normalizedFilePath = joinPaths("/", filePath);
82
- if (normalizedFilePath !== normalizedOutDir && !normalizedFilePath.startsWith(`${normalizedOutDir}/`)) {
98
+ const outDirSegments = toSegments(joinPaths(outDir));
99
+ const filePathSegments = toSegments(joinPaths(filePath));
100
+ const hasMismatchedPathRoot = getPathRoot(outDir) !== getPathRoot(filePath);
101
+ const climbsAboveOutDir = filePathSegments[outDirSegments.length] === "..";
102
+ if (hasMismatchedPathRoot || filePathSegments.length <= outDirSegments.length || !outDirSegments.every((segment, i) => segment === filePathSegments[i]) || climbsAboveOutDir) {
83
103
  throw new Error(`Path traversal detected: "${filePath}" is outside the output directory`);
84
104
  }
85
105
  };
@@ -101,6 +101,7 @@ const applyProps = (container, attributes, oldAttributes) => {
101
101
  } else if (key === "dangerouslySetInnerHTML" && value) {
102
102
  container.innerHTML = value.__html;
103
103
  } else if (key === "ref") {
104
+ refCleanupMap.get(container)?.();
104
105
  let cleanup;
105
106
  if (typeof value === "function") {
106
107
  cleanup = value(container) || (() => value(null));
@@ -165,6 +166,7 @@ const applyProps = (container, attributes, oldAttributes) => {
165
166
  container.removeEventListener(eventSpec[0], value, eventSpec[1]);
166
167
  } else if (key === "ref") {
167
168
  refCleanupMap.get(container)?.();
169
+ refCleanupMap.delete(container);
168
170
  } else {
169
171
  try {
170
172
  container.removeAttribute(toAttributeName(container, key));
@@ -44,7 +44,7 @@ const parseVaryDirectives = (vary) => {
44
44
  };
45
45
  const createCacheKey = (key, requestUrl, request, varyHeaders) => {
46
46
  const url = new URL(cacheKeyPath, requestUrl);
47
- url.searchParams.append(cacheKeyParameter, key.split("#", 1)[0]);
47
+ url.searchParams.append(cacheKeyParameter, key);
48
48
  url.searchParams.append(cacheMethodKeyParameter, request.method);
49
49
  if (request.method === "QUERY") {
50
50
  url.searchParams.append(queryDigestKeyParameter, request.digest);
@@ -32,7 +32,7 @@ const RETAINED_304_HEADERS = [
32
32
  ];
33
33
  const stripWeak = (tag) => tag.replace(/^W\//, "");
34
34
  function etagMatches(etag2, ifNoneMatch) {
35
- return ifNoneMatch != null && ifNoneMatch.split(/,\s*/).some((t) => stripWeak(t) === stripWeak(etag2));
35
+ return ifNoneMatch != null && ifNoneMatch.split(",").some((t) => stripWeak(t.trim()) === stripWeak(etag2));
36
36
  }
37
37
  function initializeGenerator(generator) {
38
38
  if (!generator) {
@@ -68,13 +68,13 @@ class HonoRequest {
68
68
  return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
69
69
  }
70
70
  #getDecodedParam(key) {
71
- const paramKey = this.#matchResult[0][this.routeIndex][1][key];
71
+ const paramKey = this.#matchResult[0][this.routeIndex]?.[1][key];
72
72
  const param = this.#getParamValue(paramKey);
73
73
  return param && (0, import_url.tryDecodeURIComponent)(param);
74
74
  }
75
75
  #getAllDecodedParams() {
76
76
  const decoded = {};
77
- const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
77
+ const keys = Object.keys(this.#matchResult[0][this.routeIndex]?.[1] ?? {});
78
78
  for (const key of keys) {
79
79
  const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
80
80
  if (value !== void 0) {
@@ -314,10 +314,14 @@ const cloneRawRequest = async (req) => {
314
314
  message: "Cannot clone request: body was already consumed and not cached. Please use HonoRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly."
315
315
  });
316
316
  }
317
- const body = await req[cacheKey]();
317
+ let body = await req[cacheKey]();
318
318
  const headers = req.header();
319
- if (body instanceof FormData) {
319
+ if (cacheKey === "json") {
320
+ body = JSON.stringify(body);
321
+ delete headers["content-length"];
322
+ } else if (body instanceof FormData) {
320
323
  delete headers["content-type"];
324
+ delete headers["content-length"];
321
325
  }
322
326
  const requestInit = {
323
327
  body,
@@ -17,10 +17,14 @@ var __copyProps = (to, from, except, desc) => {
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
18
  var node_exports = {};
19
19
  __export(node_exports, {
20
+ LABEL_REG_EXP_STR: () => LABEL_REG_EXP_STR,
20
21
  Node: () => Node,
21
- PATH_ERROR: () => PATH_ERROR
22
+ ONLY_WILDCARD_REG_EXP_STR: () => ONLY_WILDCARD_REG_EXP_STR,
23
+ PATH_ERROR: () => PATH_ERROR,
24
+ TAIL_WILDCARD_REG_EXP_STR: () => TAIL_WILDCARD_REG_EXP_STR
22
25
  });
23
26
  module.exports = __toCommonJS(node_exports);
27
+ var import_utils = require("../utils");
24
28
  const LABEL_REG_EXP_STR = "[^/]+";
25
29
  const ONLY_WILDCARD_REG_EXP_STR = ".*";
26
30
  const TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
@@ -49,7 +53,7 @@ class Node {
49
53
  // handler index of a dynamic path, or -1 for a static path terminal
50
54
  #index;
51
55
  #varIndex;
52
- #children = /* @__PURE__ */ Object.create(null);
56
+ #children = (0, import_utils.createNullObject)();
53
57
  insert(tokens, index, paramMap, context, isStatic) {
54
58
  let node = this;
55
59
  for (let i = 0, len = tokens.length; i < len; i++) {
@@ -128,6 +132,9 @@ class Node {
128
132
  }
129
133
  // Annotate the CommonJS export names for ESM import in node:
130
134
  0 && (module.exports = {
135
+ LABEL_REG_EXP_STR,
131
136
  Node,
132
- PATH_ERROR
137
+ ONLY_WILDCARD_REG_EXP_STR,
138
+ PATH_ERROR,
139
+ TAIL_WILDCARD_REG_EXP_STR
133
140
  });
@@ -22,25 +22,20 @@ __export(router_exports, {
22
22
  module.exports = __toCommonJS(router_exports);
23
23
  var import_router = require("../../router");
24
24
  var import_url = require("../../utils/url");
25
+ var import_utils = require("../utils");
25
26
  var import_matcher = require("./matcher");
26
27
  var import_node = require("./node");
27
28
  var import_trie = require("./trie");
28
- let wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
29
+ let wildcardRegExpCache = (0, import_utils.createNullObject)();
29
30
  function buildWildcardRegExp(path) {
30
31
  return wildcardRegExpCache[path] ??= new RegExp(
31
- path === "*" ? "" : `^${path.replace(
32
- /\/\*$|([.\\+*[^\]$()])/g,
33
- (_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)"
32
+ `^${path.replace(
33
+ /\/:[^/{}]+(?:\{\[\^\/]\+})?(?=[/{]|$)|\/?\*$|([.\\+*[^\]$()?{}|])/g,
34
+ (match2, metaChar) => metaChar ? `\\${metaChar}` : match2 === "/*" ? import_node.TAIL_WILDCARD_REG_EXP_STR : match2 === "*" ? import_node.ONLY_WILDCARD_REG_EXP_STR : `/:${import_node.LABEL_REG_EXP_STR}`
34
35
  )}$`
35
36
  );
36
37
  }
37
- function clearWildcardRegExpCache() {
38
- wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
39
- }
40
38
  function findMiddleware(middleware, path) {
41
- if (!middleware) {
42
- return void 0;
43
- }
44
39
  for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
45
40
  if (buildWildcardRegExp(k).test(path)) {
46
41
  return [...middleware[k]];
@@ -54,8 +49,8 @@ class RegExpRouter {
54
49
  #routes;
55
50
  #tries;
56
51
  constructor() {
57
- this.#middleware = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
58
- this.#routes = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
52
+ this.#middleware = { [import_router.METHOD_NAME_ALL]: (0, import_utils.createNullObject)() };
53
+ this.#routes = { [import_router.METHOD_NAME_ALL]: (0, import_utils.createNullObject)() };
59
54
  this.#tries = { [import_router.METHOD_NAME_ALL]: new import_trie.Trie() };
60
55
  }
61
56
  #insertPath(method, path) {
@@ -68,117 +63,86 @@ class RegExpRouter {
68
63
  add(method, path, handler) {
69
64
  const middleware = this.#middleware;
70
65
  const routes = this.#routes;
71
- if (!middleware || !routes) {
66
+ if (!middleware) {
72
67
  throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
73
68
  }
74
69
  if (!middleware[method]) {
75
70
  this.#tries[method] = new import_trie.Trie();
76
- [middleware, routes].forEach((handlerMap) => {
77
- handlerMap[method] = /* @__PURE__ */ Object.create(null);
78
- Object.keys(handlerMap[import_router.METHOD_NAME_ALL]).forEach((p) => {
71
+ for (const handlerMap of [middleware, routes]) {
72
+ handlerMap[method] = (0, import_utils.createNullObject)();
73
+ for (const p in handlerMap[import_router.METHOD_NAME_ALL]) {
79
74
  handlerMap[method][p] = [...handlerMap[import_router.METHOD_NAME_ALL][p]];
80
75
  this.#insertPath(method, p);
81
- });
82
- });
76
+ }
77
+ }
83
78
  }
84
79
  if (path === "/*") {
85
80
  path = "*";
86
81
  }
87
- const paramCount = (path.match(/\/:/g) || []).length;
82
+ const methods = method === import_router.METHOD_NAME_ALL ? Object.keys(middleware) : [method];
88
83
  if (/\*$/.test(path)) {
89
84
  const re = buildWildcardRegExp(path);
90
- Object.keys(middleware).forEach((m) => {
91
- if ((method === import_router.METHOD_NAME_ALL || method === m) && !middleware[m][path]) {
85
+ for (const m of methods) {
86
+ if (!middleware[m][path]) {
92
87
  this.#insertPath(m, path);
93
88
  middleware[m][path] = findMiddleware(middleware[m], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || [];
94
89
  }
95
- });
96
- Object.keys(middleware).forEach((m) => {
97
- if (method === import_router.METHOD_NAME_ALL || method === m) {
98
- Object.keys(middleware[m]).forEach((p) => {
99
- re.test(p) && middleware[m][p].push([handler, paramCount]);
100
- });
101
- }
102
- });
103
- Object.keys(routes).forEach((m) => {
104
- if (method === import_router.METHOD_NAME_ALL || method === m) {
105
- Object.keys(routes[m]).forEach(
106
- (p) => re.test(p) && routes[m][p].push([handler, paramCount])
107
- );
90
+ }
91
+ for (const handlerMap of [middleware, routes]) {
92
+ for (const m of methods) {
93
+ for (const p in handlerMap[m]) {
94
+ re.test(p) && handlerMap[m][p].push([handler, path]);
95
+ }
108
96
  }
109
- });
97
+ }
110
98
  return;
111
99
  }
112
100
  const paths = (0, import_url.checkOptionalParameter)(path) || [path];
113
- for (let i = 0, len = paths.length; i < len; i++) {
114
- const path2 = paths[i];
115
- Object.keys(routes).forEach((m) => {
116
- if (method === import_router.METHOD_NAME_ALL || method === m) {
117
- if (!routes[m][path2]) {
118
- this.#insertPath(m, path2);
119
- routes[m][path2] = [
120
- ...findMiddleware(middleware[m], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || []
121
- ];
122
- }
123
- routes[m][path2].push([handler, paramCount - len + i + 1]);
101
+ for (const path2 of paths) {
102
+ for (const m of methods) {
103
+ if (!routes[m][path2]) {
104
+ this.#insertPath(m, path2);
105
+ routes[m][path2] = findMiddleware(middleware[m], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || [];
124
106
  }
125
- });
107
+ routes[m][path2].push([handler, path2]);
108
+ }
126
109
  }
127
110
  }
128
111
  match = import_matcher.match;
129
112
  buildAllMatchers() {
130
- const matchers = /* @__PURE__ */ Object.create(null);
131
- Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
132
- matchers[method] ||= this.#buildMatcher(method);
133
- });
113
+ const matchers = (0, import_utils.createNullObject)();
114
+ for (const method of Object.keys(this.#routes)) {
115
+ matchers[method] = this.#buildMatcher(method);
116
+ }
134
117
  this.#middleware = this.#routes = this.#tries = void 0;
135
- clearWildcardRegExpCache();
118
+ wildcardRegExpCache = (0, import_utils.createNullObject)();
136
119
  return matchers;
137
120
  }
138
121
  #buildMatcher(method) {
139
122
  const middleware = this.#middleware[method];
140
123
  const routes = this.#routes[method];
141
124
  const trie = this.#tries[method];
142
- const staticMap = /* @__PURE__ */ Object.create(null);
125
+ const staticMap = (0, import_utils.createNullObject)();
143
126
  const handlerData = [];
144
- [middleware, routes].forEach((r) => {
127
+ const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
128
+ for (const r of [middleware, routes]) {
145
129
  for (const path in r) {
146
130
  const handlers = r[path];
147
131
  const pathData = trie.paths[path];
148
132
  if (!pathData) {
149
- staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), import_matcher.emptyParam];
133
+ staticMap[path] = [handlers.map(([h]) => [h, (0, import_utils.createNullObject)()]), import_matcher.emptyParam];
150
134
  continue;
151
135
  }
152
- const paramAssoc = pathData[1];
153
- handlerData[pathData[0]] = handlers.map(([h, paramCount]) => {
154
- const paramIndexMap = /* @__PURE__ */ Object.create(null);
155
- paramCount -= 1;
156
- for (; paramCount >= 0; paramCount--) {
157
- const [key, value] = paramAssoc[paramCount];
158
- paramIndexMap[key] = value;
159
- }
160
- return [h, paramIndexMap];
161
- });
136
+ handlerData[pathData[0]] = handlers.map(([h, handlerPath]) => [
137
+ h,
138
+ trie.paths[handlerPath][1].reduceRight((map, [key], i) => {
139
+ map[key] = paramReplacementMap[pathData[1][i][1]];
140
+ return map;
141
+ }, (0, import_utils.createNullObject)())
142
+ ]);
162
143
  }
163
- });
164
- const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
165
- for (let i = 0, len = handlerData.length; i < len; i++) {
166
- for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
167
- const map = handlerData[i][j]?.[1];
168
- if (!map) {
169
- continue;
170
- }
171
- const keys = Object.keys(map);
172
- for (let k = 0, len3 = keys.length; k < len3; k++) {
173
- map[keys[k]] = paramReplacementMap[map[keys[k]]];
174
- }
175
- }
176
- }
177
- const handlerMap = [];
178
- for (const i in indexReplacementMap) {
179
- handlerMap[i] = handlerData[indexReplacementMap[i]];
180
144
  }
181
- return [regexp, handlerMap, staticMap];
145
+ return [regexp, indexReplacementMap.map((i) => handlerData[i]), staticMap];
182
146
  }
183
147
  }
184
148
  // Annotate the CommonJS export names for ESM import in node:
@@ -20,13 +20,14 @@ __export(trie_exports, {
20
20
  Trie: () => Trie
21
21
  });
22
22
  module.exports = __toCommonJS(trie_exports);
23
+ var import_utils = require("../utils");
23
24
  var import_node = require("./node");
24
25
  class Trie {
25
26
  #context = { varIndex: 0 };
26
27
  #root = new import_node.Node();
27
28
  #index = 0;
28
29
  // dynamic path -> [handler index, param assoc]; static paths are not registered
29
- paths = /* @__PURE__ */ Object.create(null);
30
+ paths = (0, import_utils.createNullObject)();
30
31
  insert(path, isStatic) {
31
32
  if (isStatic) {
32
33
  this.#root.insert(path.split(""), 0, [], this.#context, true);
@@ -22,11 +22,12 @@ __export(node_exports, {
22
22
  module.exports = __toCommonJS(node_exports);
23
23
  var import_router = require("../../router");
24
24
  var import_url = require("../../utils/url");
25
- const emptyParams = /* @__PURE__ */ Object.create(null);
25
+ var import_utils = require("../utils");
26
+ const emptyParams = (0, import_utils.createNullObject)();
26
27
  let order = 0;
27
28
  class Node {
28
29
  #methods = [];
29
- #children = /* @__PURE__ */ Object.create(null);
30
+ #children = (0, import_utils.createNullObject)();
30
31
  #patterns = [];
31
32
  #pattern;
32
33
  #params = emptyParams;
@@ -63,7 +64,7 @@ class Node {
63
64
  const m = node.#methods[i];
64
65
  const handlerSet = m[method] || m[import_router.METHOD_NAME_ALL];
65
66
  if (handlerSet) {
66
- handlerSet.params = /* @__PURE__ */ Object.create(null);
67
+ handlerSet.params = (0, import_utils.createNullObject)();
67
68
  handlerSets.push(handlerSet);
68
69
  for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
69
70
  const key = handlerSet.possibleKeys[i2];
@@ -0,0 +1,27 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var utils_exports = {};
19
+ __export(utils_exports, {
20
+ createNullObject: () => createNullObject
21
+ });
22
+ module.exports = __toCommonJS(utils_exports);
23
+ const createNullObject = () => /* @__PURE__ */ Object.create(null);
24
+ // Annotate the CommonJS export names for ESM import in node:
25
+ 0 && (module.exports = {
26
+ createNullObject
27
+ });