-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
_adapter.ts
122 lines (115 loc) · 2.95 KB
/
_adapter.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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { createBodyParser } from "./body_parser.ts";
import { readRequest, setupBodyInit, writeResponse } from "./serveio.ts";
import {
BodyReader,
IncomingRequest,
ServeOptions,
ServerResponse,
} from "./server.ts";
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
import { closableBodyReader, noopReader, streamReader } from "./_readers.ts";
export interface HttpApiAdapter {
next(opts: ServeOptions): Promise<IncomingRequest | undefined>;
respond(resp: ServerResponse): Promise<void>;
close(): void;
}
export function classicAdapter({ conn, bufReader, bufWriter }: {
conn: Deno.Conn;
bufReader: BufReader;
bufWriter: BufWriter;
}): HttpApiAdapter {
return {
async next(opts) {
return readRequest(bufReader, opts);
},
async respond(resp) {
await writeResponse(bufWriter, resp);
},
close() {
conn.close();
},
};
}
export interface RequestEvent {
readonly request: Request;
respondWith(r: Response | Promise<Response>): void;
}
export interface HttpConn extends AsyncIterable<RequestEvent> {
readonly rid: number;
nextRequest(): Promise<RequestEvent | null>;
close(): void;
}
export function nativeAdapter(conn: Deno.Conn): HttpApiAdapter {
// @ts-ignore
const http: HttpConn = Deno.serveHttp(conn);
let ev: RequestEvent | null;
let closed = false;
return {
async next() {
ev = await http.nextRequest();
if (!ev) {
closed = true;
return;
}
return requestFromEvent(ev);
},
async respond(resp) {
if (!ev) throw new Error("Unexpected respond");
const headers = resp.headers ?? new Headers();
let body: BodyInit | undefined;
if (resp.body) {
const [_body, contentType] = setupBodyInit(resp.body);
body = _body;
if (!headers.has("content-type")) {
headers.set("content-type", contentType);
}
}
// TODO: trailer
try {
await ev.respondWith(
new Response(body, {
status: resp.status,
headers,
}),
);
} finally {
ev = null;
}
},
close() {
if (!closed) {
http.close();
}
},
};
}
function requestFromEvent(ev: RequestEvent): IncomingRequest {
const { pathname, search, searchParams } = new URL(
ev.request.url,
"http://dummy",
);
const { method, headers } = ev.request;
const contentType = headers.get("content-type") ?? "";
let body: BodyReader;
if (ev.request.body) {
body = closableBodyReader(streamReader(ev.request.body));
} else {
body = closableBodyReader(noopReader());
}
const bodyParser = createBodyParser({
reader: body,
contentType,
});
return {
url: pathname + search,
path: pathname,
query: searchParams,
method,
proto: "HTTP/1.1",
headers,
cookies: new Map(),
body,
...bodyParser,
};
}