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.
@@ -1,54 +1,106 @@
1
- import { createRequire } from 'module';
2
1
  import { arch, platform } from 'os';
3
- import { dirname, join } from 'path';
2
+ import { join, dirname, resolve } from 'path';
3
+ import { createRequire } from 'module';
4
4
  import { fileURLToPath } from 'url';
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = dirname(__filename);
7
- const require = createRequire(import.meta.url);
8
- function getNativePath() {
9
- const platformMap = {
10
- linux: 'x86_64-unknown-linux-gnu',
11
- darwin: arch() === 'arm64' ? 'aarch64-apple-darwin' : 'x86_64-apple-darwin',
12
- win32: 'x86_64-pc-windows-gnu',
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
- const prebuiltPath = join(__dirname, '../../libroxify_native.node');
30
- const targetPath = join(__dirname, `../../libroxify_native-${target}.${ext}`);
31
- const targetAltPath = targetAlt ? join(__dirname, `../../libroxify_native-${targetAlt}.${ext}`) : null;
32
- try {
33
- return require.resolve(prebuiltPath);
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
- catch {
36
- try {
37
- return require.resolve(targetPath);
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
- catch {
40
- if (targetAltPath) {
41
- try {
42
- return require.resolve(targetAltPath);
43
- }
44
- catch {
45
- throw new Error(`Native module not found for ${currentPlatform}-${arch()}. ` +
46
- `Expected: ${prebuiltPath} or ${targetPath} or ${targetAltPath}`);
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
- throw new Error(`Native module not found for ${currentPlatform}-${arch()}. ` +
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 = require(getNativePath());
106
+ export const native = getNativeModule();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roxify",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "description": "Ultra-lightweight PNG steganography with native Rust acceleration. Encode binary data into PNG images with zstd compression.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",