-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
266 lines (229 loc) · 5.82 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*!
* router
* Copyright(c) 2015-2017 Fangdun Cai
* MIT Licensed
*/
'use strict'
// Static Param Any `*` `/` `:`
const [SKIND, PKIND, AKIND, STAR, SLASH, COLON] = [0, 1, 2, 42, 47, 58]
class Node {
constructor(
prefix = '/',
children = [],
kind = SKIND,
map = Object.create(null)
) {
this.label = prefix.charCodeAt(0)
this.prefix = prefix
this.children = children
this.kind = kind
this.map = map
}
addChild(n) {
this.children.push(n)
}
findChild(c, t, l, e, i = 0) {
for (l = this.children.length; i < l; i++) {
e = this.children[i]
if (c === e.label && t === e.kind) {
return e
}
}
}
findChildWithLabel(c, l, e, i = 0) {
for (l = this.children.length; i < l; i++) {
e = this.children[i]
if (c === e.label) {
return e
}
}
}
findChildByKind(t, l, e, i = 0) {
for (l = this.children.length; i < l; i++) {
e = this.children[i]
if (t === e.kind) {
return e
}
}
}
addHandler(method, handler, pnames) {
this.map[method] = { handler, pnames }
}
findHandler(method) {
return this.map[method]
}
}
class Router {
constructor() {
this.tree = new Node()
}
add(method, path, handler) {
// Pnames: Param names
let [i, l, pnames, ch, j] = [0, path.length, []]
for (; i < l; ++i) {
ch = path.charCodeAt(i)
if (ch === COLON) {
j = i + 1
this.insert(method, path.substring(0, i), SKIND)
while (i < l && path.charCodeAt(i) !== SLASH) {
i++
}
pnames.push(path.substring(j, i))
path = path.substring(0, j) + path.substring(i)
i = j
l = path.length
if (i === l) {
this.insert(method, path.substring(0, i), PKIND, pnames, handler)
return
}
this.insert(method, path.substring(0, i), PKIND, pnames)
} else if (ch === STAR) {
this.insert(method, path.substring(0, i), SKIND)
pnames.push('*')
this.insert(method, path.substring(0, l), AKIND, pnames, handler)
return
}
}
this.insert(method, path, SKIND, pnames, handler)
}
insert(method, path, t, pnames, handler) {
// Current node as root
let [cn, prefix, sl, pl, l, max, n, c] = [this.tree]
while (true) {
prefix = cn.prefix
sl = path.length
pl = prefix.length
l = 0
// LCP
max = sl < pl ? sl : pl
while (l < max && path.charCodeAt(l) === prefix.charCodeAt(l)) {
l++
}
/*
If (l === 0) {
// At root node
cn.label = search.charCodeAt(0)
cn.prefix = search
if (handler !== undefined) {
cn.addHandler(method, { pnames, handler })
}
} else if (l < pl) {
*/
if (l < pl) {
// Split node
n = new Node(prefix.substring(l), cn.children, cn.kind, cn.map)
cn.children = [n] // Add to parent
// Reset parent node
cn.label = prefix.charCodeAt(0)
cn.prefix = prefix.substring(0, l)
cn.map = Object.create(null)
cn.kind = SKIND
if (l === sl) {
// At parent node
cn.addHandler(method, handler, pnames)
cn.kind = t
} else {
// Create child node
n = new Node(path.substring(l), [], t)
n.addHandler(method, handler, pnames)
cn.addChild(n)
}
} else if (l < sl) {
path = path.substring(l)
c = cn.findChildWithLabel(path.charCodeAt(0))
if (c !== undefined) {
// Go deeper
cn = c
continue
}
// Create child node
n = new Node(path, [], t)
n.addHandler(method, handler, pnames)
cn.addChild(n)
} else if (handler !== undefined) {
// Node already exists
cn.addHandler(method, handler, pnames)
}
return
}
}
find(method, path, cn, n = 0, result = [undefined, []]) {
cn = cn || this.tree // Current node as root
const sl = path.length
const prefix = cn.prefix
const pvalues = result[1] // Params
let i, pl, l, max, c
let preSearch // Pre search
// Search order static > param > match-any
if (sl === 0 || path === prefix) {
// Found
const r = cn.findHandler(method)
if ((result[0] = r && r.handler) !== undefined) {
const pnames = r.pnames
if (pnames !== undefined) {
for (i = 0, l = pnames.length; i < l; ++i) {
pvalues[i] = {
name: pnames[i],
value: pvalues[i]
}
}
}
}
return result
}
pl = prefix.length
l = 0
// LCP
max = sl < pl ? sl : pl
while (l < max && path.charCodeAt(l) === prefix.charCodeAt(l)) {
l++
}
if (l === pl) {
path = path.substring(l)
}
preSearch = path
// Static node
c = cn.findChild(path.charCodeAt(0), SKIND)
if (c !== undefined) {
this.find(method, path, c, n, result)
if (result[0] !== undefined) {
return result
}
path = preSearch
}
// Not found node
if (l !== pl) {
return result
}
// Param node
c = cn.findChildByKind(PKIND)
if (c !== undefined) {
l = path.length
i = 0
while (i < l && path.charCodeAt(i) !== SLASH) {
i++
}
pvalues[n] = path.substring(0, i)
n++
preSearch = path
path = path.substring(i)
this.find(method, path, c, n, result)
if (result[0] !== undefined) {
return result
}
n--
pvalues.pop()
path = preSearch
}
// Any node
c = cn.findChildByKind(AKIND)
if (c !== undefined) {
pvalues[n] = path
path = '' // End search
this.find(method, path, c, n, result)
}
return result
}
}
Router.Node = Node
module.exports = Router