cwj_monitoring 0.0.24 → 0.0.26

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.d.ts CHANGED
@@ -12,6 +12,7 @@ declare enum EMIT_TYPE {
12
12
  PERFORMANCE_INP = "performance_inp",
13
13
  PERFORMANCE_LONGTASK = "performance_longtask",
14
14
  PERFORMANCE_RESOURCE = "performance_resource",
15
+ PERFORMANCE_LOAF = "performance_loaf",
15
16
  XHR = "xhr",
16
17
  FETCH = "fetch",
17
18
  CUSTOM = "custom"
@@ -273,6 +274,8 @@ interface PerformanceOptions {
273
274
  resourceThreshold?: number;
274
275
  /** INP 阈值 (ms),超过此值则上报,默认 200 */
275
276
  inpThreshold?: number;
277
+ /** LoAF 阈值 (ms),超过此值则上报,默认 100 */
278
+ loafThreshold?: number;
276
279
  }
277
280
  declare const PerformancePlugin: (options?: PerformanceOptions) => IPlugin;
278
281
 
package/dist/index.js CHANGED
@@ -103,13 +103,19 @@ function throttle(fn, delay) {
103
103
  }
104
104
  };
105
105
  }
106
+ const isIgnoredScriptSource = (sourceURL) => {
107
+ if (!sourceURL || typeof sourceURL !== "string")
108
+ return false;
109
+ if (/node_modules/.test(sourceURL))
110
+ return true;
111
+ return false;
112
+ };
106
113
 
107
114
  const MAX_CACHE_LEN = 5;
108
115
  const MAX_WAITING_TIME = 3e4;
109
116
  const UUID = "track_uuid";
110
- const DEFAULT_LONG_TASK_THRESHOLD = 100;
111
117
  const DEFAULT_RESOURCE_THRESHOLD = 1e3;
112
- const DEFAULT_INP_THRESHOLD = 200;
118
+ const DEFAULT_LOAF_THRESHOLD = 50;
113
119
 
114
120
  var __defProp$3 = Object.defineProperty;
115
121
  var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -315,6 +321,7 @@ var EMIT_TYPE = /* @__PURE__ */ ((EMIT_TYPE2) => {
315
321
  EMIT_TYPE2["PERFORMANCE_INP"] = "performance_inp";
316
322
  EMIT_TYPE2["PERFORMANCE_LONGTASK"] = "performance_longtask";
317
323
  EMIT_TYPE2["PERFORMANCE_RESOURCE"] = "performance_resource";
324
+ EMIT_TYPE2["PERFORMANCE_LOAF"] = "performance_loaf";
318
325
  EMIT_TYPE2["XHR"] = "xhr";
319
326
  EMIT_TYPE2["FETCH"] = "fetch";
320
327
  EMIT_TYPE2["CUSTOM"] = "custom";
@@ -608,10 +615,8 @@ const PerformancePlugin = (options = {}) => {
608
615
  let context;
609
616
  let paintObserver = null;
610
617
  let lcpObserver = null;
611
- let inpObserver = null;
612
- let longTaskObserver = null;
613
618
  let resourceObserver = null;
614
- const interactionMap = /* @__PURE__ */ new Map();
619
+ let loafObserver = null;
615
620
  const monitorPaintMetrics = () => {
616
621
  const entryHandler = (list) => {
617
622
  for (const entry of list.getEntries()) {
@@ -655,63 +660,56 @@ const PerformancePlugin = (options = {}) => {
655
660
  console.warn("[CWJ Monitor] LCP observation not supported:", e);
656
661
  }
657
662
  };
658
- const monitorINP = () => {
663
+ const monitorLoAF = () => {
659
664
  const entryHandler = (list) => {
660
665
  for (const entry of list.getEntries()) {
661
- if (!entry.interactionId)
666
+ if (options.filter && !options.filter(EMIT_TYPE.PERFORMANCE_LOAF, entry)) {
662
667
  continue;
663
- const existing = interactionMap.get(entry.interactionId);
664
- if (existing) {
665
- clearTimeout(existing.timeoutId);
666
668
  }
667
- const maxEntry = existing && existing.entry.duration > entry.duration ? existing.entry : entry;
668
- const timeoutId = setTimeout(() => {
669
- interactionMap.delete(maxEntry.interactionId);
670
- if (options.filter && !options.filter(EMIT_TYPE.PERFORMANCE_INP, maxEntry)) {
671
- return;
669
+ const threshold = options.loafThreshold ?? DEFAULT_LOAF_THRESHOLD;
670
+ if (entry.duration > threshold) {
671
+ const scripts = Array.isArray(entry.scripts) ? entry.scripts : [];
672
+ let isUserTriggered = false;
673
+ let allIgnored = true;
674
+ for (let i = 0; i < scripts.length; i++) {
675
+ const s = scripts[i];
676
+ if (!s)
677
+ continue;
678
+ if (!isUserTriggered && s.invokerType === "user-callback")
679
+ isUserTriggered = true;
680
+ if (allIgnored && !isIgnoredScriptSource(s.sourceURL)) {
681
+ allIgnored = false;
682
+ }
683
+ if (isUserTriggered && !allIgnored)
684
+ break;
672
685
  }
673
- const threshold = options.inpThreshold ?? DEFAULT_INP_THRESHOLD;
674
- if (maxEntry.duration > threshold) {
675
- context?.emit(EMIT_TYPE.PERFORMANCE_INP, {
676
- value: maxEntry.duration,
677
- startTime: maxEntry.startTime,
678
- name: maxEntry.name,
679
- interactionId: maxEntry.interactionId
686
+ if (isUserTriggered && !allIgnored) {
687
+ context?.emit(EMIT_TYPE.PERFORMANCE_LOAF, {
688
+ duration: entry.duration,
689
+ startTime: entry.startTime,
690
+ renderStart: entry.renderStart,
691
+ styleAndLayoutStart: entry.styleAndLayoutStart,
692
+ hadRecentInput: entry.hadRecentInput,
693
+ scripts: entry.scripts.map((s) => ({
694
+ duration: s.duration,
695
+ invoker: s.invoker,
696
+ invokerType: s.invokerType,
697
+ sourceURL: s.sourceURL,
698
+ functionName: s.functionName,
699
+ sourceFunctionName: s.sourceFunctionName,
700
+ sourceCharPosition: s.sourceCharPosition,
701
+ startTime: s.startTime
702
+ }))
680
703
  });
681
704
  }
682
- }, 200);
683
- interactionMap.set(entry.interactionId, { entry: maxEntry, timeoutId });
684
- }
685
- };
686
- try {
687
- inpObserver = new PerformanceObserver(entryHandler);
688
- inpObserver.observe({ type: "event", buffered: true });
689
- } catch (e) {
690
- console.warn("[CWJ Monitor] INP observation not supported:", e);
691
- }
692
- };
693
- const monitorLongTask = () => {
694
- const entryHandler = (list) => {
695
- for (const entry of list.getEntries()) {
696
- if (options.filter && !options.filter(EMIT_TYPE.PERFORMANCE_LONGTASK, entry)) {
697
- continue;
698
- }
699
- const threshold = options.longTaskThreshold ?? DEFAULT_LONG_TASK_THRESHOLD;
700
- if (entry.duration > threshold) {
701
- context?.emit(EMIT_TYPE.PERFORMANCE_LONGTASK, {
702
- startTime: entry.startTime,
703
- duration: entry.duration,
704
- name: entry.name,
705
- attribution: entry.attribution
706
- });
707
705
  }
708
706
  }
709
707
  };
710
708
  try {
711
- longTaskObserver = new PerformanceObserver(entryHandler);
712
- longTaskObserver.observe({ type: "longtask", buffered: true });
709
+ loafObserver = new PerformanceObserver(entryHandler);
710
+ loafObserver.observe({ type: "long-animation-frame", buffered: true });
713
711
  } catch (e) {
714
- console.warn("[CWJ Monitor] Long Task observation not supported:", e);
712
+ console.warn("[CWJ Monitor] LoAF observation not supported:", e);
715
713
  }
716
714
  };
717
715
  const monitorResource = () => {
@@ -751,18 +749,14 @@ const PerformancePlugin = (options = {}) => {
751
749
  context = ctx;
752
750
  monitorPaintMetrics();
753
751
  monitorLCP();
754
- monitorINP();
755
- monitorLongTask();
756
752
  monitorResource();
753
+ monitorLoAF();
757
754
  },
758
755
  uninstall: () => {
759
756
  paintObserver?.disconnect();
760
757
  lcpObserver?.disconnect();
761
- inpObserver?.disconnect();
762
- longTaskObserver?.disconnect();
763
758
  resourceObserver?.disconnect();
764
- interactionMap.forEach((value) => clearTimeout(value.timeoutId));
765
- interactionMap.clear();
759
+ loafObserver?.disconnect();
766
760
  }
767
761
  };
768
762
  };