dodoko
Version:
mini http server implemented on top of vert.x
81 lines (80 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.default = void 0;
var a = require("@vertx/core/options");
const b = (a)=>a.includes(":") || a.includes("*")
;
exports.default = ()=>{
const c = new Map(), d = new Map();
let e = ({ res: a })=>{
a.setStatusCode(404).end();
};
d.set(a.HttpMethod.GET, new Map()), d.set(a.HttpMethod.POST, new Map()), d.set(a.HttpMethod.PUT, new Map()), d.set(a.HttpMethod.PATCH, new Map()), d.set(a.HttpMethod.DELETE, new Map()), d.set(a.HttpMethod.OPTIONS, new Map()), d.set(a.HttpMethod.HEAD, new Map());
const f = async (a)=>void vertx.createHttpServer().requestHandler(async (a)=>{
const b = a.path(), f = a.method(), g = a.response(), h = {
path: b,
params: {}
}, i = {
req: a,
extra: h,
res: g
}, j = async (b)=>{
const c = b instanceof Promise ? await b : b;
c && !a.isEnded() && g.send(c);
}, k = c.get(`${f}${b}`);
if (k) return await j(k(i));
const l = b.split("/");
for (let [m, n] of d.get(f).entries()){
if ("*" === m) return await j(n(i));
const o = m.split("/");
if (o.length !== l.length && !o.includes("*")) continue;
const p = {};
let q = !1, r = !0;
for(let s = 0; s <= o.length; s++){
const t = o[s], u = l[s];
if ("*" === t) {
q = !0;
let v = [
u
];
for(let w = s + 1; w < l.length; w++)v.push(l[w]);
p.rest = v.join("/");
break;
}
if (t) {
if (":" === t[0]) {
p[t.slice(1, t.length)] = u;
continue;
}
if (t !== u) {
r = !1;
break;
}
}
}
if (q || r) return h.params = p, await j(n(i));
}
return await j(e(i));
}).listen(a)
, g = {
get: (e, f)=>(b(e) ? d.get(a.HttpMethod.GET).set(e, f) : c.set(`${a.HttpMethod.GET}${e}`, f), g)
,
post: (e, f)=>(b(e) ? d.get(a.HttpMethod.POST).set(e, f) : c.set(`${a.HttpMethod.POST}${e}`, f), g)
,
put: (e, f)=>(b(e) ? d.get(a.HttpMethod.PUT).set(e, f) : c.set(`${a.HttpMethod.PUT}${e}`, f), g)
,
patch: (e, f)=>(b(e) ? d.get(a.HttpMethod.PATCH).set(e, f) : c.set(`${a.HttpMethod.PATCH}${e}`, f), g)
,
del: (e, f)=>(b(e) ? d.get(a.HttpMethod.DELETE).set(e, f) : c.set(`${a.HttpMethod.DELETE}${e}`, f), g)
,
options: (e, f)=>(b(e) ? d.get(a.HttpMethod.OPTIONS).set(e, f) : c.set(`${a.HttpMethod.OPTIONS}${e}`, f), g)
,
head: (e, f)=>(b(e) ? d.get(a.HttpMethod.HEAD).set(e, f) : c.set(`${a.HttpMethod.HEAD}${e}`, f), g)
,
listen: f,
fallback: (a)=>(e = a, g)
};
return g;
};
//# sourceMappingURL=index.js.map