summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2022-08-21 21:27:14 +0900
committerGitHub <noreply@github.com>2022-08-21 21:27:14 +0900
commitfb2aeb79a113e576ff2cc4f1bf3fc30741969508 (patch)
tree7a991db21a1a4e84cc2018bbfbf336e3ec0ef5e8
parent906aa78af33c8405a47d5446d2a6fb3348c275bb (diff)
fix(ext/flash): fix listening port (#15519)
-rw-r--r--cli/tests/unit/flash_test.ts17
-rw-r--r--ext/flash/01_http.js4
-rw-r--r--ext/flash/lib.rs14
3 files changed, 27 insertions, 8 deletions
diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts
index 1b979812f..4df225cbd 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 d850a1520..e8debd5ec 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 92350db58..de0a2231c 100644
--- a/ext/flash/lib.rs
+++ b/ext/flash/lib.rs
@@ -76,7 +76,7 @@ pub struct ServerContext {
rx: mpsc::Receiver<Request>,
requests: HashMap<u32, Request>,
next_token: u32,
- listening_rx: Option<mpsc::Receiver<()>>,
+ listening_rx: Option<mpsc::Receiver<u16>>,
close_tx: mpsc::Sender<()>,
cancel_handle: Rc<CancelHandle>,
}
@@ -939,7 +939,7 @@ pub struct ListenOpts {
fn run_server(
tx: mpsc::Sender<Request>,
- listening_tx: mpsc::Sender<()>,
+ listening_tx: mpsc::Sender<u16>,
mut close_rx: mpsc::Receiver<()>,
addr: SocketAddr,
maybe_cert: Option<String>,
@@ -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<impl Future<Output = Result<(), AnyError>> + 'static, AnyError> {
+) -> Result<impl Future<Output = Result<u16, AnyError>> + 'static, AnyError> {
let mut listening_rx = {
let flash_ctx = state.borrow_mut::<FlashContext>();
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)
})
}