-
Notifications
You must be signed in to change notification settings - Fork 0
/
res.ts
72 lines (62 loc) · 1.81 KB
/
res.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
class HTTPResponse{
private _reqs: any;
private _webRes: string | Uint8Array;
private _headers: {[key: string]: string};
public status: number;
constructor(reqs: any){
this._reqs = reqs;
this._webRes = "";
this._headers = {};
this.status = 200;
}
send(text: string): this{
this._webRes += text;
return this;
}
sendFile(path: string): this{
const fileHeaders: {[key: string]: string} = {
".png": "image/png",
".gif": "image/gif",
".jpeg": "image/jpeg",
".tiff": "image/tiff",
".csv": "text/csv",
".xml": "text/xml",
".md": "text/markdown",
".html": "text/html",
".htm": "text/html",
".json": "application/json",
".map": "application/json",
".txt": "text/plain",
".ts": "text/typescript",
".tsx": "text/tsx",
".js": "application/javascript",
".jsx": "text/jsx",
".gz": "application/gzip",
".css": "text/css",
".wasm": "application/wasm",
".mjs": "application/javascript",
".svg": "image/svg+xml"
}
const contents = Deno.readFileSync(path);
this._webRes = contents;
this.setHeader("Content-Type",fileHeaders[path.split(".")[1]]);
return this;
}
setStatus(code: number): this{
this.status = code;
return this;
}
setHeader(name: string,value: any): this{
this._headers[name] = value;
return this;
}
_send(){
this._reqs.respondWith(
new Response(this._webRes,{
status: this.status,
headers: this._headers
})
);
}
}
export {HTTPResponse};