-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_event_cfw.ts
119 lines (103 loc) Β· 2.87 KB
/
request_event_cfw.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
// Copyright 2018-2024 the oak authors. All rights reserved.
import hyperid from "hyperid";
import type {
Addr,
CloudflareExecutionContext,
RequestEvent,
} from "./types.ts";
import { createPromiseWithResolvers } from "./utils.ts";
const instance = hyperid({ urlSafe: true });
/**
* The implementation of the {@linkcode RequestEvent} interface for Cloudflare
* Workers.
*/
export class CloudflareWorkerRequestEvent<
Env extends Record<string, string> = Record<string, string>,
> implements RequestEvent<Env> {
#addr?: Addr;
#env: Env;
#id = instance();
//deno-lint-ignore no-explicit-any
#reject: (reason?: any) => void;
#request: Request;
#resolve: (value: Response | PromiseLike<Response>) => void;
#responded = false;
#response: Promise<Response>;
#url: URL;
get addr(): Addr {
if (!this.#addr) {
const hostname = this.#request.headers.get("CF-Connecting-IP") ??
"localhost";
this.#addr = { hostname, port: 80, transport: "tcp" };
}
return this.#addr;
}
get env(): Env {
return this.#env;
}
get id(): string {
return this.#id;
}
get request(): Request {
return this.#request;
}
get responded(): boolean {
return this.#responded;
}
get response(): Promise<Response> {
return this.#response;
}
get url(): URL {
return this.#url;
}
constructor(request: Request, env: Env, _ctx: CloudflareExecutionContext) {
this.#request = request;
this.#env = env;
const { resolve, reject, promise } = createPromiseWithResolvers<Response>();
this.#resolve = resolve;
this.#reject = reject;
this.#response = promise;
this.#url = URL.parse(request.url, "http://localhost/") ??
new URL("http://localhost/");
}
//deno-lint-ignore no-explicit-any
error(reason?: any): void {
if (this.#responded) {
throw new Error("Request already responded to.");
}
this.#responded = true;
this.#reject(reason);
}
respond(response: Response): void {
if (this.#responded) {
throw new Error("Request already responded to.");
}
this.#responded = true;
this.#resolve(response);
}
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
// deno-lint-ignore no-explicit-any
): any {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}
const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
return `${options.stylize(this.constructor.name, "special")} ${
inspect({
addr: this.#addr,
env: this.#env,
id: this.#id,
request: this.#request,
responded: this.#responded,
response: this.#response,
url: this.#url,
}, newOptions)
}`;
}
}