-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.ts
375 lines (358 loc) · 9.46 KB
/
server.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Created by: Henrique Emanoel Viana
Githu: https://github.com/hviana
Page: https://sites.google.com/view/henriqueviana
cel: +55 (41) 99999-4664
*/
import { DenoKvFs } from "./deps.ts";
export type Params = {
[key: string]: string;
};
export type ProcessorFunc = (ctx: Context) => Promise<void> | void;
export type ContextResponse = {
body: any;
headers: Headers;
status: number;
statusText: string;
};
export class Context {
#params: Params;
#url: URL;
req: Request;
body: any = undefined;
#extra: { [key: string]: any } = {};
error: Error | undefined = undefined;
#postProcessors: Set<ProcessorFunc> = new Set();
constructor(
params: Params,
url: URL,
req: Request,
hasRoute: boolean,
) {
this.#params = params;
this.#url = url;
this.req = req;
this.res.status = hasRoute ? 200 : 404;
}
res: ContextResponse = {
body: undefined,
headers: new Headers(),
status: 200,
statusText: "",
};
get params(): Params {
return this.#params;
}
get url(): URL {
return this.#url;
}
get extra(): any {
return this.#extra;
}
get postProcessors(): Set<ProcessorFunc> {
return this.#postProcessors;
}
redirect(...params: any[]): void {
this.postProcessors.add((ctx: Context) => {
if (params.length < 2) {
params.unshift(302);
}
ctx.res.headers.set("Location", params[1]);
ctx.res.status = params[0];
});
}
}
export type NextFunc = () => Promise<void> | void;
// Adapted from https://github.com/lukeed/regexparam/blob/master/src/index.js
export function parse(
str: RegExp | string,
loose?: boolean,
): { keys: string[]; pattern: RegExp } {
if (str instanceof RegExp) return { keys: [], pattern: str };
let c: string,
o: number,
tmp: string | undefined,
ext: number,
keys = [],
pattern: string = "",
arr: string[] = str.split("/");
arr[0] || arr.shift();
while (tmp = arr.shift()) {
c = tmp[0];
if (c === "*") {
//@ts-ignore
keys.push("wild");
pattern += "/(.*)";
} else if (c === ":") {
o = tmp.indexOf("?", 1);
ext = tmp.indexOf(".", 1);
//@ts-ignore
keys.push(tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length));
pattern += !!~o && !~ext ? "(?:/([^/]+?))?" : "/([^/]+?)";
if (!!~ext) pattern += (!!~o ? "?" : "") + "\\" + tmp.substring(ext);
} else {
pattern += "/" + tmp;
}
}
return {
keys: keys,
pattern: new RegExp("^" + pattern + (loose ? "(?=$|\/)" : "\/?$"), "i"),
};
}
export type RouteFn = (
ctx: Context,
next: NextFunc,
) => Promise<void> | void;
type RoutePattern = RegExp;
type Method =
| "ALL"
| "GET"
| "POST"
| "HEAD"
| "PATCH"
| "OPTIONS"
| "CONNECT"
| "DELETE"
| "TRACE"
| "POST"
| "PUT";
// A Route is a route when it has a routepattern otherwise it is treated as a middleware.
export type Route = {
pattern?: RoutePattern;
method: Method;
keys: string[];
handlers: RouteFn[];
};
export class Server {
static kv: Deno.Kv;
static kvFs: DenoKvFs;
static setKv(kv: Deno.Kv) {
Server.kv = kv;
Server.kvFs = new DenoKvFs(Server.kv);
}
#ac = new AbortController();
// NOTE: This is transpiled into the constructor, therefore equivalent to this.routes = [];
#routes: Route[] = [];
//@ts-ignore
server: Deno.HttpServer;
openedSockets: Map<any, any> = new Map();
// NOTE: Using .bind can significantly increase perf compared to arrow functions.
public all: Function = this.#add.bind(this, "ALL");
public get: Function = this.#add.bind(this, "GET");
public head: Function = this.#add.bind(this, "HEAD");
public patch: Function = this.#add.bind(this, "PATCH");
public options: Function = this.#add.bind(this, "OPTIONS");
public connect: Function = this.#add.bind(this, "CONNECT");
public delete: Function = this.#add.bind(this, "DELETE");
public trace: Function = this.#add.bind(this, "TRACE");
public post: Function = this.#add.bind(this, "POST");
public put: Function = this.#add.bind(this, "PUT");
public resetRoutes() {
this.#routes = [];
}
acceptOrRejectSocketConn = async (ctx: Context): Promise<any> => {
return undefined;
};
onSocketMessage = async (
id: string,
socket: WebSocket,
event: any,
): Promise<any> => {
};
onSocketClosed = async (id: string, socket: WebSocket): Promise<any> => {
};
onSocketOpen = async (id: string, socket: WebSocket): Promise<any> => {
};
onSocketError = async (id: string, socket: WebSocket): Promise<any> => {
};
public useAtBeginning(...handlers: RouteFn[]): Server {
this.#routes.unshift({
keys: [],
method: "ALL",
handlers,
});
return this;
}
public use(...handlers: RouteFn[]): Server {
this.#routes.push({
keys: [],
method: "ALL",
handlers,
});
return this;
}
#add(method: Method, route: string | RegExp, ...handlers: RouteFn[]) {
var {
keys,
pattern,
} = parse(route);
this.#routes.push({
keys,
method,
handlers,
pattern,
});
return this;
}
async #middlewareHandler(
fns: RouteFn[],
fnIndex: number,
ctx: Context,
): Promise<void> {
if (fns[fnIndex] !== undefined) {
try {
await fns[fnIndex](
ctx,
async () => await this.#middlewareHandler(fns, fnIndex + 1, ctx),
);
} catch (e: any) {
ctx.error = e;
}
}
}
async serveHandler(
request: Request,
): Promise<Response> {
try {
const req = request;
const url = new URL(request.url);
const requestHandlers: RouteFn[] = [];
const params: Params = {};
const len = this.#routes.length;
let hasRoute = false;
for (let i = 0; i < len; i++) {
const r = this.#routes[i];
const keyLength = r.keys.length;
let matches: null | string[] = null;
if (
r.pattern === undefined ||
(req.method === r.method &&
(matches = r.pattern.exec(
url.pathname,
)))
) {
if (r.pattern) {
hasRoute = true;
if (keyLength > 0) {
if (matches) {
let inc = 0;
while (inc < keyLength) {
const prevInc = inc;
const m = matches[++inc];
if (m != undefined) {
params[r.keys[prevInc]] = decodeURIComponent(m);
}
}
}
}
}
requestHandlers.push(...r.handlers);
}
}
const ctx = new Context(
params,
url,
req,
hasRoute,
);
if (req.headers.get("upgrade") == "websocket") {
const connectId = await this.acceptOrRejectSocketConn(ctx);
if (!connectId) {
ctx.res.status = 500;
return new Response(ctx.res.body, {
status: ctx.res.status,
});
}
const { socket, response } = Deno.upgradeWebSocket(req);
const existingSocket = this.openedSockets.get(connectId);
if (existingSocket) {
existingSocket.close();
}
this.openedSockets.set(connectId, socket);
socket.onmessage = async (event) => {
try {
await this.onSocketMessage(connectId, socket, event);
} catch (e) {
console.log(e);
}
};
socket.onclose = async () => {
try {
await this.onSocketClosed(connectId, socket);
} catch (e) {
console.log(e);
}
this.openedSockets.delete(connectId);
};
socket.onerror = async () => {
try {
await this.onSocketError(connectId, socket);
} catch (e) {
console.log(e);
}
this.openedSockets.delete(connectId);
};
socket.onopen = async () => {
try {
await this.onSocketOpen(connectId, socket);
} catch (e) {
console.log(e);
}
this.openedSockets.delete(connectId);
};
return response;
}
await this.#middlewareHandler(requestHandlers, 0, ctx);
if (!ctx.error) {
try {
for (const p of ctx.postProcessors) {
await p(ctx);
}
} catch (e: any) {
ctx.error = e;
}
}
if (ctx.res instanceof Response) {
if (ctx.error) {
console.log(ctx.error);
}
return ctx.res;
}
if (ctx.error) {
ctx.res.status = 500;
ctx.res.headers.set(
"Content-Type",
"application/json",
);
ctx.res.body = JSON.stringify({
msg: (ctx.error.message || JSON.stringify(ctx.error)),
stack: ctx.error.stack,
});
}
return new Response(ctx.res.body, {
headers: ctx.res.headers,
status: ctx.res.status,
statusText: ctx.res.statusText,
});
} catch (e) {
console.log(e);
return new Response();
}
}
public async listen(options: any) { //save as Deno.Serve options
if (this.server) {
this.#ac.abort();
await this.server.finished;
}
this.#ac = new AbortController();
//@ts-ignore
this.server = Deno.serve(
{ ...options, signal: this.#ac.signal },
(request: Request, info: Deno.ServeHandlerInfo) =>
this.serveHandler(request),
);
}
fetch = async (_req: Request): Promise<Response> => {
return await this.serveHandler(_req);
};
}