@keak/sdk 2.0.6 → 2.0.7

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/index.cjs.js CHANGED
@@ -195,6 +195,10 @@ if (typeof window !== 'undefined') {
195
195
  * React adds debug source information in development mode that includes the original
196
196
  * file path, line number, and column number where a component was defined.
197
197
  */
198
+ // Debug flag - set window.__KEAK_DEBUG__ = true to enable verbose logging
199
+ const DEBUG = typeof window !== 'undefined' && window.__KEAK_DEBUG__;
200
+ // Cache for Fiber source lookups to avoid repeated traversals
201
+ const fiberSourceCache = new WeakMap();
198
202
  /**
199
203
  * Extract React Fiber from a DOM element
200
204
  */
@@ -204,10 +208,14 @@ function getReactFiberFromElement(element) {
204
208
  key.startsWith('__reactInternalInstance') ||
205
209
  key.startsWith('__reactProps'));
206
210
  if (!fiberKey) {
207
- console.log('[Keak Fiber] No React Fiber key found on element. Keys:', Object.keys(element).filter(k => k.startsWith('__')));
211
+ if (DEBUG) {
212
+ console.log('[Keak Fiber] No React Fiber key found on element. Keys:', Object.keys(element).filter(k => k.startsWith('__')));
213
+ }
208
214
  return null;
209
215
  }
210
- console.log('[Keak Fiber] Found Fiber key:', fiberKey);
216
+ if (DEBUG) {
217
+ console.log('[Keak Fiber] Found Fiber key:', fiberKey);
218
+ }
211
219
  return element[fiberKey];
212
220
  }
213
221
  /**
@@ -279,7 +287,9 @@ function traverseChildren(fiber, visited, remainingDepth) {
279
287
  columnNumber: source.columnNumber || 1,
280
288
  componentName
281
289
  };
282
- console.log('[Keak Fiber] Found valid source in child traversal:', sourceInfo);
290
+ if (DEBUG) {
291
+ console.log('[Keak Fiber] Found valid source in child traversal:', sourceInfo);
292
+ }
283
293
  return sourceInfo; // Return first valid match
284
294
  }
285
295
  }
@@ -311,7 +321,9 @@ function extractSourceFromFiber(fiber) {
311
321
  if (!fiber) {
312
322
  return null;
313
323
  }
314
- console.log('[Keak Fiber] Starting Fiber traversal');
324
+ if (DEBUG) {
325
+ console.log('[Keak Fiber] Starting Fiber traversal');
326
+ }
315
327
  // Track visited fibers to avoid infinite loops
316
328
  const visited = new Set();
317
329
  let current = fiber;
@@ -326,14 +338,16 @@ function extractSourceFromFiber(fiber) {
326
338
  visited.add(current);
327
339
  // Check for debug source
328
340
  const source = current._debugSource || current.__source;
329
- if (depth === 0) {
341
+ if (DEBUG && depth === 0) {
330
342
  console.log('[Keak Fiber] Root fiber has source?', !!source);
331
343
  if (source) {
332
344
  console.log('[Keak Fiber] Root source:', source);
333
345
  }
334
346
  }
335
347
  if (source && source.fileName && source.lineNumber) {
336
- console.log('[Keak Fiber] Found source at depth', depth, ':', source.fileName, ':', source.lineNumber);
348
+ if (DEBUG) {
349
+ console.log('[Keak Fiber] Found source at depth', depth, ':', source.fileName, ':', source.lineNumber);
350
+ }
337
351
  const componentName = getComponentName(current);
338
352
  // Filter out React internals, node_modules, layout wrappers, and dynamic imports
339
353
  const isInternalFile = source.fileName.includes('node_modules') ||
@@ -355,15 +369,19 @@ function extractSourceFromFiber(fiber) {
355
369
  columnNumber: source.columnNumber || 1,
356
370
  componentName
357
371
  };
358
- console.log('[Keak Fiber] Valid source found:', sourceInfo);
372
+ if (DEBUG) {
373
+ console.log('[Keak Fiber] Valid source found:', sourceInfo);
374
+ }
359
375
  // Take the first valid source (lowest depth = most specific)
360
376
  if (!bestMatch) {
361
377
  bestMatch = sourceInfo;
362
- console.log('[Keak Fiber] Using source at depth', depth);
378
+ if (DEBUG) {
379
+ console.log('[Keak Fiber] Using source at depth', depth);
380
+ }
363
381
  return sourceInfo;
364
382
  }
365
383
  }
366
- else {
384
+ else if (DEBUG) {
367
385
  console.log('[Keak Fiber] Skipping source (wrapper/layout):', {
368
386
  file: source.fileName.split('/').pop(),
369
387
  component: componentName,
@@ -389,17 +407,21 @@ function extractSourceFromFiber(fiber) {
389
407
  }
390
408
  // After upward traversal, try downward/sibling traversal if no match found
391
409
  if (!bestMatch && fiber.child) {
392
- console.log('[Keak Fiber] No match found in upward traversal, trying children/siblings');
410
+ if (DEBUG) {
411
+ console.log('[Keak Fiber] No match found in upward traversal, trying children/siblings');
412
+ }
393
413
  const childMatch = traverseChildren(fiber, visited, maxDepth - depth);
394
414
  if (childMatch) {
395
415
  bestMatch = childMatch;
396
416
  }
397
417
  }
398
- if (bestMatch) {
399
- console.log('[Keak Fiber] Returning best match:', bestMatch);
400
- }
401
- else {
402
- console.log('[Keak Fiber] No component source found after traversing', depth, 'levels');
418
+ if (DEBUG) {
419
+ if (bestMatch) {
420
+ console.log('[Keak Fiber] Returning best match:', bestMatch);
421
+ }
422
+ else {
423
+ console.log('[Keak Fiber] No component source found after traversing', depth, 'levels');
424
+ }
403
425
  }
404
426
  return bestMatch;
405
427
  }
@@ -407,27 +429,39 @@ function extractSourceFromFiber(fiber) {
407
429
  * Main function to get source information from a DOM element
408
430
  */
409
431
  function getReactFiberSource(element) {
432
+ // Check cache first to avoid repeated Fiber traversals
433
+ if (fiberSourceCache.has(element)) {
434
+ return fiberSourceCache.get(element) || null;
435
+ }
410
436
  try {
411
437
  const fiber = getReactFiberFromElement(element);
412
438
  if (!fiber) {
413
- console.log('[Keak Fiber] No React Fiber found on element');
439
+ if (DEBUG) {
440
+ console.log('[Keak Fiber] No React Fiber found on element');
441
+ }
442
+ fiberSourceCache.set(element, null);
414
443
  return null;
415
444
  }
416
445
  const sourceInfo = extractSourceFromFiber(fiber);
417
- if (sourceInfo) {
418
- console.log('[Keak Fiber] Found source:', {
419
- file: sourceInfo.fileName,
420
- line: sourceInfo.lineNumber,
421
- component: sourceInfo.componentName
422
- });
423
- }
424
- else {
425
- console.log('[Keak Fiber] No source information found in Fiber tree');
446
+ if (DEBUG) {
447
+ if (sourceInfo) {
448
+ console.log('[Keak Fiber] Found source:', {
449
+ file: sourceInfo.fileName,
450
+ line: sourceInfo.lineNumber,
451
+ component: sourceInfo.componentName
452
+ });
453
+ }
454
+ else {
455
+ console.log('[Keak Fiber] No source information found in Fiber tree');
456
+ }
426
457
  }
458
+ // Cache the result
459
+ fiberSourceCache.set(element, sourceInfo);
427
460
  return sourceInfo;
428
461
  }
429
462
  catch (error) {
430
463
  console.error('[Keak Fiber] Error extracting source:', error);
464
+ fiberSourceCache.set(element, null);
431
465
  return null;
432
466
  }
433
467
  }