Skip to content

Commit 122debd

Browse files
authored
Report the real package version in the user agent (hscn/<version>) and release 1.5.1 (#65)
* Report the real package version in the user agent The user agent was hscn/{CARGO_PKG_VERSION}, but napi keeps the Cargo package version at 0.0.0, so it always sent hscn/0.0.0. build.rs now reads the version from package.json and injects it as NPM_PKG_VERSION, and the client uses that (hscn/<package-version>). build.rs reads package.json directly rather than relying on a build-env var, so the published artifacts get the correct version: the publish workflow builds each target with 'yarn build' from a checkout whose package.json holds the release version. Falls back to CARGO_PKG_VERSION if package.json can't be read. * Parse package.json with serde_json and fail the build if the version is missing Addresses review feedback: line-based parsing was brittle, and falling back to CARGO_PKG_VERSION would silently reintroduce hscn/0.0.0. Resolve package.json via CARGO_MANIFEST_DIR and emit rerun-if-changed before reading it (required because napi_build's rerun-if-env-changed directives disable cargo's default rerun). * Release 1.5.1 Bump package.json, the npm/* platform packages and the version embedded in index.js, so the release reports hscn/1.5.1.
1 parent f4c667b commit 122debd

10 files changed

Lines changed: 90 additions & 59 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ strum_macros = "0.27.2"
3232

3333
[build-dependencies]
3434
napi-build = "2.3.1"
35+
serde_json = "1"
3536

3637
[profile.release]
3738
lto = true

‎build.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
extern crate napi_build;
22

3+
use std::path::Path;
4+
35
fn main() {
46
napi_build::setup();
7+
8+
// Expose the npm package version (from package.json) so the client can report
9+
// it in its user agent. The Cargo package version is kept at 0.0.0 by napi, so
10+
// CARGO_PKG_VERSION is not usable here. The release artifacts are built via
11+
// `yarn build` from a checkout whose package.json holds the release version,
12+
// so reading it here picks up the real version regardless of the build env.
13+
// There is deliberately no fallback: silently reporting 0.0.0 is the bug this
14+
// fixes, so a missing or malformed package.json fails the build instead.
15+
let manifest_dir =
16+
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set by cargo");
17+
let package_json = Path::new(&manifest_dir).join("package.json");
18+
19+
// Required, not just an optimisation: napi_build emits rerun-if-env-changed
20+
// directives, which disables cargo's default rerun on any file change.
21+
println!("cargo:rerun-if-changed={}", package_json.display());
22+
23+
let contents = std::fs::read_to_string(&package_json)
24+
.unwrap_or_else(|e| panic!("failed to read {}: {e}", package_json.display()));
25+
let pkg: serde_json::Value = serde_json::from_str(&contents)
26+
.unwrap_or_else(|e| panic!("failed to parse {}: {e}", package_json.display()));
27+
let version = pkg
28+
.get("version")
29+
.and_then(|v| v.as_str())
30+
.unwrap_or_else(|| panic!("no string \"version\" field in {}", package_json.display()));
31+
32+
println!("cargo:rustc-env=NPM_PKG_VERSION={version}");
533
}

‎index.js‎

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.

‎npm/darwin-arm64/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envio-dev/hypersync-client-darwin-arm64",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"cpu": [
55
"arm64"
66
],

‎npm/darwin-x64/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envio-dev/hypersync-client-darwin-x64",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"cpu": [
55
"x64"
66
],

‎npm/linux-arm64-gnu/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envio-dev/hypersync-client-linux-arm64-gnu",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"cpu": [
55
"arm64"
66
],

‎npm/linux-x64-gnu/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envio-dev/hypersync-client-linux-x64-gnu",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"cpu": [
55
"x64"
66
],

‎npm/linux-x64-musl/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envio-dev/hypersync-client-linux-x64-musl",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"cpu": [
55
"x64"
66
],

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@envio-dev/hypersync-client",
33
"description": "HyperSync Client by Envio",
4-
"version": "1.5.0",
4+
"version": "1.5.1",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"napi": {

‎src/lib.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ impl HypersyncClient {
8989
/// Create a new client with given config
9090
#[napi(constructor)]
9191
pub fn new(cfg: ClientConfig) -> napi::Result<HypersyncClient> {
92-
Self::new_with_agent(cfg, format!("hscn/{}", env!("CARGO_PKG_VERSION")))
92+
// NPM_PKG_VERSION is the package.json version, injected by build.rs.
93+
// (CARGO_PKG_VERSION is 0.0.0 because napi keeps the Cargo version static.)
94+
Self::new_with_agent(cfg, format!("hscn/{}", env!("NPM_PKG_VERSION")))
9395
}
9496

9597
/// Create a new client with custom user agent

0 commit comments

Comments
 (0)