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