|
1 | | -const handlers = { |
2 | | - match: updateParams => (req, res, params) => { |
3 | | - if (updateParams) { |
4 | | - req.params = params |
5 | | - } |
| 1 | +// Optimized handlers with minimal allocations |
| 2 | +const createMatchHandler = (updateParams) => |
| 3 | + updateParams |
| 4 | + ? (req, res, params) => { |
| 5 | + req.params = params |
| 6 | + return true |
| 7 | + } |
| 8 | + : () => true |
| 9 | + |
| 10 | +const defaultHandler = () => false |
6 | 11 |
|
7 | | - return true |
8 | | - }, |
9 | | - default: () => false |
| 12 | +// Router cache for reusing router instances |
| 13 | +const routerCache = new WeakMap() |
| 14 | + |
| 15 | +function normalizeEndpoint (endpoint) { |
| 16 | + if (typeof endpoint === 'string') { |
| 17 | + return { url: endpoint, methods: ['GET'], updateParams: false } |
| 18 | + } |
| 19 | + return { |
| 20 | + methods: endpoint.methods || ['GET'], |
| 21 | + url: endpoint.url, |
| 22 | + version: endpoint.version, |
| 23 | + updateParams: endpoint.updateParams || false |
| 24 | + } |
10 | 25 | } |
11 | 26 |
|
12 | 27 | module.exports = function (routerOpts = {}, routerFactory = require('find-my-way')) { |
13 | | - routerOpts.defaultRoute = handlers.default |
14 | | - |
15 | 28 | function exec (options, isIff = true) { |
16 | 29 | const middleware = this |
| 30 | + let router = null |
| 31 | + let customFn = null |
| 32 | + |
| 33 | + // Process options efficiently |
| 34 | + if (typeof options === 'function') { |
| 35 | + customFn = options |
| 36 | + } else { |
| 37 | + const endpoints = Array.isArray(options) ? options : options?.endpoints |
| 38 | + |
| 39 | + if (endpoints?.length) { |
| 40 | + // Try to get cached router first |
| 41 | + let cache = routerCache.get(routerOpts) |
| 42 | + if (!cache) { |
| 43 | + cache = new Map() |
| 44 | + routerCache.set(routerOpts, cache) |
| 45 | + } |
| 46 | + |
| 47 | + const cacheKey = JSON.stringify(endpoints) |
| 48 | + router = cache.get(cacheKey) |
17 | 49 |
|
18 | | - // independent router instance per config |
19 | | - const router = routerFactory(routerOpts) |
20 | | - |
21 | | - const opts = typeof options === 'function' ? { custom: options } : (Array.isArray(options) ? { endpoints: options } : options) |
22 | | - if (opts.endpoints && opts.endpoints.length) { |
23 | | - // setup matching router |
24 | | - opts.endpoints |
25 | | - .map(endpoint => typeof endpoint === 'string' ? { url: endpoint } : endpoint) |
26 | | - .forEach(({ methods = ['GET'], url, version, updateParams = false }) => { |
27 | | - if (version) { |
28 | | - router.on(methods, url, { constraints: { version } }, handlers.match(updateParams)) |
29 | | - } else { |
30 | | - router.on(methods, url, handlers.match(updateParams)) |
| 50 | + if (!router) { |
| 51 | + router = routerFactory({ ...routerOpts, defaultRoute: defaultHandler }) |
| 52 | + |
| 53 | + // Normalize and register routes |
| 54 | + const normalized = endpoints.map(normalizeEndpoint) |
| 55 | + for (const { methods, url, version, updateParams } of normalized) { |
| 56 | + const handler = createMatchHandler(updateParams) |
| 57 | + |
| 58 | + if (version) { |
| 59 | + router.on(methods, url, { constraints: { version } }, handler) |
| 60 | + } else { |
| 61 | + router.on(methods, url, handler) |
| 62 | + } |
31 | 63 | } |
32 | | - }) |
| 64 | + |
| 65 | + cache.set(cacheKey, router) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (options?.custom) { |
| 70 | + customFn = options.custom |
| 71 | + } |
33 | 72 | } |
34 | 73 |
|
| 74 | + // Optimized execution function |
35 | 75 | const result = function (req, res, next) { |
36 | | - // supporting custom matching function |
37 | | - if (opts.custom) { |
38 | | - if (opts.custom(req)) { |
39 | | - if (isIff) { |
40 | | - return middleware(req, res, next) |
41 | | - } |
42 | | - } else if (!isIff) { |
43 | | - return middleware(req, res, next) |
44 | | - } |
| 76 | + let shouldExecute = false |
45 | 77 |
|
46 | | - // leave here and do not process opts.endpoints |
47 | | - return next() |
| 78 | + if (customFn) { |
| 79 | + shouldExecute = customFn(req) |
| 80 | + } else if (router) { |
| 81 | + shouldExecute = router.lookup(req, res) |
48 | 82 | } |
49 | 83 |
|
50 | | - // matching endpoints and moving forward |
51 | | - if (router.lookup(req, res)) { |
52 | | - if (isIff) { |
53 | | - return middleware(req, res, next) |
54 | | - } |
55 | | - } else if (!isIff) { |
| 84 | + // Simplified logic: execute middleware if conditions match |
| 85 | + if ((isIff && shouldExecute) || (!isIff && !shouldExecute)) { |
56 | 86 | return middleware(req, res, next) |
57 | 87 | } |
58 | 88 |
|
59 | 89 | return next() |
60 | 90 | } |
61 | 91 |
|
62 | | - // allowing chaining |
| 92 | + // Allow chaining |
63 | 93 | result.iff = iff |
64 | 94 | result.unless = unless |
65 | 95 |
|
|
0 commit comments