unpeek 1.6.1 → 1.7.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 (45) hide show
  1. package/dist/ast/index.cjs +531 -99
  2. package/dist/ast/index.cjs.map +1 -1
  3. package/dist/ast/index.js +531 -99
  4. package/dist/ast/index.js.map +1 -1
  5. package/dist/browser.cjs +531 -99
  6. package/dist/browser.cjs.map +1 -1
  7. package/dist/browser.js +531 -99
  8. package/dist/browser.js.map +1 -1
  9. package/dist/bundler/unplugin.cjs +531 -99
  10. package/dist/bundler/unplugin.cjs.map +1 -1
  11. package/dist/bundler/unplugin.js +531 -99
  12. package/dist/bundler/unplugin.js.map +1 -1
  13. package/dist/cli/index.cjs +532 -100
  14. package/dist/cli/index.cjs.map +1 -1
  15. package/dist/cli/index.js +532 -100
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/cli/protect.cjs +531 -99
  18. package/dist/cli/protect.cjs.map +1 -1
  19. package/dist/cli/protect.js +531 -99
  20. package/dist/cli/protect.js.map +1 -1
  21. package/dist/esbuild.cjs +531 -99
  22. package/dist/esbuild.cjs.map +1 -1
  23. package/dist/esbuild.js +531 -99
  24. package/dist/esbuild.js.map +1 -1
  25. package/dist/index.cjs +532 -100
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.js +532 -100
  28. package/dist/index.js.map +1 -1
  29. package/dist/rollup.cjs +531 -99
  30. package/dist/rollup.cjs.map +1 -1
  31. package/dist/rollup.js +531 -99
  32. package/dist/rollup.js.map +1 -1
  33. package/dist/runtime/index.cjs +2 -15
  34. package/dist/runtime/index.cjs.map +1 -1
  35. package/dist/runtime/index.js +2 -15
  36. package/dist/runtime/index.js.map +1 -1
  37. package/dist/vite.cjs +531 -99
  38. package/dist/vite.cjs.map +1 -1
  39. package/dist/vite.js +531 -99
  40. package/dist/vite.js.map +1 -1
  41. package/dist/webpack.cjs +531 -99
  42. package/dist/webpack.cjs.map +1 -1
  43. package/dist/webpack.js +531 -99
  44. package/dist/webpack.js.map +1 -1
  45. package/package.json +1 -1
package/dist/ast/index.js CHANGED
@@ -92,15 +92,100 @@ function analyzeAstHotPaths(ast) {
92
92
  const coldFunctionNodes = /* @__PURE__ */ new WeakSet();
93
93
  const hotExemptNodes = /* @__PURE__ */ new WeakSet();
94
94
  const directHotNodes = /* @__PURE__ */ new Set();
95
+ const serializedFunctionNodes = /* @__PURE__ */ new Set();
96
+ const serializerHelperNodes = /* @__PURE__ */ new Set();
95
97
  const allFunctions = [];
96
98
  const fnByName = /* @__PURE__ */ new Map();
97
99
  const scopeFunctions = /* @__PURE__ */ new Map();
98
100
  const fnScopeChain = /* @__PURE__ */ new Map();
101
+ const fnParams = /* @__PURE__ */ new Map();
102
+ const fnVarAliases = /* @__PURE__ */ new Map();
99
103
  const callerToCallees = /* @__PURE__ */ new Map();
104
+ const pendingSerializedRefs = [];
105
+ const allCallSites = [];
106
+ function ensureFnParams(fnNode) {
107
+ let paramSet = fnParams.get(fnNode);
108
+ if (!paramSet) {
109
+ paramSet = /* @__PURE__ */ new Set();
110
+ for (const p of fnNode.params || []) {
111
+ if (p?.type === "Identifier") {
112
+ paramSet.add(p.name);
113
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
114
+ paramSet.add(p.left.name);
115
+ }
116
+ }
117
+ fnParams.set(fnNode, paramSet);
118
+ }
119
+ return paramSet;
120
+ }
121
+ function recordVarAlias(scopeNode, varName, rhsName) {
122
+ let map = fnVarAliases.get(scopeNode);
123
+ if (!map) {
124
+ map = /* @__PURE__ */ new Map();
125
+ fnVarAliases.set(scopeNode, map);
126
+ }
127
+ map.set(varName, rhsName);
128
+ }
129
+ function resolveAliasTarget(scopeNode, identName) {
130
+ const map = fnVarAliases.get(scopeNode);
131
+ let cur = identName;
132
+ const seen = /* @__PURE__ */ new Set();
133
+ while (map && map.has(cur) && !seen.has(cur)) {
134
+ seen.add(cur);
135
+ cur = map.get(cur);
136
+ }
137
+ return cur;
138
+ }
139
+ function markSerializedFunction(node) {
140
+ serializedFunctionNodes.add(node);
141
+ hotFunctionNodes.add(node);
142
+ directHotNodes.add(node);
143
+ hotExemptNodes.add(node);
144
+ }
145
+ function findEnclosingFunction(ancestors, selfNode) {
146
+ for (let i = ancestors.length - 1; i >= 0; i--) {
147
+ if (ancestors[i] !== selfNode && FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
148
+ return ancestors[i];
149
+ }
150
+ }
151
+ return null;
152
+ }
153
+ function findOutermostModuleFunction(ancestors) {
154
+ const fnList = [];
155
+ for (let i = 0; i < ancestors.length; i++) {
156
+ if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
157
+ fnList.push(ancestors[i]);
158
+ }
159
+ }
160
+ if (fnList.length === 0) return null;
161
+ if (fnList.length === 1) return fnList[0];
162
+ return fnList[1];
163
+ }
164
+ function isZeroArgIifeFactoryFunction(node) {
165
+ if (!node || node.params && node.params.length > 0) return false;
166
+ if (!node.body || node.body.type !== "BlockStatement") return false;
167
+ const stmts = (node.body.body || []).filter(
168
+ (s) => !(s.type === "ExpressionStatement" && s.directive)
169
+ );
170
+ if (stmts.length !== 1 || stmts[0].type !== "ReturnStatement") return false;
171
+ const retArg = stmts[0].argument;
172
+ if (!retArg || retArg.type !== "CallExpression") return false;
173
+ const callee = retArg.callee;
174
+ return callee && (callee.type === "FunctionExpression" || callee.type === "ArrowFunctionExpression") && callee.body?.type === "BlockStatement";
175
+ }
100
176
  function registerFunctionNode(node, ancestors) {
101
177
  const name = getFunctionLabel(node, ancestors);
102
178
  allFunctions.push({ node, name });
103
179
  fnByName.set(name, node);
180
+ const paramSet = /* @__PURE__ */ new Set();
181
+ for (const p of node.params || []) {
182
+ if (p?.type === "Identifier") {
183
+ paramSet.add(p.name);
184
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
185
+ paramSet.add(p.left.name);
186
+ }
187
+ }
188
+ fnParams.set(node, paramSet);
104
189
  const enclosingScope = findEnclosingScopeNode(ancestors, node);
105
190
  let localMap = scopeFunctions.get(enclosingScope);
106
191
  if (!localMap) {
@@ -109,6 +194,22 @@ function analyzeAstHotPaths(ast) {
109
194
  }
110
195
  localMap.set(name, node);
111
196
  const parent = ancestors[ancestors.length - 2];
197
+ if (parent?.type === "VariableDeclarator" && parent.id?.type === "Identifier") {
198
+ fnByName.set(parent.id.name, node);
199
+ localMap.set(parent.id.name, node);
200
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "Identifier") {
201
+ fnByName.set(parent.left.name, node);
202
+ localMap.set(parent.left.name, node);
203
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "MemberExpression" && !parent.left.computed && parent.left.property?.type === "Identifier") {
204
+ const propName = parent.left.property.name;
205
+ fnByName.set(propName, node);
206
+ localMap.set(propName, node);
207
+ if (parent.left.object?.type === "Identifier") {
208
+ const qual = `${parent.left.object.name}.${propName}`;
209
+ fnByName.set(qual, node);
210
+ localMap.set(qual, node);
211
+ }
212
+ }
112
213
  const grandParent = ancestors[ancestors.length - 3];
113
214
  const greatGrandParent = ancestors[ancestors.length - 4];
114
215
  if (parent?.type === "Property" && parent.key?.type === "Identifier" && grandParent?.type === "ObjectExpression" && greatGrandParent?.type === "VariableDeclarator" && greatGrandParent.id?.type === "Identifier") {
@@ -118,7 +219,7 @@ function analyzeAstHotPaths(ast) {
118
219
  }
119
220
  const chain = [];
120
221
  for (let i = ancestors.length - 1; i >= 0; i--) {
121
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program") {
222
+ if (ancestors[i] !== node && (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program")) {
122
223
  chain.push(ancestors[i]);
123
224
  }
124
225
  }
@@ -127,8 +228,13 @@ function analyzeAstHotPaths(ast) {
127
228
  hotFunctionNodes.add(node);
128
229
  directHotNodes.add(node);
129
230
  }
231
+ if (isZeroArgIifeFactoryFunction(node)) {
232
+ markSerializedFunction(node);
233
+ }
130
234
  }
131
235
  function resolveCalleeNode(callerNode, calleeKey) {
236
+ const directLocal = scopeFunctions.get(callerNode)?.get(calleeKey);
237
+ if (directLocal) return directLocal;
132
238
  const chain = fnScopeChain.get(callerNode);
133
239
  if (chain) {
134
240
  for (const scopeNode of chain) {
@@ -139,6 +245,25 @@ function analyzeAstHotPaths(ast) {
139
245
  }
140
246
  return fnByName.get(calleeKey);
141
247
  }
248
+ function queueSerializedTarget(target, enclosingScope) {
249
+ if (!target) return;
250
+ if (FUNCTION_NODE_TYPES.has(target.type)) {
251
+ markSerializedFunction(target);
252
+ } else if (target.type === "Identifier") {
253
+ pendingSerializedRefs.push({ scopeNode: enclosingScope, name: target.name });
254
+ } else if (target.type === "MemberExpression" && !target.computed && target.property?.type === "Identifier") {
255
+ pendingSerializedRefs.push({
256
+ scopeNode: enclosingScope,
257
+ name: target.property.name
258
+ });
259
+ if (target.object?.type === "Identifier") {
260
+ pendingSerializedRefs.push({
261
+ scopeNode: enclosingScope,
262
+ name: `${target.object.name}.${target.property.name}`
263
+ });
264
+ }
265
+ }
266
+ }
142
267
  walk.ancestor(ast, {
143
268
  FunctionDeclaration(node, _state, ancestors) {
144
269
  registerFunctionNode(node, ancestors);
@@ -149,6 +274,59 @@ function analyzeAstHotPaths(ast) {
149
274
  ArrowFunctionExpression(node, _state, ancestors) {
150
275
  registerFunctionNode(node, ancestors);
151
276
  },
277
+ VariableDeclarator(node, _state, ancestors) {
278
+ if (node.id?.type === "Identifier" && node.init?.type === "Identifier") {
279
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
280
+ recordVarAlias(scopeNode, node.id.name, node.init.name);
281
+ }
282
+ },
283
+ AssignmentExpression(node, _state, ancestors) {
284
+ const left = node.left;
285
+ if (left?.type === "Identifier" && node.right?.type === "Identifier") {
286
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
287
+ recordVarAlias(scopeNode, left.name, node.right.name);
288
+ }
289
+ const isBareOnMessage = left?.type === "Identifier" && left.name === "onmessage";
290
+ const isSelfOnMessage = left?.type === "MemberExpression" && !left.computed && left.object?.type === "Identifier" && left.object.name === "self" && left.property?.type === "Identifier" && left.property.name === "onmessage";
291
+ if (isBareOnMessage || isSelfOnMessage) {
292
+ const enclosingFn = findEnclosingFunction(ancestors);
293
+ if (enclosingFn) {
294
+ markSerializedFunction(enclosingFn);
295
+ }
296
+ const outerFn = findOutermostModuleFunction(ancestors);
297
+ if (outerFn) {
298
+ markSerializedFunction(outerFn);
299
+ }
300
+ const scopeNode = enclosingFn ?? ast;
301
+ queueSerializedTarget(node.right, scopeNode);
302
+ }
303
+ },
304
+ ObjectExpression(node, _state, ancestors) {
305
+ const props = node.properties || [];
306
+ const propMap = /* @__PURE__ */ new Map();
307
+ for (const p of props) {
308
+ if (p?.type === "Property" && !p.computed) {
309
+ const keyName = p.key?.type === "Identifier" ? p.key.name : p.key?.type === "Literal" ? String(p.key.value) : "";
310
+ if (keyName) {
311
+ propMap.set(keyName, p.value);
312
+ }
313
+ }
314
+ }
315
+ const hasWorkerDescriptorShape = propMap.has("init") && propMap.has("dependencies") || propMap.has("isWorkerModule") || propMap.has("getTransferables") && (propMap.has("init") || propMap.has("dependencies"));
316
+ if (hasWorkerDescriptorShape) {
317
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
318
+ for (const key of ["init", "getTransferables", "worker"]) {
319
+ const val = propMap.get(key);
320
+ if (val) queueSerializedTarget(val, scopeNode);
321
+ }
322
+ const deps = propMap.get("dependencies");
323
+ if (deps?.type === "ArrayExpression") {
324
+ for (const el of deps.elements || []) {
325
+ if (el) queueSerializedTarget(el, scopeNode);
326
+ }
327
+ }
328
+ }
329
+ },
152
330
  CallExpression(node, _state, ancestors) {
153
331
  const callee = node.callee;
154
332
  let calleeName = "";
@@ -165,38 +343,76 @@ function analyzeAstHotPaths(ast) {
165
343
  qualifiedCall = memberProp;
166
344
  }
167
345
  }
346
+ const enclosingFn = findEnclosingFunction(ancestors);
347
+ const currentScope = enclosingFn ?? ast;
168
348
  if (qualifiedCall || memberProp) {
169
- for (let i = ancestors.length - 1; i >= 0; i--) {
170
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
171
- let set = callerToCallees.get(ancestors[i]);
172
- if (!set) {
173
- set = /* @__PURE__ */ new Set();
174
- callerToCallees.set(ancestors[i], set);
175
- }
176
- if (qualifiedCall) set.add(qualifiedCall);
177
- if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
178
- set.add(memberProp);
179
- }
180
- break;
349
+ allCallSites.push({
350
+ scopeNode: currentScope,
351
+ calleeKey: qualifiedCall || memberProp,
352
+ args: node.arguments || []
353
+ });
354
+ if (enclosingFn) {
355
+ let set = callerToCallees.get(enclosingFn);
356
+ if (!set) {
357
+ set = /* @__PURE__ */ new Set();
358
+ callerToCallees.set(enclosingFn, set);
359
+ }
360
+ if (qualifiedCall) set.add(qualifiedCall);
361
+ if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
362
+ set.add(memberProp);
363
+ }
364
+ }
365
+ }
366
+ if (memberProp === "toString" && (!node.arguments || node.arguments.length === 0) && callee?.type === "MemberExpression") {
367
+ const targetObj = callee.object;
368
+ queueSerializedTarget(targetObj, currentScope);
369
+ if (targetObj?.type === "Identifier" && enclosingFn) {
370
+ const resolvedName = resolveAliasTarget(enclosingFn, targetObj.name);
371
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
372
+ serializerHelperNodes.add(enclosingFn);
373
+ }
374
+ if (resolvedName !== targetObj.name) {
375
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
181
376
  }
182
377
  }
183
378
  }
379
+ if ((memberProp === "call" || memberProp === "apply") && callee?.type === "MemberExpression" && callee.object?.type === "MemberExpression" && callee.object.property?.type === "Identifier" && callee.object.property.name === "toString" && node.arguments?.length > 0) {
380
+ const firstArg = node.arguments[0];
381
+ queueSerializedTarget(firstArg, currentScope);
382
+ if (firstArg?.type === "Identifier" && enclosingFn) {
383
+ const resolvedName = resolveAliasTarget(enclosingFn, firstArg.name);
384
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
385
+ serializerHelperNodes.add(enclosingFn);
386
+ }
387
+ if (resolvedName !== firstArg.name) {
388
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
389
+ }
390
+ }
391
+ }
392
+ if (calleeName === "importScripts" || memberProp === "importScripts" && callee?.object?.type === "Identifier" && callee.object.name === "self") {
393
+ if (enclosingFn) markSerializedFunction(enclosingFn);
394
+ const outerFn = findOutermostModuleFunction(ancestors);
395
+ if (outerFn) markSerializedFunction(outerFn);
396
+ }
184
397
  if (ARRAY_HOF_METHODS.has(memberProp) || calleeName === "requestAnimationFrame" || memberProp === "requestAnimationFrame" || calleeName === "cancelAnimationFrame" || memberProp === "cancelAnimationFrame") {
185
398
  for (const arg of node.arguments || []) {
186
399
  if (FUNCTION_NODE_TYPES.has(arg.type)) {
187
400
  hotFunctionNodes.add(arg);
188
401
  directHotNodes.add(arg);
189
402
  hotExemptNodes.add(arg);
403
+ } else if (arg?.type === "Identifier") {
404
+ allCallSites.push({
405
+ scopeNode: currentScope,
406
+ calleeKey: arg.name,
407
+ args: [arg]
408
+ });
190
409
  }
191
410
  }
192
411
  }
193
412
  if (UI_HOOK_OR_RENDER_CALLS.has(calleeName) || UI_HOOK_OR_RENDER_CALLS.has(memberProp)) {
194
- for (let i = ancestors.length - 1; i >= 0; i--) {
195
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
196
- hotFunctionNodes.add(ancestors[i]);
197
- directHotNodes.add(ancestors[i]);
198
- break;
199
- }
413
+ if (enclosingFn) {
414
+ hotFunctionNodes.add(enclosingFn);
415
+ directHotNodes.add(enclosingFn);
200
416
  }
201
417
  }
202
418
  },
@@ -221,10 +437,41 @@ function analyzeAstHotPaths(ast) {
221
437
  markEnclosingFunctionHot(ancestors, hotFunctionNodes, directHotNodes);
222
438
  }
223
439
  });
440
+ for (const callSite of allCallSites) {
441
+ const resolvedCallee = resolveCalleeNode(callSite.scopeNode, callSite.calleeKey);
442
+ if (resolvedCallee && serializerHelperNodes.has(resolvedCallee)) {
443
+ for (const arg of callSite.args) {
444
+ queueSerializedTarget(arg, callSite.scopeNode);
445
+ }
446
+ }
447
+ }
448
+ for (const ref of pendingSerializedRefs) {
449
+ const targetNode = resolveCalleeNode(ref.scopeNode, ref.name);
450
+ if (targetNode) {
451
+ markSerializedFunction(targetNode);
452
+ }
453
+ }
224
454
  const forwardHotSet = new Set(directHotNodes);
225
455
  let forwardChanged = true;
226
456
  while (forwardChanged) {
227
457
  forwardChanged = false;
458
+ for (const fn of allFunctions) {
459
+ if (forwardHotSet.has(fn.node)) continue;
460
+ const chain = fnScopeChain.get(fn.node);
461
+ if (!chain) continue;
462
+ for (const anc of chain) {
463
+ if (anc !== fn.node && (serializedFunctionNodes.has(anc) || forwardHotSet.has(anc))) {
464
+ forwardHotSet.add(fn.node);
465
+ hotFunctionNodes.add(fn.node);
466
+ hotExemptNodes.add(fn.node);
467
+ if (serializedFunctionNodes.has(anc)) {
468
+ serializedFunctionNodes.add(fn.node);
469
+ }
470
+ forwardChanged = true;
471
+ break;
472
+ }
473
+ }
474
+ }
228
475
  for (const callerNode of Array.from(forwardHotSet)) {
229
476
  const calleeNames = callerToCallees.get(callerNode);
230
477
  if (!calleeNames) continue;
@@ -234,6 +481,9 @@ function analyzeAstHotPaths(ast) {
234
481
  forwardHotSet.add(targetNode);
235
482
  hotFunctionNodes.add(targetNode);
236
483
  hotExemptNodes.add(targetNode);
484
+ if (serializedFunctionNodes.has(callerNode)) {
485
+ serializedFunctionNodes.add(targetNode);
486
+ }
237
487
  forwardChanged = true;
238
488
  }
239
489
  }
@@ -308,27 +558,25 @@ var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
308
558
  "use asm"
309
559
  ]);
310
560
  var HELPER_TEMPLATE = acorn2.parse(
311
- `var _ = [].map(function f(a) {
312
- var s = "";
313
- for (var j = 0; j < a.length; j++) {
314
- s += String.fromCharCode(a.charCodeAt(j) ^ (((0 ^ ((a._u || /\\n/.test(f) * 14) | ((j & 1) * 0))) & 30) | 1));
315
- }
316
- return s;
317
- });`,
561
+ `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;});})([]);`,
318
562
  { ecmaVersion: "latest", sourceType: "script" }
319
563
  ).body[0];
320
564
  function createVaultHelperAst(cacheVarName, encryptedPayloads, mask, step) {
321
565
  const node = structuredClone(HELPER_TEMPLATE);
322
566
  node.declarations[0].id.name = cacheVarName;
323
- node.declarations[0].init.callee.object.elements = encryptedPayloads.map(
567
+ node.declarations[0].init.arguments[0].elements = encryptedPayloads.map(
324
568
  (str) => ({
325
569
  type: "Literal",
326
570
  value: str,
327
571
  raw: JSON.stringify(str)
328
572
  })
329
573
  );
330
- const fn = node.declarations[0].init.arguments[0];
331
- const binaryXor = fn.body.body[1].body.body[0].expression.right.arguments[0];
574
+ const outerFn = node.declarations[0].init.callee;
575
+ const returnStmt = outerFn.body.body[0];
576
+ const innerFn = returnStmt.argument.arguments[0];
577
+ const forStmt = innerFn.body.body[1];
578
+ const expr = forStmt.body.type === "BlockStatement" ? forStmt.body.body[0].expression : forStmt.body.expression;
579
+ const binaryXor = expr.right.arguments[0];
332
580
  const innerLeft = binaryXor.right.left.left;
333
581
  innerLeft.left.value = mask;
334
582
  innerLeft.left.raw = String(mask);
@@ -366,44 +614,58 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
366
614
  };
367
615
  }
368
616
  const mask = seed >>> 0 & 30 | 3;
369
- const step = (seed >>> 5 & 7) * 2 + 3;
617
+ const step = 1;
370
618
  const stringToIndex = /* @__PURE__ */ new Map();
371
619
  const encryptedPayloads = [];
372
620
  const targetsToReplace = [];
373
- walk2.ancestor(ast, {
374
- Literal(node, _state, ancestors) {
375
- if (typeof node.value !== "string") return;
376
- const val = node.value;
377
- if (val.length < minStringLength) return;
378
- if (DIRECTIVE_STRINGS.has(val)) return;
379
- if (hotExemptNodes) {
380
- for (let i = ancestors.length - 2; i >= 0; i--) {
381
- if (hotExemptNodes.has(ancestors[i])) {
382
- return;
621
+ const customWalkBase = {
622
+ ...walk2.base,
623
+ Property(node, st, c) {
624
+ c(node.key, st);
625
+ if (node.value) c(node.value, st);
626
+ }
627
+ };
628
+ walk2.ancestor(
629
+ ast,
630
+ {
631
+ Literal(node, _state, ancestors) {
632
+ if (typeof node.value !== "string") return;
633
+ const val = node.value;
634
+ if (val.length < minStringLength) return;
635
+ if (DIRECTIVE_STRINGS.has(val)) return;
636
+ if (hotExemptNodes) {
637
+ for (let i = ancestors.length - 2; i >= 0; i--) {
638
+ if (hotExemptNodes.has(ancestors[i])) {
639
+ return;
640
+ }
383
641
  }
384
642
  }
643
+ const parent = ancestors[ancestors.length - 2];
644
+ if (!parent) return;
645
+ if (parent.type === "ExpressionStatement" && parent.directive) return;
646
+ if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
647
+ if (parent.source === node) return;
648
+ }
649
+ if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
650
+ return;
651
+ }
652
+ if (parent.type === "Property" && parent.key === node) {
653
+ parent.computed = true;
654
+ }
655
+ if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
656
+ return;
657
+ }
658
+ let idx = stringToIndex.get(val);
659
+ if (idx === void 0) {
660
+ idx = encryptedPayloads.length;
661
+ stringToIndex.set(val, idx);
662
+ encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
663
+ }
664
+ targetsToReplace.push({ node, parent });
385
665
  }
386
- const parent = ancestors[ancestors.length - 2];
387
- if (!parent) return;
388
- if (parent.type === "ExpressionStatement" && parent.directive) return;
389
- if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
390
- if (parent.source === node) return;
391
- }
392
- if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
393
- return;
394
- }
395
- if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
396
- return;
397
- }
398
- let idx = stringToIndex.get(val);
399
- if (idx === void 0) {
400
- idx = encryptedPayloads.length;
401
- stringToIndex.set(val, idx);
402
- encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
403
- }
404
- targetsToReplace.push({ node });
405
- }
406
- });
666
+ },
667
+ customWalkBase
668
+ );
407
669
  if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
408
670
  return {
409
671
  encryptedStringsCount: 0,
@@ -414,7 +676,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
414
676
  const integrityToken = computeFnv1aHash(serializedTable, seed);
415
677
  const suffix = computeFnv1aHash(`vault:${seed}`, integrityToken).toString(36);
416
678
  const cacheVarName = `_uC${suffix}`;
417
- for (const { node } of targetsToReplace) {
679
+ for (const { node, parent } of targetsToReplace) {
680
+ if (parent?.type === "Property" && parent.key === node) {
681
+ parent.computed = true;
682
+ }
418
683
  const idx = stringToIndex.get(node.value);
419
684
  node.type = "MemberExpression";
420
685
  node.object = {
@@ -879,10 +1144,174 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
879
1144
 
880
1145
  // src/ast/cold-flow-guard.ts
881
1146
  import * as walk4 from "acorn-walk";
882
- function createOpaqueGuardStatement(seed, fnIndex) {
883
- const a = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) & 65535;
884
- const b = (a ^ 4951) & 65535;
885
- const wrongVal = (a ^ b) & 7 ^ 5;
1147
+ function createDecoyConsequent(fnIndex) {
1148
+ const model = Math.abs(fnIndex) % 3;
1149
+ if (model === 0) {
1150
+ return {
1151
+ type: "BlockStatement",
1152
+ body: [
1153
+ {
1154
+ type: "ReturnStatement",
1155
+ argument: {
1156
+ type: "ObjectExpression",
1157
+ properties: [
1158
+ {
1159
+ type: "Property",
1160
+ key: { type: "Identifier", name: "endpoint" },
1161
+ value: {
1162
+ type: "Literal",
1163
+ value: "/api/v2/auth/admin-bypass",
1164
+ raw: '"/api/v2/auth/admin-bypass"'
1165
+ },
1166
+ kind: "init",
1167
+ computed: false,
1168
+ method: false,
1169
+ shorthand: false
1170
+ },
1171
+ {
1172
+ type: "Property",
1173
+ key: { type: "Identifier", name: "superadmin_token" },
1174
+ value: { type: "Literal", value: 1, raw: "1" },
1175
+ kind: "init",
1176
+ computed: false,
1177
+ method: false,
1178
+ shorthand: false
1179
+ },
1180
+ {
1181
+ type: "Property",
1182
+ key: { type: "Identifier", name: "elevate" },
1183
+ value: { type: "Literal", value: 1, raw: "1" },
1184
+ kind: "init",
1185
+ computed: false,
1186
+ method: false,
1187
+ shorthand: false
1188
+ }
1189
+ ]
1190
+ }
1191
+ }
1192
+ ]
1193
+ };
1194
+ }
1195
+ if (model === 1) {
1196
+ return {
1197
+ type: "BlockStatement",
1198
+ body: [
1199
+ {
1200
+ type: "ReturnStatement",
1201
+ argument: {
1202
+ type: "ObjectExpression",
1203
+ properties: [
1204
+ {
1205
+ type: "Property",
1206
+ key: { type: "Identifier", name: "endpoint" },
1207
+ value: {
1208
+ type: "Literal",
1209
+ value: "/api/v2/auth/verify-master",
1210
+ raw: '"/api/v2/auth/verify-master"'
1211
+ },
1212
+ kind: "init",
1213
+ computed: false,
1214
+ method: false,
1215
+ shorthand: false
1216
+ },
1217
+ {
1218
+ type: "Property",
1219
+ key: { type: "Identifier", name: "_verifyMasterToken" },
1220
+ value: { type: "Literal", value: true, raw: "true" },
1221
+ kind: "init",
1222
+ computed: false,
1223
+ method: false,
1224
+ shorthand: false
1225
+ },
1226
+ {
1227
+ type: "Property",
1228
+ key: { type: "Identifier", name: "_bypassToken" },
1229
+ value: {
1230
+ type: "Literal",
1231
+ value: "mock_jwt_bypass_signature",
1232
+ raw: '"mock_jwt_bypass_signature"'
1233
+ },
1234
+ kind: "init",
1235
+ computed: false,
1236
+ method: false,
1237
+ shorthand: false
1238
+ },
1239
+ {
1240
+ type: "Property",
1241
+ key: { type: "Identifier", name: "authorized" },
1242
+ value: { type: "Literal", value: true, raw: "true" },
1243
+ kind: "init",
1244
+ computed: false,
1245
+ method: false,
1246
+ shorthand: false
1247
+ }
1248
+ ]
1249
+ }
1250
+ }
1251
+ ]
1252
+ };
1253
+ }
1254
+ return {
1255
+ type: "BlockStatement",
1256
+ body: [
1257
+ {
1258
+ type: "ReturnStatement",
1259
+ argument: {
1260
+ type: "ObjectExpression",
1261
+ properties: [
1262
+ {
1263
+ type: "Property",
1264
+ key: { type: "Identifier", name: "endpoint" },
1265
+ value: {
1266
+ type: "Literal",
1267
+ value: "/api/v2/auth/debug-telemetry",
1268
+ raw: '"/api/v2/auth/debug-telemetry"'
1269
+ },
1270
+ kind: "init",
1271
+ computed: false,
1272
+ method: false,
1273
+ shorthand: false
1274
+ },
1275
+ {
1276
+ type: "Property",
1277
+ key: { type: "Identifier", name: "debug_supervisor_override" },
1278
+ value: { type: "Literal", value: true, raw: "true" },
1279
+ kind: "init",
1280
+ computed: false,
1281
+ method: false,
1282
+ shorthand: false
1283
+ },
1284
+ {
1285
+ type: "Property",
1286
+ key: { type: "Identifier", name: "_bypassToken" },
1287
+ value: {
1288
+ type: "Literal",
1289
+ value: "superadmin_token",
1290
+ raw: '"superadmin_token"'
1291
+ },
1292
+ kind: "init",
1293
+ computed: false,
1294
+ method: false,
1295
+ shorthand: false
1296
+ },
1297
+ {
1298
+ type: "Property",
1299
+ key: { type: "Identifier", name: "mfa_bypassed" },
1300
+ value: { type: "Literal", value: true, raw: "true" },
1301
+ kind: "init",
1302
+ computed: false,
1303
+ method: false,
1304
+ shorthand: false
1305
+ }
1306
+ ]
1307
+ }
1308
+ }
1309
+ ]
1310
+ };
1311
+ }
1312
+ function createSotaMbaPredicate(seed, fnIndex = 0) {
1313
+ const x = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) | 0;
1314
+ const y = x ^ 1515870810 | 0;
886
1315
  return {
887
1316
  type: "IfStatement",
888
1317
  test: {
@@ -890,41 +1319,57 @@ function createOpaqueGuardStatement(seed, fnIndex) {
890
1319
  operator: "===",
891
1320
  left: {
892
1321
  type: "BinaryExpression",
893
- operator: "&",
1322
+ operator: "|",
894
1323
  left: {
895
1324
  type: "BinaryExpression",
896
1325
  operator: "^",
897
- left: { type: "Literal", value: a, raw: String(a) },
898
- right: {
1326
+ left: {
899
1327
  type: "BinaryExpression",
900
- operator: "|",
901
- left: { type: "Literal", value: b, raw: String(b) },
1328
+ operator: "+",
1329
+ left: {
1330
+ type: "BinaryExpression",
1331
+ operator: "|",
1332
+ left: { type: "Literal", value: x, raw: String(x) },
1333
+ right: { type: "Literal", value: y, raw: String(y) }
1334
+ },
902
1335
  right: {
903
- type: "UnaryExpression",
904
- operator: "!",
905
- prefix: true,
906
- argument: { type: "Identifier", name: "String" }
1336
+ type: "BinaryExpression",
1337
+ operator: "&",
1338
+ left: { type: "Literal", value: x, raw: String(x) },
1339
+ right: { type: "Literal", value: y, raw: String(y) }
907
1340
  }
1341
+ },
1342
+ right: {
1343
+ type: "BinaryExpression",
1344
+ operator: "+",
1345
+ left: { type: "Literal", value: x, raw: String(x) },
1346
+ right: { type: "Literal", value: y, raw: String(y) }
908
1347
  }
909
1348
  },
910
- right: { type: "Literal", value: 7, raw: "7" }
1349
+ right: {
1350
+ type: "UnaryExpression",
1351
+ operator: "!",
1352
+ prefix: true,
1353
+ argument: { type: "Identifier", name: "String" }
1354
+ }
911
1355
  },
912
- right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
913
- },
914
- consequent: {
915
- type: "ReturnStatement",
916
- argument: null
1356
+ right: { type: "Literal", value: 2147483647, raw: "0x7fffffff" }
917
1357
  },
1358
+ consequent: createDecoyConsequent(fnIndex),
918
1359
  alternate: null
919
1360
  };
920
1361
  }
921
- function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
1362
+ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
922
1363
  let guardedCount = 0;
923
1364
  function maybeInjectGuard(node) {
924
1365
  if (!coldFunctionNodes.has(node)) return;
925
1366
  if (!node.body || node.body.type !== "BlockStatement") return;
1367
+ const guardStmt = createSotaMbaPredicate(seed, 0);
926
1368
  guardedCount++;
927
- const guardStmt = createOpaqueGuardStatement(seed, guardedCount);
1369
+ if (hotExemptNodes) {
1370
+ hotExemptNodes.add(guardStmt);
1371
+ hotExemptNodes.add(guardStmt.consequent);
1372
+ }
928
1373
  let insertIdx = 0;
929
1374
  while (insertIdx < node.body.body.length && node.body.body[insertIdx].type === "ExpressionStatement" && node.body.body[insertIdx].directive) {
930
1375
  insertIdx++;
@@ -956,25 +1401,12 @@ function deriveIntegrityFnName(seed) {
956
1401
  const hash = computeIntegrityHash(`unpeek:fn:${seed}`, seed);
957
1402
  return `_u$${hash.toString(36)}`;
958
1403
  }
959
- function deriveLocalVar(label, seed) {
960
- const hash = computeIntegrityHash(`unpeek:var:${label}:${seed}`, seed) & 1048575;
961
- return `_${label}${hash.toString(36)}`;
962
- }
963
1404
  function createDeterrentSnippet(config) {
964
1405
  const seed = config.seed >>> 0;
965
- const expectedToken = (config.expectedIntegrityHash ?? seed) >>> 0;
966
1406
  const deterrentMode = config.deterrentMode ?? "passive";
967
1407
  const fnName = deriveIntegrityFnName(seed);
968
- const strVar = deriveLocalVar("s", seed);
969
- const tamperVar = deriveLocalVar("t", seed);
970
- const noopVar = deriveLocalVar("n", seed);
971
- const methodsVar = deriveLocalVar("m", seed);
972
- const idxVar = deriveLocalVar("i", seed);
973
- const errVar = deriveLocalVar("e", seed);
974
- const tokenBit = expectedToken & 1;
975
- const gradualConsoleSilence = deterrentMode === "gradual" ? `if(typeof console!=='undefined'){var ${noopVar}=function(){},${methodsVar}=['log','debug','info','table','dir'];for(var ${idxVar}=0;${idxVar}<${methodsVar}.length;${idxVar}++){try{console[${methodsVar}[${idxVar}]]=${noopVar};}catch(${errVar}){}}}` : "";
976
- const tamperDispatch = `if(window.dispatchEvent&&typeof CustomEvent!=='undefined'){try{window.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(${errVar}){}}`;
977
- return `(function ${fnName}(){if(typeof window!=='undefined'){try{var ${strVar}=Function.prototype.toString,${tamperVar}=${strVar}.call(${strVar}).indexOf('[native code]')<${tokenBit - 1}||/[\\r\\n]/.test(${strVar}.call(${fnName}));if(${tamperVar}){window.__unpeek_tampered=!0;${tamperDispatch}${gradualConsoleSilence}String.prototype._u=14;}}catch(${errVar}){}}})();`;
1408
+ const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=f=>0;" : "";
1409
+ 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);`;
978
1410
  }
979
1411
 
980
1412
  // src/runtime/index.ts
@@ -1039,7 +1471,7 @@ function transformJavaScript(code, options = {}) {
1039
1471
  coldFunctionNodes,
1040
1472
  hotExemptNodes
1041
1473
  } = analyzeAstHotPaths(ast);
1042
- applyColdFlowGuard(ast, coldFunctionNodes, seed);
1474
+ applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
1043
1475
  const mangledSymbolsCount = applyPolymorphicMangler(ast, seed);
1044
1476
  const { encryptedStringsCount, integrityToken } = applyStringVault(
1045
1477
  ast,