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