diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-02-24 23:36:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-24 23:36:07 +0100 |
commit | 484b6fe2faeec93912e1fc57b54784428cbd15d3 (patch) | |
tree | 798ac60ce328a1f18204a31c06cf2f9ef2cbae82 | |
parent | 9aebc8bc19b1dfd9bce1789018f3f6b784175171 (diff) |
refactor(flash): move remoteAddr to options bag (#17913)
Applies suggestion from #17912
-rw-r--r-- | cli/tests/unit/flash_test.ts | 5 | ||||
-rw-r--r-- | cli/tsc/dts/lib.deno.unstable.d.ts | 18 | ||||
-rw-r--r-- | ext/flash/01_http.js | 21 |
3 files changed, 29 insertions, 15 deletions
diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts index 0924aab66..ebee7a024 100644 --- a/cli/tests/unit/flash_test.ts +++ b/cli/tests/unit/flash_test.ts @@ -89,14 +89,13 @@ Deno.test({ permissions: { net: true } }, async function httpServerBasic() { const listeningPromise = deferred(); const server = Deno.serve({ - handler: async (request, getRemoteAddr) => { + handler: async (request, { remoteAddr }) => { // FIXME(bartlomieju): // make sure that request can be inspected console.log(request); assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/"); assertEquals(await request.text(), ""); - const addr = getRemoteAddr(); - assertEquals(addr.hostname, "127.0.0.1"); + assertEquals(remoteAddr.hostname, "127.0.0.1"); promise.resolve(); return new Response("Hello World", { headers: { "foo": "bar" } }); }, diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index a5ca3fc55..238c34df9 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -514,7 +514,7 @@ declare namespace Deno { * All `UnsafeCallback` are always thread safe in that they can be called from * foreign threads without crashing. However, they do not wake up the Deno event * loop by default. - * + * * If a callback is to be called from foreign threads, use the `threadSafe()` * static constructor or explicitly call `ref()` to have the callback wake up * the Deno event loop when called from foreign threads. This also stops @@ -580,7 +580,7 @@ declare namespace Deno { /** * Decrements the callback's reference counting and returns the new * reference count. - * + * * Calling `unref()` does not stop a callback from waking up the Deno * event loop when called from foreign threads. * @@ -1165,6 +1165,18 @@ declare namespace Deno { /** **UNSTABLE**: New API, yet to be vetted. * + * Information for a HTTP request. + * + * @category HTTP Server + */ + export interface ServeHandlerInfo { + /** The remote address of the connection. */ + remoteAddr: Deno.NetAddr; + } + + + /** **UNSTABLE**: New API, yet to be vetted. + * * A handler for HTTP requests. Consumes a request and returns a response. * * If a handler throws, the server calling the handler will assume the impact @@ -1173,7 +1185,7 @@ declare namespace Deno { * * @category HTTP Server */ - export type ServeHandler = (request: Request, getRemoteAddr: () => Deno.NetAddr) => Response | Promise<Response>; + export type ServeHandler = (request: Request, info: ServeHandlerInfo) => Response | Promise<Response>; /** **UNSTABLE**: New API, yet to be vetted. * diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index 79a4963a5..267a3551c 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -579,16 +579,19 @@ function createServe(opFn) { ); let resp; + let remoteAddr; try { - resp = handler(req, () => { - const { 0: hostname, 1: port } = core.ops.op_flash_addr( - serverId, - i, - ); - return { - hostname, - port, - }; + resp = handler(req, { + get remoteAddr() { + if (!remoteAddr) { + const { 0: hostname, 1: port } = core.ops.op_flash_addr( + serverId, + i, + ); + remoteAddr = { hostname, port }; + } + return remoteAddr; + }, }); if (ObjectPrototypeIsPrototypeOf(PromisePrototype, resp)) { PromisePrototypeCatch( |