unpeek 1.7.2 → 1.8.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/dist/ast/index.cjs +2278 -386
- package/dist/ast/index.cjs.map +1 -1
- package/dist/ast/index.d.cts +69 -1
- package/dist/ast/index.d.ts +69 -1
- package/dist/ast/index.js +2266 -385
- package/dist/ast/index.js.map +1 -1
- package/dist/browser.cjs +2265 -393
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +2 -1
- package/dist/browser.d.ts +2 -1
- package/dist/browser.js +2260 -388
- package/dist/browser.js.map +1 -1
- package/dist/bundler/unplugin.cjs +2259 -385
- package/dist/bundler/unplugin.cjs.map +1 -1
- package/dist/bundler/unplugin.js +2258 -384
- package/dist/bundler/unplugin.js.map +1 -1
- package/dist/cli/index.cjs +2292 -396
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2292 -396
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/protect.cjs +2284 -391
- package/dist/cli/protect.cjs.map +1 -1
- package/dist/cli/protect.js +2285 -392
- package/dist/cli/protect.js.map +1 -1
- package/dist/esbuild.cjs +2259 -385
- package/dist/esbuild.cjs.map +1 -1
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2258 -384
- package/dist/esbuild.js.map +1 -1
- package/dist/index.cjs +2309 -411
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2296 -398
- package/dist/index.js.map +1 -1
- package/dist/rollup.cjs +2259 -385
- package/dist/rollup.cjs.map +1 -1
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2258 -384
- package/dist/rollup.js.map +1 -1
- package/dist/runtime/index.cjs +366 -8
- package/dist/runtime/index.cjs.map +1 -1
- package/dist/runtime/index.d.cts +11 -3
- package/dist/runtime/index.d.ts +11 -3
- package/dist/runtime/index.js +364 -8
- package/dist/runtime/index.js.map +1 -1
- package/dist/{types-D-lVfaDU.d.cts → types-CQHprSVk.d.cts} +10 -0
- package/dist/{types-D-lVfaDU.d.ts → types-CQHprSVk.d.ts} +10 -0
- package/dist/vite.cjs +2259 -385
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2258 -384
- package/dist/vite.js.map +1 -1
- package/dist/webpack.cjs +2259 -385
- package/dist/webpack.cjs.map +1 -1
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2258 -384
- package/dist/webpack.js.map +1 -1
- package/package.json +1 -1
package/dist/ast/index.cjs
CHANGED
|
@@ -30,12 +30,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/ast/index.ts
|
|
31
31
|
var ast_exports = {};
|
|
32
32
|
__export(ast_exports, {
|
|
33
|
+
analyzeAstHotPaths: () => analyzeAstHotPaths,
|
|
34
|
+
applyColdFlowGuard: () => applyColdFlowGuard,
|
|
35
|
+
applyMicroVm: () => applyMicroVm,
|
|
36
|
+
applyPolymorphicMangler: () => applyPolymorphicMangler,
|
|
37
|
+
applySaltedStringVault: () => applySaltedStringVault,
|
|
38
|
+
applyStringVault: () => applyStringVault,
|
|
33
39
|
computeFnv1aHash: () => computeFnv1aHash,
|
|
40
|
+
derivePolymorphicIdent: () => derivePolymorphicIdent,
|
|
34
41
|
detectHotPaths: () => detectHotPaths,
|
|
42
|
+
generateCompactJs: () => generateCompactJs,
|
|
43
|
+
injectWasmVault: () => injectWasmVault,
|
|
35
44
|
transformJavaScript: () => transformJavaScript
|
|
36
45
|
});
|
|
37
46
|
module.exports = __toCommonJS(ast_exports);
|
|
38
|
-
var import_astring = require("astring");
|
|
39
47
|
|
|
40
48
|
// src/ast/hot-path-detector.ts
|
|
41
49
|
var acorn = __toESM(require("acorn"), 1);
|
|
@@ -51,7 +59,12 @@ var ARRAY_HOF_METHODS = /* @__PURE__ */ new Set([
|
|
|
51
59
|
"some",
|
|
52
60
|
"every"
|
|
53
61
|
]);
|
|
54
|
-
var
|
|
62
|
+
var ANIMATION_FRAME_CALLS = /* @__PURE__ */ new Set([
|
|
63
|
+
"requestAnimationFrame",
|
|
64
|
+
"cancelAnimationFrame",
|
|
65
|
+
"useFrame"
|
|
66
|
+
]);
|
|
67
|
+
var REACT_HOOK_OR_RENDER_CALLS = /* @__PURE__ */ new Set([
|
|
55
68
|
"useState",
|
|
56
69
|
"useEffect",
|
|
57
70
|
"useMemo",
|
|
@@ -62,9 +75,7 @@ var UI_HOOK_OR_RENDER_CALLS = /* @__PURE__ */ new Set([
|
|
|
62
75
|
"jsxs",
|
|
63
76
|
"_jsx",
|
|
64
77
|
"_jsxs",
|
|
65
|
-
"createElement"
|
|
66
|
-
"requestAnimationFrame",
|
|
67
|
-
"cancelAnimationFrame"
|
|
78
|
+
"createElement"
|
|
68
79
|
]);
|
|
69
80
|
var FUNCTION_NODE_TYPES = /* @__PURE__ */ new Set([
|
|
70
81
|
"FunctionDeclaration",
|
|
@@ -99,8 +110,13 @@ function getFunctionLabel(node, ancestors) {
|
|
|
99
110
|
if (parent.type === "VariableDeclarator" && parent.id?.type === "Identifier") {
|
|
100
111
|
return parent.id.name;
|
|
101
112
|
}
|
|
102
|
-
if (parent.type === "Property"
|
|
103
|
-
|
|
113
|
+
if (parent.type === "Property") {
|
|
114
|
+
if (parent.key?.type === "Identifier") {
|
|
115
|
+
return parent.key.name;
|
|
116
|
+
}
|
|
117
|
+
if (parent.key?.type === "Literal" && typeof parent.key.value === "string") {
|
|
118
|
+
return parent.key.value;
|
|
119
|
+
}
|
|
104
120
|
}
|
|
105
121
|
if (parent.type === "MethodDefinition" && parent.key?.type === "Identifier") {
|
|
106
122
|
return parent.key.name;
|
|
@@ -114,6 +130,9 @@ function getFunctionLabel(node, ancestors) {
|
|
|
114
130
|
function isPascalCaseComponent(name) {
|
|
115
131
|
return /^[A-Z][a-zA-Z0-9]*$/.test(name);
|
|
116
132
|
}
|
|
133
|
+
function isEventHandlerName(name) {
|
|
134
|
+
return /^(?:on|handle)[A-Z]/.test(name);
|
|
135
|
+
}
|
|
117
136
|
function findEnclosingScopeNode(ancestors, selfNode) {
|
|
118
137
|
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
119
138
|
const anc = ancestors[i];
|
|
@@ -123,12 +142,80 @@ function findEnclosingScopeNode(ancestors, selfNode) {
|
|
|
123
142
|
}
|
|
124
143
|
return ancestors[0];
|
|
125
144
|
}
|
|
145
|
+
function findEnclosingFunction(ancestors, selfNode) {
|
|
146
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
147
|
+
if (ancestors[i] !== selfNode && FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
|
|
148
|
+
return ancestors[i];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
function findOutermostModuleFunction(ancestors) {
|
|
154
|
+
const fnList = [];
|
|
155
|
+
for (let i = 0; i < ancestors.length; i++) {
|
|
156
|
+
if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
|
|
157
|
+
fnList.push(ancestors[i]);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (fnList.length === 0) return null;
|
|
161
|
+
if (fnList.length === 1) return fnList[0];
|
|
162
|
+
return fnList[1];
|
|
163
|
+
}
|
|
164
|
+
function isZeroArgIifeFactoryFunction(node) {
|
|
165
|
+
if (!node || node.params && node.params.length > 0) return false;
|
|
166
|
+
if (!node.body || node.body.type !== "BlockStatement") return false;
|
|
167
|
+
const stmts = (node.body.body || []).filter(
|
|
168
|
+
(s) => !(s.type === "ExpressionStatement" && s.directive)
|
|
169
|
+
);
|
|
170
|
+
if (stmts.length !== 1 || stmts[0].type !== "ReturnStatement") return false;
|
|
171
|
+
const retArg = stmts[0].argument;
|
|
172
|
+
if (!retArg || retArg.type !== "CallExpression") return false;
|
|
173
|
+
const callee = retArg.callee;
|
|
174
|
+
return callee && (callee.type === "FunctionExpression" || callee.type === "ArrowFunctionExpression") && callee.body?.type === "BlockStatement";
|
|
175
|
+
}
|
|
176
|
+
function containsLoopOrAnimation(fnNode) {
|
|
177
|
+
let found = false;
|
|
178
|
+
walk.simple(fnNode, {
|
|
179
|
+
ForStatement() {
|
|
180
|
+
found = true;
|
|
181
|
+
},
|
|
182
|
+
ForInStatement() {
|
|
183
|
+
found = true;
|
|
184
|
+
},
|
|
185
|
+
ForOfStatement() {
|
|
186
|
+
found = true;
|
|
187
|
+
},
|
|
188
|
+
WhileStatement() {
|
|
189
|
+
found = true;
|
|
190
|
+
},
|
|
191
|
+
DoWhileStatement() {
|
|
192
|
+
found = true;
|
|
193
|
+
},
|
|
194
|
+
CallExpression(callNode) {
|
|
195
|
+
let cName = "";
|
|
196
|
+
let mProp = "";
|
|
197
|
+
if (callNode.callee?.type === "Identifier") {
|
|
198
|
+
cName = callNode.callee.name;
|
|
199
|
+
} else if (callNode.callee?.type === "MemberExpression" && !callNode.callee.computed && callNode.callee.property?.type === "Identifier") {
|
|
200
|
+
mProp = callNode.callee.property.name;
|
|
201
|
+
}
|
|
202
|
+
if (ANIMATION_FRAME_CALLS.has(cName) || ANIMATION_FRAME_CALLS.has(mProp)) {
|
|
203
|
+
found = true;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
return found;
|
|
208
|
+
}
|
|
126
209
|
function analyzeAstHotPaths(ast) {
|
|
127
210
|
const hotFunctionNodes = /* @__PURE__ */ new WeakSet();
|
|
128
211
|
const coldFunctionNodes = /* @__PURE__ */ new WeakSet();
|
|
129
212
|
const hotExemptNodes = /* @__PURE__ */ new WeakSet();
|
|
213
|
+
const serializedFunctionNodes = /* @__PURE__ */ new WeakSet();
|
|
214
|
+
const serializedWorkerNodes = /* @__PURE__ */ new Set();
|
|
215
|
+
const trueHotNodes = /* @__PURE__ */ new Set();
|
|
216
|
+
const shallowComponentNodes = /* @__PURE__ */ new Set();
|
|
217
|
+
const coldForcedNodes = /* @__PURE__ */ new Set();
|
|
130
218
|
const directHotNodes = /* @__PURE__ */ new Set();
|
|
131
|
-
const serializedFunctionNodes = /* @__PURE__ */ new Set();
|
|
132
219
|
const serializerHelperNodes = /* @__PURE__ */ new Set();
|
|
133
220
|
const allFunctions = [];
|
|
134
221
|
const fnByName = /* @__PURE__ */ new Map();
|
|
@@ -173,42 +260,12 @@ function analyzeAstHotPaths(ast) {
|
|
|
173
260
|
return cur;
|
|
174
261
|
}
|
|
175
262
|
function markSerializedFunction(node) {
|
|
263
|
+
serializedWorkerNodes.add(node);
|
|
176
264
|
serializedFunctionNodes.add(node);
|
|
177
265
|
hotFunctionNodes.add(node);
|
|
178
266
|
directHotNodes.add(node);
|
|
179
267
|
hotExemptNodes.add(node);
|
|
180
268
|
}
|
|
181
|
-
function findEnclosingFunction(ancestors, selfNode) {
|
|
182
|
-
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
183
|
-
if (ancestors[i] !== selfNode && FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
|
|
184
|
-
return ancestors[i];
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
function findOutermostModuleFunction(ancestors) {
|
|
190
|
-
const fnList = [];
|
|
191
|
-
for (let i = 0; i < ancestors.length; i++) {
|
|
192
|
-
if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
|
|
193
|
-
fnList.push(ancestors[i]);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (fnList.length === 0) return null;
|
|
197
|
-
if (fnList.length === 1) return fnList[0];
|
|
198
|
-
return fnList[1];
|
|
199
|
-
}
|
|
200
|
-
function isZeroArgIifeFactoryFunction(node) {
|
|
201
|
-
if (!node || node.params && node.params.length > 0) return false;
|
|
202
|
-
if (!node.body || node.body.type !== "BlockStatement") return false;
|
|
203
|
-
const stmts = (node.body.body || []).filter(
|
|
204
|
-
(s) => !(s.type === "ExpressionStatement" && s.directive)
|
|
205
|
-
);
|
|
206
|
-
if (stmts.length !== 1 || stmts[0].type !== "ReturnStatement") return false;
|
|
207
|
-
const retArg = stmts[0].argument;
|
|
208
|
-
if (!retArg || retArg.type !== "CallExpression") return false;
|
|
209
|
-
const callee = retArg.callee;
|
|
210
|
-
return callee && (callee.type === "FunctionExpression" || callee.type === "ArrowFunctionExpression") && callee.body?.type === "BlockStatement";
|
|
211
|
-
}
|
|
212
269
|
function registerFunctionNode(node, ancestors) {
|
|
213
270
|
const name = getFunctionLabel(node, ancestors);
|
|
214
271
|
allFunctions.push({ node, name });
|
|
@@ -261,6 +318,7 @@ function analyzeAstHotPaths(ast) {
|
|
|
261
318
|
}
|
|
262
319
|
fnScopeChain.set(node, chain);
|
|
263
320
|
if (isPascalCaseComponent(name)) {
|
|
321
|
+
shallowComponentNodes.add(node);
|
|
264
322
|
hotFunctionNodes.add(node);
|
|
265
323
|
directHotNodes.add(node);
|
|
266
324
|
}
|
|
@@ -430,9 +488,15 @@ function analyzeAstHotPaths(ast) {
|
|
|
430
488
|
const outerFn = findOutermostModuleFunction(ancestors);
|
|
431
489
|
if (outerFn) markSerializedFunction(outerFn);
|
|
432
490
|
}
|
|
433
|
-
if (
|
|
491
|
+
if (ANIMATION_FRAME_CALLS.has(calleeName) || ANIMATION_FRAME_CALLS.has(memberProp)) {
|
|
492
|
+
if (enclosingFn) {
|
|
493
|
+
trueHotNodes.add(enclosingFn);
|
|
494
|
+
hotFunctionNodes.add(enclosingFn);
|
|
495
|
+
directHotNodes.add(enclosingFn);
|
|
496
|
+
}
|
|
434
497
|
for (const arg of node.arguments || []) {
|
|
435
498
|
if (FUNCTION_NODE_TYPES.has(arg.type)) {
|
|
499
|
+
trueHotNodes.add(arg);
|
|
436
500
|
hotFunctionNodes.add(arg);
|
|
437
501
|
directHotNodes.add(arg);
|
|
438
502
|
hotExemptNodes.add(arg);
|
|
@@ -445,8 +509,48 @@ function analyzeAstHotPaths(ast) {
|
|
|
445
509
|
}
|
|
446
510
|
}
|
|
447
511
|
}
|
|
448
|
-
if (
|
|
512
|
+
if (ARRAY_HOF_METHODS.has(memberProp)) {
|
|
513
|
+
if (enclosingFn !== null) {
|
|
514
|
+
for (const arg of node.arguments || []) {
|
|
515
|
+
if (FUNCTION_NODE_TYPES.has(arg.type)) {
|
|
516
|
+
trueHotNodes.add(arg);
|
|
517
|
+
hotFunctionNodes.add(arg);
|
|
518
|
+
directHotNodes.add(arg);
|
|
519
|
+
hotExemptNodes.add(arg);
|
|
520
|
+
} else if (arg?.type === "Identifier") {
|
|
521
|
+
allCallSites.push({
|
|
522
|
+
scopeNode: currentScope,
|
|
523
|
+
calleeKey: arg.name,
|
|
524
|
+
args: [arg]
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
} else {
|
|
529
|
+
for (const arg of node.arguments || []) {
|
|
530
|
+
if (FUNCTION_NODE_TYPES.has(arg.type)) {
|
|
531
|
+
coldForcedNodes.add(arg);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (calleeName === "useEffect" || memberProp === "useEffect" || calleeName === "useLayoutEffect" || memberProp === "useLayoutEffect") {
|
|
537
|
+
const effectArg = node.arguments?.[0];
|
|
538
|
+
if (effectArg && FUNCTION_NODE_TYPES.has(effectArg.type)) {
|
|
539
|
+
if (!containsLoopOrAnimation(effectArg)) {
|
|
540
|
+
coldForcedNodes.add(effectArg);
|
|
541
|
+
if (effectArg.body?.type === "BlockStatement") {
|
|
542
|
+
for (const s of effectArg.body.body || []) {
|
|
543
|
+
if (s.type === "ReturnStatement" && s.argument && FUNCTION_NODE_TYPES.has(s.argument.type) && !containsLoopOrAnimation(s.argument)) {
|
|
544
|
+
coldForcedNodes.add(s.argument);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (REACT_HOOK_OR_RENDER_CALLS.has(calleeName) || REACT_HOOK_OR_RENDER_CALLS.has(memberProp)) {
|
|
449
552
|
if (enclosingFn) {
|
|
553
|
+
shallowComponentNodes.add(enclosingFn);
|
|
450
554
|
hotFunctionNodes.add(enclosingFn);
|
|
451
555
|
directHotNodes.add(enclosingFn);
|
|
452
556
|
}
|
|
@@ -454,23 +558,48 @@ function analyzeAstHotPaths(ast) {
|
|
|
454
558
|
},
|
|
455
559
|
ForStatement(node, _state, ancestors) {
|
|
456
560
|
hotExemptNodes.add(node);
|
|
457
|
-
|
|
561
|
+
const enc = findEnclosingFunction(ancestors);
|
|
562
|
+
if (enc) {
|
|
563
|
+
trueHotNodes.add(enc);
|
|
564
|
+
hotFunctionNodes.add(enc);
|
|
565
|
+
directHotNodes.add(enc);
|
|
566
|
+
}
|
|
458
567
|
},
|
|
459
568
|
ForInStatement(node, _state, ancestors) {
|
|
460
569
|
hotExemptNodes.add(node);
|
|
461
|
-
|
|
570
|
+
const enc = findEnclosingFunction(ancestors);
|
|
571
|
+
if (enc) {
|
|
572
|
+
trueHotNodes.add(enc);
|
|
573
|
+
hotFunctionNodes.add(enc);
|
|
574
|
+
directHotNodes.add(enc);
|
|
575
|
+
}
|
|
462
576
|
},
|
|
463
577
|
ForOfStatement(node, _state, ancestors) {
|
|
464
578
|
hotExemptNodes.add(node);
|
|
465
|
-
|
|
579
|
+
const enc = findEnclosingFunction(ancestors);
|
|
580
|
+
if (enc) {
|
|
581
|
+
trueHotNodes.add(enc);
|
|
582
|
+
hotFunctionNodes.add(enc);
|
|
583
|
+
directHotNodes.add(enc);
|
|
584
|
+
}
|
|
466
585
|
},
|
|
467
586
|
WhileStatement(node, _state, ancestors) {
|
|
468
587
|
hotExemptNodes.add(node);
|
|
469
|
-
|
|
588
|
+
const enc = findEnclosingFunction(ancestors);
|
|
589
|
+
if (enc) {
|
|
590
|
+
trueHotNodes.add(enc);
|
|
591
|
+
hotFunctionNodes.add(enc);
|
|
592
|
+
directHotNodes.add(enc);
|
|
593
|
+
}
|
|
470
594
|
},
|
|
471
595
|
DoWhileStatement(node, _state, ancestors) {
|
|
472
596
|
hotExemptNodes.add(node);
|
|
473
|
-
|
|
597
|
+
const enc = findEnclosingFunction(ancestors);
|
|
598
|
+
if (enc) {
|
|
599
|
+
trueHotNodes.add(enc);
|
|
600
|
+
hotFunctionNodes.add(enc);
|
|
601
|
+
directHotNodes.add(enc);
|
|
602
|
+
}
|
|
474
603
|
}
|
|
475
604
|
});
|
|
476
605
|
for (const callSite of allCallSites) {
|
|
@@ -487,59 +616,113 @@ function analyzeAstHotPaths(ast) {
|
|
|
487
616
|
markSerializedFunction(targetNode);
|
|
488
617
|
}
|
|
489
618
|
}
|
|
490
|
-
const
|
|
619
|
+
for (const fn of allFunctions) {
|
|
620
|
+
if (!trueHotNodes.has(fn.node) && !containsLoopOrAnimation(fn.node)) {
|
|
621
|
+
if (fn.node.async === true || isEventHandlerName(fn.name)) {
|
|
622
|
+
coldForcedNodes.add(fn.node);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
491
626
|
let forwardChanged = true;
|
|
492
627
|
while (forwardChanged) {
|
|
493
628
|
forwardChanged = false;
|
|
494
629
|
for (const fn of allFunctions) {
|
|
495
|
-
if (
|
|
630
|
+
if (coldForcedNodes.has(fn.node)) continue;
|
|
496
631
|
const chain = fnScopeChain.get(fn.node);
|
|
497
632
|
if (!chain) continue;
|
|
498
633
|
for (const anc of chain) {
|
|
499
|
-
if (anc
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
if (serializedFunctionNodes.has(anc)) {
|
|
634
|
+
if (anc === fn.node) continue;
|
|
635
|
+
if (serializedWorkerNodes.has(anc)) {
|
|
636
|
+
if (!serializedWorkerNodes.has(fn.node)) {
|
|
637
|
+
serializedWorkerNodes.add(fn.node);
|
|
504
638
|
serializedFunctionNodes.add(fn.node);
|
|
639
|
+
hotFunctionNodes.add(fn.node);
|
|
640
|
+
hotExemptNodes.add(fn.node);
|
|
641
|
+
forwardChanged = true;
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
const isModuleContainer = (fnScopeChain.get(anc)?.length ?? 0) <= 1;
|
|
646
|
+
if (trueHotNodes.has(anc) && !shallowComponentNodes.has(anc) && !isModuleContainer) {
|
|
647
|
+
if (!trueHotNodes.has(fn.node)) {
|
|
648
|
+
trueHotNodes.add(fn.node);
|
|
649
|
+
hotFunctionNodes.add(fn.node);
|
|
650
|
+
hotExemptNodes.add(fn.node);
|
|
651
|
+
forwardChanged = true;
|
|
505
652
|
}
|
|
506
|
-
forwardChanged = true;
|
|
507
653
|
break;
|
|
508
654
|
}
|
|
509
655
|
}
|
|
510
656
|
}
|
|
511
|
-
for (const callerNode of Array.from(
|
|
657
|
+
for (const callerNode of Array.from(serializedWorkerNodes)) {
|
|
512
658
|
const calleeNames = callerToCallees.get(callerNode);
|
|
513
659
|
if (!calleeNames) continue;
|
|
514
660
|
for (const cName of calleeNames) {
|
|
515
661
|
const targetNode = resolveCalleeNode(callerNode, cName);
|
|
516
|
-
if (targetNode && !
|
|
517
|
-
|
|
662
|
+
if (targetNode && !serializedWorkerNodes.has(targetNode) && !coldForcedNodes.has(targetNode)) {
|
|
663
|
+
serializedWorkerNodes.add(targetNode);
|
|
664
|
+
serializedFunctionNodes.add(targetNode);
|
|
665
|
+
hotFunctionNodes.add(targetNode);
|
|
666
|
+
hotExemptNodes.add(targetNode);
|
|
667
|
+
forwardChanged = true;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const callerNode of Array.from(trueHotNodes)) {
|
|
672
|
+
const calleeNames = callerToCallees.get(callerNode);
|
|
673
|
+
if (!calleeNames) continue;
|
|
674
|
+
for (const cName of calleeNames) {
|
|
675
|
+
const targetNode = resolveCalleeNode(callerNode, cName);
|
|
676
|
+
if (targetNode && !trueHotNodes.has(targetNode) && !coldForcedNodes.has(targetNode)) {
|
|
677
|
+
trueHotNodes.add(targetNode);
|
|
518
678
|
hotFunctionNodes.add(targetNode);
|
|
519
679
|
hotExemptNodes.add(targetNode);
|
|
520
|
-
if (serializedFunctionNodes.has(callerNode)) {
|
|
521
|
-
serializedFunctionNodes.add(targetNode);
|
|
522
|
-
}
|
|
523
680
|
forwardChanged = true;
|
|
524
681
|
}
|
|
525
682
|
}
|
|
526
683
|
}
|
|
527
684
|
}
|
|
685
|
+
const calleeToCallers = /* @__PURE__ */ new Map();
|
|
686
|
+
for (const [callerNode, calleeNames] of callerToCallees.entries()) {
|
|
687
|
+
for (const cName of calleeNames) {
|
|
688
|
+
const targetNode = resolveCalleeNode(callerNode, cName);
|
|
689
|
+
if (targetNode) {
|
|
690
|
+
let set = calleeToCallers.get(targetNode);
|
|
691
|
+
if (!set) {
|
|
692
|
+
set = /* @__PURE__ */ new Set();
|
|
693
|
+
calleeToCallers.set(targetNode, set);
|
|
694
|
+
}
|
|
695
|
+
set.add(callerNode);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
528
699
|
let changed = true;
|
|
529
700
|
while (changed) {
|
|
530
701
|
changed = false;
|
|
531
702
|
for (const [callerNode, calleeNames] of callerToCallees.entries()) {
|
|
532
|
-
if (hotFunctionNodes.has(callerNode))
|
|
703
|
+
if (hotFunctionNodes.has(callerNode) || shallowComponentNodes.has(callerNode) || coldForcedNodes.has(callerNode)) {
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
533
706
|
for (const cName of calleeNames) {
|
|
534
707
|
const targetNode = resolveCalleeNode(callerNode, cName);
|
|
535
|
-
if (targetNode &&
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
708
|
+
if (targetNode && trueHotNodes.has(targetNode)) {
|
|
709
|
+
const callers = calleeToCallers.get(targetNode);
|
|
710
|
+
const isDedicatedPipeline = callers && callers.size <= 2;
|
|
711
|
+
if (isDedicatedPipeline) {
|
|
712
|
+
trueHotNodes.add(callerNode);
|
|
713
|
+
hotFunctionNodes.add(callerNode);
|
|
714
|
+
hotExemptNodes.add(callerNode);
|
|
715
|
+
changed = true;
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
539
718
|
}
|
|
540
719
|
}
|
|
541
720
|
}
|
|
542
721
|
}
|
|
722
|
+
for (const node of coldForcedNodes) {
|
|
723
|
+
hotFunctionNodes.delete(node);
|
|
724
|
+
hotExemptNodes.delete(node);
|
|
725
|
+
}
|
|
543
726
|
const hotFunctionsSkipped = [];
|
|
544
727
|
const coldFunctionsProtected = [];
|
|
545
728
|
for (const fn of allFunctions) {
|
|
@@ -558,20 +741,10 @@ function analyzeAstHotPaths(ast) {
|
|
|
558
741
|
},
|
|
559
742
|
hotFunctionNodes,
|
|
560
743
|
coldFunctionNodes,
|
|
561
|
-
hotExemptNodes
|
|
744
|
+
hotExemptNodes,
|
|
745
|
+
serializedFunctionNodes
|
|
562
746
|
};
|
|
563
747
|
}
|
|
564
|
-
function markEnclosingFunctionHot(ancestors, hotSet, directHotSet) {
|
|
565
|
-
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
566
|
-
if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
|
|
567
|
-
hotSet.add(ancestors[i]);
|
|
568
|
-
if (directHotSet) {
|
|
569
|
-
directHotSet.add(ancestors[i]);
|
|
570
|
-
}
|
|
571
|
-
break;
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
748
|
function detectHotPaths(code) {
|
|
576
749
|
const ast = parseJavaScriptAst(code);
|
|
577
750
|
if (!ast) {
|
|
@@ -587,38 +760,446 @@ function detectHotPaths(code) {
|
|
|
587
760
|
// src/ast/string-vault.ts
|
|
588
761
|
var acorn2 = __toESM(require("acorn"), 1);
|
|
589
762
|
var walk2 = __toESM(require("acorn-walk"), 1);
|
|
763
|
+
|
|
764
|
+
// src/ast/codegen.ts
|
|
765
|
+
var import_astring = require("astring");
|
|
766
|
+
function derivePolymorphicIdent(seed, role) {
|
|
767
|
+
let h = (2166136261 ^ seed >>> 0) >>> 0;
|
|
768
|
+
for (let i = 0; i < role.length; i++) {
|
|
769
|
+
h ^= role.charCodeAt(i);
|
|
770
|
+
h = Math.imul(h, 16777619) >>> 0;
|
|
771
|
+
}
|
|
772
|
+
const rolePrefixes = {
|
|
773
|
+
vault: "_a",
|
|
774
|
+
shield: "$b",
|
|
775
|
+
decoy: "_d",
|
|
776
|
+
shader: "_s",
|
|
777
|
+
vm: "_m",
|
|
778
|
+
wasm: "_w",
|
|
779
|
+
salted: "_k",
|
|
780
|
+
windowKey: "_z"
|
|
781
|
+
};
|
|
782
|
+
const prefix = rolePrefixes[role] ?? `_${String.fromCharCode(97 + h % 26)}`;
|
|
783
|
+
const suffix = ((h >>> 4) % 1296).toString(36).padStart(2, "0");
|
|
784
|
+
return `${prefix}${suffix}`;
|
|
785
|
+
}
|
|
786
|
+
var OPERATOR_PRECEDENCE = {
|
|
787
|
+
"||": 2,
|
|
788
|
+
"??": 3,
|
|
789
|
+
"&&": 4,
|
|
790
|
+
"|": 5,
|
|
791
|
+
"^": 6,
|
|
792
|
+
"&": 7,
|
|
793
|
+
"==": 8,
|
|
794
|
+
"!=": 8,
|
|
795
|
+
"===": 8,
|
|
796
|
+
"!==": 8,
|
|
797
|
+
"<": 9,
|
|
798
|
+
">": 9,
|
|
799
|
+
"<=": 9,
|
|
800
|
+
">=": 9,
|
|
801
|
+
"in": 9,
|
|
802
|
+
"instanceof": 9,
|
|
803
|
+
"<<": 10,
|
|
804
|
+
">>": 10,
|
|
805
|
+
">>>": 10,
|
|
806
|
+
"+": 11,
|
|
807
|
+
"-": 11,
|
|
808
|
+
"*": 12,
|
|
809
|
+
"%": 12,
|
|
810
|
+
"/": 12,
|
|
811
|
+
"**": 13
|
|
812
|
+
};
|
|
813
|
+
var NEEDS_PARENTHESES = 17;
|
|
814
|
+
function expressionNeedsParenthesis(state, node, parentNode, isRightHand) {
|
|
815
|
+
const nodePrecedence = state.expressionsPrecedence[node.type];
|
|
816
|
+
if (nodePrecedence === NEEDS_PARENTHESES) {
|
|
817
|
+
return true;
|
|
818
|
+
}
|
|
819
|
+
const parentNodePrecedence = state.expressionsPrecedence[parentNode.type];
|
|
820
|
+
if (nodePrecedence !== parentNodePrecedence) {
|
|
821
|
+
return !isRightHand && nodePrecedence === 15 && parentNodePrecedence === 14 && parentNode.operator === "**" || nodePrecedence < parentNodePrecedence;
|
|
822
|
+
}
|
|
823
|
+
if (nodePrecedence !== 13 && nodePrecedence !== 14) {
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
if (node.operator === "**" && parentNode.operator === "**") {
|
|
827
|
+
return !isRightHand;
|
|
828
|
+
}
|
|
829
|
+
if (nodePrecedence === 13 && parentNodePrecedence === 13 && (node.operator === "??" || parentNode.operator === "??")) {
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
if (isRightHand) {
|
|
833
|
+
return OPERATOR_PRECEDENCE[node.operator] <= OPERATOR_PRECEDENCE[parentNode.operator];
|
|
834
|
+
}
|
|
835
|
+
return OPERATOR_PRECEDENCE[node.operator] < OPERATOR_PRECEDENCE[parentNode.operator];
|
|
836
|
+
}
|
|
837
|
+
function formatExpression(state, node, parentNode, isRightHand) {
|
|
838
|
+
const generator = state.generator;
|
|
839
|
+
if (expressionNeedsParenthesis(state, node, parentNode, isRightHand)) {
|
|
840
|
+
state.write("(");
|
|
841
|
+
generator[node.type](node, state);
|
|
842
|
+
state.write(")");
|
|
843
|
+
} else {
|
|
844
|
+
generator[node.type](node, state);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
function formatSequence(state, expressions) {
|
|
848
|
+
const length = expressions.length;
|
|
849
|
+
if (length > 0) {
|
|
850
|
+
state.write("(");
|
|
851
|
+
state.generator[expressions[0].type](expressions[0], state);
|
|
852
|
+
for (let i = 1; i < length; i++) {
|
|
853
|
+
state.write(",");
|
|
854
|
+
state.generator[expressions[i].type](expressions[i], state);
|
|
855
|
+
}
|
|
856
|
+
state.write(")");
|
|
857
|
+
} else {
|
|
858
|
+
state.write("()");
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
function expressionStartsWith(node, char) {
|
|
862
|
+
if (!node) return false;
|
|
863
|
+
if (node.type === "UnaryExpression" && node.prefix) {
|
|
864
|
+
return node.operator === char;
|
|
865
|
+
}
|
|
866
|
+
if (node.type === "UpdateExpression" && node.prefix) {
|
|
867
|
+
return node.operator[0] === char;
|
|
868
|
+
}
|
|
869
|
+
return false;
|
|
870
|
+
}
|
|
871
|
+
function expressionEndsWith(node, char) {
|
|
872
|
+
if (!node) return false;
|
|
873
|
+
if (node.type === "UpdateExpression" && !node.prefix) {
|
|
874
|
+
return node.operator[0] === char;
|
|
875
|
+
}
|
|
876
|
+
return false;
|
|
877
|
+
}
|
|
878
|
+
function isRegExp(node) {
|
|
879
|
+
return node && (node.type === "Literal" && !!node.regex || node.type === "RegExpLiteral");
|
|
880
|
+
}
|
|
881
|
+
var compactGenerator = Object.assign({}, import_astring.baseGenerator, {
|
|
882
|
+
BinaryExpression(node, state) {
|
|
883
|
+
const isIn = node.operator === "in";
|
|
884
|
+
if (isIn) {
|
|
885
|
+
state.write("(");
|
|
886
|
+
}
|
|
887
|
+
formatExpression(state, node.left, node, false);
|
|
888
|
+
const op = node.operator;
|
|
889
|
+
let spaceBefore = false;
|
|
890
|
+
let spaceAfter = false;
|
|
891
|
+
if (op === "in" || op === "instanceof") {
|
|
892
|
+
spaceBefore = true;
|
|
893
|
+
spaceAfter = true;
|
|
894
|
+
} else {
|
|
895
|
+
if ((op === "+" || op === "-") && expressionEndsWith(node.left, op) && !expressionNeedsParenthesis(state, node.left, node, false)) {
|
|
896
|
+
spaceBefore = true;
|
|
897
|
+
}
|
|
898
|
+
if ((op === "+" || op === "-") && expressionStartsWith(node.right, op) && !expressionNeedsParenthesis(state, node.right, node, true) || op === "/" && isRegExp(node.right)) {
|
|
899
|
+
spaceAfter = true;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
if (spaceBefore) state.write(" ");
|
|
903
|
+
state.write(op);
|
|
904
|
+
if (spaceAfter) state.write(" ");
|
|
905
|
+
formatExpression(state, node.right, node, true);
|
|
906
|
+
if (isIn) {
|
|
907
|
+
state.write(")");
|
|
908
|
+
}
|
|
909
|
+
},
|
|
910
|
+
LogicalExpression(node, state) {
|
|
911
|
+
formatExpression(state, node.left, node, false);
|
|
912
|
+
state.write(node.operator);
|
|
913
|
+
formatExpression(state, node.right, node, true);
|
|
914
|
+
},
|
|
915
|
+
AssignmentExpression(node, state) {
|
|
916
|
+
this[node.left.type](node.left, state);
|
|
917
|
+
const op = node.operator;
|
|
918
|
+
if (op.endsWith("+") && expressionStartsWith(node.right, "+") || op.endsWith("-") && expressionStartsWith(node.right, "-") || op.endsWith("/") && isRegExp(node.right)) {
|
|
919
|
+
state.write(op + " ");
|
|
920
|
+
} else {
|
|
921
|
+
state.write(op);
|
|
922
|
+
}
|
|
923
|
+
this[node.right.type](node.right, state);
|
|
924
|
+
},
|
|
925
|
+
ConditionalExpression(node, state) {
|
|
926
|
+
const test = node.test;
|
|
927
|
+
const precedence = state.expressionsPrecedence[test.type];
|
|
928
|
+
if (precedence === NEEDS_PARENTHESES || precedence <= state.expressionsPrecedence.ConditionalExpression) {
|
|
929
|
+
state.write("(");
|
|
930
|
+
this[test.type](test, state);
|
|
931
|
+
state.write(")");
|
|
932
|
+
} else {
|
|
933
|
+
this[test.type](test, state);
|
|
934
|
+
}
|
|
935
|
+
state.write("?");
|
|
936
|
+
this[node.consequent.type](node.consequent, state);
|
|
937
|
+
state.write(":");
|
|
938
|
+
this[node.alternate.type](node.alternate, state);
|
|
939
|
+
},
|
|
940
|
+
ArrayExpression(node, state) {
|
|
941
|
+
state.write("[");
|
|
942
|
+
const elements = node.elements;
|
|
943
|
+
const length = elements.length;
|
|
944
|
+
for (let i = 0; i < length; i++) {
|
|
945
|
+
const element = elements[i];
|
|
946
|
+
if (element != null) {
|
|
947
|
+
this[element.type](element, state);
|
|
948
|
+
}
|
|
949
|
+
if (i + 1 < length) {
|
|
950
|
+
state.write(",");
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
state.write("]");
|
|
954
|
+
},
|
|
955
|
+
ObjectExpression(node, state) {
|
|
956
|
+
state.write("{");
|
|
957
|
+
const properties = node.properties;
|
|
958
|
+
const length = properties.length;
|
|
959
|
+
for (let i = 0; i < length; i++) {
|
|
960
|
+
const property = properties[i];
|
|
961
|
+
this[property.type](property, state);
|
|
962
|
+
if (i + 1 < length) {
|
|
963
|
+
state.write(",");
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
state.write("}");
|
|
967
|
+
},
|
|
968
|
+
Property(node, state) {
|
|
969
|
+
if (node.method || node.kind[0] !== "i") {
|
|
970
|
+
this.MethodDefinition(node, state);
|
|
971
|
+
} else {
|
|
972
|
+
if (!node.shorthand) {
|
|
973
|
+
if (node.computed) {
|
|
974
|
+
state.write("[");
|
|
975
|
+
this[node.key.type](node.key, state);
|
|
976
|
+
state.write("]");
|
|
977
|
+
} else {
|
|
978
|
+
this[node.key.type](node.key, state);
|
|
979
|
+
}
|
|
980
|
+
state.write(":");
|
|
981
|
+
}
|
|
982
|
+
this[node.value.type](node.value, state);
|
|
983
|
+
}
|
|
984
|
+
},
|
|
985
|
+
CallExpression(node, state) {
|
|
986
|
+
formatExpression(state, node.callee, node, false);
|
|
987
|
+
if (node.optional) {
|
|
988
|
+
state.write("?.");
|
|
989
|
+
}
|
|
990
|
+
formatSequence(state, node.arguments);
|
|
991
|
+
},
|
|
992
|
+
SequenceExpression(node, state) {
|
|
993
|
+
formatSequence(state, node.expressions);
|
|
994
|
+
},
|
|
995
|
+
FunctionDeclaration(node, state) {
|
|
996
|
+
state.write(
|
|
997
|
+
(node.async ? "async " : "") + (node.generator ? "function* " : "function ") + (node.id ? node.id.name : ""),
|
|
998
|
+
node
|
|
999
|
+
);
|
|
1000
|
+
formatSequence(state, node.params);
|
|
1001
|
+
this[node.body.type](node.body, state);
|
|
1002
|
+
},
|
|
1003
|
+
FunctionExpression(node, state) {
|
|
1004
|
+
compactGenerator.FunctionDeclaration.call(this, node, state);
|
|
1005
|
+
},
|
|
1006
|
+
ArrowFunctionExpression(node, state) {
|
|
1007
|
+
state.write(node.async ? "async " : "", node);
|
|
1008
|
+
const params = node.params;
|
|
1009
|
+
if (params != null) {
|
|
1010
|
+
if (params.length === 0) {
|
|
1011
|
+
state.write("()");
|
|
1012
|
+
} else if (params.length === 1 && params[0].type === "Identifier") {
|
|
1013
|
+
state.write(params[0].name, params[0]);
|
|
1014
|
+
} else {
|
|
1015
|
+
formatSequence(state, node.params);
|
|
1016
|
+
}
|
|
1017
|
+
} else {
|
|
1018
|
+
state.write("()");
|
|
1019
|
+
}
|
|
1020
|
+
state.write("=>");
|
|
1021
|
+
if (node.body.type[0] === "O") {
|
|
1022
|
+
state.write("(");
|
|
1023
|
+
this.ObjectExpression(node.body, state);
|
|
1024
|
+
state.write(")");
|
|
1025
|
+
} else {
|
|
1026
|
+
this[node.body.type](node.body, state);
|
|
1027
|
+
}
|
|
1028
|
+
},
|
|
1029
|
+
ForStatement(node, state) {
|
|
1030
|
+
state.write("for(");
|
|
1031
|
+
if (node.init != null) {
|
|
1032
|
+
const init = node.init;
|
|
1033
|
+
if (init.type[0] === "V") {
|
|
1034
|
+
this.VariableDeclaration(init, state);
|
|
1035
|
+
} else {
|
|
1036
|
+
this[init.type](init, state);
|
|
1037
|
+
state.write(";");
|
|
1038
|
+
}
|
|
1039
|
+
} else {
|
|
1040
|
+
state.write(";");
|
|
1041
|
+
}
|
|
1042
|
+
if (node.test) {
|
|
1043
|
+
this[node.test.type](node.test, state);
|
|
1044
|
+
}
|
|
1045
|
+
state.write(";");
|
|
1046
|
+
if (node.update) {
|
|
1047
|
+
this[node.update.type](node.update, state);
|
|
1048
|
+
}
|
|
1049
|
+
state.write(")");
|
|
1050
|
+
this[node.body.type](node.body, state);
|
|
1051
|
+
},
|
|
1052
|
+
IfStatement(node, state) {
|
|
1053
|
+
state.write("if(");
|
|
1054
|
+
this[node.test.type](node.test, state);
|
|
1055
|
+
state.write(")");
|
|
1056
|
+
this[node.consequent.type](node.consequent, state);
|
|
1057
|
+
if (node.alternate != null) {
|
|
1058
|
+
if (node.consequent.type === "BlockStatement") {
|
|
1059
|
+
state.write("else");
|
|
1060
|
+
} else {
|
|
1061
|
+
state.write(" else");
|
|
1062
|
+
}
|
|
1063
|
+
if (node.alternate.type === "BlockStatement") {
|
|
1064
|
+
this[node.alternate.type](node.alternate, state);
|
|
1065
|
+
} else if (node.alternate.type === "IfStatement") {
|
|
1066
|
+
state.write(" ");
|
|
1067
|
+
this[node.alternate.type](node.alternate, state);
|
|
1068
|
+
} else {
|
|
1069
|
+
state.write(" ");
|
|
1070
|
+
this[node.alternate.type](node.alternate, state);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
},
|
|
1074
|
+
VariableDeclaration(node, state) {
|
|
1075
|
+
state.write(node.kind + " ");
|
|
1076
|
+
const declarations = node.declarations;
|
|
1077
|
+
const length = declarations.length;
|
|
1078
|
+
for (let i = 0; i < length; i++) {
|
|
1079
|
+
this.VariableDeclarator(declarations[i], state);
|
|
1080
|
+
if (i + 1 < length) {
|
|
1081
|
+
state.write(",");
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
state.write(";");
|
|
1085
|
+
},
|
|
1086
|
+
VariableDeclarator(node, state) {
|
|
1087
|
+
this[node.id.type](node.id, state);
|
|
1088
|
+
if (node.init != null) {
|
|
1089
|
+
state.write("=");
|
|
1090
|
+
this[node.init.type](node.init, state);
|
|
1091
|
+
}
|
|
1092
|
+
},
|
|
1093
|
+
ReturnStatement(node, state) {
|
|
1094
|
+
state.write("return");
|
|
1095
|
+
if (node.argument) {
|
|
1096
|
+
state.write(" ");
|
|
1097
|
+
this[node.argument.type](node.argument, state);
|
|
1098
|
+
}
|
|
1099
|
+
state.write(";");
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
function generateCompactJs(ast) {
|
|
1103
|
+
return (0, import_astring.generate)(ast, {
|
|
1104
|
+
generator: compactGenerator,
|
|
1105
|
+
indent: "",
|
|
1106
|
+
lineEnd: ""
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// src/runtime/integrity-guard.ts
|
|
1111
|
+
var FNV_OFFSET_BASIS = 2166136261;
|
|
1112
|
+
var FNV_PRIME = 16777619;
|
|
1113
|
+
function computeIntegrityHash(source, seed = 0) {
|
|
1114
|
+
let hash = (FNV_OFFSET_BASIS ^ seed >>> 0) >>> 0;
|
|
1115
|
+
for (let i = 0; i < source.length; i++) {
|
|
1116
|
+
hash ^= source.charCodeAt(i);
|
|
1117
|
+
hash = Math.imul(hash, FNV_PRIME) >>> 0;
|
|
1118
|
+
}
|
|
1119
|
+
return hash >>> 0;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
// src/runtime/devtools-deterrent.ts
|
|
1123
|
+
function deriveIntegrityFnName(seed) {
|
|
1124
|
+
return derivePolymorphicIdent(seed, "shield");
|
|
1125
|
+
}
|
|
1126
|
+
function deriveWindowKeySymbol(seed) {
|
|
1127
|
+
return derivePolymorphicIdent(seed, "windowKey");
|
|
1128
|
+
}
|
|
1129
|
+
function createDeterrentSnippet(config) {
|
|
1130
|
+
const seed = config.seed >>> 0;
|
|
1131
|
+
const deterrentMode = config.deterrentMode ?? "passive";
|
|
1132
|
+
const fnName = deriveIntegrityFnName(seed);
|
|
1133
|
+
const sym = config.windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1134
|
+
const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=function(){return 0;};" : "";
|
|
1135
|
+
return `(function ${fnName}(w){if(w){try{var s=Function.prototype.toString,n=w.navigator||0,src=s.call(${fnName}),t=s.call(s).indexOf('[native code]')<0||/\\n/.test(src)||n.webdriver||/HeadlessChrome/.test(n.userAgent);for(var h=0x811c9dc5,i=0;i<src.length;i++)h=Math.imul(h^src.charCodeAt(i),0x01000193)>>>0;var d=(h&31);if(t){w.__unpeek_tampered=!0;w.__unpeek_seed=((w.__unpeek_seed||${seed})^1)>>>0;String.prototype._u=14;w['${sym}']=d^29;try{${consoleSilence}w.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(e){}}else{w['${sym}']=d;}}catch(e){}}})(typeof window!=='undefined'&&window);`;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// src/runtime/index.ts
|
|
1139
|
+
function generateRuntimeShield(config) {
|
|
1140
|
+
const seed = config.seed >>> 0;
|
|
1141
|
+
const windowKeySymbol = config.windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1142
|
+
const integrityCheckFnName = deriveIntegrityFnName(seed);
|
|
1143
|
+
const preambleCode = createDeterrentSnippet({
|
|
1144
|
+
...config,
|
|
1145
|
+
windowKeySymbol
|
|
1146
|
+
});
|
|
1147
|
+
const fnStart = preambleCode.indexOf("function");
|
|
1148
|
+
const fnEnd = preambleCode.lastIndexOf("(") - 1;
|
|
1149
|
+
const fnDecl = preambleCode.slice(fnStart, fnEnd);
|
|
1150
|
+
const computedHash = computeIntegrityHash(fnDecl, 0);
|
|
1151
|
+
const expectedHash = config.expectedIntegrityHash !== void 0 ? config.expectedIntegrityHash >>> 0 : computedHash;
|
|
1152
|
+
const expectedDelta = config.expectedDelta !== void 0 ? config.expectedDelta & 31 : computedHash >>> 0 & 31;
|
|
1153
|
+
return {
|
|
1154
|
+
preambleCode,
|
|
1155
|
+
integrityCheckFnName,
|
|
1156
|
+
expectedHash,
|
|
1157
|
+
windowKeySymbol,
|
|
1158
|
+
expectedDelta
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
// src/ast/string-vault.ts
|
|
1163
|
+
var SAFE_ALPHA = " !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~";
|
|
590
1164
|
var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
|
|
591
1165
|
"use strict",
|
|
592
1166
|
"use client",
|
|
593
1167
|
"use server",
|
|
594
1168
|
"use asm"
|
|
595
1169
|
]);
|
|
596
|
-
var
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
1170
|
+
var STANDARD_PROP_KEYS = /* @__PURE__ */ new Set([
|
|
1171
|
+
"id",
|
|
1172
|
+
"type",
|
|
1173
|
+
"key",
|
|
1174
|
+
"ref",
|
|
1175
|
+
"className",
|
|
1176
|
+
"style",
|
|
1177
|
+
"name",
|
|
1178
|
+
"value",
|
|
1179
|
+
"children",
|
|
1180
|
+
"src",
|
|
1181
|
+
"href",
|
|
1182
|
+
"alt",
|
|
1183
|
+
"role",
|
|
1184
|
+
"tabIndex"
|
|
1185
|
+
]);
|
|
1186
|
+
var TAILWIND_TOKEN_RE = /^(?:flex|grid|block|inline|hidden|relative|absolute|fixed|w-|h-|min-|max-|p[xytrbl]?-|m[xytrbl]?-|space-[xy]-|text-|font-|bg-|border|rounded|shadow|ring|outline|transition|duration|opacity|cursor|pointer-events|select-|items-|justify-|gap-|overflow-|z-|inset-|top-|left-|right-|bottom-|hover:|focus:|focus-visible:|disabled:|dark:|data-\[|aria-|\[&_)/;
|
|
1187
|
+
var SVG_PATH_RE = /^[Mm]\s*-?\d+(?:\.\d+)?[\s,]+-?\d+/;
|
|
1188
|
+
var SHADER_SIG_RE = /@group|@binding|@vertex|@fragment|@compute|vec4f|gl_Position|uniform/;
|
|
1189
|
+
function shouldEncryptString(val, minLen) {
|
|
1190
|
+
if (val.length < minLen || DIRECTIVE_STRINGS.has(val)) return false;
|
|
1191
|
+
if (SVG_PATH_RE.test(val)) return false;
|
|
1192
|
+
if (val.includes(" ")) {
|
|
1193
|
+
const tokens = val.trim().split(/\s+/);
|
|
1194
|
+
if (tokens.length >= 3) {
|
|
1195
|
+
let twHits = 0;
|
|
1196
|
+
for (const t of tokens) {
|
|
1197
|
+
if (TAILWIND_TOKEN_RE.test(t)) twHits++;
|
|
1198
|
+
}
|
|
1199
|
+
if (twHits / tokens.length >= 0.5) return false;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
return true;
|
|
622
1203
|
}
|
|
623
1204
|
function computeFnv1aHash(input, seed = 2166136261) {
|
|
624
1205
|
let hash = seed >>> 0;
|
|
@@ -628,31 +1209,127 @@ function computeFnv1aHash(input, seed = 2166136261) {
|
|
|
628
1209
|
}
|
|
629
1210
|
return hash >>> 0;
|
|
630
1211
|
}
|
|
631
|
-
function
|
|
1212
|
+
function createPermutationTable(seed) {
|
|
1213
|
+
let s = (seed || 305419896) >>> 0;
|
|
1214
|
+
function xorshift() {
|
|
1215
|
+
s ^= s << 13;
|
|
1216
|
+
s ^= s >>> 17;
|
|
1217
|
+
s ^= s << 5;
|
|
1218
|
+
return s >>> 0;
|
|
1219
|
+
}
|
|
1220
|
+
const indices = [];
|
|
1221
|
+
for (let i = 0; i < 92; i++) indices.push(i);
|
|
1222
|
+
for (let i = 91; i > 0; i--) {
|
|
1223
|
+
const j = xorshift() % (i + 1);
|
|
1224
|
+
const tmp = indices[i];
|
|
1225
|
+
indices[i] = indices[j];
|
|
1226
|
+
indices[j] = tmp;
|
|
1227
|
+
}
|
|
1228
|
+
const B = new Array(92);
|
|
1229
|
+
for (let i = 0; i < 46; i++) {
|
|
1230
|
+
const a = indices[2 * i];
|
|
1231
|
+
const b = indices[2 * i + 1];
|
|
1232
|
+
B[a] = b;
|
|
1233
|
+
B[b] = a;
|
|
1234
|
+
}
|
|
1235
|
+
let P_CHARS = "";
|
|
1236
|
+
for (let i = 0; i < 92; i++) {
|
|
1237
|
+
P_CHARS += SAFE_ALPHA[B[i]];
|
|
1238
|
+
}
|
|
1239
|
+
return P_CHARS;
|
|
1240
|
+
}
|
|
1241
|
+
function encodeStringWithSeed(str, P_CHARS, expectedRot) {
|
|
632
1242
|
let out = "";
|
|
633
1243
|
for (let i = 0; i < str.length; i++) {
|
|
634
|
-
const
|
|
635
|
-
const k = (
|
|
636
|
-
|
|
1244
|
+
const c = str.charAt(i);
|
|
1245
|
+
const k = SAFE_ALPHA.indexOf(c);
|
|
1246
|
+
if (k < 0) {
|
|
1247
|
+
out += c;
|
|
1248
|
+
} else {
|
|
1249
|
+
const y = (k + expectedRot) % 92;
|
|
1250
|
+
const z = SAFE_ALPHA.indexOf(P_CHARS.charAt(y));
|
|
1251
|
+
out += SAFE_ALPHA.charAt((z - expectedRot + 92) % 92);
|
|
1252
|
+
}
|
|
637
1253
|
}
|
|
638
1254
|
return out;
|
|
639
1255
|
}
|
|
640
|
-
function
|
|
1256
|
+
function createVaultHelperAst(cacheVarName, encryptedPayloads, fnAst) {
|
|
1257
|
+
return {
|
|
1258
|
+
type: "VariableDeclaration",
|
|
1259
|
+
kind: "var",
|
|
1260
|
+
declarations: [
|
|
1261
|
+
{
|
|
1262
|
+
type: "VariableDeclarator",
|
|
1263
|
+
id: { type: "Identifier", name: cacheVarName },
|
|
1264
|
+
init: {
|
|
1265
|
+
type: "CallExpression",
|
|
1266
|
+
callee: fnAst,
|
|
1267
|
+
arguments: [
|
|
1268
|
+
{
|
|
1269
|
+
type: "ArrayExpression",
|
|
1270
|
+
elements: encryptedPayloads.map((str) => ({
|
|
1271
|
+
type: "Literal",
|
|
1272
|
+
value: str,
|
|
1273
|
+
raw: JSON.stringify(str)
|
|
1274
|
+
}))
|
|
1275
|
+
}
|
|
1276
|
+
],
|
|
1277
|
+
optional: false
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
]
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
function createShaderVaultAst(shaderVarName, shaderPayloads, seed) {
|
|
1284
|
+
const b64List = [];
|
|
1285
|
+
const key = seed >>> 0 & 255 ^ 90;
|
|
1286
|
+
for (let idx = 0; idx < shaderPayloads.length; idx++) {
|
|
1287
|
+
const str = shaderPayloads[idx];
|
|
1288
|
+
const buf = Buffer.from(str, "utf8");
|
|
1289
|
+
for (let i = 0; i < buf.length; i++) {
|
|
1290
|
+
buf[i] ^= key;
|
|
1291
|
+
}
|
|
1292
|
+
b64List.push(buf.toString("base64"));
|
|
1293
|
+
}
|
|
1294
|
+
const shaderCode = `var ${shaderVarName} = (function(){var B=${JSON.stringify(
|
|
1295
|
+
b64List
|
|
1296
|
+
)};var k=${key};var b=B.map(function(s){var bin=typeof atob==='function'?atob(s):(typeof Buffer!=='undefined'?Buffer.from(s,'base64').toString('binary'):'');var u=new Uint8Array(bin.length);for(var i=0;i<bin.length;i++)u[i]=bin.charCodeAt(i);return u;});var d=typeof TextDecoder!=='undefined'?new TextDecoder():null;return function(i){var u=b[i];for(var j=0;j<u.length;j++)u[j]^=k;try{return d?d.decode(u):String.fromCharCode.apply(null,u);}finally{for(var j=0;j<u.length;j++)u[j]^=k;}};})();`;
|
|
1297
|
+
return acorn2.parse(shaderCode, { ecmaVersion: "latest", sourceType: "script" }).body[0];
|
|
1298
|
+
}
|
|
1299
|
+
function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, windowKeySymbol, expectedDelta) {
|
|
1300
|
+
const cacheVarName = derivePolymorphicIdent(seed, "vault");
|
|
1301
|
+
const shaderVarName = derivePolymorphicIdent(seed, "shader");
|
|
1302
|
+
const sym = windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1303
|
+
let expDelta = expectedDelta;
|
|
1304
|
+
if (expDelta === void 0) {
|
|
1305
|
+
const shield = generateRuntimeShield({ seed, windowKeySymbol: sym });
|
|
1306
|
+
expDelta = shield.expectedDelta ?? 0;
|
|
1307
|
+
}
|
|
641
1308
|
const alreadyVaulted = ast.body.some(
|
|
642
1309
|
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
643
|
-
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uC")
|
|
1310
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && (d.id.name.startsWith("_uC") || d.id.name === cacheVarName || d.init?.type === "CallExpression" && d.init.callee?.type === "FunctionExpression" && d.init.callee?.id?.name === "f" && d.init.arguments?.[0]?.type === "ArrayExpression")
|
|
644
1311
|
)
|
|
645
1312
|
);
|
|
646
1313
|
if (alreadyVaulted) {
|
|
647
1314
|
return {
|
|
648
1315
|
encryptedStringsCount: 0,
|
|
649
|
-
integrityToken: computeFnv1aHash("already-vaulted", seed)
|
|
1316
|
+
integrityToken: computeFnv1aHash("already-vaulted", seed),
|
|
1317
|
+
shaderBuffersCount: 0
|
|
650
1318
|
};
|
|
651
1319
|
}
|
|
652
|
-
const
|
|
653
|
-
const
|
|
1320
|
+
const P_CHARS = createPermutationTable(seed);
|
|
1321
|
+
const fBody = `var s=Function.prototype.toString,w=typeof window!=='undefined'&&window,src=s.call(f);for(var h=0x811c9dc5,i=0;i<src.length;i++)h=Math.imul(h^src.charCodeAt(i),0x01000193)>>>0;var rot=(h^(a._u||0)^(w&&w['${sym}']!=null?w['${sym}']:${expDelta}))&31;var A="${SAFE_ALPHA}";var P="${P_CHARS}";return a.map(function(s){var o="";for(var j=0;j<s.length;j++){var c=s.charAt(j),k=A.indexOf(c);if(k<0){o+=c;}else{var y=(k+rot)%92;var z=A.indexOf(P.charAt(y));o+=A.charAt((z-rot+92)%92);}}return o;});`;
|
|
1322
|
+
const fnAst = acorn2.parse(`(function f(a){${fBody}})`, {
|
|
1323
|
+
ecmaVersion: "latest",
|
|
1324
|
+
sourceType: "script"
|
|
1325
|
+
}).body[0].expression;
|
|
1326
|
+
const compactFnStr = generateCompactJs(fnAst);
|
|
1327
|
+
const buildH = computeIntegrityHash(compactFnStr, 0);
|
|
1328
|
+
const expectedRot = (buildH ^ 0 ^ expDelta) & 31;
|
|
654
1329
|
const stringToIndex = /* @__PURE__ */ new Map();
|
|
655
1330
|
const encryptedPayloads = [];
|
|
1331
|
+
const shaderToIndex = /* @__PURE__ */ new Map();
|
|
1332
|
+
const shaderPayloads = [];
|
|
656
1333
|
const targetsToReplace = [];
|
|
657
1334
|
const customWalkBase = {
|
|
658
1335
|
...walk2.base,
|
|
@@ -667,7 +1344,6 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
667
1344
|
Literal(node, _state, ancestors) {
|
|
668
1345
|
if (typeof node.value !== "string") return;
|
|
669
1346
|
const val = node.value;
|
|
670
|
-
if (val.length < minStringLength) return;
|
|
671
1347
|
if (DIRECTIVE_STRINGS.has(val)) return;
|
|
672
1348
|
if (hotExemptNodes) {
|
|
673
1349
|
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
@@ -685,59 +1361,125 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
685
1361
|
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
686
1362
|
return;
|
|
687
1363
|
}
|
|
688
|
-
if (parent.type === "Property" && parent.key === node) {
|
|
689
|
-
parent.computed = true;
|
|
690
|
-
}
|
|
691
1364
|
if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
|
|
692
1365
|
return;
|
|
693
1366
|
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
1367
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
1368
|
+
if (STANDARD_PROP_KEYS.has(val)) {
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
const isShader = val.length >= 256 || SHADER_SIG_RE.test(val);
|
|
1373
|
+
if (isShader) {
|
|
1374
|
+
let idx = shaderToIndex.get(val);
|
|
1375
|
+
if (idx === void 0) {
|
|
1376
|
+
idx = shaderPayloads.length;
|
|
1377
|
+
shaderToIndex.set(val, idx);
|
|
1378
|
+
shaderPayloads.push(val);
|
|
1379
|
+
}
|
|
1380
|
+
targetsToReplace.push({ type: "shader", node, parent, idx });
|
|
1381
|
+
} else {
|
|
1382
|
+
if (!shouldEncryptString(val, minStringLength)) return;
|
|
1383
|
+
let idx = stringToIndex.get(val);
|
|
1384
|
+
if (idx === void 0) {
|
|
1385
|
+
idx = encryptedPayloads.length;
|
|
1386
|
+
stringToIndex.set(val, idx);
|
|
1387
|
+
encryptedPayloads.push(
|
|
1388
|
+
encodeStringWithSeed(val, P_CHARS, expectedRot)
|
|
1389
|
+
);
|
|
1390
|
+
}
|
|
1391
|
+
targetsToReplace.push({ type: "string", node, parent, idx });
|
|
1392
|
+
}
|
|
1393
|
+
},
|
|
1394
|
+
TemplateLiteral(node, _state, ancestors) {
|
|
1395
|
+
if (!node.quasis || node.quasis.length !== 1) return;
|
|
1396
|
+
if (node.expressions && node.expressions.length !== 0) return;
|
|
1397
|
+
if (hotExemptNodes) {
|
|
1398
|
+
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
1399
|
+
if (hotExemptNodes.has(ancestors[i])) {
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
const parent = ancestors[ancestors.length - 2];
|
|
1405
|
+
if (!parent) return;
|
|
1406
|
+
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
1407
|
+
if (typeof val !== "string" || DIRECTIVE_STRINGS.has(val)) return;
|
|
1408
|
+
const isShader = val.length >= 256 || SHADER_SIG_RE.test(val);
|
|
1409
|
+
if (isShader) {
|
|
1410
|
+
let idx = shaderToIndex.get(val);
|
|
1411
|
+
if (idx === void 0) {
|
|
1412
|
+
idx = shaderPayloads.length;
|
|
1413
|
+
shaderToIndex.set(val, idx);
|
|
1414
|
+
shaderPayloads.push(val);
|
|
1415
|
+
}
|
|
1416
|
+
targetsToReplace.push({ type: "shader", node, parent, idx });
|
|
1417
|
+
} else {
|
|
1418
|
+
if (!shouldEncryptString(val, minStringLength)) return;
|
|
1419
|
+
let idx = stringToIndex.get(val);
|
|
1420
|
+
if (idx === void 0) {
|
|
1421
|
+
idx = encryptedPayloads.length;
|
|
1422
|
+
stringToIndex.set(val, idx);
|
|
1423
|
+
encryptedPayloads.push(
|
|
1424
|
+
encodeStringWithSeed(val, P_CHARS, expectedRot)
|
|
1425
|
+
);
|
|
1426
|
+
}
|
|
1427
|
+
targetsToReplace.push({ type: "string", node, parent, idx });
|
|
699
1428
|
}
|
|
700
|
-
targetsToReplace.push({ node, parent });
|
|
701
1429
|
}
|
|
702
1430
|
},
|
|
703
1431
|
customWalkBase
|
|
704
1432
|
);
|
|
705
|
-
if (targetsToReplace.length === 0
|
|
1433
|
+
if (targetsToReplace.length === 0) {
|
|
706
1434
|
return {
|
|
707
1435
|
encryptedStringsCount: 0,
|
|
708
|
-
integrityToken: computeFnv1aHash("empty", seed)
|
|
1436
|
+
integrityToken: computeFnv1aHash("empty", seed),
|
|
1437
|
+
shaderBuffersCount: 0
|
|
709
1438
|
};
|
|
710
1439
|
}
|
|
711
1440
|
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
712
1441
|
const integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
713
|
-
const
|
|
714
|
-
const cacheVarName = `_uC${suffix}`;
|
|
715
|
-
for (const { node, parent } of targetsToReplace) {
|
|
1442
|
+
for (const { type, node, parent, idx } of targetsToReplace) {
|
|
716
1443
|
if (parent?.type === "Property" && parent.key === node) {
|
|
717
1444
|
parent.computed = true;
|
|
718
1445
|
}
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
1446
|
+
if (type === "shader") {
|
|
1447
|
+
node.type = "CallExpression";
|
|
1448
|
+
node.callee = {
|
|
1449
|
+
type: "Identifier",
|
|
1450
|
+
name: shaderVarName
|
|
1451
|
+
};
|
|
1452
|
+
node.arguments = [
|
|
1453
|
+
{
|
|
1454
|
+
type: "Literal",
|
|
1455
|
+
value: idx,
|
|
1456
|
+
raw: String(idx)
|
|
1457
|
+
}
|
|
1458
|
+
];
|
|
1459
|
+
node.optional = false;
|
|
1460
|
+
delete node.value;
|
|
1461
|
+
delete node.raw;
|
|
1462
|
+
delete node.quasis;
|
|
1463
|
+
delete node.expressions;
|
|
1464
|
+
} else {
|
|
1465
|
+
node.type = "MemberExpression";
|
|
1466
|
+
node.object = {
|
|
1467
|
+
type: "Identifier",
|
|
1468
|
+
name: cacheVarName
|
|
1469
|
+
};
|
|
1470
|
+
node.property = {
|
|
1471
|
+
type: "Literal",
|
|
1472
|
+
value: idx,
|
|
1473
|
+
raw: String(idx)
|
|
1474
|
+
};
|
|
1475
|
+
node.computed = true;
|
|
1476
|
+
node.optional = false;
|
|
1477
|
+
delete node.value;
|
|
1478
|
+
delete node.raw;
|
|
1479
|
+
delete node.quasis;
|
|
1480
|
+
delete node.expressions;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
741
1483
|
let insertIdx = 0;
|
|
742
1484
|
while (insertIdx < ast.body.length) {
|
|
743
1485
|
const stmt = ast.body[insertIdx];
|
|
@@ -748,11 +1490,27 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
748
1490
|
break;
|
|
749
1491
|
}
|
|
750
1492
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
1493
|
+
if (encryptedPayloads.length > 0) {
|
|
1494
|
+
const vaultHelperStmt = createVaultHelperAst(
|
|
1495
|
+
cacheVarName,
|
|
1496
|
+
encryptedPayloads,
|
|
1497
|
+
fnAst
|
|
1498
|
+
);
|
|
1499
|
+
ast.body.splice(insertIdx++, 0, vaultHelperStmt);
|
|
1500
|
+
}
|
|
1501
|
+
if (shaderPayloads.length > 0) {
|
|
1502
|
+
const shaderHelperStmt = createShaderVaultAst(
|
|
1503
|
+
shaderVarName,
|
|
1504
|
+
shaderPayloads,
|
|
1505
|
+
seed
|
|
1506
|
+
);
|
|
1507
|
+
ast.body.splice(insertIdx++, 0, shaderHelperStmt);
|
|
1508
|
+
}
|
|
1509
|
+
return {
|
|
1510
|
+
encryptedStringsCount: encryptedPayloads.length,
|
|
1511
|
+
integrityToken,
|
|
1512
|
+
shaderBuffersCount: shaderPayloads.length
|
|
1513
|
+
};
|
|
756
1514
|
}
|
|
757
1515
|
|
|
758
1516
|
// src/ast/polymorphic-mangler.ts
|
|
@@ -850,9 +1608,9 @@ function collectPatternTargets(pattern, out, shorthandProp) {
|
|
|
850
1608
|
}
|
|
851
1609
|
}
|
|
852
1610
|
function isMangleableName(origName) {
|
|
853
|
-
return !RESERVED_IDENTIFIERS.has(origName) && !origName.startsWith("_u") && !origName.startsWith("_0x");
|
|
1611
|
+
return origName.length > 2 && !RESERVED_IDENTIFIERS.has(origName) && !origName.startsWith("_u") && !origName.startsWith("_0x") && !origName.startsWith("$$");
|
|
854
1612
|
}
|
|
855
|
-
function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
1613
|
+
function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes) {
|
|
856
1614
|
const scopeRecords = /* @__PURE__ */ new Map();
|
|
857
1615
|
const existingIdentifiers = /* @__PURE__ */ new Set();
|
|
858
1616
|
let counter = 0;
|
|
@@ -898,11 +1656,16 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
898
1656
|
}
|
|
899
1657
|
return ancestors[0];
|
|
900
1658
|
}
|
|
901
|
-
function registerBindingTarget(scopeNode, target, shouldMangle) {
|
|
1659
|
+
function registerBindingTarget(scopeNode, target, shouldMangle, fnScope) {
|
|
902
1660
|
const scope = scopeRecords.get(scopeNode);
|
|
903
1661
|
if (!scope) return;
|
|
904
1662
|
const origName = target.idNode.name;
|
|
905
|
-
|
|
1663
|
+
let canMangle = shouldMangle && isMangleableName(origName);
|
|
1664
|
+
if (canMangle && fnScope) {
|
|
1665
|
+
if (serializedFunctionNodes && serializedFunctionNodes.has(fnScope)) {
|
|
1666
|
+
canMangle = false;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
906
1669
|
let entry = scope.bindings.get(origName);
|
|
907
1670
|
if (!entry) {
|
|
908
1671
|
entry = {
|
|
@@ -941,7 +1704,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
941
1704
|
const targets = [];
|
|
942
1705
|
collectPatternTargets(param, targets);
|
|
943
1706
|
for (const target of targets) {
|
|
944
|
-
registerBindingTarget(fnNode, target, true);
|
|
1707
|
+
registerBindingTarget(fnNode, target, true, fnNode);
|
|
945
1708
|
}
|
|
946
1709
|
}
|
|
947
1710
|
}
|
|
@@ -1011,10 +1774,11 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1011
1774
|
CatchClause(node, _state, ancestors) {
|
|
1012
1775
|
ensureScopeChain(ancestors);
|
|
1013
1776
|
if (node.param) {
|
|
1777
|
+
const fnScope = findEnclosingFunctionNode(ancestors);
|
|
1014
1778
|
const targets = [];
|
|
1015
1779
|
collectPatternTargets(node.param, targets);
|
|
1016
1780
|
for (const target of targets) {
|
|
1017
|
-
registerBindingTarget(node, target, true);
|
|
1781
|
+
registerBindingTarget(node, target, true, fnScope);
|
|
1018
1782
|
}
|
|
1019
1783
|
}
|
|
1020
1784
|
},
|
|
@@ -1049,7 +1813,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1049
1813
|
}
|
|
1050
1814
|
}
|
|
1051
1815
|
}
|
|
1052
|
-
registerBindingTarget(targetScopeNode, target, true);
|
|
1816
|
+
registerBindingTarget(targetScopeNode, target, true, fnScope);
|
|
1053
1817
|
}
|
|
1054
1818
|
}
|
|
1055
1819
|
}
|
|
@@ -1065,12 +1829,16 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1065
1829
|
}
|
|
1066
1830
|
return false;
|
|
1067
1831
|
}
|
|
1832
|
+
const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
1068
1833
|
function makeScopedMangledName(scope, localIndex) {
|
|
1069
1834
|
counter++;
|
|
1070
1835
|
let salt = 0;
|
|
1071
1836
|
while (true) {
|
|
1072
1837
|
const h = computeFnv1aHash(`s${scope.depth}:i${localIndex}:${salt}`, seed);
|
|
1073
|
-
const
|
|
1838
|
+
const prefix = (h & 1) === 0 ? "_" : "$";
|
|
1839
|
+
const ch = ALPHABET[((h >>> 1) + localIndex) % ALPHABET.length];
|
|
1840
|
+
const num = (localIndex + salt).toString(36);
|
|
1841
|
+
const candidate = `${prefix}${ch}${num}`;
|
|
1074
1842
|
if (!isNameUsedInScopeOrAncestors(candidate, scope)) {
|
|
1075
1843
|
return candidate;
|
|
1076
1844
|
}
|
|
@@ -1180,227 +1948,420 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1180
1948
|
|
|
1181
1949
|
// src/ast/cold-flow-guard.ts
|
|
1182
1950
|
var walk4 = __toESM(require("acorn-walk"), 1);
|
|
1183
|
-
function
|
|
1184
|
-
const
|
|
1185
|
-
if (
|
|
1951
|
+
function createDecoyObjectExpression(model) {
|
|
1952
|
+
const m = Math.abs(model) % 3;
|
|
1953
|
+
if (m === 0) {
|
|
1186
1954
|
return {
|
|
1187
|
-
type: "
|
|
1188
|
-
|
|
1955
|
+
type: "ObjectExpression",
|
|
1956
|
+
properties: [
|
|
1189
1957
|
{
|
|
1190
|
-
type: "
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
value: { type: "Literal", value: 1, raw: "1" },
|
|
1220
|
-
kind: "init",
|
|
1221
|
-
computed: false,
|
|
1222
|
-
method: false,
|
|
1223
|
-
shorthand: false
|
|
1224
|
-
}
|
|
1225
|
-
]
|
|
1226
|
-
}
|
|
1958
|
+
type: "Property",
|
|
1959
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
1960
|
+
value: {
|
|
1961
|
+
type: "Literal",
|
|
1962
|
+
value: "/api/v2/auth/admin-bypass",
|
|
1963
|
+
raw: '"/api/v2/auth/admin-bypass"'
|
|
1964
|
+
},
|
|
1965
|
+
kind: "init",
|
|
1966
|
+
computed: false,
|
|
1967
|
+
method: false,
|
|
1968
|
+
shorthand: false
|
|
1969
|
+
},
|
|
1970
|
+
{
|
|
1971
|
+
type: "Property",
|
|
1972
|
+
key: { type: "Identifier", name: "superadmin_token" },
|
|
1973
|
+
value: { type: "Literal", value: 1, raw: "1" },
|
|
1974
|
+
kind: "init",
|
|
1975
|
+
computed: false,
|
|
1976
|
+
method: false,
|
|
1977
|
+
shorthand: false
|
|
1978
|
+
},
|
|
1979
|
+
{
|
|
1980
|
+
type: "Property",
|
|
1981
|
+
key: { type: "Identifier", name: "elevate" },
|
|
1982
|
+
value: { type: "Literal", value: 1, raw: "1" },
|
|
1983
|
+
kind: "init",
|
|
1984
|
+
computed: false,
|
|
1985
|
+
method: false,
|
|
1986
|
+
shorthand: false
|
|
1227
1987
|
}
|
|
1228
1988
|
]
|
|
1229
1989
|
};
|
|
1230
1990
|
}
|
|
1231
|
-
if (
|
|
1991
|
+
if (m === 1) {
|
|
1232
1992
|
return {
|
|
1233
|
-
type: "
|
|
1234
|
-
|
|
1993
|
+
type: "ObjectExpression",
|
|
1994
|
+
properties: [
|
|
1235
1995
|
{
|
|
1236
|
-
type: "
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
value: { type: "Literal", value: true, raw: "true" },
|
|
1279
|
-
kind: "init",
|
|
1280
|
-
computed: false,
|
|
1281
|
-
method: false,
|
|
1282
|
-
shorthand: false
|
|
1283
|
-
}
|
|
1284
|
-
]
|
|
1285
|
-
}
|
|
1996
|
+
type: "Property",
|
|
1997
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
1998
|
+
value: {
|
|
1999
|
+
type: "Literal",
|
|
2000
|
+
value: "/api/v2/auth/verify-master",
|
|
2001
|
+
raw: '"/api/v2/auth/verify-master"'
|
|
2002
|
+
},
|
|
2003
|
+
kind: "init",
|
|
2004
|
+
computed: false,
|
|
2005
|
+
method: false,
|
|
2006
|
+
shorthand: false
|
|
2007
|
+
},
|
|
2008
|
+
{
|
|
2009
|
+
type: "Property",
|
|
2010
|
+
key: { type: "Identifier", name: "_verifyMasterToken" },
|
|
2011
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2012
|
+
kind: "init",
|
|
2013
|
+
computed: false,
|
|
2014
|
+
method: false,
|
|
2015
|
+
shorthand: false
|
|
2016
|
+
},
|
|
2017
|
+
{
|
|
2018
|
+
type: "Property",
|
|
2019
|
+
key: { type: "Identifier", name: "_bypassToken" },
|
|
2020
|
+
value: {
|
|
2021
|
+
type: "Literal",
|
|
2022
|
+
value: "mock_jwt_bypass_signature",
|
|
2023
|
+
raw: '"mock_jwt_bypass_signature"'
|
|
2024
|
+
},
|
|
2025
|
+
kind: "init",
|
|
2026
|
+
computed: false,
|
|
2027
|
+
method: false,
|
|
2028
|
+
shorthand: false
|
|
2029
|
+
},
|
|
2030
|
+
{
|
|
2031
|
+
type: "Property",
|
|
2032
|
+
key: { type: "Identifier", name: "authorized" },
|
|
2033
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2034
|
+
kind: "init",
|
|
2035
|
+
computed: false,
|
|
2036
|
+
method: false,
|
|
2037
|
+
shorthand: false
|
|
1286
2038
|
}
|
|
1287
2039
|
]
|
|
1288
2040
|
};
|
|
1289
2041
|
}
|
|
2042
|
+
return {
|
|
2043
|
+
type: "ObjectExpression",
|
|
2044
|
+
properties: [
|
|
2045
|
+
{
|
|
2046
|
+
type: "Property",
|
|
2047
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
2048
|
+
value: {
|
|
2049
|
+
type: "Literal",
|
|
2050
|
+
value: "/api/v2/auth/debug-telemetry",
|
|
2051
|
+
raw: '"/api/v2/auth/debug-telemetry"'
|
|
2052
|
+
},
|
|
2053
|
+
kind: "init",
|
|
2054
|
+
computed: false,
|
|
2055
|
+
method: false,
|
|
2056
|
+
shorthand: false
|
|
2057
|
+
},
|
|
2058
|
+
{
|
|
2059
|
+
type: "Property",
|
|
2060
|
+
key: { type: "Identifier", name: "debug_supervisor_override" },
|
|
2061
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2062
|
+
kind: "init",
|
|
2063
|
+
computed: false,
|
|
2064
|
+
method: false,
|
|
2065
|
+
shorthand: false
|
|
2066
|
+
},
|
|
2067
|
+
{
|
|
2068
|
+
type: "Property",
|
|
2069
|
+
key: { type: "Identifier", name: "_bypassToken" },
|
|
2070
|
+
value: {
|
|
2071
|
+
type: "Literal",
|
|
2072
|
+
value: "superadmin_token",
|
|
2073
|
+
raw: '"superadmin_token"'
|
|
2074
|
+
},
|
|
2075
|
+
kind: "init",
|
|
2076
|
+
computed: false,
|
|
2077
|
+
method: false,
|
|
2078
|
+
shorthand: false
|
|
2079
|
+
},
|
|
2080
|
+
{
|
|
2081
|
+
type: "Property",
|
|
2082
|
+
key: { type: "Identifier", name: "mfa_bypassed" },
|
|
2083
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2084
|
+
kind: "init",
|
|
2085
|
+
computed: false,
|
|
2086
|
+
method: false,
|
|
2087
|
+
shorthand: false
|
|
2088
|
+
}
|
|
2089
|
+
]
|
|
2090
|
+
};
|
|
2091
|
+
}
|
|
2092
|
+
function createDecoyConsequent(fnIndex) {
|
|
2093
|
+
return {
|
|
2094
|
+
type: "BlockStatement",
|
|
2095
|
+
body: [
|
|
2096
|
+
{
|
|
2097
|
+
type: "ReturnStatement",
|
|
2098
|
+
argument: createDecoyObjectExpression(fnIndex)
|
|
2099
|
+
}
|
|
2100
|
+
]
|
|
2101
|
+
};
|
|
2102
|
+
}
|
|
2103
|
+
function createSharedDecoyConsequent(decoyIdent, fnIndex) {
|
|
2104
|
+
const model = Math.abs(fnIndex) % 3;
|
|
1290
2105
|
return {
|
|
1291
2106
|
type: "BlockStatement",
|
|
1292
2107
|
body: [
|
|
1293
2108
|
{
|
|
1294
2109
|
type: "ReturnStatement",
|
|
1295
2110
|
argument: {
|
|
1296
|
-
type: "
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
},
|
|
1306
|
-
kind: "init",
|
|
1307
|
-
computed: false,
|
|
1308
|
-
method: false,
|
|
1309
|
-
shorthand: false
|
|
1310
|
-
},
|
|
1311
|
-
{
|
|
1312
|
-
type: "Property",
|
|
1313
|
-
key: { type: "Identifier", name: "debug_supervisor_override" },
|
|
1314
|
-
value: { type: "Literal", value: true, raw: "true" },
|
|
1315
|
-
kind: "init",
|
|
1316
|
-
computed: false,
|
|
1317
|
-
method: false,
|
|
1318
|
-
shorthand: false
|
|
1319
|
-
},
|
|
1320
|
-
{
|
|
1321
|
-
type: "Property",
|
|
1322
|
-
key: { type: "Identifier", name: "_bypassToken" },
|
|
1323
|
-
value: {
|
|
1324
|
-
type: "Literal",
|
|
1325
|
-
value: "superadmin_token",
|
|
1326
|
-
raw: '"superadmin_token"'
|
|
1327
|
-
},
|
|
1328
|
-
kind: "init",
|
|
1329
|
-
computed: false,
|
|
1330
|
-
method: false,
|
|
1331
|
-
shorthand: false
|
|
1332
|
-
},
|
|
1333
|
-
{
|
|
1334
|
-
type: "Property",
|
|
1335
|
-
key: { type: "Identifier", name: "mfa_bypassed" },
|
|
1336
|
-
value: { type: "Literal", value: true, raw: "true" },
|
|
1337
|
-
kind: "init",
|
|
1338
|
-
computed: false,
|
|
1339
|
-
method: false,
|
|
1340
|
-
shorthand: false
|
|
2111
|
+
type: "CallExpression",
|
|
2112
|
+
callee: {
|
|
2113
|
+
type: "MemberExpression",
|
|
2114
|
+
computed: true,
|
|
2115
|
+
object: { type: "Identifier", name: decoyIdent },
|
|
2116
|
+
property: {
|
|
2117
|
+
type: "Literal",
|
|
2118
|
+
value: model,
|
|
2119
|
+
raw: String(model)
|
|
1341
2120
|
}
|
|
1342
|
-
|
|
2121
|
+
},
|
|
2122
|
+
arguments: []
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
]
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
function createSharedDecoyDeclaration(decoyIdent = "_d") {
|
|
2129
|
+
return {
|
|
2130
|
+
type: "VariableDeclaration",
|
|
2131
|
+
kind: "var",
|
|
2132
|
+
declarations: [
|
|
2133
|
+
{
|
|
2134
|
+
type: "VariableDeclarator",
|
|
2135
|
+
id: { type: "Identifier", name: decoyIdent },
|
|
2136
|
+
init: {
|
|
2137
|
+
type: "ArrayExpression",
|
|
2138
|
+
elements: [0, 1, 2].map((m) => ({
|
|
2139
|
+
type: "ArrowFunctionExpression",
|
|
2140
|
+
id: null,
|
|
2141
|
+
params: [],
|
|
2142
|
+
body: createDecoyObjectExpression(m),
|
|
2143
|
+
generator: false,
|
|
2144
|
+
async: false,
|
|
2145
|
+
expression: true
|
|
2146
|
+
}))
|
|
1343
2147
|
}
|
|
1344
2148
|
}
|
|
1345
2149
|
]
|
|
1346
2150
|
};
|
|
1347
2151
|
}
|
|
1348
|
-
function
|
|
1349
|
-
|
|
1350
|
-
|
|
2152
|
+
function cloneNode(node) {
|
|
2153
|
+
return JSON.parse(JSON.stringify(node));
|
|
2154
|
+
}
|
|
2155
|
+
function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArrowFn = false) {
|
|
2156
|
+
const family = Math.abs(seed + fnIndex & 3);
|
|
2157
|
+
const c1 = (seed >>> 0) + Math.imul(family + 1, 73244475) | 0;
|
|
2158
|
+
const c2 = c1 ^ 1515870810 | 0;
|
|
2159
|
+
const target = (seed ^ Math.imul(family + 1, 2654435761)) & 8388607 | 1;
|
|
2160
|
+
let paramName = null;
|
|
2161
|
+
if (typeof paramNode === "string") {
|
|
2162
|
+
paramName = paramNode;
|
|
2163
|
+
} else if (paramNode?.type === "Identifier") {
|
|
2164
|
+
paramName = paramNode.name;
|
|
2165
|
+
} else if (paramNode?.type === "AssignmentPattern" && paramNode.left?.type === "Identifier") {
|
|
2166
|
+
paramName = paramNode.left.name;
|
|
2167
|
+
}
|
|
2168
|
+
const zNode = {
|
|
2169
|
+
type: "UnaryExpression",
|
|
2170
|
+
operator: "+",
|
|
2171
|
+
prefix: true,
|
|
2172
|
+
argument: {
|
|
2173
|
+
type: "UnaryExpression",
|
|
2174
|
+
operator: "!",
|
|
2175
|
+
prefix: true,
|
|
2176
|
+
argument: { type: "Identifier", name: "String" }
|
|
2177
|
+
}
|
|
2178
|
+
};
|
|
2179
|
+
const vNode = {
|
|
2180
|
+
type: "BinaryExpression",
|
|
2181
|
+
operator: "|",
|
|
2182
|
+
left: zNode,
|
|
2183
|
+
right: { type: "Literal", value: c1, raw: String(c1) }
|
|
2184
|
+
};
|
|
2185
|
+
const kNode = {
|
|
2186
|
+
type: "BinaryExpression",
|
|
2187
|
+
operator: "|",
|
|
2188
|
+
left: { type: "Literal", value: c2, raw: String(c2) },
|
|
2189
|
+
right: {
|
|
2190
|
+
type: "UnaryExpression",
|
|
2191
|
+
operator: "!",
|
|
2192
|
+
prefix: true,
|
|
2193
|
+
argument: { type: "Identifier", name: "String" }
|
|
2194
|
+
}
|
|
2195
|
+
};
|
|
2196
|
+
let mbaExpr;
|
|
2197
|
+
if (family === 0) {
|
|
2198
|
+
const vXorK = {
|
|
2199
|
+
type: "BinaryExpression",
|
|
2200
|
+
operator: "^",
|
|
2201
|
+
left: cloneNode(vNode),
|
|
2202
|
+
right: cloneNode(kNode)
|
|
2203
|
+
};
|
|
2204
|
+
const vAndK = {
|
|
2205
|
+
type: "BinaryExpression",
|
|
2206
|
+
operator: "&",
|
|
2207
|
+
left: cloneNode(vNode),
|
|
2208
|
+
right: cloneNode(kNode)
|
|
2209
|
+
};
|
|
2210
|
+
const shift = {
|
|
2211
|
+
type: "BinaryExpression",
|
|
2212
|
+
operator: "<<",
|
|
2213
|
+
left: vAndK,
|
|
2214
|
+
right: { type: "Literal", value: 1, raw: "1" }
|
|
2215
|
+
};
|
|
2216
|
+
const addLeft = {
|
|
2217
|
+
type: "BinaryExpression",
|
|
2218
|
+
operator: "+",
|
|
2219
|
+
left: vXorK,
|
|
2220
|
+
right: shift
|
|
2221
|
+
};
|
|
2222
|
+
const vPlusK = {
|
|
2223
|
+
type: "BinaryExpression",
|
|
2224
|
+
operator: "+",
|
|
2225
|
+
left: cloneNode(vNode),
|
|
2226
|
+
right: cloneNode(kNode)
|
|
2227
|
+
};
|
|
2228
|
+
mbaExpr = {
|
|
2229
|
+
type: "BinaryExpression",
|
|
2230
|
+
operator: "-",
|
|
2231
|
+
left: addLeft,
|
|
2232
|
+
right: vPlusK
|
|
2233
|
+
};
|
|
2234
|
+
} else if (family === 1) {
|
|
2235
|
+
const vOrK = {
|
|
2236
|
+
type: "BinaryExpression",
|
|
2237
|
+
operator: "|",
|
|
2238
|
+
left: cloneNode(vNode),
|
|
2239
|
+
right: cloneNode(kNode)
|
|
2240
|
+
};
|
|
2241
|
+
const vAndK = {
|
|
2242
|
+
type: "BinaryExpression",
|
|
2243
|
+
operator: "&",
|
|
2244
|
+
left: cloneNode(vNode),
|
|
2245
|
+
right: cloneNode(kNode)
|
|
2246
|
+
};
|
|
2247
|
+
const addLeft = {
|
|
2248
|
+
type: "BinaryExpression",
|
|
2249
|
+
operator: "+",
|
|
2250
|
+
left: vOrK,
|
|
2251
|
+
right: vAndK
|
|
2252
|
+
};
|
|
2253
|
+
const vPlusK = {
|
|
2254
|
+
type: "BinaryExpression",
|
|
2255
|
+
operator: "+",
|
|
2256
|
+
left: cloneNode(vNode),
|
|
2257
|
+
right: cloneNode(kNode)
|
|
2258
|
+
};
|
|
2259
|
+
mbaExpr = {
|
|
2260
|
+
type: "BinaryExpression",
|
|
2261
|
+
operator: "^",
|
|
2262
|
+
left: addLeft,
|
|
2263
|
+
right: vPlusK
|
|
2264
|
+
};
|
|
2265
|
+
} else if (family === 2) {
|
|
2266
|
+
const notK = {
|
|
2267
|
+
type: "UnaryExpression",
|
|
2268
|
+
operator: "~",
|
|
2269
|
+
prefix: true,
|
|
2270
|
+
argument: cloneNode(kNode)
|
|
2271
|
+
};
|
|
2272
|
+
const vOrNotK = {
|
|
2273
|
+
type: "BinaryExpression",
|
|
2274
|
+
operator: "|",
|
|
2275
|
+
left: cloneNode(vNode),
|
|
2276
|
+
right: cloneNode(notK)
|
|
2277
|
+
};
|
|
2278
|
+
const subLeft = {
|
|
2279
|
+
type: "BinaryExpression",
|
|
2280
|
+
operator: "-",
|
|
2281
|
+
left: vOrNotK,
|
|
2282
|
+
right: cloneNode(notK)
|
|
2283
|
+
};
|
|
2284
|
+
const vAndK = {
|
|
2285
|
+
type: "BinaryExpression",
|
|
2286
|
+
operator: "&",
|
|
2287
|
+
left: cloneNode(vNode),
|
|
2288
|
+
right: cloneNode(kNode)
|
|
2289
|
+
};
|
|
2290
|
+
mbaExpr = {
|
|
2291
|
+
type: "BinaryExpression",
|
|
2292
|
+
operator: "^",
|
|
2293
|
+
left: subLeft,
|
|
2294
|
+
right: vAndK
|
|
2295
|
+
};
|
|
2296
|
+
} else {
|
|
2297
|
+
const notK = {
|
|
2298
|
+
type: "UnaryExpression",
|
|
2299
|
+
operator: "~",
|
|
2300
|
+
prefix: true,
|
|
2301
|
+
argument: cloneNode(kNode)
|
|
2302
|
+
};
|
|
2303
|
+
const vAndNotK = {
|
|
2304
|
+
type: "BinaryExpression",
|
|
2305
|
+
operator: "&",
|
|
2306
|
+
left: cloneNode(vNode),
|
|
2307
|
+
right: cloneNode(notK)
|
|
2308
|
+
};
|
|
2309
|
+
const vAndK = {
|
|
2310
|
+
type: "BinaryExpression",
|
|
2311
|
+
operator: "&",
|
|
2312
|
+
left: cloneNode(vNode),
|
|
2313
|
+
right: cloneNode(kNode)
|
|
2314
|
+
};
|
|
2315
|
+
const addLeft = {
|
|
2316
|
+
type: "BinaryExpression",
|
|
2317
|
+
operator: "+",
|
|
2318
|
+
left: vAndNotK,
|
|
2319
|
+
right: vAndK
|
|
2320
|
+
};
|
|
2321
|
+
mbaExpr = {
|
|
2322
|
+
type: "BinaryExpression",
|
|
2323
|
+
operator: "^",
|
|
2324
|
+
left: addLeft,
|
|
2325
|
+
right: cloneNode(vNode)
|
|
2326
|
+
};
|
|
2327
|
+
}
|
|
2328
|
+
const decoyIdx = Math.abs(fnIndex) % 3;
|
|
2329
|
+
const consequent = decoyIdent ? createSharedDecoyConsequent(decoyIdent, decoyIdx) : createDecoyConsequent(decoyIdx);
|
|
1351
2330
|
return {
|
|
1352
2331
|
type: "IfStatement",
|
|
1353
2332
|
test: {
|
|
1354
2333
|
type: "BinaryExpression",
|
|
1355
2334
|
operator: "===",
|
|
1356
|
-
left:
|
|
1357
|
-
|
|
1358
|
-
operator: "|",
|
|
1359
|
-
left: {
|
|
1360
|
-
type: "BinaryExpression",
|
|
1361
|
-
operator: "^",
|
|
1362
|
-
left: {
|
|
1363
|
-
type: "BinaryExpression",
|
|
1364
|
-
operator: "+",
|
|
1365
|
-
left: {
|
|
1366
|
-
type: "BinaryExpression",
|
|
1367
|
-
operator: "|",
|
|
1368
|
-
left: { type: "Literal", value: x, raw: String(x) },
|
|
1369
|
-
right: { type: "Literal", value: y, raw: String(y) }
|
|
1370
|
-
},
|
|
1371
|
-
right: {
|
|
1372
|
-
type: "BinaryExpression",
|
|
1373
|
-
operator: "&",
|
|
1374
|
-
left: { type: "Literal", value: x, raw: String(x) },
|
|
1375
|
-
right: { type: "Literal", value: y, raw: String(y) }
|
|
1376
|
-
}
|
|
1377
|
-
},
|
|
1378
|
-
right: {
|
|
1379
|
-
type: "BinaryExpression",
|
|
1380
|
-
operator: "+",
|
|
1381
|
-
left: { type: "Literal", value: x, raw: String(x) },
|
|
1382
|
-
right: { type: "Literal", value: y, raw: String(y) }
|
|
1383
|
-
}
|
|
1384
|
-
},
|
|
1385
|
-
right: {
|
|
1386
|
-
type: "UnaryExpression",
|
|
1387
|
-
operator: "!",
|
|
1388
|
-
prefix: true,
|
|
1389
|
-
argument: { type: "Identifier", name: "String" }
|
|
1390
|
-
}
|
|
1391
|
-
},
|
|
1392
|
-
right: { type: "Literal", value: 2147483647, raw: "0x7fffffff" }
|
|
2335
|
+
left: mbaExpr,
|
|
2336
|
+
right: { type: "Literal", value: target, raw: String(target) }
|
|
1393
2337
|
},
|
|
1394
|
-
consequent
|
|
2338
|
+
consequent,
|
|
1395
2339
|
alternate: null
|
|
1396
2340
|
};
|
|
1397
2341
|
}
|
|
1398
2342
|
function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
|
|
2343
|
+
const decoyIdent = "_d";
|
|
2344
|
+
if (Array.isArray(ast.body)) {
|
|
2345
|
+
const alreadyGuarded = ast.body.some(
|
|
2346
|
+
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
2347
|
+
(d) => d.id?.type === "Identifier" && d.id.name === decoyIdent
|
|
2348
|
+
)
|
|
2349
|
+
);
|
|
2350
|
+
if (alreadyGuarded) return 0;
|
|
2351
|
+
}
|
|
1399
2352
|
let guardedCount = 0;
|
|
1400
2353
|
function maybeInjectGuard(node) {
|
|
1401
2354
|
if (!coldFunctionNodes.has(node)) return;
|
|
1402
2355
|
if (!node.body || node.body.type !== "BlockStatement") return;
|
|
1403
|
-
const
|
|
2356
|
+
const firstParam = node.params?.[0];
|
|
2357
|
+
const isArrow = node.type === "ArrowFunctionExpression";
|
|
2358
|
+
const guardStmt = createSotaMbaPredicate(
|
|
2359
|
+
seed,
|
|
2360
|
+
guardedCount,
|
|
2361
|
+
firstParam,
|
|
2362
|
+
decoyIdent,
|
|
2363
|
+
isArrow
|
|
2364
|
+
);
|
|
1404
2365
|
guardedCount++;
|
|
1405
2366
|
if (hotExemptNodes) {
|
|
1406
2367
|
hotExemptNodes.add(guardStmt);
|
|
@@ -1417,64 +2378,946 @@ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExempt
|
|
|
1417
2378
|
FunctionExpression: maybeInjectGuard,
|
|
1418
2379
|
ArrowFunctionExpression: maybeInjectGuard
|
|
1419
2380
|
});
|
|
2381
|
+
if (guardedCount > 0 && Array.isArray(ast.body)) {
|
|
2382
|
+
const sharedDecoyDecl = createSharedDecoyDeclaration(decoyIdent);
|
|
2383
|
+
if (hotExemptNodes) {
|
|
2384
|
+
hotExemptNodes.add(sharedDecoyDecl);
|
|
2385
|
+
for (const decl of sharedDecoyDecl.declarations || []) {
|
|
2386
|
+
if (decl.init?.elements) {
|
|
2387
|
+
for (const el of decl.init.elements) {
|
|
2388
|
+
hotExemptNodes.add(el);
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
let insertIdx = 0;
|
|
2394
|
+
while (insertIdx < ast.body.length && ast.body[insertIdx].type === "ExpressionStatement" && ast.body[insertIdx].directive) {
|
|
2395
|
+
insertIdx++;
|
|
2396
|
+
}
|
|
2397
|
+
ast.body.splice(insertIdx, 0, sharedDecoyDecl);
|
|
2398
|
+
}
|
|
1420
2399
|
return guardedCount;
|
|
1421
2400
|
}
|
|
1422
2401
|
|
|
1423
|
-
// src/
|
|
1424
|
-
var
|
|
1425
|
-
var
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
2402
|
+
// src/ast/micro-vm.ts
|
|
2403
|
+
var acorn3 = __toESM(require("acorn"), 1);
|
|
2404
|
+
var walk5 = __toESM(require("acorn-walk"), 1);
|
|
2405
|
+
var OP_LDC = 0;
|
|
2406
|
+
var OP_LDV = 1;
|
|
2407
|
+
var OP_STV = 2;
|
|
2408
|
+
var OP_LDG = 3;
|
|
2409
|
+
var OP_GETP = 4;
|
|
2410
|
+
var OP_SETP = 5;
|
|
2411
|
+
var OP_CALL = 6;
|
|
2412
|
+
var OP_MCALL = 7;
|
|
2413
|
+
var OP_ADD = 8;
|
|
2414
|
+
var OP_SUB = 9;
|
|
2415
|
+
var OP_MUL = 10;
|
|
2416
|
+
var OP_XOR = 11;
|
|
2417
|
+
var OP_AND = 12;
|
|
2418
|
+
var OP_OR = 13;
|
|
2419
|
+
var OP_SEQ = 14;
|
|
2420
|
+
var OP_LT = 15;
|
|
2421
|
+
var OP_NOT = 16;
|
|
2422
|
+
var OP_JMP = 17;
|
|
2423
|
+
var OP_JZ = 18;
|
|
2424
|
+
var OP_RET = 19;
|
|
2425
|
+
var OP_THROW = 20;
|
|
2426
|
+
var OP_AWAIT = 21;
|
|
2427
|
+
var OP_OBJ = 22;
|
|
2428
|
+
var OP_ARR = 23;
|
|
2429
|
+
function generateOpcodeMap(seed) {
|
|
2430
|
+
const pool = [];
|
|
2431
|
+
for (let i = 0; i < 256; i++) pool.push(i);
|
|
2432
|
+
let s = (seed ^ 2779096485) >>> 0;
|
|
2433
|
+
function rand() {
|
|
2434
|
+
s ^= s << 13;
|
|
2435
|
+
s ^= s >>> 17;
|
|
2436
|
+
s ^= s << 5;
|
|
2437
|
+
return (s >>> 0) / 4294967296;
|
|
1431
2438
|
}
|
|
1432
|
-
|
|
2439
|
+
for (let i = pool.length - 1; i > 0; i--) {
|
|
2440
|
+
const j = Math.floor(rand() * (i + 1));
|
|
2441
|
+
const tmp = pool[i];
|
|
2442
|
+
pool[i] = pool[j];
|
|
2443
|
+
pool[j] = tmp;
|
|
2444
|
+
}
|
|
2445
|
+
return pool.slice(0, 24);
|
|
1433
2446
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
2447
|
+
function createInterpreterSource(vmHelperName, opMap) {
|
|
2448
|
+
return `function ${vmHelperName}(c, k, s, t) {
|
|
2449
|
+
var p = 0, r = 0, q = [];
|
|
2450
|
+
var G = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : this);
|
|
2451
|
+
function x() {
|
|
2452
|
+
while (p < c.length) {
|
|
2453
|
+
var o = c[p++];
|
|
2454
|
+
switch (o) {
|
|
2455
|
+
case ${opMap[OP_LDC]}: q[r++] = k[c[p++]]; break;
|
|
2456
|
+
case ${opMap[OP_LDV]}: q[r++] = s[c[p++]]; break;
|
|
2457
|
+
case ${opMap[OP_STV]}: s[c[p++]] = q[--r]; break;
|
|
2458
|
+
case ${opMap[OP_LDG]}: q[r++] = G[k[c[p++]]]; break;
|
|
2459
|
+
case ${opMap[OP_GETP]}: { var b = q[--r], a = q[--r]; q[r++] = a[b]; break; }
|
|
2460
|
+
case ${opMap[OP_SETP]}: { var v = q[--r], b = q[--r], a = q[--r]; q[r++] = (a[b] = v); break; }
|
|
2461
|
+
case ${opMap[OP_CALL]}: {
|
|
2462
|
+
var n = c[p++], g = [];
|
|
2463
|
+
for (var i = n - 1; i >= 0; i--) g[i] = q[--r];
|
|
2464
|
+
var f = q[--r]; q[r++] = f.apply(null, g); break;
|
|
2465
|
+
}
|
|
2466
|
+
case ${opMap[OP_MCALL]}: {
|
|
2467
|
+
var n = c[p++], g = [];
|
|
2468
|
+
for (var i = n - 1; i >= 0; i--) g[i] = q[--r];
|
|
2469
|
+
var b = q[--r], a = q[--r]; q[r++] = a[b].apply(a, g); break;
|
|
2470
|
+
}
|
|
2471
|
+
case ${opMap[OP_ADD]}: { var b = q[--r], a = q[--r]; q[r++] = a + b; break; }
|
|
2472
|
+
case ${opMap[OP_SUB]}: { var b = q[--r], a = q[--r]; q[r++] = a - b; break; }
|
|
2473
|
+
case ${opMap[OP_MUL]}: { var b = q[--r], a = q[--r]; q[r++] = a * b; break; }
|
|
2474
|
+
case ${opMap[OP_XOR]}: { var b = q[--r], a = q[--r]; q[r++] = a ^ b; break; }
|
|
2475
|
+
case ${opMap[OP_AND]}: { var b = q[--r], a = q[--r]; q[r++] = a & b; break; }
|
|
2476
|
+
case ${opMap[OP_OR]}: { var b = q[--r], a = q[--r]; q[r++] = a | b; break; }
|
|
2477
|
+
case ${opMap[OP_SEQ]}: { var b = q[--r], a = q[--r]; q[r++] = a === b; break; }
|
|
2478
|
+
case ${opMap[OP_LT]}: { var b = q[--r], a = q[--r]; q[r++] = a < b; break; }
|
|
2479
|
+
case ${opMap[OP_NOT]}: { q[r - 1] = !q[r - 1]; break; }
|
|
2480
|
+
case ${opMap[OP_JMP]}: { p = c[p]; break; }
|
|
2481
|
+
case ${opMap[OP_JZ]}: { var j = c[p++]; if (!q[--r]) p = j; break; }
|
|
2482
|
+
case ${opMap[OP_RET]}: return q[--r];
|
|
2483
|
+
case ${opMap[OP_THROW]}: throw q[--r];
|
|
2484
|
+
case ${opMap[OP_AWAIT]}: {
|
|
2485
|
+
var v = q[--r];
|
|
2486
|
+
return Promise.resolve(v).then(function(w) { q[r++] = w; return x(); });
|
|
2487
|
+
}
|
|
2488
|
+
case ${opMap[OP_OBJ]}: {
|
|
2489
|
+
var n = c[p++], o = {}, e = [];
|
|
2490
|
+
for (var i = 0; i < n; i++) { var v = q[--r], k0 = q[--r]; e.push([k0, v]); }
|
|
2491
|
+
for (var i = e.length - 1; i >= 0; i--) { o[e[i][0]] = e[i][1]; }
|
|
2492
|
+
q[r++] = o;
|
|
2493
|
+
break;
|
|
2494
|
+
}
|
|
2495
|
+
case ${opMap[OP_ARR]}: {
|
|
2496
|
+
var n = c[p++], a = [];
|
|
2497
|
+
for (var i = n - 1; i >= 0; i--) a[i] = q[--r];
|
|
2498
|
+
q[r++] = a;
|
|
2499
|
+
break;
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
return x();
|
|
2505
|
+
}`;
|
|
1439
2506
|
}
|
|
1440
|
-
function
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
return
|
|
2507
|
+
function countAstNodes(node) {
|
|
2508
|
+
let count = 0;
|
|
2509
|
+
walk5.full(node, () => {
|
|
2510
|
+
count++;
|
|
2511
|
+
});
|
|
2512
|
+
return count;
|
|
2513
|
+
}
|
|
2514
|
+
function hasForbiddenConstructs(fnNode) {
|
|
2515
|
+
let forbidden = false;
|
|
2516
|
+
walk5.full(fnNode, (node) => {
|
|
2517
|
+
if (forbidden) return;
|
|
2518
|
+
const type = node.type;
|
|
2519
|
+
if (type === "JSXElement" || type === "JSXFragment" || type === "Super" || type === "YieldExpression") {
|
|
2520
|
+
forbidden = true;
|
|
2521
|
+
return;
|
|
2522
|
+
}
|
|
2523
|
+
if (type === "Identifier" && node.name === "arguments") {
|
|
2524
|
+
forbidden = true;
|
|
2525
|
+
return;
|
|
2526
|
+
}
|
|
2527
|
+
if (type === "MetaProperty" && node.meta?.name === "new" && node.property?.name === "target") {
|
|
2528
|
+
forbidden = true;
|
|
2529
|
+
return;
|
|
2530
|
+
}
|
|
2531
|
+
});
|
|
2532
|
+
return forbidden;
|
|
2533
|
+
}
|
|
2534
|
+
function compileFunctionToBytecode(fnNode, opMap) {
|
|
2535
|
+
const bytecode = [];
|
|
2536
|
+
const consts = [];
|
|
2537
|
+
const localMap = /* @__PURE__ */ new Map();
|
|
2538
|
+
let localCount = 0;
|
|
2539
|
+
for (const param of fnNode.params) {
|
|
2540
|
+
if (param.type !== "Identifier") return null;
|
|
2541
|
+
localMap.set(param.name, localCount++);
|
|
2542
|
+
}
|
|
2543
|
+
const bodyNodes = fnNode.body.type === "BlockStatement" ? fnNode.body.body : [fnNode.body];
|
|
2544
|
+
for (const stmt of bodyNodes) {
|
|
2545
|
+
if (stmt.type === "VariableDeclaration") {
|
|
2546
|
+
for (const decl of stmt.declarations) {
|
|
2547
|
+
if (decl.id.type === "Identifier") {
|
|
2548
|
+
if (!localMap.has(decl.id.name)) {
|
|
2549
|
+
localMap.set(decl.id.name, localCount++);
|
|
2550
|
+
}
|
|
2551
|
+
} else {
|
|
2552
|
+
return null;
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
function addConst(val) {
|
|
2558
|
+
for (let i = 0; i < consts.length; i++) {
|
|
2559
|
+
if (consts[i] === val) return i;
|
|
2560
|
+
}
|
|
2561
|
+
const idx = consts.length;
|
|
2562
|
+
consts.push(val);
|
|
2563
|
+
return idx;
|
|
2564
|
+
}
|
|
2565
|
+
function emit(op, ...args) {
|
|
2566
|
+
bytecode.push(opMap[op]);
|
|
2567
|
+
for (const a of args) bytecode.push(a);
|
|
2568
|
+
}
|
|
2569
|
+
function compileExpr(node) {
|
|
2570
|
+
if (!node) return false;
|
|
2571
|
+
switch (node.type) {
|
|
2572
|
+
case "Literal": {
|
|
2573
|
+
const idx = addConst(node.value);
|
|
2574
|
+
emit(OP_LDC, idx);
|
|
2575
|
+
return true;
|
|
2576
|
+
}
|
|
2577
|
+
case "Identifier": {
|
|
2578
|
+
if (node.name === "undefined") {
|
|
2579
|
+
const idx = addConst(void 0);
|
|
2580
|
+
emit(OP_LDC, idx);
|
|
2581
|
+
} else if (localMap.has(node.name)) {
|
|
2582
|
+
emit(OP_LDV, localMap.get(node.name));
|
|
2583
|
+
} else {
|
|
2584
|
+
const idx = addConst(node.name);
|
|
2585
|
+
emit(OP_LDG, idx);
|
|
2586
|
+
}
|
|
2587
|
+
return true;
|
|
2588
|
+
}
|
|
2589
|
+
case "BinaryExpression": {
|
|
2590
|
+
if (node.operator === ">") {
|
|
2591
|
+
if (!compileExpr(node.right)) return false;
|
|
2592
|
+
if (!compileExpr(node.left)) return false;
|
|
2593
|
+
emit(OP_LT);
|
|
2594
|
+
return true;
|
|
2595
|
+
}
|
|
2596
|
+
if (node.operator === "<=") {
|
|
2597
|
+
if (!compileExpr(node.right)) return false;
|
|
2598
|
+
if (!compileExpr(node.left)) return false;
|
|
2599
|
+
emit(OP_LT);
|
|
2600
|
+
emit(OP_NOT);
|
|
2601
|
+
return true;
|
|
2602
|
+
}
|
|
2603
|
+
if (node.operator === ">=") {
|
|
2604
|
+
if (!compileExpr(node.left)) return false;
|
|
2605
|
+
if (!compileExpr(node.right)) return false;
|
|
2606
|
+
emit(OP_LT);
|
|
2607
|
+
emit(OP_NOT);
|
|
2608
|
+
return true;
|
|
2609
|
+
}
|
|
2610
|
+
if (!compileExpr(node.left)) return false;
|
|
2611
|
+
if (!compileExpr(node.right)) return false;
|
|
2612
|
+
switch (node.operator) {
|
|
2613
|
+
case "+":
|
|
2614
|
+
emit(OP_ADD);
|
|
2615
|
+
break;
|
|
2616
|
+
case "-":
|
|
2617
|
+
emit(OP_SUB);
|
|
2618
|
+
break;
|
|
2619
|
+
case "*":
|
|
2620
|
+
emit(OP_MUL);
|
|
2621
|
+
break;
|
|
2622
|
+
case "^":
|
|
2623
|
+
emit(OP_XOR);
|
|
2624
|
+
break;
|
|
2625
|
+
case "&":
|
|
2626
|
+
emit(OP_AND);
|
|
2627
|
+
break;
|
|
2628
|
+
case "|":
|
|
2629
|
+
emit(OP_OR);
|
|
2630
|
+
break;
|
|
2631
|
+
case "==":
|
|
2632
|
+
case "===":
|
|
2633
|
+
emit(OP_SEQ);
|
|
2634
|
+
break;
|
|
2635
|
+
case "!=":
|
|
2636
|
+
case "!==":
|
|
2637
|
+
emit(OP_SEQ);
|
|
2638
|
+
emit(OP_NOT);
|
|
2639
|
+
break;
|
|
2640
|
+
case "<":
|
|
2641
|
+
emit(OP_LT);
|
|
2642
|
+
break;
|
|
2643
|
+
default:
|
|
2644
|
+
return false;
|
|
2645
|
+
}
|
|
2646
|
+
return true;
|
|
2647
|
+
}
|
|
2648
|
+
case "UnaryExpression": {
|
|
2649
|
+
if (!compileExpr(node.argument)) return false;
|
|
2650
|
+
switch (node.operator) {
|
|
2651
|
+
case "!":
|
|
2652
|
+
emit(OP_NOT);
|
|
2653
|
+
break;
|
|
2654
|
+
case "-": {
|
|
2655
|
+
const neg1Idx = addConst(-1);
|
|
2656
|
+
emit(OP_LDC, neg1Idx);
|
|
2657
|
+
emit(OP_MUL);
|
|
2658
|
+
break;
|
|
2659
|
+
}
|
|
2660
|
+
case "+": {
|
|
2661
|
+
const zeroIdx = addConst(0);
|
|
2662
|
+
emit(OP_LDC, zeroIdx);
|
|
2663
|
+
emit(OP_ADD);
|
|
2664
|
+
break;
|
|
2665
|
+
}
|
|
2666
|
+
case "~": {
|
|
2667
|
+
const neg1Idx = addConst(-1);
|
|
2668
|
+
emit(OP_LDC, neg1Idx);
|
|
2669
|
+
emit(OP_XOR);
|
|
2670
|
+
break;
|
|
2671
|
+
}
|
|
2672
|
+
case "void": {
|
|
2673
|
+
const undefIdx2 = addConst(void 0);
|
|
2674
|
+
emit(OP_LDC, undefIdx2);
|
|
2675
|
+
break;
|
|
2676
|
+
}
|
|
2677
|
+
default:
|
|
2678
|
+
return false;
|
|
2679
|
+
}
|
|
2680
|
+
return true;
|
|
2681
|
+
}
|
|
2682
|
+
case "MemberExpression": {
|
|
2683
|
+
if (!compileExpr(node.object)) return false;
|
|
2684
|
+
if (node.computed) {
|
|
2685
|
+
if (!compileExpr(node.property)) return false;
|
|
2686
|
+
} else {
|
|
2687
|
+
const idx = addConst(node.property.name);
|
|
2688
|
+
emit(OP_LDC, idx);
|
|
2689
|
+
}
|
|
2690
|
+
emit(OP_GETP);
|
|
2691
|
+
return true;
|
|
2692
|
+
}
|
|
2693
|
+
case "CallExpression": {
|
|
2694
|
+
if (node.callee.type === "MemberExpression") {
|
|
2695
|
+
if (!compileExpr(node.callee.object)) return false;
|
|
2696
|
+
if (node.callee.computed) {
|
|
2697
|
+
if (!compileExpr(node.callee.property)) return false;
|
|
2698
|
+
} else {
|
|
2699
|
+
const idx = addConst(node.callee.property.name);
|
|
2700
|
+
emit(OP_LDC, idx);
|
|
2701
|
+
}
|
|
2702
|
+
for (const arg of node.arguments) {
|
|
2703
|
+
if (!compileExpr(arg)) return false;
|
|
2704
|
+
}
|
|
2705
|
+
emit(OP_MCALL, node.arguments.length);
|
|
2706
|
+
} else {
|
|
2707
|
+
if (!compileExpr(node.callee)) return false;
|
|
2708
|
+
for (const arg of node.arguments) {
|
|
2709
|
+
if (!compileExpr(arg)) return false;
|
|
2710
|
+
}
|
|
2711
|
+
emit(OP_CALL, node.arguments.length);
|
|
2712
|
+
}
|
|
2713
|
+
return true;
|
|
2714
|
+
}
|
|
2715
|
+
case "AssignmentExpression": {
|
|
2716
|
+
if (node.operator !== "=") return false;
|
|
2717
|
+
if (node.left.type === "Identifier") {
|
|
2718
|
+
if (!localMap.has(node.left.name)) return false;
|
|
2719
|
+
if (!compileExpr(node.right)) return false;
|
|
2720
|
+
emit(OP_STV, localMap.get(node.left.name));
|
|
2721
|
+
return true;
|
|
2722
|
+
}
|
|
2723
|
+
if (node.left.type === "MemberExpression") {
|
|
2724
|
+
if (!compileExpr(node.left.object)) return false;
|
|
2725
|
+
if (node.left.computed) {
|
|
2726
|
+
if (!compileExpr(node.left.property)) return false;
|
|
2727
|
+
} else {
|
|
2728
|
+
const idx = addConst(node.left.property.name);
|
|
2729
|
+
emit(OP_LDC, idx);
|
|
2730
|
+
}
|
|
2731
|
+
if (!compileExpr(node.right)) return false;
|
|
2732
|
+
emit(OP_SETP);
|
|
2733
|
+
return true;
|
|
2734
|
+
}
|
|
2735
|
+
return false;
|
|
2736
|
+
}
|
|
2737
|
+
case "ObjectExpression": {
|
|
2738
|
+
for (const prop of node.properties) {
|
|
2739
|
+
if (prop.type !== "Property") return false;
|
|
2740
|
+
if (prop.computed) {
|
|
2741
|
+
if (!compileExpr(prop.key)) return false;
|
|
2742
|
+
} else {
|
|
2743
|
+
const keyName = prop.key.name ?? prop.key.value;
|
|
2744
|
+
const idx = addConst(keyName);
|
|
2745
|
+
emit(OP_LDC, idx);
|
|
2746
|
+
}
|
|
2747
|
+
if (!compileExpr(prop.value)) return false;
|
|
2748
|
+
}
|
|
2749
|
+
emit(OP_OBJ, node.properties.length);
|
|
2750
|
+
return true;
|
|
2751
|
+
}
|
|
2752
|
+
case "ArrayExpression": {
|
|
2753
|
+
for (const elem of node.elements) {
|
|
2754
|
+
if (!compileExpr(elem)) return false;
|
|
2755
|
+
}
|
|
2756
|
+
emit(OP_ARR, node.elements.length);
|
|
2757
|
+
return true;
|
|
2758
|
+
}
|
|
2759
|
+
case "AwaitExpression": {
|
|
2760
|
+
if (!compileExpr(node.argument)) return false;
|
|
2761
|
+
emit(OP_AWAIT);
|
|
2762
|
+
return true;
|
|
2763
|
+
}
|
|
2764
|
+
default:
|
|
2765
|
+
return false;
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
function compileStmt(stmt) {
|
|
2769
|
+
if (!stmt) return false;
|
|
2770
|
+
switch (stmt.type) {
|
|
2771
|
+
case "ReturnStatement": {
|
|
2772
|
+
if (stmt.argument) {
|
|
2773
|
+
if (!compileExpr(stmt.argument)) return false;
|
|
2774
|
+
} else {
|
|
2775
|
+
const idx = addConst(void 0);
|
|
2776
|
+
emit(OP_LDC, idx);
|
|
2777
|
+
}
|
|
2778
|
+
emit(OP_RET);
|
|
2779
|
+
return true;
|
|
2780
|
+
}
|
|
2781
|
+
case "ExpressionStatement": {
|
|
2782
|
+
if (!compileExpr(stmt.expression)) return false;
|
|
2783
|
+
return true;
|
|
2784
|
+
}
|
|
2785
|
+
case "VariableDeclaration": {
|
|
2786
|
+
for (const decl of stmt.declarations) {
|
|
2787
|
+
if (decl.init) {
|
|
2788
|
+
if (!compileExpr(decl.init)) return false;
|
|
2789
|
+
emit(OP_STV, localMap.get(decl.id.name));
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
return true;
|
|
2793
|
+
}
|
|
2794
|
+
case "IfStatement": {
|
|
2795
|
+
if (!compileExpr(stmt.test)) return false;
|
|
2796
|
+
const jzPos = bytecode.length;
|
|
2797
|
+
emit(OP_JZ, 0);
|
|
2798
|
+
if (!compileStmt(stmt.consequent)) return false;
|
|
2799
|
+
if (stmt.alternate) {
|
|
2800
|
+
const jmpPos = bytecode.length;
|
|
2801
|
+
emit(OP_JMP, 0);
|
|
2802
|
+
bytecode[jzPos + 1] = bytecode.length;
|
|
2803
|
+
if (!compileStmt(stmt.alternate)) return false;
|
|
2804
|
+
bytecode[jmpPos + 1] = bytecode.length;
|
|
2805
|
+
} else {
|
|
2806
|
+
bytecode[jzPos + 1] = bytecode.length;
|
|
2807
|
+
}
|
|
2808
|
+
return true;
|
|
2809
|
+
}
|
|
2810
|
+
case "BlockStatement": {
|
|
2811
|
+
for (const s of stmt.body) {
|
|
2812
|
+
if (!compileStmt(s)) return false;
|
|
2813
|
+
}
|
|
2814
|
+
return true;
|
|
2815
|
+
}
|
|
2816
|
+
default:
|
|
2817
|
+
return false;
|
|
2818
|
+
}
|
|
2819
|
+
}
|
|
2820
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
2821
|
+
if (!compileStmt(fnNode.body)) return null;
|
|
2822
|
+
} else {
|
|
2823
|
+
if (!compileExpr(fnNode.body)) return null;
|
|
2824
|
+
emit(OP_RET);
|
|
2825
|
+
}
|
|
2826
|
+
const undefIdx = addConst(void 0);
|
|
2827
|
+
emit(OP_LDC, undefIdx);
|
|
2828
|
+
emit(OP_RET);
|
|
2829
|
+
return { bytecode, consts, localCount };
|
|
2830
|
+
}
|
|
2831
|
+
function isMicroVmAlreadyInjected(ast) {
|
|
2832
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
2833
|
+
return ast.body.some((stmt) => {
|
|
2834
|
+
if (stmt.type === "FunctionDeclaration" && typeof stmt.id?.name === "string" && stmt.id.name.startsWith("_uVm")) {
|
|
2835
|
+
return true;
|
|
2836
|
+
}
|
|
2837
|
+
return false;
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 2654435769) {
|
|
2841
|
+
const suffix = computeFnv1aHash(`micro-vm:${seed}`, seed).toString(36);
|
|
2842
|
+
const vmHelperName = `_uVm${suffix}`;
|
|
2843
|
+
if (isMicroVmAlreadyInjected(ast)) {
|
|
2844
|
+
const existing = ast.body.find(
|
|
2845
|
+
(stmt) => stmt.type === "FunctionDeclaration" && typeof stmt.id?.name === "string" && stmt.id.name.startsWith("_uVm")
|
|
2846
|
+
);
|
|
2847
|
+
return {
|
|
2848
|
+
virtualizedCount: 0,
|
|
2849
|
+
vmHelperName: existing?.id?.name ?? vmHelperName
|
|
2850
|
+
};
|
|
2851
|
+
}
|
|
2852
|
+
const opMap = generateOpcodeMap(seed);
|
|
2853
|
+
const candidates = [];
|
|
2854
|
+
walk5.simple(ast, {
|
|
2855
|
+
FunctionDeclaration(node) {
|
|
2856
|
+
candidates.push(node);
|
|
2857
|
+
},
|
|
2858
|
+
FunctionExpression(node) {
|
|
2859
|
+
candidates.push(node);
|
|
2860
|
+
},
|
|
2861
|
+
ArrowFunctionExpression(node) {
|
|
2862
|
+
candidates.push(node);
|
|
2863
|
+
}
|
|
2864
|
+
});
|
|
2865
|
+
let virtualizedCount = 0;
|
|
2866
|
+
for (const fn of candidates) {
|
|
2867
|
+
if (virtualizedCount >= 10) break;
|
|
2868
|
+
if (coldFunctionNodes && !coldFunctionNodes.has(fn)) {
|
|
2869
|
+
continue;
|
|
2870
|
+
}
|
|
2871
|
+
if (serializedFunctionNodes && serializedFunctionNodes.has(fn)) {
|
|
2872
|
+
continue;
|
|
2873
|
+
}
|
|
2874
|
+
if (fn.generator) {
|
|
2875
|
+
continue;
|
|
2876
|
+
}
|
|
2877
|
+
if (hasForbiddenConstructs(fn)) {
|
|
2878
|
+
continue;
|
|
2879
|
+
}
|
|
2880
|
+
const nodeCount = countAstNodes(fn);
|
|
2881
|
+
if (nodeCount < 15 || nodeCount > 80) {
|
|
2882
|
+
continue;
|
|
2883
|
+
}
|
|
2884
|
+
const compiled = compileFunctionToBytecode(fn, opMap);
|
|
2885
|
+
if (!compiled) {
|
|
2886
|
+
continue;
|
|
2887
|
+
}
|
|
2888
|
+
const paramArgs = fn.params.map((p) => ({
|
|
2889
|
+
type: "Identifier",
|
|
2890
|
+
name: p.name
|
|
2891
|
+
}));
|
|
2892
|
+
const callExpr = {
|
|
2893
|
+
type: "CallExpression",
|
|
2894
|
+
callee: {
|
|
2895
|
+
type: "Identifier",
|
|
2896
|
+
name: vmHelperName
|
|
2897
|
+
},
|
|
2898
|
+
arguments: [
|
|
2899
|
+
{
|
|
2900
|
+
type: "ArrayExpression",
|
|
2901
|
+
elements: compiled.bytecode.map((b) => ({
|
|
2902
|
+
type: "Literal",
|
|
2903
|
+
value: b,
|
|
2904
|
+
raw: String(b)
|
|
2905
|
+
}))
|
|
2906
|
+
},
|
|
2907
|
+
{
|
|
2908
|
+
type: "ArrayExpression",
|
|
2909
|
+
elements: compiled.consts.map((c) => ({
|
|
2910
|
+
type: "Literal",
|
|
2911
|
+
value: c,
|
|
2912
|
+
raw: c === void 0 ? "void 0" : JSON.stringify(c)
|
|
2913
|
+
}))
|
|
2914
|
+
},
|
|
2915
|
+
{
|
|
2916
|
+
type: "ArrayExpression",
|
|
2917
|
+
elements: paramArgs
|
|
2918
|
+
},
|
|
2919
|
+
{
|
|
2920
|
+
type: "ThisExpression"
|
|
2921
|
+
}
|
|
2922
|
+
],
|
|
2923
|
+
optional: false
|
|
2924
|
+
};
|
|
2925
|
+
if (fn.body.type === "BlockStatement") {
|
|
2926
|
+
fn.body.body = [
|
|
2927
|
+
{
|
|
2928
|
+
type: "ReturnStatement",
|
|
2929
|
+
argument: callExpr
|
|
2930
|
+
}
|
|
2931
|
+
];
|
|
2932
|
+
} else {
|
|
2933
|
+
fn.body = callExpr;
|
|
2934
|
+
}
|
|
2935
|
+
virtualizedCount++;
|
|
2936
|
+
}
|
|
2937
|
+
if (virtualizedCount > 0) {
|
|
2938
|
+
const interpreterSource = createInterpreterSource(vmHelperName, opMap);
|
|
2939
|
+
const parsedHelper = acorn3.parse(interpreterSource, {
|
|
2940
|
+
ecmaVersion: "latest",
|
|
2941
|
+
sourceType: "script"
|
|
2942
|
+
});
|
|
2943
|
+
ast.body.unshift(...parsedHelper.body);
|
|
2944
|
+
}
|
|
2945
|
+
return {
|
|
2946
|
+
virtualizedCount,
|
|
2947
|
+
vmHelperName
|
|
2948
|
+
};
|
|
1446
2949
|
}
|
|
1447
2950
|
|
|
1448
|
-
// src/
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
const
|
|
1452
|
-
|
|
2951
|
+
// src/ast/wasm-vault.ts
|
|
2952
|
+
var acorn4 = __toESM(require("acorn"), 1);
|
|
2953
|
+
function encodeSignedLeb128(value) {
|
|
2954
|
+
const bytes = [];
|
|
2955
|
+
let val = value | 0;
|
|
2956
|
+
let more = true;
|
|
2957
|
+
while (more) {
|
|
2958
|
+
let byte = val & 127;
|
|
2959
|
+
val >>= 7;
|
|
2960
|
+
if (val === 0 && (byte & 64) === 0 || val === -1 && (byte & 64) !== 0) {
|
|
2961
|
+
more = false;
|
|
2962
|
+
} else {
|
|
2963
|
+
byte |= 128;
|
|
2964
|
+
}
|
|
2965
|
+
bytes.push(byte);
|
|
2966
|
+
}
|
|
2967
|
+
return bytes;
|
|
2968
|
+
}
|
|
2969
|
+
function encodeUnsignedLeb128(value) {
|
|
2970
|
+
const bytes = [];
|
|
2971
|
+
let val = value >>> 0;
|
|
2972
|
+
do {
|
|
2973
|
+
let byte = val & 127;
|
|
2974
|
+
val >>>= 7;
|
|
2975
|
+
if (val !== 0) {
|
|
2976
|
+
byte |= 128;
|
|
2977
|
+
}
|
|
2978
|
+
bytes.push(byte);
|
|
2979
|
+
} while (val !== 0);
|
|
2980
|
+
return bytes;
|
|
2981
|
+
}
|
|
2982
|
+
function generateWasmKdfBinary(seed) {
|
|
2983
|
+
const instructions = [
|
|
2984
|
+
32,
|
|
2985
|
+
0,
|
|
2986
|
+
// local.get 0
|
|
2987
|
+
65,
|
|
2988
|
+
...encodeSignedLeb128(seed),
|
|
2989
|
+
// i32.const seed
|
|
2990
|
+
115,
|
|
2991
|
+
// i32.xor
|
|
2992
|
+
65,
|
|
2993
|
+
...encodeSignedLeb128(16777619),
|
|
2994
|
+
// i32.const 0x01000193
|
|
2995
|
+
108,
|
|
2996
|
+
// i32.mul
|
|
2997
|
+
32,
|
|
2998
|
+
1,
|
|
2999
|
+
// local.get 1
|
|
3000
|
+
32,
|
|
3001
|
+
2,
|
|
3002
|
+
// local.get 2
|
|
3003
|
+
106,
|
|
3004
|
+
// i32.add
|
|
3005
|
+
65,
|
|
3006
|
+
...encodeSignedLeb128(73244475),
|
|
3007
|
+
// i32.const 0x45d9f3b
|
|
3008
|
+
108,
|
|
3009
|
+
// i32.mul
|
|
3010
|
+
115,
|
|
3011
|
+
// i32.xor
|
|
3012
|
+
11
|
|
3013
|
+
// end
|
|
3014
|
+
];
|
|
3015
|
+
const funcBody = [0, ...instructions];
|
|
3016
|
+
const funcBodyWithLen = [...encodeUnsignedLeb128(funcBody.length), ...funcBody];
|
|
3017
|
+
const codeSectionPayload = [1, ...funcBodyWithLen];
|
|
3018
|
+
const codeSection = [
|
|
3019
|
+
10,
|
|
3020
|
+
...encodeUnsignedLeb128(codeSectionPayload.length),
|
|
3021
|
+
...codeSectionPayload
|
|
3022
|
+
];
|
|
3023
|
+
const typePayload = [
|
|
3024
|
+
1,
|
|
3025
|
+
// 1 type
|
|
3026
|
+
96,
|
|
3027
|
+
// func
|
|
3028
|
+
3,
|
|
3029
|
+
127,
|
|
3030
|
+
127,
|
|
3031
|
+
127,
|
|
3032
|
+
// 3 params of type i32
|
|
3033
|
+
1,
|
|
3034
|
+
127
|
|
3035
|
+
// 1 result of type i32
|
|
3036
|
+
];
|
|
3037
|
+
const typeSection = [
|
|
3038
|
+
1,
|
|
3039
|
+
...encodeUnsignedLeb128(typePayload.length),
|
|
3040
|
+
...typePayload
|
|
3041
|
+
];
|
|
3042
|
+
const funcPayload = [
|
|
3043
|
+
1,
|
|
3044
|
+
// 1 function
|
|
3045
|
+
0
|
|
3046
|
+
// type index 0
|
|
3047
|
+
];
|
|
3048
|
+
const funcSection = [
|
|
3049
|
+
3,
|
|
3050
|
+
...encodeUnsignedLeb128(funcPayload.length),
|
|
3051
|
+
...funcPayload
|
|
3052
|
+
];
|
|
3053
|
+
const exportName = [3, 107, 100, 102];
|
|
3054
|
+
const exportPayload = [
|
|
3055
|
+
1,
|
|
3056
|
+
// 1 export
|
|
3057
|
+
...exportName,
|
|
3058
|
+
0,
|
|
3059
|
+
// function export
|
|
3060
|
+
0
|
|
3061
|
+
// function index 0
|
|
3062
|
+
];
|
|
3063
|
+
const exportSection = [
|
|
3064
|
+
7,
|
|
3065
|
+
...encodeUnsignedLeb128(exportPayload.length),
|
|
3066
|
+
...exportPayload
|
|
3067
|
+
];
|
|
3068
|
+
const header = [0, 97, 115, 109, 1, 0, 0, 0];
|
|
3069
|
+
return new Uint8Array([
|
|
3070
|
+
...header,
|
|
3071
|
+
...typeSection,
|
|
3072
|
+
...funcSection,
|
|
3073
|
+
...exportSection,
|
|
3074
|
+
...codeSection
|
|
3075
|
+
]);
|
|
3076
|
+
}
|
|
3077
|
+
function fallbackKdf(fnId, callSiteSalt, shieldHash, seed) {
|
|
3078
|
+
return Math.imul(fnId ^ seed | 0, 16777619) ^ Math.imul(callSiteSalt + shieldHash | 0, 73244475) | 0;
|
|
3079
|
+
}
|
|
3080
|
+
function isWasmVaultAlreadyInjected(ast) {
|
|
3081
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
3082
|
+
return ast.body.some((stmt) => {
|
|
3083
|
+
if (stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
3084
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_wKdf")
|
|
3085
|
+
)) {
|
|
3086
|
+
return true;
|
|
3087
|
+
}
|
|
3088
|
+
return false;
|
|
3089
|
+
});
|
|
3090
|
+
}
|
|
3091
|
+
function injectWasmVault(ast, seed = 2654435769) {
|
|
3092
|
+
const suffix = computeFnv1aHash(`wasm:${seed}`, seed).toString(36);
|
|
3093
|
+
const kdfFnName = `_wKdf${suffix}`;
|
|
3094
|
+
const wasmVarName = `_wb${suffix}`;
|
|
3095
|
+
if (isWasmVaultAlreadyInjected(ast)) {
|
|
3096
|
+
const existing = ast.body.find(
|
|
3097
|
+
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
3098
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_wKdf")
|
|
3099
|
+
)
|
|
3100
|
+
);
|
|
3101
|
+
const id = existing?.declarations[0]?.id?.name ?? kdfFnName;
|
|
3102
|
+
return {
|
|
3103
|
+
kdfFnName: id,
|
|
3104
|
+
wasmBytes: generateWasmKdfBinary(seed),
|
|
3105
|
+
wasmVarName
|
|
3106
|
+
};
|
|
3107
|
+
}
|
|
3108
|
+
const wasmBytes = generateWasmKdfBinary(seed);
|
|
3109
|
+
const byteArrayLiteral = Array.from(wasmBytes).join(",");
|
|
3110
|
+
const helperSnippet = `var ${kdfFnName};
|
|
3111
|
+
try {
|
|
3112
|
+
var ${wasmVarName} = new Uint8Array([${byteArrayLiteral}]);
|
|
3113
|
+
var _wm${suffix} = new WebAssembly.Module(${wasmVarName});
|
|
3114
|
+
var _wi${suffix} = new WebAssembly.Instance(_wm${suffix});
|
|
3115
|
+
${kdfFnName} = _wi${suffix}.exports.kdf;
|
|
3116
|
+
} catch (e) {
|
|
3117
|
+
${kdfFnName} = function(f, s, h) {
|
|
3118
|
+
return (Math.imul((f ^ ${seed | 0}) | 0, 16777619) ^ Math.imul(((s + h) | 0), 73244475)) | 0;
|
|
3119
|
+
};
|
|
3120
|
+
}`;
|
|
3121
|
+
const parsedHelper = acorn4.parse(helperSnippet, {
|
|
3122
|
+
ecmaVersion: "latest",
|
|
3123
|
+
sourceType: "script"
|
|
3124
|
+
});
|
|
3125
|
+
ast.body.unshift(...parsedHelper.body);
|
|
1453
3126
|
return {
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
3127
|
+
kdfFnName,
|
|
3128
|
+
wasmBytes,
|
|
3129
|
+
wasmVarName
|
|
1457
3130
|
};
|
|
1458
3131
|
}
|
|
1459
3132
|
|
|
1460
|
-
// src/ast/
|
|
1461
|
-
|
|
3133
|
+
// src/ast/salted-vault.ts
|
|
3134
|
+
var acorn5 = __toESM(require("acorn"), 1);
|
|
3135
|
+
var walk6 = __toESM(require("acorn-walk"), 1);
|
|
3136
|
+
var DIRECTIVE_STRINGS2 = /* @__PURE__ */ new Set([
|
|
3137
|
+
"use strict",
|
|
3138
|
+
"use client",
|
|
3139
|
+
"use server",
|
|
3140
|
+
"use asm"
|
|
3141
|
+
]);
|
|
3142
|
+
function encodeSaltedString(str, k) {
|
|
3143
|
+
let out = "";
|
|
3144
|
+
for (let j = 0; j < str.length; j++) {
|
|
3145
|
+
const code = str.charCodeAt(j);
|
|
3146
|
+
const mask = k >>> j % 24 & 127 | 1;
|
|
3147
|
+
out += String.fromCharCode(code ^ mask);
|
|
3148
|
+
}
|
|
3149
|
+
return out;
|
|
3150
|
+
}
|
|
3151
|
+
function isAstAlreadySalted(ast) {
|
|
1462
3152
|
if (!ast || !Array.isArray(ast.body)) return false;
|
|
1463
3153
|
return ast.body.some((stmt) => {
|
|
1464
3154
|
if (stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
1465
|
-
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("
|
|
3155
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uP")
|
|
1466
3156
|
)) {
|
|
1467
3157
|
return true;
|
|
1468
3158
|
}
|
|
1469
|
-
|
|
3159
|
+
return false;
|
|
3160
|
+
});
|
|
3161
|
+
}
|
|
3162
|
+
function replaceNodeInParent(parent, oldNode, newNode) {
|
|
3163
|
+
if (!parent) return false;
|
|
3164
|
+
for (const key of Object.keys(parent)) {
|
|
3165
|
+
const val = parent[key];
|
|
3166
|
+
if (val === oldNode) {
|
|
3167
|
+
parent[key] = newNode;
|
|
1470
3168
|
return true;
|
|
1471
3169
|
}
|
|
3170
|
+
if (Array.isArray(val)) {
|
|
3171
|
+
const idx = val.indexOf(oldNode);
|
|
3172
|
+
if (idx !== -1) {
|
|
3173
|
+
val[idx] = newNode;
|
|
3174
|
+
return true;
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
return false;
|
|
3179
|
+
}
|
|
3180
|
+
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes) {
|
|
3181
|
+
if (isAstAlreadySalted(ast)) {
|
|
3182
|
+
return {
|
|
3183
|
+
encryptedStringsCount: 0,
|
|
3184
|
+
integrityToken: computeFnv1aHash("already-salted", seed),
|
|
3185
|
+
vaultVarName: "",
|
|
3186
|
+
decryptFnName: ""
|
|
3187
|
+
};
|
|
3188
|
+
}
|
|
3189
|
+
const targetsToReplace = [];
|
|
3190
|
+
const encryptedPayloads = [];
|
|
3191
|
+
walk6.ancestor(ast, {
|
|
3192
|
+
Literal(node, ancestors) {
|
|
3193
|
+
if (typeof node.value !== "string") return;
|
|
3194
|
+
if (node.value.length < minStringLength) return;
|
|
3195
|
+
if (DIRECTIVE_STRINGS2.has(node.value)) return;
|
|
3196
|
+
const parent = ancestors[ancestors.length - 2];
|
|
3197
|
+
if (!parent) return;
|
|
3198
|
+
if ((parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration") && parent.source === node) {
|
|
3199
|
+
return;
|
|
3200
|
+
}
|
|
3201
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
3202
|
+
return;
|
|
3203
|
+
}
|
|
3204
|
+
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
3205
|
+
return;
|
|
3206
|
+
}
|
|
3207
|
+
if (hotExemptNodes) {
|
|
3208
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
3209
|
+
if (hotExemptNodes.has(ancestors[i])) {
|
|
3210
|
+
return;
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
const callSiteIdx = targetsToReplace.length;
|
|
3215
|
+
const localSalt = computeFnv1aHash(`salt:${seed}:${callSiteIdx}`, seed) & 2147483647 | 1;
|
|
3216
|
+
targetsToReplace.push({
|
|
3217
|
+
node,
|
|
3218
|
+
parent,
|
|
3219
|
+
callSiteIdx,
|
|
3220
|
+
localSalt,
|
|
3221
|
+
rawString: node.value
|
|
3222
|
+
});
|
|
3223
|
+
}
|
|
3224
|
+
});
|
|
3225
|
+
if (targetsToReplace.length === 0) {
|
|
3226
|
+
return {
|
|
3227
|
+
encryptedStringsCount: 0,
|
|
3228
|
+
integrityToken: computeFnv1aHash("empty-salted", seed),
|
|
3229
|
+
vaultVarName: "",
|
|
3230
|
+
decryptFnName: ""
|
|
3231
|
+
};
|
|
3232
|
+
}
|
|
3233
|
+
const wasmResult = injectWasmVault(ast, seed);
|
|
3234
|
+
const kdfFnName = wasmResult.kdfFnName;
|
|
3235
|
+
const suffix = computeFnv1aHash(`salted:${seed}`, seed).toString(36);
|
|
3236
|
+
const vaultVarName = `_uP${suffix}`;
|
|
3237
|
+
const cacheVarName = `_uCache${suffix}`;
|
|
3238
|
+
const decryptFnName = `_uD${suffix}`;
|
|
3239
|
+
for (const target of targetsToReplace) {
|
|
3240
|
+
const k = fallbackKdf(target.callSiteIdx, target.localSalt, 0, seed);
|
|
3241
|
+
encryptedPayloads.push(encodeSaltedString(target.rawString, k));
|
|
3242
|
+
const callExpr = {
|
|
3243
|
+
type: "CallExpression",
|
|
3244
|
+
callee: {
|
|
3245
|
+
type: "Identifier",
|
|
3246
|
+
name: decryptFnName
|
|
3247
|
+
},
|
|
3248
|
+
arguments: [
|
|
3249
|
+
{
|
|
3250
|
+
type: "Literal",
|
|
3251
|
+
value: target.callSiteIdx,
|
|
3252
|
+
raw: String(target.callSiteIdx)
|
|
3253
|
+
},
|
|
3254
|
+
{
|
|
3255
|
+
type: "Literal",
|
|
3256
|
+
value: target.localSalt,
|
|
3257
|
+
raw: String(target.localSalt)
|
|
3258
|
+
}
|
|
3259
|
+
],
|
|
3260
|
+
optional: false
|
|
3261
|
+
};
|
|
3262
|
+
replaceNodeInParent(target.parent, target.node, callExpr);
|
|
3263
|
+
}
|
|
3264
|
+
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
3265
|
+
const integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
3266
|
+
const helperSnippet = `var ${vaultVarName} = ${serializedTable};
|
|
3267
|
+
var ${cacheVarName} = [];
|
|
3268
|
+
function ${decryptFnName}(i, s) {
|
|
3269
|
+
if (${cacheVarName}[i] !== void 0) return ${cacheVarName}[i];
|
|
3270
|
+
var d = typeof window !== 'undefined' && window.__unpeek_delta != null ? window.__unpeek_delta : 0;
|
|
3271
|
+
var k = ${kdfFnName}(i, s, d);
|
|
3272
|
+
var p = ${vaultVarName}[i], o = '';
|
|
3273
|
+
for (var j = 0; j < p.length; j++) {
|
|
3274
|
+
o += String.fromCharCode(p.charCodeAt(j) ^ (((k >>> (j % 24)) & 127) | 1));
|
|
3275
|
+
}
|
|
3276
|
+
return (${cacheVarName}[i] = o);
|
|
3277
|
+
}`;
|
|
3278
|
+
const parsedHelper = acorn5.parse(helperSnippet, {
|
|
3279
|
+
ecmaVersion: "latest",
|
|
3280
|
+
sourceType: "script"
|
|
3281
|
+
});
|
|
3282
|
+
ast.body.unshift(...parsedHelper.body);
|
|
3283
|
+
return {
|
|
3284
|
+
encryptedStringsCount: targetsToReplace.length,
|
|
3285
|
+
integrityToken,
|
|
3286
|
+
vaultVarName,
|
|
3287
|
+
decryptFnName
|
|
3288
|
+
};
|
|
3289
|
+
}
|
|
3290
|
+
|
|
3291
|
+
// src/ast/index.ts
|
|
3292
|
+
function isAstAlreadyProtected(ast) {
|
|
3293
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
3294
|
+
return ast.body.some((stmt) => {
|
|
3295
|
+
if (stmt.type === "VariableDeclaration" && Array.isArray(stmt.declarations)) {
|
|
3296
|
+
for (const d of stmt.declarations) {
|
|
3297
|
+
const name = d.id?.name;
|
|
3298
|
+
if (typeof name === "string") {
|
|
3299
|
+
if (name === "_d" || name.startsWith("_uC") || name.startsWith("_uD") || name.startsWith("_uS") || name.startsWith("_uVm") || name.startsWith("_wKdf") || name.startsWith("_m") || name.startsWith("_w") || name.startsWith("_k") || name.startsWith("_a") && d.init?.type === "CallExpression" && d.init.callee?.type === "FunctionExpression" && d.init.callee?.id?.name === "f") {
|
|
3300
|
+
return true;
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
if (stmt.type === "ExpressionStatement" && stmt.expression?.type === "CallExpression") {
|
|
3306
|
+
const callee = stmt.expression.callee;
|
|
3307
|
+
if (callee?.type === "FunctionExpression") {
|
|
3308
|
+
const name = callee.id?.name;
|
|
3309
|
+
if (typeof name === "string" && (name.startsWith("_u$") || name.startsWith("_$") || name.startsWith("_s") || name.startsWith("$b"))) {
|
|
3310
|
+
return true;
|
|
3311
|
+
}
|
|
3312
|
+
}
|
|
3313
|
+
}
|
|
1472
3314
|
return false;
|
|
1473
3315
|
});
|
|
1474
3316
|
}
|
|
1475
3317
|
function transformJavaScript(code, options = {}) {
|
|
1476
3318
|
const seed = options.seed ?? 2654435769;
|
|
1477
3319
|
const minStringLength = options.minStringLength ?? 4;
|
|
3320
|
+
const isAggressive = options.aggressive ?? false;
|
|
1478
3321
|
if (!code || !code.trim()) {
|
|
1479
3322
|
return {
|
|
1480
3323
|
code,
|
|
@@ -1485,7 +3328,9 @@ function transformJavaScript(code, options = {}) {
|
|
|
1485
3328
|
hotFunctionsSkipped: [],
|
|
1486
3329
|
coldFunctionsProtected: []
|
|
1487
3330
|
},
|
|
1488
|
-
integrityToken: computeFnv1aHash("", seed)
|
|
3331
|
+
integrityToken: computeFnv1aHash("", seed),
|
|
3332
|
+
virtualizedFunctionsCount: 0,
|
|
3333
|
+
shaderBuffersCount: 0
|
|
1489
3334
|
};
|
|
1490
3335
|
}
|
|
1491
3336
|
const ast = parseJavaScriptAst(code);
|
|
@@ -1499,23 +3344,59 @@ function transformJavaScript(code, options = {}) {
|
|
|
1499
3344
|
hotFunctionsSkipped: [],
|
|
1500
3345
|
coldFunctionsProtected: []
|
|
1501
3346
|
},
|
|
1502
|
-
integrityToken: computeFnv1aHash(code, seed)
|
|
3347
|
+
integrityToken: computeFnv1aHash(code, seed),
|
|
3348
|
+
virtualizedFunctionsCount: 0,
|
|
3349
|
+
shaderBuffersCount: 0
|
|
1503
3350
|
};
|
|
1504
3351
|
}
|
|
1505
3352
|
const {
|
|
1506
3353
|
report: hotPathReport,
|
|
1507
3354
|
coldFunctionNodes,
|
|
1508
|
-
|
|
3355
|
+
hotFunctionNodes,
|
|
3356
|
+
hotExemptNodes,
|
|
3357
|
+
serializedFunctionNodes
|
|
1509
3358
|
} = analyzeAstHotPaths(ast);
|
|
3359
|
+
let virtualizedFunctionsCount = 0;
|
|
3360
|
+
if (isAggressive) {
|
|
3361
|
+
const vmRes = applyMicroVm(
|
|
3362
|
+
ast,
|
|
3363
|
+
coldFunctionNodes,
|
|
3364
|
+
serializedFunctionNodes,
|
|
3365
|
+
seed
|
|
3366
|
+
);
|
|
3367
|
+
virtualizedFunctionsCount = vmRes.virtualizedCount;
|
|
3368
|
+
}
|
|
1510
3369
|
applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
|
|
1511
|
-
const mangledSymbolsCount = applyPolymorphicMangler(
|
|
1512
|
-
const { encryptedStringsCount, integrityToken } = applyStringVault(
|
|
3370
|
+
const mangledSymbolsCount = applyPolymorphicMangler(
|
|
1513
3371
|
ast,
|
|
1514
3372
|
seed,
|
|
1515
|
-
|
|
1516
|
-
hotExemptNodes
|
|
3373
|
+
serializedFunctionNodes
|
|
1517
3374
|
);
|
|
1518
|
-
|
|
3375
|
+
let encryptedStringsCount = 0;
|
|
3376
|
+
let integrityToken = 0;
|
|
3377
|
+
let shaderBuffersCount = 0;
|
|
3378
|
+
if (isAggressive) {
|
|
3379
|
+
injectWasmVault(ast, seed);
|
|
3380
|
+
const saltedRes = applySaltedStringVault(
|
|
3381
|
+
ast,
|
|
3382
|
+
seed,
|
|
3383
|
+
minStringLength,
|
|
3384
|
+
hotExemptNodes
|
|
3385
|
+
);
|
|
3386
|
+
encryptedStringsCount = saltedRes.encryptedStringsCount;
|
|
3387
|
+
integrityToken = saltedRes.integrityToken;
|
|
3388
|
+
} else {
|
|
3389
|
+
const vaultRes = applyStringVault(
|
|
3390
|
+
ast,
|
|
3391
|
+
seed,
|
|
3392
|
+
minStringLength,
|
|
3393
|
+
hotExemptNodes
|
|
3394
|
+
);
|
|
3395
|
+
encryptedStringsCount = vaultRes.encryptedStringsCount;
|
|
3396
|
+
integrityToken = vaultRes.integrityToken;
|
|
3397
|
+
shaderBuffersCount = vaultRes.shaderBuffersCount ?? 0;
|
|
3398
|
+
}
|
|
3399
|
+
const outputCode = generateCompactJs(ast);
|
|
1519
3400
|
let finalCode = outputCode;
|
|
1520
3401
|
if (options.injectRuntimeShield) {
|
|
1521
3402
|
const shield = generateRuntimeShield({
|
|
@@ -1533,13 +3414,24 @@ ${finalCode}`;
|
|
|
1533
3414
|
encryptedStringsCount,
|
|
1534
3415
|
mangledSymbolsCount,
|
|
1535
3416
|
hotPathReport,
|
|
1536
|
-
integrityToken
|
|
3417
|
+
integrityToken,
|
|
3418
|
+
virtualizedFunctionsCount,
|
|
3419
|
+
shaderBuffersCount
|
|
1537
3420
|
};
|
|
1538
3421
|
}
|
|
1539
3422
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1540
3423
|
0 && (module.exports = {
|
|
3424
|
+
analyzeAstHotPaths,
|
|
3425
|
+
applyColdFlowGuard,
|
|
3426
|
+
applyMicroVm,
|
|
3427
|
+
applyPolymorphicMangler,
|
|
3428
|
+
applySaltedStringVault,
|
|
3429
|
+
applyStringVault,
|
|
1541
3430
|
computeFnv1aHash,
|
|
3431
|
+
derivePolymorphicIdent,
|
|
1542
3432
|
detectHotPaths,
|
|
3433
|
+
generateCompactJs,
|
|
3434
|
+
injectWasmVault,
|
|
1543
3435
|
transformJavaScript
|
|
1544
3436
|
});
|
|
1545
3437
|
//# sourceMappingURL=index.cjs.map
|