From 5522b613f9cf731bb4f03cc9b2ded61127ccb5a6 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 2 May 2024 13:13:28 +0200 Subject: [PATCH 1/3] fix: serve handler error with 0 arguments --- ext/http/00_serve.ts | 4 ++-- tests/specs/serve/no_args/__test__.jsonc | 5 +++++ tests/specs/serve/no_args/main.out | 1 + tests/specs/serve/no_args/main.ts | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/specs/serve/no_args/__test__.jsonc create mode 100644 tests/specs/serve/no_args/main.out create mode 100644 tests/specs/serve/no_args/main.ts diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index b12a8739050be8..8af0eaa3f717df 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -793,9 +793,9 @@ internals.serveHttpOnConnection = serveHttpOnConnection; function registerDeclarativeServer(exports) { if (ObjectHasOwn(exports, "fetch")) { - if (typeof exports.fetch !== "function" || exports.fetch.length !== 1) { + if (typeof exports.fetch !== "function" || exports.fetch.length > 1) { throw new TypeError( - "Invalid type for fetch: must be a function with a single parameter", + "Invalid type for fetch: must be a function with a single or no parameter", ); } return ({ servePort, serveHost }) => { diff --git a/tests/specs/serve/no_args/__test__.jsonc b/tests/specs/serve/no_args/__test__.jsonc new file mode 100644 index 00000000000000..9a37d60ffdd189 --- /dev/null +++ b/tests/specs/serve/no_args/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "serve --port 12345 main.ts", + "output": "main.out", + "tempDir": true +} diff --git a/tests/specs/serve/no_args/main.out b/tests/specs/serve/no_args/main.out new file mode 100644 index 00000000000000..e79d76af535ffb --- /dev/null +++ b/tests/specs/serve/no_args/main.out @@ -0,0 +1 @@ +deno serve: Listening on http://localhost:12345/ diff --git a/tests/specs/serve/no_args/main.ts b/tests/specs/serve/no_args/main.ts new file mode 100644 index 00000000000000..eecac9dd4b1ea0 --- /dev/null +++ b/tests/specs/serve/no_args/main.ts @@ -0,0 +1,18 @@ +(async () => { + for (let i = 0; i < 1000; i++) { + try { + const resp = await fetch("http://localhost:12345/"); + Deno.exit(0); + } catch { + await new Promise((r) => setTimeout(r, 10)); + } + } + + Deno.exit(2); +})(); + +export default { + fetch() { + return new Response("Hello world!"); + }, +}; From e93b0fc33f1b812e38c0f98b1287469c2689adc2 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 16 May 2024 10:25:37 +0200 Subject: [PATCH 2/3] fix: only check for type function Co-authored-by: Satya Rohith Signed-off-by: Marvin Hagemeister --- ext/http/00_serve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index 8af0eaa3f717df..de94779dcd0434 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -793,7 +793,7 @@ internals.serveHttpOnConnection = serveHttpOnConnection; function registerDeclarativeServer(exports) { if (ObjectHasOwn(exports, "fetch")) { - if (typeof exports.fetch !== "function" || exports.fetch.length > 1) { + if (typeof exports.fetch !== "function") { throw new TypeError( "Invalid type for fetch: must be a function with a single or no parameter", ); From 0a18e5108ebd715c0cd706b01f2fc498e1916ee4 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Fri, 17 May 2024 11:31:51 +0530 Subject: [PATCH 3/3] update tests to use a random port --- tests/integration/serve_tests.rs | 47 +++++++++++++++++++- tests/specs/serve/no_args/__test__.jsonc | 5 --- tests/specs/serve/no_args/main.out | 1 - tests/specs/serve/no_args/main.ts | 18 -------- tests/testdata/serve/no_args.ts | 5 +++ tests/testdata/{serve.ts => serve/port_0.ts} | 0 6 files changed, 50 insertions(+), 26 deletions(-) delete mode 100644 tests/specs/serve/no_args/__test__.jsonc delete mode 100644 tests/specs/serve/no_args/main.out delete mode 100644 tests/specs/serve/no_args/main.ts create mode 100644 tests/testdata/serve/no_args.ts rename tests/testdata/{serve.ts => serve/port_0.ts} (100%) diff --git a/tests/integration/serve_tests.rs b/tests/integration/serve_tests.rs index 92b0576ed1b333..85de068c9ca409 100644 --- a/tests/integration/serve_tests.rs +++ b/tests/integration/serve_tests.rs @@ -14,12 +14,12 @@ async fn deno_serve_port_0() { .arg("serve") .arg("--port") .arg("0") - .arg("./serve.ts") + .arg("./serve/port_0.ts") .stdout_piped() .spawn() .unwrap(); let stdout = child.stdout.as_mut().unwrap(); - let mut buffer = [0; 50]; + let mut buffer = [0; 52]; let _read = stdout.read(&mut buffer).unwrap(); let msg = std::str::from_utf8(&buffer).unwrap(); let port_regex = Regex::new(r"(\d+)").unwrap(); @@ -49,3 +49,46 @@ async fn deno_serve_port_0() { child.kill().unwrap(); child.wait().unwrap(); } + +#[tokio::test] +async fn deno_serve_no_args() { + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("serve") + .arg("--port") + .arg("0") + .arg("./serve/no_args.ts") + .stdout_piped() + .spawn() + .unwrap(); + let stdout = child.stdout.as_mut().unwrap(); + let mut buffer = [0; 52]; + let _read = stdout.read(&mut buffer).unwrap(); + let msg = std::str::from_utf8(&buffer).unwrap(); + let port_regex = Regex::new(r"(\d+)").unwrap(); + let port = port_regex.find(msg).unwrap().as_str(); + + let cert = reqwest::Certificate::from_pem(include_bytes!( + "../testdata/tls/RootCA.crt" + )) + .unwrap(); + + let client = reqwest::Client::builder() + .add_root_certificate(cert) + .http2_prior_knowledge() + .build() + .unwrap(); + + let res = client + .get(&format!("http://127.0.0.1:{port}")) + .send() + .await + .unwrap(); + assert_eq!(200, res.status()); + + let body = res.text().await.unwrap(); + assert_eq!(body, "deno serve with no args in fetch() works!"); + + child.kill().unwrap(); + child.wait().unwrap(); +} diff --git a/tests/specs/serve/no_args/__test__.jsonc b/tests/specs/serve/no_args/__test__.jsonc deleted file mode 100644 index 9a37d60ffdd189..00000000000000 --- a/tests/specs/serve/no_args/__test__.jsonc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "args": "serve --port 12345 main.ts", - "output": "main.out", - "tempDir": true -} diff --git a/tests/specs/serve/no_args/main.out b/tests/specs/serve/no_args/main.out deleted file mode 100644 index e79d76af535ffb..00000000000000 --- a/tests/specs/serve/no_args/main.out +++ /dev/null @@ -1 +0,0 @@ -deno serve: Listening on http://localhost:12345/ diff --git a/tests/specs/serve/no_args/main.ts b/tests/specs/serve/no_args/main.ts deleted file mode 100644 index eecac9dd4b1ea0..00000000000000 --- a/tests/specs/serve/no_args/main.ts +++ /dev/null @@ -1,18 +0,0 @@ -(async () => { - for (let i = 0; i < 1000; i++) { - try { - const resp = await fetch("http://localhost:12345/"); - Deno.exit(0); - } catch { - await new Promise((r) => setTimeout(r, 10)); - } - } - - Deno.exit(2); -})(); - -export default { - fetch() { - return new Response("Hello world!"); - }, -}; diff --git a/tests/testdata/serve/no_args.ts b/tests/testdata/serve/no_args.ts new file mode 100644 index 00000000000000..891ddea6758634 --- /dev/null +++ b/tests/testdata/serve/no_args.ts @@ -0,0 +1,5 @@ +export default { + fetch() { + return new Response("deno serve with no args in fetch() works!"); + }, +}; diff --git a/tests/testdata/serve.ts b/tests/testdata/serve/port_0.ts similarity index 100% rename from tests/testdata/serve.ts rename to tests/testdata/serve/port_0.ts