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