-
Notifications
You must be signed in to change notification settings - Fork 233
/
http_server_native.ts
114 lines (101 loc) · 3.05 KB
/
http_server_native.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
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
/** The abstraction that oak uses when dealing with requests and responses
* within the Deno runtime.
*
* @module
*/
import type { Application, State } from "./application.ts";
import { NativeRequest } from "./http_server_native_request.ts";
import type {
HttpServer,
Listener,
OakServer,
ServeInit,
ServeOptions,
ServeTlsOptions,
} from "./types.ts";
import { createPromiseWithResolvers } from "./utils/create_promise_with_resolvers.ts";
const serve:
| ((
options: ServeInit & (ServeOptions | ServeTlsOptions),
) => HttpServer)
| undefined = "Deno" in globalThis && "serve" in globalThis.Deno
? globalThis.Deno.serve.bind(globalThis.Deno)
: undefined;
/** The oak abstraction of the Deno native HTTP server which is used internally
* for handling native HTTP requests. Generally users of oak do not need to
* worry about this class. */
// deno-lint-ignore no-explicit-any
export class Server<AS extends State = Record<string, any>>
implements OakServer<NativeRequest> {
#app: Application<AS>;
#closed = false;
#httpServer?: HttpServer;
#options: ServeOptions | ServeTlsOptions;
#stream?: ReadableStream<NativeRequest>;
constructor(
app: Application<AS>,
options: Omit<ServeOptions | ServeTlsOptions, "signal">,
) {
if (!serve) {
throw new Error(
"The native bindings for serving HTTP are not available.",
);
}
this.#app = app;
this.#options = options;
}
get app(): Application<AS> {
return this.#app;
}
get closed(): boolean {
return this.#closed;
}
async close(): Promise<void> {
if (this.#closed) {
return;
}
if (this.#httpServer) {
this.#httpServer.unref();
await this.#httpServer.shutdown();
this.#httpServer = undefined;
}
this.#closed = true;
}
listen(): Promise<Listener> {
if (this.#httpServer) {
throw new Error("Server already listening.");
}
const { signal } = this.#options;
const { onListen, ...options } = this.#options;
const { promise, resolve } = createPromiseWithResolvers<Listener>();
this.#stream = new ReadableStream<NativeRequest>({
start: (controller) => {
this.#httpServer = serve?.({
handler: (req, info) => {
const nativeRequest = new NativeRequest(req, info);
controller.enqueue(nativeRequest);
return nativeRequest.response;
},
onListen({ hostname, port }) {
if (onListen) {
onListen({ hostname, port });
}
resolve({ addr: { hostname, port } });
},
signal,
...options,
});
},
});
signal?.addEventListener("abort", () => this.close(), { once: true });
return promise;
}
[Symbol.asyncIterator](): AsyncIterableIterator<NativeRequest> {
if (!this.#stream) {
throw new TypeError("Server hasn't started listening.");
}
return this.#stream[Symbol.asyncIterator]();
}
static type: "native" = "native";
}