roxify 1.5.7 → 1.5.9

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,7 +1,8 @@
1
1
  import { arch, platform } from 'os';
2
- import { join, dirname } from 'path';
2
+ import { join, dirname, resolve } from 'path';
3
3
  import { createRequire } from 'module';
4
4
  import { fileURLToPath } from 'url';
5
+ import { existsSync } from 'fs';
5
6
  function getNativeModule() {
6
7
  let moduleDir;
7
8
  let nativeRequire;
@@ -12,11 +13,24 @@ function getNativeModule() {
12
13
  nativeRequire = require;
13
14
  }
14
15
  else {
15
- // Mode ESM - utiliser import.meta.url
16
- // @ts-ignore - import.meta.url existe en ESM
17
- const __filename = fileURLToPath(import.meta.url);
18
- moduleDir = dirname(__filename);
19
- nativeRequire = createRequire(import.meta.url);
16
+ // Try ESM import.meta.url first (may throw in CJS/bundled contexts), otherwise fallback to CWD
17
+ try {
18
+ // @ts-ignore - import.meta.url exists in proper ESM contexts
19
+ const __filename = fileURLToPath(import.meta.url);
20
+ moduleDir = dirname(__filename);
21
+ nativeRequire = createRequire(import.meta.url);
22
+ }
23
+ catch {
24
+ // Fallback (bundled CJS without __dirname): use current working directory
25
+ moduleDir = process.cwd();
26
+ try {
27
+ // @ts-ignore
28
+ nativeRequire = require;
29
+ }
30
+ catch {
31
+ nativeRequire = createRequire(process.cwd());
32
+ }
33
+ }
20
34
  }
21
35
  function getNativePath() {
22
36
  const platformMap = {
@@ -41,36 +55,64 @@ function getNativeModule() {
41
55
  }
42
56
  const prebuiltPath = join(moduleDir, '../../libroxify_native.node');
43
57
  const bundlePath = join(moduleDir, '../libroxify_native.node');
44
- const targetPath = join(moduleDir, `../../libroxify_native-${target}.${ext}`);
45
- const targetAltPath = targetAlt
46
- ? join(moduleDir, `../../libroxify_native-${targetAlt}.${ext}`)
47
- : null;
48
- try {
49
- return nativeRequire.resolve(prebuiltPath);
58
+ // compute repo root by walking up from moduleDir (fallback to process.cwd())
59
+ // @ts-ignore
60
+ console.debug('[native] moduleDir', moduleDir);
61
+ let root = moduleDir && moduleDir !== '.' ? moduleDir : process.cwd();
62
+ while (root.length > 1 && !existsSync(resolve(root, 'package.json')) && !existsSync(resolve(root, 'Cargo.toml'))) {
63
+ const parent = resolve(root, '..');
64
+ if (parent === root)
65
+ break;
66
+ root = parent;
50
67
  }
51
- catch {
68
+ const localTargetRelease = resolve(root, `target/release/libroxify_native${ext === 'node' ? '.node' : `-${target}.${ext}`}`);
69
+ const localReleaseGeneric = resolve(root, 'target/release/libroxify_native.so');
70
+ const nodeModulesBase = resolve(root, 'node_modules/roxify');
71
+ const nodeModulesTarget = resolve(nodeModulesBase, `libroxify_native-${target}.${ext}`);
72
+ const nodeModulesGeneric = resolve(nodeModulesBase, ext === 'node' ? 'libroxify_native.node' : `libroxify_native.${ext}`);
73
+ const bundleTarget = resolve(moduleDir, `../libroxify_native-${target}.${ext}`);
74
+ const bundleGeneric = resolve(moduleDir, bundlePath);
75
+ const candidates = [
76
+ localTargetRelease,
77
+ localReleaseGeneric,
78
+ nodeModulesTarget,
79
+ nodeModulesGeneric,
80
+ bundleTarget,
81
+ bundleGeneric,
82
+ prebuiltPath,
83
+ ];
84
+ // use built-in fs.existsSync (static import to work in ESM and CJS)
85
+ for (const c of candidates) {
52
86
  try {
53
- return nativeRequire.resolve(bundlePath);
54
- }
55
- catch {
56
- try {
57
- return nativeRequire.resolve(targetPath);
58
- }
59
- catch {
60
- if (targetAltPath) {
61
- try {
62
- return nativeRequire.resolve(targetAltPath);
63
- }
64
- catch {
65
- throw new Error(`Native module not found for ${currentPlatform}-${arch()}. ` +
66
- `Expected: ${prebuiltPath} or ${bundlePath} or ${targetPath} or ${targetAltPath}`);
87
+ if (!existsSync(c))
88
+ continue;
89
+ // If it's a .so (native build) but Node expects .node extension, create a .node symlink
90
+ if (c.endsWith('.so')) {
91
+ const nodeAlias = c.replace(/\.so$/, '.node');
92
+ try {
93
+ if (!existsSync(nodeAlias)) {
94
+ // copy the .so to a .node so Node treats it as a native addon
95
+ // @ts-ignore
96
+ require('fs').copyFileSync(c, nodeAlias);
67
97
  }
98
+ // debug
99
+ // @ts-ignore
100
+ console.debug('[native] using node alias', nodeAlias);
101
+ return nodeAlias;
102
+ }
103
+ catch (e) {
104
+ // fallback to original .so (might fail to load via require)
105
+ return c;
68
106
  }
69
- throw new Error(`Native module not found for ${currentPlatform}-${arch()}. ` +
70
- `Expected: ${prebuiltPath} or ${bundlePath} or ${targetPath}`);
71
107
  }
108
+ // debug
109
+ // @ts-ignore
110
+ console.debug('[native] using path', c);
111
+ return c;
72
112
  }
113
+ catch { }
73
114
  }
115
+ throw new Error(`Native module not found for ${currentPlatform}-${arch()}. Checked: ${candidates.join(' ')}`);
74
116
  }
75
117
  return nativeRequire(getNativePath());
76
118
  }
@@ -2,8 +2,22 @@ import { spawn } from 'child_process';
2
2
  import { existsSync } from 'fs';
3
3
  import { dirname, join } from 'path';
4
4
  import { fileURLToPath } from 'url';
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = dirname(__filename);
5
+ let moduleDir;
6
+ try {
7
+ // CJS bundlers may provide __dirname; prefer it when available
8
+ if (typeof __dirname !== 'undefined') {
9
+ // @ts-ignore
10
+ moduleDir = __dirname;
11
+ }
12
+ else {
13
+ // @ts-ignore - import.meta.url exists in ESM
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ moduleDir = dirname(__filename);
16
+ }
17
+ }
18
+ catch {
19
+ moduleDir = process.cwd();
20
+ }
7
21
  function findRustBinary() {
8
22
  const candidates = [];
9
23
  const binNames = process.platform === 'win32'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roxify",
3
- "version": "1.5.7",
3
+ "version": "1.5.9",
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",