-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
371 lines (267 loc) · 8.45 KB
/
index.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
type Logger = (ip: string, method: string, url: URL, error: null|HTTPError|Error) => void;
type HTTPServerOpts = {
port: number,
hostname: string,
routes: string|Routes,
static?: string,
logger?: Logger // not documented
};
export function rootDir() {
return Deno.cwd();
}
export default async function startHTTPServer({ port = 8080,
hostname = "localhost",
routes = "/routes",
static: _static,
logger = () => {}
}: HTTPServerOpts) {
let routesHandlers: Routes = routes as any;
if( typeof routes === "string" ) {
if(routes[0] === "/")
routes = rootDir() + routes;
routesHandlers = await loadAllRoutesHandlers(routes);
}
if(_static?.[0] === "/")
_static = rootDir() + _static;
const requestHandler = buildRequestHandler(routesHandlers, _static, logger);
// https://docs.deno.com/runtime/tutorials/http_server
await Deno.serve({ port, hostname }, requestHandler).finished;
}
export class HTTPError extends Error {
#error_code:number;
constructor(http_error_code: number, message: string) {
super(message);
this.name = "HTTPError";
this.#error_code = http_error_code;
}
get error_code() {
return this.#error_code;
}
}
export class SSEResponse {
#controller?: ReadableStreamDefaultController;
#stream = new ReadableStream({
start: (controller: any) => {
this.#controller = controller;
},
cancel: () => {
this.onConnectionClosed?.();
}
});
onConnectionClosed: null| (() => Promise<void>|void) = null;
constructor(run: (self: SSEResponse) => Promise<void>) {
run(this);
}
get _body() {
return this.#stream;
}
async send(data: any, event?: string) {
// JSON.stringify is required to escape characters.
let text = `data: ${JSON.stringify(data)}\n\n`;
if( event !== undefined)
text = `event: ${event}\n${text}`
this.#controller?.enqueue( new TextEncoder().encode( text ) );
}
}
export type HandlerParams = {
url : URL,
body: null|any,
route: {
path: string,
vars: Record<string, string>
}
};
type Handler = (request: HandlerParams) => Promise<any|SSEResponse>;
type Routes = Record<string, Handler>;
async function loadAllRoutesHandlers(routes: string): Promise<Routes> {
const ROOT = rootDir();
const routes_uri = await getAllRoutes(routes);
type Module = {default: Handler};
const handlers = Object.fromEntries( await Promise.all( routes_uri.map( async (uri) => {
// only with imports map, but bugged
// https://github.com/denoland/deno/issues/22237
//if( uri.startsWith(ROOT) )
// uri = uri.slice(ROOT.length)
if( uri[1] === ':' ) // windows drive
uri = `file://${uri}`;
let module!: Module;
try{
module = await import(uri);
} catch(e) {
console.error(e);
}
return [uri.slice(routes.length, - ".ts".length), module.default];
})));
return handlers;
}
async function getAllRoutes(currentPath: string): Promise<string[]> {
const files: string[] = [];
for await (const dirEntry of Deno.readDir(currentPath)) {
const entryPath = `${currentPath}/${dirEntry.name}`;
if ( ! dirEntry.isDirectory)
files.push( entryPath )
else
files.push(... await getAllRoutes(entryPath));
}
return files;
}
type REST_Methods = "POST"|"GET"|"DELETE"|"PUT"|"PATCH";
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, PATCH, PUT, OPTIONS, DELETE"
};
async function buildAnswer(http_code: number,
response: string|SSEResponse|any,
mime: string|null = null) {
switch (true) {
case response === null || response === undefined:
response = null;
mime = null;
break;
case response instanceof SSEResponse:
response = response._body;
mime = "text/event-stream";
break;
case typeof response === "string":
mime ??= "text/plain";
break;
case response instanceof FormData:
response = new URLSearchParams(response as any)
case response instanceof URLSearchParams:
mime = "application/x-www-form-urlencoded"
response = response.toString();
break;
case response instanceof Uint8Array:
mime ??= "application/octet-stream";
break;
case response instanceof Blob:
mime ??= response.type ?? "application/octet-stream";
response = await response.arrayBuffer();
break;
default:
response = JSON.stringify(response, null, 4);
mime = "application/json";
}
const headers: HeadersInit = {...CORS_HEADERS};
if(mime !== null)
headers["content-type"] = mime;
return new Response( response, {status: http_code,
headers} );
}
async function parseBody(request: Request) {
if( request.body === null)
return null;
let content_type = request.headers.get('Content-Type');
if( content_type === null || content_type === 'application/octet-stream') {
const buffer = await request.arrayBuffer();
if(buffer.byteLength === 0)
return null;
return new Uint8Array(buffer);
}
const [mime] = content_type.split(';')
if( ["text/plain", "application/json", "application/x-www-form-urlencoded"].includes(mime) ) {
const text = await request.text();
if( text === "")
return null;
try {
return JSON.parse(text);
} catch(e) {
if( mime === "application/json" )
throw e;
if( mime === "application/x-www-form-urlencoded")
return Object.fromEntries(new URLSearchParams(text).entries() );
return text;
}
}
const buffer = await request.arrayBuffer()
if(buffer.byteLength === 0)
return null;
return new Blob([buffer], {type: mime});
}
import { mimelite } from "https://deno.land/x/[email protected]/mod.ts";
function buildRequestHandler(routes: Routes, _static?: string, logger?: Logger) {
const regexes = Object.entries(routes).map( ([uri, handler]) => [path2regex(uri), handler, uri] as const);
return async function(request: Request, connInfo: any): Promise<Response> {
const ip = connInfo.remoteAddr.hostname;
const url = new URL(request.url);
let error = null;
const method = request.method as REST_Methods | "OPTIONS";
try {
if(method === "OPTIONS")
return new Response(null, {headers: CORS_HEADERS});
const route = getRouteHandler(regexes, method, url);
if(route === null) {
if( _static === undefined )
throw new HTTPError(404, "Not found");
let filepath = `${_static}/${url.pathname}`;
let content!: Uint8Array;
try {
const info = await Deno.stat(filepath);
if( info.isDirectory )
filepath = `${filepath}/index.html`;
content = await Deno.readFile(filepath);
} catch(e) {
if(e instanceof Deno.errors.NotFound)
throw new HTTPError(404, "Not Found");
if( e instanceof Deno.errors.PermissionDenied )
throw new HTTPError(403, "Forbidden");
throw new HTTPError(500, e.message);
}
const parts = filepath.split('.');
const ext = parts[parts.length-1];
const mime = mimelite.getType(ext) ?? "text/plain";
return await buildAnswer(200, content, mime);
}
const body = await parseBody(request);
let answer = await route.handler({url, body, route});
return await buildAnswer(200, answer);
} catch(e) {
error = e;
let error_code = 500;
if( e instanceof HTTPError )
error_code = e.error_code;
else
console.error(e);
const error_url = new URL(`/errors/${error_code}`, url);
const route = getRouteHandler(regexes, "GET", error_url);
let answer = e.message;
if(route !== null) {
try{
answer = await route.handler({url, body: e.message, route});
} catch(e) {
console.error(e); // errors handlers shoudn't raise errors...
}
}
return await buildAnswer(error_code, answer);
} finally {
if( logger !== undefined )
logger(ip, method, url, error);
}
};
}
// tests
function path2regex(path: string) {
// Escape special characters.
// cf https://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
path = path.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
return new RegExp("^" + path.replace(/\\\{[^\}]+\\\}/g, (captured) => `(?<${captured.slice(2,-2)}>[^/]+)`) + "$");
}
function match(regex: RegExp, uri: string) {
let result = regex.exec(uri);
if(result === null)
return false;
return result.groups ?? {};
}
function getRouteHandler(regexes: (readonly [RegExp, Handler, string])[], method: REST_Methods, url: URL) {
let curRoute = `${ decodeURI(url.pathname) }/${method}`;
for(let route of regexes) {
var vars = match(route[0], curRoute);
if(vars !== false)
return {
handler: route[1],
path : route[2],
vars
};
}
return null;
}