-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·165 lines (137 loc) · 3.77 KB
/
index.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
export const WILDCARD = '*'
const MATCH_ORDER = ['method', 'protocol', 'hostname', 'pathname']
export function makeFetch (handler, {
Request = globalThis.Request,
Response = globalThis.Response
} = {}) {
return async function fetch (...requestOptions) {
const request = new Request(...requestOptions)
const { body = null, ...responseOptions } = await handler(request)
const response = new Response(body, responseOptions)
return response
}
}
export function makeRoutedFetch ({
onNotFound = DEFAULT_NOT_FOUND,
onError = DEFAULT_ON_ERROR
} = {}) {
const router = new Router()
const fetch = makeFetch(async (request) => {
const route = router.route(request)
if (!route) {
return onNotFound(request)
}
try {
const response = await route.handler(request)
return response
} catch (e) {
return await onError(e, request)
}
})
return { fetch, router }
}
export function DEFAULT_NOT_FOUND () {
return { status: 404, statusText: 'Invalid URL' }
}
export function DEFAULT_ON_ERROR (e) {
return {
status: 500,
headers: {
'Content-Type': 'text/plain; charset=utf-8'
},
body: e.stack
}
}
export class Router {
constructor () {
this.routes = []
}
get (url, handler) {
return this.add('GET', url, handler)
}
head (url, handler) {
return this.add('HEAD', url, handler)
}
post (url, handler) {
return this.add('POST', url, handler)
}
put (url, handler) {
return this.add('PUT', url, handler)
}
delete (url, handler) {
return this.add('DELETE', url, handler)
}
patch (url, handler) {
return this.add('PATCH', url, handler)
}
any (url, handler) {
return this.add(WILDCARD, url, handler)
}
add (method, url, handler) {
const parsed = new URL(url)
const { hostname, protocol, pathname } = parsed
const segments = pathname.slice(1).split('/')
const route = {
protocol,
method: method.toUpperCase(),
hostname,
segments,
handler
}
this.routes.push(route)
return this
}
route (request) {
for (const route of this.routes) {
let hasFail = false
for (const property of MATCH_ORDER) {
if (!matches(request, route, property)) {
hasFail = true
}
}
if (!hasFail) {
return route
}
}
return null
}
}
function matches (request, route, property) {
if (property === 'pathname') {
const routeSegments = route.segments
const { pathname } = new URL(request.url)
const requestSegments = pathname.slice(1).split('/')
let i = 0
while (true) {
const routeLast = i === (routeSegments.length - 1)
const requestLast = i === (requestSegments.length - 1)
const routeSegment = routeSegments[i]
const requestSegment = requestSegments[i]
const routeWild = routeSegment === WILDCARD
const matches = routeWild || (routeSegment === requestSegment)
if (routeLast) {
if (routeSegment === (WILDCARD + WILDCARD)) return true
if (requestLast) return matches
return false
} else if (requestLast) {
return false
}
if (!matches) return false
i++
}
} if (property === 'hostname') {
return areEqual(route.hostname, new URL(request.url).hostname)
} else if (property === 'protocol') {
return areEqual(route.protocol, new URL(request.url).protocol)
} else if(property === 'method') {
return areEqual(route.method, request.method.toUpperCase())
} else {
const routeProperty = route[property]
const requestProperty = request[property]
return areEqual(routeProperty, requestProperty)
}
}
function areEqual (routeProperty, requestProperty) {
if (routeProperty === '*') return true
return routeProperty === requestProperty
}