-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
285 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license. | ||
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts"; | ||
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts"; | ||
import { classicAdapter, nativeAdapter } from "./_adapter.ts"; | ||
import { group } from "./_test_util.ts"; | ||
|
||
group("adapter", ({ test }) => { | ||
async function doTest() { | ||
const resp = await fetch("http://localhost:8899", { | ||
method: "POST", | ||
body: "hello", | ||
}); | ||
assertEquals(resp.status, 200); | ||
assertEquals(resp.headers.get("content-type"), "text/html"); | ||
assertEquals(await resp.text(), "hello"); | ||
} | ||
test("classic", async () => { | ||
async function serve() { | ||
const l = Deno.listen({ port: 8899 }); | ||
const conn = await l.accept(); | ||
const bufReader = new BufReader(conn); | ||
const bufWriter = new BufWriter(conn); | ||
const adapter = classicAdapter({ conn, bufReader, bufWriter }); | ||
const req = await adapter.next({}); | ||
await adapter.respond({ | ||
status: 200, | ||
headers: new Headers({ | ||
"content-type": "text/html", | ||
}), | ||
body: req!.body, | ||
}); | ||
adapter.close(); | ||
l.close(); | ||
} | ||
serve(); | ||
await doTest(); | ||
}); | ||
test("native", async () => { | ||
async function serve() { | ||
const l = Deno.listen({ port: 8899 }); | ||
const conn = await l.accept(); | ||
const adapter = nativeAdapter(conn); | ||
const req = await adapter.next({}); | ||
await adapter.respond({ | ||
status: 200, | ||
headers: new Headers({ | ||
"content-type": "text/html", | ||
}), | ||
body: req!.body, | ||
}); | ||
adapter.close(); | ||
l.close(); | ||
} | ||
serve(); | ||
await doTest(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license. | ||
export const Version = "v1.1.7"; | ||
export const Version = "v1.3.0"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.