unpeek 1.6.0 → 1.6.1

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.
@@ -344,7 +344,7 @@ var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
344
344
  "use asm"
345
345
  ]);
346
346
  var HELPER_TEMPLATE = acorn2.parse(
347
- `const _ = [].map(function f(a) {
347
+ `var _ = [].map(function f(a) {
348
348
  var s = "";
349
349
  for (var j = 0; j < a.length; j++) {
350
350
  s += String.fromCharCode(a.charCodeAt(j) ^ (((0 ^ ((a._u || /\\n/.test(f) * 14) | ((j & 1) * 0))) & 30) | 1));
@@ -390,8 +390,17 @@ function encodeStringWithSeed(str, mask, step) {
390
390
  return out;
391
391
  }
392
392
  function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes) {
393
- const suffix = ((seed >>> 0) % 255).toString(36);
394
- const cacheVarName = `_uC${suffix}`;
393
+ const alreadyVaulted = ast.body.some(
394
+ (stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
395
+ (d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uC")
396
+ )
397
+ );
398
+ if (alreadyVaulted) {
399
+ return {
400
+ encryptedStringsCount: 0,
401
+ integrityToken: computeFnv1aHash("already-vaulted", seed)
402
+ };
403
+ }
395
404
  const mask = seed >>> 0 & 30 | 3;
396
405
  const step = (seed >>> 5 & 7) * 2 + 3;
397
406
  const stringToIndex = /* @__PURE__ */ new Map();
@@ -437,6 +446,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
437
446
  integrityToken: computeFnv1aHash("empty", seed)
438
447
  };
439
448
  }
449
+ const serializedTable = JSON.stringify(encryptedPayloads);
450
+ const integrityToken = computeFnv1aHash(serializedTable, seed);
451
+ const suffix = computeFnv1aHash(`vault:${seed}`, integrityToken).toString(36);
452
+ const cacheVarName = `_uC${suffix}`;
440
453
  for (const { node } of targetsToReplace) {
441
454
  const idx = stringToIndex.get(node.value);
442
455
  node.type = "MemberExpression";
@@ -454,8 +467,6 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
454
467
  delete node.value;
455
468
  delete node.raw;
456
469
  }
457
- const serializedTable = JSON.stringify(encryptedPayloads);
458
- const integrityToken = computeFnv1aHash(serializedTable, seed);
459
470
  const helperStmt = createVaultHelperAst(
460
471
  cacheVarName,
461
472
  encryptedPayloads,
@@ -527,167 +538,351 @@ var FUNCTION_TYPES = /* @__PURE__ */ new Set([
527
538
  "FunctionExpression",
528
539
  "ArrowFunctionExpression"
529
540
  ]);
530
- function collectPatternIdentifiers(pattern, out) {
541
+ var LEXICAL_SCOPE_TYPES = /* @__PURE__ */ new Set([
542
+ "Program",
543
+ "FunctionDeclaration",
544
+ "FunctionExpression",
545
+ "ArrowFunctionExpression",
546
+ "BlockStatement",
547
+ "ForStatement",
548
+ "ForInStatement",
549
+ "ForOfStatement",
550
+ "SwitchStatement",
551
+ "CatchClause",
552
+ "ClassDeclaration",
553
+ "ClassExpression"
554
+ ]);
555
+ function collectPatternTargets(pattern, out, shorthandProp) {
531
556
  if (!pattern) return;
532
557
  switch (pattern.type) {
533
558
  case "Identifier":
534
- out.push(pattern);
559
+ out.push({ idNode: pattern, shorthandProp });
535
560
  break;
536
561
  case "AssignmentPattern":
537
- collectPatternIdentifiers(pattern.left, out);
562
+ collectPatternTargets(pattern.left, out, shorthandProp);
538
563
  break;
539
564
  case "RestElement":
540
- collectPatternIdentifiers(pattern.argument, out);
565
+ collectPatternTargets(pattern.argument, out);
541
566
  break;
542
567
  case "ArrayPattern":
543
568
  for (const el of pattern.elements || []) {
544
- if (el) collectPatternIdentifiers(el, out);
569
+ if (el) collectPatternTargets(el, out);
545
570
  }
546
571
  break;
547
572
  case "ObjectPattern":
548
573
  for (const prop of pattern.properties || []) {
549
574
  if (prop.type === "Property") {
550
- if (prop.shorthand) {
551
- prop.shorthand = false;
552
- prop.key = { ...prop.key };
553
- }
554
- collectPatternIdentifiers(prop.value, out);
575
+ collectPatternTargets(
576
+ prop.value,
577
+ out,
578
+ prop.shorthand ? prop : void 0
579
+ );
555
580
  } else if (prop.type === "RestElement") {
556
- collectPatternIdentifiers(prop.argument, out);
581
+ collectPatternTargets(prop.argument, out);
557
582
  }
558
583
  }
559
584
  break;
560
585
  }
561
586
  }
587
+ function isMangleableName(origName) {
588
+ return !RESERVED_IDENTIFIERS.has(origName) && !origName.startsWith("_u") && !origName.startsWith("_0x");
589
+ }
562
590
  function applyPolymorphicMangler(ast, seed = 2654435769) {
563
- const scopeMap = /* @__PURE__ */ new Map();
564
- const scopeDepthMap = /* @__PURE__ */ new Map();
591
+ const scopeRecords = /* @__PURE__ */ new Map();
592
+ const existingIdentifiers = /* @__PURE__ */ new Set();
565
593
  let counter = 0;
566
- function makeScopedMangledName(scopeDepth, localIndex, usedValues) {
567
- counter++;
568
- let salt = 0;
569
- while (true) {
570
- const h = computeFnv1aHash(`s${scopeDepth}:i${localIndex}:${salt}`, seed);
571
- const candidate = `_0x${((h & 31) + localIndex + salt).toString(36)}`;
572
- if (!usedValues.has(candidate)) {
573
- return candidate;
594
+ function ensureScopeChain(ancestors) {
595
+ let prevScope = null;
596
+ for (let i = 0; i < ancestors.length; i++) {
597
+ const node = ancestors[i];
598
+ if (!LEXICAL_SCOPE_TYPES.has(node.type)) continue;
599
+ let record = scopeRecords.get(node);
600
+ if (!record) {
601
+ const isFn = FUNCTION_TYPES.has(node.type);
602
+ const depth = prevScope ? prevScope.depth + (isFn ? 1 : 0) : 0;
603
+ record = {
604
+ node,
605
+ parent: prevScope,
606
+ children: [],
607
+ depth: Math.max(1, depth),
608
+ bindings: /* @__PURE__ */ new Map(),
609
+ usedNames: /* @__PURE__ */ new Set()
610
+ };
611
+ scopeRecords.set(node, record);
612
+ if (prevScope) {
613
+ prevScope.children.push(record);
614
+ }
574
615
  }
575
- salt++;
616
+ prevScope = record;
576
617
  }
618
+ return prevScope;
577
619
  }
578
- function countFunctionDepth(ancestors) {
579
- let depth = 0;
580
- for (let i = 0; i < ancestors.length; i++) {
620
+ function findEnclosingFunctionNode(ancestors) {
621
+ for (let i = ancestors.length - 1; i >= 0; i--) {
581
622
  if (FUNCTION_TYPES.has(ancestors[i].type)) {
582
- depth++;
623
+ return ancestors[i];
583
624
  }
584
625
  }
585
- return Math.max(1, depth);
626
+ return null;
586
627
  }
587
- function findEnclosingFunction(ancestors) {
628
+ function findEnclosingLexicalNode(ancestors) {
588
629
  for (let i = ancestors.length - 1; i >= 0; i--) {
589
- if (FUNCTION_TYPES.has(ancestors[i].type)) {
630
+ if (LEXICAL_SCOPE_TYPES.has(ancestors[i].type)) {
590
631
  return ancestors[i];
591
632
  }
592
633
  }
593
- return null;
634
+ return ancestors[0];
594
635
  }
595
- function registerBindingAndMutateDeclSite(idNode, bindings, scopeDepth) {
596
- const origName = idNode.name;
597
- if (RESERVED_IDENTIFIERS.has(origName) || origName.startsWith("_u") || origName.startsWith("_0x")) {
598
- return;
636
+ function registerBindingTarget(scopeNode, target, shouldMangle) {
637
+ const scope = scopeRecords.get(scopeNode);
638
+ if (!scope) return;
639
+ const origName = target.idNode.name;
640
+ const canMangle = shouldMangle && isMangleableName(origName);
641
+ let entry = scope.bindings.get(origName);
642
+ if (!entry) {
643
+ entry = {
644
+ origName,
645
+ shouldMangle: canMangle,
646
+ targets: [target],
647
+ mangledName: origName
648
+ };
649
+ scope.bindings.set(origName, entry);
650
+ } else {
651
+ if (!canMangle) {
652
+ entry.shouldMangle = false;
653
+ }
654
+ entry.targets.push(target);
599
655
  }
600
- let mangled = bindings.get(origName);
601
- if (!mangled) {
602
- const usedValues = new Set(bindings.values());
603
- mangled = makeScopedMangledName(scopeDepth, bindings.size, usedValues);
604
- bindings.set(origName, mangled);
656
+ }
657
+ function registerBarrierName(scopeNode, origName) {
658
+ const scope = scopeRecords.get(scopeNode);
659
+ if (!scope) return;
660
+ let entry = scope.bindings.get(origName);
661
+ if (!entry) {
662
+ entry = {
663
+ origName,
664
+ shouldMangle: false,
665
+ targets: [],
666
+ mangledName: origName
667
+ };
668
+ scope.bindings.set(origName, entry);
669
+ } else {
670
+ entry.shouldMangle = false;
605
671
  }
606
- idNode.name = mangled;
607
672
  }
608
673
  function registerFunctionParams(fnNode, ancestors) {
609
- let bindings = scopeMap.get(fnNode);
610
- const depth = countFunctionDepth(ancestors);
611
- scopeDepthMap.set(fnNode, depth);
612
- if (!bindings) {
613
- bindings = /* @__PURE__ */ new Map();
614
- scopeMap.set(fnNode, bindings);
615
- }
674
+ ensureScopeChain(ancestors);
616
675
  for (const param of fnNode.params || []) {
617
- const idNodes = [];
618
- collectPatternIdentifiers(param, idNodes);
619
- for (const idNode of idNodes) {
620
- registerBindingAndMutateDeclSite(idNode, bindings, depth);
676
+ const targets = [];
677
+ collectPatternTargets(param, targets);
678
+ for (const target of targets) {
679
+ registerBindingTarget(fnNode, target, true);
621
680
  }
622
681
  }
623
682
  }
683
+ ensureScopeChain([ast]);
624
684
  walk3.ancestor(ast, {
685
+ Identifier(node) {
686
+ if (typeof node.name === "string") {
687
+ existingIdentifiers.add(node.name);
688
+ }
689
+ },
690
+ BlockStatement(_node, _state, ancestors) {
691
+ ensureScopeChain(ancestors);
692
+ },
693
+ ForStatement(_node, _state, ancestors) {
694
+ ensureScopeChain(ancestors);
695
+ },
696
+ ForInStatement(_node, _state, ancestors) {
697
+ ensureScopeChain(ancestors);
698
+ },
699
+ ForOfStatement(_node, _state, ancestors) {
700
+ ensureScopeChain(ancestors);
701
+ },
702
+ SwitchStatement(_node, _state, ancestors) {
703
+ ensureScopeChain(ancestors);
704
+ },
625
705
  FunctionDeclaration(node, _state, ancestors) {
626
706
  registerFunctionParams(node, ancestors);
707
+ if (node.id?.type === "Identifier") {
708
+ const parentAncestors = ancestors.slice(0, -1);
709
+ const lexNode = findEnclosingLexicalNode(parentAncestors);
710
+ const fnNode = findEnclosingFunctionNode(parentAncestors) ?? ast;
711
+ registerBarrierName(lexNode, node.id.name);
712
+ if (lexNode === fnNode.body || fnNode === ast) {
713
+ registerBarrierName(fnNode, node.id.name);
714
+ }
715
+ if (!scopeRecords.get(node)?.bindings.has(node.id.name)) {
716
+ registerBarrierName(node, node.id.name);
717
+ }
718
+ }
627
719
  },
628
720
  FunctionExpression(node, _state, ancestors) {
629
721
  registerFunctionParams(node, ancestors);
722
+ if (node.id?.type === "Identifier") {
723
+ if (!scopeRecords.get(node)?.bindings.has(node.id.name)) {
724
+ registerBarrierName(node, node.id.name);
725
+ }
726
+ }
630
727
  },
631
728
  ArrowFunctionExpression(node, _state, ancestors) {
632
729
  registerFunctionParams(node, ancestors);
633
730
  },
731
+ ClassDeclaration(node, _state, ancestors) {
732
+ ensureScopeChain(ancestors);
733
+ if (node.id?.type === "Identifier") {
734
+ const parentAncestors = ancestors.slice(0, -1);
735
+ const lexNode = findEnclosingLexicalNode(parentAncestors);
736
+ registerBarrierName(lexNode, node.id.name);
737
+ registerBarrierName(node, node.id.name);
738
+ }
739
+ },
740
+ ClassExpression(node, _state, ancestors) {
741
+ ensureScopeChain(ancestors);
742
+ if (node.id?.type === "Identifier") {
743
+ registerBarrierName(node, node.id.name);
744
+ }
745
+ },
746
+ CatchClause(node, _state, ancestors) {
747
+ ensureScopeChain(ancestors);
748
+ if (node.param) {
749
+ const targets = [];
750
+ collectPatternTargets(node.param, targets);
751
+ for (const target of targets) {
752
+ registerBindingTarget(node, target, true);
753
+ }
754
+ }
755
+ },
634
756
  VariableDeclaration(node, _state, ancestors) {
635
- const fnScope = findEnclosingFunction(ancestors);
636
- if (!fnScope) return;
637
- let bindings = scopeMap.get(fnScope);
638
- const depth = scopeDepthMap.get(fnScope) ?? countFunctionDepth(ancestors);
639
- if (!bindings) {
640
- bindings = /* @__PURE__ */ new Map();
641
- scopeMap.set(fnScope, bindings);
757
+ ensureScopeChain(ancestors);
758
+ const fnScope = findEnclosingFunctionNode(ancestors);
759
+ if (!fnScope) {
760
+ for (const decl of node.declarations || []) {
761
+ const targets = [];
762
+ collectPatternTargets(decl.id, targets);
763
+ for (const target of targets) {
764
+ registerBarrierName(ast, target.idNode.name);
765
+ }
766
+ }
767
+ return;
642
768
  }
769
+ const targetScopeNode = node.kind === "var" ? fnScope : findEnclosingLexicalNode(ancestors);
643
770
  for (const decl of node.declarations || []) {
644
- const idNodes = [];
645
- collectPatternIdentifiers(decl.id, idNodes);
646
- for (const idNode of idNodes) {
647
- registerBindingAndMutateDeclSite(idNode, bindings, depth);
771
+ const targets = [];
772
+ collectPatternTargets(decl.id, targets);
773
+ for (const target of targets) {
774
+ const origName = target.idNode.name;
775
+ if (node.kind === "var") {
776
+ for (let i = ancestors.length - 1; i >= 0; i--) {
777
+ if (ancestors[i] === fnScope) break;
778
+ if (ancestors[i].type === "CatchClause") {
779
+ const catchScope = scopeRecords.get(ancestors[i]);
780
+ if (catchScope?.bindings.has(origName)) {
781
+ registerBarrierName(ancestors[i], origName);
782
+ registerBarrierName(fnScope, origName);
783
+ }
784
+ }
785
+ }
786
+ }
787
+ registerBindingTarget(targetScopeNode, target, true);
648
788
  }
649
789
  }
650
790
  }
651
791
  });
792
+ function isNameUsedInScopeOrAncestors(candidate, scope) {
793
+ if (existingIdentifiers.has(candidate)) return true;
794
+ let curr = scope;
795
+ while (curr) {
796
+ if (curr.usedNames.has(candidate) || curr.bindings.has(candidate)) {
797
+ return true;
798
+ }
799
+ curr = curr.parent;
800
+ }
801
+ return false;
802
+ }
803
+ function makeScopedMangledName(scope, localIndex) {
804
+ counter++;
805
+ let salt = 0;
806
+ while (true) {
807
+ const h = computeFnv1aHash(`s${scope.depth}:i${localIndex}:${salt}`, seed);
808
+ const candidate = `_0x${((h & 31) + localIndex + salt).toString(36)}`;
809
+ if (!isNameUsedInScopeOrAncestors(candidate, scope)) {
810
+ return candidate;
811
+ }
812
+ salt++;
813
+ }
814
+ }
815
+ function assignScopeNamesTopDown(scope) {
816
+ let localIndex = 0;
817
+ for (const entry of scope.bindings.values()) {
818
+ if (!entry.shouldMangle) {
819
+ entry.mangledName = entry.origName;
820
+ scope.usedNames.add(entry.origName);
821
+ continue;
822
+ }
823
+ const mangled = makeScopedMangledName(scope, localIndex);
824
+ localIndex++;
825
+ entry.mangledName = mangled;
826
+ scope.usedNames.add(mangled);
827
+ for (const target of entry.targets) {
828
+ if (target.shorthandProp && target.shorthandProp.shorthand) {
829
+ target.shorthandProp.shorthand = false;
830
+ target.shorthandProp.key = {
831
+ type: "Identifier",
832
+ name: entry.origName
833
+ };
834
+ }
835
+ target.idNode.name = mangled;
836
+ }
837
+ }
838
+ for (const child of scope.children) {
839
+ assignScopeNamesTopDown(child);
840
+ }
841
+ }
842
+ const rootScope = scopeRecords.get(ast);
843
+ if (rootScope) {
844
+ assignScopeNamesTopDown(rootScope);
845
+ }
652
846
  if (counter === 0) return 0;
653
- walk3.ancestor(ast, {
654
- AssignmentExpression(node, _state, ancestors) {
655
- const idNodes = [];
656
- collectPatternIdentifiers(node.left, idNodes);
657
- for (const idNode of idNodes) {
658
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
659
- if (resolved) {
660
- idNode.name = resolved;
847
+ function resolveBinding(name, ancestors) {
848
+ for (let i = ancestors.length - 1; i >= 0; i--) {
849
+ const scope = scopeRecords.get(ancestors[i]);
850
+ if (scope && scope.bindings.has(name)) {
851
+ return scope.bindings.get(name);
852
+ }
853
+ }
854
+ return null;
855
+ }
856
+ function renamePatternTargetsInScope(pattern, ancestors) {
857
+ const targets = [];
858
+ collectPatternTargets(pattern, targets);
859
+ for (const target of targets) {
860
+ const origName = target.idNode.name;
861
+ const entry = resolveBinding(origName, ancestors);
862
+ if (entry && entry.shouldMangle && entry.mangledName !== origName) {
863
+ if (target.shorthandProp && target.shorthandProp.shorthand) {
864
+ target.shorthandProp.shorthand = false;
865
+ target.shorthandProp.key = {
866
+ type: "Identifier",
867
+ name: origName
868
+ };
661
869
  }
870
+ target.idNode.name = entry.mangledName;
662
871
  }
872
+ }
873
+ }
874
+ walk3.ancestor(ast, {
875
+ AssignmentExpression(node, _state, ancestors) {
876
+ renamePatternTargetsInScope(node.left, ancestors);
663
877
  },
664
878
  ForInStatement(node, _state, ancestors) {
665
879
  if (node.left && node.left.type !== "VariableDeclaration") {
666
- const idNodes = [];
667
- collectPatternIdentifiers(node.left, idNodes);
668
- for (const idNode of idNodes) {
669
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
670
- if (resolved) idNode.name = resolved;
671
- }
880
+ renamePatternTargetsInScope(node.left, ancestors);
672
881
  }
673
882
  },
674
883
  ForOfStatement(node, _state, ancestors) {
675
884
  if (node.left && node.left.type !== "VariableDeclaration") {
676
- const idNodes = [];
677
- collectPatternIdentifiers(node.left, idNodes);
678
- for (const idNode of idNodes) {
679
- const resolved = resolveMangledName(idNode.name, ancestors, scopeMap);
680
- if (resolved) idNode.name = resolved;
681
- }
682
- }
683
- },
684
- Property(node, _state, ancestors) {
685
- if (node.shorthand && node.value?.type === "Identifier") {
686
- const resolved = resolveMangledName(node.value.name, ancestors, scopeMap);
687
- if (resolved) {
688
- node.shorthand = false;
689
- node.key = { type: "Identifier", name: node.value.name };
690
- }
885
+ renamePatternTargetsInScope(node.left, ancestors);
691
886
  }
692
887
  },
693
888
  Identifier(node, _state, ancestors) {
@@ -705,24 +900,18 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
705
900
  if (parent.type === "LabeledStatement" || parent.type === "BreakStatement" || parent.type === "ContinueStatement" || parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier" || parent.type === "ExportSpecifier") {
706
901
  return;
707
902
  }
708
- const resolved = resolveMangledName(node.name, ancestors, scopeMap);
709
- if (resolved) {
710
- node.name = resolved;
903
+ const entry = resolveBinding(node.name, ancestors);
904
+ if (entry && entry.shouldMangle && entry.mangledName !== node.name) {
905
+ if (parent.type === "Property" && parent.shorthand && parent.value === node) {
906
+ parent.shorthand = false;
907
+ parent.key = { type: "Identifier", name: node.name };
908
+ }
909
+ node.name = entry.mangledName;
711
910
  }
712
911
  }
713
912
  });
714
913
  return counter;
715
914
  }
716
- function resolveMangledName(name, ancestors, scopeMap) {
717
- for (let i = ancestors.length - 1; i >= 0; i--) {
718
- const anc = ancestors[i];
719
- const bindings = scopeMap.get(anc);
720
- if (bindings && bindings.has(name)) {
721
- return bindings.get(name);
722
- }
723
- }
724
- return null;
725
- }
726
915
 
727
916
  // src/ast/cold-flow-guard.ts
728
917
  var walk4 = __toESM(require("acorn-walk"), 1);
@@ -837,6 +1026,20 @@ function generateRuntimeShield(config) {
837
1026
  }
838
1027
 
839
1028
  // src/ast/index.ts
1029
+ function isAstAlreadyProtected(ast) {
1030
+ if (!ast || !Array.isArray(ast.body)) return false;
1031
+ return ast.body.some((stmt) => {
1032
+ if (stmt.type === "VariableDeclaration" && stmt.declarations?.some(
1033
+ (d) => d.id?.type === "Identifier" && typeof d.id.name === "string" && d.id.name.startsWith("_uC")
1034
+ )) {
1035
+ return true;
1036
+ }
1037
+ 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$")) {
1038
+ return true;
1039
+ }
1040
+ return false;
1041
+ });
1042
+ }
840
1043
  function transformJavaScript(code, options = {}) {
841
1044
  const seed = options.seed ?? 2654435769;
842
1045
  const minStringLength = options.minStringLength ?? 4;
@@ -854,7 +1057,7 @@ function transformJavaScript(code, options = {}) {
854
1057
  };
855
1058
  }
856
1059
  const ast = parseJavaScriptAst(code);
857
- if (!ast) {
1060
+ if (!ast || isAstAlreadyProtected(ast)) {
858
1061
  return {
859
1062
  code,
860
1063
  encryptedStringsCount: 0,