diff --git a/build.rs b/build.rs index 9fc2367..f7966ef 100644 --- a/build.rs +++ b/build.rs @@ -2,4 +2,28 @@ extern crate napi_build; fn main() { napi_build::setup(); + + // Expose the npm package version (from package.json) so the client can report + // it in its user agent. The Cargo package version is kept at 0.0.0 by napi, so + // CARGO_PKG_VERSION is not usable here. The release artifacts are built via + // `yarn build` from a checkout whose package.json holds the release version, + // so reading it here picks up the real version regardless of the build env. + // Falls back to CARGO_PKG_VERSION if package.json can't be read. + let version = std::fs::read_to_string("package.json") + .ok() + .and_then(|pkg| { + pkg.lines().find_map(|line| { + line.trim() + .strip_prefix("\"version\":") + .map(|rest| { + rest.trim() + .trim_matches(|c| c == ',' || c == '"' || c == ' ') + .to_string() + }) + }) + }) + .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string()); + + println!("cargo:rustc-env=NPM_PKG_VERSION={version}"); + println!("cargo:rerun-if-changed=package.json"); } diff --git a/src/lib.rs b/src/lib.rs index 277ea07..73bd014 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,9 @@ impl HypersyncClient { /// Create a new client with given config #[napi(constructor)] pub fn new(cfg: ClientConfig) -> napi::Result { - Self::new_with_agent(cfg, format!("hscn/{}", env!("CARGO_PKG_VERSION"))) + // NPM_PKG_VERSION is the package.json version, injected by build.rs. + // (CARGO_PKG_VERSION is 0.0.0 because napi keeps the Cargo version static.) + Self::new_with_agent(cfg, format!("hscn/{}", env!("NPM_PKG_VERSION"))) } /// Create a new client with custom user agent