-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
234 lines (207 loc) · 6.58 KB
/
mod.ts
File metadata and controls
234 lines (207 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import * as Path from "@std/path";
import { LruCache } from "@std/cache";
import Logger from "@deno-lib/logger";
const logger: Logger = new Logger();
const routeCache = new LruCache<
[string, string],
[Route, Record<string, string | undefined>]
>(1000); // Cache size: 1000
type Handler = (
request: Request,
params?: Record<string, string | undefined>,
) => Response | Promise<Response>;
interface Route {
pattern: URLPattern;
method?: string;
handler: Handler;
}
type WsMap = Map<
string,
(socket?: WebSocket, event?: MessageEvent<string>) => void
>;
/**
* Recursively creates HTTP and WebSocket routes from a given directory.
*
* @param path - The root directory to scan for route modules.
* @param basePath - An optional base path for nested routes.
* @returns A tuple containing an array of routes and a WebSocket map.
*
* @example
* ```ts
* const [routes, wsMap] = await createRoutes("./routes");
* ```
*/
async function createRoutes(
path: string,
basePath = "",
): Promise<[Route[], WsMap]> {
let res: Route[] = [];
const wsll: WsMap = new Map<
string,
(socket?: WebSocket, event?: MessageEvent<string>) => void
>();
for await (const dirEntry of Deno.readDir(path)) {
if (dirEntry.isDirectory) {
const [nestedRoutes, _] = await createRoutes(
Path.join(path, dirEntry.name),
Path.join(basePath, Path.parse(Path.join(path, dirEntry.name)).name),
);
res = res.concat(nestedRoutes);
} else {
const modulePath = Path.join(path, dirEntry.name);
const module = await import(modulePath);
const MethodType = ["GET", "POST", "PUT", "DELETE", "PATCH"] as const;
if (
(module.default && typeof module.default === "function") ||
MethodType.some((method) => method in module)
) {
let routePath: string = "";
const regex = /\[(\w+)\]\.(ts|js)$/;
const paramMatch = dirEntry.name.match(regex);
if (dirEntry.name === "index.ts") {
routePath = basePath === "" ? "/" : `/${basePath}`;
} else if (paramMatch) {
const paramName = paramMatch[1]; // This will be "id"
routePath =
basePath === "" ? `/:${paramName}` : `/${basePath}/:${paramName}`;
} else {
// For non-index.ts files, map them to their name
const fileName = Path.parse(dirEntry.name).name;
routePath = `/${basePath}/${fileName}`;
}
// Clean up double slashes in the route path
routePath = routePath.replace(/\/+/g, "/");
for (const method of [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"default",
]) {
if (method in module) {
res.push({
pattern: new URLPattern({
pathname: routePath,
}),
method: method === "default" ? "GET" : method,
handler: module[method],
});
}
}
} else if (dirEntry.name === "+ws.ts") {
for (const evt of Object.keys(module)) {
wsll.set(evt, module[evt]);
}
} else {
logger.error(
`No default export or not a function in module: ${modulePath}`,
);
}
}
}
return [res, wsll];
}
/**
* Creates a request handler to match HTTP routes and WebSocket events.
*
* @param routes - The array of HTTP routes to handle.
* @param wsall - The map of WebSocket events and handlers.
* @param defaultHandler - The default handler to use for unmatched requests.
* @returns A function to handle HTTP and WebSocket requests.
*/
function route(
routes: Route[],
wsall: WsMap,
defaultHandler: (
request: Request,
info?: Deno.ServeHandlerInfo,
) => Response | Promise<Response>,
): (
request: Request,
info?: Deno.ServeHandlerInfo,
) => Response | Promise<Response> {
return (request: Request, info?: Deno.ServeHandlerInfo) => {
const url = request.url;
logger.info(`${request.method} ${url}`);
// WebSocket handling (remains the same)
if (request.headers.get("upgrade") === "websocket") {
const { socket, response } = Deno.upgradeWebSocket(request);
wsall.forEach((val, key) => {
socket.addEventListener(key, (event) => {
if (event instanceof MessageEvent) {
val(socket, event);
} else {
val(socket);
}
});
});
return response;
}
// Check if route is cached
if (routeCache.has([url, request.method])) {
const [cachedRoute, params] = routeCache.get([url, request.method])!;
return cachedRoute.handler(request, params);
}
// If not cached, match the routes and cache the result
const matchedRoutes: Array<[Route, Record<string, string | undefined>]> =
[];
for (const route of routes) {
const params = route.pattern.exec(url);
if (params && request.method === (route.method ?? "GET")) {
matchedRoutes.push([route, params.pathname.groups]);
}
}
if (matchedRoutes.length > 0) {
const base = Path.parse(request.url).base;
let bestMatch: [Route, Record<string, string | undefined>] | null = null;
for (const match of matchedRoutes) {
const [r] = match;
if (Path.parse(r.pattern.pathname).base === base) {
bestMatch = match;
}
}
const [routeToUse, params] = bestMatch ?? matchedRoutes[0];
// Cache the matched route for future requests
routeCache.set([url, request.method], [routeToUse, params]);
return routeToUse.handler(request, params);
}
// Return default handler if no route matches
return defaultHandler(request, info);
};
}
type MiloOptions = {
port?: number;
hostname?: string;
fsRouteDir: string;
};
/**
* The main class for the Milo framework. Handles routing and WebSocket integration.
*/
export class Milo {
options: MiloOptions;
/**
* Constructs a new instance of Milo.
*
* @param options - The configuration options for Milo, such as port, hostname, and the file system route directory.
*/
constructor(options: Partial<MiloOptions> = {}) {
this.options = options as MiloOptions;
}
/**
* Starts the server and serves the routes and WebSocket connections.
*
* @example
* ```ts
* const milo = new Milo({ port: 8000, fsRouteDir: "./routes" });
* await milo.run();
* ```
*/
async run(): Promise<void> {
const [Routes, Ws] = await createRoutes(this.options.fsRouteDir);
Deno.serve(
{ port: this.options.port, hostname: this.options.hostname },
route(Routes, Ws, () => new Response("404 not found")),
);
}
}