From fb2aeb79a113e576ff2cc4f1bf3fc30741969508 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sun, 21 Aug 2022 21:27:14 +0900 Subject: [PATCH] fix(ext/flash): fix listening port (#15519) --- cli/tests/unit/flash_test.ts | 17 +++++++++++++++++ ext/flash/01_http.js | 4 ++-- ext/flash/lib.rs | 14 ++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts index 1b979812fc..4df225cbd4 100644 --- a/cli/tests/unit/flash_test.ts +++ b/cli/tests/unit/flash_test.ts @@ -72,6 +72,23 @@ Deno.test({ permissions: { net: true } }, async function httpServerBasic() { await server; }); +Deno.test({ permissions: { net: true } }, async function httpServerPort0() { + const ac = new AbortController(); + + const server = Deno.serve({ + fetch() { + return new Response("Hello World"); + }, + port: 0, + signal: ac.signal, + onListen({ port }) { + assert(port > 0 && port < 65536); + ac.abort(); + }, + }); + await server; +}); + // https://github.com/denoland/deno/issues/15107 Deno.test( { permissions: { net: true } }, diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index d850a15201..e8debd5ec3 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -216,8 +216,8 @@ const serverId = core.ops.op_flash_serve(opts); const serverPromise = core.opAsync("op_flash_drive_server", serverId); - core.opAsync("op_flash_wait_for_listening", serverId).then(() => { - onListen({ hostname: opts.hostname, port: opts.port }); + core.opAsync("op_flash_wait_for_listening", serverId).then((port) => { + onListen({ hostname: opts.hostname, port }); }); const server = { diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 92350db585..de0a2231ca 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -76,7 +76,7 @@ pub struct ServerContext { rx: mpsc::Receiver, requests: HashMap, next_token: u32, - listening_rx: Option>, + listening_rx: Option>, close_tx: mpsc::Sender<()>, cancel_handle: Rc, } @@ -939,7 +939,7 @@ pub struct ListenOpts { fn run_server( tx: mpsc::Sender, - listening_tx: mpsc::Sender<()>, + listening_tx: mpsc::Sender, mut close_rx: mpsc::Receiver<()>, addr: SocketAddr, maybe_cert: Option, @@ -971,7 +971,9 @@ fn run_server( } }; - listening_tx.blocking_send(()).unwrap(); + listening_tx + .blocking_send(listener.local_addr().unwrap().port()) + .unwrap(); let mut sockets = HashMap::with_capacity(1000); let mut counter: usize = 1; let mut events = Events::with_capacity(1024); @@ -1274,7 +1276,7 @@ where fn op_flash_wait_for_listening( state: &mut OpState, server_id: u32, -) -> Result> + 'static, AnyError> { +) -> Result> + 'static, AnyError> { let mut listening_rx = { let flash_ctx = state.borrow_mut::(); let server_ctx = flash_ctx @@ -1284,8 +1286,8 @@ fn op_flash_wait_for_listening( server_ctx.listening_rx.take().unwrap() }; Ok(async move { - listening_rx.recv().await; - Ok(()) + let port = listening_rx.recv().await.unwrap(); + Ok(port) }) }