import { promises as fs } from "fs"; import path from "path"; export default new class Router { #mimes; constructor() { this.routes = new Map(); }; route(method, args) { this.routes.set(args[0], { method: method, f: args[1] }); console.info("route set", method, args[0]); }; get() { this.route("GET", arguments); }; post() { this.route("POST", arguments); }; async static({ dir = path.resolve() + "/public", route = /^\/public/ }) { if(!this.#mimes) { this.#mimes = new Map(); (await fs.readFile("/etc/mime.types", "utf-8")) .split("\n") .filter(e => !e.startsWith("#") && e) .map(e => e.split(/\s{2,}/)) .filter(e => e.length > 1) .forEach(m => m[1].split(" ").forEach(ext => this.#mimes.set(ext, m[0]))); } this.get(route, async (req, res) => { try { return res.reply({ type: this.#mimes.get(req.url.path.split(".").pop()).toLowerCase(), body: await fs.readFile(path.join(dir, req.url.path.replace(route, ""))) }); } catch { return res.reply({ code: 404, body: "404 - file not found" }); } }); }; }; Map.prototype.getRoute = function(path, method, tmp) { return (!(tmp = [...this.entries()].filter(r => ( r[0] === path || r[0].exec?.(path) ) && r[1].method.includes(method) )[0])) ? false : tmp[1].f; };