@whnz/frontend-experience-core 1.0.0 → 1.0.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.
@@ -47,6 +47,32 @@ function report(event) {
47
47
  }
48
48
  }
49
49
 
50
+ // src/longtask.ts
51
+ function initLongTaskObserver() {
52
+ if (typeof PerformanceObserver === "undefined" || !PerformanceObserver.supportedEntryTypes.includes("longtask")) {
53
+ return;
54
+ }
55
+ const observer = new PerformanceObserver((list) => {
56
+ var _a;
57
+ for (const entry of list.getEntries()) {
58
+ const anyEntry = entry;
59
+ const attribution = (_a = anyEntry.attribution) == null ? void 0 : _a[0];
60
+ report({
61
+ type: "longtask",
62
+ duration: entry.duration,
63
+ timestamp: Date.now(),
64
+ extra: {
65
+ startTime: entry.startTime,
66
+ blockingTime: Math.max(0, entry.duration - 50),
67
+ name: attribution == null ? void 0 : attribution.name,
68
+ containerType: attribution == null ? void 0 : attribution.containerType
69
+ }
70
+ });
71
+ }
72
+ });
73
+ observer.observe({ entryTypes: ["longtask"] });
74
+ }
75
+
50
76
  // src/init.ts
51
77
  var inited = false;
52
78
  function initCore(options) {
@@ -61,8 +87,9 @@ function initCore(options) {
61
87
  });
62
88
  initReporter(options.reporter);
63
89
  initSampler((_a = options.sampleRate) != null ? _a : 1);
90
+ initLongTaskObserver();
64
91
  }
65
92
 
66
93
  export { getContext, initCore, report, setRoute, setUserId };
67
- //# sourceMappingURL=index.mjs.map
68
- //# sourceMappingURL=index.mjs.map
94
+ //# sourceMappingURL=index.js.map
95
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts","../src/sampler.ts","../src/reporter.ts","../src/longtask.ts","../src/init.ts"],"names":[],"mappings":";AACA,IAAM,OAAA,GAA6B;AAAA,EACjC,GAAA,EAAK,EAAA;AAAA,EACL,OAAA,EAAS,EAAA;AAAA,EACT,GAAA,EAAK,MAAA;AAAA,EACL,KAAA,EAAO;AACT,CA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AAA,OAAA,CAAQ,UAAA,KAAR,IAAA,GAAA,EAAA,GAAsB,CAAC,CAAA;AACnC,EAAA,oBAAA,EAAqB;AACvB","file":"index.js","sourcesContent":["import { ExperienceContext } from './types';\nconst context: ExperienceContext = {\n app: '',\n version: '',\n env: 'prod',\n route: '/',\n};\nexport function initContext(partial: Partial<ExperienceContext>) {\n Object.assign(context, partial);\n}\nexport function setRoute(route: string) {\n context.route = route;\n}\nexport function setUserId(userId: string) {\n context.userId = userId;\n}\nexport function getContext(): ExperienceContext {\n return { ...context };\n}\n","let sampleRate = 1;\nexport function initSampler(rate = 1) {\n sampleRate = rate;\n}\nexport function shouldSample(): boolean {\n if (sampleRate >= 1) return true;\n return Math.random() < sampleRate;\n}\n","import { ExperienceEvent } from './types';\nimport { getContext } from './context';\nimport { shouldSample } from './sampler';\nlet reporterFn: ((event: any) => void) | null = null;\nexport function initReporter(fn: (event: any) => void) {\n reporterFn = fn;\n}\nexport function report(event: ExperienceEvent) {\n if (!reporterFn) return;\n if (!shouldSample()) return;\n const payload = {\n ...event,\n ...getContext(),\n timestamp: event.timestamp || Date.now(),\n };\n try {\n reporterFn(payload);\n } catch (e) {\n // core 永远不能抛异常\n }\n}\n","import { report } from './reporter';\n\nexport function initLongTaskObserver() {\n if (\n typeof PerformanceObserver === 'undefined' ||\n !PerformanceObserver.supportedEntryTypes.includes('longtask')\n ) {\n return;\n }\n\n const observer = new PerformanceObserver((list) => {\n for (const entry of list.getEntries() as PerformanceEntry[]) {\n const anyEntry = entry as any;\n const attribution = anyEntry.attribution?.[0];\n\n report({\n type: 'longtask',\n duration: entry.duration,\n timestamp: Date.now(),\n extra: {\n startTime: entry.startTime,\n blockingTime: Math.max(0, entry.duration - 50),\n name: attribution?.name,\n containerType: attribution?.containerType,\n },\n });\n }\n });\n\n observer.observe({ entryTypes: ['longtask'] });\n}\n","import { InitCoreOptions } from './types';\nimport { initContext } from './context';\nimport { initReporter } from './reporter';\nimport { initSampler } from './sampler';\nimport {initLongTaskObserver} from \"./longtask\";\n\nlet inited = false;\nexport function initCore(options: InitCoreOptions) {\n if (inited) return;\n inited = true;\n initContext({\n app: options.app,\n version: options.version,\n env: options.env,\n route: location.pathname,\n });\n initReporter(options.reporter);\n initSampler(options.sampleRate ?? 1);\n initLongTaskObserver();\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@whnz/frontend-experience-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
+ "type": "module",
5
6
  "types": "dist/index.d.ts",
6
7
  "sideEffects": false,
7
8
  "files": [
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context.ts","../src/sampler.ts","../src/reporter.ts","../src/init.ts"],"names":[],"mappings":";AACA,IAAM,OAAA,GAA6B;AAAA,EACjC,GAAA,EAAK,EAAA;AAAA,EACL,OAAA,EAAS,EAAA;AAAA,EACT,GAAA,EAAK,MAAA;AAAA,EACL,KAAA,EAAO;AACT,CAAA;AACO,SAAS,YAAY,OAAA,EAAqC;AAC/D,EAAA,MAAA,CAAO,MAAA,CAAO,SAAS,OAAO,CAAA;AAChC;AACO,SAAS,SAAS,KAAA,EAAe;AACtC,EAAA,OAAA,CAAQ,KAAA,GAAQ,KAAA;AAClB;AACO,SAAS,UAAU,MAAA,EAAgB;AACxC,EAAA,OAAA,CAAQ,MAAA,GAAS,MAAA;AACnB;AACO,SAAS,UAAA,GAAgC;AAC9C,EAAA,OAAO,EAAE,GAAG,OAAA,EAAQ;AACtB;;;AClBA,IAAI,UAAA,GAAa,CAAA;AACV,SAAS,WAAA,CAAY,OAAO,CAAA,EAAG;AACpC,EAAA,UAAA,GAAa,IAAA;AACf;AACO,SAAS,YAAA,GAAwB;AACtC,EAAA,IAAI,UAAA,IAAc,GAAG,OAAO,IAAA;AAC5B,EAAA,OAAO,IAAA,CAAK,QAAO,GAAI,UAAA;AACzB;;;ACJA,IAAI,UAAA,GAA4C,IAAA;AACzC,SAAS,aAAa,EAAA,EAA0B;AACrD,EAAA,UAAA,GAAa,EAAA;AACf;AACO,SAAS,OAAO,KAAA,EAAwB;AAC7C,EAAA,IAAI,CAAC,UAAA,EAAY;AACjB,EAAA,IAAI,CAAC,cAAa,EAAG;AACrB,EAAA,MAAM,OAAA,GAAU;AAAA,IACd,GAAG,KAAA;AAAA,IACH,GAAG,UAAA,EAAW;AAAA,IACd,SAAA,EAAW,KAAA,CAAM,SAAA,IAAa,IAAA,CAAK,GAAA;AAAI,GACzC;AACA,EAAA,IAAI;AACF,IAAA,UAAA,CAAW,OAAO,CAAA;AAAA,EACpB,SAAS,CAAA,EAAG;AAAA,EAEZ;AACF;;;AChBA,IAAI,MAAA,GAAS,KAAA;AACN,SAAS,SAAS,OAAA,EAA0B;AALnD,EAAA,IAAA,EAAA;AAME,EAAA,IAAI,MAAA,EAAQ;AACZ,EAAA,MAAA,GAAS,IAAA;AACT,EAAA,WAAA,CAAY;AAAA,IACV,KAAK,OAAA,CAAQ,GAAA;AAAA,IACb,SAAS,OAAA,CAAQ,OAAA;AAAA,IACjB,KAAK,OAAA,CAAQ,GAAA;AAAA,IACb,OAAO,QAAA,CAAS;AAAA,GACjB,CAAA;AACD,EAAA,YAAA,CAAa,QAAQ,QAAQ,CAAA;AAC7B,EAAA,WAAA,CAAA,CAAY,EAAA,GAAA,OAAA,CAAQ,UAAA,KAAR,IAAA,GAAA,EAAA,GAAsB,CAAC,CAAA;AACrC","file":"index.mjs","sourcesContent":["import { ExperienceContext } from './types';\nconst context: ExperienceContext = {\n app: '',\n version: '',\n env: 'prod',\n route: '/',\n};\nexport function initContext(partial: Partial<ExperienceContext>) {\n Object.assign(context, partial);\n}\nexport function setRoute(route: string) {\n context.route = route;\n}\nexport function setUserId(userId: string) {\n context.userId = userId;\n}\nexport function getContext(): ExperienceContext {\n return { ...context };\n}\n","let sampleRate = 1;\nexport function initSampler(rate = 1) {\n sampleRate = rate;\n}\nexport function shouldSample(): boolean {\n if (sampleRate >= 1) return true;\n return Math.random() < sampleRate;\n}\n","import { ExperienceEvent } from './types';\nimport { getContext } from './context';\nimport { shouldSample } from './sampler';\nlet reporterFn: ((event: any) => void) | null = null;\nexport function initReporter(fn: (event: any) => void) {\n reporterFn = fn;\n}\nexport function report(event: ExperienceEvent) {\n if (!reporterFn) return;\n if (!shouldSample()) return;\n const payload = {\n ...event,\n ...getContext(),\n timestamp: event.timestamp || Date.now(),\n };\n try {\n reporterFn(payload);\n } catch (e) {\n // core 永远不能抛异常\n }\n}\n","import { InitCoreOptions } from './types';\nimport { initContext } from './context';\nimport { initReporter } from './reporter';\nimport { initSampler } from './sampler';\nlet inited = false;\nexport function initCore(options: InitCoreOptions) {\n if (inited) return;\n inited = true;\n initContext({\n app: options.app,\n version: options.version,\n env: options.env,\n route: location.pathname,\n });\n initReporter(options.reporter);\n initSampler(options.sampleRate ?? 1);\n}\n"]}
File without changes