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