roxify 1.5.6 → 1.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/native.js +96 -44
- package/package.json +1 -1
package/dist/utils/native.js
CHANGED
|
@@ -1,54 +1,106 @@
|
|
|
1
|
-
import { createRequire } from 'module';
|
|
2
1
|
import { arch, platform } from 'os';
|
|
3
|
-
import { dirname,
|
|
2
|
+
import { join, dirname, resolve } from 'path';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const platformAltMap = {
|
|
15
|
-
win32: 'x86_64-pc-windows-msvc',
|
|
16
|
-
};
|
|
17
|
-
const extMap = {
|
|
18
|
-
linux: 'so',
|
|
19
|
-
darwin: 'dylib',
|
|
20
|
-
win32: 'node',
|
|
21
|
-
};
|
|
22
|
-
const currentPlatform = platform();
|
|
23
|
-
const target = platformMap[currentPlatform];
|
|
24
|
-
const targetAlt = platformAltMap[currentPlatform];
|
|
25
|
-
const ext = extMap[currentPlatform];
|
|
26
|
-
if (!target || !ext) {
|
|
27
|
-
throw new Error(`Unsupported platform: ${currentPlatform}`);
|
|
5
|
+
import { existsSync } from 'fs';
|
|
6
|
+
function getNativeModule() {
|
|
7
|
+
let moduleDir;
|
|
8
|
+
let nativeRequire;
|
|
9
|
+
if (typeof __dirname !== 'undefined') {
|
|
10
|
+
// Mode CJS - variables globales disponibles
|
|
11
|
+
moduleDir = __dirname;
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
nativeRequire = require;
|
|
28
14
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
15
|
+
else {
|
|
16
|
+
// Mode ESM - utiliser import.meta.url
|
|
17
|
+
// @ts-ignore - import.meta.url existe en ESM
|
|
18
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
19
|
+
moduleDir = dirname(__filename);
|
|
20
|
+
nativeRequire = createRequire(import.meta.url);
|
|
34
21
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
function getNativePath() {
|
|
23
|
+
const platformMap = {
|
|
24
|
+
linux: 'x86_64-unknown-linux-gnu',
|
|
25
|
+
darwin: arch() === 'arm64' ? 'aarch64-apple-darwin' : 'x86_64-apple-darwin',
|
|
26
|
+
win32: 'x86_64-pc-windows-gnu',
|
|
27
|
+
};
|
|
28
|
+
const platformAltMap = {
|
|
29
|
+
win32: 'x86_64-pc-windows-msvc',
|
|
30
|
+
};
|
|
31
|
+
const extMap = {
|
|
32
|
+
linux: 'so',
|
|
33
|
+
darwin: 'dylib',
|
|
34
|
+
win32: 'node',
|
|
35
|
+
};
|
|
36
|
+
const currentPlatform = platform();
|
|
37
|
+
const target = platformMap[currentPlatform];
|
|
38
|
+
const targetAlt = platformAltMap[currentPlatform];
|
|
39
|
+
const ext = extMap[currentPlatform];
|
|
40
|
+
if (!target || !ext) {
|
|
41
|
+
throw new Error(`Unsupported platform: ${currentPlatform}`);
|
|
38
42
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
const prebuiltPath = join(moduleDir, '../../libroxify_native.node');
|
|
44
|
+
const bundlePath = join(moduleDir, '../libroxify_native.node');
|
|
45
|
+
// compute repo root by walking up from moduleDir (fallback to process.cwd())
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
console.debug('[native] moduleDir', moduleDir);
|
|
48
|
+
let root = moduleDir && moduleDir !== '.' ? moduleDir : process.cwd();
|
|
49
|
+
while (root.length > 1 && !existsSync(resolve(root, 'package.json')) && !existsSync(resolve(root, 'Cargo.toml'))) {
|
|
50
|
+
const parent = resolve(root, '..');
|
|
51
|
+
if (parent === root)
|
|
52
|
+
break;
|
|
53
|
+
root = parent;
|
|
54
|
+
}
|
|
55
|
+
const localTargetRelease = resolve(root, `target/release/libroxify_native${ext === 'node' ? '.node' : `-${target}.${ext}`}`);
|
|
56
|
+
const localReleaseGeneric = resolve(root, 'target/release/libroxify_native.so');
|
|
57
|
+
const nodeModulesBase = resolve(root, 'node_modules/roxify');
|
|
58
|
+
const nodeModulesTarget = resolve(nodeModulesBase, `libroxify_native-${target}.${ext}`);
|
|
59
|
+
const nodeModulesGeneric = resolve(nodeModulesBase, ext === 'node' ? 'libroxify_native.node' : `libroxify_native.${ext}`);
|
|
60
|
+
const bundleTarget = resolve(moduleDir, `../libroxify_native-${target}.${ext}`);
|
|
61
|
+
const bundleGeneric = resolve(moduleDir, bundlePath);
|
|
62
|
+
const candidates = [
|
|
63
|
+
localTargetRelease,
|
|
64
|
+
localReleaseGeneric,
|
|
65
|
+
nodeModulesTarget,
|
|
66
|
+
nodeModulesGeneric,
|
|
67
|
+
bundleTarget,
|
|
68
|
+
bundleGeneric,
|
|
69
|
+
prebuiltPath,
|
|
70
|
+
];
|
|
71
|
+
// use built-in fs.existsSync (static import to work in ESM and CJS)
|
|
72
|
+
for (const c of candidates) {
|
|
73
|
+
try {
|
|
74
|
+
if (!existsSync(c))
|
|
75
|
+
continue;
|
|
76
|
+
// If it's a .so (native build) but Node expects .node extension, create a .node symlink
|
|
77
|
+
if (c.endsWith('.so')) {
|
|
78
|
+
const nodeAlias = c.replace(/\.so$/, '.node');
|
|
79
|
+
try {
|
|
80
|
+
if (!existsSync(nodeAlias)) {
|
|
81
|
+
// copy the .so to a .node so Node treats it as a native addon
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
require('fs').copyFileSync(c, nodeAlias);
|
|
84
|
+
}
|
|
85
|
+
// debug
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
console.debug('[native] using node alias', nodeAlias);
|
|
88
|
+
return nodeAlias;
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
// fallback to original .so (might fail to load via require)
|
|
92
|
+
return c;
|
|
93
|
+
}
|
|
47
94
|
}
|
|
95
|
+
// debug
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
console.debug('[native] using path', c);
|
|
98
|
+
return c;
|
|
48
99
|
}
|
|
49
|
-
|
|
50
|
-
`Expected: ${prebuiltPath} or ${targetPath}`);
|
|
100
|
+
catch { }
|
|
51
101
|
}
|
|
102
|
+
throw new Error(`Native module not found for ${currentPlatform}-${arch()}. Checked: ${candidates.join(' ')}`);
|
|
52
103
|
}
|
|
104
|
+
return nativeRequire(getNativePath());
|
|
53
105
|
}
|
|
54
|
-
export const native =
|
|
106
|
+
export const native = getNativeModule();
|
package/package.json
CHANGED