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