MCPcopy
hub / github.com/freshframework/fresh / UrlPatternRouter

Class UrlPatternRouter

packages/fresh/src/router.ts:57–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55const EMPTY: string[] = [];
56
57export class UrlPatternRouter<T> implements Router<T> {
58 #statics = new Map<string, StaticRouteDef<T>>();
59 #dynamics = new Map<string, DynamicRouteDef<T>>();
60 #dynamicArr: DynamicRouteDef<T>[] = [];
61 #allowed = new Map<string, Set<string>>();
62
63 getAllowedMethods(pattern: string): string[] {
64 const allowed = this.#allowed.get(pattern);
65 if (allowed === undefined) return EMPTY;
66 return Array.from(allowed);
67 }
68
69 add(
70 method: Method,
71 pathname: string,
72 item: T,
73 ) {
74 let allowed = this.#allowed.get(pathname);
75 if (allowed === undefined) {
76 allowed = new Set();
77 this.#allowed.set(pathname, allowed);
78 }
79
80 allowed.add(method);
81
82 let byMethod: RouteByMethod<T>;
83 if (IS_PATTERN.test(pathname)) {
84 let def = this.#dynamics.get(pathname);
85 if (def === undefined) {
86 def = {
87 pattern: new URLPattern({ pathname }),
88 byMethod: newByMethod(),
89 };
90 this.#dynamics.set(pathname, def);
91 this.#dynamicArr.push(def);
92 }
93
94 byMethod = def.byMethod;
95 } else {
96 let def = this.#statics.get(pathname);
97 if (def === undefined) {
98 def = {
99 pattern: pathname,
100 byMethod: newByMethod(),
101 };
102 this.#statics.set(pathname, def);
103 }
104
105 byMethod = def.byMethod;
106 }
107
108 if (byMethod[method] === null) {
109 byMethod[method] = item;
110 }
111 }
112
113 match(method: Method, url: URL): RouteResult<T> {
114 const result: RouteResult<T> = {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected