alef 0.72.0

Opinionated polyglot binding generator for Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! NAPI-RS Node.js native binding packager.
//!
//! Produces a per-platform npm sub-package directory and runs `npm pack` to
//! generate a tarball. The sub-package follows the `@scope/{name}-{platform}`
//! naming convention used by napi-rs.
//!
//! Platform list is read from `[publish.languages.node] npm_subpackage_platforms`
//! in alef.toml. When absent, a sensible default set is used.

use super::PackageArtifact;
use crate::core::config::ResolvedCrateConfig;
use crate::core::template_versions as tv;
use crate::publish::package::BuildProfile;
use crate::publish::platform::RustTarget;
use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};

/// Default set of NAPI platform identifiers when the config is absent.
const DEFAULT_PLATFORMS: &[&str] = &[
    "linux-x64-gnu",
    "linux-arm64-gnu",
    "linux-x64-musl",
    "linux-arm64-musl",
    "darwin-x64",
    "darwin-arm64",
    "win32-x64-msvc",
    "win32-arm64-msvc",
];

/// Package a NAPI native binding for one target into a per-platform npm sub-package.
///
/// Produces: `{scope}-{name}-{platform}-{version}.tgz`
///
/// Steps:
/// 1. Locate the `.node` binary from `target/{triple}/release/` or `target/release/`.
/// 2. Create `output_dir/npm/{platform}/` with `package.json` + `.node` binary.
/// 3. Run `npm pack` inside that directory and move the `.tgz` to `output_dir`.
pub fn package_node(
    config: &ResolvedCrateConfig,
    target: &RustTarget,
    workspace_root: &Path,
    output_dir: &Path,
    version: &str,
) -> Result<PackageArtifact> {
    let platform = target.platform_for(crate::core::config::extras::Language::Node);
    let node_pkg_name = config.node_package_name();
    let base_name = if let Some(slash_pos) = node_pkg_name.rfind('/') {
        &node_pkg_name[slash_pos + 1..]
    } else {
        node_pkg_name.as_str()
    };
    let scope = if node_pkg_name.starts_with('@') {
        let at_end = node_pkg_name.find('/').map(|i| &node_pkg_name[..i]);
        at_end.map(|s| s.to_string())
    } else {
        None
    };

    let node_crate = crate::publish::crate_name_from_output(config, crate::core::config::extras::Language::Node)
        .unwrap_or_else(|| format!("{}-node", config.name));
    let node_lib_name = format!("{}.{}.node", base_name, platform);
    let node_lib_simple = format!("{}.node", base_name.replace('-', "_"));

    let node_bin = find_node_binary(workspace_root, target, &node_crate, &node_lib_name, &node_lib_simple)?;

    let platform_dir = output_dir.join("npm").join(&platform);
    if platform_dir.exists() {
        fs::remove_dir_all(&platform_dir)?;
    }
    fs::create_dir_all(&platform_dir)?;

    let dest_bin_name = format!("{base_name}.{platform}.node");
    fs::copy(&node_bin, platform_dir.join(&dest_bin_name))
        .with_context(|| format!("copying .node binary to {}", platform_dir.display()))?;

    let sub_pkg_name = match &scope {
        Some(s) => format!("{s}/{base_name}-{platform}"),
        None => format!("{base_name}-{platform}"),
    };
    let (pkg_os, pkg_cpu, pkg_libc) = platform_to_os_cpu_libc(&platform);
    let metadata = package_metadata(config);
    let pkg_json = generate_sub_package_json(
        &sub_pkg_name,
        version,
        &dest_bin_name,
        pkg_os,
        pkg_cpu,
        pkg_libc,
        &metadata,
    );
    fs::write(platform_dir.join("package.json"), pkg_json)?;

    let readme = format!("# {sub_pkg_name}\n\nNative binary for {platform}.\n");
    fs::write(platform_dir.join("README.md"), readme)?;

    crate::publish::run_shell_command_in("npm pack", &platform_dir)?;

    let tgz = find_tgz(&platform_dir).context("npm pack: no .tgz found")?;
    let tgz_name = tgz
        .file_name()
        .context("tgz has no name")?
        .to_string_lossy()
        .to_string();
    let tgz_dest = output_dir.join(&tgz_name);
    fs::rename(&tgz, &tgz_dest)?;

    Ok(PackageArtifact {
        path: tgz_dest,
        name: tgz_name,
        checksum: None,
    })
}

/// Return the configured npm subpackage platforms for Node, or the default set.
pub fn npm_platforms(config: &ResolvedCrateConfig) -> Vec<String> {
    if let Some(publish) = &config.publish
        && let Some(lang_cfg) = publish.languages.get("node")
        && let Some(platforms) = &lang_cfg.npm_subpackage_platforms
        && !platforms.is_empty()
    {
        return platforms.clone();
    }
    DEFAULT_PLATFORMS.iter().map(|s| s.to_string()).collect()
}

/// Map a napi platform string to (os, cpu, optional libc) for package.json fields.
fn platform_to_os_cpu_libc(platform: &str) -> (&'static str, &'static str, Option<&'static str>) {
    match platform {
        "linux-x64-gnu" => ("linux", "x64", Some("glibc")),
        "linux-x64-musl" => ("linux", "x64", Some("musl")),
        "linux-arm64-gnu" => ("linux", "arm64", Some("glibc")),
        "linux-arm64-musl" => ("linux", "arm64", Some("musl")),
        "darwin-x64" => ("darwin", "x64", None),
        "darwin-arm64" => ("darwin", "arm64", None),
        "win32-x64-msvc" => ("win32", "x64", None),
        "win32-arm64-msvc" => ("win32", "arm64", None),
        "linux-arm-gnueabihf" => ("linux", "arm", Some("glibc")),
        _ => ("linux", "x64", None),
    }
}

struct PackageMetadata {
    license: Option<String>,
    repository_url: Option<String>,
}

fn package_metadata(config: &ResolvedCrateConfig) -> PackageMetadata {
    let meta = crate::scaffold::scaffold_meta(config);
    let repository_url = meta.configured_repository.map(|repository| {
        if repository.starts_with("git+") {
            repository
        } else {
            format!("git+{}.git", repository.trim_end_matches('/').trim_end_matches(".git"))
        }
    });
    PackageMetadata {
        license: meta.license,
        repository_url,
    }
}

fn generate_sub_package_json(
    name: &str,
    version: &str,
    bin_file: &str,
    os: &str,
    cpu: &str,
    libc: Option<&str>,
    metadata: &PackageMetadata,
) -> String {
    let libc_field = if let Some(l) = libc {
        format!(",\n  \"libc\": [\"{l}\"]")
    } else {
        String::new()
    };
    let repository_field = metadata
        .repository_url
        .as_deref()
        .map(|url| {
            format!(
                r#",
  "repository": {{
    "type": "git",
    "url": "{url}"
  }}"#
            )
        })
        .unwrap_or_default();
    let license_field = metadata
        .license
        .as_deref()
        .map(|license| format!(",\n  \"license\": \"{license}\""))
        .unwrap_or_default();
    format!(
        r#"{{
  "name": "{name}",
  "version": "{version}"{license_field}{repository_field},
  "os": ["{os}"],
  "cpu": ["{cpu}"]{libc_field},
  "main": "{bin_file}",
  "files": ["{bin_file}"],
  "engines": {{ "node": "{node_engine}" }},
  "publishConfig": {{ "access": "public" }}
}}
"#,
        node_engine = tv::npm::NODE_ENGINE,
    )
}

/// Locate the compiled `.node` binary, preferring `primary_name` (the cross-compile-renamed form
/// napi-rs produces, e.g. `mylib.linux-x64-gnu.node`) over `fallback_name` (the simple form a
/// plain `napi build` produces, e.g. `mylib.node`) at each location tier in turn.
///
/// Delegates to [`crate::publish::package::find_built_artifact_any_with_extra_dirs`] for the
/// two canonical `target/{triple}/release/` / `target/release/` locations, plus
/// `crates/{node_crate}/`, which is where `napi build` writes its output directly rather than
/// into `target/` at all.
fn find_node_binary(
    workspace_root: &Path,
    target: &RustTarget,
    node_crate: &str,
    primary_name: &str,
    fallback_name: &str,
) -> Result<PathBuf> {
    let in_crate_dir = workspace_root.join("crates").join(node_crate);
    crate::publish::package::find_built_artifact_any_with_extra_dirs(
        workspace_root,
        target,
        &[primary_name, fallback_name],
        BuildProfile::Release,
        std::slice::from_ref(&in_crate_dir),
    )
    .with_context(|| {
        format!(
            ".node binary not found for target {}. Expected '{primary_name}' or '{fallback_name}' in target \
             dirs or crates/{node_crate}/",
            target.triple
        )
    })
}

fn find_tgz(dir: &Path) -> Result<PathBuf> {
    let mut candidates: Vec<PathBuf> = fs::read_dir(dir)?
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|e| e == "tgz"))
        .collect();
    candidates.sort_by_key(|p| {
        fs::metadata(p)
            .and_then(|m| m.modified())
            .unwrap_or(std::time::SystemTime::UNIX_EPOCH)
    });
    candidates
        .into_iter()
        .next_back()
        .with_context(|| format!("no .tgz found in {}", dir.display()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn minimal_config() -> ResolvedCrateConfig {
        let cfg: crate::core::config::NewAlefConfig = toml::from_str(
            r#"
[workspace]
languages = ["node"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
[crates.node]
package_name = "@myorg/my-lib"
"#,
        )
        .unwrap();
        cfg.resolve().unwrap().remove(0)
    }

    #[test]
    fn platform_to_os_cpu_linux_gnu() {
        let (os, cpu, libc) = platform_to_os_cpu_libc("linux-x64-gnu");
        assert_eq!(os, "linux");
        assert_eq!(cpu, "x64");
        assert_eq!(libc, Some("glibc"));
    }

    #[test]
    fn platform_to_os_cpu_darwin() {
        let (os, cpu, libc) = platform_to_os_cpu_libc("darwin-arm64");
        assert_eq!(os, "darwin");
        assert_eq!(cpu, "arm64");
        assert!(libc.is_none());
    }

    #[test]
    fn sub_package_json_has_required_fields() {
        let json = generate_sub_package_json(
            "@scope/foo-linux-x64-gnu",
            "1.0.0",
            "foo.linux-x64-gnu.node",
            "linux",
            "x64",
            Some("glibc"),
            &PackageMetadata {
                license: Some("MIT".to_string()),
                repository_url: Some("git+https://github.com/scope/foo.git".to_string()),
            },
        );
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["name"], "@scope/foo-linux-x64-gnu");
        assert_eq!(parsed["version"], "1.0.0");
        assert_eq!(parsed["license"], "MIT");
        assert_eq!(parsed["repository"]["url"], "git+https://github.com/scope/foo.git");
        assert_eq!(parsed["publishConfig"]["access"], "public");
        assert!(parsed["os"].is_array());
        assert!(parsed["cpu"].is_array());
        assert!(parsed["libc"].is_array());
    }

    #[test]
    fn platform_to_os_cpu_musl_sets_libc() {
        let (os, cpu, libc) = platform_to_os_cpu_libc("linux-arm64-musl");
        assert_eq!(os, "linux");
        assert_eq!(cpu, "arm64");
        assert_eq!(libc, Some("musl"));
    }

    #[test]
    fn platform_to_os_cpu_win32_arm64() {
        let (os, cpu, libc) = platform_to_os_cpu_libc("win32-arm64-msvc");
        assert_eq!(os, "win32");
        assert_eq!(cpu, "arm64");
        assert!(libc.is_none());
    }

    #[test]
    fn default_npm_platforms_nonempty() {
        let config = minimal_config();
        let platforms = npm_platforms(&config);
        assert!(!platforms.is_empty());
        assert!(platforms.contains(&"win32-arm64-msvc".to_string()));
    }

    #[test]
    fn config_npm_platforms_override() {
        let cfg: crate::core::config::NewAlefConfig = toml::from_str(
            r#"
[workspace]
languages = ["node"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
[crates.publish.languages.node]
npm_subpackage_platforms = ["linux-x64-gnu", "darwin-arm64"]
"#,
        )
        .unwrap();
        let config = cfg.resolve().unwrap().remove(0);
        let platforms = npm_platforms(&config);
        assert_eq!(platforms, vec!["linux-x64-gnu", "darwin-arm64"]);
    }

    #[test]
    fn find_node_binary_cross_path() {
        let tmp = TempDir::new().unwrap();
        let target = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        let bin_dir = tmp.path().join("target/x86_64-unknown-linux-gnu/release");
        std::fs::create_dir_all(&bin_dir).unwrap();
        std::fs::write(bin_dir.join("my-lib.x64-linux-gnu.node"), b"fake").unwrap();

        let fallback_dir = tmp.path().join("target/x86_64-unknown-linux-gnu/release");
        std::fs::write(fallback_dir.join("my_lib.node"), b"fake").unwrap();

        let result = find_node_binary(
            tmp.path(),
            &target,
            "my-lib-node",
            "my-lib.x64-linux-gnu.node",
            "my_lib.node",
        )
        .unwrap();
        assert!(result.exists());
    }

    /// The in-crate fallback this rewrite preserves: `napi build` writes its output directly into
    /// the napi crate's own directory rather than into `target/` at all, so neither canonical
    /// uplifted location will ever contain it. This is the exact case
    /// `find_built_artifact`/`find_built_artifact_with_extra_dirs` with an empty `extra_dirs`
    /// would miss.
    #[test]
    fn find_node_binary_falls_back_to_in_crate_output() {
        let tmp = TempDir::new().unwrap();
        let target = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        let in_crate_dir = tmp.path().join("crates/my-lib-node");
        std::fs::create_dir_all(&in_crate_dir).unwrap();
        std::fs::write(in_crate_dir.join("my_lib.node"), b"in-crate-bytes").unwrap();

        let result = find_node_binary(
            tmp.path(),
            &target,
            "my-lib-node",
            "my-lib.x64-linux-gnu.node",
            "my_lib.node",
        )
        .unwrap();
        assert_eq!(
            result,
            in_crate_dir.join("my_lib.node"),
            "expected the in-crate fallback at {}, got {}",
            in_crate_dir.join("my_lib.node").display(),
            result.display()
        );
    }

    /// Tier priority must win over name priority: a `fallback_name` artifact at the
    /// higher-priority cross-compile tier must be found even when a `primary_name` artifact also
    /// exists at the lower-priority in-crate tier -- proves the delegation to
    /// `find_built_artifact_any_with_extra_dirs` searches tier-then-name, not name-then-tier
    /// (which would wrongly prefer the in-crate primary_name copy here).
    #[test]
    fn find_node_binary_searches_tier_before_name() {
        let tmp = TempDir::new().unwrap();
        let target = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();

        let cross_dir = tmp.path().join("target/x86_64-unknown-linux-gnu/release");
        std::fs::create_dir_all(&cross_dir).unwrap();
        std::fs::write(cross_dir.join("my_lib.node"), b"cross-fallback-name").unwrap();

        let in_crate_dir = tmp.path().join("crates/my-lib-node");
        std::fs::create_dir_all(&in_crate_dir).unwrap();
        std::fs::write(in_crate_dir.join("my-lib.x64-linux-gnu.node"), b"in-crate-primary-name").unwrap();

        let result = find_node_binary(
            tmp.path(),
            &target,
            "my-lib-node",
            "my-lib.x64-linux-gnu.node",
            "my_lib.node",
        )
        .unwrap();
        assert_eq!(
            result,
            cross_dir.join("my_lib.node"),
            "the higher-priority cross tier must win over a lower-priority tier even under a preferred name; \
             expected {}, got {}",
            cross_dir.join("my_lib.node").display(),
            result.display()
        );
    }

    /// `primary_name` must be tried before `fallback_name` at every location tier -- proves the
    /// name-preference order survived the delegation to `find_built_artifact_any_with_extra_dirs`.
    #[test]
    fn find_node_binary_prefers_primary_name_over_fallback_name() {
        let tmp = TempDir::new().unwrap();
        let target = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        let release_dir = tmp.path().join("target/x86_64-unknown-linux-gnu/release");
        std::fs::create_dir_all(&release_dir).unwrap();
        std::fs::write(release_dir.join("my-lib.x64-linux-gnu.node"), b"primary").unwrap();
        std::fs::write(release_dir.join("my_lib.node"), b"fallback").unwrap();

        let result = find_node_binary(
            tmp.path(),
            &target,
            "my-lib-node",
            "my-lib.x64-linux-gnu.node",
            "my_lib.node",
        )
        .unwrap();
        assert_eq!(result, release_dir.join("my-lib.x64-linux-gnu.node"));
    }
}