hono 2.3.2 → 2.4.1

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 (143) 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 +33 -7
  14. package/dist/cjs/middleware/jsx/jsx-runtime.js +32 -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 +15 -1
  56. package/dist/cjs/utils/url.js +89 -78
  57. package/dist/cjs/validator/rule.js +78 -64
  58. package/dist/cjs/validator/sanitizer.js +28 -4
  59. package/dist/cjs/validator/schema.js +15 -1
  60. package/dist/cjs/validator/validator.js +405 -386
  61. package/dist/compose.d.ts +1 -1
  62. package/dist/compose.js +61 -64
  63. package/dist/context.d.ts +4 -4
  64. package/dist/context.js +139 -143
  65. package/dist/hono.d.ts +1 -17
  66. package/dist/hono.js +153 -149
  67. package/dist/index.d.ts +3 -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.d.ts +1 -0
  84. package/dist/middleware/jsx/jsx-dev-runtime.js +11 -5
  85. package/dist/middleware/jsx/jsx-runtime.d.ts +1 -1
  86. package/dist/middleware/jsx/jsx-runtime.js +8 -2
  87. package/dist/middleware/jwt/index.d.ts +1 -1
  88. package/dist/middleware/jwt/index.js +53 -52
  89. package/dist/middleware/logger/index.d.ts +1 -1
  90. package/dist/middleware/logger/index.js +35 -39
  91. package/dist/middleware/powered-by/index.d.ts +1 -1
  92. package/dist/middleware/powered-by/index.js +9 -5
  93. package/dist/middleware/pretty-json/index.d.ts +1 -1
  94. package/dist/middleware/pretty-json/index.js +10 -6
  95. package/dist/middleware/serve-static/bun.d.ts +1 -1
  96. package/dist/middleware/serve-static/bun.js +35 -36
  97. package/dist/middleware/serve-static/index.js +5 -1
  98. package/dist/middleware/serve-static/{module.d.mts → module.d.ts} +1 -1
  99. package/dist/middleware/serve-static/module.js +13 -0
  100. package/dist/middleware/serve-static/serve-static.d.ts +1 -1
  101. package/dist/middleware/serve-static/serve-static.js +36 -36
  102. package/dist/middleware/validator/index.js +5 -2
  103. package/dist/middleware/validator/middleware.d.ts +1 -1
  104. package/dist/middleware/validator/middleware.js +104 -89
  105. package/dist/request.js +108 -114
  106. package/dist/router/reg-exp-router/index.js +5 -1
  107. package/dist/router/reg-exp-router/node.js +87 -96
  108. package/dist/router/reg-exp-router/router.js +140 -142
  109. package/dist/router/reg-exp-router/trie.js +35 -38
  110. package/dist/router/smart-router/index.js +5 -1
  111. package/dist/router/smart-router/router.js +50 -48
  112. package/dist/router/static-router/index.js +5 -1
  113. package/dist/router/static-router/router.js +58 -63
  114. package/dist/router/trie-router/index.js +5 -1
  115. package/dist/router/trie-router/node.js +147 -158
  116. package/dist/router/trie-router/router.js +23 -19
  117. package/dist/router.js +12 -5
  118. package/dist/types.d.ts +22 -0
  119. package/dist/types.js +0 -0
  120. package/dist/utils/body.js +15 -13
  121. package/dist/utils/buffer.js +35 -29
  122. package/dist/utils/cloudflare.js +32 -32
  123. package/dist/utils/cookie.js +38 -33
  124. package/dist/utils/crypto.js +48 -41
  125. package/dist/utils/encode.js +70 -62
  126. package/dist/utils/filepath.js +18 -20
  127. package/dist/utils/html.js +34 -32
  128. package/dist/utils/http-status.js +48 -44
  129. package/dist/utils/json.js +71 -75
  130. package/dist/utils/jwt/index.js +5 -1
  131. package/dist/utils/jwt/jwt.js +95 -90
  132. package/dist/utils/jwt/types.js +47 -41
  133. package/dist/utils/mime.js +90 -86
  134. package/dist/utils/object.js +31 -32
  135. package/dist/utils/types.js +0 -1
  136. package/dist/utils/url.js +69 -77
  137. package/dist/validator/rule.js +56 -62
  138. package/dist/validator/sanitizer.js +6 -2
  139. package/dist/validator/schema.js +0 -1
  140. package/dist/validator/validator.d.ts +5 -4
  141. package/dist/validator/validator.js +393 -393
  142. package/package.json +14 -8
  143. package/dist/middleware/serve-static/module.mjs +0 -13
@@ -1,96 +1,111 @@
1
- import { getStatusText } from '../../utils/http-status';
2
- import { mergeObjects } from '../../utils/object';
3
- import { Validator, VBase, VObjectBase } from '../../validator/validator';
4
- export const validatorMiddleware = (validationFunction, options) => {
5
- const v = new Validator();
6
- const handler = async (c, next) => {
7
- const resultSet = {
8
- hasError: false,
9
- messages: [],
10
- results: [],
11
- };
12
- const schema = validationFunction(v, c);
13
- const validatorList = getValidatorList(schema);
14
- let data = {};
15
- for (const [keys, validator] of validatorList) {
16
- let results;
17
- try {
18
- results = await validator.validate(c.req);
19
- }
20
- catch (e) {
21
- // Invalid JSON request
22
- return c.text(getStatusText(400), 400);
23
- }
24
- let isValid = true;
25
- const value = results[0].value;
26
- const jsonData = results[0].jsonData;
27
- for (const result of results) {
28
- if (!result.isValid) {
29
- isValid = false;
30
- resultSet.hasError = true;
31
- if (result.ruleType === 'value' && result.message !== undefined) {
32
- resultSet.messages.push(result.message);
33
- }
34
- }
35
- resultSet.results.push(result);
36
- }
37
- // If it's invalid but it has no "value" messages, have to set the "type" messages.
38
- // This approach is verbose, but if do not so, the response body will be empty.
39
- if (!isValid && resultSet.messages.length === 0) {
40
- resultSet.results.map((r) => {
41
- if (!r.isValid && r.ruleType === 'type' && r.message) {
42
- resultSet.messages.push(r.message);
43
- }
44
- });
45
- }
46
- // Set data on request object
47
- if (isValid) {
48
- // Set data on request object
49
- if (jsonData) {
50
- const dst = data;
51
- data = mergeObjects(dst, jsonData);
52
- }
53
- else {
54
- c.req.valid(keys, value);
55
- }
56
- }
57
- }
58
- if (!resultSet.hasError) {
59
- Object.keys(data).map((key) => {
60
- c.req.valid(key, data[key]);
61
- });
1
+ // src/middleware/validator/middleware.ts
2
+ import { getStatusText } from "../../utils/http-status.js";
3
+ import { mergeObjects } from "../../utils/object.js";
4
+ import { Validator, VBase, VObjectBase } from "../../validator/validator.js";
5
+ var validatorMiddleware = (validationFunction, options) => {
6
+ const v = new Validator();
7
+ const handler = async (c, next) => {
8
+ const resultSet = {
9
+ hasError: false,
10
+ messages: [],
11
+ results: []
12
+ };
13
+ const schema = validationFunction(v, c);
14
+ const validatorList = getValidatorList(schema);
15
+ let data = {};
16
+ for (const [keys, validator] of validatorList) {
17
+ let results;
18
+ try {
19
+ results = await validator.validate(c.req);
20
+ } catch (e) {
21
+ if (e instanceof Error) {
22
+ const result = getErrorResult(e);
23
+ resultSet.hasError = true;
24
+ resultSet.messages = [result.message || ""];
25
+ resultSet.results = [result];
26
+ break;
27
+ } else {
28
+ return c.text(getStatusText(400), 400);
62
29
  }
63
- if (options && options.done) {
64
- const res = options.done(resultSet, c);
65
- if (res) {
66
- return res;
67
- }
30
+ }
31
+ let isValid = true;
32
+ const value = results[0].value;
33
+ const jsonData = results[0].jsonData;
34
+ for (const result of results) {
35
+ if (!result.isValid) {
36
+ isValid = false;
37
+ resultSet.hasError = true;
38
+ if (result.ruleType === "value" && result.message !== void 0) {
39
+ resultSet.messages.push(result.message);
40
+ }
68
41
  }
69
- if (resultSet.hasError) {
70
- return c.text(resultSet.messages.join('\n'), 400);
42
+ resultSet.results.push(result);
43
+ }
44
+ if (!isValid && resultSet.messages.length === 0) {
45
+ resultSet.results.map((r) => {
46
+ if (!r.isValid && r.ruleType === "type" && r.message) {
47
+ resultSet.messages.push(r.message);
48
+ }
49
+ });
50
+ }
51
+ if (isValid) {
52
+ if (jsonData) {
53
+ const dst = data;
54
+ data = mergeObjects(dst, jsonData);
55
+ } else {
56
+ c.req.valid(keys, value);
71
57
  }
72
- await next();
73
- };
74
- return handler;
58
+ }
59
+ }
60
+ if (!resultSet.hasError) {
61
+ Object.keys(data).map((key) => {
62
+ c.req.valid(key, data[key]);
63
+ });
64
+ }
65
+ if (options && options.done) {
66
+ const res = options.done(resultSet, c);
67
+ if (res) {
68
+ return res;
69
+ }
70
+ }
71
+ if (resultSet.hasError) {
72
+ return c.text(resultSet.messages.join("\n"), 400);
73
+ }
74
+ await next();
75
+ };
76
+ return handler;
75
77
  };
76
78
  function getValidatorList(schema) {
77
- const map = [];
78
- for (const [key, value] of Object.entries(schema)) {
79
- if (value instanceof VObjectBase) {
80
- const validators = value.getValidators();
81
- for (const validator of validators) {
82
- map.push([value.keys, validator]);
83
- }
84
- }
85
- else if (value instanceof VBase) {
86
- map.push([[key], value]);
87
- }
88
- else {
89
- const children = getValidatorList(value);
90
- for (const [keys, validator] of children) {
91
- map.push([[key, ...keys], validator]);
92
- }
93
- }
79
+ const map = [];
80
+ for (const [key, value] of Object.entries(schema)) {
81
+ if (value instanceof VObjectBase) {
82
+ const validators = value.getValidators();
83
+ for (const validator of validators) {
84
+ map.push([value.keys, validator]);
85
+ }
86
+ } else if (value instanceof VBase) {
87
+ map.push([[key], value]);
88
+ } else {
89
+ const children = getValidatorList(value);
90
+ for (const [keys, validator] of children) {
91
+ map.push([[key, ...keys], validator]);
92
+ }
94
93
  }
95
- return map;
94
+ }
95
+ return map;
96
96
  }
97
+ var getErrorResult = (e) => {
98
+ const result = {
99
+ isValid: false,
100
+ message: e.message,
101
+ target: "unknown",
102
+ key: null,
103
+ value: null,
104
+ ruleName: e.message,
105
+ ruleType: "value"
106
+ };
107
+ return result;
108
+ };
109
+ export {
110
+ validatorMiddleware
111
+ };
package/dist/request.js CHANGED
@@ -1,116 +1,110 @@
1
- import { parseBody } from './utils/body';
2
- import { parse } from './utils/cookie';
3
- import { getQueryStringFromURL } from './utils/url';
4
- export function extendRequestPrototype() {
5
- if (!!Request.prototype.param) {
6
- // already extended
7
- return;
1
+ // src/request.ts
2
+ import { parseBody } from "./utils/body.js";
3
+ import { parse } from "./utils/cookie.js";
4
+ import { getQueryStringFromURL } from "./utils/url.js";
5
+ function extendRequestPrototype() {
6
+ if (!!Request.prototype.param) {
7
+ return;
8
+ }
9
+ Request.prototype.param = function(key) {
10
+ if (this.paramData) {
11
+ if (key) {
12
+ return this.paramData[key];
13
+ } else {
14
+ return this.paramData;
15
+ }
8
16
  }
9
- Request.prototype.param = function (key) {
10
- if (this.paramData) {
11
- if (key) {
12
- return this.paramData[key];
13
- }
14
- else {
15
- return this.paramData;
16
- }
17
- }
18
- return null;
19
- };
20
- Request.prototype.header = function (name) {
21
- if (!this.headerData) {
22
- this.headerData = {};
23
- for (const [key, value] of this.headers) {
24
- this.headerData[key] = value;
25
- }
26
- }
27
- if (name) {
28
- return this.headerData[name.toLowerCase()];
29
- }
30
- else {
31
- return this.headerData;
32
- }
33
- };
34
- Request.prototype.query = function (key) {
35
- const queryString = getQueryStringFromURL(this.url);
36
- const searchParams = new URLSearchParams(queryString);
37
- if (!this.queryData) {
38
- this.queryData = {};
39
- for (const key of searchParams.keys()) {
40
- this.queryData[key] = searchParams.get(key) || '';
41
- }
42
- }
43
- if (key) {
44
- return this.queryData[key];
45
- }
46
- else {
47
- return this.queryData;
48
- }
49
- };
50
- Request.prototype.queries = function (key) {
51
- const queryString = getQueryStringFromURL(this.url);
52
- const searchParams = new URLSearchParams(queryString);
53
- if (key) {
54
- return searchParams.getAll(key);
55
- }
56
- else {
57
- const result = {};
58
- for (const key of searchParams.keys()) {
59
- result[key] = searchParams.getAll(key);
60
- }
61
- return result;
62
- }
63
- };
64
- Request.prototype.cookie = function (key) {
65
- const cookie = this.headers.get('Cookie') || '';
66
- const obj = parse(cookie);
67
- if (key) {
68
- const value = obj[key];
69
- return value;
70
- }
71
- else {
72
- return obj;
73
- }
74
- };
75
- Request.prototype.parseBody = async function () {
76
- // Cache the parsed body
77
- let body;
78
- if (!this.bodyData) {
79
- body = await parseBody(this);
80
- this.bodyData = body;
81
- }
82
- else {
83
- body = this.bodyData;
84
- }
85
- return body;
86
- };
87
- Request.prototype.json = async function () {
88
- // Cache the JSON body
89
- let jsonData;
90
- if (!this.jsonData) {
91
- jsonData = JSON.parse(await this.text());
92
- this.jsonData = jsonData;
93
- }
94
- else {
95
- jsonData = this.jsonData;
96
- }
97
- return jsonData;
98
- };
99
- Request.prototype.valid = function (keys, value) {
100
- var _a;
101
- if (!this.data) {
102
- this.data = {};
103
- }
104
- if (keys !== undefined) {
105
- if (typeof keys === 'string') {
106
- keys = [keys];
107
- }
108
- let data = this.data;
109
- for (let i = 0; i < keys.length - 1; i++) {
110
- data = data[_a = keys[i]] || (data[_a] = {});
111
- }
112
- data[keys[keys.length - 1]] = value;
113
- }
114
- return this.data;
115
- };
17
+ return null;
18
+ };
19
+ Request.prototype.header = function(name) {
20
+ if (!this.headerData) {
21
+ this.headerData = {};
22
+ for (const [key, value] of this.headers) {
23
+ this.headerData[key] = value;
24
+ }
25
+ }
26
+ if (name) {
27
+ return this.headerData[name.toLowerCase()];
28
+ } else {
29
+ return this.headerData;
30
+ }
31
+ };
32
+ Request.prototype.query = function(key) {
33
+ const queryString = getQueryStringFromURL(this.url);
34
+ const searchParams = new URLSearchParams(queryString);
35
+ if (!this.queryData) {
36
+ this.queryData = {};
37
+ for (const key2 of searchParams.keys()) {
38
+ this.queryData[key2] = searchParams.get(key2) || "";
39
+ }
40
+ }
41
+ if (key) {
42
+ return this.queryData[key];
43
+ } else {
44
+ return this.queryData;
45
+ }
46
+ };
47
+ Request.prototype.queries = function(key) {
48
+ const queryString = getQueryStringFromURL(this.url);
49
+ const searchParams = new URLSearchParams(queryString);
50
+ if (key) {
51
+ return searchParams.getAll(key);
52
+ } else {
53
+ const result = {};
54
+ for (const key2 of searchParams.keys()) {
55
+ result[key2] = searchParams.getAll(key2);
56
+ }
57
+ return result;
58
+ }
59
+ };
60
+ Request.prototype.cookie = function(key) {
61
+ const cookie = this.headers.get("Cookie") || "";
62
+ const obj = parse(cookie);
63
+ if (key) {
64
+ const value = obj[key];
65
+ return value;
66
+ } else {
67
+ return obj;
68
+ }
69
+ };
70
+ Request.prototype.parseBody = async function() {
71
+ let body;
72
+ if (!this.bodyData) {
73
+ body = await parseBody(this);
74
+ this.bodyData = body;
75
+ } else {
76
+ body = this.bodyData;
77
+ }
78
+ return body;
79
+ };
80
+ Request.prototype.json = async function() {
81
+ let jsonData;
82
+ if (!this.jsonData) {
83
+ jsonData = JSON.parse(await this.text());
84
+ this.jsonData = jsonData;
85
+ } else {
86
+ jsonData = this.jsonData;
87
+ }
88
+ return jsonData;
89
+ };
90
+ Request.prototype.valid = function(keys, value) {
91
+ var _a;
92
+ if (!this.data) {
93
+ this.data = {};
94
+ }
95
+ if (keys !== void 0) {
96
+ if (typeof keys === "string") {
97
+ keys = [keys];
98
+ }
99
+ let data = this.data;
100
+ for (let i = 0; i < keys.length - 1; i++) {
101
+ data = data[_a = keys[i]] || (data[_a] = {});
102
+ }
103
+ data[keys[keys.length - 1]] = value;
104
+ }
105
+ return this.data;
106
+ };
116
107
  }
108
+ export {
109
+ extendRequestPrototype
110
+ };
@@ -1 +1,5 @@
1
- export { RegExpRouter } from './router';
1
+ // src/router/reg-exp-router/index.ts
2
+ import { RegExpRouter } from "./router.js";
3
+ export {
4
+ RegExpRouter
5
+ };
@@ -1,104 +1,95 @@
1
- const LABEL_REG_EXP_STR = '[^/]+';
2
- const ONLY_WILDCARD_REG_EXP_STR = '.*';
3
- const TAIL_WILDCARD_REG_EXP_STR = '(?:|/.*)';
4
- export const PATH_ERROR = Symbol();
5
- /**
6
- * Sort order:
7
- * 1. literal
8
- * 2. special pattern (e.g. :label{[0-9]+})
9
- * 3. common label pattern (e.g. :label)
10
- * 4. wildcard
11
- */
1
+ // src/router/reg-exp-router/node.ts
2
+ var LABEL_REG_EXP_STR = "[^/]+";
3
+ var ONLY_WILDCARD_REG_EXP_STR = ".*";
4
+ var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
5
+ var PATH_ERROR = Symbol();
12
6
  function compareKey(a, b) {
13
- if (a.length === 1) {
14
- return b.length === 1 ? (a < b ? -1 : 1) : -1;
15
- }
16
- if (b.length === 1) {
17
- return 1;
18
- }
19
- // wildcard
20
- if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
21
- return 1;
22
- }
23
- else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
24
- return -1;
25
- }
26
- // label
27
- if (a === LABEL_REG_EXP_STR) {
28
- return 1;
29
- }
30
- else if (b === LABEL_REG_EXP_STR) {
31
- return -1;
32
- }
33
- return a.length === b.length ? (a < b ? -1 : 1) : b.length - a.length;
7
+ if (a.length === 1) {
8
+ return b.length === 1 ? a < b ? -1 : 1 : -1;
9
+ }
10
+ if (b.length === 1) {
11
+ return 1;
12
+ }
13
+ if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
14
+ return 1;
15
+ } else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
16
+ return -1;
17
+ }
18
+ if (a === LABEL_REG_EXP_STR) {
19
+ return 1;
20
+ } else if (b === LABEL_REG_EXP_STR) {
21
+ return -1;
22
+ }
23
+ return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
34
24
  }
35
- export class Node {
36
- constructor() {
37
- this.children = {};
25
+ var Node = class {
26
+ constructor() {
27
+ this.children = {};
28
+ }
29
+ insert(tokens, index, paramMap, context) {
30
+ if (tokens.length === 0) {
31
+ if (this.index !== void 0) {
32
+ throw PATH_ERROR;
33
+ }
34
+ this.index = index;
35
+ return;
38
36
  }
39
- insert(tokens, index, paramMap, context) {
40
- if (tokens.length === 0) {
41
- if (this.index !== undefined) {
42
- throw PATH_ERROR;
43
- }
44
- this.index = index;
45
- return;
37
+ const [token, ...restTokens] = tokens;
38
+ const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
39
+ let node;
40
+ if (pattern) {
41
+ const name = pattern[1];
42
+ const regexpStr = pattern[2] || LABEL_REG_EXP_STR;
43
+ node = this.children[regexpStr];
44
+ if (!node) {
45
+ if (Object.keys(this.children).some(
46
+ (k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
47
+ )) {
48
+ throw PATH_ERROR;
46
49
  }
47
- const [token, ...restTokens] = tokens;
48
- const pattern = token === '*'
49
- ? restTokens.length === 0
50
- ? ['', '', ONLY_WILDCARD_REG_EXP_STR] // '*' matches to all the trailing paths
51
- : ['', '', LABEL_REG_EXP_STR]
52
- : token === '/*'
53
- ? ['', '', TAIL_WILDCARD_REG_EXP_STR] // '/path/to/*' is /\/path\/to(?:|/.*)$
54
- : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
55
- let node;
56
- if (pattern) {
57
- const name = pattern[1];
58
- const regexpStr = pattern[2] || LABEL_REG_EXP_STR;
59
- node = this.children[regexpStr];
60
- if (!node) {
61
- if (Object.keys(this.children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
62
- throw PATH_ERROR;
63
- }
64
- node = this.children[regexpStr] = new Node();
65
- if (name !== '') {
66
- node.varIndex = context.varIndex++;
67
- }
68
- }
69
- if (name !== '') {
70
- if (paramMap.some((p) => p[0] === name)) {
71
- throw new Error('Duplicate param name');
72
- }
73
- paramMap.push([name, node.varIndex]);
74
- }
50
+ node = this.children[regexpStr] = new Node();
51
+ if (name !== "") {
52
+ node.varIndex = context.varIndex++;
75
53
  }
76
- else {
77
- node = this.children[token];
78
- if (!node) {
79
- if (Object.keys(this.children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
80
- throw PATH_ERROR;
81
- }
82
- node = this.children[token] = new Node();
83
- }
54
+ }
55
+ if (name !== "") {
56
+ if (paramMap.some((p) => p[0] === name)) {
57
+ throw new Error("Duplicate param name");
84
58
  }
85
- node.insert(restTokens, index, paramMap, context);
86
- }
87
- buildRegExpStr() {
88
- const childKeys = Object.keys(this.children).sort(compareKey);
89
- const strList = childKeys.map((k) => {
90
- const c = this.children[k];
91
- return (typeof c.varIndex === 'number' ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr();
92
- });
93
- if (typeof this.index === 'number') {
94
- strList.unshift(`#${this.index}`);
59
+ paramMap.push([name, node.varIndex]);
60
+ }
61
+ } else {
62
+ node = this.children[token];
63
+ if (!node) {
64
+ if (Object.keys(this.children).some(
65
+ (k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
66
+ )) {
67
+ throw PATH_ERROR;
95
68
  }
96
- if (strList.length === 0) {
97
- return '';
98
- }
99
- if (strList.length === 1) {
100
- return strList[0];
101
- }
102
- return '(?:' + strList.join('|') + ')';
69
+ node = this.children[token] = new Node();
70
+ }
103
71
  }
104
- }
72
+ node.insert(restTokens, index, paramMap, context);
73
+ }
74
+ buildRegExpStr() {
75
+ const childKeys = Object.keys(this.children).sort(compareKey);
76
+ const strList = childKeys.map((k) => {
77
+ const c = this.children[k];
78
+ return (typeof c.varIndex === "number" ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr();
79
+ });
80
+ if (typeof this.index === "number") {
81
+ strList.unshift(`#${this.index}`);
82
+ }
83
+ if (strList.length === 0) {
84
+ return "";
85
+ }
86
+ if (strList.length === 1) {
87
+ return strList[0];
88
+ }
89
+ return "(?:" + strList.join("|") + ")";
90
+ }
91
+ };
92
+ export {
93
+ Node,
94
+ PATH_ERROR
95
+ };