unpeek 1.6.1 → 1.6.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.
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);
379
+ }
380
+ }
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);
184
392
  }
185
393
  }
186
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
  }
@@ -904,8 +1154,13 @@ function createOpaqueGuardStatement(seed, fnIndex) {
904
1154
  right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
905
1155
  },
906
1156
  consequent: {
907
- type: "ReturnStatement",
908
- argument: null
1157
+ type: "ExpressionStatement",
1158
+ expression: {
1159
+ type: "UnaryExpression",
1160
+ operator: "void",
1161
+ prefix: true,
1162
+ argument: { type: "Literal", value: 0, raw: "0" }
1163
+ }
909
1164
  },
910
1165
  alternate: null
911
1166
  };