Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3db02e5

Browse files
authoredMay 5, 2025··
Merge branch 'main' into f_cache_env_file
2 parents 12a742a + f47c4ea commit 3db02e5

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed
 

‎cli/main.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ async fn run_subcommand(
292292
}
293293
}),
294294
DenoSubcommand::Serve(serve_flags) => spawn_subcommand(async move {
295-
tools::serve::serve(flags, serve_flags).await
295+
tools::serve::serve(flags, serve_flags, unconfigured_runtime, roots).await
296296
}),
297297
DenoSubcommand::Task(task_flags) => spawn_subcommand(async {
298298
tools::task::execute_script(flags, task_flags).await
@@ -610,6 +610,8 @@ fn wait_for_start(
610610
Some(async move {
611611
use tokio::io::AsyncBufReadExt;
612612
use tokio::io::AsyncRead;
613+
use tokio::io::AsyncWrite;
614+
use tokio::io::AsyncWriteExt;
613615
use tokio::io::BufReader;
614616
use tokio::net::TcpListener;
615617
use tokio::net::UnixSocket;
@@ -630,18 +632,23 @@ fn wait_for_start(
630632
vec![],
631633
);
632634

633-
let stream: Box<dyn AsyncRead + Unpin> = match addr.split_once(':') {
635+
let (rx, mut tx): (
636+
Box<dyn AsyncRead + Unpin>,
637+
Box<dyn AsyncWrite + Send + Unpin>,
638+
) = match addr.split_once(':') {
634639
Some(("tcp", addr)) => {
635640
let listener = TcpListener::bind(addr).await?;
636641
let (stream, _) = listener.accept().await?;
637-
Box::new(stream)
642+
let (rx, tx) = stream.into_split();
643+
(Box::new(rx), Box::new(tx))
638644
}
639645
Some(("unix", path)) => {
640646
let socket = UnixSocket::new_stream()?;
641647
socket.bind(path)?;
642648
let listener = socket.listen(1)?;
643649
let (stream, _) = listener.accept().await?;
644-
Box::new(stream)
650+
let (rx, tx) = stream.into_split();
651+
(Box::new(rx), Box::new(tx))
645652
}
646653
Some(("vsock", addr)) => {
647654
let Some((cid, port)) = addr.split_once(':') else {
@@ -652,17 +659,28 @@ fn wait_for_start(
652659
let addr = VsockAddr::new(cid, port);
653660
let listener = VsockListener::bind(addr)?;
654661
let (stream, _) = listener.accept().await?;
655-
Box::new(stream)
662+
let (rx, tx) = stream.into_split();
663+
(Box::new(rx), Box::new(tx))
656664
}
657665
_ => {
658666
deno_core::anyhow::bail!("invalid control sock");
659667
}
660668
};
661669

662-
let mut stream = BufReader::new(stream);
663-
664670
let mut buf = Vec::with_capacity(1024);
665-
stream.read_until(b'\n', &mut buf).await?;
671+
BufReader::new(rx).read_until(b'\n', &mut buf).await?;
672+
673+
tokio::spawn(async move {
674+
deno_runtime::deno_http::SERVE_NOTIFIER.notified().await;
675+
676+
#[derive(deno_core::serde::Serialize)]
677+
enum Event {
678+
Serving,
679+
}
680+
681+
let buf = deno_core::serde_json::to_vec(&Event::Serving).unwrap();
682+
let _ = tx.write_all(&buf).await;
683+
});
666684

667685
#[derive(deno_core::serde::Deserialize)]
668686
struct Start {

‎cli/tools/serve.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use deno_core::error::AnyError;
77
use deno_core::futures::FutureExt;
88
use deno_core::futures::TryFutureExt;
99
use deno_core::ModuleSpecifier;
10+
use deno_lib::worker::LibWorkerFactoryRoots;
11+
use deno_runtime::UnconfiguredRuntime;
1012

1113
use super::run::check_permission_before_script;
1214
use super::run::maybe_npm_install;
@@ -20,6 +22,8 @@ use crate::worker::CliMainWorkerFactory;
2022
pub async fn serve(
2123
flags: Arc<Flags>,
2224
serve_flags: ServeFlags,
25+
unconfigured_runtime: Option<UnconfiguredRuntime>,
26+
roots: LibWorkerFactoryRoots,
2327
) -> Result<i32, AnyError> {
2428
check_permission_before_script(&flags);
2529

@@ -45,8 +49,11 @@ pub async fn serve(
4549

4650
maybe_npm_install(&factory).await?;
4751

48-
let worker_factory =
49-
Arc::new(factory.create_cli_main_worker_factory().await?);
52+
let worker_factory = Arc::new(
53+
factory
54+
.create_cli_main_worker_factory_with_roots(roots)
55+
.await?,
56+
);
5057

5158
if serve_flags.open_site {
5259
let url = resolve_serve_url(serve_flags.host, serve_flags.port);
@@ -62,6 +69,7 @@ pub async fn serve(
6269
main_module.clone(),
6370
serve_flags.worker_count,
6471
hmr,
72+
unconfigured_runtime,
6573
)
6674
.await
6775
}
@@ -71,14 +79,16 @@ async fn do_serve(
7179
main_module: ModuleSpecifier,
7280
worker_count: Option<usize>,
7381
hmr: bool,
82+
unconfigured_runtime: Option<UnconfiguredRuntime>,
7483
) -> Result<i32, AnyError> {
7584
let mut worker = worker_factory
76-
.create_main_worker(
85+
.create_main_worker_with_unconfigured_runtime(
7786
deno_runtime::WorkerExecutionMode::Serve {
7887
is_main: true,
7988
worker_count,
8089
},
8190
main_module.clone(),
91+
unconfigured_runtime,
8292
)
8393
.await?;
8494
let worker_count = match worker_count {
@@ -176,7 +186,7 @@ async fn serve_with_watch(
176186
let worker_factory =
177187
Arc::new(factory.create_cli_main_worker_factory().await?);
178188

179-
do_serve(worker_factory, main_module.clone(), worker_count, hmr)
189+
do_serve(worker_factory, main_module.clone(), worker_count, hmr, None)
180190
.await?;
181191

182192
Ok(())

‎cli/tsc/dts/lib.deno.ns.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ declare namespace Deno {
15101510
*
15111511
* ```ts
15121512
* console.log(Deno.env.get("HOME")); // e.g. outputs "/home/alice"
1513-
* console.log(Deno.env.get("MADE_UP_VAR")); // outputs "undefined"
1513+
* console.log(Deno.env.get("MADE_UP_VAR")); // outputs undefined
15141514
* ```
15151515
*
15161516
* Requires `allow-env` permission.

‎ext/http/00_serve.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
op_http_get_request_headers,
1515
op_http_get_request_method_and_url,
1616
op_http_metric_handle_otel_error,
17+
op_http_notify_serving,
1718
op_http_read_request_body,
1819
op_http_request_on_cancel,
1920
op_http_serve,
@@ -1049,6 +1050,8 @@ function serveHttpOn(context, addr, callback) {
10491050
}
10501051
})();
10511052

1053+
op_http_notify_serving();
1054+
10521055
return {
10531056
addr,
10541057
finished,

‎ext/http/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use std::net::SocketAddr;
1515
use std::pin::pin;
1616
use std::pin::Pin;
1717
use std::rc::Rc;
18+
use std::sync::atomic::AtomicBool;
19+
use std::sync::atomic::Ordering;
1820
use std::sync::Arc;
1921
use std::task::ready;
2022
use std::task::Context;
@@ -80,6 +82,7 @@ use serde::Serialize;
8082
use tokio::io::AsyncRead;
8183
use tokio::io::AsyncWrite;
8284
use tokio::io::AsyncWriteExt;
85+
use tokio::sync::Notify;
8386

8487
use crate::network_buffered_stream::NetworkBufferedStream;
8588
use crate::reader_stream::ExternallyAbortableReaderStream;
@@ -201,6 +204,7 @@ deno_core::extension!(
201204
op_http_write_headers,
202205
op_http_write_resource,
203206
op_http_write,
207+
op_http_notify_serving,
204208
http_next::op_http_close_after_finish,
205209
http_next::op_http_get_request_header,
206210
http_next::op_http_get_request_headers,
@@ -1764,6 +1768,20 @@ fn parse_serve_address(input: &str) -> (u8, String, u32, bool) {
17641768
}
17651769
}
17661770

1771+
pub static SERVE_NOTIFIER: Notify = Notify::const_new();
1772+
1773+
#[op2(fast)]
1774+
fn op_http_notify_serving() {
1775+
static ONCE: AtomicBool = AtomicBool::new(false);
1776+
1777+
if ONCE
1778+
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
1779+
.is_ok()
1780+
{
1781+
SERVE_NOTIFIER.notify_one();
1782+
}
1783+
}
1784+
17671785
#[cfg(test)]
17681786
mod tests {
17691787
use super::*;

‎tests/specs/run/unconfigured/main.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
true
22
hello world
3+
EVENT: "Serving"
34
[Object: null prototype] { success: true, code: 0, signal: null }

‎tests/specs/run/unconfigured/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Deno.writeTextFile(
3636
`
3737
console.log(Deno[Deno.internal].isFromUnconfiguredRuntime());
3838
console.log(Deno.env.get('A'));
39+
Deno.serve({ onListen() {} }, () => {}).shutdown();
3940
`,
4041
);
4142

@@ -47,4 +48,9 @@ const data = JSON.stringify({
4748

4849
await sock.write(new TextEncoder().encode(data + "\n"));
4950

51+
const buf = new Uint8Array(128);
52+
const n = await sock.read(buf);
53+
54+
console.log("EVENT:", new TextDecoder().decode(buf.subarray(0, n)));
55+
5056
console.log(await child.status);

0 commit comments

Comments
 (0)
Please sign in to comment.