22
33const Trouter = require ( 'trouter' )
44const next = require ( './../next' )
5- const LRU = require ( 'lru-cache' )
6- const { parse } = require ( 'regexparam' )
75
86module . exports = ( config = { } ) => {
9- if ( config . defaultRoute === undefined ) {
7+ if ( ! config . defaultRoute ) {
108 config . defaultRoute = ( req ) => {
119 const res = new Response ( null , {
1210 status : 404
@@ -15,7 +13,7 @@ module.exports = (config = {}) => {
1513 return res
1614 }
1715 }
18- if ( config . errorHandler === undefined ) {
16+ if ( ! config . errorHandler ) {
1917 config . errorHandler = ( err , req ) => {
2018 const res = new Response ( err . message , {
2119 status : 500
@@ -24,18 +22,9 @@ module.exports = (config = {}) => {
2422 return res
2523 }
2624 }
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- }
3325
34- const routers = { }
35- const isCacheEnabled = config . cacheSize > 0
36- const cache = isCacheEnabled ? new LRU ( { max : config . cacheSize } ) : null
3726 const router = new Trouter ( )
38- router . id = config . id
27+ router . port = config . port || 3000
3928
4029 const _use = router . use
4130
@@ -46,53 +35,24 @@ module.exports = (config = {}) => {
4635 }
4736 _use . call ( router , prefix , middlewares )
4837
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-
5538 return this
5639 }
5740
58- router . lookup = ( req , step ) => {
41+ router . fetch = ( req , step ) => {
5942 const url = new URL ( req . url )
6043 req . path = url . pathname || '/'
6144 req . query = url . queryparams
6245 req . search = url . search
6346 req . hostname = url . hostname
6447
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-
48+ const match = router . find ( req . method , req . path )
49+ if ( match . handlers . length > 0 ) {
9050 if ( ! req . params ) {
9151 req . params = { }
9252 }
9353 Object . assign ( req . params , match . params )
9454
95- return next ( middlewares , req , 0 , routers , config . defaultRoute , config . errorHandler )
55+ return next ( match . handlers , req , 0 , config . defaultRoute , config . errorHandler )
9656 } else {
9757 return config . defaultRoute ( req )
9858 }
0 commit comments