@arms/rum-reactnative 0.0.2-bate.1

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 (108) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +37 -0
  3. package/RumSdkReactnative.podspec +21 -0
  4. package/android/build.gradle +77 -0
  5. package/android/gradle.properties +5 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/com/rumsdkreactnative/RumSdkReactnativeModule.kt +23 -0
  8. package/android/src/main/java/com/rumsdkreactnative/RumSdkReactnativePackage.kt +33 -0
  9. package/ios/RumSdkReactnative.h +5 -0
  10. package/ios/RumSdkReactnative.mm +18 -0
  11. package/lib/module/collector/action/index.js +68 -0
  12. package/lib/module/collector/action/index.js.map +1 -0
  13. package/lib/module/collector/exception/index.js +135 -0
  14. package/lib/module/collector/exception/index.js.map +1 -0
  15. package/lib/module/collector/resource/api.js +281 -0
  16. package/lib/module/collector/resource/api.js.map +1 -0
  17. package/lib/module/collector/view/perf.js +79 -0
  18. package/lib/module/collector/view/perf.js.map +1 -0
  19. package/lib/module/collector/view/pv.js +72 -0
  20. package/lib/module/collector/view/pv.js.map +1 -0
  21. package/lib/module/index.js +6 -0
  22. package/lib/module/index.js.map +1 -0
  23. package/lib/module/package.json +1 -0
  24. package/lib/module/plugin.js +39 -0
  25. package/lib/module/plugin.js.map +1 -0
  26. package/lib/module/processor/default-processor.js +22 -0
  27. package/lib/module/processor/default-processor.js.map +1 -0
  28. package/lib/module/processor/session-processor.js +20 -0
  29. package/lib/module/processor/session-processor.js.map +1 -0
  30. package/lib/module/reporter/index.js +56 -0
  31. package/lib/module/reporter/index.js.map +1 -0
  32. package/lib/module/shell.js +63 -0
  33. package/lib/module/shell.js.map +1 -0
  34. package/lib/module/types/client.js +4 -0
  35. package/lib/module/types/client.js.map +1 -0
  36. package/lib/module/types/index.js +2 -0
  37. package/lib/module/types/index.js.map +1 -0
  38. package/lib/module/utils/api.js +75 -0
  39. package/lib/module/utils/api.js.map +1 -0
  40. package/lib/module/utils/base.js +90 -0
  41. package/lib/module/utils/base.js.map +1 -0
  42. package/lib/module/utils/platform.js +95 -0
  43. package/lib/module/utils/platform.js.map +1 -0
  44. package/lib/module/utils/session.js +135 -0
  45. package/lib/module/utils/session.js.map +1 -0
  46. package/lib/module/utils/url.js +54 -0
  47. package/lib/module/utils/url.js.map +1 -0
  48. package/lib/module/utils/view.js +87 -0
  49. package/lib/module/utils/view.js.map +1 -0
  50. package/lib/typescript/package.json +1 -0
  51. package/lib/typescript/src/collector/action/index.d.ts +15 -0
  52. package/lib/typescript/src/collector/action/index.d.ts.map +1 -0
  53. package/lib/typescript/src/collector/exception/index.d.ts +25 -0
  54. package/lib/typescript/src/collector/exception/index.d.ts.map +1 -0
  55. package/lib/typescript/src/collector/resource/api.d.ts +21 -0
  56. package/lib/typescript/src/collector/resource/api.d.ts.map +1 -0
  57. package/lib/typescript/src/collector/view/perf.d.ts +1 -0
  58. package/lib/typescript/src/collector/view/perf.d.ts.map +1 -0
  59. package/lib/typescript/src/collector/view/pv.d.ts +16 -0
  60. package/lib/typescript/src/collector/view/pv.d.ts.map +1 -0
  61. package/lib/typescript/src/index.d.ts +4 -0
  62. package/lib/typescript/src/index.d.ts.map +1 -0
  63. package/lib/typescript/src/plugin.d.ts +3 -0
  64. package/lib/typescript/src/plugin.d.ts.map +1 -0
  65. package/lib/typescript/src/processor/default-processor.d.ts +6 -0
  66. package/lib/typescript/src/processor/default-processor.d.ts.map +1 -0
  67. package/lib/typescript/src/processor/session-processor.d.ts +9 -0
  68. package/lib/typescript/src/processor/session-processor.d.ts.map +1 -0
  69. package/lib/typescript/src/reporter/index.d.ts +16 -0
  70. package/lib/typescript/src/reporter/index.d.ts.map +1 -0
  71. package/lib/typescript/src/shell.d.ts +29 -0
  72. package/lib/typescript/src/shell.d.ts.map +1 -0
  73. package/lib/typescript/src/types/client.d.ts +27 -0
  74. package/lib/typescript/src/types/client.d.ts.map +1 -0
  75. package/lib/typescript/src/types/index.d.ts +118 -0
  76. package/lib/typescript/src/types/index.d.ts.map +1 -0
  77. package/lib/typescript/src/utils/api.d.ts +59 -0
  78. package/lib/typescript/src/utils/api.d.ts.map +1 -0
  79. package/lib/typescript/src/utils/base.d.ts +8 -0
  80. package/lib/typescript/src/utils/base.d.ts.map +1 -0
  81. package/lib/typescript/src/utils/platform.d.ts +15 -0
  82. package/lib/typescript/src/utils/platform.d.ts.map +1 -0
  83. package/lib/typescript/src/utils/session.d.ts +37 -0
  84. package/lib/typescript/src/utils/session.d.ts.map +1 -0
  85. package/lib/typescript/src/utils/url.d.ts +17 -0
  86. package/lib/typescript/src/utils/url.d.ts.map +1 -0
  87. package/lib/typescript/src/utils/view.d.ts +13 -0
  88. package/lib/typescript/src/utils/view.d.ts.map +1 -0
  89. package/package.json +158 -0
  90. package/src/collector/action/index.ts +67 -0
  91. package/src/collector/exception/index.ts +158 -0
  92. package/src/collector/resource/api.ts +379 -0
  93. package/src/collector/view/perf.ts +77 -0
  94. package/src/collector/view/pv.ts +73 -0
  95. package/src/index.ts +4 -0
  96. package/src/plugin.ts +41 -0
  97. package/src/processor/default-processor.ts +22 -0
  98. package/src/processor/session-processor.ts +23 -0
  99. package/src/reporter/index.ts +67 -0
  100. package/src/shell.ts +81 -0
  101. package/src/types/client.ts +32 -0
  102. package/src/types/index.ts +159 -0
  103. package/src/utils/api.ts +119 -0
  104. package/src/utils/base.ts +92 -0
  105. package/src/utils/platform.ts +107 -0
  106. package/src/utils/session.ts +168 -0
  107. package/src/utils/url.ts +53 -0
  108. package/src/utils/view.ts +93 -0
@@ -0,0 +1,168 @@
1
+ import {
2
+ generateEventId,
3
+ generateGUID,
4
+ generateSpanId,
5
+ IContext,
6
+ IRumSession,
7
+ isNumber,
8
+ ONE_DAY,
9
+ ONE_HOUR,
10
+ ONE_MINUTE,
11
+ performDraw,
12
+ SessionConfig,
13
+ } from '@arms/rum-core';
14
+ import { getCurrentTime } from './base';
15
+ import { getCurView } from './view';
16
+ import { getStorage, setStorage } from './platform';
17
+
18
+ // 存储userID的键名
19
+ const USER_ID = '_arms_uid';
20
+ // 存储session
21
+ // const RUM_SESSION = '_arms_session';
22
+
23
+ interface SessionInfo {
24
+ sessionId: string;
25
+ sampled: boolean;
26
+ startTime: number;
27
+ lastTime: number;
28
+ isNew?: boolean;
29
+ }
30
+
31
+ export class RumSession implements IRumSession {
32
+ ctx?: IContext;
33
+
34
+ // @ts-ignore
35
+ sessionConfig: SessionConfig;
36
+
37
+ // private sessionId = '';
38
+
39
+ private session = '';
40
+
41
+ private userId = '';
42
+
43
+ constructor() {
44
+ this.initUser();
45
+ }
46
+
47
+ init(ctx: IContext) {
48
+ this.ctx = ctx;
49
+ this.sessionConfig = this.fixSessionConfig(ctx.config?.sessionConfig);
50
+ }
51
+
52
+ getSessionId() {
53
+ return this.getSessionInfo().sessionId;
54
+ }
55
+
56
+ getSampled() {
57
+ return this.getSessionInfo().sampled;
58
+ }
59
+
60
+ checkSession(info: SessionInfo) {
61
+ const { overtime = 0, maxDuration = 0 } = this.sessionConfig;
62
+ const now = getCurrentTime();
63
+ return !(
64
+ info.startTime + maxDuration < now || info.lastTime + overtime < now
65
+ );
66
+ }
67
+
68
+ updateSession() {
69
+ const info = this.getSessionInfo();
70
+ if (info.isNew) {
71
+ return;
72
+ }
73
+ const startTime = info.startTime;
74
+ const lastTime = getCurrentTime();
75
+ const sampled = info.sampled ? 1 : 0;
76
+ this.session = `${info.sessionId}-${sampled}-${startTime}-${lastTime}`;
77
+ // setStorage(RUM_SESSION, `${info.sessionId}-${sampled}-${startTime}-${lastTime}`)
78
+ }
79
+
80
+ getSessionInfo() {
81
+ const [sid, sampled, startTime, lastTime] = this.session.split('-');
82
+ let info: SessionInfo = {
83
+ sessionId: sid as string,
84
+ sampled: sampled !== '0',
85
+ startTime: parseInt(startTime || '') || 0,
86
+ lastTime: parseInt(lastTime || '') || 0,
87
+ };
88
+ if (!this.checkSession(info)) {
89
+ info = this.resetSession();
90
+ }
91
+ return info;
92
+ }
93
+
94
+ getEventId() {
95
+ const sid = this.getSessionId();
96
+ return generateEventId(sid);
97
+ }
98
+
99
+ getViewId() {
100
+ return this.getUUID();
101
+ }
102
+
103
+ async initUser() {
104
+ this.userId = (await getStorage(USER_ID)) as string;
105
+ if (!this.userId) {
106
+ this.userId = `uid_${generateSpanId(16, 36)}`;
107
+ await setStorage(USER_ID, this.userId);
108
+ }
109
+ }
110
+
111
+ getUserId() {
112
+ return this.userId;
113
+ }
114
+
115
+ private resetSession() {
116
+ const { sampleRate = 1 } = this.sessionConfig;
117
+ const sid = this.getUUID();
118
+ const now = getCurrentTime();
119
+ const sampled = performDraw(sampleRate * 100);
120
+ this.session = `${sid}-${sampled ? 1 : 0}-${now}-${now}`;
121
+ // setStorage(RUM_SESSION, this.session);
122
+ return {
123
+ sessionId: sid,
124
+ sampled: sampled,
125
+ startTime: now,
126
+ lastTime: now,
127
+ isNew: true,
128
+ } as SessionInfo;
129
+ }
130
+
131
+ private getUUID() {
132
+ return generateGUID().replace(/-/g, '');
133
+ }
134
+
135
+ fixSessionConfig(sessionConfig: SessionConfig = {}): SessionConfig {
136
+ let { sampleRate, maxDuration, overtime } = sessionConfig;
137
+ if (!isNumber(sampleRate) || sampleRate < 0 || sampleRate > 1) {
138
+ sampleRate = 1;
139
+ }
140
+ if (
141
+ !isNumber(maxDuration) ||
142
+ maxDuration < 4 * ONE_HOUR ||
143
+ maxDuration > ONE_DAY
144
+ ) {
145
+ maxDuration = ONE_DAY;
146
+ }
147
+ if (
148
+ !isNumber(overtime) ||
149
+ overtime > ONE_HOUR ||
150
+ overtime < 10 * ONE_MINUTE
151
+ ) {
152
+ overtime = 30 * ONE_MINUTE;
153
+ }
154
+ return { sampleRate, maxDuration, overtime };
155
+ }
156
+
157
+ getBaseEvent() {
158
+ const session_id = this.getSessionId();
159
+ const event_id = this.getEventId();
160
+ return {
161
+ timestamp: getCurrentTime(),
162
+ session_id,
163
+ event_id,
164
+ view: this.ctx ? getCurView(this.ctx) : undefined,
165
+ times: 1,
166
+ };
167
+ }
168
+ }
@@ -0,0 +1,53 @@
1
+
2
+ /**
3
+ * 根据指定 URL 获取视图的名称,默认为其path部分
4
+ * @param url 需要提取路径部分的 URL
5
+ * @param isHash 是否为 hash 模式路由的 URL
6
+ */
7
+ export function getPathByURL(url: string, isHash = false) {
8
+ try {
9
+ const urlObj = getURL(url);
10
+ if (!urlObj) return;
11
+ if (isHash) {
12
+ return urlObj.hash;
13
+ } else {
14
+ return urlObj.pathname;
15
+ }
16
+ } catch (error) {
17
+ return '';
18
+ }
19
+ }
20
+
21
+ // /**
22
+ // * 检查是否为上报域名
23
+ // * @param ctx
24
+ // * @param url 需要提取路径部分的 URL
25
+ // */
26
+ // export function isEndpoint(ctx: IContext, url: string): boolean {
27
+ // const { endpoint } = ctx.getConfig();
28
+ // try {
29
+ // return getURL(url).hostname === getURL(endpoint).hostname
30
+ // } catch (e) {
31
+ // return false;
32
+ // }
33
+ // }
34
+
35
+ const REGEX = /^(?:([^:\/?#]+):\/\/)?((?:([^\/?#@]*)@)?([^\/?#:]*)(?:\:(\d*))?)?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n)*))?/i
36
+
37
+ export function getURL(url: string) {
38
+ const parts = decodeURIComponent(url).match(REGEX);
39
+ if (!parts) return null;
40
+ const auth = (parts[3] || '').split(':');
41
+ const host = auth.length ? (parts[2] || '').replace(/(.*\@)/, '') : parts[2];
42
+ return {
43
+ uri: parts[0],
44
+ protocol: parts[1],
45
+ host: host,
46
+ hostname: parts[4],
47
+ port: parts[5],
48
+ pathname: parts[6],
49
+ search: parts[7],
50
+ // query: mapSearchParams(parts[7]),
51
+ hash: parts[8]
52
+ }
53
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ IContext,
3
+ isFunction,
4
+ // IRumSession,
5
+ // IViewData
6
+ } from '@arms/rum-core';
7
+ import { NavigationContainerRef, NavigationListener } from '../types'
8
+ // import { IPage } from "../types/client";
9
+
10
+ // export const PAGE_ID = '__rum_view_id';
11
+ //
12
+ // /**
13
+ // * 获取 RUM 内置的view_id标识
14
+ // * @param page
15
+ // * @param session
16
+ // * @returns
17
+ // */
18
+ // export async function getRumViewId(page: IPage, session?: IRumSession): Promise<string> {
19
+ // if (!page) return;
20
+ // if (page.id) {
21
+ // return page.id;
22
+ // }
23
+ // if (!session) return;
24
+ // return page.id = session.getViewId();
25
+ // }
26
+ //
27
+ // /**
28
+ // * 获取当前 view
29
+ // * @param viewId
30
+ // * @param ctx
31
+ // * @returns
32
+ // */
33
+ // export function getViewById(viewId: string, ctx: IContext): IViewData {
34
+ // if (!viewId || !ctx) return;
35
+ // return ctx.getViews().find(view => view.id === viewId);
36
+ // }
37
+
38
+ /**
39
+ * 获取当前 view
40
+ * @param ctx
41
+ * @returns
42
+ */
43
+ export function getCurView(ctx: IContext) {
44
+ if (!ctx) return;
45
+ const views = ctx.getViews();
46
+ if (views && views.length) {
47
+ return views[views.length - 1];
48
+ }
49
+ return null;
50
+ }
51
+
52
+ let currentNavigationRef: NavigationContainerRef | undefined;
53
+ const navigateListeners: NavigationListener[] = [];
54
+
55
+ export function addNavigateListener(listener: NavigationListener) {
56
+ if (isFunction(listener)) {
57
+ navigateListeners.push(listener);
58
+ }
59
+ }
60
+
61
+ export function removeNavigateListener(listener: NavigationListener) {
62
+ navigateListeners.splice(navigateListeners.indexOf(listener), 1);
63
+ }
64
+
65
+ function triggerNavigateChange(event: any) {
66
+ if (!currentNavigationRef) return;
67
+ let route = event;
68
+ if (isFunction(currentNavigationRef.getCurrentRoute)) {
69
+ route = currentNavigationRef.getCurrentRoute();
70
+ }
71
+ if (!route) return;
72
+ navigateListeners.forEach(listener => {
73
+ listener(route);
74
+ })
75
+ }
76
+
77
+ export function startTrackingViews(nav: any) {
78
+ if (!nav || currentNavigationRef) return;
79
+ if (isFunction(nav.events)) {
80
+ // react-native-navigation
81
+ nav.events().registerComponentDidAppearListener(triggerNavigateChange)
82
+ } else {
83
+ // @react-native/navigation
84
+ nav.addListener('state', triggerNavigateChange)
85
+ }
86
+ currentNavigationRef = nav;
87
+ }
88
+
89
+ export function stopTrackingViews() {
90
+ if (!currentNavigationRef) return;
91
+ currentNavigationRef.removeListener('state', triggerNavigateChange)
92
+ currentNavigationRef = undefined;
93
+ }