-
Notifications
You must be signed in to change notification settings - Fork 1
/
requestEventProcessor.ts
71 lines (70 loc) · 2.04 KB
/
requestEventProcessor.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
import { ConnInfo } from "./ConnInfo.ts";
import { Handler } from "./Handler.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 { on_request } from "./on_request.ts";
export async function requestEventProcessor({
handlers,
requestEvent,
conn_info,
onError = on_Error,
onNotFound = on_NotFound,
}: {
handlers: Partial<{ request: Handler; connect: Handler; upgrade: Handler }>;
requestEvent: Deno.RequestEvent;
conn_info: ConnInfo;
onError?: (
// deno-lint-ignore no-explicit-any
reason: any,
request: Request,
connInfo: ConnInfo,
) => Response | PromiseLike<Response>;
onNotFound?: Handler;
}) {
const { alpnProtocol } = conn_info;
if (handlers.connect && alpnProtocol !== "h2") {
if (requestEvent.request.method === "CONNECT") {
await on_request({
requestEvent,
connInfo: conn_info,
handler: handlers.connect,
onError,
}).catch(console.error);
return;
}
}
if (handlers.upgrade && alpnProtocol !== "h2") {
if (
requestEvent.request.headers.get("Connection")?.toLowerCase() ===
"upgrade"
) {
await on_request({
requestEvent,
connInfo: conn_info,
handler: handlers.upgrade,
onError,
}).catch(console.error);
return;
}
}
if (
handlers.request &&
(alpnProtocol === "h2" || !is_connect_or_upgrade(requestEvent.request))
) {
await on_request({
requestEvent,
connInfo: conn_info,
handler: handlers.request,
onError,
}).catch(console.error);
return;
}
await on_request({
requestEvent,
connInfo: conn_info,
handler: onNotFound,
onError,
}).catch(console.error);
return;
}