Skip to content

Commit b3dedc3

Browse files
authored
feat(NODE-4569): add "musl" builds for Alpine Linux (#12)
1 parent 1110d8d commit b3dedc3

File tree

9 files changed

+199
-20
lines changed

9 files changed

+199
-20
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[target.aarch64-unknown-linux-gnu]
22
linker = "aarch64-linux-gnu-gcc"
3+
[target.aarch64-unknown-linux-musl]
4+
linker = "aarch64-linux-musl-gcc"
5+
rustflags = ["-C", "target-feature=-crt-static"]

.github/workflows/CI.yml

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ env:
1818
pull_request: null
1919
jobs:
2020
build:
21-
if: '!contains(github.event.head_commit.message, ''skip ci'')'
21+
if: "!contains(github.event.head_commit.message, 'skip ci')"
2222
strategy:
2323
fail-fast: false
2424
matrix:
@@ -42,6 +42,10 @@ jobs:
4242
rustup target add x86_64-unknown-linux-gnu &&
4343
npm run build -- --target x86_64-unknown-linux-gnu --zig --zig-abi-suffix 2.12 &&
4444
llvm-strip -x *.node
45+
- host: ubuntu-latest
46+
target: x86_64-unknown-linux-musl
47+
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
48+
build: set -e && npm run build && strip *.node
4549
- host: macos-latest
4650
architecture: x64
4751
target: aarch64-apple-darwin
@@ -62,6 +66,14 @@ jobs:
6266
rustup target add aarch64-unknown-linux-gnu &&
6367
npm run build -- --target aarch64-unknown-linux-gnu --zig --zig-abi-suffix 2.17 &&
6468
llvm-strip -x *.node
69+
- host: ubuntu-latest
70+
target: aarch64-unknown-linux-musl
71+
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
72+
build: |-
73+
set -e &&
74+
rustup target add aarch64-unknown-linux-musl &&
75+
npm run build -- --target aarch64-unknown-linux-musl &&
76+
/aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node
6577
name: stable - ${{ matrix.settings.target }} - node@16
6678
runs-on: ${{ matrix.settings.host }}
6779
steps:
@@ -204,6 +216,44 @@ jobs:
204216
shell: bash
205217
- name: Test bindings
206218
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-slim npm test
219+
test-linux-x64-musl-binding:
220+
name: Test bindings on x86_64-unknown-linux-musl - node@${{ matrix.node }}
221+
needs:
222+
- build
223+
strategy:
224+
fail-fast: false
225+
matrix:
226+
node:
227+
- '12'
228+
- '14'
229+
- '16'
230+
- '18'
231+
runs-on: ubuntu-latest
232+
steps:
233+
- uses: actions/checkout@v3
234+
- name: Setup node
235+
uses: actions/setup-node@v3
236+
with:
237+
node-version: ${{ matrix.node }}
238+
check-latest: true
239+
cache: npm
240+
- name: Cache NPM dependencies
241+
uses: actions/cache@v3
242+
with:
243+
path: node_modules
244+
key: npm-cache-test-x86_64-unknown-linux-musl-${{ matrix.node }}-${{ hashFiles('package-lock.json') }}
245+
- name: Install dependencies
246+
run: npm clean-install --ignore-scripts
247+
- name: Download artifacts
248+
uses: actions/download-artifact@v3
249+
with:
250+
name: bindings-x86_64-unknown-linux-musl
251+
path: .
252+
- name: List packages
253+
run: ls -R .
254+
shell: bash
255+
- name: Test bindings
256+
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-alpine npm test
207257
test-linux-aarch64-gnu-binding:
208258
name: Test bindings on aarch64-unknown-linux-gnu - node@${{ matrix.node }}
209259
needs:
@@ -244,13 +294,47 @@ jobs:
244294
set -e
245295
npm test
246296
ls -la
297+
test-linux-aarch64-musl-binding:
298+
name: Test bindings on aarch64-unknown-linux-musl - node@${{ matrix.node }}
299+
needs:
300+
- build
301+
runs-on: ubuntu-latest
302+
steps:
303+
- run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
304+
- uses: actions/checkout@v3
305+
- name: Download artifacts
306+
uses: actions/download-artifact@v3
307+
with:
308+
name: bindings-aarch64-unknown-linux-musl
309+
path: .
310+
- name: List packages
311+
run: ls -R .
312+
shell: bash
313+
- name: Cache NPM dependencies
314+
uses: actions/cache@v3
315+
with:
316+
path: node_modules
317+
key: npm-cache-test-linux-aarch64-musl-${{ matrix.node }}-${{ hashFiles('package-lock.json') }}
318+
- name: Install dependencies
319+
run: npm clean-install --ignore-scripts
320+
- name: Setup and run tests
321+
uses: addnab/docker-run-action@v3
322+
with:
323+
image: multiarch/alpine:aarch64-latest-stable
324+
options: '-v ${{ github.workspace }}:/build -w /build'
325+
run: |
326+
set -e
327+
apk add nodejs npm
328+
npm test
247329
publish:
248330
name: Publish
249331
runs-on: ubuntu-latest
250332
needs:
251333
- test-macOS-windows-binding
252334
- test-linux-x64-gnu-binding
335+
- test-linux-x64-musl-binding
253336
- test-linux-aarch64-gnu-binding
337+
- test-linux-aarch64-musl-binding
254338
steps:
255339
- uses: actions/checkout@v3
256340
- name: Setup node

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ npm install @mongodb-js/zstd
2020
| Linux x64 gnu |||||
2121
| Linux arm gnu |||||
2222
| Linux arm64 gnu |||||
23+
| Linux x64 musl |||||
24+
| Linux arm64 musl |||||
2325

2426
## API
2527

2628
```ts
27-
export function compress(buffer: Buffer | ArrayBuffer | Uint8Array, level: number): Promise<Buffer>
28-
export function decompress(buffer: Buffer): Promise<Buffer>
29+
export function compress(buffer: Buffer | ArrayBuffer | Uint8Array, level: number): Promise<Buffer>;
30+
export function decompress(buffer: Buffer): Promise<Buffer>;
2931
```
3032

3133
### Bugs / Feature Requests

index.js

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ let nativeBinding = null;
77
let localFileExisted = false;
88
let loadError = null;
99

10+
function isMusl() {
11+
// For Node 10
12+
if (!process.report || typeof process.report.getReport !== 'function') {
13+
try {
14+
return readFileSync('/usr/bin/ldd', 'utf8').includes('musl');
15+
} catch (e) {
16+
return true;
17+
}
18+
} else {
19+
const { glibcVersionRuntime } = process.report.getReport().header;
20+
return !glibcVersionRuntime;
21+
}
22+
}
23+
1024
switch (platform) {
1125
case 'win32':
1226
switch (arch) {
@@ -59,27 +73,53 @@ switch (platform) {
5973
case 'linux':
6074
switch (arch) {
6175
case 'x64':
62-
localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-gnu.node'));
63-
try {
64-
if (localFileExisted) {
65-
nativeBinding = require('./zstd.linux-x64-gnu.node');
66-
} else {
67-
nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu');
76+
if (isMusl()) {
77+
localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-musl.node'));
78+
try {
79+
if (localFileExisted) {
80+
nativeBinding = require('./zstd.linux-x64-musl.node');
81+
} else {
82+
nativeBinding = require('@mongodb-js/zstd-linux-x64-musl');
83+
}
84+
} catch (e) {
85+
loadError = e;
86+
}
87+
} else {
88+
localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-gnu.node'));
89+
try {
90+
if (localFileExisted) {
91+
nativeBinding = require('./zstd.linux-x64-gnu.node');
92+
} else {
93+
nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu');
94+
}
95+
} catch (e) {
96+
loadError = e;
6897
}
69-
} catch (e) {
70-
loadError = e;
7198
}
7299
break;
73100
case 'arm64':
74-
localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-gnu.node'));
75-
try {
76-
if (localFileExisted) {
77-
nativeBinding = require('./zstd.linux-arm64-gnu.node');
78-
} else {
79-
nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu');
101+
if (isMusl()) {
102+
localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-musl.node'));
103+
try {
104+
if (localFileExisted) {
105+
nativeBinding = require('./zstd.linux-arm64-musl.node');
106+
} else {
107+
nativeBinding = require('@mongodb-js/zstd-linux-arm64-musl');
108+
}
109+
} catch (e) {
110+
loadError = e;
111+
}
112+
} else {
113+
localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-gnu.node'));
114+
try {
115+
if (localFileExisted) {
116+
nativeBinding = require('./zstd.linux-arm64-gnu.node');
117+
} else {
118+
nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu');
119+
}
120+
} catch (e) {
121+
loadError = e;
80122
}
81-
} catch (e) {
82-
loadError = e;
83123
}
84124
break;
85125
case 'arm':

npm/linux-arm64-musl/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@mongodb-js/zstd-linux-arm64-musl`
2+
3+
This is the **aarch64-unknown-linux-musl** binary for `@mongodb-js/zstd`

npm/linux-arm64-musl/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@mongodb-js/zstd-linux-arm64-musl",
3+
"version": "0.0.0",
4+
"os": [
5+
"linux"
6+
],
7+
"cpu": [
8+
"arm64"
9+
],
10+
"main": "zstd.linux-arm64-musl.node",
11+
"files": [
12+
"zstd.linux-arm64-musl.node"
13+
],
14+
"license": "MIT",
15+
"engines": {
16+
"node": ">= 10"
17+
},
18+
"libc": [
19+
"musl"
20+
]
21+
}

npm/linux-x64-musl/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@mongodb-js/zstd-linux-x64-musl`
2+
3+
This is the **x86_64-unknown-linux-musl** binary for `@mongodb-js/zstd`

npm/linux-x64-musl/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@mongodb-js/zstd-linux-x64-musl",
3+
"version": "0.0.0",
4+
"os": [
5+
"linux"
6+
],
7+
"cpu": [
8+
"x64"
9+
],
10+
"main": "zstd.linux-x64-musl.node",
11+
"files": [
12+
"zstd.linux-x64-musl.node"
13+
],
14+
"license": "MIT",
15+
"engines": {
16+
"node": ">= 10"
17+
},
18+
"libc": [
19+
"musl"
20+
]
21+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"defaults": true,
1414
"additional": [
1515
"aarch64-apple-darwin",
16-
"aarch64-unknown-linux-gnu"
16+
"aarch64-unknown-linux-gnu",
17+
"aarch64-unknown-linux-musl",
18+
"x86_64-unknown-linux-musl"
1719
]
1820
}
1921
},

0 commit comments

Comments
 (0)