swc/bundler/tests/.cache/deno/212822c05c6e42e4f774ec561becbdf587150b2f.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

54 lines
1.2 KiB
TypeScript

// Loaded from https://deno.land/x/abc@v1.2.4/router.ts
import type { HandlerFunc } from "./types.ts";
import type { Context } from "./context.ts";
import { Node } from "./vendor/https/deno.land/x/router/mod.ts";
import { hasTrailingSlash, NotFoundHandler } from "./util.ts";
export class Router {
trees: Record<string, Node> = {};
add(method: string, path: string, h: HandlerFunc): void {
if (path[0] !== "/") {
path = `/${path}`;
}
if (hasTrailingSlash(path)) {
path = path.slice(0, path.length - 1);
}
let root = this.trees[method];
if (!root) {
root = new Node();
this.trees[method] = root;
}
root.add(path, h);
}
find(method: string, c: Context): HandlerFunc {
const node = this.trees[method];
let path = c.path;
if (hasTrailingSlash(path)) {
path = path.slice(0, path.length - 1);
}
let h: HandlerFunc | undefined;
if (node) {
const [handle, params] = node.find(path);
if (params) {
for (const [k, v] of params) {
c.params[k] = v;
}
}
if (handle) {
h = handle as HandlerFunc;
}
}
return h ?? NotFoundHandler;
}
}