2
2
3
3
const Trouter = require ( 'trouter' )
4
4
const next = require ( './../next' )
5
- const LRU = require ( 'lru-cache' )
6
- const { parse } = require ( 'regexparam' )
7
5
8
6
module . exports = ( config = { } ) => {
9
- if ( config . defaultRoute === undefined ) {
7
+ if ( ! config . defaultRoute ) {
10
8
config . defaultRoute = ( req ) => {
11
9
const res = new Response ( null , {
12
10
status : 404
@@ -15,7 +13,7 @@ module.exports = (config = {}) => {
15
13
return res
16
14
}
17
15
}
18
- if ( config . errorHandler === undefined ) {
16
+ if ( ! config . errorHandler ) {
19
17
config . errorHandler = ( err , req ) => {
20
18
const res = new Response ( err . message , {
21
19
status : 500
@@ -24,18 +22,9 @@ module.exports = (config = {}) => {
24
22
return res
25
23
}
26
24
}
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
25
34
- const routers = { }
35
- const isCacheEnabled = config . cacheSize > 0
36
- const cache = isCacheEnabled ? new LRU ( { max : config . cacheSize } ) : null
37
26
const router = new Trouter ( )
38
- router . id = config . id
27
+ router . port = config . port || 3000
39
28
40
29
const _use = router . use
41
30
@@ -46,53 +35,24 @@ module.exports = (config = {}) => {
46
35
}
47
36
_use . call ( router , prefix , middlewares )
48
37
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
38
return this
56
39
}
57
40
58
- router . lookup = ( req , step ) => {
41
+ router . fetch = ( req , step ) => {
59
42
const url = new URL ( req . url )
60
43
req . path = url . pathname || '/'
61
44
req . query = url . queryparams
62
45
req . search = url . search
63
46
req . hostname = url . hostname
64
47
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 ) {
90
50
if ( ! req . params ) {
91
51
req . params = { }
92
52
}
93
53
Object . assign ( req . params , match . params )
94
54
95
- return next ( middlewares , req , 0 , routers , config . defaultRoute , config . errorHandler )
55
+ return next ( match . handlers , req , 0 , config . defaultRoute , config . errorHandler )
96
56
} else {
97
57
return config . defaultRoute ( req )
98
58
}
0 commit comments