forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_std.ts
56 lines (48 loc) · 1.55 KB
/
http_server_std.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
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license.
import type { Application, State } from "./application.ts";
import { serve, serveTLS } from "./deps.ts";
import type { BufReader, BufWriter } from "./deps.ts";
import type { Server } from "./types.d.ts";
import { isListenTlsOptions } from "./util.ts";
interface StdServer extends AsyncIterable<ServerRequest> {
close(): void;
[Symbol.asyncIterator](): AsyncIterableIterator<ServerRequest>;
}
/** An interface that aligns to the parts of `std/http/server`'s
* `ServerRequest` that actually is consumed by oak. */
export interface ServerRequest {
body: Deno.Reader;
conn: Deno.Conn;
headers: Headers;
method: string;
r: BufReader;
respond(response: ServerResponse): Promise<void>;
url: string;
w: BufWriter;
}
/** An interface that aligns to what oak returns and is compatible with
* `std/http/server`'s `request.respond()`. */
export interface ServerResponse {
status: number;
headers: Headers;
body: Uint8Array | Deno.Reader | undefined;
}
// deno-lint-ignore no-explicit-any
export class HttpServerStd<AS extends State = Record<string, any>>
implements Server<ServerRequest> {
#server: StdServer;
constructor(
_app: Application<AS>,
options: Deno.ListenOptions | Deno.ListenTlsOptions,
) {
this.#server = isListenTlsOptions(options)
? serveTLS(options)
: serve(options);
}
close(): void {
this.#server.close();
}
[Symbol.asyncIterator](): AsyncIterableIterator<ServerRequest> {
return this.#server[Symbol.asyncIterator]();
}
}