seabox 0.1.0-beta.3 → 0.1.0

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.
@@ -0,0 +1,203 @@
1
+ /**
2
+ * native-scanner.mjs
3
+ * Deep scanning of node_modules for native modules and automatic detection.
4
+ */
5
+
6
+ import fs from 'fs/promises';
7
+ import fsSync from 'fs';
8
+ import path from 'path';
9
+ import * as diag from './diagnostics.mjs';
10
+
11
+ /**
12
+ * @typedef {Object} NativeModuleMetadata
13
+ * @property {string} name - Module name
14
+ * @property {string} path - Absolute path to module root
15
+ * @property {string} version - Module version
16
+ * @property {boolean} hasBindingGyp - Whether binding.gyp exists
17
+ * @property {string[]} binaryFiles - Detected .node files
18
+ */
19
+
20
+ /**
21
+ * Scan node_modules directory for native modules
22
+ * @param {string} projectRoot - Project root directory
23
+ * @param {boolean} verbose - Enable verbose logging
24
+ * @returns {Promise<NativeModuleMetadata[]>}
25
+ */
26
+ export async function scanDependenciesForNativeModules(projectRoot, verbose = false) {
27
+ const nativeModules = [];
28
+ const nodeModulesPath = path.join(projectRoot, 'node_modules');
29
+
30
+ if (!fsSync.existsSync(nodeModulesPath)) {
31
+ diag.verbose('No node_modules directory found');
32
+ return [];
33
+ }
34
+
35
+ diag.verbose('Scanning node_modules for native modules');
36
+
37
+ /**
38
+ * Recursively scan a directory for packages
39
+ */
40
+ async function scanDir(dir, isScoped = false) {
41
+ try {
42
+ const entries = await fs.readdir(dir, { withFileTypes: true });
43
+
44
+ for (const entry of entries) {
45
+ // Skip hidden directories and bin
46
+ if (entry.name.startsWith('.') || entry.name === '.bin') {
47
+ continue;
48
+ }
49
+
50
+ const fullPath = path.join(dir, entry.name);
51
+
52
+ if (entry.isDirectory()) {
53
+ const pkgPath = path.join(fullPath, 'package.json');
54
+
55
+ // Check if this is a scoped package directory
56
+ if (entry.name.startsWith('@')) {
57
+ await scanDir(fullPath, true);
58
+ continue;
59
+ }
60
+
61
+ // Check if package.json exists
62
+ if (fsSync.existsSync(pkgPath)) {
63
+ const moduleInfo = await analyzePackage(fullPath, pkgPath);
64
+ if (moduleInfo) {
65
+ nativeModules.push(moduleInfo);
66
+ }
67
+ }
68
+ }
69
+ }
70
+ } catch (err) {
71
+ diag.verbose(`Error scanning directory: ${dir} - ${err.message}`);
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Analyze a package to determine if it's a native module
77
+ */
78
+ async function analyzePackage(modulePath, pkgPath) {
79
+ try {
80
+ const pkgContent = await fs.readFile(pkgPath, 'utf8');
81
+ const pkg = JSON.parse(pkgContent);
82
+
83
+ // Check for native module indicators
84
+ const hasBindingGyp = fsSync.existsSync(path.join(modulePath, 'binding.gyp'));
85
+ const hasGypfile = pkg.gypfile === true;
86
+ const hasBinaryField = pkg.binary != null;
87
+
88
+ if (!hasBindingGyp && !hasGypfile && !hasBinaryField) {
89
+ return null;
90
+ }
91
+
92
+ // Find .node files
93
+ const binaryFiles = await findNodeFiles(modulePath);
94
+
95
+ if (hasBindingGyp || hasGypfile || binaryFiles.length > 0) {
96
+ return {
97
+ name: pkg.name,
98
+ path: modulePath,
99
+ version: pkg.version,
100
+ hasBindingGyp: hasBindingGyp,
101
+ binaryFiles: binaryFiles
102
+ };
103
+ }
104
+
105
+ return null;
106
+ } catch (err) {
107
+ // Ignore packages with invalid package.json
108
+ return null;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Find all .node files in a directory tree
114
+ */
115
+ async function findNodeFiles(dir, maxDepth = 5, currentDepth = 0) {
116
+ const nodeFiles = [];
117
+
118
+ if (currentDepth > maxDepth) {
119
+ return nodeFiles;
120
+ }
121
+
122
+ try {
123
+ const entries = await fs.readdir(dir, { withFileTypes: true });
124
+
125
+ for (const entry of entries) {
126
+ const fullPath = path.join(dir, entry.name);
127
+
128
+ if (entry.isDirectory()) {
129
+ // Skip node_modules subdirectories
130
+ if (entry.name === 'node_modules') {
131
+ continue;
132
+ }
133
+
134
+ // Recurse into subdirectories
135
+ const subFiles = await findNodeFiles(fullPath, maxDepth, currentDepth + 1);
136
+ nodeFiles.push(...subFiles);
137
+ } else if (entry.name.endsWith('.node')) {
138
+ nodeFiles.push(fullPath);
139
+ }
140
+ }
141
+ } catch (err) {
142
+ // Ignore read errors
143
+ }
144
+
145
+ return nodeFiles;
146
+ }
147
+
148
+ await scanDir(nodeModulesPath);
149
+
150
+ diag.verbose(`Found ${nativeModules.length} native modules`);
151
+ for (const mod of nativeModules) {
152
+ diag.verbose(`- ${mod.name}@${mod.version} (${mod.binaryFiles.length} binaries)`, 1);
153
+ }
154
+
155
+ return nativeModules;
156
+ }
157
+
158
+ /**
159
+ * Find the build output path for a native module
160
+ * @param {string} moduleRoot - Root directory of the native module
161
+ * @param {string} target - Build target (e.g., node24.11.0-win32-x64)
162
+ * @returns {Promise<string|null>}
163
+ */
164
+ export async function findNativeModuleBuildPath(moduleRoot, target) {
165
+ const { platform, arch } = parseTarget(target);
166
+
167
+ // Common build output locations
168
+ const searchPaths = [
169
+ path.join(moduleRoot, 'build/Release'),
170
+ path.join(moduleRoot, 'build/Debug'),
171
+ path.join(moduleRoot, 'lib/binding', `${platform}-${arch}`),
172
+ path.join(moduleRoot, 'prebuilds', `${platform}-${arch}`)
173
+ ];
174
+
175
+ for (const searchPath of searchPaths) {
176
+ if (fsSync.existsSync(searchPath)) {
177
+ // Look for .node files
178
+ const files = await fs.readdir(searchPath);
179
+ const nodeFile = files.find(f => f.endsWith('.node'));
180
+
181
+ if (nodeFile) {
182
+ return path.join(searchPath, nodeFile);
183
+ }
184
+ }
185
+ }
186
+
187
+ return null;
188
+ }
189
+
190
+ /**
191
+ * Parse a target string into components
192
+ */
193
+ function parseTarget(target) {
194
+ const match = target.match(/^node(\d+\.\d+\.\d+)-(\w+)-(\w+)$/);
195
+ if (!match) {
196
+ throw new Error(`Cannot parse target: ${target}`);
197
+ }
198
+ return {
199
+ nodeVersion: match[1],
200
+ platform: match[2],
201
+ arch: match[3]
202
+ };
203
+ }
@@ -1,7 +1,11 @@
1
1
  /**
2
- * @file Obfuscate bootstrap code to protect encryption keys and decryption logic
2
+ * obfuscate.mjs
3
+ * Obfuscate bootstrap code to protect encryption keys and decryption logic
3
4
  */
4
5
 
6
+ import Module from 'module';
7
+
8
+ const require = Module.createRequire(import.meta.url);
5
9
  const JavaScriptObfuscator = require('javascript-obfuscator');
6
10
 
7
11
  /**
@@ -10,11 +14,9 @@ const JavaScriptObfuscator = require('javascript-obfuscator');
10
14
  * @param {string} bootstrapCode - The bootstrap JavaScript code to obfuscate
11
15
  * @returns {string} Obfuscated JavaScript code
12
16
  */
13
- function obfuscateBootstrap(bootstrapCode) {
17
+ export function obfuscateBootstrap(bootstrapCode) {
14
18
  const obfuscationResult = JavaScriptObfuscator.obfuscate(bootstrapCode, {
15
19
  // Maximum protection settings for encryption key and decryption logic
16
-
17
- // String encoding
18
20
  stringArray: true,
19
21
  stringArrayThreshold: 1,
20
22
  stringArrayEncoding: ['rc4'],
@@ -25,49 +27,25 @@ function obfuscateBootstrap(bootstrapCode) {
25
27
  stringArrayWrappersChainedCalls: true,
26
28
  stringArrayWrappersParametersMaxCount: 5,
27
29
  stringArrayWrappersType: 'function',
28
-
29
- // Control flow
30
30
  controlFlowFlattening: true,
31
31
  controlFlowFlatteningThreshold: 1,
32
32
  deadCodeInjection: true,
33
33
  deadCodeInjectionThreshold: 0.4,
34
-
35
- // Code transformations
36
34
  transformObjectKeys: true,
37
35
  splitStrings: true,
38
36
  splitStringsChunkLength: 10,
39
-
40
- // Identifiers
41
37
  identifierNamesGenerator: 'hexadecimal',
42
38
  identifiersPrefix: '',
43
- renameGlobals: false, // Keep false - we need to preserve global scope
44
- renameProperties: false, // Keep false - breaks sea.getAsset patching
45
-
46
- // Self-defending
39
+ renameGlobals: false,
40
+ renameProperties: false,
47
41
  selfDefending: true,
48
-
49
- // Compact output
50
42
  compact: true,
51
-
52
- // Additional obfuscation
53
43
  numbersToExpressions: true,
54
44
  simplify: true,
55
-
56
- // Disable source maps (we don't want them)
57
- sourceMap: false,
58
-
59
- // Performance vs protection tradeoff
60
- // (these settings prioritize protection over performance)
61
45
  target: 'node',
62
46
  ignoreImports: true,
63
-
64
- // Comments removal
65
- // (handled automatically by compact: true)
47
+ sourceMap: false
66
48
  });
67
49
 
68
50
  return obfuscationResult.getObfuscatedCode();
69
51
  }
70
-
71
- module.exports = {
72
- obfuscateBootstrap
73
- };
@@ -0,0 +1,113 @@
1
+ /**
2
+ * require-shim.js
3
+ * SEA-aware require replacement that intercepts .node module loads
4
+ */
5
+
6
+ import * as diag from './diagnostics.mjs';
7
+
8
+ /**
9
+ * Generate the __requireSeabox shim code
10
+ * @returns {string} - The shim code to inject
11
+ */
12
+ export function generateRequireShim() {
13
+ return `
14
+ // SEA-aware require replacement
15
+ const __originalRequire = require;
16
+ function __requireSeabox(id) {
17
+ // Check if this is a native module request (either .node extension or asset key)
18
+ if (typeof id === 'string' && (id.endsWith('.node') || id.startsWith('native/'))) {
19
+ // Check if we're in SEA mode
20
+ let isSEA = false;
21
+ try {
22
+ const sea = __originalRequire('node:sea');
23
+ isSEA = sea.isSea();
24
+ } catch (e) {
25
+ // Not in SEA mode
26
+ }
27
+
28
+ if (isSEA && global.__seaNativeModuleMap) {
29
+ const path = __originalRequire('path');
30
+
31
+ // Try multiple resolution strategies
32
+ const basename = path.basename(id);
33
+ const nameWithoutExt = basename.replace(/\\.node$/, '');
34
+
35
+ // 1. Try the ID as-is (asset key)
36
+ let resolvedPath = global.__seaNativeModuleMap[id];
37
+
38
+ // 2. Try basename
39
+ if (!resolvedPath) {
40
+ resolvedPath = global.__seaNativeModuleMap[basename];
41
+ }
42
+
43
+ // 3. Try name without extension
44
+ if (!resolvedPath) {
45
+ resolvedPath = global.__seaNativeModuleMap[nameWithoutExt];
46
+ }
47
+
48
+ // 4. Try searching for matching keys
49
+ if (!resolvedPath) {
50
+ for (const [key, value] of Object.entries(global.__seaNativeModuleMap)) {
51
+ if (key.endsWith(basename) || key.endsWith(nameWithoutExt)) {
52
+ resolvedPath = value;
53
+ break;
54
+ }
55
+ }
56
+ }
57
+
58
+ if (resolvedPath) {
59
+ const module = { exports: {} };
60
+ process.dlopen(module, resolvedPath);
61
+ return module.exports;
62
+ } else {
63
+ console.error('[X] Native module not found in map');
64
+ console.error(' Requested:', id);
65
+ console.error(' Available:', Object.keys(global.__seaNativeModuleMap));
66
+ }
67
+ }
68
+ }
69
+
70
+ // Handle bindings module - return a shim that uses our native module map
71
+ if (id === 'bindings') {
72
+ return function(name) {
73
+ if (!name.endsWith('.node')) {
74
+ name += '.node';
75
+ }
76
+ return __requireSeabox(name);
77
+ };
78
+ }
79
+
80
+ // Fall back to original require
81
+ return __originalRequire(id);
82
+ }
83
+ `;
84
+ }
85
+
86
+ /**
87
+ * Replace all require() calls with __requireSeabox() in source code
88
+ * @param {string} sourceCode - The source code to transform
89
+ * @param {boolean} verbose - Enable verbose logging
90
+ * @returns {Object} - { code: string, count: number }
91
+ */
92
+ export function replaceRequireCalls(sourceCode, verbose = false) {
93
+ diag.verbose('Replacing require() calls with __requireSeabox()');
94
+
95
+ const requirePattern = /\brequire\s*\(/g;
96
+ let replacementCount = 0;
97
+
98
+ const transformedCode = sourceCode.replace(requirePattern, (match) => {
99
+ replacementCount++;
100
+ return '__requireSeabox(';
101
+ });
102
+
103
+ diag.verbose(`Replaced ${replacementCount} require() calls`);
104
+
105
+ return {
106
+ code: transformedCode,
107
+ count: replacementCount
108
+ };
109
+ }
110
+
111
+
112
+
113
+