@saltyaom/trek-router
Version:
Smol HTTP router library
138 lines (137 loc) • 4.87 kB
JavaScript
"use strict";
!function(a, b) {
for(var c in b)Object.defineProperty(a, c, {
enumerable: !0,
get: b[c]
});
}(exports, {
default: ()=>k,
removeHostnamePath: ()=>b
});
const a = (a, b)=>{
let c = b.indexOf(a);
return -1 === c ? [
b,
''
] : [
b.slice(0, c),
b.slice(c + 1)
];
}, b = (a)=>{
if (47 === a.charCodeAt(0)) return a;
let b = a.length, c = 12;
for(; c < b && 47 !== a.charCodeAt(c); c++);
return a.slice(c);
}, c = (b)=>b.split("&").reduce((b, c)=>{
let [d, e] = a('=', c);
return b[d] = e, b;
}, {}), [d, e, f, g, h, i] = [
0,
1,
2,
42,
47,
58
];
class j {
constructor(a = '/', b = [], c = d, e = Object.create(null)){
this.label = a.charCodeAt(0), this.prefix = a, this.children = b, this.kind = c, this.map = e;
}
addChild(a) {
this.children.push(a);
}
findChild(a, b, c, d, e = 0) {
for(c = this.children.length; e < c; e++)if (a === (null == (d = this.children[e]) ? void 0 : d.label) && b === d.kind) return d;
}
findChildWithLabel(a, b, c, d = 0) {
for(b = this.children.length; d < b; d++)if (a === (null == (c = this.children[d]) ? void 0 : c.label)) return c;
}
findChildByKind(a, b, c, d = 0) {
for(b = this.children.length; d < b; d++)if (a === (null == (c = this.children[d]) ? void 0 : c.kind)) return c;
}
addHandler(a, b, c) {
this.map[a] = {
handler: b,
pnames: c
};
}
findHandler(a) {
return this.map[a];
}
}
class k {
constructor(){
this._tree = new j(), this.routes = [];
}
add(a, b, c) {
let [j, k, l] = [
0,
b.length,
[]
], m, n;
for(this.routes.push([
a,
b,
c
]); j < k; ++j)if ((m = b.charCodeAt(j)) === i) {
for(n = j + 1, this._insert(a, b.substring(0, j), d); j < k && b.charCodeAt(j) !== h;)j++;
if (l.push(b.substring(n, j)), b = b.substring(0, n) + b.substring(j), j = n, k = b.length, j === k) return void this._insert(a, b.substring(0, j), e, l, c);
this._insert(a, b.substring(0, j), e, l);
} else if (m === g) return this._insert(a, b.substring(0, j), d), l.push('*'), void this._insert(a, b.substring(0, k), f, l, c);
this._insert(a, b, d, l, c);
}
_insert(a, b, c, e, f) {
let [g] = [
this._tree
], h, i, k, l, m, n, o;
for(;;){
for(h = g.prefix, i = b.length, k = h.length, l = 0, m = i < k ? i : k; l < m && b.charCodeAt(l) === h.charCodeAt(l);)l++;
if (l < k) n = new j(h.substring(l), g.children, g.kind, g.map), g.children = [
n
], g.label = h.charCodeAt(0), g.prefix = h.substring(0, l), g.map = Object.create(null), g.kind = d, l === i ? (g.addHandler(a, f, e), g.kind = c) : ((n = new j(b.substring(l), [], c)).addHandler(a, f, e), g.addChild(n));
else if (l < i) {
if (b = b.substring(l), void 0 !== (o = g.findChildWithLabel(b.charCodeAt(0)))) {
g = o;
continue;
}
(n = new j(b, [], c)).addHandler(a, f, e), g.addChild(n);
} else void 0 !== f && g.addHandler(a, f, e);
return;
}
}
find(d, e) {
let [f, g] = a("?", b(e)), h = this._find(d, f, void 0, 0, [
void 0,
[]
]);
return h[2] = g ? c(g) : {}, h;
}
_find(a, b, c, g, i) {
c = c || this._tree;
let j = b.length, k = c.prefix, l = i[1], m, n, o, p, q, r;
if (0 === j || b === k) {
let s = c.findHandler(a);
if (void 0 !== (i[0] = s && s.handler)) {
let t = s.pnames;
if (void 0 !== t) for(m = 0, o = t.length; m < o; ++m)l[m] = [
t[m],
l[m]
];
}
return i;
}
for(n = k.length, o = 0, p = j < n ? j : n; o < p && b.charCodeAt(o) === k.charCodeAt(o);)o++;
if (o === n && (b = b.substring(o)), r = b, void 0 !== (q = c.findChild(b.charCodeAt(0), d))) {
if (this._find(a, b, q, g, i), void 0 !== i[0]) return i;
b = r;
}
if (o !== n) return i;
if (void 0 !== (q = c.findChildByKind(e))) {
for(o = b.length, m = 0; m < o && b.charCodeAt(m) !== h;)m++;
if (l[g] = b.substring(0, m), g++, r = b, b = b.substring(m), this._find(a, b, q, g, i), void 0 !== i[0]) return i;
g--, l.pop(), b = r;
}
return void 0 !== (q = c.findChildByKind(f)) && (l[g] = b, b = '', this._find(a, b, q, g, i)), i;
}
}
//# sourceMappingURL=index.js.map