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