-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathplatform.js
More file actions
86 lines (74 loc) · 1.96 KB
/
Copy pathplatform.js
File metadata and controls
86 lines (74 loc) · 1.96 KB
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
#!/usr/bin/env node
"use strict";
const os = require("os");
const path = require("path");
const fs = require("fs");
const { spawnSync } = require("child_process");
const { supportedPlatforms } = require("./package.json");
/**
* Map Node.js os.type() and os.arch() to Rust-style target triples.
*/
function getPlatformKey() {
const rawOs = os.type();
const rawArch = os.arch();
let osType;
switch (rawOs) {
case "Windows_NT":
osType = "pc-windows-msvc";
break;
case "Darwin":
osType = "apple-darwin";
break;
case "Linux":
osType = "unknown-linux-gnu";
break;
default:
throw new Error(`Unsupported operating system: ${rawOs}`);
}
let arch;
switch (rawArch) {
case "x64":
arch = "x86_64";
break;
case "arm64":
arch = "aarch64";
break;
default:
throw new Error(`Unsupported architecture: ${rawArch}`);
}
// On Linux, try to detect musl libc
if (rawOs === "Linux") {
try {
const result = spawnSync("ldd", ["--version"], {
encoding: "utf8",
stdio: ["pipe", "pipe", "pipe"],
});
// musl ldd prints version info to stderr
const output = (result.stdout || "") + (result.stderr || "");
if (output.toLowerCase().includes("musl")) {
osType = "unknown-linux-musl";
}
} catch {
// If ldd fails, assume glibc
}
}
const key = `${arch}-${osType}`;
if (!supportedPlatforms[key]) {
// Try musl fallback on Linux if glibc binary is not available
if (rawOs === "Linux") {
const muslKey = `${arch}-unknown-linux-musl`;
if (supportedPlatforms[muslKey]) {
return muslKey;
}
}
throw new Error(
`Unsupported platform: ${key}\nSupported platforms: ${Object.keys(supportedPlatforms).join(", ")}`,
);
}
return key;
}
function getPlatform() {
const key = getPlatformKey();
return supportedPlatforms[key];
}
module.exports = { getPlatform, getPlatformKey };