|
| 1 | +/* global Response */ |
| 2 | + |
| 3 | +const Trouter = require('trouter') |
| 4 | +const next = require('./../next') |
| 5 | +const LRU = require('lru-cache') |
| 6 | +const { parse } = require('regexparam') |
| 7 | + |
| 8 | +module.exports = (config = {}) => { |
| 9 | + if (config.defaultRoute === undefined) { |
| 10 | + config.defaultRoute = (req) => { |
| 11 | + const res = new Response(null, { |
| 12 | + status: 404 |
| 13 | + }) |
| 14 | + |
| 15 | + return res |
| 16 | + } |
| 17 | + } |
| 18 | + if (config.errorHandler === undefined) { |
| 19 | + config.errorHandler = (err, req) => { |
| 20 | + const res = new Response(err.message, { |
| 21 | + status: 500 |
| 22 | + }) |
| 23 | + |
| 24 | + return res |
| 25 | + } |
| 26 | + } |
| 27 | + if (config.cacheSize === undefined) { |
| 28 | + config.cacheSize = 1000 |
| 29 | + } |
| 30 | + if (config.id === undefined) { |
| 31 | + config.id = (Date.now().toString(36) + Math.random().toString(36).substring(2, 5)).toUpperCase() |
| 32 | + } |
| 33 | + |
| 34 | + const routers = {} |
| 35 | + const isCacheEnabled = config.cacheSize > 0 |
| 36 | + const cache = isCacheEnabled ? new LRU({ max: config.cacheSize }) : null |
| 37 | + const router = new Trouter() |
| 38 | + router.id = config.id |
| 39 | + |
| 40 | + const _use = router.use |
| 41 | + |
| 42 | + router.use = (prefix, ...middlewares) => { |
| 43 | + if (typeof prefix === 'function') { |
| 44 | + middlewares = [prefix, ...middlewares] |
| 45 | + prefix = '/' |
| 46 | + } |
| 47 | + _use.call(router, prefix, middlewares) |
| 48 | + |
| 49 | + if (middlewares[0].id) { |
| 50 | + // caching router -> pattern relation for urls pattern replacement |
| 51 | + const { pattern } = parse(prefix, true) |
| 52 | + routers[middlewares[0].id] = pattern |
| 53 | + } |
| 54 | + |
| 55 | + return this |
| 56 | + } |
| 57 | + |
| 58 | + router.lookup = async (req, step) => { |
| 59 | + const url = new URL(req.url) |
| 60 | + req.path = url.pathname || '/' |
| 61 | + req.query = url.queryparams |
| 62 | + req.search = url.search |
| 63 | + req.hostname = url.hostname |
| 64 | + |
| 65 | + let match |
| 66 | + if (isCacheEnabled) { |
| 67 | + const reqCacheKey = req.method + req.path |
| 68 | + match = cache.get(reqCacheKey) |
| 69 | + if (!match) { |
| 70 | + match = router.find(req.method, req.path) |
| 71 | + cache.set(reqCacheKey, match) |
| 72 | + } |
| 73 | + } else { |
| 74 | + match = router.find(req.method, req.path) |
| 75 | + } |
| 76 | + |
| 77 | + if (match.handlers.length !== 0) { |
| 78 | + const middlewares = [...match.handlers] |
| 79 | + if (step !== undefined) { |
| 80 | + // router is being used as a nested router |
| 81 | + middlewares.push((req, next) => { |
| 82 | + req.path = req.preRouterPath |
| 83 | + |
| 84 | + delete req.preRouterPath |
| 85 | + |
| 86 | + return step() |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + if (!req.params) { |
| 91 | + req.params = {} |
| 92 | + } |
| 93 | + Object.assign(req.params, match.params) |
| 94 | + |
| 95 | + return next(middlewares, req, 0, routers, config.defaultRoute, config.errorHandler) |
| 96 | + } else { |
| 97 | + return config.defaultRoute(req) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + router.on = (method, pattern, ...handlers) => router.add(method, pattern, handlers) |
| 102 | + |
| 103 | + return router |
| 104 | +} |
0 commit comments