-
Notifications
You must be signed in to change notification settings - Fork 14
/
mod.ts
153 lines (136 loc) · 4.21 KB
/
mod.ts
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
import {
match,
pathToRegexp,
} from "https://deno.land/x/path_to_regexp@v6.2.0/index.ts";
import { json, jsx, validateRequest } from "./util.ts";
interface ResponseCache {
[path: string]: Response;
}
export interface PathParams {
[key: string]: string | string[];
}
export type Handler = (
request: Request,
params?: PathParams,
) => Response | Promise<Response>;
export interface Routes {
[path: string]: Handler;
}
function serve(routes: Routes): void {
routes = { 404: defaultNotFoundPage, ...routes };
// deno-lint-ignore no-explicit-any
addEventListener("fetch", (event: any) => {
event.respondWith(handleRequest(event.request, routes));
});
}
const responseCache: ResponseCache = {};
async function handleRequest(
request: Request,
routes: Routes,
): Promise<Response> {
const { search, pathname } = new URL(request.url);
try {
if (!(responseCache["404"] instanceof Response)) {
let NotFoundResponse = await routes[404](request);
if (!(NotFoundResponse instanceof Response)) {
responseCache["404"] = jsx(NotFoundResponse, { status: 404 });
}
}
const startTime = Date.now();
let response = responseCache["404"].clone();
response.headers.set("from-function-cache", "true");
for (const route of Object.keys(routes)) {
if (pathToRegexp(route).test(pathname)) {
const getParams = match(route);
const { params = {} } = getParams(pathname) as {
params: { [key: string]: string };
};
try {
response = await routes[route](request, params);
} catch (error) {
response = json({ error: error.message }, { status: 500 });
}
if (!(response instanceof Response)) {
response = jsx(response);
}
break;
}
}
// method path+params timeTaken status
console.log(
`${request.method} ${pathname + search} ${
response.headers.has("from-function-cache")
? String.fromCodePoint(0x26a1)
: ""
}${Date.now() - startTime}ms ${response.status}`,
);
return response;
} catch (error) {
return json({ error: error.message }, { status: 500 });
}
}
/** Caches the response - it's really dumb and only works for static assets.
*
* @param {Function} handler
*/
function cache(handler: Handler) {
return async (request: Request, params?: PathParams): Promise<Response> => {
const { pathname } = new URL(request.url);
if (responseCache[pathname]) {
const response = responseCache[pathname].clone();
response.headers.set("from-function-cache", "true");
return response;
}
let response = await handler(request, params);
if (!(response instanceof Response)) {
response = jsx(response);
}
responseCache[pathname] = response.clone();
return response;
};
}
function defaultNotFoundPage() {
return new Response("<h1 align=center>page not found</h1>", {
status: 404,
headers: { "Content-Type": "text/html; charset=utf-8" },
});
}
/**
* This function serves a file or files inside a directory.
*
* All subsequent requests are served from in-memory cache.
*
* TODO(@satyarohith): add examples to show usage.
*/
function serveStatic(
relativePath: string,
config: {
baseUrl: string;
intervene?: (response: Response) => Promise<Response> | Response;
},
): Handler {
return cache(
async (request: Request, params?: PathParams): Promise<Response> => {
let filePath = relativePath;
if (params && params.filename) {
if (Array.isArray(params.filename)) {
params.filename = params.filename.join("/");
}
filePath = relativePath.endsWith("/")
? relativePath + params.filename
: relativePath + "/" + params.filename;
}
const fileUrl = new URL(filePath, config.baseUrl).toString();
let response = await fetch(new Request(fileUrl, request));
if (typeof config.intervene === "function") {
response = await config.intervene(response);
}
if (response.status == 404) {
return defaultNotFoundPage();
}
return response;
},
);
}
export { json, jsx, serve, serveStatic, validateRequest };
export * from "https://x.lcas.dev/preact@10.5.7/mod.js";