-
Notifications
You must be signed in to change notification settings - Fork 2
/
request_server_bun.ts
284 lines (256 loc) Β· 7.08 KB
/
request_server_bun.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2018-2024 the oak authors. All rights reserved.
import { createHttpError } from "@oak/commons/http_errors";
import { Status } from "@oak/commons/status";
import hyperid from "hyperid";
import process from "node:process";
import type {
Addr,
RequestEvent,
RequestServer,
RequestServerOptions,
} from "./types.ts";
import { createPromiseWithResolvers } from "./utils.ts";
type TypedArray =
| Uint8Array
| Uint16Array
| Uint32Array
| Int8Array
| Int16Array
| Int32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array
| Uint8ClampedArray;
type BunFile = File;
interface Bun {
serve(options: {
fetch: (req: Request, server: BunServer) => Response | Promise<Response>;
hostname?: string;
port?: number;
development?: boolean;
error?: (error: Error) => Response | Promise<Response>;
tls?: {
key?:
| string
| TypedArray
| BunFile
| Array<string | TypedArray | BunFile>;
cert?:
| string
| TypedArray
| BunFile
| Array<string | TypedArray | BunFile>;
ca?: string | TypedArray | BunFile | Array<string | TypedArray | BunFile>;
passphrase?: string;
dhParamsFile?: string;
};
maxRequestBodySize?: number;
lowMemoryMode?: boolean;
}): BunServer;
}
interface BunServer {
development: boolean;
hostname: string;
port: number;
pendingRequests: number;
requestIP(req: Request): SocketAddress | null;
stop(): void;
upgrade(req: Request, options?: {
headers?: HeadersInit;
//deno-lint-ignore no-explicit-any
data?: any;
}): boolean;
}
interface SocketAddress {
address: string;
port: number;
family: "IPv4" | "IPv6";
}
declare const Bun: Bun;
const instance = hyperid({ urlSafe: true });
/**
* The implementation of the {@linkcode RequestEvent} interface for Bun.
*/
class BunRequestEvent<
Env extends Record<string, string> = Record<string, string>,
> implements RequestEvent<Env> {
#addr: Addr;
#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 {
return this.#addr;
}
get id(): string {
return this.#id;
}
get env(): Env {
return process.env as Env;
}
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, server: BunServer) {
this.#request = request;
const socketAddr = server.requestIP(request);
this.#addr = {
hostname: socketAddr?.address ?? "",
port: socketAddr?.port ?? 0,
transport: "tcp",
};
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 createHttpError(
Status.InternalServerError,
"Request already responded to.",
);
}
this.#responded = true;
this.#reject(reason);
}
respond(response: Response): void {
if (this.#responded) {
throw createHttpError(
Status.InternalServerError,
"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)
}`;
}
}
/**
* A request server that uses the Bun HTTP server to handle requests.
*/
export default class BunRequestServer<
Env extends Record<string, string> = Record<string, string>,
> implements RequestServer<Env> {
#closed = true;
#controller?: ReadableStreamDefaultController<BunRequestEvent<Env>>;
#options: RequestServerOptions;
#server?: BunServer;
#stream?: ReadableStream<BunRequestEvent<Env>>;
get closed(): boolean {
return this.#closed;
}
constructor(options: RequestServerOptions) {
this.#options = options;
this.#options.signal.addEventListener("abort", () => {
this.#closed = true;
this.#server?.stop();
try {
this.#controller?.close();
} catch {
// just swallow here
}
this.#server = undefined;
this.#controller = undefined;
this.#stream = undefined;
}, { once: true });
}
listen(): Promise<Addr> {
if (!this.#closed) {
throw new Error("Server already listening.");
}
const { promise, resolve } = createPromiseWithResolvers<Addr>();
this.#stream = new ReadableStream<BunRequestEvent<Env>>({
start: (controller) => {
if (!Bun) {
return controller.error(
createHttpError(
Status.InternalServerError,
"Unable to start server, cannot find Bun.",
),
);
}
this.#controller = controller;
const { hostname, port } = this.#server = Bun.serve({
fetch(req, server) {
const requestEvent = new BunRequestEvent<Env>(req, server);
controller.enqueue(requestEvent);
return requestEvent.response;
},
hostname: this.#options.hostname,
port: this.#options.port,
tls: this.#options.tls,
});
resolve({ hostname, port, transport: "tcp" });
},
});
return promise;
}
[Symbol.asyncIterator](): AsyncIterableIterator<BunRequestEvent<Env>> {
if (!this.#stream) {
throw createHttpError(
Status.InternalServerError,
"Server hasn't started listening.",
);
}
return this.#stream[Symbol.asyncIterator]();
}
[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({ closed: this.#closed }, newOptions)
}`;
}
}