@midwayjs/core 3.3.6 → 3.4.0-beta.11
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.
- package/LICENSE +21 -0
- package/dist/baseFramework.d.ts +3 -0
- package/dist/baseFramework.js +32 -13
- package/dist/common/asyncContextManager.d.ts +61 -0
- package/dist/common/asyncContextManager.js +63 -0
- package/dist/common/dataSourceManager.d.ts +8 -1
- package/dist/common/dataSourceManager.js +61 -3
- package/dist/common/fileDetector.d.ts +1 -0
- package/dist/common/fileDetector.js +20 -0
- package/dist/common/filterManager.d.ts +1 -0
- package/dist/common/filterManager.js +37 -5
- package/dist/common/serviceFactory.js +2 -1
- package/dist/common/webGenerator.d.ts +4 -3
- package/dist/common/webGenerator.js +24 -15
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +3 -0
- package/dist/context/container.js +3 -0
- package/dist/error/base.d.ts +1 -1
- package/dist/error/base.js +6 -1
- package/dist/error/framework.d.ts +8 -0
- package/dist/error/framework.js +15 -1
- package/dist/error/http.js +16 -16
- package/dist/index.d.ts +5 -3
- package/dist/index.js +11 -3
- package/dist/interface.d.ts +14 -4
- package/dist/interface.js +3 -1
- package/dist/service/configService.js +7 -4
- package/dist/service/middlewareService.js +13 -5
- package/dist/service/slsFunctionService.d.ts +25 -0
- package/dist/service/slsFunctionService.js +242 -0
- package/dist/{common/webRouterCollector.d.ts → service/webRouterService.d.ts} +87 -36
- package/dist/{common/webRouterCollector.js → service/webRouterService.js} +121 -116
- package/dist/setup.js +3 -0
- package/dist/util/contextUtil.d.ts +2 -0
- package/dist/util/contextUtil.js +6 -1
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +23 -2
- package/dist/util/pathToRegexp.d.ts +113 -7
- package/dist/util/pathToRegexp.js +326 -205
- package/package.json +3 -3
- package/CHANGELOG.md +0 -2551
- package/dist/common/triggerCollector.d.ts +0 -8
- package/dist/common/triggerCollector.js +0 -37
|
@@ -1,250 +1,382 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* this file fork from path-to-regexp package v1.8.0
|
|
4
|
-
*/
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.PathToRegexpUtil = void 0;
|
|
7
4
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @type {RegExp}
|
|
5
|
+
* Tokenize input string.
|
|
11
6
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
function lexer(str) {
|
|
8
|
+
const tokens = [];
|
|
9
|
+
let i = 0;
|
|
10
|
+
while (i < str.length) {
|
|
11
|
+
const char = str[i];
|
|
12
|
+
if (char === '*' || char === '+' || char === '?') {
|
|
13
|
+
tokens.push({ type: 'MODIFIER', index: i, value: str[i++] });
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (char === '\\') {
|
|
17
|
+
tokens.push({ type: 'ESCAPED_CHAR', index: i++, value: str[i++] });
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (char === '{') {
|
|
21
|
+
tokens.push({ type: 'OPEN', index: i, value: str[i++] });
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (char === '}') {
|
|
25
|
+
tokens.push({ type: 'CLOSE', index: i, value: str[i++] });
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (char === ':') {
|
|
29
|
+
let name = '';
|
|
30
|
+
let j = i + 1;
|
|
31
|
+
while (j < str.length) {
|
|
32
|
+
const code = str.charCodeAt(j);
|
|
33
|
+
if (
|
|
34
|
+
// `0-9`
|
|
35
|
+
(code >= 48 && code <= 57) ||
|
|
36
|
+
// `A-Z`
|
|
37
|
+
(code >= 65 && code <= 90) ||
|
|
38
|
+
// `a-z`
|
|
39
|
+
(code >= 97 && code <= 122) ||
|
|
40
|
+
// `_`
|
|
41
|
+
code === 95) {
|
|
42
|
+
name += str[j++];
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
if (!name)
|
|
48
|
+
throw new TypeError(`Missing parameter name at ${i}`);
|
|
49
|
+
tokens.push({ type: 'NAME', index: i, value: name });
|
|
50
|
+
i = j;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (char === '(') {
|
|
54
|
+
let count = 1;
|
|
55
|
+
let pattern = '';
|
|
56
|
+
let j = i + 1;
|
|
57
|
+
if (str[j] === '?') {
|
|
58
|
+
throw new TypeError(`Pattern cannot start with "?" at ${j}`);
|
|
59
|
+
}
|
|
60
|
+
while (j < str.length) {
|
|
61
|
+
if (str[j] === '\\') {
|
|
62
|
+
pattern += str[j++] + str[j++];
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (str[j] === ')') {
|
|
66
|
+
count--;
|
|
67
|
+
if (count === 0) {
|
|
68
|
+
j++;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (str[j] === '(') {
|
|
73
|
+
count++;
|
|
74
|
+
if (str[j + 1] !== '?') {
|
|
75
|
+
throw new TypeError(`Capturing groups are not allowed at ${j}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
pattern += str[j++];
|
|
79
|
+
}
|
|
80
|
+
if (count)
|
|
81
|
+
throw new TypeError(`Unbalanced pattern at ${i}`);
|
|
82
|
+
if (!pattern)
|
|
83
|
+
throw new TypeError(`Missing pattern at ${i}`);
|
|
84
|
+
tokens.push({ type: 'PATTERN', index: i, value: pattern });
|
|
85
|
+
i = j;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
tokens.push({ type: 'CHAR', index: i, value: str[i++] });
|
|
89
|
+
}
|
|
90
|
+
tokens.push({ type: 'END', index: i, value: '' });
|
|
91
|
+
return tokens;
|
|
92
|
+
}
|
|
24
93
|
/**
|
|
25
94
|
* Parse a string for the raw tokens.
|
|
26
|
-
*
|
|
27
|
-
* @param {string} str
|
|
28
|
-
* @param {Object=} options
|
|
29
|
-
* @return {!Array}
|
|
30
95
|
*/
|
|
31
|
-
function parse(str, options) {
|
|
32
|
-
const tokens =
|
|
96
|
+
function parse(str, options = {}) {
|
|
97
|
+
const tokens = lexer(str);
|
|
98
|
+
const { prefixes = './' } = options;
|
|
99
|
+
const defaultPattern = `[^${escapeString(options.delimiter || '/#?')}]+?`;
|
|
100
|
+
const result = [];
|
|
33
101
|
let key = 0;
|
|
34
|
-
let
|
|
102
|
+
let i = 0;
|
|
35
103
|
let path = '';
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
104
|
+
const tryConsume = (type) => {
|
|
105
|
+
if (i < tokens.length && tokens[i].type === type)
|
|
106
|
+
return tokens[i++].value;
|
|
107
|
+
};
|
|
108
|
+
const mustConsume = (type) => {
|
|
109
|
+
const value = tryConsume(type);
|
|
110
|
+
if (value !== undefined)
|
|
111
|
+
return value;
|
|
112
|
+
const { type: nextType, index } = tokens[i];
|
|
113
|
+
throw new TypeError(`Unexpected ${nextType} at ${index}, expected ${type}`);
|
|
114
|
+
};
|
|
115
|
+
const consumeText = () => {
|
|
116
|
+
let result = '';
|
|
117
|
+
let value;
|
|
118
|
+
while ((value = tryConsume('CHAR') || tryConsume('ESCAPED_CHAR'))) {
|
|
119
|
+
result += value;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
};
|
|
123
|
+
while (i < tokens.length) {
|
|
124
|
+
const char = tryConsume('CHAR');
|
|
125
|
+
const name = tryConsume('NAME');
|
|
126
|
+
const pattern = tryConsume('PATTERN');
|
|
127
|
+
if (name || pattern) {
|
|
128
|
+
let prefix = char || '';
|
|
129
|
+
if (prefixes.indexOf(prefix) === -1) {
|
|
130
|
+
path += prefix;
|
|
131
|
+
prefix = '';
|
|
132
|
+
}
|
|
133
|
+
if (path) {
|
|
134
|
+
result.push(path);
|
|
135
|
+
path = '';
|
|
136
|
+
}
|
|
137
|
+
result.push({
|
|
138
|
+
name: name || key++,
|
|
139
|
+
prefix,
|
|
140
|
+
suffix: '',
|
|
141
|
+
pattern: pattern || defaultPattern,
|
|
142
|
+
modifier: tryConsume('MODIFIER') || '',
|
|
143
|
+
});
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const value = char || tryConsume('ESCAPED_CHAR');
|
|
147
|
+
if (value) {
|
|
148
|
+
path += value;
|
|
47
149
|
continue;
|
|
48
150
|
}
|
|
49
|
-
const next = str[index];
|
|
50
|
-
const prefix = res[2];
|
|
51
|
-
const name = res[3];
|
|
52
|
-
const capture = res[4];
|
|
53
|
-
const group = res[5];
|
|
54
|
-
const modifier = res[6];
|
|
55
|
-
const asterisk = res[7];
|
|
56
|
-
// Push the current path onto the tokens.
|
|
57
151
|
if (path) {
|
|
58
|
-
|
|
152
|
+
result.push(path);
|
|
59
153
|
path = '';
|
|
60
154
|
}
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
: '[^' + escapeString(delimiter) + ']+?',
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
// Match any characters still remaining.
|
|
82
|
-
if (index < str.length) {
|
|
83
|
-
path += str.slice(index);
|
|
84
|
-
}
|
|
85
|
-
// If the path exists, push it onto the end.
|
|
86
|
-
if (path) {
|
|
87
|
-
tokens.push(path);
|
|
155
|
+
const open = tryConsume('OPEN');
|
|
156
|
+
if (open) {
|
|
157
|
+
const prefix = consumeText();
|
|
158
|
+
const name = tryConsume('NAME') || '';
|
|
159
|
+
const pattern = tryConsume('PATTERN') || '';
|
|
160
|
+
const suffix = consumeText();
|
|
161
|
+
mustConsume('CLOSE');
|
|
162
|
+
result.push({
|
|
163
|
+
name: name || (pattern ? key++ : ''),
|
|
164
|
+
pattern: name && !pattern ? defaultPattern : pattern,
|
|
165
|
+
prefix,
|
|
166
|
+
suffix,
|
|
167
|
+
modifier: tryConsume('MODIFIER') || '',
|
|
168
|
+
});
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
mustConsume('END');
|
|
88
172
|
}
|
|
89
|
-
return
|
|
173
|
+
return result;
|
|
90
174
|
}
|
|
91
175
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @param {string} str
|
|
95
|
-
* @return {string}
|
|
176
|
+
* Compile a string to a template function for the path.
|
|
96
177
|
*/
|
|
97
|
-
function
|
|
98
|
-
return
|
|
178
|
+
function compile(str, options) {
|
|
179
|
+
return tokensToFunction(parse(str, options), options);
|
|
99
180
|
}
|
|
100
181
|
/**
|
|
101
|
-
*
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
182
|
+
* Expose a method for transforming tokens into the path function.
|
|
183
|
+
*/
|
|
184
|
+
function tokensToFunction(tokens, options = {}) {
|
|
185
|
+
const reFlags = flags(options);
|
|
186
|
+
const { encode = (x) => x, validate = true } = options;
|
|
187
|
+
// Compile all the tokens into regexps.
|
|
188
|
+
const matches = tokens.map(token => {
|
|
189
|
+
if (typeof token === 'object') {
|
|
190
|
+
return new RegExp(`^(?:${token.pattern})$`, reFlags);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return (data) => {
|
|
194
|
+
let path = '';
|
|
195
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
196
|
+
const token = tokens[i];
|
|
197
|
+
if (typeof token === 'string') {
|
|
198
|
+
path += token;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const value = data ? data[token.name] : undefined;
|
|
202
|
+
const optional = token.modifier === '?' || token.modifier === '*';
|
|
203
|
+
const repeat = token.modifier === '*' || token.modifier === '+';
|
|
204
|
+
if (Array.isArray(value)) {
|
|
205
|
+
if (!repeat) {
|
|
206
|
+
throw new TypeError(`Expected "${token.name}" to not repeat, but got an array`);
|
|
207
|
+
}
|
|
208
|
+
if (value.length === 0) {
|
|
209
|
+
if (optional)
|
|
210
|
+
continue;
|
|
211
|
+
throw new TypeError(`Expected "${token.name}" to not be empty`);
|
|
212
|
+
}
|
|
213
|
+
for (let j = 0; j < value.length; j++) {
|
|
214
|
+
const segment = encode(value[j], token);
|
|
215
|
+
if (validate && !matches[i].test(segment)) {
|
|
216
|
+
throw new TypeError(`Expected all "${token.name}" to match "${token.pattern}", but got "${segment}"`);
|
|
217
|
+
}
|
|
218
|
+
path += token.prefix + segment + token.suffix;
|
|
219
|
+
}
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
223
|
+
const segment = encode(String(value), token);
|
|
224
|
+
if (validate && !matches[i].test(segment)) {
|
|
225
|
+
throw new TypeError(`Expected "${token.name}" to match "${token.pattern}", but got "${segment}"`);
|
|
226
|
+
}
|
|
227
|
+
path += token.prefix + segment + token.suffix;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (optional)
|
|
231
|
+
continue;
|
|
232
|
+
const typeOfMessage = repeat ? 'an array' : 'a string';
|
|
233
|
+
throw new TypeError(`Expected "${token.name}" to be ${typeOfMessage}`);
|
|
234
|
+
}
|
|
235
|
+
return path;
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Create path match function from `path-to-regexp` spec.
|
|
105
240
|
*/
|
|
106
|
-
function
|
|
107
|
-
|
|
241
|
+
function match(str, options) {
|
|
242
|
+
const keys = [];
|
|
243
|
+
const re = toRegexp(str, keys, options);
|
|
244
|
+
return regexpToFunction(re, keys, options);
|
|
108
245
|
}
|
|
109
246
|
/**
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
247
|
+
* Create a path match function from `path-to-regexp` output.
|
|
248
|
+
*/
|
|
249
|
+
function regexpToFunction(re, keys, options = {}) {
|
|
250
|
+
const { decode = (x) => x } = options;
|
|
251
|
+
return function (pathname) {
|
|
252
|
+
const m = re.exec(pathname);
|
|
253
|
+
if (!m)
|
|
254
|
+
return false;
|
|
255
|
+
const { 0: path, index } = m;
|
|
256
|
+
const params = Object.create(null);
|
|
257
|
+
for (let i = 1; i < m.length; i++) {
|
|
258
|
+
if (m[i] === undefined)
|
|
259
|
+
continue;
|
|
260
|
+
const key = keys[i - 1];
|
|
261
|
+
if (key.modifier === '*' || key.modifier === '+') {
|
|
262
|
+
params[key.name] = m[i].split(key.prefix + key.suffix).map(value => {
|
|
263
|
+
return decode(value, key);
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
params[key.name] = decode(m[i], key);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return { path, index, params };
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Escape a regular expression string.
|
|
115
275
|
*/
|
|
116
|
-
function
|
|
117
|
-
|
|
118
|
-
return re;
|
|
276
|
+
function escapeString(str) {
|
|
277
|
+
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1');
|
|
119
278
|
}
|
|
120
279
|
/**
|
|
121
280
|
* Get the flags for a regexp from the options.
|
|
122
|
-
*
|
|
123
|
-
* @param {Object} options
|
|
124
|
-
* @return {string}
|
|
125
281
|
*/
|
|
126
282
|
function flags(options) {
|
|
127
283
|
return options && options.sensitive ? '' : 'i';
|
|
128
284
|
}
|
|
129
285
|
/**
|
|
130
286
|
* Pull out keys from a regexp.
|
|
131
|
-
*
|
|
132
|
-
* @param {!RegExp} path
|
|
133
|
-
* @param {!Array} keys
|
|
134
|
-
* @return {!RegExp}
|
|
135
287
|
*/
|
|
136
288
|
function regexpToRegexp(path, keys) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
289
|
+
if (!keys)
|
|
290
|
+
return path;
|
|
291
|
+
const groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
|
|
292
|
+
let index = 0;
|
|
293
|
+
let execResult = groupsRegex.exec(path.source);
|
|
294
|
+
while (execResult) {
|
|
295
|
+
keys.push({
|
|
296
|
+
// Use parenthesized substring match if available, index otherwise
|
|
297
|
+
name: execResult[1] || index++,
|
|
298
|
+
prefix: '',
|
|
299
|
+
suffix: '',
|
|
300
|
+
modifier: '',
|
|
301
|
+
pattern: '',
|
|
302
|
+
});
|
|
303
|
+
execResult = groupsRegex.exec(path.source);
|
|
152
304
|
}
|
|
153
|
-
return
|
|
305
|
+
return path;
|
|
154
306
|
}
|
|
155
307
|
/**
|
|
156
308
|
* Transform an array into a regexp.
|
|
157
|
-
*
|
|
158
|
-
* @param {!Array} path
|
|
159
|
-
* @param {Array} keys
|
|
160
|
-
* @param {!Object} options
|
|
161
|
-
* @return {!RegExp}
|
|
162
309
|
*/
|
|
163
|
-
function arrayToRegexp(
|
|
164
|
-
const parts =
|
|
165
|
-
|
|
166
|
-
parts.push(pathToRegexp(path[i], keys, options).source);
|
|
167
|
-
}
|
|
168
|
-
const regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options));
|
|
169
|
-
return attachKeys(regexp, keys);
|
|
310
|
+
function arrayToRegexp(paths, keys, options) {
|
|
311
|
+
const parts = paths.map(path => toRegexp(path, keys, options).source);
|
|
312
|
+
return new RegExp(`(?:${parts.join('|')})`, flags(options));
|
|
170
313
|
}
|
|
171
314
|
/**
|
|
172
315
|
* Create a path regexp from string input.
|
|
173
|
-
*
|
|
174
|
-
* @param {string} path
|
|
175
|
-
* @param {!Array} keys
|
|
176
|
-
* @param {!Object} options
|
|
177
|
-
* @return {!RegExp}
|
|
178
316
|
*/
|
|
179
317
|
function stringToRegexp(path, keys, options) {
|
|
180
|
-
return
|
|
318
|
+
return tokensToRegexp(parse(path, options), keys, options);
|
|
181
319
|
}
|
|
182
320
|
/**
|
|
183
321
|
* Expose a function for taking tokens and returning a RegExp.
|
|
184
|
-
*
|
|
185
|
-
* @param {!Array} tokens
|
|
186
|
-
* @param {(Array|Object)=} keys
|
|
187
|
-
* @param {Object=} options
|
|
188
|
-
* @return {!RegExp}
|
|
189
322
|
*/
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
options = options || {};
|
|
196
|
-
const strict = options.strict;
|
|
197
|
-
const end = options.end !== false;
|
|
198
|
-
let route = '';
|
|
323
|
+
function tokensToRegexp(tokens, keys, options = {}) {
|
|
324
|
+
const { strict = false, start = true, end = true, encode = (x) => x, delimiter = '/#?', endsWith = '', } = options;
|
|
325
|
+
const endsWithRe = `[${escapeString(endsWith)}]|$`;
|
|
326
|
+
const delimiterRe = `[${escapeString(delimiter)}]`;
|
|
327
|
+
let route = start ? '^' : '';
|
|
199
328
|
// Iterate over the tokens and create our regexp string.
|
|
200
|
-
for (
|
|
201
|
-
const token = tokens[i];
|
|
329
|
+
for (const token of tokens) {
|
|
202
330
|
if (typeof token === 'string') {
|
|
203
|
-
route += escapeString(token);
|
|
331
|
+
route += escapeString(encode(token));
|
|
204
332
|
}
|
|
205
333
|
else {
|
|
206
|
-
const prefix = escapeString(token.prefix);
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
334
|
+
const prefix = escapeString(encode(token.prefix));
|
|
335
|
+
const suffix = escapeString(encode(token.suffix));
|
|
336
|
+
if (token.pattern) {
|
|
337
|
+
if (keys)
|
|
338
|
+
keys.push(token);
|
|
339
|
+
if (prefix || suffix) {
|
|
340
|
+
if (token.modifier === '+' || token.modifier === '*') {
|
|
341
|
+
const mod = token.modifier === '*' ? '?' : '';
|
|
342
|
+
route += `(?:${prefix}((?:${token.pattern})(?:${suffix}${prefix}(?:${token.pattern}))*)${suffix})${mod}`;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
route += `(?:${prefix}(${token.pattern})${suffix})${token.modifier}`;
|
|
346
|
+
}
|
|
215
347
|
}
|
|
216
348
|
else {
|
|
217
|
-
|
|
349
|
+
if (token.modifier === '+' || token.modifier === '*') {
|
|
350
|
+
route += `((?:${token.pattern})${token.modifier})`;
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
route += `(${token.pattern})${token.modifier}`;
|
|
354
|
+
}
|
|
218
355
|
}
|
|
219
356
|
}
|
|
220
357
|
else {
|
|
221
|
-
|
|
358
|
+
route += `(?:${prefix}${suffix})${token.modifier}`;
|
|
222
359
|
}
|
|
223
|
-
route += capture;
|
|
224
360
|
}
|
|
225
361
|
}
|
|
226
|
-
const delimiter = escapeString(options.delimiter || '/');
|
|
227
|
-
const endsWithDelimiter = route.slice(-delimiter.length) === delimiter;
|
|
228
|
-
// In non-strict mode we allow a slash at the end of match. If the path to
|
|
229
|
-
// match already ends with a slash, we remove it for consistency. The slash
|
|
230
|
-
// is valid at the end of a path match, not in the middle. This is important
|
|
231
|
-
// in non-ending mode, where "/test/" shouldn't match "/test//route".
|
|
232
|
-
if (!strict) {
|
|
233
|
-
route =
|
|
234
|
-
(endsWithDelimiter ? route.slice(0, -delimiter.length) : route) +
|
|
235
|
-
'(?:' +
|
|
236
|
-
delimiter +
|
|
237
|
-
'(?=$))?';
|
|
238
|
-
}
|
|
239
362
|
if (end) {
|
|
240
|
-
|
|
363
|
+
if (!strict)
|
|
364
|
+
route += `${delimiterRe}?`;
|
|
365
|
+
route += !options.endsWith ? '$' : `(?=${endsWithRe})`;
|
|
241
366
|
}
|
|
242
367
|
else {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
368
|
+
const endToken = tokens[tokens.length - 1];
|
|
369
|
+
const isEndDelimited = typeof endToken === 'string'
|
|
370
|
+
? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1
|
|
371
|
+
: endToken === undefined;
|
|
372
|
+
if (!strict) {
|
|
373
|
+
route += `(?:${delimiterRe}(?=${endsWithRe}))?`;
|
|
374
|
+
}
|
|
375
|
+
if (!isEndDelimited) {
|
|
376
|
+
route += `(?=${delimiterRe}|${endsWithRe})`;
|
|
377
|
+
}
|
|
246
378
|
}
|
|
247
|
-
return
|
|
379
|
+
return new RegExp(route, flags(options));
|
|
248
380
|
}
|
|
249
381
|
/**
|
|
250
382
|
* Normalize the given path string, returning a regular expression.
|
|
@@ -252,29 +384,18 @@ function tokensToRegExp(tokens, keys, options) {
|
|
|
252
384
|
* An empty array can be passed in for the keys, which will hold the
|
|
253
385
|
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
|
254
386
|
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
|
255
|
-
*
|
|
256
|
-
* @param {(string|RegExp|Array)} path
|
|
257
|
-
* @param {(Array|Object)=} keys
|
|
258
|
-
* @param {Object=} options
|
|
259
|
-
* @return {!RegExp}
|
|
260
387
|
*/
|
|
261
|
-
function
|
|
262
|
-
if (
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (path instanceof RegExp) {
|
|
268
|
-
return regexpToRegexp(path, /** @type {!Array} */ keys);
|
|
269
|
-
}
|
|
270
|
-
if (Array.isArray(path)) {
|
|
271
|
-
return arrayToRegexp(
|
|
272
|
-
/** @type {!Array} */ path,
|
|
273
|
-
/** @type {!Array} */ keys, options);
|
|
274
|
-
}
|
|
275
|
-
return stringToRegexp(
|
|
276
|
-
/** @type {string} */ path,
|
|
277
|
-
/** @type {!Array} */ keys, options);
|
|
388
|
+
function toRegexp(path, keys, options) {
|
|
389
|
+
if (path instanceof RegExp)
|
|
390
|
+
return regexpToRegexp(path, keys);
|
|
391
|
+
if (Array.isArray(path))
|
|
392
|
+
return arrayToRegexp(path, keys, options);
|
|
393
|
+
return stringToRegexp(path, keys, options);
|
|
278
394
|
}
|
|
279
|
-
exports.
|
|
395
|
+
exports.PathToRegexpUtil = {
|
|
396
|
+
toRegexp,
|
|
397
|
+
compile,
|
|
398
|
+
parse,
|
|
399
|
+
match,
|
|
400
|
+
};
|
|
280
401
|
//# sourceMappingURL=pathToRegexp.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0-beta.11",
|
|
4
4
|
"description": "midway core",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@midwayjs/decorator": "^3.
|
|
24
|
+
"@midwayjs/decorator": "^3.4.0-beta.11",
|
|
25
25
|
"koa": "2.13.4",
|
|
26
26
|
"midway-test-component": "*",
|
|
27
27
|
"mm": "3.2.0",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=12"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "b1c7a439b0df37d3e381cd182ea3b9e74323107b"
|
|
49
49
|
}
|