-
Notifications
You must be signed in to change notification settings - Fork 576
/
http.ts
158 lines (137 loc) · 2.96 KB
/
http.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
import { Status as STATUS_CODES } from "../http/http_status.ts";
import { Buffer } from "./buffer.ts";
const METHODS = [
"ACL",
"BIND",
"CHECKOUT",
"CONNECT",
"COPY",
"DELETE",
"GET",
"HEAD",
"LINK",
"LOCK",
"M-SEARCH",
"MERGE",
"MKACTIVITY",
"MKCALENDAR",
"MKCOL",
"MOVE",
"NOTIFY",
"OPTIONS",
"PATCH",
"POST",
"PROPFIND",
"PROPPATCH",
"PURGE",
"PUT",
"REBIND",
"REPORT",
"SEARCH",
"SOURCE",
"SUBSCRIBE",
"TRACE",
"UNBIND",
"UNLINK",
"UNLOCK",
"UNSUBSCRIBE",
];
type Chunk = string | Buffer | Uint8Array;
type Headers = Record<string, string>;
export class ServerResponse {
status?: number;
headers: Headers;
chunks: Chunk[];
headersSent: boolean;
private reqEvent: Deno.RequestEvent;
constructor(reqEvent: Deno.RequestEvent) {
this.status = undefined;
this.headers = {};
this.chunks = [];
this.headersSent = false;
this.reqEvent = reqEvent;
}
setHeader(name: string, value: string) {
this.headers[name] = value;
}
getHeader(name: string) {
return this.headers[name];
}
writeHead(status: number, headers: Headers) {
this.status = status;
Object.assign(this.headers, headers);
}
write(chunk: Chunk) {
this._addChunk(chunk);
}
_addChunk(chunk: Chunk) {
if (this.status === null) {
this.status = 200;
this.headers = { "content-type": "text/plain" };
}
this.chunks.push(chunk);
}
_body(lastChunk?: Chunk) {
// TODO: incorrectly assumes all chunks are strings
return this.chunks ? this.chunks.join("") + lastChunk : lastChunk;
}
end(lastChunk: Chunk) {
const body = this._body(lastChunk);
this.headersSent = true;
this.reqEvent.respondWith(new Response(body, { headers: this.headers }));
}
}
export class IncomingMessage {
private req: Request;
constructor(req: Request) {
this.req = req;
}
get aborted() {
return false;
}
get completed() {
return true;
}
get httpVersion() {
return "1.1";
}
get headers() {
return this.req.headers;
}
get url() {
// TODO: consider more robust path extraction, e.g:
// url: (new URL(request.url).pathname),
return this.req.url.slice(this.req.url.indexOf("/", 8));
}
}
type ServerHandler = (req: IncomingMessage, res: ServerResponse) => void;
export class Server {
handler: ServerHandler;
constructor(handler: ServerHandler) {
this.handler = handler;
}
async listen(port: number) {
for await (const conn of Deno.listen({ port })) {
(async () => {
for await (const reqEvent of Deno.serveHttp(conn)) {
this.handler(
new IncomingMessage(reqEvent.request),
new ServerResponse(reqEvent),
);
}
})();
}
}
}
export function createServer(handler: ServerHandler) {
return new Server(handler);
}
export { METHODS, STATUS_CODES };
export default {
STATUS_CODES,
METHODS,
createServer,
Server,
IncomingMessage,
ServerResponse,
};