-
Notifications
You must be signed in to change notification settings - Fork 1
/
on_tcp_connection.ts
67 lines (62 loc) · 1.7 KB
/
on_tcp_connection.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
import { ConnInfo } from "./ConnInfo.ts";
import { Handler } from "./Handler.ts";
import { Handlers } from "./Handlers.ts";
import { is_connect_or_upgrade } from "./is_connect_or_upgrade.ts";
import { on_Error } from "./on_Error.ts";
import { on_NotFound } from "./on_NotFound.ts";
import { requestEventProcessor } from "./requestEventProcessor.ts";
export async function on_tcp_connection({
conn,
handlers,
onError = on_Error,
signal,
onNotFound = on_NotFound,
}: {
signal?: AbortSignal;
conn: Deno.Conn;
handlers: Handlers;
onError?: (
// deno-lint-ignore no-explicit-any
reason: any,
request: Request,
connInfo: ConnInfo,
) => Response | PromiseLike<Response>;
onNotFound?: Handler;
}) {
if (signal?.aborted) {
return;
}
const { localAddr, remoteAddr } = conn;
// const hand_shake_info =undefined
// const { alpnProtocol } = hand_shake_info;
const conn_info: ConnInfo = {
localAddr,
alpnProtocol: null,
remoteAddr,
};
const httpConn = Deno.serveHttp(conn);
signal?.addEventListener("abort", function () {
try {
httpConn.close();
conn.close();
} catch {
/* (error) */
// console.error(error);
}
});
for await (const requestEvent of httpConn) {
if (signal?.aborted) {
return;
}
const promise = requestEventProcessor({
handlers,
requestEvent,
conn_info,
onError,
onNotFound,
}).catch(console.error);
if (is_connect_or_upgrade(requestEvent.request)) {
return await promise;
}
}
}