hono 0.5.4 → 0.5.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.
@@ -5,9 +5,7 @@ export interface Context {
5
5
  export declare class Node {
6
6
  index?: number;
7
7
  varIndex?: number;
8
- children: {
9
- [key: string]: Node;
10
- };
8
+ children: Record<string, Node>;
11
9
  insert(tokens: readonly string[], index: number, paramMap: ParamMap, context: Context): void;
12
10
  buildRegExpStr(): string;
13
11
  }
@@ -1,15 +1,7 @@
1
1
  import { Router, Result } from '../../router';
2
- import type { ParamMap, ReplacementMap } from './trie';
3
2
  declare type Route<T> = [string, T];
4
- declare type HandlerData<T> = [T, ParamMap];
5
- declare type Matcher<T> = [RegExp | true, ReplacementMap, HandlerData<T>[]];
6
3
  export declare class RegExpRouter<T> extends Router<T> {
7
- routes?: {
8
- [method: string]: Route<T>[];
9
- };
10
- matchers?: {
11
- [method: string]: Matcher<T> | null;
12
- };
4
+ routes?: Record<string, Route<T>[]>;
13
5
  add(method: string, path: string, handler: T): void;
14
6
  match(method: string, path: string): Result<T> | null;
15
7
  private buildAllMatchers;
@@ -3,11 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RegExpRouter = void 0;
4
4
  const router_1 = require("../../router");
5
5
  const trie_1 = require("./trie");
6
+ const regExpMatchAll = new RegExp('');
7
+ const emptyParam = {};
6
8
  class RegExpRouter extends router_1.Router {
7
9
  constructor() {
8
10
  super(...arguments);
9
11
  this.routes = {};
10
- this.matchers = null;
11
12
  }
12
13
  add(method, path, handler) {
13
14
  var _a;
@@ -18,43 +19,54 @@ class RegExpRouter extends router_1.Router {
18
19
  this.routes[method].push([path, handler]);
19
20
  }
20
21
  match(method, path) {
21
- if (!this.matchers) {
22
- this.buildAllMatchers();
23
- }
24
- const matcher = this.matchers[method] || this.matchers[router_1.METHOD_NAME_OF_ALL];
25
- if (!matcher) {
26
- return null;
27
- }
28
- const [regexp, replacementMap, handlers] = matcher;
29
- if (regexp === true) {
30
- // '*'
31
- return new router_1.Result(handlers[0][0], {});
32
- }
33
- const match = path.match(regexp);
34
- if (!match) {
35
- return null;
36
- }
37
- if (!replacementMap) {
38
- // there is only one route and no capture
39
- return new router_1.Result(handlers[0][0], {});
40
- }
41
- const index = match.indexOf('', 1);
42
- const [handler, paramMap] = handlers[replacementMap[index]];
43
- const params = {};
44
- for (let i = 0; i < paramMap.length; i++) {
45
- params[paramMap[i][0]] = match[paramMap[i][1]];
22
+ const matchers = this.buildAllMatchers();
23
+ let match;
24
+ // Optimization for middleware
25
+ const methods = Object.keys(matchers);
26
+ if (methods.length === 1 && methods[0] === router_1.METHOD_NAME_OF_ALL) {
27
+ const [regexp, handlers] = matchers[router_1.METHOD_NAME_OF_ALL];
28
+ if (handlers.length === 1) {
29
+ const result = new router_1.Result(handlers[0][0], emptyParam);
30
+ if (regexp === regExpMatchAll) {
31
+ match = () => result;
32
+ }
33
+ else if (handlers.length === 1 && !handlers[0][1]) {
34
+ match = (_, path) => (regexp.test(path) ? result : null);
35
+ }
36
+ }
46
37
  }
47
- return new router_1.Result(handler, params);
38
+ match || (match = (method, path) => {
39
+ const matcher = matchers[method] || matchers[router_1.METHOD_NAME_OF_ALL];
40
+ if (!matcher) {
41
+ return null;
42
+ }
43
+ const match = path.match(matcher[0]);
44
+ if (!match) {
45
+ return null;
46
+ }
47
+ const index = match.indexOf('', 1);
48
+ const [handler, paramMap] = matcher[1][index];
49
+ if (!paramMap) {
50
+ return new router_1.Result(handler, emptyParam);
51
+ }
52
+ const params = {};
53
+ for (let i = 0; i < paramMap.length; i++) {
54
+ params[paramMap[i][0]] = match[paramMap[i][1]];
55
+ }
56
+ return new router_1.Result(handler, params);
57
+ });
58
+ this.match = match;
59
+ return this.match(method, path);
48
60
  }
49
61
  buildAllMatchers() {
50
- this.matchers || (this.matchers = {});
62
+ const matchers = {};
51
63
  Object.keys(this.routes).forEach((method) => {
52
- this.buildMatcher(method);
64
+ matchers[method] = this.buildMatcher(method);
53
65
  });
54
66
  delete this.routes; // to reduce memory usage
67
+ return matchers;
55
68
  }
56
69
  buildMatcher(method) {
57
- this.matchers || (this.matchers = {});
58
70
  const trie = new trie_1.Trie();
59
71
  const handlers = [];
60
72
  const targetMethods = [method];
@@ -63,34 +75,40 @@ class RegExpRouter extends router_1.Router {
63
75
  }
64
76
  const routes = targetMethods.flatMap((method) => this.routes[method] || []);
65
77
  if (routes.length === 0) {
66
- this.matchers[method] = null;
67
- return;
68
- }
69
- if (routes.length === 1 && routes[0][0] === '*') {
70
- this.matchers[method] = [true, null, [[routes[0][1], []]]];
71
- return;
78
+ return null;
72
79
  }
73
- if (routes.length === 1 && !routes[0][0].match(/:/)) {
74
- // there is only one route and no capture
75
- const tmp = routes[0][0].endsWith('*')
76
- ? routes[0][0].replace(/\/\*$/, '(?:$|/)') // /path/to/* => /path/to(?:$|/)
77
- : `${routes[0][0]}$`; // /path/to/action => /path/to/action$
78
- const regExpStr = `^${tmp.replace(/\*/g, '[^/]+')}`; // /prefix/*/path/to => /prefix/[^/]+/path/to
79
- this.matchers[method] = [new RegExp(regExpStr), null, [[routes[0][1], []]]];
80
- return;
80
+ if (method === router_1.METHOD_NAME_OF_ALL) {
81
+ if (routes.length === 1 && routes[0][0] === '*') {
82
+ return [regExpMatchAll, [[routes[0][1], null]]];
83
+ }
84
+ if (routes.length === 1 && !routes[0][0].match(/:/)) {
85
+ // there is only one route and no capture
86
+ const tmp = routes[0][0].endsWith('*')
87
+ ? routes[0][0].replace(/\/\*$/, '(?:$|/)') // /path/to/* => /path/to(?:$|/)
88
+ : `${routes[0][0]}$`; // /path/to/action => /path/to/action$
89
+ const regExpStr = `^${tmp.replace(/\*/g, '[^/]+')}`; // /prefix/*/path/to => /prefix/[^/]+/path/to
90
+ return [new RegExp(regExpStr), [[routes[0][1], null]]];
91
+ }
81
92
  }
82
93
  for (let i = 0; i < routes.length; i++) {
83
94
  const paramMap = trie.insert(routes[i][0], i);
84
- handlers[i] = [routes[i][1], paramMap];
95
+ handlers[i] = [routes[i][1], Object.keys(paramMap).length !== 0 ? paramMap : null];
85
96
  }
86
97
  const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
87
98
  for (let i = 0; i < handlers.length; i++) {
88
99
  const paramMap = handlers[i][1];
89
- for (let i = 0; i < paramMap.length; i++) {
90
- paramMap[i][1] = paramReplacementMap[paramMap[i][1]];
100
+ if (paramMap) {
101
+ for (let i = 0; i < paramMap.length; i++) {
102
+ paramMap[i][1] = paramReplacementMap[paramMap[i][1]];
103
+ }
91
104
  }
92
105
  }
93
- this.matchers[method] = [new RegExp(regexp), indexReplacementMap, handlers];
106
+ const handlerMap = [];
107
+ // using `in` because indexReplacementMap is a sparse array
108
+ for (const i in indexReplacementMap) {
109
+ handlerMap[i] = handlers[indexReplacementMap[i]];
110
+ }
111
+ return [regexp, handlerMap];
94
112
  }
95
113
  }
96
114
  exports.RegExpRouter = RegExpRouter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,10 +20,12 @@
20
20
  "./body-parse": "./dist/middleware/body-parse/body-parse.js",
21
21
  "./cookie": "./dist/middleware/cookie/cookie.js",
22
22
  "./cors": "./dist/middleware/cors/cors.js",
23
+ "./etag": "./dist/middleware/etag/etag.js",
23
24
  "./logger": "./dist/middleware/logger/logger.js",
24
25
  "./mustache": "./dist/middleware/mustache/mustache.js",
25
26
  "./powered-by": "./dist/middleware/powered-by/powered-by.js",
26
27
  "./serve-static": "./dist/middleware/serve-static/serve-static.js",
28
+ "./router/trie-router": "./dist/router/trie-router/router.js",
27
29
  "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js"
28
30
  },
29
31
  "typesVersions": {
@@ -40,6 +42,9 @@
40
42
  "cors": [
41
43
  "./dist/middleware/cors/cors.d.ts"
42
44
  ],
45
+ "etag": [
46
+ "./dist/middleware/etag/etag.d.ts"
47
+ ],
43
48
  "logger": [
44
49
  "./dist/middleware/logger/logger.d.ts"
45
50
  ],
@@ -52,6 +57,9 @@
52
57
  "serve-static": [
53
58
  "./dist/middleware/serve-static/serve-static.d.ts"
54
59
  ],
60
+ "router/trie-router": [
61
+ "./dist/router/trie-router/router.d.ts"
62
+ ],
55
63
  "router/reg-exp-router": [
56
64
  "./dist/router/reg-exp-router/router.d.ts"
57
65
  ]