unpeek 1.6.2 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ast/index.cjs +2341 -272
- 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 +2332 -274
- package/dist/ast/index.js.map +1 -1
- package/dist/browser.cjs +2326 -277
- 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 +2327 -278
- package/dist/browser.js.map +1 -1
- package/dist/bundler/unplugin.cjs +2324 -273
- package/dist/bundler/unplugin.cjs.map +1 -1
- package/dist/bundler/unplugin.js +2326 -275
- package/dist/bundler/unplugin.js.map +1 -1
- package/dist/cli/index.cjs +2360 -287
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2364 -291
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/protect.cjs +2352 -282
- package/dist/cli/protect.cjs.map +1 -1
- package/dist/cli/protect.js +2352 -282
- package/dist/cli/protect.js.map +1 -1
- package/dist/esbuild.cjs +2324 -273
- 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 +2326 -275
- package/dist/esbuild.js.map +1 -1
- package/dist/index.cjs +2438 -363
- 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 +2437 -362
- package/dist/index.js.map +1 -1
- package/dist/rollup.cjs +2324 -273
- 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 +2326 -275
- package/dist/rollup.js.map +1 -1
- package/dist/runtime/index.cjs +365 -20
- 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 +363 -20
- 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 +2324 -273
- 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 +2326 -275
- package/dist/vite.js.map +1 -1
- package/dist/webpack.cjs +2324 -273
- 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 +2326 -275
- package/dist/webpack.js.map +1 -1
- package/package.json +1 -1
package/dist/ast/index.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,40 +713,446 @@ 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
|
+
|
|
1075
|
+
// src/runtime/devtools-deterrent.ts
|
|
1076
|
+
function deriveIntegrityFnName(seed) {
|
|
1077
|
+
return derivePolymorphicIdent(seed, "shield");
|
|
1078
|
+
}
|
|
1079
|
+
function deriveWindowKeySymbol(seed) {
|
|
1080
|
+
return derivePolymorphicIdent(seed, "windowKey");
|
|
1081
|
+
}
|
|
1082
|
+
function createDeterrentSnippet(config) {
|
|
1083
|
+
const seed = config.seed >>> 0;
|
|
1084
|
+
const deterrentMode = config.deterrentMode ?? "passive";
|
|
1085
|
+
const fnName = deriveIntegrityFnName(seed);
|
|
1086
|
+
const sym = config.windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1087
|
+
const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=function(){return 0;};" : "";
|
|
1088
|
+
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);`;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// src/runtime/index.ts
|
|
1092
|
+
function generateRuntimeShield(config) {
|
|
1093
|
+
const seed = config.seed >>> 0;
|
|
1094
|
+
const windowKeySymbol = config.windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1095
|
+
const integrityCheckFnName = deriveIntegrityFnName(seed);
|
|
1096
|
+
const preambleCode = createDeterrentSnippet({
|
|
1097
|
+
...config,
|
|
1098
|
+
windowKeySymbol
|
|
1099
|
+
});
|
|
1100
|
+
const fnStart = preambleCode.indexOf("function");
|
|
1101
|
+
const fnEnd = preambleCode.lastIndexOf("(") - 1;
|
|
1102
|
+
const fnDecl = preambleCode.slice(fnStart, fnEnd);
|
|
1103
|
+
const computedHash = computeIntegrityHash(fnDecl, 0);
|
|
1104
|
+
const expectedHash = config.expectedIntegrityHash !== void 0 ? config.expectedIntegrityHash >>> 0 : computedHash;
|
|
1105
|
+
const expectedDelta = config.expectedDelta !== void 0 ? config.expectedDelta & 31 : computedHash >>> 0 & 31;
|
|
1106
|
+
return {
|
|
1107
|
+
preambleCode,
|
|
1108
|
+
integrityCheckFnName,
|
|
1109
|
+
expectedHash,
|
|
1110
|
+
windowKeySymbol,
|
|
1111
|
+
expectedDelta
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// src/ast/string-vault.ts
|
|
1116
|
+
var SAFE_ALPHA = " !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~";
|
|
554
1117
|
var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
|
|
555
1118
|
"use strict",
|
|
556
1119
|
"use client",
|
|
557
1120
|
"use server",
|
|
558
1121
|
"use asm"
|
|
559
1122
|
]);
|
|
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
|
-
|
|
586
|
-
|
|
587
|
-
|
|
1123
|
+
var STANDARD_PROP_KEYS = /* @__PURE__ */ new Set([
|
|
1124
|
+
"id",
|
|
1125
|
+
"type",
|
|
1126
|
+
"key",
|
|
1127
|
+
"ref",
|
|
1128
|
+
"className",
|
|
1129
|
+
"style",
|
|
1130
|
+
"name",
|
|
1131
|
+
"value",
|
|
1132
|
+
"children",
|
|
1133
|
+
"src",
|
|
1134
|
+
"href",
|
|
1135
|
+
"alt",
|
|
1136
|
+
"role",
|
|
1137
|
+
"tabIndex"
|
|
1138
|
+
]);
|
|
1139
|
+
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-|\[&_)/;
|
|
1140
|
+
var SVG_PATH_RE = /^[Mm]\s*-?\d+(?:\.\d+)?[\s,]+-?\d+/;
|
|
1141
|
+
var SHADER_SIG_RE = /@group|@binding|@vertex|@fragment|@compute|vec4f|gl_Position|uniform/;
|
|
1142
|
+
function shouldEncryptString(val, minLen) {
|
|
1143
|
+
if (val.length < minLen || DIRECTIVE_STRINGS.has(val)) return false;
|
|
1144
|
+
if (SVG_PATH_RE.test(val)) return false;
|
|
1145
|
+
if (val.includes(" ")) {
|
|
1146
|
+
const tokens = val.trim().split(/\s+/);
|
|
1147
|
+
if (tokens.length >= 3) {
|
|
1148
|
+
let twHits = 0;
|
|
1149
|
+
for (const t of tokens) {
|
|
1150
|
+
if (TAILWIND_TOKEN_RE.test(t)) twHits++;
|
|
1151
|
+
}
|
|
1152
|
+
if (twHits / tokens.length >= 0.5) return false;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
return true;
|
|
588
1156
|
}
|
|
589
1157
|
function computeFnv1aHash(input, seed = 2166136261) {
|
|
590
1158
|
let hash = seed >>> 0;
|
|
@@ -594,99 +1162,277 @@ function computeFnv1aHash(input, seed = 2166136261) {
|
|
|
594
1162
|
}
|
|
595
1163
|
return hash >>> 0;
|
|
596
1164
|
}
|
|
597
|
-
function
|
|
1165
|
+
function createPermutationTable(seed) {
|
|
1166
|
+
let s = (seed || 305419896) >>> 0;
|
|
1167
|
+
function xorshift() {
|
|
1168
|
+
s ^= s << 13;
|
|
1169
|
+
s ^= s >>> 17;
|
|
1170
|
+
s ^= s << 5;
|
|
1171
|
+
return s >>> 0;
|
|
1172
|
+
}
|
|
1173
|
+
const indices = [];
|
|
1174
|
+
for (let i = 0; i < 92; i++) indices.push(i);
|
|
1175
|
+
for (let i = 91; i > 0; i--) {
|
|
1176
|
+
const j = xorshift() % (i + 1);
|
|
1177
|
+
const tmp = indices[i];
|
|
1178
|
+
indices[i] = indices[j];
|
|
1179
|
+
indices[j] = tmp;
|
|
1180
|
+
}
|
|
1181
|
+
const B = new Array(92);
|
|
1182
|
+
for (let i = 0; i < 46; i++) {
|
|
1183
|
+
const a = indices[2 * i];
|
|
1184
|
+
const b = indices[2 * i + 1];
|
|
1185
|
+
B[a] = b;
|
|
1186
|
+
B[b] = a;
|
|
1187
|
+
}
|
|
1188
|
+
let P_CHARS = "";
|
|
1189
|
+
for (let i = 0; i < 92; i++) {
|
|
1190
|
+
P_CHARS += SAFE_ALPHA[B[i]];
|
|
1191
|
+
}
|
|
1192
|
+
return P_CHARS;
|
|
1193
|
+
}
|
|
1194
|
+
function encodeStringWithSeed(str, P_CHARS, expectedRot) {
|
|
598
1195
|
let out = "";
|
|
599
1196
|
for (let i = 0; i < str.length; i++) {
|
|
600
|
-
const
|
|
601
|
-
const k = (
|
|
602
|
-
|
|
1197
|
+
const c = str.charAt(i);
|
|
1198
|
+
const k = SAFE_ALPHA.indexOf(c);
|
|
1199
|
+
if (k < 0) {
|
|
1200
|
+
out += c;
|
|
1201
|
+
} else {
|
|
1202
|
+
const y = (k + expectedRot) % 92;
|
|
1203
|
+
const z = SAFE_ALPHA.indexOf(P_CHARS.charAt(y));
|
|
1204
|
+
out += SAFE_ALPHA.charAt((z - expectedRot + 92) % 92);
|
|
1205
|
+
}
|
|
603
1206
|
}
|
|
604
1207
|
return out;
|
|
605
1208
|
}
|
|
606
|
-
function
|
|
1209
|
+
function createVaultHelperAst(cacheVarName, encryptedPayloads, fnAst) {
|
|
1210
|
+
return {
|
|
1211
|
+
type: "VariableDeclaration",
|
|
1212
|
+
kind: "var",
|
|
1213
|
+
declarations: [
|
|
1214
|
+
{
|
|
1215
|
+
type: "VariableDeclarator",
|
|
1216
|
+
id: { type: "Identifier", name: cacheVarName },
|
|
1217
|
+
init: {
|
|
1218
|
+
type: "CallExpression",
|
|
1219
|
+
callee: fnAst,
|
|
1220
|
+
arguments: [
|
|
1221
|
+
{
|
|
1222
|
+
type: "ArrayExpression",
|
|
1223
|
+
elements: encryptedPayloads.map((str) => ({
|
|
1224
|
+
type: "Literal",
|
|
1225
|
+
value: str,
|
|
1226
|
+
raw: JSON.stringify(str)
|
|
1227
|
+
}))
|
|
1228
|
+
}
|
|
1229
|
+
],
|
|
1230
|
+
optional: false
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
]
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
function createShaderVaultAst(shaderVarName, shaderPayloads, seed) {
|
|
1237
|
+
const b64List = [];
|
|
1238
|
+
const key = seed >>> 0 & 255 ^ 90;
|
|
1239
|
+
for (let idx = 0; idx < shaderPayloads.length; idx++) {
|
|
1240
|
+
const str = shaderPayloads[idx];
|
|
1241
|
+
const buf = Buffer.from(str, "utf8");
|
|
1242
|
+
for (let i = 0; i < buf.length; i++) {
|
|
1243
|
+
buf[i] ^= key;
|
|
1244
|
+
}
|
|
1245
|
+
b64List.push(buf.toString("base64"));
|
|
1246
|
+
}
|
|
1247
|
+
const shaderCode = `var ${shaderVarName} = (function(){var B=${JSON.stringify(
|
|
1248
|
+
b64List
|
|
1249
|
+
)};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;}};})();`;
|
|
1250
|
+
return acorn2.parse(shaderCode, { ecmaVersion: "latest", sourceType: "script" }).body[0];
|
|
1251
|
+
}
|
|
1252
|
+
function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, windowKeySymbol, expectedDelta) {
|
|
1253
|
+
const cacheVarName = derivePolymorphicIdent(seed, "vault");
|
|
1254
|
+
const shaderVarName = derivePolymorphicIdent(seed, "shader");
|
|
1255
|
+
const sym = windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1256
|
+
let expDelta = expectedDelta;
|
|
1257
|
+
if (expDelta === void 0) {
|
|
1258
|
+
const shield = generateRuntimeShield({ seed, windowKeySymbol: sym });
|
|
1259
|
+
expDelta = shield.expectedDelta ?? 0;
|
|
1260
|
+
}
|
|
607
1261
|
const alreadyVaulted = ast.body.some(
|
|
608
1262
|
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
609
|
-
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uC")
|
|
1263
|
+
(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")
|
|
610
1264
|
)
|
|
611
1265
|
);
|
|
612
1266
|
if (alreadyVaulted) {
|
|
613
1267
|
return {
|
|
614
1268
|
encryptedStringsCount: 0,
|
|
615
|
-
integrityToken: computeFnv1aHash("already-vaulted", seed)
|
|
1269
|
+
integrityToken: computeFnv1aHash("already-vaulted", seed),
|
|
1270
|
+
shaderBuffersCount: 0
|
|
616
1271
|
};
|
|
617
1272
|
}
|
|
618
|
-
const
|
|
619
|
-
const
|
|
1273
|
+
const P_CHARS = createPermutationTable(seed);
|
|
1274
|
+
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;});`;
|
|
1275
|
+
const fnAst = acorn2.parse(`(function f(a){${fBody}})`, {
|
|
1276
|
+
ecmaVersion: "latest",
|
|
1277
|
+
sourceType: "script"
|
|
1278
|
+
}).body[0].expression;
|
|
1279
|
+
const compactFnStr = generateCompactJs(fnAst);
|
|
1280
|
+
const buildH = computeIntegrityHash(compactFnStr, 0);
|
|
1281
|
+
const expectedRot = (buildH ^ 0 ^ expDelta) & 31;
|
|
620
1282
|
const stringToIndex = /* @__PURE__ */ new Map();
|
|
621
1283
|
const encryptedPayloads = [];
|
|
1284
|
+
const shaderToIndex = /* @__PURE__ */ new Map();
|
|
1285
|
+
const shaderPayloads = [];
|
|
622
1286
|
const targetsToReplace = [];
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
1287
|
+
const customWalkBase = {
|
|
1288
|
+
...walk2.base,
|
|
1289
|
+
Property(node, st, c) {
|
|
1290
|
+
c(node.key, st);
|
|
1291
|
+
if (node.value) c(node.value, st);
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
walk2.ancestor(
|
|
1295
|
+
ast,
|
|
1296
|
+
{
|
|
1297
|
+
Literal(node, _state, ancestors) {
|
|
1298
|
+
if (typeof node.value !== "string") return;
|
|
1299
|
+
const val = node.value;
|
|
1300
|
+
if (DIRECTIVE_STRINGS.has(val)) return;
|
|
1301
|
+
if (hotExemptNodes) {
|
|
1302
|
+
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
1303
|
+
if (hotExemptNodes.has(ancestors[i])) {
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
const parent = ancestors[ancestors.length - 2];
|
|
1309
|
+
if (!parent) return;
|
|
1310
|
+
if (parent.type === "ExpressionStatement" && parent.directive) return;
|
|
1311
|
+
if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
|
|
1312
|
+
if (parent.source === node) return;
|
|
1313
|
+
}
|
|
1314
|
+
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
1321
|
+
if (STANDARD_PROP_KEYS.has(val)) {
|
|
632
1322
|
return;
|
|
633
1323
|
}
|
|
634
1324
|
}
|
|
1325
|
+
const isShader = val.length >= 256 || SHADER_SIG_RE.test(val);
|
|
1326
|
+
if (isShader) {
|
|
1327
|
+
let idx = shaderToIndex.get(val);
|
|
1328
|
+
if (idx === void 0) {
|
|
1329
|
+
idx = shaderPayloads.length;
|
|
1330
|
+
shaderToIndex.set(val, idx);
|
|
1331
|
+
shaderPayloads.push(val);
|
|
1332
|
+
}
|
|
1333
|
+
targetsToReplace.push({ type: "shader", node, parent, idx });
|
|
1334
|
+
} else {
|
|
1335
|
+
if (!shouldEncryptString(val, minStringLength)) return;
|
|
1336
|
+
let idx = stringToIndex.get(val);
|
|
1337
|
+
if (idx === void 0) {
|
|
1338
|
+
idx = encryptedPayloads.length;
|
|
1339
|
+
stringToIndex.set(val, idx);
|
|
1340
|
+
encryptedPayloads.push(
|
|
1341
|
+
encodeStringWithSeed(val, P_CHARS, expectedRot)
|
|
1342
|
+
);
|
|
1343
|
+
}
|
|
1344
|
+
targetsToReplace.push({ type: "string", node, parent, idx });
|
|
1345
|
+
}
|
|
1346
|
+
},
|
|
1347
|
+
TemplateLiteral(node, _state, ancestors) {
|
|
1348
|
+
if (!node.quasis || node.quasis.length !== 1) return;
|
|
1349
|
+
if (node.expressions && node.expressions.length !== 0) return;
|
|
1350
|
+
if (hotExemptNodes) {
|
|
1351
|
+
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
1352
|
+
if (hotExemptNodes.has(ancestors[i])) {
|
|
1353
|
+
return;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
const parent = ancestors[ancestors.length - 2];
|
|
1358
|
+
if (!parent) return;
|
|
1359
|
+
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
1360
|
+
if (typeof val !== "string" || DIRECTIVE_STRINGS.has(val)) return;
|
|
1361
|
+
const isShader = val.length >= 256 || SHADER_SIG_RE.test(val);
|
|
1362
|
+
if (isShader) {
|
|
1363
|
+
let idx = shaderToIndex.get(val);
|
|
1364
|
+
if (idx === void 0) {
|
|
1365
|
+
idx = shaderPayloads.length;
|
|
1366
|
+
shaderToIndex.set(val, idx);
|
|
1367
|
+
shaderPayloads.push(val);
|
|
1368
|
+
}
|
|
1369
|
+
targetsToReplace.push({ type: "shader", node, parent, idx });
|
|
1370
|
+
} else {
|
|
1371
|
+
if (!shouldEncryptString(val, minStringLength)) return;
|
|
1372
|
+
let idx = stringToIndex.get(val);
|
|
1373
|
+
if (idx === void 0) {
|
|
1374
|
+
idx = encryptedPayloads.length;
|
|
1375
|
+
stringToIndex.set(val, idx);
|
|
1376
|
+
encryptedPayloads.push(
|
|
1377
|
+
encodeStringWithSeed(val, P_CHARS, expectedRot)
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
targetsToReplace.push({ type: "string", node, parent, idx });
|
|
1381
|
+
}
|
|
635
1382
|
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
if (parent.source === node) return;
|
|
641
|
-
}
|
|
642
|
-
if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
|
|
646
|
-
return;
|
|
647
|
-
}
|
|
648
|
-
let idx = stringToIndex.get(val);
|
|
649
|
-
if (idx === void 0) {
|
|
650
|
-
idx = encryptedPayloads.length;
|
|
651
|
-
stringToIndex.set(val, idx);
|
|
652
|
-
encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
|
|
653
|
-
}
|
|
654
|
-
targetsToReplace.push({ node });
|
|
655
|
-
}
|
|
656
|
-
});
|
|
657
|
-
if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
|
|
1383
|
+
},
|
|
1384
|
+
customWalkBase
|
|
1385
|
+
);
|
|
1386
|
+
if (targetsToReplace.length === 0) {
|
|
658
1387
|
return {
|
|
659
1388
|
encryptedStringsCount: 0,
|
|
660
|
-
integrityToken: computeFnv1aHash("empty", seed)
|
|
1389
|
+
integrityToken: computeFnv1aHash("empty", seed),
|
|
1390
|
+
shaderBuffersCount: 0
|
|
661
1391
|
};
|
|
662
1392
|
}
|
|
663
1393
|
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
664
1394
|
const integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
1395
|
+
for (const { type, node, parent, idx } of targetsToReplace) {
|
|
1396
|
+
if (parent?.type === "Property" && parent.key === node) {
|
|
1397
|
+
parent.computed = true;
|
|
1398
|
+
}
|
|
1399
|
+
if (type === "shader") {
|
|
1400
|
+
node.type = "CallExpression";
|
|
1401
|
+
node.callee = {
|
|
1402
|
+
type: "Identifier",
|
|
1403
|
+
name: shaderVarName
|
|
1404
|
+
};
|
|
1405
|
+
node.arguments = [
|
|
1406
|
+
{
|
|
1407
|
+
type: "Literal",
|
|
1408
|
+
value: idx,
|
|
1409
|
+
raw: String(idx)
|
|
1410
|
+
}
|
|
1411
|
+
];
|
|
1412
|
+
node.optional = false;
|
|
1413
|
+
delete node.value;
|
|
1414
|
+
delete node.raw;
|
|
1415
|
+
delete node.quasis;
|
|
1416
|
+
delete node.expressions;
|
|
1417
|
+
} else {
|
|
1418
|
+
node.type = "MemberExpression";
|
|
1419
|
+
node.object = {
|
|
1420
|
+
type: "Identifier",
|
|
1421
|
+
name: cacheVarName
|
|
1422
|
+
};
|
|
1423
|
+
node.property = {
|
|
1424
|
+
type: "Literal",
|
|
1425
|
+
value: idx,
|
|
1426
|
+
raw: String(idx)
|
|
1427
|
+
};
|
|
1428
|
+
node.computed = true;
|
|
1429
|
+
node.optional = false;
|
|
1430
|
+
delete node.value;
|
|
1431
|
+
delete node.raw;
|
|
1432
|
+
delete node.quasis;
|
|
1433
|
+
delete node.expressions;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
690
1436
|
let insertIdx = 0;
|
|
691
1437
|
while (insertIdx < ast.body.length) {
|
|
692
1438
|
const stmt = ast.body[insertIdx];
|
|
@@ -697,10 +1443,26 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
697
1443
|
break;
|
|
698
1444
|
}
|
|
699
1445
|
}
|
|
700
|
-
|
|
1446
|
+
if (encryptedPayloads.length > 0) {
|
|
1447
|
+
const vaultHelperStmt = createVaultHelperAst(
|
|
1448
|
+
cacheVarName,
|
|
1449
|
+
encryptedPayloads,
|
|
1450
|
+
fnAst
|
|
1451
|
+
);
|
|
1452
|
+
ast.body.splice(insertIdx++, 0, vaultHelperStmt);
|
|
1453
|
+
}
|
|
1454
|
+
if (shaderPayloads.length > 0) {
|
|
1455
|
+
const shaderHelperStmt = createShaderVaultAst(
|
|
1456
|
+
shaderVarName,
|
|
1457
|
+
shaderPayloads,
|
|
1458
|
+
seed
|
|
1459
|
+
);
|
|
1460
|
+
ast.body.splice(insertIdx++, 0, shaderHelperStmt);
|
|
1461
|
+
}
|
|
701
1462
|
return {
|
|
702
1463
|
encryptedStringsCount: encryptedPayloads.length,
|
|
703
|
-
integrityToken
|
|
1464
|
+
integrityToken,
|
|
1465
|
+
shaderBuffersCount: shaderPayloads.length
|
|
704
1466
|
};
|
|
705
1467
|
}
|
|
706
1468
|
|
|
@@ -799,9 +1561,9 @@ function collectPatternTargets(pattern, out, shorthandProp) {
|
|
|
799
1561
|
}
|
|
800
1562
|
}
|
|
801
1563
|
function isMangleableName(origName) {
|
|
802
|
-
return !RESERVED_IDENTIFIERS.has(origName) && !origName.startsWith("_u") && !origName.startsWith("_0x");
|
|
1564
|
+
return origName.length > 2 && !RESERVED_IDENTIFIERS.has(origName) && !origName.startsWith("_u") && !origName.startsWith("_0x") && !origName.startsWith("$$");
|
|
803
1565
|
}
|
|
804
|
-
function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
1566
|
+
function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes) {
|
|
805
1567
|
const scopeRecords = /* @__PURE__ */ new Map();
|
|
806
1568
|
const existingIdentifiers = /* @__PURE__ */ new Set();
|
|
807
1569
|
let counter = 0;
|
|
@@ -847,11 +1609,16 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
847
1609
|
}
|
|
848
1610
|
return ancestors[0];
|
|
849
1611
|
}
|
|
850
|
-
function registerBindingTarget(scopeNode, target, shouldMangle) {
|
|
1612
|
+
function registerBindingTarget(scopeNode, target, shouldMangle, fnScope) {
|
|
851
1613
|
const scope = scopeRecords.get(scopeNode);
|
|
852
1614
|
if (!scope) return;
|
|
853
1615
|
const origName = target.idNode.name;
|
|
854
|
-
|
|
1616
|
+
let canMangle = shouldMangle && isMangleableName(origName);
|
|
1617
|
+
if (canMangle && fnScope) {
|
|
1618
|
+
if (serializedFunctionNodes && serializedFunctionNodes.has(fnScope)) {
|
|
1619
|
+
canMangle = false;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
855
1622
|
let entry = scope.bindings.get(origName);
|
|
856
1623
|
if (!entry) {
|
|
857
1624
|
entry = {
|
|
@@ -890,7 +1657,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
890
1657
|
const targets = [];
|
|
891
1658
|
collectPatternTargets(param, targets);
|
|
892
1659
|
for (const target of targets) {
|
|
893
|
-
registerBindingTarget(fnNode, target, true);
|
|
1660
|
+
registerBindingTarget(fnNode, target, true, fnNode);
|
|
894
1661
|
}
|
|
895
1662
|
}
|
|
896
1663
|
}
|
|
@@ -960,10 +1727,11 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
960
1727
|
CatchClause(node, _state, ancestors) {
|
|
961
1728
|
ensureScopeChain(ancestors);
|
|
962
1729
|
if (node.param) {
|
|
1730
|
+
const fnScope = findEnclosingFunctionNode(ancestors);
|
|
963
1731
|
const targets = [];
|
|
964
1732
|
collectPatternTargets(node.param, targets);
|
|
965
1733
|
for (const target of targets) {
|
|
966
|
-
registerBindingTarget(node, target, true);
|
|
1734
|
+
registerBindingTarget(node, target, true, fnScope);
|
|
967
1735
|
}
|
|
968
1736
|
}
|
|
969
1737
|
},
|
|
@@ -998,7 +1766,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
998
1766
|
}
|
|
999
1767
|
}
|
|
1000
1768
|
}
|
|
1001
|
-
registerBindingTarget(targetScopeNode, target, true);
|
|
1769
|
+
registerBindingTarget(targetScopeNode, target, true, fnScope);
|
|
1002
1770
|
}
|
|
1003
1771
|
}
|
|
1004
1772
|
}
|
|
@@ -1014,12 +1782,16 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1014
1782
|
}
|
|
1015
1783
|
return false;
|
|
1016
1784
|
}
|
|
1785
|
+
const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
1017
1786
|
function makeScopedMangledName(scope, localIndex) {
|
|
1018
1787
|
counter++;
|
|
1019
1788
|
let salt = 0;
|
|
1020
1789
|
while (true) {
|
|
1021
1790
|
const h = computeFnv1aHash(`s${scope.depth}:i${localIndex}:${salt}`, seed);
|
|
1022
|
-
const
|
|
1791
|
+
const prefix = (h & 1) === 0 ? "_" : "$";
|
|
1792
|
+
const ch = ALPHABET[((h >>> 1) + localIndex) % ALPHABET.length];
|
|
1793
|
+
const num = (localIndex + salt).toString(36);
|
|
1794
|
+
const candidate = `${prefix}${ch}${num}`;
|
|
1023
1795
|
if (!isNameUsedInScopeOrAncestors(candidate, scope)) {
|
|
1024
1796
|
return candidate;
|
|
1025
1797
|
}
|
|
@@ -1129,57 +1901,425 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
|
|
|
1129
1901
|
|
|
1130
1902
|
// src/ast/cold-flow-guard.ts
|
|
1131
1903
|
import * as walk4 from "acorn-walk";
|
|
1132
|
-
function
|
|
1133
|
-
const
|
|
1134
|
-
|
|
1135
|
-
|
|
1904
|
+
function createDecoyObjectExpression(model) {
|
|
1905
|
+
const m = Math.abs(model) % 3;
|
|
1906
|
+
if (m === 0) {
|
|
1907
|
+
return {
|
|
1908
|
+
type: "ObjectExpression",
|
|
1909
|
+
properties: [
|
|
1910
|
+
{
|
|
1911
|
+
type: "Property",
|
|
1912
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
1913
|
+
value: {
|
|
1914
|
+
type: "Literal",
|
|
1915
|
+
value: "/api/v2/auth/admin-bypass",
|
|
1916
|
+
raw: '"/api/v2/auth/admin-bypass"'
|
|
1917
|
+
},
|
|
1918
|
+
kind: "init",
|
|
1919
|
+
computed: false,
|
|
1920
|
+
method: false,
|
|
1921
|
+
shorthand: false
|
|
1922
|
+
},
|
|
1923
|
+
{
|
|
1924
|
+
type: "Property",
|
|
1925
|
+
key: { type: "Identifier", name: "superadmin_token" },
|
|
1926
|
+
value: { type: "Literal", value: 1, raw: "1" },
|
|
1927
|
+
kind: "init",
|
|
1928
|
+
computed: false,
|
|
1929
|
+
method: false,
|
|
1930
|
+
shorthand: false
|
|
1931
|
+
},
|
|
1932
|
+
{
|
|
1933
|
+
type: "Property",
|
|
1934
|
+
key: { type: "Identifier", name: "elevate" },
|
|
1935
|
+
value: { type: "Literal", value: 1, raw: "1" },
|
|
1936
|
+
kind: "init",
|
|
1937
|
+
computed: false,
|
|
1938
|
+
method: false,
|
|
1939
|
+
shorthand: false
|
|
1940
|
+
}
|
|
1941
|
+
]
|
|
1942
|
+
};
|
|
1943
|
+
}
|
|
1944
|
+
if (m === 1) {
|
|
1945
|
+
return {
|
|
1946
|
+
type: "ObjectExpression",
|
|
1947
|
+
properties: [
|
|
1948
|
+
{
|
|
1949
|
+
type: "Property",
|
|
1950
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
1951
|
+
value: {
|
|
1952
|
+
type: "Literal",
|
|
1953
|
+
value: "/api/v2/auth/verify-master",
|
|
1954
|
+
raw: '"/api/v2/auth/verify-master"'
|
|
1955
|
+
},
|
|
1956
|
+
kind: "init",
|
|
1957
|
+
computed: false,
|
|
1958
|
+
method: false,
|
|
1959
|
+
shorthand: false
|
|
1960
|
+
},
|
|
1961
|
+
{
|
|
1962
|
+
type: "Property",
|
|
1963
|
+
key: { type: "Identifier", name: "_verifyMasterToken" },
|
|
1964
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
1965
|
+
kind: "init",
|
|
1966
|
+
computed: false,
|
|
1967
|
+
method: false,
|
|
1968
|
+
shorthand: false
|
|
1969
|
+
},
|
|
1970
|
+
{
|
|
1971
|
+
type: "Property",
|
|
1972
|
+
key: { type: "Identifier", name: "_bypassToken" },
|
|
1973
|
+
value: {
|
|
1974
|
+
type: "Literal",
|
|
1975
|
+
value: "mock_jwt_bypass_signature",
|
|
1976
|
+
raw: '"mock_jwt_bypass_signature"'
|
|
1977
|
+
},
|
|
1978
|
+
kind: "init",
|
|
1979
|
+
computed: false,
|
|
1980
|
+
method: false,
|
|
1981
|
+
shorthand: false
|
|
1982
|
+
},
|
|
1983
|
+
{
|
|
1984
|
+
type: "Property",
|
|
1985
|
+
key: { type: "Identifier", name: "authorized" },
|
|
1986
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
1987
|
+
kind: "init",
|
|
1988
|
+
computed: false,
|
|
1989
|
+
method: false,
|
|
1990
|
+
shorthand: false
|
|
1991
|
+
}
|
|
1992
|
+
]
|
|
1993
|
+
};
|
|
1994
|
+
}
|
|
1995
|
+
return {
|
|
1996
|
+
type: "ObjectExpression",
|
|
1997
|
+
properties: [
|
|
1998
|
+
{
|
|
1999
|
+
type: "Property",
|
|
2000
|
+
key: { type: "Identifier", name: "endpoint" },
|
|
2001
|
+
value: {
|
|
2002
|
+
type: "Literal",
|
|
2003
|
+
value: "/api/v2/auth/debug-telemetry",
|
|
2004
|
+
raw: '"/api/v2/auth/debug-telemetry"'
|
|
2005
|
+
},
|
|
2006
|
+
kind: "init",
|
|
2007
|
+
computed: false,
|
|
2008
|
+
method: false,
|
|
2009
|
+
shorthand: false
|
|
2010
|
+
},
|
|
2011
|
+
{
|
|
2012
|
+
type: "Property",
|
|
2013
|
+
key: { type: "Identifier", name: "debug_supervisor_override" },
|
|
2014
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2015
|
+
kind: "init",
|
|
2016
|
+
computed: false,
|
|
2017
|
+
method: false,
|
|
2018
|
+
shorthand: false
|
|
2019
|
+
},
|
|
2020
|
+
{
|
|
2021
|
+
type: "Property",
|
|
2022
|
+
key: { type: "Identifier", name: "_bypassToken" },
|
|
2023
|
+
value: {
|
|
2024
|
+
type: "Literal",
|
|
2025
|
+
value: "superadmin_token",
|
|
2026
|
+
raw: '"superadmin_token"'
|
|
2027
|
+
},
|
|
2028
|
+
kind: "init",
|
|
2029
|
+
computed: false,
|
|
2030
|
+
method: false,
|
|
2031
|
+
shorthand: false
|
|
2032
|
+
},
|
|
2033
|
+
{
|
|
2034
|
+
type: "Property",
|
|
2035
|
+
key: { type: "Identifier", name: "mfa_bypassed" },
|
|
2036
|
+
value: { type: "Literal", value: true, raw: "true" },
|
|
2037
|
+
kind: "init",
|
|
2038
|
+
computed: false,
|
|
2039
|
+
method: false,
|
|
2040
|
+
shorthand: false
|
|
2041
|
+
}
|
|
2042
|
+
]
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
function createDecoyConsequent(fnIndex) {
|
|
2046
|
+
return {
|
|
2047
|
+
type: "BlockStatement",
|
|
2048
|
+
body: [
|
|
2049
|
+
{
|
|
2050
|
+
type: "ReturnStatement",
|
|
2051
|
+
argument: createDecoyObjectExpression(fnIndex)
|
|
2052
|
+
}
|
|
2053
|
+
]
|
|
2054
|
+
};
|
|
2055
|
+
}
|
|
2056
|
+
function createSharedDecoyConsequent(decoyIdent, fnIndex) {
|
|
2057
|
+
const model = Math.abs(fnIndex) % 3;
|
|
2058
|
+
return {
|
|
2059
|
+
type: "BlockStatement",
|
|
2060
|
+
body: [
|
|
2061
|
+
{
|
|
2062
|
+
type: "ReturnStatement",
|
|
2063
|
+
argument: {
|
|
2064
|
+
type: "CallExpression",
|
|
2065
|
+
callee: {
|
|
2066
|
+
type: "MemberExpression",
|
|
2067
|
+
computed: true,
|
|
2068
|
+
object: { type: "Identifier", name: decoyIdent },
|
|
2069
|
+
property: {
|
|
2070
|
+
type: "Literal",
|
|
2071
|
+
value: model,
|
|
2072
|
+
raw: String(model)
|
|
2073
|
+
}
|
|
2074
|
+
},
|
|
2075
|
+
arguments: []
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
]
|
|
2079
|
+
};
|
|
2080
|
+
}
|
|
2081
|
+
function createSharedDecoyDeclaration(decoyIdent = "_d") {
|
|
2082
|
+
return {
|
|
2083
|
+
type: "VariableDeclaration",
|
|
2084
|
+
kind: "var",
|
|
2085
|
+
declarations: [
|
|
2086
|
+
{
|
|
2087
|
+
type: "VariableDeclarator",
|
|
2088
|
+
id: { type: "Identifier", name: decoyIdent },
|
|
2089
|
+
init: {
|
|
2090
|
+
type: "ArrayExpression",
|
|
2091
|
+
elements: [0, 1, 2].map((m) => ({
|
|
2092
|
+
type: "ArrowFunctionExpression",
|
|
2093
|
+
id: null,
|
|
2094
|
+
params: [],
|
|
2095
|
+
body: createDecoyObjectExpression(m),
|
|
2096
|
+
generator: false,
|
|
2097
|
+
async: false,
|
|
2098
|
+
expression: true
|
|
2099
|
+
}))
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
]
|
|
2103
|
+
};
|
|
2104
|
+
}
|
|
2105
|
+
function cloneNode(node) {
|
|
2106
|
+
return JSON.parse(JSON.stringify(node));
|
|
2107
|
+
}
|
|
2108
|
+
function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArrowFn = false) {
|
|
2109
|
+
const family = Math.abs(seed + fnIndex & 3);
|
|
2110
|
+
const c1 = (seed >>> 0) + Math.imul(family + 1, 73244475) | 0;
|
|
2111
|
+
const c2 = c1 ^ 1515870810 | 0;
|
|
2112
|
+
const target = (seed ^ Math.imul(family + 1, 2654435761)) & 8388607 | 1;
|
|
2113
|
+
let paramName = null;
|
|
2114
|
+
if (typeof paramNode === "string") {
|
|
2115
|
+
paramName = paramNode;
|
|
2116
|
+
} else if (paramNode?.type === "Identifier") {
|
|
2117
|
+
paramName = paramNode.name;
|
|
2118
|
+
} else if (paramNode?.type === "AssignmentPattern" && paramNode.left?.type === "Identifier") {
|
|
2119
|
+
paramName = paramNode.left.name;
|
|
2120
|
+
}
|
|
2121
|
+
const zNode = {
|
|
2122
|
+
type: "UnaryExpression",
|
|
2123
|
+
operator: "+",
|
|
2124
|
+
prefix: true,
|
|
2125
|
+
argument: {
|
|
2126
|
+
type: "UnaryExpression",
|
|
2127
|
+
operator: "!",
|
|
2128
|
+
prefix: true,
|
|
2129
|
+
argument: { type: "Identifier", name: "String" }
|
|
2130
|
+
}
|
|
2131
|
+
};
|
|
2132
|
+
const vNode = {
|
|
2133
|
+
type: "BinaryExpression",
|
|
2134
|
+
operator: "|",
|
|
2135
|
+
left: zNode,
|
|
2136
|
+
right: { type: "Literal", value: c1, raw: String(c1) }
|
|
2137
|
+
};
|
|
2138
|
+
const kNode = {
|
|
2139
|
+
type: "BinaryExpression",
|
|
2140
|
+
operator: "|",
|
|
2141
|
+
left: { type: "Literal", value: c2, raw: String(c2) },
|
|
2142
|
+
right: {
|
|
2143
|
+
type: "UnaryExpression",
|
|
2144
|
+
operator: "!",
|
|
2145
|
+
prefix: true,
|
|
2146
|
+
argument: { type: "Identifier", name: "String" }
|
|
2147
|
+
}
|
|
2148
|
+
};
|
|
2149
|
+
let mbaExpr;
|
|
2150
|
+
if (family === 0) {
|
|
2151
|
+
const vXorK = {
|
|
2152
|
+
type: "BinaryExpression",
|
|
2153
|
+
operator: "^",
|
|
2154
|
+
left: cloneNode(vNode),
|
|
2155
|
+
right: cloneNode(kNode)
|
|
2156
|
+
};
|
|
2157
|
+
const vAndK = {
|
|
2158
|
+
type: "BinaryExpression",
|
|
2159
|
+
operator: "&",
|
|
2160
|
+
left: cloneNode(vNode),
|
|
2161
|
+
right: cloneNode(kNode)
|
|
2162
|
+
};
|
|
2163
|
+
const shift = {
|
|
2164
|
+
type: "BinaryExpression",
|
|
2165
|
+
operator: "<<",
|
|
2166
|
+
left: vAndK,
|
|
2167
|
+
right: { type: "Literal", value: 1, raw: "1" }
|
|
2168
|
+
};
|
|
2169
|
+
const addLeft = {
|
|
2170
|
+
type: "BinaryExpression",
|
|
2171
|
+
operator: "+",
|
|
2172
|
+
left: vXorK,
|
|
2173
|
+
right: shift
|
|
2174
|
+
};
|
|
2175
|
+
const vPlusK = {
|
|
2176
|
+
type: "BinaryExpression",
|
|
2177
|
+
operator: "+",
|
|
2178
|
+
left: cloneNode(vNode),
|
|
2179
|
+
right: cloneNode(kNode)
|
|
2180
|
+
};
|
|
2181
|
+
mbaExpr = {
|
|
2182
|
+
type: "BinaryExpression",
|
|
2183
|
+
operator: "-",
|
|
2184
|
+
left: addLeft,
|
|
2185
|
+
right: vPlusK
|
|
2186
|
+
};
|
|
2187
|
+
} else if (family === 1) {
|
|
2188
|
+
const vOrK = {
|
|
2189
|
+
type: "BinaryExpression",
|
|
2190
|
+
operator: "|",
|
|
2191
|
+
left: cloneNode(vNode),
|
|
2192
|
+
right: cloneNode(kNode)
|
|
2193
|
+
};
|
|
2194
|
+
const vAndK = {
|
|
2195
|
+
type: "BinaryExpression",
|
|
2196
|
+
operator: "&",
|
|
2197
|
+
left: cloneNode(vNode),
|
|
2198
|
+
right: cloneNode(kNode)
|
|
2199
|
+
};
|
|
2200
|
+
const addLeft = {
|
|
2201
|
+
type: "BinaryExpression",
|
|
2202
|
+
operator: "+",
|
|
2203
|
+
left: vOrK,
|
|
2204
|
+
right: vAndK
|
|
2205
|
+
};
|
|
2206
|
+
const vPlusK = {
|
|
2207
|
+
type: "BinaryExpression",
|
|
2208
|
+
operator: "+",
|
|
2209
|
+
left: cloneNode(vNode),
|
|
2210
|
+
right: cloneNode(kNode)
|
|
2211
|
+
};
|
|
2212
|
+
mbaExpr = {
|
|
2213
|
+
type: "BinaryExpression",
|
|
2214
|
+
operator: "^",
|
|
2215
|
+
left: addLeft,
|
|
2216
|
+
right: vPlusK
|
|
2217
|
+
};
|
|
2218
|
+
} else if (family === 2) {
|
|
2219
|
+
const notK = {
|
|
2220
|
+
type: "UnaryExpression",
|
|
2221
|
+
operator: "~",
|
|
2222
|
+
prefix: true,
|
|
2223
|
+
argument: cloneNode(kNode)
|
|
2224
|
+
};
|
|
2225
|
+
const vOrNotK = {
|
|
2226
|
+
type: "BinaryExpression",
|
|
2227
|
+
operator: "|",
|
|
2228
|
+
left: cloneNode(vNode),
|
|
2229
|
+
right: cloneNode(notK)
|
|
2230
|
+
};
|
|
2231
|
+
const subLeft = {
|
|
2232
|
+
type: "BinaryExpression",
|
|
2233
|
+
operator: "-",
|
|
2234
|
+
left: vOrNotK,
|
|
2235
|
+
right: cloneNode(notK)
|
|
2236
|
+
};
|
|
2237
|
+
const vAndK = {
|
|
2238
|
+
type: "BinaryExpression",
|
|
2239
|
+
operator: "&",
|
|
2240
|
+
left: cloneNode(vNode),
|
|
2241
|
+
right: cloneNode(kNode)
|
|
2242
|
+
};
|
|
2243
|
+
mbaExpr = {
|
|
2244
|
+
type: "BinaryExpression",
|
|
2245
|
+
operator: "^",
|
|
2246
|
+
left: subLeft,
|
|
2247
|
+
right: vAndK
|
|
2248
|
+
};
|
|
2249
|
+
} else {
|
|
2250
|
+
const notK = {
|
|
2251
|
+
type: "UnaryExpression",
|
|
2252
|
+
operator: "~",
|
|
2253
|
+
prefix: true,
|
|
2254
|
+
argument: cloneNode(kNode)
|
|
2255
|
+
};
|
|
2256
|
+
const vAndNotK = {
|
|
2257
|
+
type: "BinaryExpression",
|
|
2258
|
+
operator: "&",
|
|
2259
|
+
left: cloneNode(vNode),
|
|
2260
|
+
right: cloneNode(notK)
|
|
2261
|
+
};
|
|
2262
|
+
const vAndK = {
|
|
2263
|
+
type: "BinaryExpression",
|
|
2264
|
+
operator: "&",
|
|
2265
|
+
left: cloneNode(vNode),
|
|
2266
|
+
right: cloneNode(kNode)
|
|
2267
|
+
};
|
|
2268
|
+
const addLeft = {
|
|
2269
|
+
type: "BinaryExpression",
|
|
2270
|
+
operator: "+",
|
|
2271
|
+
left: vAndNotK,
|
|
2272
|
+
right: vAndK
|
|
2273
|
+
};
|
|
2274
|
+
mbaExpr = {
|
|
2275
|
+
type: "BinaryExpression",
|
|
2276
|
+
operator: "^",
|
|
2277
|
+
left: addLeft,
|
|
2278
|
+
right: cloneNode(vNode)
|
|
2279
|
+
};
|
|
2280
|
+
}
|
|
2281
|
+
const decoyIdx = Math.abs(fnIndex) % 3;
|
|
2282
|
+
const consequent = decoyIdent ? createSharedDecoyConsequent(decoyIdent, decoyIdx) : createDecoyConsequent(decoyIdx);
|
|
1136
2283
|
return {
|
|
1137
2284
|
type: "IfStatement",
|
|
1138
2285
|
test: {
|
|
1139
2286
|
type: "BinaryExpression",
|
|
1140
2287
|
operator: "===",
|
|
1141
|
-
left:
|
|
1142
|
-
|
|
1143
|
-
operator: "&",
|
|
1144
|
-
left: {
|
|
1145
|
-
type: "BinaryExpression",
|
|
1146
|
-
operator: "^",
|
|
1147
|
-
left: { type: "Literal", value: a, raw: String(a) },
|
|
1148
|
-
right: {
|
|
1149
|
-
type: "BinaryExpression",
|
|
1150
|
-
operator: "|",
|
|
1151
|
-
left: { type: "Literal", value: b, raw: String(b) },
|
|
1152
|
-
right: {
|
|
1153
|
-
type: "UnaryExpression",
|
|
1154
|
-
operator: "!",
|
|
1155
|
-
prefix: true,
|
|
1156
|
-
argument: { type: "Identifier", name: "String" }
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
},
|
|
1160
|
-
right: { type: "Literal", value: 7, raw: "7" }
|
|
1161
|
-
},
|
|
1162
|
-
right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
|
|
1163
|
-
},
|
|
1164
|
-
consequent: {
|
|
1165
|
-
type: "ExpressionStatement",
|
|
1166
|
-
expression: {
|
|
1167
|
-
type: "UnaryExpression",
|
|
1168
|
-
operator: "void",
|
|
1169
|
-
prefix: true,
|
|
1170
|
-
argument: { type: "Literal", value: 0, raw: "0" }
|
|
1171
|
-
}
|
|
2288
|
+
left: mbaExpr,
|
|
2289
|
+
right: { type: "Literal", value: target, raw: String(target) }
|
|
1172
2290
|
},
|
|
2291
|
+
consequent,
|
|
1173
2292
|
alternate: null
|
|
1174
2293
|
};
|
|
1175
2294
|
}
|
|
1176
|
-
function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
|
|
2295
|
+
function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
|
|
2296
|
+
const decoyIdent = "_d";
|
|
2297
|
+
if (Array.isArray(ast.body)) {
|
|
2298
|
+
const alreadyGuarded = ast.body.some(
|
|
2299
|
+
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
2300
|
+
(d) => d.id?.type === "Identifier" && d.id.name === decoyIdent
|
|
2301
|
+
)
|
|
2302
|
+
);
|
|
2303
|
+
if (alreadyGuarded) return 0;
|
|
2304
|
+
}
|
|
1177
2305
|
let guardedCount = 0;
|
|
1178
2306
|
function maybeInjectGuard(node) {
|
|
1179
2307
|
if (!coldFunctionNodes.has(node)) return;
|
|
1180
2308
|
if (!node.body || node.body.type !== "BlockStatement") return;
|
|
2309
|
+
const firstParam = node.params?.[0];
|
|
2310
|
+
const isArrow = node.type === "ArrowFunctionExpression";
|
|
2311
|
+
const guardStmt = createSotaMbaPredicate(
|
|
2312
|
+
seed,
|
|
2313
|
+
guardedCount,
|
|
2314
|
+
firstParam,
|
|
2315
|
+
decoyIdent,
|
|
2316
|
+
isArrow
|
|
2317
|
+
);
|
|
1181
2318
|
guardedCount++;
|
|
1182
|
-
|
|
2319
|
+
if (hotExemptNodes) {
|
|
2320
|
+
hotExemptNodes.add(guardStmt);
|
|
2321
|
+
hotExemptNodes.add(guardStmt.consequent);
|
|
2322
|
+
}
|
|
1183
2323
|
let insertIdx = 0;
|
|
1184
2324
|
while (insertIdx < node.body.body.length && node.body.body[insertIdx].type === "ExpressionStatement" && node.body.body[insertIdx].directive) {
|
|
1185
2325
|
insertIdx++;
|
|
@@ -1191,77 +2331,946 @@ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
|
|
|
1191
2331
|
FunctionExpression: maybeInjectGuard,
|
|
1192
2332
|
ArrowFunctionExpression: maybeInjectGuard
|
|
1193
2333
|
});
|
|
2334
|
+
if (guardedCount > 0 && Array.isArray(ast.body)) {
|
|
2335
|
+
const sharedDecoyDecl = createSharedDecoyDeclaration(decoyIdent);
|
|
2336
|
+
if (hotExemptNodes) {
|
|
2337
|
+
hotExemptNodes.add(sharedDecoyDecl);
|
|
2338
|
+
for (const decl of sharedDecoyDecl.declarations || []) {
|
|
2339
|
+
if (decl.init?.elements) {
|
|
2340
|
+
for (const el of decl.init.elements) {
|
|
2341
|
+
hotExemptNodes.add(el);
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
let insertIdx = 0;
|
|
2347
|
+
while (insertIdx < ast.body.length && ast.body[insertIdx].type === "ExpressionStatement" && ast.body[insertIdx].directive) {
|
|
2348
|
+
insertIdx++;
|
|
2349
|
+
}
|
|
2350
|
+
ast.body.splice(insertIdx, 0, sharedDecoyDecl);
|
|
2351
|
+
}
|
|
1194
2352
|
return guardedCount;
|
|
1195
2353
|
}
|
|
1196
2354
|
|
|
1197
|
-
// src/
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
2355
|
+
// src/ast/micro-vm.ts
|
|
2356
|
+
import * as acorn3 from "acorn";
|
|
2357
|
+
import * as walk5 from "acorn-walk";
|
|
2358
|
+
var OP_LDC = 0;
|
|
2359
|
+
var OP_LDV = 1;
|
|
2360
|
+
var OP_STV = 2;
|
|
2361
|
+
var OP_LDG = 3;
|
|
2362
|
+
var OP_GETP = 4;
|
|
2363
|
+
var OP_SETP = 5;
|
|
2364
|
+
var OP_CALL = 6;
|
|
2365
|
+
var OP_MCALL = 7;
|
|
2366
|
+
var OP_ADD = 8;
|
|
2367
|
+
var OP_SUB = 9;
|
|
2368
|
+
var OP_MUL = 10;
|
|
2369
|
+
var OP_XOR = 11;
|
|
2370
|
+
var OP_AND = 12;
|
|
2371
|
+
var OP_OR = 13;
|
|
2372
|
+
var OP_SEQ = 14;
|
|
2373
|
+
var OP_LT = 15;
|
|
2374
|
+
var OP_NOT = 16;
|
|
2375
|
+
var OP_JMP = 17;
|
|
2376
|
+
var OP_JZ = 18;
|
|
2377
|
+
var OP_RET = 19;
|
|
2378
|
+
var OP_THROW = 20;
|
|
2379
|
+
var OP_AWAIT = 21;
|
|
2380
|
+
var OP_OBJ = 22;
|
|
2381
|
+
var OP_ARR = 23;
|
|
2382
|
+
function generateOpcodeMap(seed) {
|
|
2383
|
+
const pool = [];
|
|
2384
|
+
for (let i = 0; i < 256; i++) pool.push(i);
|
|
2385
|
+
let s = (seed ^ 2779096485) >>> 0;
|
|
2386
|
+
function rand() {
|
|
2387
|
+
s ^= s << 13;
|
|
2388
|
+
s ^= s >>> 17;
|
|
2389
|
+
s ^= s << 5;
|
|
2390
|
+
return (s >>> 0) / 4294967296;
|
|
1205
2391
|
}
|
|
1206
|
-
|
|
2392
|
+
for (let i = pool.length - 1; i > 0; i--) {
|
|
2393
|
+
const j = Math.floor(rand() * (i + 1));
|
|
2394
|
+
const tmp = pool[i];
|
|
2395
|
+
pool[i] = pool[j];
|
|
2396
|
+
pool[j] = tmp;
|
|
2397
|
+
}
|
|
2398
|
+
return pool.slice(0, 24);
|
|
1207
2399
|
}
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
2400
|
+
function createInterpreterSource(vmHelperName, opMap) {
|
|
2401
|
+
return `function ${vmHelperName}(c, k, s, t) {
|
|
2402
|
+
var p = 0, r = 0, q = [];
|
|
2403
|
+
var G = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : this);
|
|
2404
|
+
function x() {
|
|
2405
|
+
while (p < c.length) {
|
|
2406
|
+
var o = c[p++];
|
|
2407
|
+
switch (o) {
|
|
2408
|
+
case ${opMap[OP_LDC]}: q[r++] = k[c[p++]]; break;
|
|
2409
|
+
case ${opMap[OP_LDV]}: q[r++] = s[c[p++]]; break;
|
|
2410
|
+
case ${opMap[OP_STV]}: s[c[p++]] = q[--r]; break;
|
|
2411
|
+
case ${opMap[OP_LDG]}: q[r++] = G[k[c[p++]]]; break;
|
|
2412
|
+
case ${opMap[OP_GETP]}: { var b = q[--r], a = q[--r]; q[r++] = a[b]; break; }
|
|
2413
|
+
case ${opMap[OP_SETP]}: { var v = q[--r], b = q[--r], a = q[--r]; q[r++] = (a[b] = v); break; }
|
|
2414
|
+
case ${opMap[OP_CALL]}: {
|
|
2415
|
+
var n = c[p++], g = [];
|
|
2416
|
+
for (var i = n - 1; i >= 0; i--) g[i] = q[--r];
|
|
2417
|
+
var f = q[--r]; q[r++] = f.apply(null, g); break;
|
|
2418
|
+
}
|
|
2419
|
+
case ${opMap[OP_MCALL]}: {
|
|
2420
|
+
var n = c[p++], g = [];
|
|
2421
|
+
for (var i = n - 1; i >= 0; i--) g[i] = q[--r];
|
|
2422
|
+
var b = q[--r], a = q[--r]; q[r++] = a[b].apply(a, g); break;
|
|
2423
|
+
}
|
|
2424
|
+
case ${opMap[OP_ADD]}: { var b = q[--r], a = q[--r]; q[r++] = a + b; break; }
|
|
2425
|
+
case ${opMap[OP_SUB]}: { var b = q[--r], a = q[--r]; q[r++] = a - b; break; }
|
|
2426
|
+
case ${opMap[OP_MUL]}: { var b = q[--r], a = q[--r]; q[r++] = a * b; break; }
|
|
2427
|
+
case ${opMap[OP_XOR]}: { var b = q[--r], a = q[--r]; q[r++] = a ^ b; break; }
|
|
2428
|
+
case ${opMap[OP_AND]}: { var b = q[--r], a = q[--r]; q[r++] = a & b; break; }
|
|
2429
|
+
case ${opMap[OP_OR]}: { var b = q[--r], a = q[--r]; q[r++] = a | b; break; }
|
|
2430
|
+
case ${opMap[OP_SEQ]}: { var b = q[--r], a = q[--r]; q[r++] = a === b; break; }
|
|
2431
|
+
case ${opMap[OP_LT]}: { var b = q[--r], a = q[--r]; q[r++] = a < b; break; }
|
|
2432
|
+
case ${opMap[OP_NOT]}: { q[r - 1] = !q[r - 1]; break; }
|
|
2433
|
+
case ${opMap[OP_JMP]}: { p = c[p]; break; }
|
|
2434
|
+
case ${opMap[OP_JZ]}: { var j = c[p++]; if (!q[--r]) p = j; break; }
|
|
2435
|
+
case ${opMap[OP_RET]}: return q[--r];
|
|
2436
|
+
case ${opMap[OP_THROW]}: throw q[--r];
|
|
2437
|
+
case ${opMap[OP_AWAIT]}: {
|
|
2438
|
+
var v = q[--r];
|
|
2439
|
+
return Promise.resolve(v).then(function(w) { q[r++] = w; return x(); });
|
|
2440
|
+
}
|
|
2441
|
+
case ${opMap[OP_OBJ]}: {
|
|
2442
|
+
var n = c[p++], o = {}, e = [];
|
|
2443
|
+
for (var i = 0; i < n; i++) { var v = q[--r], k0 = q[--r]; e.push([k0, v]); }
|
|
2444
|
+
for (var i = e.length - 1; i >= 0; i--) { o[e[i][0]] = e[i][1]; }
|
|
2445
|
+
q[r++] = o;
|
|
2446
|
+
break;
|
|
2447
|
+
}
|
|
2448
|
+
case ${opMap[OP_ARR]}: {
|
|
2449
|
+
var n = c[p++], a = [];
|
|
2450
|
+
for (var i = n - 1; i >= 0; i--) a[i] = q[--r];
|
|
2451
|
+
q[r++] = a;
|
|
2452
|
+
break;
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
return x();
|
|
2458
|
+
}`;
|
|
1213
2459
|
}
|
|
1214
|
-
function
|
|
1215
|
-
|
|
1216
|
-
|
|
2460
|
+
function countAstNodes(node) {
|
|
2461
|
+
let count = 0;
|
|
2462
|
+
walk5.full(node, () => {
|
|
2463
|
+
count++;
|
|
2464
|
+
});
|
|
2465
|
+
return count;
|
|
1217
2466
|
}
|
|
1218
|
-
function
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
2467
|
+
function hasForbiddenConstructs(fnNode) {
|
|
2468
|
+
let forbidden = false;
|
|
2469
|
+
walk5.full(fnNode, (node) => {
|
|
2470
|
+
if (forbidden) return;
|
|
2471
|
+
const type = node.type;
|
|
2472
|
+
if (type === "JSXElement" || type === "JSXFragment" || type === "Super" || type === "YieldExpression") {
|
|
2473
|
+
forbidden = true;
|
|
2474
|
+
return;
|
|
2475
|
+
}
|
|
2476
|
+
if (type === "Identifier" && node.name === "arguments") {
|
|
2477
|
+
forbidden = true;
|
|
2478
|
+
return;
|
|
2479
|
+
}
|
|
2480
|
+
if (type === "MetaProperty" && node.meta?.name === "new" && node.property?.name === "target") {
|
|
2481
|
+
forbidden = true;
|
|
2482
|
+
return;
|
|
2483
|
+
}
|
|
2484
|
+
});
|
|
2485
|
+
return forbidden;
|
|
2486
|
+
}
|
|
2487
|
+
function compileFunctionToBytecode(fnNode, opMap) {
|
|
2488
|
+
const bytecode = [];
|
|
2489
|
+
const consts = [];
|
|
2490
|
+
const localMap = /* @__PURE__ */ new Map();
|
|
2491
|
+
let localCount = 0;
|
|
2492
|
+
for (const param of fnNode.params) {
|
|
2493
|
+
if (param.type !== "Identifier") return null;
|
|
2494
|
+
localMap.set(param.name, localCount++);
|
|
2495
|
+
}
|
|
2496
|
+
const bodyNodes = fnNode.body.type === "BlockStatement" ? fnNode.body.body : [fnNode.body];
|
|
2497
|
+
for (const stmt of bodyNodes) {
|
|
2498
|
+
if (stmt.type === "VariableDeclaration") {
|
|
2499
|
+
for (const decl of stmt.declarations) {
|
|
2500
|
+
if (decl.id.type === "Identifier") {
|
|
2501
|
+
if (!localMap.has(decl.id.name)) {
|
|
2502
|
+
localMap.set(decl.id.name, localCount++);
|
|
2503
|
+
}
|
|
2504
|
+
} else {
|
|
2505
|
+
return null;
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
function addConst(val) {
|
|
2511
|
+
for (let i = 0; i < consts.length; i++) {
|
|
2512
|
+
if (consts[i] === val) return i;
|
|
2513
|
+
}
|
|
2514
|
+
const idx = consts.length;
|
|
2515
|
+
consts.push(val);
|
|
2516
|
+
return idx;
|
|
2517
|
+
}
|
|
2518
|
+
function emit(op, ...args) {
|
|
2519
|
+
bytecode.push(opMap[op]);
|
|
2520
|
+
for (const a of args) bytecode.push(a);
|
|
2521
|
+
}
|
|
2522
|
+
function compileExpr(node) {
|
|
2523
|
+
if (!node) return false;
|
|
2524
|
+
switch (node.type) {
|
|
2525
|
+
case "Literal": {
|
|
2526
|
+
const idx = addConst(node.value);
|
|
2527
|
+
emit(OP_LDC, idx);
|
|
2528
|
+
return true;
|
|
2529
|
+
}
|
|
2530
|
+
case "Identifier": {
|
|
2531
|
+
if (node.name === "undefined") {
|
|
2532
|
+
const idx = addConst(void 0);
|
|
2533
|
+
emit(OP_LDC, idx);
|
|
2534
|
+
} else if (localMap.has(node.name)) {
|
|
2535
|
+
emit(OP_LDV, localMap.get(node.name));
|
|
2536
|
+
} else {
|
|
2537
|
+
const idx = addConst(node.name);
|
|
2538
|
+
emit(OP_LDG, idx);
|
|
2539
|
+
}
|
|
2540
|
+
return true;
|
|
2541
|
+
}
|
|
2542
|
+
case "BinaryExpression": {
|
|
2543
|
+
if (node.operator === ">") {
|
|
2544
|
+
if (!compileExpr(node.right)) return false;
|
|
2545
|
+
if (!compileExpr(node.left)) return false;
|
|
2546
|
+
emit(OP_LT);
|
|
2547
|
+
return true;
|
|
2548
|
+
}
|
|
2549
|
+
if (node.operator === "<=") {
|
|
2550
|
+
if (!compileExpr(node.right)) return false;
|
|
2551
|
+
if (!compileExpr(node.left)) return false;
|
|
2552
|
+
emit(OP_LT);
|
|
2553
|
+
emit(OP_NOT);
|
|
2554
|
+
return true;
|
|
2555
|
+
}
|
|
2556
|
+
if (node.operator === ">=") {
|
|
2557
|
+
if (!compileExpr(node.left)) return false;
|
|
2558
|
+
if (!compileExpr(node.right)) return false;
|
|
2559
|
+
emit(OP_LT);
|
|
2560
|
+
emit(OP_NOT);
|
|
2561
|
+
return true;
|
|
2562
|
+
}
|
|
2563
|
+
if (!compileExpr(node.left)) return false;
|
|
2564
|
+
if (!compileExpr(node.right)) return false;
|
|
2565
|
+
switch (node.operator) {
|
|
2566
|
+
case "+":
|
|
2567
|
+
emit(OP_ADD);
|
|
2568
|
+
break;
|
|
2569
|
+
case "-":
|
|
2570
|
+
emit(OP_SUB);
|
|
2571
|
+
break;
|
|
2572
|
+
case "*":
|
|
2573
|
+
emit(OP_MUL);
|
|
2574
|
+
break;
|
|
2575
|
+
case "^":
|
|
2576
|
+
emit(OP_XOR);
|
|
2577
|
+
break;
|
|
2578
|
+
case "&":
|
|
2579
|
+
emit(OP_AND);
|
|
2580
|
+
break;
|
|
2581
|
+
case "|":
|
|
2582
|
+
emit(OP_OR);
|
|
2583
|
+
break;
|
|
2584
|
+
case "==":
|
|
2585
|
+
case "===":
|
|
2586
|
+
emit(OP_SEQ);
|
|
2587
|
+
break;
|
|
2588
|
+
case "!=":
|
|
2589
|
+
case "!==":
|
|
2590
|
+
emit(OP_SEQ);
|
|
2591
|
+
emit(OP_NOT);
|
|
2592
|
+
break;
|
|
2593
|
+
case "<":
|
|
2594
|
+
emit(OP_LT);
|
|
2595
|
+
break;
|
|
2596
|
+
default:
|
|
2597
|
+
return false;
|
|
2598
|
+
}
|
|
2599
|
+
return true;
|
|
2600
|
+
}
|
|
2601
|
+
case "UnaryExpression": {
|
|
2602
|
+
if (!compileExpr(node.argument)) return false;
|
|
2603
|
+
switch (node.operator) {
|
|
2604
|
+
case "!":
|
|
2605
|
+
emit(OP_NOT);
|
|
2606
|
+
break;
|
|
2607
|
+
case "-": {
|
|
2608
|
+
const neg1Idx = addConst(-1);
|
|
2609
|
+
emit(OP_LDC, neg1Idx);
|
|
2610
|
+
emit(OP_MUL);
|
|
2611
|
+
break;
|
|
2612
|
+
}
|
|
2613
|
+
case "+": {
|
|
2614
|
+
const zeroIdx = addConst(0);
|
|
2615
|
+
emit(OP_LDC, zeroIdx);
|
|
2616
|
+
emit(OP_ADD);
|
|
2617
|
+
break;
|
|
2618
|
+
}
|
|
2619
|
+
case "~": {
|
|
2620
|
+
const neg1Idx = addConst(-1);
|
|
2621
|
+
emit(OP_LDC, neg1Idx);
|
|
2622
|
+
emit(OP_XOR);
|
|
2623
|
+
break;
|
|
2624
|
+
}
|
|
2625
|
+
case "void": {
|
|
2626
|
+
const undefIdx2 = addConst(void 0);
|
|
2627
|
+
emit(OP_LDC, undefIdx2);
|
|
2628
|
+
break;
|
|
2629
|
+
}
|
|
2630
|
+
default:
|
|
2631
|
+
return false;
|
|
2632
|
+
}
|
|
2633
|
+
return true;
|
|
2634
|
+
}
|
|
2635
|
+
case "MemberExpression": {
|
|
2636
|
+
if (!compileExpr(node.object)) return false;
|
|
2637
|
+
if (node.computed) {
|
|
2638
|
+
if (!compileExpr(node.property)) return false;
|
|
2639
|
+
} else {
|
|
2640
|
+
const idx = addConst(node.property.name);
|
|
2641
|
+
emit(OP_LDC, idx);
|
|
2642
|
+
}
|
|
2643
|
+
emit(OP_GETP);
|
|
2644
|
+
return true;
|
|
2645
|
+
}
|
|
2646
|
+
case "CallExpression": {
|
|
2647
|
+
if (node.callee.type === "MemberExpression") {
|
|
2648
|
+
if (!compileExpr(node.callee.object)) return false;
|
|
2649
|
+
if (node.callee.computed) {
|
|
2650
|
+
if (!compileExpr(node.callee.property)) return false;
|
|
2651
|
+
} else {
|
|
2652
|
+
const idx = addConst(node.callee.property.name);
|
|
2653
|
+
emit(OP_LDC, idx);
|
|
2654
|
+
}
|
|
2655
|
+
for (const arg of node.arguments) {
|
|
2656
|
+
if (!compileExpr(arg)) return false;
|
|
2657
|
+
}
|
|
2658
|
+
emit(OP_MCALL, node.arguments.length);
|
|
2659
|
+
} else {
|
|
2660
|
+
if (!compileExpr(node.callee)) return false;
|
|
2661
|
+
for (const arg of node.arguments) {
|
|
2662
|
+
if (!compileExpr(arg)) return false;
|
|
2663
|
+
}
|
|
2664
|
+
emit(OP_CALL, node.arguments.length);
|
|
2665
|
+
}
|
|
2666
|
+
return true;
|
|
2667
|
+
}
|
|
2668
|
+
case "AssignmentExpression": {
|
|
2669
|
+
if (node.operator !== "=") return false;
|
|
2670
|
+
if (node.left.type === "Identifier") {
|
|
2671
|
+
if (!localMap.has(node.left.name)) return false;
|
|
2672
|
+
if (!compileExpr(node.right)) return false;
|
|
2673
|
+
emit(OP_STV, localMap.get(node.left.name));
|
|
2674
|
+
return true;
|
|
2675
|
+
}
|
|
2676
|
+
if (node.left.type === "MemberExpression") {
|
|
2677
|
+
if (!compileExpr(node.left.object)) return false;
|
|
2678
|
+
if (node.left.computed) {
|
|
2679
|
+
if (!compileExpr(node.left.property)) return false;
|
|
2680
|
+
} else {
|
|
2681
|
+
const idx = addConst(node.left.property.name);
|
|
2682
|
+
emit(OP_LDC, idx);
|
|
2683
|
+
}
|
|
2684
|
+
if (!compileExpr(node.right)) return false;
|
|
2685
|
+
emit(OP_SETP);
|
|
2686
|
+
return true;
|
|
2687
|
+
}
|
|
2688
|
+
return false;
|
|
2689
|
+
}
|
|
2690
|
+
case "ObjectExpression": {
|
|
2691
|
+
for (const prop of node.properties) {
|
|
2692
|
+
if (prop.type !== "Property") return false;
|
|
2693
|
+
if (prop.computed) {
|
|
2694
|
+
if (!compileExpr(prop.key)) return false;
|
|
2695
|
+
} else {
|
|
2696
|
+
const keyName = prop.key.name ?? prop.key.value;
|
|
2697
|
+
const idx = addConst(keyName);
|
|
2698
|
+
emit(OP_LDC, idx);
|
|
2699
|
+
}
|
|
2700
|
+
if (!compileExpr(prop.value)) return false;
|
|
2701
|
+
}
|
|
2702
|
+
emit(OP_OBJ, node.properties.length);
|
|
2703
|
+
return true;
|
|
2704
|
+
}
|
|
2705
|
+
case "ArrayExpression": {
|
|
2706
|
+
for (const elem of node.elements) {
|
|
2707
|
+
if (!compileExpr(elem)) return false;
|
|
2708
|
+
}
|
|
2709
|
+
emit(OP_ARR, node.elements.length);
|
|
2710
|
+
return true;
|
|
2711
|
+
}
|
|
2712
|
+
case "AwaitExpression": {
|
|
2713
|
+
if (!compileExpr(node.argument)) return false;
|
|
2714
|
+
emit(OP_AWAIT);
|
|
2715
|
+
return true;
|
|
2716
|
+
}
|
|
2717
|
+
default:
|
|
2718
|
+
return false;
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
function compileStmt(stmt) {
|
|
2722
|
+
if (!stmt) return false;
|
|
2723
|
+
switch (stmt.type) {
|
|
2724
|
+
case "ReturnStatement": {
|
|
2725
|
+
if (stmt.argument) {
|
|
2726
|
+
if (!compileExpr(stmt.argument)) return false;
|
|
2727
|
+
} else {
|
|
2728
|
+
const idx = addConst(void 0);
|
|
2729
|
+
emit(OP_LDC, idx);
|
|
2730
|
+
}
|
|
2731
|
+
emit(OP_RET);
|
|
2732
|
+
return true;
|
|
2733
|
+
}
|
|
2734
|
+
case "ExpressionStatement": {
|
|
2735
|
+
if (!compileExpr(stmt.expression)) return false;
|
|
2736
|
+
return true;
|
|
2737
|
+
}
|
|
2738
|
+
case "VariableDeclaration": {
|
|
2739
|
+
for (const decl of stmt.declarations) {
|
|
2740
|
+
if (decl.init) {
|
|
2741
|
+
if (!compileExpr(decl.init)) return false;
|
|
2742
|
+
emit(OP_STV, localMap.get(decl.id.name));
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
return true;
|
|
2746
|
+
}
|
|
2747
|
+
case "IfStatement": {
|
|
2748
|
+
if (!compileExpr(stmt.test)) return false;
|
|
2749
|
+
const jzPos = bytecode.length;
|
|
2750
|
+
emit(OP_JZ, 0);
|
|
2751
|
+
if (!compileStmt(stmt.consequent)) return false;
|
|
2752
|
+
if (stmt.alternate) {
|
|
2753
|
+
const jmpPos = bytecode.length;
|
|
2754
|
+
emit(OP_JMP, 0);
|
|
2755
|
+
bytecode[jzPos + 1] = bytecode.length;
|
|
2756
|
+
if (!compileStmt(stmt.alternate)) return false;
|
|
2757
|
+
bytecode[jmpPos + 1] = bytecode.length;
|
|
2758
|
+
} else {
|
|
2759
|
+
bytecode[jzPos + 1] = bytecode.length;
|
|
2760
|
+
}
|
|
2761
|
+
return true;
|
|
2762
|
+
}
|
|
2763
|
+
case "BlockStatement": {
|
|
2764
|
+
for (const s of stmt.body) {
|
|
2765
|
+
if (!compileStmt(s)) return false;
|
|
2766
|
+
}
|
|
2767
|
+
return true;
|
|
2768
|
+
}
|
|
2769
|
+
default:
|
|
2770
|
+
return false;
|
|
2771
|
+
}
|
|
2772
|
+
}
|
|
2773
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
2774
|
+
if (!compileStmt(fnNode.body)) return null;
|
|
2775
|
+
} else {
|
|
2776
|
+
if (!compileExpr(fnNode.body)) return null;
|
|
2777
|
+
emit(OP_RET);
|
|
2778
|
+
}
|
|
2779
|
+
const undefIdx = addConst(void 0);
|
|
2780
|
+
emit(OP_LDC, undefIdx);
|
|
2781
|
+
emit(OP_RET);
|
|
2782
|
+
return { bytecode, consts, localCount };
|
|
2783
|
+
}
|
|
2784
|
+
function isMicroVmAlreadyInjected(ast) {
|
|
2785
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
2786
|
+
return ast.body.some((stmt) => {
|
|
2787
|
+
if (stmt.type === "FunctionDeclaration" && typeof stmt.id?.name === "string" && stmt.id.name.startsWith("_uVm")) {
|
|
2788
|
+
return true;
|
|
2789
|
+
}
|
|
2790
|
+
return false;
|
|
2791
|
+
});
|
|
2792
|
+
}
|
|
2793
|
+
function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 2654435769) {
|
|
2794
|
+
const suffix = computeFnv1aHash(`micro-vm:${seed}`, seed).toString(36);
|
|
2795
|
+
const vmHelperName = `_uVm${suffix}`;
|
|
2796
|
+
if (isMicroVmAlreadyInjected(ast)) {
|
|
2797
|
+
const existing = ast.body.find(
|
|
2798
|
+
(stmt) => stmt.type === "FunctionDeclaration" && typeof stmt.id?.name === "string" && stmt.id.name.startsWith("_uVm")
|
|
2799
|
+
);
|
|
2800
|
+
return {
|
|
2801
|
+
virtualizedCount: 0,
|
|
2802
|
+
vmHelperName: existing?.id?.name ?? vmHelperName
|
|
2803
|
+
};
|
|
2804
|
+
}
|
|
2805
|
+
const opMap = generateOpcodeMap(seed);
|
|
2806
|
+
const candidates = [];
|
|
2807
|
+
walk5.simple(ast, {
|
|
2808
|
+
FunctionDeclaration(node) {
|
|
2809
|
+
candidates.push(node);
|
|
2810
|
+
},
|
|
2811
|
+
FunctionExpression(node) {
|
|
2812
|
+
candidates.push(node);
|
|
2813
|
+
},
|
|
2814
|
+
ArrowFunctionExpression(node) {
|
|
2815
|
+
candidates.push(node);
|
|
2816
|
+
}
|
|
2817
|
+
});
|
|
2818
|
+
let virtualizedCount = 0;
|
|
2819
|
+
for (const fn of candidates) {
|
|
2820
|
+
if (virtualizedCount >= 10) break;
|
|
2821
|
+
if (coldFunctionNodes && !coldFunctionNodes.has(fn)) {
|
|
2822
|
+
continue;
|
|
2823
|
+
}
|
|
2824
|
+
if (serializedFunctionNodes && serializedFunctionNodes.has(fn)) {
|
|
2825
|
+
continue;
|
|
2826
|
+
}
|
|
2827
|
+
if (fn.generator) {
|
|
2828
|
+
continue;
|
|
2829
|
+
}
|
|
2830
|
+
if (hasForbiddenConstructs(fn)) {
|
|
2831
|
+
continue;
|
|
2832
|
+
}
|
|
2833
|
+
const nodeCount = countAstNodes(fn);
|
|
2834
|
+
if (nodeCount < 15 || nodeCount > 80) {
|
|
2835
|
+
continue;
|
|
2836
|
+
}
|
|
2837
|
+
const compiled = compileFunctionToBytecode(fn, opMap);
|
|
2838
|
+
if (!compiled) {
|
|
2839
|
+
continue;
|
|
2840
|
+
}
|
|
2841
|
+
const paramArgs = fn.params.map((p) => ({
|
|
2842
|
+
type: "Identifier",
|
|
2843
|
+
name: p.name
|
|
2844
|
+
}));
|
|
2845
|
+
const callExpr = {
|
|
2846
|
+
type: "CallExpression",
|
|
2847
|
+
callee: {
|
|
2848
|
+
type: "Identifier",
|
|
2849
|
+
name: vmHelperName
|
|
2850
|
+
},
|
|
2851
|
+
arguments: [
|
|
2852
|
+
{
|
|
2853
|
+
type: "ArrayExpression",
|
|
2854
|
+
elements: compiled.bytecode.map((b) => ({
|
|
2855
|
+
type: "Literal",
|
|
2856
|
+
value: b,
|
|
2857
|
+
raw: String(b)
|
|
2858
|
+
}))
|
|
2859
|
+
},
|
|
2860
|
+
{
|
|
2861
|
+
type: "ArrayExpression",
|
|
2862
|
+
elements: compiled.consts.map((c) => ({
|
|
2863
|
+
type: "Literal",
|
|
2864
|
+
value: c,
|
|
2865
|
+
raw: c === void 0 ? "void 0" : JSON.stringify(c)
|
|
2866
|
+
}))
|
|
2867
|
+
},
|
|
2868
|
+
{
|
|
2869
|
+
type: "ArrayExpression",
|
|
2870
|
+
elements: paramArgs
|
|
2871
|
+
},
|
|
2872
|
+
{
|
|
2873
|
+
type: "ThisExpression"
|
|
2874
|
+
}
|
|
2875
|
+
],
|
|
2876
|
+
optional: false
|
|
2877
|
+
};
|
|
2878
|
+
if (fn.body.type === "BlockStatement") {
|
|
2879
|
+
fn.body.body = [
|
|
2880
|
+
{
|
|
2881
|
+
type: "ReturnStatement",
|
|
2882
|
+
argument: callExpr
|
|
2883
|
+
}
|
|
2884
|
+
];
|
|
2885
|
+
} else {
|
|
2886
|
+
fn.body = callExpr;
|
|
2887
|
+
}
|
|
2888
|
+
virtualizedCount++;
|
|
2889
|
+
}
|
|
2890
|
+
if (virtualizedCount > 0) {
|
|
2891
|
+
const interpreterSource = createInterpreterSource(vmHelperName, opMap);
|
|
2892
|
+
const parsedHelper = acorn3.parse(interpreterSource, {
|
|
2893
|
+
ecmaVersion: "latest",
|
|
2894
|
+
sourceType: "script"
|
|
2895
|
+
});
|
|
2896
|
+
ast.body.unshift(...parsedHelper.body);
|
|
2897
|
+
}
|
|
2898
|
+
return {
|
|
2899
|
+
virtualizedCount,
|
|
2900
|
+
vmHelperName
|
|
2901
|
+
};
|
|
1233
2902
|
}
|
|
1234
2903
|
|
|
1235
|
-
// src/
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
const
|
|
1239
|
-
|
|
2904
|
+
// src/ast/wasm-vault.ts
|
|
2905
|
+
import * as acorn4 from "acorn";
|
|
2906
|
+
function encodeSignedLeb128(value) {
|
|
2907
|
+
const bytes = [];
|
|
2908
|
+
let val = value | 0;
|
|
2909
|
+
let more = true;
|
|
2910
|
+
while (more) {
|
|
2911
|
+
let byte = val & 127;
|
|
2912
|
+
val >>= 7;
|
|
2913
|
+
if (val === 0 && (byte & 64) === 0 || val === -1 && (byte & 64) !== 0) {
|
|
2914
|
+
more = false;
|
|
2915
|
+
} else {
|
|
2916
|
+
byte |= 128;
|
|
2917
|
+
}
|
|
2918
|
+
bytes.push(byte);
|
|
2919
|
+
}
|
|
2920
|
+
return bytes;
|
|
2921
|
+
}
|
|
2922
|
+
function encodeUnsignedLeb128(value) {
|
|
2923
|
+
const bytes = [];
|
|
2924
|
+
let val = value >>> 0;
|
|
2925
|
+
do {
|
|
2926
|
+
let byte = val & 127;
|
|
2927
|
+
val >>>= 7;
|
|
2928
|
+
if (val !== 0) {
|
|
2929
|
+
byte |= 128;
|
|
2930
|
+
}
|
|
2931
|
+
bytes.push(byte);
|
|
2932
|
+
} while (val !== 0);
|
|
2933
|
+
return bytes;
|
|
2934
|
+
}
|
|
2935
|
+
function generateWasmKdfBinary(seed) {
|
|
2936
|
+
const instructions = [
|
|
2937
|
+
32,
|
|
2938
|
+
0,
|
|
2939
|
+
// local.get 0
|
|
2940
|
+
65,
|
|
2941
|
+
...encodeSignedLeb128(seed),
|
|
2942
|
+
// i32.const seed
|
|
2943
|
+
115,
|
|
2944
|
+
// i32.xor
|
|
2945
|
+
65,
|
|
2946
|
+
...encodeSignedLeb128(16777619),
|
|
2947
|
+
// i32.const 0x01000193
|
|
2948
|
+
108,
|
|
2949
|
+
// i32.mul
|
|
2950
|
+
32,
|
|
2951
|
+
1,
|
|
2952
|
+
// local.get 1
|
|
2953
|
+
32,
|
|
2954
|
+
2,
|
|
2955
|
+
// local.get 2
|
|
2956
|
+
106,
|
|
2957
|
+
// i32.add
|
|
2958
|
+
65,
|
|
2959
|
+
...encodeSignedLeb128(73244475),
|
|
2960
|
+
// i32.const 0x45d9f3b
|
|
2961
|
+
108,
|
|
2962
|
+
// i32.mul
|
|
2963
|
+
115,
|
|
2964
|
+
// i32.xor
|
|
2965
|
+
11
|
|
2966
|
+
// end
|
|
2967
|
+
];
|
|
2968
|
+
const funcBody = [0, ...instructions];
|
|
2969
|
+
const funcBodyWithLen = [...encodeUnsignedLeb128(funcBody.length), ...funcBody];
|
|
2970
|
+
const codeSectionPayload = [1, ...funcBodyWithLen];
|
|
2971
|
+
const codeSection = [
|
|
2972
|
+
10,
|
|
2973
|
+
...encodeUnsignedLeb128(codeSectionPayload.length),
|
|
2974
|
+
...codeSectionPayload
|
|
2975
|
+
];
|
|
2976
|
+
const typePayload = [
|
|
2977
|
+
1,
|
|
2978
|
+
// 1 type
|
|
2979
|
+
96,
|
|
2980
|
+
// func
|
|
2981
|
+
3,
|
|
2982
|
+
127,
|
|
2983
|
+
127,
|
|
2984
|
+
127,
|
|
2985
|
+
// 3 params of type i32
|
|
2986
|
+
1,
|
|
2987
|
+
127
|
|
2988
|
+
// 1 result of type i32
|
|
2989
|
+
];
|
|
2990
|
+
const typeSection = [
|
|
2991
|
+
1,
|
|
2992
|
+
...encodeUnsignedLeb128(typePayload.length),
|
|
2993
|
+
...typePayload
|
|
2994
|
+
];
|
|
2995
|
+
const funcPayload = [
|
|
2996
|
+
1,
|
|
2997
|
+
// 1 function
|
|
2998
|
+
0
|
|
2999
|
+
// type index 0
|
|
3000
|
+
];
|
|
3001
|
+
const funcSection = [
|
|
3002
|
+
3,
|
|
3003
|
+
...encodeUnsignedLeb128(funcPayload.length),
|
|
3004
|
+
...funcPayload
|
|
3005
|
+
];
|
|
3006
|
+
const exportName = [3, 107, 100, 102];
|
|
3007
|
+
const exportPayload = [
|
|
3008
|
+
1,
|
|
3009
|
+
// 1 export
|
|
3010
|
+
...exportName,
|
|
3011
|
+
0,
|
|
3012
|
+
// function export
|
|
3013
|
+
0
|
|
3014
|
+
// function index 0
|
|
3015
|
+
];
|
|
3016
|
+
const exportSection = [
|
|
3017
|
+
7,
|
|
3018
|
+
...encodeUnsignedLeb128(exportPayload.length),
|
|
3019
|
+
...exportPayload
|
|
3020
|
+
];
|
|
3021
|
+
const header = [0, 97, 115, 109, 1, 0, 0, 0];
|
|
3022
|
+
return new Uint8Array([
|
|
3023
|
+
...header,
|
|
3024
|
+
...typeSection,
|
|
3025
|
+
...funcSection,
|
|
3026
|
+
...exportSection,
|
|
3027
|
+
...codeSection
|
|
3028
|
+
]);
|
|
3029
|
+
}
|
|
3030
|
+
function fallbackKdf(fnId, callSiteSalt, shieldHash, seed) {
|
|
3031
|
+
return Math.imul(fnId ^ seed | 0, 16777619) ^ Math.imul(callSiteSalt + shieldHash | 0, 73244475) | 0;
|
|
3032
|
+
}
|
|
3033
|
+
function isWasmVaultAlreadyInjected(ast) {
|
|
3034
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
3035
|
+
return ast.body.some((stmt) => {
|
|
3036
|
+
if (stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
3037
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_wKdf")
|
|
3038
|
+
)) {
|
|
3039
|
+
return true;
|
|
3040
|
+
}
|
|
3041
|
+
return false;
|
|
3042
|
+
});
|
|
3043
|
+
}
|
|
3044
|
+
function injectWasmVault(ast, seed = 2654435769) {
|
|
3045
|
+
const suffix = computeFnv1aHash(`wasm:${seed}`, seed).toString(36);
|
|
3046
|
+
const kdfFnName = `_wKdf${suffix}`;
|
|
3047
|
+
const wasmVarName = `_wb${suffix}`;
|
|
3048
|
+
if (isWasmVaultAlreadyInjected(ast)) {
|
|
3049
|
+
const existing = ast.body.find(
|
|
3050
|
+
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
3051
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_wKdf")
|
|
3052
|
+
)
|
|
3053
|
+
);
|
|
3054
|
+
const id = existing?.declarations[0]?.id?.name ?? kdfFnName;
|
|
3055
|
+
return {
|
|
3056
|
+
kdfFnName: id,
|
|
3057
|
+
wasmBytes: generateWasmKdfBinary(seed),
|
|
3058
|
+
wasmVarName
|
|
3059
|
+
};
|
|
3060
|
+
}
|
|
3061
|
+
const wasmBytes = generateWasmKdfBinary(seed);
|
|
3062
|
+
const byteArrayLiteral = Array.from(wasmBytes).join(",");
|
|
3063
|
+
const helperSnippet = `var ${kdfFnName};
|
|
3064
|
+
try {
|
|
3065
|
+
var ${wasmVarName} = new Uint8Array([${byteArrayLiteral}]);
|
|
3066
|
+
var _wm${suffix} = new WebAssembly.Module(${wasmVarName});
|
|
3067
|
+
var _wi${suffix} = new WebAssembly.Instance(_wm${suffix});
|
|
3068
|
+
${kdfFnName} = _wi${suffix}.exports.kdf;
|
|
3069
|
+
} catch (e) {
|
|
3070
|
+
${kdfFnName} = function(f, s, h) {
|
|
3071
|
+
return (Math.imul((f ^ ${seed | 0}) | 0, 16777619) ^ Math.imul(((s + h) | 0), 73244475)) | 0;
|
|
3072
|
+
};
|
|
3073
|
+
}`;
|
|
3074
|
+
const parsedHelper = acorn4.parse(helperSnippet, {
|
|
3075
|
+
ecmaVersion: "latest",
|
|
3076
|
+
sourceType: "script"
|
|
3077
|
+
});
|
|
3078
|
+
ast.body.unshift(...parsedHelper.body);
|
|
1240
3079
|
return {
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
3080
|
+
kdfFnName,
|
|
3081
|
+
wasmBytes,
|
|
3082
|
+
wasmVarName
|
|
1244
3083
|
};
|
|
1245
3084
|
}
|
|
1246
3085
|
|
|
1247
|
-
// src/ast/
|
|
1248
|
-
|
|
3086
|
+
// src/ast/salted-vault.ts
|
|
3087
|
+
import * as acorn5 from "acorn";
|
|
3088
|
+
import * as walk6 from "acorn-walk";
|
|
3089
|
+
var DIRECTIVE_STRINGS2 = /* @__PURE__ */ new Set([
|
|
3090
|
+
"use strict",
|
|
3091
|
+
"use client",
|
|
3092
|
+
"use server",
|
|
3093
|
+
"use asm"
|
|
3094
|
+
]);
|
|
3095
|
+
function encodeSaltedString(str, k) {
|
|
3096
|
+
let out = "";
|
|
3097
|
+
for (let j = 0; j < str.length; j++) {
|
|
3098
|
+
const code = str.charCodeAt(j);
|
|
3099
|
+
const mask = k >>> j % 24 & 127 | 1;
|
|
3100
|
+
out += String.fromCharCode(code ^ mask);
|
|
3101
|
+
}
|
|
3102
|
+
return out;
|
|
3103
|
+
}
|
|
3104
|
+
function isAstAlreadySalted(ast) {
|
|
1249
3105
|
if (!ast || !Array.isArray(ast.body)) return false;
|
|
1250
3106
|
return ast.body.some((stmt) => {
|
|
1251
3107
|
if (stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
1252
|
-
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("
|
|
3108
|
+
(d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uP")
|
|
1253
3109
|
)) {
|
|
1254
3110
|
return true;
|
|
1255
3111
|
}
|
|
1256
|
-
|
|
3112
|
+
return false;
|
|
3113
|
+
});
|
|
3114
|
+
}
|
|
3115
|
+
function replaceNodeInParent(parent, oldNode, newNode) {
|
|
3116
|
+
if (!parent) return false;
|
|
3117
|
+
for (const key of Object.keys(parent)) {
|
|
3118
|
+
const val = parent[key];
|
|
3119
|
+
if (val === oldNode) {
|
|
3120
|
+
parent[key] = newNode;
|
|
1257
3121
|
return true;
|
|
1258
3122
|
}
|
|
3123
|
+
if (Array.isArray(val)) {
|
|
3124
|
+
const idx = val.indexOf(oldNode);
|
|
3125
|
+
if (idx !== -1) {
|
|
3126
|
+
val[idx] = newNode;
|
|
3127
|
+
return true;
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
return false;
|
|
3132
|
+
}
|
|
3133
|
+
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes) {
|
|
3134
|
+
if (isAstAlreadySalted(ast)) {
|
|
3135
|
+
return {
|
|
3136
|
+
encryptedStringsCount: 0,
|
|
3137
|
+
integrityToken: computeFnv1aHash("already-salted", seed),
|
|
3138
|
+
vaultVarName: "",
|
|
3139
|
+
decryptFnName: ""
|
|
3140
|
+
};
|
|
3141
|
+
}
|
|
3142
|
+
const targetsToReplace = [];
|
|
3143
|
+
const encryptedPayloads = [];
|
|
3144
|
+
walk6.ancestor(ast, {
|
|
3145
|
+
Literal(node, ancestors) {
|
|
3146
|
+
if (typeof node.value !== "string") return;
|
|
3147
|
+
if (node.value.length < minStringLength) return;
|
|
3148
|
+
if (DIRECTIVE_STRINGS2.has(node.value)) return;
|
|
3149
|
+
const parent = ancestors[ancestors.length - 2];
|
|
3150
|
+
if (!parent) return;
|
|
3151
|
+
if ((parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration") && parent.source === node) {
|
|
3152
|
+
return;
|
|
3153
|
+
}
|
|
3154
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
3155
|
+
return;
|
|
3156
|
+
}
|
|
3157
|
+
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
3158
|
+
return;
|
|
3159
|
+
}
|
|
3160
|
+
if (hotExemptNodes) {
|
|
3161
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
3162
|
+
if (hotExemptNodes.has(ancestors[i])) {
|
|
3163
|
+
return;
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
const callSiteIdx = targetsToReplace.length;
|
|
3168
|
+
const localSalt = computeFnv1aHash(`salt:${seed}:${callSiteIdx}`, seed) & 2147483647 | 1;
|
|
3169
|
+
targetsToReplace.push({
|
|
3170
|
+
node,
|
|
3171
|
+
parent,
|
|
3172
|
+
callSiteIdx,
|
|
3173
|
+
localSalt,
|
|
3174
|
+
rawString: node.value
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
});
|
|
3178
|
+
if (targetsToReplace.length === 0) {
|
|
3179
|
+
return {
|
|
3180
|
+
encryptedStringsCount: 0,
|
|
3181
|
+
integrityToken: computeFnv1aHash("empty-salted", seed),
|
|
3182
|
+
vaultVarName: "",
|
|
3183
|
+
decryptFnName: ""
|
|
3184
|
+
};
|
|
3185
|
+
}
|
|
3186
|
+
const wasmResult = injectWasmVault(ast, seed);
|
|
3187
|
+
const kdfFnName = wasmResult.kdfFnName;
|
|
3188
|
+
const suffix = computeFnv1aHash(`salted:${seed}`, seed).toString(36);
|
|
3189
|
+
const vaultVarName = `_uP${suffix}`;
|
|
3190
|
+
const cacheVarName = `_uCache${suffix}`;
|
|
3191
|
+
const decryptFnName = `_uD${suffix}`;
|
|
3192
|
+
for (const target of targetsToReplace) {
|
|
3193
|
+
const k = fallbackKdf(target.callSiteIdx, target.localSalt, 0, seed);
|
|
3194
|
+
encryptedPayloads.push(encodeSaltedString(target.rawString, k));
|
|
3195
|
+
const callExpr = {
|
|
3196
|
+
type: "CallExpression",
|
|
3197
|
+
callee: {
|
|
3198
|
+
type: "Identifier",
|
|
3199
|
+
name: decryptFnName
|
|
3200
|
+
},
|
|
3201
|
+
arguments: [
|
|
3202
|
+
{
|
|
3203
|
+
type: "Literal",
|
|
3204
|
+
value: target.callSiteIdx,
|
|
3205
|
+
raw: String(target.callSiteIdx)
|
|
3206
|
+
},
|
|
3207
|
+
{
|
|
3208
|
+
type: "Literal",
|
|
3209
|
+
value: target.localSalt,
|
|
3210
|
+
raw: String(target.localSalt)
|
|
3211
|
+
}
|
|
3212
|
+
],
|
|
3213
|
+
optional: false
|
|
3214
|
+
};
|
|
3215
|
+
replaceNodeInParent(target.parent, target.node, callExpr);
|
|
3216
|
+
}
|
|
3217
|
+
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
3218
|
+
const integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
3219
|
+
const helperSnippet = `var ${vaultVarName} = ${serializedTable};
|
|
3220
|
+
var ${cacheVarName} = [];
|
|
3221
|
+
function ${decryptFnName}(i, s) {
|
|
3222
|
+
if (${cacheVarName}[i] !== void 0) return ${cacheVarName}[i];
|
|
3223
|
+
var d = typeof window !== 'undefined' && window.__unpeek_delta != null ? window.__unpeek_delta : 0;
|
|
3224
|
+
var k = ${kdfFnName}(i, s, d);
|
|
3225
|
+
var p = ${vaultVarName}[i], o = '';
|
|
3226
|
+
for (var j = 0; j < p.length; j++) {
|
|
3227
|
+
o += String.fromCharCode(p.charCodeAt(j) ^ (((k >>> (j % 24)) & 127) | 1));
|
|
3228
|
+
}
|
|
3229
|
+
return (${cacheVarName}[i] = o);
|
|
3230
|
+
}`;
|
|
3231
|
+
const parsedHelper = acorn5.parse(helperSnippet, {
|
|
3232
|
+
ecmaVersion: "latest",
|
|
3233
|
+
sourceType: "script"
|
|
3234
|
+
});
|
|
3235
|
+
ast.body.unshift(...parsedHelper.body);
|
|
3236
|
+
return {
|
|
3237
|
+
encryptedStringsCount: targetsToReplace.length,
|
|
3238
|
+
integrityToken,
|
|
3239
|
+
vaultVarName,
|
|
3240
|
+
decryptFnName
|
|
3241
|
+
};
|
|
3242
|
+
}
|
|
3243
|
+
|
|
3244
|
+
// src/ast/index.ts
|
|
3245
|
+
function isAstAlreadyProtected(ast) {
|
|
3246
|
+
if (!ast || !Array.isArray(ast.body)) return false;
|
|
3247
|
+
return ast.body.some((stmt) => {
|
|
3248
|
+
if (stmt.type === "VariableDeclaration" && Array.isArray(stmt.declarations)) {
|
|
3249
|
+
for (const d of stmt.declarations) {
|
|
3250
|
+
const name = d.id?.name;
|
|
3251
|
+
if (typeof name === "string") {
|
|
3252
|
+
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") {
|
|
3253
|
+
return true;
|
|
3254
|
+
}
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
}
|
|
3258
|
+
if (stmt.type === "ExpressionStatement" && stmt.expression?.type === "CallExpression") {
|
|
3259
|
+
const callee = stmt.expression.callee;
|
|
3260
|
+
if (callee?.type === "FunctionExpression") {
|
|
3261
|
+
const name = callee.id?.name;
|
|
3262
|
+
if (typeof name === "string" && (name.startsWith("_u$") || name.startsWith("_$") || name.startsWith("_s") || name.startsWith("$b"))) {
|
|
3263
|
+
return true;
|
|
3264
|
+
}
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
1259
3267
|
return false;
|
|
1260
3268
|
});
|
|
1261
3269
|
}
|
|
1262
3270
|
function transformJavaScript(code, options = {}) {
|
|
1263
3271
|
const seed = options.seed ?? 2654435769;
|
|
1264
3272
|
const minStringLength = options.minStringLength ?? 4;
|
|
3273
|
+
const isAggressive = options.aggressive ?? false;
|
|
1265
3274
|
if (!code || !code.trim()) {
|
|
1266
3275
|
return {
|
|
1267
3276
|
code,
|
|
@@ -1272,7 +3281,9 @@ function transformJavaScript(code, options = {}) {
|
|
|
1272
3281
|
hotFunctionsSkipped: [],
|
|
1273
3282
|
coldFunctionsProtected: []
|
|
1274
3283
|
},
|
|
1275
|
-
integrityToken: computeFnv1aHash("", seed)
|
|
3284
|
+
integrityToken: computeFnv1aHash("", seed),
|
|
3285
|
+
virtualizedFunctionsCount: 0,
|
|
3286
|
+
shaderBuffersCount: 0
|
|
1276
3287
|
};
|
|
1277
3288
|
}
|
|
1278
3289
|
const ast = parseJavaScriptAst(code);
|
|
@@ -1286,23 +3297,59 @@ function transformJavaScript(code, options = {}) {
|
|
|
1286
3297
|
hotFunctionsSkipped: [],
|
|
1287
3298
|
coldFunctionsProtected: []
|
|
1288
3299
|
},
|
|
1289
|
-
integrityToken: computeFnv1aHash(code, seed)
|
|
3300
|
+
integrityToken: computeFnv1aHash(code, seed),
|
|
3301
|
+
virtualizedFunctionsCount: 0,
|
|
3302
|
+
shaderBuffersCount: 0
|
|
1290
3303
|
};
|
|
1291
3304
|
}
|
|
1292
3305
|
const {
|
|
1293
3306
|
report: hotPathReport,
|
|
1294
3307
|
coldFunctionNodes,
|
|
1295
|
-
|
|
3308
|
+
hotFunctionNodes,
|
|
3309
|
+
hotExemptNodes,
|
|
3310
|
+
serializedFunctionNodes
|
|
1296
3311
|
} = analyzeAstHotPaths(ast);
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
3312
|
+
let virtualizedFunctionsCount = 0;
|
|
3313
|
+
if (isAggressive) {
|
|
3314
|
+
const vmRes = applyMicroVm(
|
|
3315
|
+
ast,
|
|
3316
|
+
coldFunctionNodes,
|
|
3317
|
+
serializedFunctionNodes,
|
|
3318
|
+
seed
|
|
3319
|
+
);
|
|
3320
|
+
virtualizedFunctionsCount = vmRes.virtualizedCount;
|
|
3321
|
+
}
|
|
3322
|
+
applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
|
|
3323
|
+
const mangledSymbolsCount = applyPolymorphicMangler(
|
|
1300
3324
|
ast,
|
|
1301
3325
|
seed,
|
|
1302
|
-
|
|
1303
|
-
hotExemptNodes
|
|
3326
|
+
serializedFunctionNodes
|
|
1304
3327
|
);
|
|
1305
|
-
|
|
3328
|
+
let encryptedStringsCount = 0;
|
|
3329
|
+
let integrityToken = 0;
|
|
3330
|
+
let shaderBuffersCount = 0;
|
|
3331
|
+
if (isAggressive) {
|
|
3332
|
+
injectWasmVault(ast, seed);
|
|
3333
|
+
const saltedRes = applySaltedStringVault(
|
|
3334
|
+
ast,
|
|
3335
|
+
seed,
|
|
3336
|
+
minStringLength,
|
|
3337
|
+
hotExemptNodes
|
|
3338
|
+
);
|
|
3339
|
+
encryptedStringsCount = saltedRes.encryptedStringsCount;
|
|
3340
|
+
integrityToken = saltedRes.integrityToken;
|
|
3341
|
+
} else {
|
|
3342
|
+
const vaultRes = applyStringVault(
|
|
3343
|
+
ast,
|
|
3344
|
+
seed,
|
|
3345
|
+
minStringLength,
|
|
3346
|
+
hotExemptNodes
|
|
3347
|
+
);
|
|
3348
|
+
encryptedStringsCount = vaultRes.encryptedStringsCount;
|
|
3349
|
+
integrityToken = vaultRes.integrityToken;
|
|
3350
|
+
shaderBuffersCount = vaultRes.shaderBuffersCount ?? 0;
|
|
3351
|
+
}
|
|
3352
|
+
const outputCode = generateCompactJs(ast);
|
|
1306
3353
|
let finalCode = outputCode;
|
|
1307
3354
|
if (options.injectRuntimeShield) {
|
|
1308
3355
|
const shield = generateRuntimeShield({
|
|
@@ -1320,12 +3367,23 @@ ${finalCode}`;
|
|
|
1320
3367
|
encryptedStringsCount,
|
|
1321
3368
|
mangledSymbolsCount,
|
|
1322
3369
|
hotPathReport,
|
|
1323
|
-
integrityToken
|
|
3370
|
+
integrityToken,
|
|
3371
|
+
virtualizedFunctionsCount,
|
|
3372
|
+
shaderBuffersCount
|
|
1324
3373
|
};
|
|
1325
3374
|
}
|
|
1326
3375
|
export {
|
|
3376
|
+
analyzeAstHotPaths,
|
|
3377
|
+
applyColdFlowGuard,
|
|
3378
|
+
applyMicroVm,
|
|
3379
|
+
applyPolymorphicMangler,
|
|
3380
|
+
applySaltedStringVault,
|
|
3381
|
+
applyStringVault,
|
|
1327
3382
|
computeFnv1aHash,
|
|
3383
|
+
derivePolymorphicIdent,
|
|
1328
3384
|
detectHotPaths,
|
|
3385
|
+
generateCompactJs,
|
|
3386
|
+
injectWasmVault,
|
|
1329
3387
|
transformJavaScript
|
|
1330
3388
|
};
|
|
1331
3389
|
//# sourceMappingURL=index.js.map
|