deno.land / x / deno@v1.28.2 / cli / bench / testdata / npm / hono / dist / router / reg-exp-router / router.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.RegExpRouter = void 0;const router_1 = require("../../router");const trie_1 = require("./trie");const emptyParam = {};const nullMatcher = [/^$/, []];function initHint(path) { const components = path.match(/\/(?::\w+{[^}]+}|[^\/]*)/g) || []; let componentsLength = components.length; const paramIndexList = []; const regExpComponents = []; const namedParams = []; for (let i = 0, len = components.length; i < len; i++) { if (i === len - 1 && components[i] === '/*') { componentsLength--; break; } const m = components[i].match(/^\/:(\w+)({[^}]+})?/); if (m) { namedParams.push([i, m[1], m[2] || '[^/]+']); regExpComponents[i] = m[2] || true; } else if (components[i] === '/*') { regExpComponents[i] = true; } else { regExpComponents[i] = components[i]; } if (/\/(?::|\*)/.test(components[i])) { paramIndexList.push(i); } } return { components, regExpComponents, componentsLength, endWithWildcard: path.endsWith('*'), paramIndexList, namedParams, maybeHandler: true, };}function compareRoute(a, b) { if (a.path === '*') { return 1; } let i = 0; const len = a.hint.regExpComponents.length; for (; i < len; i++) { if (a.hint.regExpComponents[i] !== b.hint.regExpComponents[i]) { if (a.hint.regExpComponents[i] === true) { break; } return 0; } } // may be ambiguous for (; i < len; i++) { if (a.hint.regExpComponents[i] !== true && a.hint.regExpComponents[i] !== b.hint.regExpComponents[i]) { return 2; } } return i === b.hint.regExpComponents.length || a.hint.endWithWildcard ? 1 : 0;}function compareHandler(a, b) { return a.index - b.index;}function getSortedHandlers(handlers) { return [...handlers].sort(compareHandler).map((h) => h.handler);}function buildMatcherFromPreprocessedRoutes(routes, hasAmbiguous = false) { const trie = new trie_1.Trie({ reverse: hasAmbiguous }); const handlers = []; if (routes.length === 0) { return nullMatcher; } for (let i = 0, len = routes.length; i < len; i++) { const paramMap = trie.insert(routes[i].path, i); handlers[i] = [ [...routes[i].middleware, ...routes[i].handlers], Object.keys(paramMap).length !== 0 ? paramMap : null, ]; if (!hasAmbiguous) { handlers[i][0] = getSortedHandlers(handlers[i][0]); } } const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp(); for (let i = 0, len = handlers.length; i < len; i++) { const paramMap = handlers[i][1]; if (paramMap) { for (let j = 0, len = paramMap.length; j < len; j++) { paramMap[j][1] = paramReplacementMap[paramMap[j][1]]; const aliasTo = routes[i].paramAliasMap[paramMap[j][0]]; if (aliasTo) { for (let k = 0, len = aliasTo.length; k < len; k++) { paramMap.push([aliasTo[k], paramMap[j][1]]); } } } } } const handlerMap = []; // using `in` because indexReplacementMap is a sparse array for (const i in indexReplacementMap) { handlerMap[i] = handlers[indexReplacementMap[i]]; } return [regexp, handlerMap];}function verifyDuplicateParam(routes) { const nameMap = {}; for (let i = 0, len = routes.length; i < len; i++) { const route = routes[i]; for (let k = 0, len = route.hint.namedParams.length; k < len; k++) { const [index, name] = route.hint.namedParams[k]; if (name in nameMap && index !== nameMap[name]) { return false; } else { nameMap[name] = index; } } const paramAliasMap = route.paramAliasMap; const paramAliasMapKeys = Object.keys(paramAliasMap); for (let k = 0, len = paramAliasMapKeys.length; k < len; k++) { const aliasFrom = paramAliasMapKeys[k]; for (let l = 0, len = paramAliasMap[aliasFrom].length; l < len; l++) { const aliasTo = paramAliasMap[aliasFrom][l]; const index = nameMap[aliasFrom]; if (aliasTo in nameMap && index !== nameMap[aliasTo]) { return false; } else { nameMap[aliasTo] = index; } } } } return true;}class RegExpRouter { constructor() { this.routeData = { index: 0, routes: [], methods: new Set() }; } add(method, path, handler) { if (!this.routeData) { throw new Error('Can not add a route since the matcher is already built.'); } this.routeData.index++; const { index, routes, methods } = this.routeData; if (path === '/*') { path = '*'; } const hint = initHint(path); const handlerWithSortIndex = { index, handler, }; for (let i = 0, len = routes.length; i < len; i++) { if (routes[i].method === method && routes[i].path === path) { routes[i].handlers.push(handlerWithSortIndex); return; } } methods.add(method); routes.push({ method, path, hint, handlers: [handlerWithSortIndex], middleware: [], paramAliasMap: {}, }); } match(method, path) { const [primaryMatchers, secondaryMatchers, hasAmbiguous] = this.buildAllMatchers(); this.match = hasAmbiguous ? (method, path) => { const matcher = (primaryMatchers[method] || primaryMatchers[router_1.METHOD_NAME_ALL]); let match = path.match(matcher[0]); if (!match) { // do not support secondary matchers here. return null; } const params = {}; const handlers = new Set(); let regExpSrc = matcher[0].source; while (match) { let index = match.indexOf('', 1); for (;;) { const [handler, paramMap] = matcher[1][index]; if (paramMap) { for (let i = 0, len = paramMap.length; i < len; i++) { params[paramMap[i][0]] = match[paramMap[i][1]]; } } for (let i = 0, len = handler.length; i < len; i++) { handlers.add(handler[i]); } const newIndex = match.indexOf('', index + 1); if (newIndex === -1) { break; } index = newIndex; } regExpSrc = regExpSrc.replace(new RegExp(`((?:(?:\\(\\?:|.)*?\\([^)]*\\)){${index - 1}}.*?)\\(\\)`), '$1(^)'); match = path.match(new RegExp(regExpSrc)); } return { handlers: getSortedHandlers(handlers.values()), params }; } : (method, path) => { let matcher = (primaryMatchers[method] || primaryMatchers[router_1.METHOD_NAME_ALL]); let match = path.match(matcher[0]); if (!match) { const matchers = secondaryMatchers[method] || secondaryMatchers[router_1.METHOD_NAME_ALL]; for (let i = 0, len = matchers.length; i < len && !match; i++) { matcher = matchers[i]; match = path.match(matcher[0]); } if (!match) { return null; } } const index = match.indexOf('', 1); const [handlers, paramMap] = matcher[1][index]; if (!paramMap) { return { handlers, params: emptyParam }; } const params = {}; for (let i = 0, len = paramMap.length; i < len; i++) { params[paramMap[i][0]] = match[paramMap[i][1]]; } return { handlers, params }; }; return this.match(method, path); } buildAllMatchers() { // @ts-ignore this.routeData.routes.sort(({ hint: a }, { hint: b }) => { if (a.componentsLength !== b.componentsLength) { return a.componentsLength - b.componentsLength; } for (let i = 0, len = Math.min(a.paramIndexList.length, b.paramIndexList.length) + 1; i < len; i++) { if (a.paramIndexList[i] !== b.paramIndexList[i]) { if (a.paramIndexList[i] === undefined) { return -1; } else if (b.paramIndexList[i] === undefined) { return 1; } else { return a.paramIndexList[i] - b.paramIndexList[i]; } } } if (a.endWithWildcard !== b.endWithWildcard) { return a.endWithWildcard ? -1 : 1; } return 0; }); const primaryMatchers = {}; const secondaryMatchers = {}; let hasAmbiguous = false; // @ts-ignore this.routeData.methods.forEach((method) => { let _hasAmbiguous; [primaryMatchers[method], secondaryMatchers[method], _hasAmbiguous] = this.buildMatcher(method); hasAmbiguous = hasAmbiguous || _hasAmbiguous; }); primaryMatchers[router_1.METHOD_NAME_ALL] || (primaryMatchers[router_1.METHOD_NAME_ALL] = nullMatcher); secondaryMatchers[router_1.METHOD_NAME_ALL] || (secondaryMatchers[router_1.METHOD_NAME_ALL] = []); delete this.routeData; // to reduce memory usage return [primaryMatchers, secondaryMatchers, hasAmbiguous]; } buildMatcher(method) { var _a, _b; let hasAmbiguous = false; const targetMethods = new Set([method, router_1.METHOD_NAME_ALL]); // @ts-ignore const routes = this.routeData.routes.filter(({ method }) => targetMethods.has(method)); // Reset temporary data per method for (let i = 0, len = routes.length; i < len; i++) { routes[i].middleware = []; routes[i].paramAliasMap = {}; } // preprocess routes for (let i = 0, len = routes.length; i < len; i++) { for (let j = i + 1; j < len; j++) { const compareResult = compareRoute(routes[i], routes[j]); // i includes j if (compareResult === 1) { const components = routes[j].hint.components; const namedParams = routes[i].hint.namedParams; for (let k = 0, len = namedParams.length; k < len; k++) { const c = components[namedParams[k][0]]; const m = c.match(/^\/:(\w+)({[^}]+})?/); if (m && namedParams[k][1] === m[1]) { continue; } if (m) { (_a = routes[j].paramAliasMap)[_b = m[1]] || (_a[_b] = []); routes[j].paramAliasMap[m[1]].push(namedParams[k][1]); } else { components[namedParams[k][0]] = `/:${namedParams[k][1]}{${c.substring(1)}}`; routes[j].hint.namedParams.push([ namedParams[k][0], namedParams[k][1], c.substring(1), ]); routes[j].path = components.join(''); } } if (routes[j].hint.components.length < routes[i].hint.components.length) { routes[j].middleware.push(...routes[i].handlers.map((h) => ({ index: h.index, handler: h.handler, }))); } else { routes[j].middleware.push(...routes[i].handlers); } routes[i].hint.maybeHandler = false; } else if (compareResult === 2) { // ambiguous hasAmbiguous = true; if (!verifyDuplicateParam([routes[i], routes[j]])) { throw new Error('Duplicate param name'); } } } if (!verifyDuplicateParam([routes[i]])) { throw new Error('Duplicate param name'); } } if (hasAmbiguous) { return [buildMatcherFromPreprocessedRoutes(routes, hasAmbiguous), [], hasAmbiguous]; } const primaryRoutes = []; const secondaryRoutes = []; for (let i = 0, len = routes.length; i < len; i++) { if (routes[i].hint.maybeHandler || !routes[i].hint.endWithWildcard) { primaryRoutes.push(routes[i]); } else { secondaryRoutes.push(routes[i]); } } return [ buildMatcherFromPreprocessedRoutes(primaryRoutes, hasAmbiguous), [buildMatcherFromPreprocessedRoutes(secondaryRoutes, hasAmbiguous)], hasAmbiguous, ]; }}exports.RegExpRouter = RegExpRouter;
deno

Version Info

Tagged at
2 years ago