-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 changed file
with
7 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,23 +8,8 @@ import { | |
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { CookieJar } from "./cookie_jar.ts"; | ||
import { wrapFetch } from "./fetch_wrapper.ts"; | ||
import { delay } from "https://deno.land/[email protected]/async/delay.ts"; | ||
import { Cookie } from "./cookie.ts"; | ||
|
||
function drop(resourceName: string) { | ||
const rt: Deno.ResourceMap = Deno.resources(); | ||
for (const rid in rt) { | ||
if (rt[rid] == resourceName) { | ||
try { | ||
Deno.close(Number(rid)); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
const serverOnePort = 53250; | ||
const serverTwoPort = 53251; | ||
const serverOneHostname = "127.0.0.1"; | ||
|
@@ -123,44 +108,17 @@ type ListenAndServeOptions = { | |
abortController: AbortController; | ||
}; | ||
|
||
async function listenAndServe(options: ListenAndServeOptions) { | ||
const { hostname, port, abortController } = options; | ||
|
||
const listener = Deno.listen({ hostname, port }); | ||
|
||
const handleAbort = () => { | ||
abortController.signal.removeEventListener("abort", handleAbort); | ||
listener.close(); | ||
drop("httpConn"); | ||
}; | ||
|
||
abortController.signal.addEventListener("abort", handleAbort); | ||
|
||
try { | ||
for await (const conn of listener) { | ||
for await ( | ||
const { respondWith, request } of Deno.serveHttp(conn) | ||
) { | ||
respondWith(serverHandler(request)); | ||
} | ||
} | ||
} catch { | ||
drop("httpConn"); | ||
} | ||
} | ||
|
||
function runServer( | ||
options: Omit<ListenAndServeOptions, "abortController">, | ||
) { | ||
const { hostname, port } = options; | ||
const abortController = new AbortController(); | ||
listenAndServe({ | ||
...options, | ||
abortController, | ||
}).catch((e) => { | ||
abortController.abort(); | ||
delay(10); | ||
throw e; | ||
}); | ||
|
||
Deno.serve( | ||
{ signal: abortController.signal, hostname, port, onListen() {} }, | ||
(req) => serverHandler(req), | ||
); | ||
|
||
return abortController; | ||
} | ||
|
||
|