From 358fe3a8e4470a173ff73c5d6997b10072429e4a Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 19:13:26 +0530 Subject: [PATCH 1/5] feat(net/util.rs): use a standardized user agent format --- src/lune/builtins/net/mod.rs | 2 +- src/lune/builtins/net/util.rs | 19 +++++++++++++------ src/lune/globals/version.rs | 5 +++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/lune/builtins/net/mod.rs b/src/lune/builtins/net/mod.rs index 6253dec6..9ea59509 100644 --- a/src/lune/builtins/net/mod.rs +++ b/src/lune/builtins/net/mod.rs @@ -23,7 +23,7 @@ use super::serde::encode_decode::{EncodeDecodeConfig, EncodeDecodeFormat}; pub fn create(lua: &Lua) -> LuaResult { NetClientBuilder::new() - .headers(&[("User-Agent", create_user_agent_header())])? + .headers(&[("User-Agent", create_user_agent_header(lua)?)])? .build()? .into_registry(lua); TableBuilder::new(lua)? diff --git a/src/lune/builtins/net/util.rs b/src/lune/builtins/net/util.rs index 46035474..e18235e2 100644 --- a/src/lune/builtins/net/util.rs +++ b/src/lune/builtins/net/util.rs @@ -7,12 +7,19 @@ use mlua::prelude::*; use crate::lune::util::TableBuilder; -pub fn create_user_agent_header() -> String { - let (github_owner, github_repo) = env!("CARGO_PKG_REPOSITORY") - .trim_start_matches("https://github.com/") - .split_once('/') - .unwrap(); - format!("{github_owner}-{github_repo}-cli") +pub fn create_user_agent_header(lua: &Lua) -> LuaResult { + let version_global = lua + .globals() + .get::<_, LuaString>("_VERSION") + .expect("Missing _VERSION global"); + + let version_global_str = version_global + .to_str() + .context("Invalid utf8 found in _VERSION global")?; + + let (package_name, full_version) = version_global_str.split_once(' ').unwrap(); + + Ok(format!("{}/{}", package_name.to_lowercase(), full_version)) } pub fn header_map_to_table( diff --git a/src/lune/globals/version.rs b/src/lune/globals/version.rs index 4904144e..8ca5864c 100644 --- a/src/lune/globals/version.rs +++ b/src/lune/globals/version.rs @@ -1,19 +1,20 @@ use mlua::prelude::*; pub fn create(lua: &Lua) -> LuaResult> { - let lune_version = format!("Lune {}", env!("CARGO_PKG_VERSION")); + let lune_version = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); let luau_version_full = lua .globals() .get::<_, LuaString>("_VERSION") .expect("Missing _VERSION global"); + let luau_version_str = luau_version_full .to_str() .context("Invalid utf8 found in _VERSION global")?; // If this function runs more than once, we // may get an already formatted lune version. - if luau_version_str.starts_with(&lune_version) { + if luau_version_str.starts_with(lune_version.as_str()) { return Ok(luau_version_full); } From d07168d60ba220e0d597526c11f05aa9ea829da1 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 19:22:07 +0530 Subject: [PATCH 2/5] chore(tests): include test for custom user agent --- tests/net/request/user_agent.luau | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/net/request/user_agent.luau diff --git a/tests/net/request/user_agent.luau b/tests/net/request/user_agent.luau new file mode 100644 index 00000000..04491b7e --- /dev/null +++ b/tests/net/request/user_agent.luau @@ -0,0 +1,9 @@ +local net = require("@lune/net") + +local runtime, version = table.unpack(_VERSION:split(" ")) +local expectedUserAgent = runtime:lower() .. "/" .. version + +local userAgent: string = + net.jsonDecode(net.request("https://www.whatsmyua.info/api/v1/ua").body)[1].ua.rawUa + +assert(userAgent == expectedUserAgent, "Expected user agent to be " .. expectedUserAgent) From ac8d3a944d947ccc4518cf55f7738a8282887c3a Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 20:08:08 +0530 Subject: [PATCH 3/5] fix: revert accidentally lowercased version global --- src/lune/globals/version.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lune/globals/version.rs b/src/lune/globals/version.rs index 8ca5864c..8774c9d1 100644 --- a/src/lune/globals/version.rs +++ b/src/lune/globals/version.rs @@ -1,13 +1,12 @@ use mlua::prelude::*; pub fn create(lua: &Lua) -> LuaResult> { - let lune_version = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); - + let lune_version = format!("Lune {}", env!("CARGO_PKG_VERSION")); + let luau_version_full = lua .globals() .get::<_, LuaString>("_VERSION") .expect("Missing _VERSION global"); - let luau_version_str = luau_version_full .to_str() .context("Invalid utf8 found in _VERSION global")?; From 3ee1b28e30c0236aa33e3068aeeb294ab6145f0d Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 20:09:50 +0530 Subject: [PATCH 4/5] chore: fixup formatting From d9f645cd43b8bd1aa11b1a88d5347ddc7a364703 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 20:14:08 +0530 Subject: [PATCH 5/5] chore: fixup formatting (x2) --- src/lune/globals/version.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lune/globals/version.rs b/src/lune/globals/version.rs index 8774c9d1..1228fd05 100644 --- a/src/lune/globals/version.rs +++ b/src/lune/globals/version.rs @@ -2,7 +2,7 @@ use mlua::prelude::*; pub fn create(lua: &Lua) -> LuaResult> { let lune_version = format!("Lune {}", env!("CARGO_PKG_VERSION")); - + let luau_version_full = lua .globals() .get::<_, LuaString>("_VERSION")