11"use strict" ;
22
3+ const fs = require ( 'fs' ) ;
34const os = require ( 'os' ) ;
5+ const childProcess = require ( 'child_process' ) ;
6+
7+ function normalizeArch ( arch ) {
8+ if ( arch === 'x64' ) return 'x86_64' ;
9+ if ( arch === 'arm64' ) return 'aarch64' ;
10+ return arch ;
11+ }
12+
13+ function detectLibc ( ) {
14+ if ( process . report && typeof process . report . getReport === 'function' ) {
15+ try {
16+ const report = process . report . getReport ( ) ;
17+ if ( report ?. header ?. glibcVersionRuntime ) {
18+ return 'gnu' ;
19+ }
20+ } catch ( _ ) {
21+ // ignore and continue with fallbacks
22+ }
23+ }
24+
25+ try {
26+ const output = childProcess . execSync ( 'ldd --version' , { encoding : 'utf8' , stdio : [ 'ignore' , 'pipe' , 'ignore' ] } ) ;
27+ if ( / m u s l / i. test ( output ) ) {
28+ return 'musl' ;
29+ }
30+ if ( / g l i b c | g n u l i b c / i. test ( output ) ) {
31+ return 'gnu' ;
32+ }
33+ } catch ( _ ) {
34+ // ignore failures, fall back to fs detection
35+ }
36+
37+ if (
38+ fs . existsSync ( '/lib/ld-musl-x86_64.so.1' ) ||
39+ fs . existsSync ( '/lib/ld-musl-aarch64.so.1' ) ||
40+ fs . existsSync ( '/lib/ld-musl.so.1' )
41+ ) {
42+ return 'musl' ;
43+ }
44+
45+ return 'gnu' ;
46+ }
447
548function getPlatform ( ) {
649 const platform = os . platform ( ) ;
750 const arch = os . arch ( ) ;
851
952 if ( platform === 'linux' ) {
10- if ( arch === 'arm64' ) {
11- return 'linux-aarch64' ;
12- }
13- if ( arch === 'x64' ) {
14- return 'linux-x86_64' ;
53+ const normalizedArch = normalizeArch ( arch ) ;
54+ if ( ! normalizedArch ) {
55+ throw new Error ( `Unsupported architecture for Linux: ${ arch } ` ) ;
1556 }
57+ const libc = detectLibc ( ) ;
58+ return `linux-${ normalizedArch } -${ libc } ` ;
1659 }
1760
1861 if ( platform === 'darwin' ) {
@@ -22,14 +65,25 @@ function getPlatform() {
2265 throw new Error ( `Unsupported platform: ${ platform } , architecture: ${ arch } ` ) ;
2366}
2467
25- function binding ( ) {
68+ function loadBinding ( ) {
2669 const platform = getPlatform ( ) ;
70+ const candidates = new Set ( [ `${ platform } -rocketmq.node` ] ) ;
2771
28- try {
29- return require ( 'bindings' ) ( `${ platform } -rocketmq.node` )
30- } catch ( err ) {
31- throw new Error ( `Failed to load RocketMQ addon: ${ err . message } ` ) ;
72+ if ( platform . startsWith ( 'linux-' ) ) {
73+ const legacy = platform . replace ( / - ( g n u | m u s l ) $ / , '' ) ;
74+ candidates . add ( `${ legacy } -rocketmq.node` ) ;
3275 }
76+
77+ let lastError ;
78+ for ( const candidate of candidates ) {
79+ try {
80+ return require ( 'bindings' ) ( candidate ) ;
81+ } catch ( err ) {
82+ lastError = err ;
83+ }
84+ }
85+
86+ throw new Error ( `Failed to load RocketMQ addon (${ platform } ): ${ lastError ?. message || 'unknown error' } ` ) ;
3387}
3488
35- module . exports = binding ( ) ;
89+ module . exports = loadBinding ( ) ;
0 commit comments