-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
148 lines (136 loc) · 3.82 KB
/
mod.ts
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
import { Middleware, IResponse, IRequest, NextFunction, Mith } from 'https://deno.land/x/mith@v0.8.3/mod.ts'
import { match, MatchFunction } from 'https://raw.githubusercontent.com/pillarjs/path-to-regexp/master/src/index.ts'
interface RouterMiddleware<
Req extends IRequest = any,
Res extends IResponse = any,
Next extends NextFunction = any
> extends Middleware {
isRouter: boolean
}
type methods = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
const state: {
[key: number]: string
} = {}
/** Router state handlers
* they are used to keep track of the current state of the matcher during deep routing
* this is cleaned when the request ends
*/
/** Sets the router state
* @param id connection id from Deno request object
* @param path string containing the currently matched path
* @return void
*/
const setStatePath = (id: number, path: string) => {
return state[id] = path
}
/** Get a router state
* @param id connection id from Deno request object
* @return current state for the id
*/
const getStatePath = (id: number) => {
return state[id]
}
/** Delete a router state
* @param id connection id from Deno request object
* @return void
*/
const deleteStatePath = (id: number) => {
delete state[id]
}
/** A class which registers middleware (via `.use()`) and then processes
* inbound requests against that middleware (via `.getRoutes()`).
*/
export class Router {
private paths: {
[key in methods]: {
[key: string]: {
middleware: Mith,
isRouter: boolean,
matcher: MatchFunction,
route: string
}
}
} = {
GET: {},
POST: {},
PUT: {},
DELETE: {},
PATCH: {},
OPTIONS: {}
}
private savedPaths: {
[key in methods]: string[]
} = {
GET: [],
POST: [],
PUT: [],
DELETE: [],
PATCH: [],
OPTIONS: []
}
/** Register middleware to be used with the router.
* @param methods http method
* @param path path that will be matched with the requested url
* @param middleware
* @return void
*/
use(method: methods, path: string, middleware: Middleware | RouterMiddleware | Array<Middleware | RouterMiddleware>) {
const subApp = new Mith()
subApp.use(middleware)
let isRouter = false
if (Array.isArray(middleware)) {
isRouter = middleware.filter(
(item) => {
return (item as RouterMiddleware).isRouter
}
).length > 0
} else {
isRouter = (middleware as RouterMiddleware).isRouter
}
this.paths[method][path] = {
middleware: subApp,
isRouter,
matcher: match(path, { end: !isRouter }),
route: path,
}
this.savedPaths[method] = Object.keys(this.paths[method])
return this
}
/** Returns a middleware that will trigger the routing system
* @return middleware
*/
getRoutes(): Middleware {
const router = (req: IRequest, res: IResponse, next: NextFunction) => {
const connectionId = req.serverRequest.conn.rid
const statePath = getStatePath(connectionId)
const {
method,
url
} = req.serverRequest
for (const savedPath of this.savedPaths[method as methods]) {
const route = this.paths[method as methods][savedPath]
const matched = route.matcher(url.replace(statePath, '') || '/')
if (matched) {
req.params = {
...req.params,
...matched.params
}
if (route.isRouter) {
if (matched.path !== '/') {
setStatePath(connectionId, (statePath || '') + matched.path)
}
} else {
req.requestHandled = true
deleteStatePath(connectionId)
}
route.middleware.dispatch(req, res, 'main', 0, next)
return
}
}
deleteStatePath(connectionId)
next()
}
router.isRouter = true
return router
}
}