|
1 | 1 | const fs = require('fs'); |
2 | 2 | const path = require('path'); |
3 | | -// Prefer a prebuilt native binary if present, otherwise fall back to local build |
| 3 | +// Prefer a prebuilt native binary if present, otherwise fall back to local build. |
| 4 | +// If prebuilt has a vendor/ folder, adjust PATH/LD_LIBRARY_PATH on platforms that need it. |
4 | 5 | function loadNative() { |
5 | 6 | const abi = process.versions.modules; // Node ABI |
6 | | - const candidate = path.join(__dirname, '..', 'prebuilt', `${process.platform}-${process.arch}`, `node-v${abi}`, 'baresip_node.node'); |
| 7 | + const base = path.join(__dirname, '..', 'prebuilt', `${process.platform}-${process.arch}`, `node-v${abi}`); |
| 8 | + const candidate = path.join(base, 'baresip_node.node'); |
| 9 | + const vendor = path.join(base, 'vendor'); |
| 10 | + |
| 11 | + const tryRequire = (p) => { |
| 12 | + try { return require(p); } catch (e) { return e; } |
| 13 | + }; |
| 14 | + |
7 | 15 | if (fs.existsSync(candidate)) { |
8 | | - try { return require(candidate); } catch (_) {} |
| 16 | + // Windows needs PATH to include vendor for co-located DLLs |
| 17 | + if (process.platform === 'win32' && fs.existsSync(vendor)) { |
| 18 | + process.env.PATH = `${vendor};${process.env.PATH || ''}`; |
| 19 | + } |
| 20 | + // Linux may need LD_LIBRARY_PATH if RUNPATH wasn't embedded |
| 21 | + if (process.platform === 'linux' && fs.existsSync(vendor)) { |
| 22 | + const sep = ':'; |
| 23 | + process.env.LD_LIBRARY_PATH = `${vendor}${sep}${process.env.LD_LIBRARY_PATH || ''}`; |
| 24 | + } |
| 25 | + const res = tryRequire(candidate); |
| 26 | + if (!(res instanceof Error)) return res; |
| 27 | + // capture error but try fallback below |
| 28 | + var prebuiltErr = res; |
9 | 29 | } |
10 | | - // Fallback to build output |
11 | | - return require('../build/Release/baresip_node.node'); |
| 30 | + |
| 31 | + // Fallback to local build output |
| 32 | + const fallback = path.join(__dirname, '..', 'build', 'Release', 'baresip_node.node'); |
| 33 | + if (fs.existsSync(fallback)) { |
| 34 | + const res2 = tryRequire(fallback); |
| 35 | + if (!(res2 instanceof Error)) return res2; |
| 36 | + // If both failed, throw a helpful error |
| 37 | + const msg = [ |
| 38 | + 'Failed to load baresip_node native addon.', |
| 39 | + candidate && fs.existsSync(candidate) |
| 40 | + ? `- Prebuilt found at ${candidate} but failed: ${prebuiltErr && prebuiltErr.message}` |
| 41 | + : `- No prebuilt found for ${process.platform}-${process.arch} (ABI node-v${abi}).`, |
| 42 | + `- Local build found at ${fallback} but failed: ${res2.message}`, |
| 43 | + 'Solutions:', |
| 44 | + ' • Use a release that includes a prebuilt for your platform/Node version', |
| 45 | + ' • Or build locally: npm ci && npx cmake-js rebuild' |
| 46 | + ].join('\n'); |
| 47 | + throw new Error(msg); |
| 48 | + } |
| 49 | + |
| 50 | + // Neither prebuilt nor local exists |
| 51 | + const msg = [ |
| 52 | + 'baresip_node native addon not available.', |
| 53 | + `- No prebuilt at ${candidate}`, |
| 54 | + `- No local build at ${fallback}`, |
| 55 | + 'Solutions:', |
| 56 | + ' • Use a release that includes a prebuilt for your platform/Node version', |
| 57 | + ' • Or build locally: npm ci && npx cmake-js rebuild' |
| 58 | + ].join('\n'); |
| 59 | + throw new Error(msg); |
12 | 60 | } |
13 | 61 | const BareSIP = loadNative(); |
14 | 62 | // Javascript Wrapper for baresip_node addon |
|
0 commit comments