Skip to content

Commit d38cadb

Browse files
fix: building platform binaries
1 parent b555707 commit d38cadb

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

.github/workflows/build.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ jobs:
4545
with:
4646
targets: ${{ matrix.settings.target }}
4747

48+
- name: Install cross-compilation toolchain (Linux ARM64)
49+
if: matrix.settings.target == 'aarch64-unknown-linux-gnu'
50+
run: |
51+
sudo apt-get update
52+
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
53+
4854
- name: Install dependencies
4955
run: npm install
5056

5157
- name: Build native module
5258
run: ${{ matrix.settings.build }}
59+
env:
60+
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
61+
CC_aarch64_unknown_linux_gnu: aarch64-linux-gnu-gcc
62+
CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++
5363

5464
- name: Upload artifacts
5565
uses: actions/upload-artifact@v4

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,18 @@
4848
"files": [
4949
"dist",
5050
"rust/*.node"
51-
]
51+
],
52+
"napi": {
53+
"name": "index",
54+
"triples": {
55+
"defaults": true,
56+
"additional": [
57+
"x86_64-apple-darwin",
58+
"aarch64-apple-darwin",
59+
"x86_64-unknown-linux-gnu",
60+
"aarch64-unknown-linux-gnu",
61+
"x86_64-pc-windows-msvc"
62+
]
63+
}
64+
}
5265
}

src/node-wreq.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,56 @@ let nativeBinding: {
66
getProfiles: () => string[];
77
};
88

9+
function loadNativeBinding() {
10+
const platform = process.platform;
11+
const arch = process.arch;
12+
13+
// Map Node.js platform/arch to napi binary naming convention
14+
const platformArchMap: Record<string, Record<string, string>> = {
15+
darwin: {
16+
x64: 'darwin-x64',
17+
arm64: 'darwin-arm64',
18+
},
19+
linux: {
20+
x64: 'linux-x64',
21+
arm64: 'linux-arm64',
22+
},
23+
win32: {
24+
x64: 'win32-x64',
25+
},
26+
};
27+
28+
const platformArch = platformArchMap[platform]?.[arch];
29+
if (!platformArch) {
30+
throw new Error(
31+
`Unsupported platform: ${platform}-${arch}. ` +
32+
`Supported platforms: darwin-x64, darwin-arm64, linux-x64, linux-arm64, win32-x64`
33+
);
34+
}
35+
36+
// Try to load platform-specific binary (napi naming convention)
37+
const binaryName = `index.${platformArch}.node`;
38+
39+
try {
40+
return require(`../rust/${binaryName}`);
41+
} catch (e1) {
42+
// Fallback to index.node (for local development)
43+
try {
44+
return require('../rust/index.node');
45+
} catch (e2) {
46+
throw new Error(
47+
`Failed to load native module for ${platform}-${arch}. ` +
48+
`Tried: ../rust/${binaryName} and ../rust/index.node. ` +
49+
`Make sure the package is installed correctly and the native module is built for your platform.`
50+
);
51+
}
52+
}
53+
}
54+
955
try {
10-
nativeBinding = require('../rust/index.node');
56+
nativeBinding = loadNativeBinding();
1157
} catch (error) {
12-
throw new Error(
13-
`Failed to load native module: ${error}. ` +
14-
`Make sure the package is installed correctly and the native module is built for your platform (${process.platform}-${process.arch}).`
15-
);
58+
throw error;
1659
}
1760

1861
/**

0 commit comments

Comments
 (0)