seabox 0.1.1 → 0.2.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.
- package/.mocharc.json +6 -6
- package/LICENSE.MD +21 -21
- package/README.md +310 -310
- package/bin/seabox-rebuild.mjs +88 -88
- package/bin/seabox.mjs +150 -147
- package/lib/blob.mjs +104 -104
- package/lib/bootstrap.cjs +756 -753
- package/lib/build-cache.mjs +199 -199
- package/lib/build.mjs +77 -77
- package/lib/config.mjs +243 -243
- package/lib/crypto-assets.mjs +125 -125
- package/lib/diagnostics.mjs +203 -203
- package/lib/entry-bundler.mjs +64 -64
- package/lib/fetch-node.mjs +172 -172
- package/lib/index.mjs +26 -26
- package/lib/inject.mjs +106 -106
- package/lib/manifest.mjs +100 -100
- package/lib/multi-target-builder.mjs +697 -705
- package/lib/native-scanner.mjs +203 -203
- package/lib/obfuscate.mjs +51 -51
- package/lib/require-shim.mjs +113 -113
- package/lib/rolldown-bundler.mjs +411 -411
- package/lib/unsign.cjs +197 -169
- package/package.json +61 -61
package/lib/require-shim.mjs
CHANGED
|
@@ -1,113 +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
|
-
|
|
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
|
+
|