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/browser.cjs CHANGED
@@ -134,15 +134,100 @@ function analyzeAstHotPaths(ast) {
134
134
  const coldFunctionNodes = /* @__PURE__ */ new WeakSet();
135
135
  const hotExemptNodes = /* @__PURE__ */ new WeakSet();
136
136
  const directHotNodes = /* @__PURE__ */ new Set();
137
+ const serializedFunctionNodes = /* @__PURE__ */ new Set();
138
+ const serializerHelperNodes = /* @__PURE__ */ new Set();
137
139
  const allFunctions = [];
138
140
  const fnByName = /* @__PURE__ */ new Map();
139
141
  const scopeFunctions = /* @__PURE__ */ new Map();
140
142
  const fnScopeChain = /* @__PURE__ */ new Map();
143
+ const fnParams = /* @__PURE__ */ new Map();
144
+ const fnVarAliases = /* @__PURE__ */ new Map();
141
145
  const callerToCallees = /* @__PURE__ */ new Map();
146
+ const pendingSerializedRefs = [];
147
+ const allCallSites = [];
148
+ function ensureFnParams(fnNode) {
149
+ let paramSet = fnParams.get(fnNode);
150
+ if (!paramSet) {
151
+ paramSet = /* @__PURE__ */ new Set();
152
+ for (const p of fnNode.params || []) {
153
+ if (p?.type === "Identifier") {
154
+ paramSet.add(p.name);
155
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
156
+ paramSet.add(p.left.name);
157
+ }
158
+ }
159
+ fnParams.set(fnNode, paramSet);
160
+ }
161
+ return paramSet;
162
+ }
163
+ function recordVarAlias(scopeNode, varName, rhsName) {
164
+ let map = fnVarAliases.get(scopeNode);
165
+ if (!map) {
166
+ map = /* @__PURE__ */ new Map();
167
+ fnVarAliases.set(scopeNode, map);
168
+ }
169
+ map.set(varName, rhsName);
170
+ }
171
+ function resolveAliasTarget(scopeNode, identName) {
172
+ const map = fnVarAliases.get(scopeNode);
173
+ let cur = identName;
174
+ const seen = /* @__PURE__ */ new Set();
175
+ while (map && map.has(cur) && !seen.has(cur)) {
176
+ seen.add(cur);
177
+ cur = map.get(cur);
178
+ }
179
+ return cur;
180
+ }
181
+ function markSerializedFunction(node) {
182
+ serializedFunctionNodes.add(node);
183
+ hotFunctionNodes.add(node);
184
+ directHotNodes.add(node);
185
+ hotExemptNodes.add(node);
186
+ }
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
+ }
142
218
  function registerFunctionNode(node, ancestors) {
143
219
  const name = getFunctionLabel(node, ancestors);
144
220
  allFunctions.push({ node, name });
145
221
  fnByName.set(name, node);
222
+ const paramSet = /* @__PURE__ */ new Set();
223
+ for (const p of node.params || []) {
224
+ if (p?.type === "Identifier") {
225
+ paramSet.add(p.name);
226
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
227
+ paramSet.add(p.left.name);
228
+ }
229
+ }
230
+ fnParams.set(node, paramSet);
146
231
  const enclosingScope = findEnclosingScopeNode(ancestors, node);
147
232
  let localMap = scopeFunctions.get(enclosingScope);
148
233
  if (!localMap) {
@@ -151,6 +236,22 @@ function analyzeAstHotPaths(ast) {
151
236
  }
152
237
  localMap.set(name, node);
153
238
  const parent = ancestors[ancestors.length - 2];
239
+ if (parent?.type === "VariableDeclarator" && parent.id?.type === "Identifier") {
240
+ fnByName.set(parent.id.name, node);
241
+ localMap.set(parent.id.name, node);
242
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "Identifier") {
243
+ fnByName.set(parent.left.name, node);
244
+ localMap.set(parent.left.name, node);
245
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "MemberExpression" && !parent.left.computed && parent.left.property?.type === "Identifier") {
246
+ const propName = parent.left.property.name;
247
+ fnByName.set(propName, node);
248
+ localMap.set(propName, node);
249
+ if (parent.left.object?.type === "Identifier") {
250
+ const qual = `${parent.left.object.name}.${propName}`;
251
+ fnByName.set(qual, node);
252
+ localMap.set(qual, node);
253
+ }
254
+ }
154
255
  const grandParent = ancestors[ancestors.length - 3];
155
256
  const greatGrandParent = ancestors[ancestors.length - 4];
156
257
  if (parent?.type === "Property" && parent.key?.type === "Identifier" && grandParent?.type === "ObjectExpression" && greatGrandParent?.type === "VariableDeclarator" && greatGrandParent.id?.type === "Identifier") {
@@ -160,7 +261,7 @@ function analyzeAstHotPaths(ast) {
160
261
  }
161
262
  const chain = [];
162
263
  for (let i = ancestors.length - 1; i >= 0; i--) {
163
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program") {
264
+ if (ancestors[i] !== node && (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program")) {
164
265
  chain.push(ancestors[i]);
165
266
  }
166
267
  }
@@ -169,8 +270,13 @@ function analyzeAstHotPaths(ast) {
169
270
  hotFunctionNodes.add(node);
170
271
  directHotNodes.add(node);
171
272
  }
273
+ if (isZeroArgIifeFactoryFunction(node)) {
274
+ markSerializedFunction(node);
275
+ }
172
276
  }
173
277
  function resolveCalleeNode(callerNode, calleeKey) {
278
+ const directLocal = scopeFunctions.get(callerNode)?.get(calleeKey);
279
+ if (directLocal) return directLocal;
174
280
  const chain = fnScopeChain.get(callerNode);
175
281
  if (chain) {
176
282
  for (const scopeNode of chain) {
@@ -181,6 +287,25 @@ function analyzeAstHotPaths(ast) {
181
287
  }
182
288
  return fnByName.get(calleeKey);
183
289
  }
290
+ function queueSerializedTarget(target, enclosingScope) {
291
+ if (!target) return;
292
+ if (FUNCTION_NODE_TYPES.has(target.type)) {
293
+ markSerializedFunction(target);
294
+ } else if (target.type === "Identifier") {
295
+ pendingSerializedRefs.push({ scopeNode: enclosingScope, name: target.name });
296
+ } else if (target.type === "MemberExpression" && !target.computed && target.property?.type === "Identifier") {
297
+ pendingSerializedRefs.push({
298
+ scopeNode: enclosingScope,
299
+ name: target.property.name
300
+ });
301
+ if (target.object?.type === "Identifier") {
302
+ pendingSerializedRefs.push({
303
+ scopeNode: enclosingScope,
304
+ name: `${target.object.name}.${target.property.name}`
305
+ });
306
+ }
307
+ }
308
+ }
184
309
  walk.ancestor(ast, {
185
310
  FunctionDeclaration(node, _state, ancestors) {
186
311
  registerFunctionNode(node, ancestors);
@@ -191,6 +316,59 @@ function analyzeAstHotPaths(ast) {
191
316
  ArrowFunctionExpression(node, _state, ancestors) {
192
317
  registerFunctionNode(node, ancestors);
193
318
  },
319
+ VariableDeclarator(node, _state, ancestors) {
320
+ if (node.id?.type === "Identifier" && node.init?.type === "Identifier") {
321
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
322
+ recordVarAlias(scopeNode, node.id.name, node.init.name);
323
+ }
324
+ },
325
+ AssignmentExpression(node, _state, ancestors) {
326
+ const left = node.left;
327
+ if (left?.type === "Identifier" && node.right?.type === "Identifier") {
328
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
329
+ recordVarAlias(scopeNode, left.name, node.right.name);
330
+ }
331
+ const isBareOnMessage = left?.type === "Identifier" && left.name === "onmessage";
332
+ const isSelfOnMessage = left?.type === "MemberExpression" && !left.computed && left.object?.type === "Identifier" && left.object.name === "self" && left.property?.type === "Identifier" && left.property.name === "onmessage";
333
+ if (isBareOnMessage || isSelfOnMessage) {
334
+ const enclosingFn = findEnclosingFunction(ancestors);
335
+ if (enclosingFn) {
336
+ markSerializedFunction(enclosingFn);
337
+ }
338
+ const outerFn = findOutermostModuleFunction(ancestors);
339
+ if (outerFn) {
340
+ markSerializedFunction(outerFn);
341
+ }
342
+ const scopeNode = enclosingFn ?? ast;
343
+ queueSerializedTarget(node.right, scopeNode);
344
+ }
345
+ },
346
+ ObjectExpression(node, _state, ancestors) {
347
+ const props = node.properties || [];
348
+ const propMap = /* @__PURE__ */ new Map();
349
+ for (const p of props) {
350
+ if (p?.type === "Property" && !p.computed) {
351
+ const keyName = p.key?.type === "Identifier" ? p.key.name : p.key?.type === "Literal" ? String(p.key.value) : "";
352
+ if (keyName) {
353
+ propMap.set(keyName, p.value);
354
+ }
355
+ }
356
+ }
357
+ const hasWorkerDescriptorShape = propMap.has("init") && propMap.has("dependencies") || propMap.has("isWorkerModule") || propMap.has("getTransferables") && (propMap.has("init") || propMap.has("dependencies"));
358
+ if (hasWorkerDescriptorShape) {
359
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
360
+ for (const key of ["init", "getTransferables", "worker"]) {
361
+ const val = propMap.get(key);
362
+ if (val) queueSerializedTarget(val, scopeNode);
363
+ }
364
+ const deps = propMap.get("dependencies");
365
+ if (deps?.type === "ArrayExpression") {
366
+ for (const el of deps.elements || []) {
367
+ if (el) queueSerializedTarget(el, scopeNode);
368
+ }
369
+ }
370
+ }
371
+ },
194
372
  CallExpression(node, _state, ancestors) {
195
373
  const callee = node.callee;
196
374
  let calleeName = "";
@@ -207,38 +385,76 @@ function analyzeAstHotPaths(ast) {
207
385
  qualifiedCall = memberProp;
208
386
  }
209
387
  }
388
+ const enclosingFn = findEnclosingFunction(ancestors);
389
+ const currentScope = enclosingFn ?? ast;
210
390
  if (qualifiedCall || memberProp) {
211
- for (let i = ancestors.length - 1; i >= 0; i--) {
212
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
213
- let set = callerToCallees.get(ancestors[i]);
214
- if (!set) {
215
- set = /* @__PURE__ */ new Set();
216
- callerToCallees.set(ancestors[i], set);
217
- }
218
- if (qualifiedCall) set.add(qualifiedCall);
219
- if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
220
- set.add(memberProp);
221
- }
222
- break;
391
+ allCallSites.push({
392
+ scopeNode: currentScope,
393
+ calleeKey: qualifiedCall || memberProp,
394
+ args: node.arguments || []
395
+ });
396
+ if (enclosingFn) {
397
+ let set = callerToCallees.get(enclosingFn);
398
+ if (!set) {
399
+ set = /* @__PURE__ */ new Set();
400
+ callerToCallees.set(enclosingFn, set);
401
+ }
402
+ if (qualifiedCall) set.add(qualifiedCall);
403
+ if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
404
+ set.add(memberProp);
405
+ }
406
+ }
407
+ }
408
+ if (memberProp === "toString" && (!node.arguments || node.arguments.length === 0) && callee?.type === "MemberExpression") {
409
+ const targetObj = callee.object;
410
+ queueSerializedTarget(targetObj, currentScope);
411
+ if (targetObj?.type === "Identifier" && enclosingFn) {
412
+ const resolvedName = resolveAliasTarget(enclosingFn, targetObj.name);
413
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
414
+ serializerHelperNodes.add(enclosingFn);
415
+ }
416
+ if (resolvedName !== targetObj.name) {
417
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
223
418
  }
224
419
  }
225
420
  }
421
+ 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) {
422
+ const firstArg = node.arguments[0];
423
+ queueSerializedTarget(firstArg, currentScope);
424
+ if (firstArg?.type === "Identifier" && enclosingFn) {
425
+ const resolvedName = resolveAliasTarget(enclosingFn, firstArg.name);
426
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
427
+ serializerHelperNodes.add(enclosingFn);
428
+ }
429
+ if (resolvedName !== firstArg.name) {
430
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
431
+ }
432
+ }
433
+ }
434
+ if (calleeName === "importScripts" || memberProp === "importScripts" && callee?.object?.type === "Identifier" && callee.object.name === "self") {
435
+ if (enclosingFn) markSerializedFunction(enclosingFn);
436
+ const outerFn = findOutermostModuleFunction(ancestors);
437
+ if (outerFn) markSerializedFunction(outerFn);
438
+ }
226
439
  if (ARRAY_HOF_METHODS.has(memberProp) || calleeName === "requestAnimationFrame" || memberProp === "requestAnimationFrame" || calleeName === "cancelAnimationFrame" || memberProp === "cancelAnimationFrame") {
227
440
  for (const arg of node.arguments || []) {
228
441
  if (FUNCTION_NODE_TYPES.has(arg.type)) {
229
442
  hotFunctionNodes.add(arg);
230
443
  directHotNodes.add(arg);
231
444
  hotExemptNodes.add(arg);
445
+ } else if (arg?.type === "Identifier") {
446
+ allCallSites.push({
447
+ scopeNode: currentScope,
448
+ calleeKey: arg.name,
449
+ args: [arg]
450
+ });
232
451
  }
233
452
  }
234
453
  }
235
454
  if (UI_HOOK_OR_RENDER_CALLS.has(calleeName) || UI_HOOK_OR_RENDER_CALLS.has(memberProp)) {
236
- for (let i = ancestors.length - 1; i >= 0; i--) {
237
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
238
- hotFunctionNodes.add(ancestors[i]);
239
- directHotNodes.add(ancestors[i]);
240
- break;
241
- }
455
+ if (enclosingFn) {
456
+ hotFunctionNodes.add(enclosingFn);
457
+ directHotNodes.add(enclosingFn);
242
458
  }
243
459
  }
244
460
  },
@@ -263,10 +479,41 @@ function analyzeAstHotPaths(ast) {
263
479
  markEnclosingFunctionHot(ancestors, hotFunctionNodes, directHotNodes);
264
480
  }
265
481
  });
482
+ for (const callSite of allCallSites) {
483
+ const resolvedCallee = resolveCalleeNode(callSite.scopeNode, callSite.calleeKey);
484
+ if (resolvedCallee && serializerHelperNodes.has(resolvedCallee)) {
485
+ for (const arg of callSite.args) {
486
+ queueSerializedTarget(arg, callSite.scopeNode);
487
+ }
488
+ }
489
+ }
490
+ for (const ref of pendingSerializedRefs) {
491
+ const targetNode = resolveCalleeNode(ref.scopeNode, ref.name);
492
+ if (targetNode) {
493
+ markSerializedFunction(targetNode);
494
+ }
495
+ }
266
496
  const forwardHotSet = new Set(directHotNodes);
267
497
  let forwardChanged = true;
268
498
  while (forwardChanged) {
269
499
  forwardChanged = false;
500
+ for (const fn of allFunctions) {
501
+ if (forwardHotSet.has(fn.node)) continue;
502
+ const chain = fnScopeChain.get(fn.node);
503
+ if (!chain) continue;
504
+ 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)) {
510
+ serializedFunctionNodes.add(fn.node);
511
+ }
512
+ forwardChanged = true;
513
+ break;
514
+ }
515
+ }
516
+ }
270
517
  for (const callerNode of Array.from(forwardHotSet)) {
271
518
  const calleeNames = callerToCallees.get(callerNode);
272
519
  if (!calleeNames) continue;
@@ -276,6 +523,9 @@ function analyzeAstHotPaths(ast) {
276
523
  forwardHotSet.add(targetNode);
277
524
  hotFunctionNodes.add(targetNode);
278
525
  hotExemptNodes.add(targetNode);
526
+ if (serializedFunctionNodes.has(callerNode)) {
527
+ serializedFunctionNodes.add(targetNode);
528
+ }
279
529
  forwardChanged = true;
280
530
  }
281
531
  }
@@ -350,27 +600,25 @@ var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
350
600
  "use asm"
351
601
  ]);
352
602
  var HELPER_TEMPLATE = acorn2.parse(
353
- `var _ = [].map(function f(a) {
354
- var s = "";
355
- for (var j = 0; j < a.length; j++) {
356
- s += String.fromCharCode(a.charCodeAt(j) ^ (((0 ^ ((a._u || /\\n/.test(f) * 14) | ((j & 1) * 0))) & 30) | 1));
357
- }
358
- return s;
359
- });`,
603
+ `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;});})([]);`,
360
604
  { ecmaVersion: "latest", sourceType: "script" }
361
605
  ).body[0];
362
606
  function createVaultHelperAst(cacheVarName, encryptedPayloads, mask, step) {
363
607
  const node = structuredClone(HELPER_TEMPLATE);
364
608
  node.declarations[0].id.name = cacheVarName;
365
- node.declarations[0].init.callee.object.elements = encryptedPayloads.map(
609
+ node.declarations[0].init.arguments[0].elements = encryptedPayloads.map(
366
610
  (str) => ({
367
611
  type: "Literal",
368
612
  value: str,
369
613
  raw: JSON.stringify(str)
370
614
  })
371
615
  );
372
- const fn = node.declarations[0].init.arguments[0];
373
- const binaryXor = fn.body.body[1].body.body[0].expression.right.arguments[0];
616
+ const outerFn = node.declarations[0].init.callee;
617
+ const returnStmt = outerFn.body.body[0];
618
+ const innerFn = returnStmt.argument.arguments[0];
619
+ const forStmt = innerFn.body.body[1];
620
+ const expr = forStmt.body.type === "BlockStatement" ? forStmt.body.body[0].expression : forStmt.body.expression;
621
+ const binaryXor = expr.right.arguments[0];
374
622
  const innerLeft = binaryXor.right.left.left;
375
623
  innerLeft.left.value = mask;
376
624
  innerLeft.left.raw = String(mask);
@@ -408,44 +656,58 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
408
656
  };
409
657
  }
410
658
  const mask = seed >>> 0 & 30 | 3;
411
- const step = (seed >>> 5 & 7) * 2 + 3;
659
+ const step = 1;
412
660
  const stringToIndex = /* @__PURE__ */ new Map();
413
661
  const encryptedPayloads = [];
414
662
  const targetsToReplace = [];
415
- walk2.ancestor(ast, {
416
- Literal(node, _state, ancestors) {
417
- if (typeof node.value !== "string") return;
418
- const val = node.value;
419
- if (val.length < minStringLength) return;
420
- if (DIRECTIVE_STRINGS.has(val)) return;
421
- if (hotExemptNodes) {
422
- for (let i = ancestors.length - 2; i >= 0; i--) {
423
- if (hotExemptNodes.has(ancestors[i])) {
424
- return;
663
+ const customWalkBase = {
664
+ ...walk2.base,
665
+ Property(node, st, c) {
666
+ c(node.key, st);
667
+ if (node.value) c(node.value, st);
668
+ }
669
+ };
670
+ walk2.ancestor(
671
+ ast,
672
+ {
673
+ Literal(node, _state, ancestors) {
674
+ if (typeof node.value !== "string") return;
675
+ const val = node.value;
676
+ if (val.length < minStringLength) return;
677
+ if (DIRECTIVE_STRINGS.has(val)) return;
678
+ if (hotExemptNodes) {
679
+ for (let i = ancestors.length - 2; i >= 0; i--) {
680
+ if (hotExemptNodes.has(ancestors[i])) {
681
+ return;
682
+ }
425
683
  }
426
684
  }
685
+ const parent = ancestors[ancestors.length - 2];
686
+ if (!parent) return;
687
+ if (parent.type === "ExpressionStatement" && parent.directive) return;
688
+ if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
689
+ if (parent.source === node) return;
690
+ }
691
+ if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
692
+ return;
693
+ }
694
+ if (parent.type === "Property" && parent.key === node) {
695
+ parent.computed = true;
696
+ }
697
+ if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
698
+ return;
699
+ }
700
+ let idx = stringToIndex.get(val);
701
+ if (idx === void 0) {
702
+ idx = encryptedPayloads.length;
703
+ stringToIndex.set(val, idx);
704
+ encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
705
+ }
706
+ targetsToReplace.push({ node, parent });
427
707
  }
428
- const parent = ancestors[ancestors.length - 2];
429
- if (!parent) return;
430
- if (parent.type === "ExpressionStatement" && parent.directive) return;
431
- if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
432
- if (parent.source === node) return;
433
- }
434
- if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
435
- return;
436
- }
437
- if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
438
- return;
439
- }
440
- let idx = stringToIndex.get(val);
441
- if (idx === void 0) {
442
- idx = encryptedPayloads.length;
443
- stringToIndex.set(val, idx);
444
- encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
445
- }
446
- targetsToReplace.push({ node });
447
- }
448
- });
708
+ },
709
+ customWalkBase
710
+ );
449
711
  if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
450
712
  return {
451
713
  encryptedStringsCount: 0,
@@ -456,7 +718,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
456
718
  const integrityToken = computeFnv1aHash(serializedTable, seed);
457
719
  const suffix = computeFnv1aHash(`vault:${seed}`, integrityToken).toString(36);
458
720
  const cacheVarName = `_uC${suffix}`;
459
- for (const { node } of targetsToReplace) {
721
+ for (const { node, parent } of targetsToReplace) {
722
+ if (parent?.type === "Property" && parent.key === node) {
723
+ parent.computed = true;
724
+ }
460
725
  const idx = stringToIndex.get(node.value);
461
726
  node.type = "MemberExpression";
462
727
  node.object = {
@@ -921,10 +1186,174 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
921
1186
 
922
1187
  // src/ast/cold-flow-guard.ts
923
1188
  var walk4 = __toESM(require("acorn-walk"), 1);
924
- function createOpaqueGuardStatement(seed, fnIndex) {
925
- const a = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) & 65535;
926
- const b = (a ^ 4951) & 65535;
927
- const wrongVal = (a ^ b) & 7 ^ 5;
1189
+ function createDecoyConsequent(fnIndex) {
1190
+ const model = Math.abs(fnIndex) % 3;
1191
+ if (model === 0) {
1192
+ return {
1193
+ type: "BlockStatement",
1194
+ body: [
1195
+ {
1196
+ type: "ReturnStatement",
1197
+ argument: {
1198
+ type: "ObjectExpression",
1199
+ properties: [
1200
+ {
1201
+ type: "Property",
1202
+ key: { type: "Identifier", name: "endpoint" },
1203
+ value: {
1204
+ type: "Literal",
1205
+ value: "/api/v2/auth/admin-bypass",
1206
+ raw: '"/api/v2/auth/admin-bypass"'
1207
+ },
1208
+ kind: "init",
1209
+ computed: false,
1210
+ method: false,
1211
+ shorthand: false
1212
+ },
1213
+ {
1214
+ type: "Property",
1215
+ key: { type: "Identifier", name: "superadmin_token" },
1216
+ value: { type: "Literal", value: 1, raw: "1" },
1217
+ kind: "init",
1218
+ computed: false,
1219
+ method: false,
1220
+ shorthand: false
1221
+ },
1222
+ {
1223
+ type: "Property",
1224
+ key: { type: "Identifier", name: "elevate" },
1225
+ value: { type: "Literal", value: 1, raw: "1" },
1226
+ kind: "init",
1227
+ computed: false,
1228
+ method: false,
1229
+ shorthand: false
1230
+ }
1231
+ ]
1232
+ }
1233
+ }
1234
+ ]
1235
+ };
1236
+ }
1237
+ if (model === 1) {
1238
+ return {
1239
+ type: "BlockStatement",
1240
+ body: [
1241
+ {
1242
+ type: "ReturnStatement",
1243
+ argument: {
1244
+ type: "ObjectExpression",
1245
+ properties: [
1246
+ {
1247
+ type: "Property",
1248
+ key: { type: "Identifier", name: "endpoint" },
1249
+ value: {
1250
+ type: "Literal",
1251
+ value: "/api/v2/auth/verify-master",
1252
+ raw: '"/api/v2/auth/verify-master"'
1253
+ },
1254
+ kind: "init",
1255
+ computed: false,
1256
+ method: false,
1257
+ shorthand: false
1258
+ },
1259
+ {
1260
+ type: "Property",
1261
+ key: { type: "Identifier", name: "_verifyMasterToken" },
1262
+ value: { type: "Literal", value: true, raw: "true" },
1263
+ kind: "init",
1264
+ computed: false,
1265
+ method: false,
1266
+ shorthand: false
1267
+ },
1268
+ {
1269
+ type: "Property",
1270
+ key: { type: "Identifier", name: "_bypassToken" },
1271
+ value: {
1272
+ type: "Literal",
1273
+ value: "mock_jwt_bypass_signature",
1274
+ raw: '"mock_jwt_bypass_signature"'
1275
+ },
1276
+ kind: "init",
1277
+ computed: false,
1278
+ method: false,
1279
+ shorthand: false
1280
+ },
1281
+ {
1282
+ type: "Property",
1283
+ key: { type: "Identifier", name: "authorized" },
1284
+ value: { type: "Literal", value: true, raw: "true" },
1285
+ kind: "init",
1286
+ computed: false,
1287
+ method: false,
1288
+ shorthand: false
1289
+ }
1290
+ ]
1291
+ }
1292
+ }
1293
+ ]
1294
+ };
1295
+ }
1296
+ return {
1297
+ type: "BlockStatement",
1298
+ body: [
1299
+ {
1300
+ type: "ReturnStatement",
1301
+ argument: {
1302
+ type: "ObjectExpression",
1303
+ properties: [
1304
+ {
1305
+ type: "Property",
1306
+ key: { type: "Identifier", name: "endpoint" },
1307
+ value: {
1308
+ type: "Literal",
1309
+ value: "/api/v2/auth/debug-telemetry",
1310
+ raw: '"/api/v2/auth/debug-telemetry"'
1311
+ },
1312
+ kind: "init",
1313
+ computed: false,
1314
+ method: false,
1315
+ shorthand: false
1316
+ },
1317
+ {
1318
+ type: "Property",
1319
+ key: { type: "Identifier", name: "debug_supervisor_override" },
1320
+ value: { type: "Literal", value: true, raw: "true" },
1321
+ kind: "init",
1322
+ computed: false,
1323
+ method: false,
1324
+ shorthand: false
1325
+ },
1326
+ {
1327
+ type: "Property",
1328
+ key: { type: "Identifier", name: "_bypassToken" },
1329
+ value: {
1330
+ type: "Literal",
1331
+ value: "superadmin_token",
1332
+ raw: '"superadmin_token"'
1333
+ },
1334
+ kind: "init",
1335
+ computed: false,
1336
+ method: false,
1337
+ shorthand: false
1338
+ },
1339
+ {
1340
+ type: "Property",
1341
+ key: { type: "Identifier", name: "mfa_bypassed" },
1342
+ value: { type: "Literal", value: true, raw: "true" },
1343
+ kind: "init",
1344
+ computed: false,
1345
+ method: false,
1346
+ shorthand: false
1347
+ }
1348
+ ]
1349
+ }
1350
+ }
1351
+ ]
1352
+ };
1353
+ }
1354
+ function createSotaMbaPredicate(seed, fnIndex = 0) {
1355
+ const x = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) | 0;
1356
+ const y = x ^ 1515870810 | 0;
928
1357
  return {
929
1358
  type: "IfStatement",
930
1359
  test: {
@@ -932,41 +1361,57 @@ function createOpaqueGuardStatement(seed, fnIndex) {
932
1361
  operator: "===",
933
1362
  left: {
934
1363
  type: "BinaryExpression",
935
- operator: "&",
1364
+ operator: "|",
936
1365
  left: {
937
1366
  type: "BinaryExpression",
938
1367
  operator: "^",
939
- left: { type: "Literal", value: a, raw: String(a) },
940
- right: {
1368
+ left: {
941
1369
  type: "BinaryExpression",
942
- operator: "|",
943
- left: { type: "Literal", value: b, raw: String(b) },
1370
+ operator: "+",
1371
+ left: {
1372
+ type: "BinaryExpression",
1373
+ operator: "|",
1374
+ left: { type: "Literal", value: x, raw: String(x) },
1375
+ right: { type: "Literal", value: y, raw: String(y) }
1376
+ },
944
1377
  right: {
945
- type: "UnaryExpression",
946
- operator: "!",
947
- prefix: true,
948
- argument: { type: "Identifier", name: "String" }
1378
+ type: "BinaryExpression",
1379
+ operator: "&",
1380
+ left: { type: "Literal", value: x, raw: String(x) },
1381
+ right: { type: "Literal", value: y, raw: String(y) }
949
1382
  }
1383
+ },
1384
+ right: {
1385
+ type: "BinaryExpression",
1386
+ operator: "+",
1387
+ left: { type: "Literal", value: x, raw: String(x) },
1388
+ right: { type: "Literal", value: y, raw: String(y) }
950
1389
  }
951
1390
  },
952
- right: { type: "Literal", value: 7, raw: "7" }
1391
+ right: {
1392
+ type: "UnaryExpression",
1393
+ operator: "!",
1394
+ prefix: true,
1395
+ argument: { type: "Identifier", name: "String" }
1396
+ }
953
1397
  },
954
- right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
955
- },
956
- consequent: {
957
- type: "ReturnStatement",
958
- argument: null
1398
+ right: { type: "Literal", value: 2147483647, raw: "0x7fffffff" }
959
1399
  },
1400
+ consequent: createDecoyConsequent(fnIndex),
960
1401
  alternate: null
961
1402
  };
962
1403
  }
963
- function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
1404
+ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
964
1405
  let guardedCount = 0;
965
1406
  function maybeInjectGuard(node) {
966
1407
  if (!coldFunctionNodes.has(node)) return;
967
1408
  if (!node.body || node.body.type !== "BlockStatement") return;
1409
+ const guardStmt = createSotaMbaPredicate(seed, 0);
968
1410
  guardedCount++;
969
- const guardStmt = createOpaqueGuardStatement(seed, guardedCount);
1411
+ if (hotExemptNodes) {
1412
+ hotExemptNodes.add(guardStmt);
1413
+ hotExemptNodes.add(guardStmt.consequent);
1414
+ }
970
1415
  let insertIdx = 0;
971
1416
  while (insertIdx < node.body.body.length && node.body.body[insertIdx].type === "ExpressionStatement" && node.body.body[insertIdx].directive) {
972
1417
  insertIdx++;
@@ -1002,25 +1447,12 @@ function deriveIntegrityFnName(seed) {
1002
1447
  const hash = computeIntegrityHash(`unpeek:fn:${seed}`, seed);
1003
1448
  return `_u$${hash.toString(36)}`;
1004
1449
  }
1005
- function deriveLocalVar(label, seed) {
1006
- const hash = computeIntegrityHash(`unpeek:var:${label}:${seed}`, seed) & 1048575;
1007
- return `_${label}${hash.toString(36)}`;
1008
- }
1009
1450
  function createDeterrentSnippet(config) {
1010
1451
  const seed = config.seed >>> 0;
1011
- const expectedToken = (config.expectedIntegrityHash ?? seed) >>> 0;
1012
1452
  const deterrentMode = config.deterrentMode ?? "passive";
1013
1453
  const fnName = deriveIntegrityFnName(seed);
1014
- const strVar = deriveLocalVar("s", seed);
1015
- const tamperVar = deriveLocalVar("t", seed);
1016
- const noopVar = deriveLocalVar("n", seed);
1017
- const methodsVar = deriveLocalVar("m", seed);
1018
- const idxVar = deriveLocalVar("i", seed);
1019
- const errVar = deriveLocalVar("e", seed);
1020
- const tokenBit = expectedToken & 1;
1021
- 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}){}}}` : "";
1022
- const tamperDispatch = `if(window.dispatchEvent&&typeof CustomEvent!=='undefined'){try{window.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(${errVar}){}}`;
1023
- 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}){}}})();`;
1454
+ const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=f=>0;" : "";
1455
+ 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);`;
1024
1456
  }
1025
1457
 
1026
1458
  // src/runtime/index.ts
@@ -1085,7 +1517,7 @@ function transformJavaScript(code, options = {}) {
1085
1517
  coldFunctionNodes,
1086
1518
  hotExemptNodes
1087
1519
  } = analyzeAstHotPaths(ast);
1088
- applyColdFlowGuard(ast, coldFunctionNodes, seed);
1520
+ applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
1089
1521
  const mangledSymbolsCount = applyPolymorphicMangler(ast, seed);
1090
1522
  const { encryptedStringsCount, integrityToken } = applyStringVault(
1091
1523
  ast,