@multiplayer-app/session-recorder-react-native 0.0.1-alpha.1 → 0.0.1-alpha.10

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 (234) hide show
  1. package/RRWEB_INTEGRATION.md +336 -0
  2. package/VIEWSHOT_INTEGRATION_TEST.md +123 -0
  3. package/copy-react-native-dist.sh +38 -0
  4. package/dist/components/GestureCaptureWrapper/GestureCaptureWrapper.d.ts +6 -0
  5. package/dist/components/GestureCaptureWrapper/GestureCaptureWrapper.js +1 -0
  6. package/dist/components/GestureCaptureWrapper/GestureCaptureWrapper.js.map +1 -0
  7. package/dist/components/GestureCaptureWrapper/index.d.ts +1 -0
  8. package/dist/components/GestureCaptureWrapper/index.js +1 -0
  9. package/dist/components/GestureCaptureWrapper/index.js.map +1 -0
  10. package/dist/components/GestureCaptureWrapper.d.ts +6 -0
  11. package/dist/components/GestureCaptureWrapper.js +1 -0
  12. package/dist/components/GestureCaptureWrapper.js.map +1 -0
  13. package/dist/components/ScreenRecorderView/ScreenRecorderView.d.ts +5 -0
  14. package/dist/components/ScreenRecorderView/ScreenRecorderView.js +1 -0
  15. package/dist/components/ScreenRecorderView/ScreenRecorderView.js.map +1 -0
  16. package/dist/components/ScreenRecorderView/index.d.ts +1 -0
  17. package/dist/components/ScreenRecorderView/index.js +1 -0
  18. package/dist/components/ScreenRecorderView/index.js.map +1 -0
  19. package/dist/components/index.d.ts +1 -0
  20. package/dist/components/index.js +1 -0
  21. package/dist/components/index.js.map +1 -0
  22. package/dist/config/constants.d.ts +18 -0
  23. package/dist/config/constants.js +1 -0
  24. package/dist/config/constants.js.map +1 -0
  25. package/dist/config/defaults.d.ts +4 -0
  26. package/dist/config/defaults.js +1 -0
  27. package/dist/config/defaults.js.map +1 -0
  28. package/dist/config/index.d.ts +5 -0
  29. package/dist/config/index.js +1 -0
  30. package/dist/config/index.js.map +1 -0
  31. package/dist/config/masking.d.ts +2 -30
  32. package/dist/config/masking.js +1 -1
  33. package/dist/config/masking.js.map +1 -1
  34. package/dist/config/session-recorder.d.ts +2 -0
  35. package/dist/config/session-recorder.js +1 -0
  36. package/dist/config/session-recorder.js.map +1 -0
  37. package/dist/config/validators.d.ts +10 -0
  38. package/dist/config/validators.js +1 -0
  39. package/dist/config/validators.js.map +1 -0
  40. package/dist/context/SessionRecorderContext.d.ts +12 -0
  41. package/dist/context/SessionRecorderContext.js +1 -0
  42. package/dist/context/SessionRecorderContext.js.map +1 -0
  43. package/dist/expo.d.ts +5 -9
  44. package/dist/expo.js +1 -1
  45. package/dist/expo.js.map +1 -1
  46. package/dist/index.d.ts +6 -10
  47. package/dist/index.js +1 -1
  48. package/dist/index.js.map +1 -1
  49. package/dist/otel/helpers.d.ts +45 -3
  50. package/dist/otel/helpers.js +1 -1
  51. package/dist/otel/helpers.js.map +1 -1
  52. package/dist/otel/index.d.ts +4 -25
  53. package/dist/otel/index.js +1 -1
  54. package/dist/otel/index.js.map +1 -1
  55. package/dist/otel/instrumentations/gestureInstrumentation.js +1 -1
  56. package/dist/otel/instrumentations/gestureInstrumentation.js.map +1 -1
  57. package/dist/otel/instrumentations/index.d.ts +3 -4
  58. package/dist/otel/instrumentations/index.js +1 -1
  59. package/dist/otel/instrumentations/index.js.map +1 -1
  60. package/dist/otel/instrumentations/reactNativeInstrumentation.js +1 -1
  61. package/dist/otel/instrumentations/reactNativeInstrumentation.js.map +1 -1
  62. package/dist/otel/instrumentations/reactNavigationInstrumentation.d.ts +1 -0
  63. package/dist/otel/instrumentations/reactNavigationInstrumentation.js +1 -1
  64. package/dist/otel/instrumentations/reactNavigationInstrumentation.js.map +1 -1
  65. package/dist/patch/index.d.ts +1 -0
  66. package/dist/patch/index.js +1 -0
  67. package/dist/patch/index.js.map +1 -0
  68. package/dist/patch/xhr.d.ts +2 -0
  69. package/dist/patch/xhr.js +1 -0
  70. package/dist/patch/xhr.js.map +1 -0
  71. package/dist/recorder/eventExporter.d.ts +21 -0
  72. package/dist/recorder/eventExporter.js +1 -0
  73. package/dist/recorder/eventExporter.js.map +1 -0
  74. package/dist/recorder/gestureHandlerRecorder.d.ts +19 -0
  75. package/dist/recorder/gestureHandlerRecorder.js +1 -0
  76. package/dist/recorder/gestureHandlerRecorder.js.map +1 -0
  77. package/dist/recorder/gestureRecorder.d.ts +68 -11
  78. package/dist/recorder/gestureRecorder.js +1 -1
  79. package/dist/recorder/gestureRecorder.js.map +1 -1
  80. package/dist/recorder/index.d.ts +60 -6
  81. package/dist/recorder/index.js +1 -1
  82. package/dist/recorder/index.js.map +1 -1
  83. package/dist/recorder/navigationTracker.js +1 -1
  84. package/dist/recorder/navigationTracker.js.map +1 -1
  85. package/dist/recorder/screenRecorder.d.ts +79 -10
  86. package/dist/recorder/screenRecorder.js +1 -1
  87. package/dist/recorder/screenRecorder.js.map +1 -1
  88. package/dist/services/api.service.d.ts +62 -10
  89. package/dist/services/api.service.js +1 -1
  90. package/dist/services/api.service.js.map +1 -1
  91. package/dist/services/storage.service.d.ts +23 -16
  92. package/dist/services/storage.service.js +1 -1
  93. package/dist/services/storage.service.js.map +1 -1
  94. package/dist/session-recorder.d.ts +166 -0
  95. package/dist/session-recorder.js +1 -0
  96. package/dist/session-recorder.js.map +1 -0
  97. package/dist/types/index.d.ts +15 -76
  98. package/dist/types/index.js +1 -1
  99. package/dist/types/index.js.map +1 -1
  100. package/dist/types/rrweb.d.ts +118 -0
  101. package/dist/types/rrweb.js +1 -0
  102. package/dist/types/rrweb.js.map +1 -0
  103. package/dist/types/session-recorder.d.ts +366 -0
  104. package/dist/types/session-recorder.js +1 -0
  105. package/dist/types/session-recorder.js.map +1 -0
  106. package/dist/types/session.d.ts +59 -0
  107. package/dist/types/session.js +1 -0
  108. package/dist/types/session.js.map +1 -0
  109. package/dist/utils/app-metadata.d.ts +16 -0
  110. package/dist/utils/app-metadata.js +1 -0
  111. package/dist/utils/app-metadata.js.map +1 -0
  112. package/dist/utils/index.d.ts +7 -0
  113. package/dist/utils/index.js +1 -0
  114. package/dist/utils/index.js.map +1 -0
  115. package/dist/utils/logger.d.ts +112 -0
  116. package/dist/utils/logger.js +1 -0
  117. package/dist/utils/logger.js.map +1 -0
  118. package/dist/utils/platform.d.ts +37 -0
  119. package/dist/utils/platform.js +1 -1
  120. package/dist/utils/platform.js.map +1 -1
  121. package/dist/utils/request-utils.d.ts +21 -0
  122. package/dist/utils/request-utils.js +1 -0
  123. package/dist/utils/request-utils.js.map +1 -0
  124. package/dist/utils/rrweb-events.d.ts +65 -0
  125. package/dist/utils/rrweb-events.js +1 -0
  126. package/dist/utils/rrweb-events.js.map +1 -0
  127. package/dist/utils/session.d.ts +5 -0
  128. package/dist/utils/session.js +1 -0
  129. package/dist/utils/session.js.map +1 -0
  130. package/dist/utils/time.d.ts +4 -0
  131. package/dist/utils/time.js +1 -0
  132. package/dist/utils/time.js.map +1 -0
  133. package/dist/utils/type-utils.d.ts +16 -0
  134. package/dist/utils/type-utils.js +1 -0
  135. package/dist/utils/type-utils.js.map +1 -0
  136. package/dist/version.d.ts +1 -1
  137. package/dist/version.js +1 -1
  138. package/dist/version.js.map +1 -1
  139. package/docs/AUTO_METADATA_DETECTION.md +108 -0
  140. package/package.json +10 -9
  141. package/scripts/generate-app-metadata.js +173 -0
  142. package/src/components/GestureCaptureWrapper/GestureCaptureWrapper.tsx +86 -0
  143. package/src/components/GestureCaptureWrapper/index.ts +1 -0
  144. package/src/components/ScreenRecorderView/ScreenRecorderView.tsx +72 -0
  145. package/src/components/ScreenRecorderView/index.ts +1 -0
  146. package/src/components/index.ts +1 -0
  147. package/src/config/constants.ts +60 -0
  148. package/src/config/defaults.ts +82 -0
  149. package/src/config/index.ts +6 -0
  150. package/src/config/masking.ts +10 -61
  151. package/src/config/session-recorder.ts +55 -0
  152. package/src/config/validators.ts +31 -0
  153. package/src/context/SessionRecorderContext.tsx +75 -0
  154. package/src/expo.ts +7 -37
  155. package/src/index.ts +14 -17
  156. package/src/otel/helpers.ts +265 -11
  157. package/src/otel/index.ts +37 -247
  158. package/src/otel/instrumentations/index.ts +82 -53
  159. package/src/patch/index.ts +1 -0
  160. package/src/patch/xhr.ts +142 -0
  161. package/src/recorder/eventExporter.ts +141 -0
  162. package/src/recorder/gestureRecorder.ts +194 -125
  163. package/src/recorder/index.ts +132 -24
  164. package/src/recorder/navigationTracker.ts +12 -10
  165. package/src/recorder/screenRecorder.ts +242 -155
  166. package/src/services/api.service.ts +170 -45
  167. package/src/services/storage.service.ts +102 -74
  168. package/src/session-recorder.ts +600 -0
  169. package/src/types/index.ts +19 -79
  170. package/src/types/session-recorder.ts +423 -0
  171. package/src/types/session.ts +65 -0
  172. package/src/utils/app-metadata.ts +31 -0
  173. package/src/utils/index.ts +8 -0
  174. package/src/utils/logger.ts +225 -0
  175. package/src/utils/platform.ts +321 -6
  176. package/src/utils/request-utils.ts +61 -0
  177. package/src/utils/rrweb-events.ts +309 -0
  178. package/src/utils/session.ts +18 -0
  179. package/src/utils/time.ts +17 -0
  180. package/src/utils/type-utils.ts +75 -0
  181. package/src/version.ts +1 -1
  182. package/dist/sessionRecorder.d.ts +0 -54
  183. package/dist/sessionRecorder.js +0 -1
  184. package/dist/sessionRecorder.js.map +0 -1
  185. package/examples/sample-expo-app/README.md +0 -142
  186. package/examples/sample-expo-app/app/(tabs)/_layout.tsx +0 -60
  187. package/examples/sample-expo-app/app/(tabs)/explore.tsx +0 -110
  188. package/examples/sample-expo-app/app/(tabs)/index.tsx +0 -125
  189. package/examples/sample-expo-app/app/(tabs)/posts.tsx +0 -96
  190. package/examples/sample-expo-app/app/(tabs)/users.tsx +0 -131
  191. package/examples/sample-expo-app/app/+not-found.tsx +0 -32
  192. package/examples/sample-expo-app/app/_layout.tsx +0 -53
  193. package/examples/sample-expo-app/app/post/[id].tsx +0 -199
  194. package/examples/sample-expo-app/app/user/[id].tsx +0 -270
  195. package/examples/sample-expo-app/app.json +0 -42
  196. package/examples/sample-expo-app/assets/fonts/SpaceMono-Regular.ttf +0 -0
  197. package/examples/sample-expo-app/assets/images/adaptive-icon.png +0 -0
  198. package/examples/sample-expo-app/assets/images/favicon.png +0 -0
  199. package/examples/sample-expo-app/assets/images/icon.png +0 -0
  200. package/examples/sample-expo-app/assets/images/partial-react-logo.png +0 -0
  201. package/examples/sample-expo-app/assets/images/react-logo.png +0 -0
  202. package/examples/sample-expo-app/assets/images/react-logo@2x.png +0 -0
  203. package/examples/sample-expo-app/assets/images/react-logo@3x.png +0 -0
  204. package/examples/sample-expo-app/assets/images/splash-icon.png +0 -0
  205. package/examples/sample-expo-app/components/Collapsible.tsx +0 -45
  206. package/examples/sample-expo-app/components/ErrorView.tsx +0 -52
  207. package/examples/sample-expo-app/components/ExternalLink.tsx +0 -24
  208. package/examples/sample-expo-app/components/HapticTab.tsx +0 -18
  209. package/examples/sample-expo-app/components/HelloWave.tsx +0 -40
  210. package/examples/sample-expo-app/components/LoadingSpinner.tsx +0 -34
  211. package/examples/sample-expo-app/components/ParallaxScrollView.tsx +0 -82
  212. package/examples/sample-expo-app/components/ThemedText.tsx +0 -60
  213. package/examples/sample-expo-app/components/ThemedView.tsx +0 -14
  214. package/examples/sample-expo-app/components/ui/IconSymbol.ios.tsx +0 -32
  215. package/examples/sample-expo-app/components/ui/IconSymbol.tsx +0 -41
  216. package/examples/sample-expo-app/components/ui/TabBarBackground.ios.tsx +0 -19
  217. package/examples/sample-expo-app/components/ui/TabBarBackground.tsx +0 -6
  218. package/examples/sample-expo-app/constants/Colors.ts +0 -26
  219. package/examples/sample-expo-app/eslint.config.js +0 -10
  220. package/examples/sample-expo-app/hooks/useApi.ts +0 -41
  221. package/examples/sample-expo-app/hooks/useColorScheme.ts +0 -1
  222. package/examples/sample-expo-app/hooks/useColorScheme.web.ts +0 -21
  223. package/examples/sample-expo-app/hooks/useThemeColor.ts +0 -21
  224. package/examples/sample-expo-app/metro.config.js +0 -26
  225. package/examples/sample-expo-app/package-lock.json +0 -26296
  226. package/examples/sample-expo-app/package.json +0 -59
  227. package/examples/sample-expo-app/scripts/reset-project.js +0 -112
  228. package/examples/sample-expo-app/services/api.ts +0 -98
  229. package/examples/sample-expo-app/tsconfig.json +0 -17
  230. package/examples/sample-expo-app/utils/navigation.ts +0 -19
  231. package/src/otel/instrumentations/gestureInstrumentation.ts +0 -141
  232. package/src/otel/instrumentations/reactNativeInstrumentation.ts +0 -164
  233. package/src/otel/instrumentations/reactNavigationInstrumentation.ts +0 -114
  234. package/src/sessionRecorder.ts +0 -367
@@ -1,19 +1,39 @@
1
- import { ScreenEvent, RecorderConfig } from '../types'
1
+ import { ScreenEvent, RecorderConfig, EventRecorder } from '../types'
2
+ import { eventWithTime } from '@rrweb/types'
2
3
  import { trace, SpanStatusCode } from '@opentelemetry/api'
3
-
4
- export class ScreenRecorder {
4
+ import { Dimensions } from 'react-native'
5
+ import { captureRef } from 'react-native-view-shot'
6
+ import {
7
+ createRecordingMetaEvent,
8
+ createFullSnapshotEvent,
9
+ createIncrementalSnapshotWithImageUpdate as createIncrementalSnapshotUtil,
10
+ generateScreenHash,
11
+ logger,
12
+ } from '../utils'
13
+
14
+ export class ScreenRecorder implements EventRecorder {
5
15
  private config?: RecorderConfig
6
16
  private isRecording = false
7
17
  private events: ScreenEvent[] = []
8
18
  private captureInterval?: NodeJS.Timeout
9
19
  private captureCount: number = 0
10
20
  private maxCaptures: number = 100 // Limit captures to prevent memory issues
11
- private captureQuality: number = 0.8
12
- private captureFormat: 'png' | 'jpg' | 'webp' = 'png'
21
+ private captureQuality: number = 0.3
22
+ private captureFormat: 'png' | 'jpg' = 'jpg'
13
23
  private screenDimensions: { width: number; height: number } | null = null
14
-
15
- init(config: RecorderConfig): void {
24
+ private currentScreen: string | null = null
25
+ private eventRecorder?: EventRecorder
26
+ private nodeIdCounter: number = 1
27
+ private viewShotRef: any = null
28
+ private lastScreenCapture: string | null = null
29
+ private lastScreenHash: string | null = null
30
+ private enableChangeDetection: boolean = true
31
+ private hashSampleSize: number = 100
32
+ private currentImageNodeId: number | null = null
33
+
34
+ init(config: RecorderConfig, eventRecorder?: EventRecorder): void {
16
35
  this.config = config
36
+ this.eventRecorder = eventRecorder
17
37
  this._getScreenDimensions()
18
38
  }
19
39
 
@@ -21,14 +41,26 @@ export class ScreenRecorder {
21
41
  this.isRecording = true
22
42
  this.events = []
23
43
  this.captureCount = 0
44
+ this.lastScreenCapture = null
45
+ this.lastScreenHash = null
46
+ this.currentImageNodeId = null // Reset image node ID for new session
47
+ logger.info('ScreenRecorder', 'Screen recording started')
48
+ // Emit screen recording started meta event
49
+
50
+ this.recordEvent(createRecordingMetaEvent())
51
+
24
52
  this._startPeriodicCapture()
25
- console.log('Screen recording started')
53
+
54
+ // Capture initial screen immediately
55
+ this._captureScreen()
56
+
57
+ // Screen recording started
26
58
  }
27
59
 
28
60
  stop(): void {
29
61
  this.isRecording = false
30
62
  this._stopPeriodicCapture()
31
- console.log('Screen recording stopped')
63
+ // Screen recording stopped
32
64
  }
33
65
 
34
66
  pause(): void {
@@ -38,15 +70,14 @@ export class ScreenRecorder {
38
70
 
39
71
  resume(): void {
40
72
  this.isRecording = true
41
- this._startPeriodicCapture()
73
+ // this._startPeriodicCapture()
42
74
  }
43
75
 
44
76
  private _getScreenDimensions(): void {
45
77
  try {
46
- const { Dimensions } = require('react-native')
47
78
  this.screenDimensions = Dimensions.get('window')
48
79
  } catch (error) {
49
- console.warn('Failed to get screen dimensions:', error)
80
+ // Failed to get screen dimensions - silently continue
50
81
  this.screenDimensions = { width: 375, height: 667 } // Default fallback
51
82
  }
52
83
  }
@@ -56,7 +87,7 @@ export class ScreenRecorder {
56
87
  clearInterval(this.captureInterval)
57
88
  }
58
89
 
59
- // Capture screen every 5 seconds
90
+ // Capture screen every 5 seconds (reduced frequency)
60
91
  this.captureInterval = setInterval(() => {
61
92
  this._captureScreen()
62
93
  }, 5000)
@@ -73,145 +104,166 @@ export class ScreenRecorder {
73
104
  if (!this.isRecording || this.captureCount >= this.maxCaptures) return
74
105
 
75
106
  try {
76
- const startTime = Date.now()
77
- const screenData = await this._performScreenCapture()
78
- const captureTime = Date.now() - startTime
79
-
80
- if (screenData) {
81
- const screenEvent: ScreenEvent = {
82
- type: 'screenCapture',
83
- timestamp: Date.now(),
84
- dataUrl: screenData,
85
- metadata: {
86
- captureTime,
87
- captureCount: this.captureCount + 1,
88
- quality: this.captureQuality,
89
- format: this.captureFormat,
90
- screenWidth: this.screenDimensions?.width,
91
- screenHeight: this.screenDimensions?.height,
92
- },
107
+ const base64Image = await this._captureScreenBase64()
108
+
109
+ if (base64Image) {
110
+ // Check if screen has changed by comparing with previous capture
111
+ const hasChanged = this.enableChangeDetection ? this._hasScreenChanged(base64Image) : true
112
+
113
+ if (hasChanged) {
114
+ // Use incremental snapshot if we have an existing image node, otherwise create full snapshot
115
+ if (this.currentImageNodeId !== null && this.lastScreenCapture) {
116
+ const success = this.updateScreenWithIncrementalSnapshot(base64Image)
117
+ if (!success) {
118
+ // Fallback to full snapshot if incremental update fails
119
+ this._createAndEmitFullSnapshotEvent(base64Image)
120
+ }
121
+ } else {
122
+ // First capture or no existing image node - create full snapshot
123
+ this._createAndEmitFullSnapshotEvent(base64Image)
124
+ }
125
+
126
+ this.lastScreenCapture = base64Image
127
+ this.lastScreenHash = this._generateScreenHash(base64Image)
128
+ this.captureCount++
93
129
  }
94
-
95
- this.events.push(screenEvent)
96
- this.captureCount++
97
- this._sendEvent(screenEvent)
98
- this._recordOpenTelemetrySpan(screenEvent)
99
130
  }
100
131
  } catch (error) {
101
- console.error('Failed to capture screen:', error)
102
- this._recordScreenCaptureError(error instanceof Error ? error : new Error(String(error)))
132
+ this._recordScreenCaptureError(error as Error)
103
133
  }
104
134
  }
105
135
 
106
-
107
-
108
- private async _performScreenCapture(): Promise<string | null> {
136
+ private async _captureScreenBase64(): Promise<string | null> {
109
137
  try {
110
- // Try react-native-view-shot first
111
- const ViewShot = require('react-native-view-shot')
112
- if (ViewShot && ViewShot.captureRef) {
113
- return await this._captureWithViewShot()
138
+ if (!this.viewShotRef) {
139
+ logger.warn('ScreenRecorder', 'ViewShot ref not available for screen capture')
140
+ return null
114
141
  }
115
- } catch (error) {
116
- console.warn('react-native-view-shot not available:', error)
117
- }
118
142
 
119
- try {
120
- // Try react-native-screenshot
121
- // const Screenshot = require('react-native-screenshot')
122
- // if (Screenshot && Screenshot.takeScreenshot) {
123
- // return await this._captureWithScreenshot()
124
- // }
143
+ // Capture the screen using react-native-view-shot
144
+ const result = await captureRef(this.viewShotRef, {
145
+ format: this.captureFormat,
146
+ quality: this.captureQuality,
147
+ result: 'base64',
148
+ })
149
+
150
+ return result
125
151
  } catch (error) {
126
- console.warn('react-native-screenshot not available:', error)
152
+ logger.error('ScreenRecorder', 'Failed to capture screen. Make sure react-native-view-shot is properly installed and linked:', error)
153
+ return null
127
154
  }
155
+ }
156
+
157
+ private _createAndEmitFullSnapshotEvent(base64Image: string): void {
158
+ if (!this.screenDimensions) return
128
159
 
129
- // Fallback to placeholder
130
- return this._createPlaceholderImage()
160
+ // Use the new createFullSnapshot method
161
+ const fullSnapshotEvent = this.createFullSnapshot(base64Image)
162
+ this.recordEvent(fullSnapshotEvent)
131
163
  }
132
164
 
133
- private async _captureWithViewShot(): Promise<string | null> {
134
- try {
135
- const ViewShot = require('react-native-view-shot')
165
+ /**
166
+ * Create a full snapshot event with the given base64 image
167
+ * @param base64Image - Base64 encoded image data
168
+ * @returns Full snapshot event
169
+ */
170
+ createFullSnapshot(base64Image: string): eventWithTime {
171
+ if (!this.screenDimensions) {
172
+ throw new Error('Screen dimensions not available')
173
+ }
136
174
 
137
- // Get the root view reference
138
- const { findNodeHandle } = require('react-native')
139
- const rootViewRef = this._getRootViewRef()
175
+ const { width, height } = this.screenDimensions
176
+ this.nodeIdCounter = 1
140
177
 
141
- if (rootViewRef) {
142
- const options = {
143
- format: this.captureFormat,
144
- quality: this.captureQuality,
145
- result: 'data-uri',
146
- }
178
+ // Use utility function to create full snapshot event
179
+ const fullSnapshotEvent = createFullSnapshotEvent(
180
+ base64Image,
181
+ width,
182
+ height,
183
+ this.captureFormat,
184
+ { current: this.nodeIdCounter },
185
+ )
147
186
 
148
- return await ViewShot.captureRef(rootViewRef, options)
149
- }
150
- } catch (error) {
151
- console.warn('Failed to capture with ViewShot:', error)
152
- }
153
- return null
187
+ // Store the image node ID for future incremental updates
188
+ // The image node ID is the first node created (after the document)
189
+ this.currentImageNodeId = 0 // First element node is the image
190
+
191
+ return fullSnapshotEvent
154
192
  }
155
193
 
156
- private async _captureWithScreenshot(): Promise<string | null> {
157
- try {
158
- // const Screenshot = require('react-native-screenshot')
194
+ /**
195
+ * Create an incremental snapshot event with mutation data to update image src
196
+ * @param base64Image - New base64 encoded image data
197
+ * @param imageNodeId - ID of the image node to update
198
+ * @returns Incremental snapshot event with mutation data
199
+ */
200
+ createIncrementalSnapshotWithImageUpdate(base64Image: string): eventWithTime {
201
+ return createIncrementalSnapshotUtil(
202
+ base64Image,
203
+ this.captureFormat,
204
+ )
205
+ }
159
206
 
160
- const options = {
161
- format: this.captureFormat,
162
- quality: this.captureQuality,
163
- }
164
207
 
165
- // return await Screenshot.takeScreenshot(options)
166
- } catch (error) {
167
- console.warn('Failed to capture with Screenshot:', error)
208
+ /**
209
+ * Update the screen with a new image using incremental snapshot
210
+ * @param base64Image - New base64 encoded image data
211
+ * @returns true if update was successful, false otherwise
212
+ */
213
+ updateScreenWithIncrementalSnapshot(base64Image: string): boolean {
214
+ if (this.currentImageNodeId === null) {
215
+ logger.warn('ScreenRecorder', 'No image node ID available for incremental update')
216
+ return false
168
217
  }
169
- return null
218
+
219
+ const incrementalEvent = this.createIncrementalSnapshotWithImageUpdate(base64Image)
220
+ this.recordEvent(incrementalEvent)
221
+ return true
170
222
  }
171
223
 
172
- private _getRootViewRef(): any {
173
- try {
174
- // Try to get the root view reference
175
- const { AppRegistry } = require('react-native')
176
- const appName = AppRegistry.getAppKeys()[0]
177
- return appName ? { current: null } : null
178
- } catch (error) {
179
- console.warn('Failed to get root view ref:', error)
180
- return null
181
- }
224
+ /**
225
+ * Force a full snapshot (useful when screen dimensions change or for debugging)
226
+ * @param base64Image - Base64 encoded image data
227
+ */
228
+ forceFullSnapshot(base64Image: string): void {
229
+ this._createAndEmitFullSnapshotEvent(base64Image)
230
+ this.lastScreenCapture = base64Image
231
+ this.lastScreenHash = this._generateScreenHash(base64Image)
232
+ this.captureCount++
182
233
  }
183
234
 
184
- private _createPlaceholderImage(): string {
185
- // Create a simple placeholder image data URL
186
- // Note: This won't work in React Native environment, but provides a fallback
187
- const width = this.screenDimensions?.width || 375
188
- const height = this.screenDimensions?.height || 667
235
+ /**
236
+ * Check if the screen has changed by comparing with the previous capture
237
+ * @param currentBase64 - Current screen capture as base64
238
+ * @returns true if screen has changed, false otherwise
239
+ */
240
+ private _hasScreenChanged(currentBase64: string): boolean {
241
+ // If this is the first capture, consider it changed
242
+ if (!this.lastScreenCapture) {
243
+ return true
244
+ }
189
245
 
190
- // Return a simple data URL for placeholder
191
- const svgContent = `
192
- <svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
193
- <rect width="100%" height="100%" fill="#f0f0f0"/>
194
- <text x="50%" y="50%" text-anchor="middle" dy=".3em" fill="#666" font-family="Arial" font-size="16">
195
- Screen Capture Placeholder
196
- </text>
197
- <text x="50%" y="60%" text-anchor="middle" dy=".3em" fill="#666" font-family="Arial" font-size="14">
198
- Captured at ${new Date().toLocaleTimeString()}
199
- </text>
200
- </svg>
201
- `
246
+ // Generate hash for current capture
247
+ const currentHash = this._generateScreenHash(currentBase64)
202
248
 
203
- // Use a simple base64 encoding alternative for React Native
204
- const base64Content = Buffer.from(svgContent, 'utf8').toString('base64')
205
- return `data:image/svg+xml;base64,${base64Content}`
249
+ // Compare with previous hash
250
+ return currentHash !== this.lastScreenHash
251
+ }
252
+
253
+ /**
254
+ * Generate a simple hash for screen comparison
255
+ * This is a lightweight hash that focuses on the beginning and end of the base64 string
256
+ * to detect changes without doing a full comparison
257
+ * @param base64Image - Base64 encoded image
258
+ * @returns Hash string for comparison
259
+ */
260
+ private _generateScreenHash(base64Image: string): string {
261
+ return generateScreenHash(base64Image, this.hashSampleSize)
206
262
  }
207
263
 
208
264
  private _sendEvent(event: ScreenEvent): void {
209
- console.log('Screen event recorded:', {
210
- type: event.type,
211
- timestamp: event.timestamp,
212
- captureCount: event.metadata?.captureCount,
213
- captureTime: event.metadata?.captureTime,
214
- })
265
+ // Screen event recorded
266
+ // Send event to backend or store locally
215
267
  }
216
268
 
217
269
  private _recordOpenTelemetrySpan(event: ScreenEvent): void {
@@ -233,7 +285,7 @@ export class ScreenRecorder {
233
285
  span.setStatus({ code: SpanStatusCode.OK })
234
286
  span.end()
235
287
  } catch (error) {
236
- console.warn('Failed to record OpenTelemetry span for screen:', error)
288
+ // Failed to record OpenTelemetry span for screen - silently continue
237
289
  }
238
290
  }
239
291
 
@@ -252,36 +304,21 @@ export class ScreenRecorder {
252
304
  span.recordException(error)
253
305
  span.end()
254
306
  } catch (spanError) {
255
- console.warn('Failed to record error span:', spanError)
256
- }
257
- }
258
-
259
- // Manual screen capture methods
260
- async captureScreenNow(): Promise<string | null> {
261
- if (!this.isRecording) {
262
- console.warn('Screen recording not active')
263
- return null
307
+ // Failed to record error span - silently continue
264
308
  }
265
-
266
- return await this._performScreenCapture()
267
309
  }
268
310
 
269
- async captureSpecificElement(elementRef: any, options?: {
270
- format?: 'png' | 'jpg' | 'webp'
271
- quality?: number
272
- }): Promise<string | null> {
311
+ async captureSpecificElement(
312
+ elementRef: any,
313
+ options?: {
314
+ format?: 'png' | 'jpg' | 'webp'
315
+ quality?: number
316
+ },
317
+ ): Promise<string | null> {
273
318
  try {
274
- const ViewShot = require('react-native-view-shot')
275
-
276
- const captureOptions = {
277
- format: options?.format || this.captureFormat,
278
- quality: options?.quality || this.captureQuality,
279
- result: 'data-uri',
280
- }
281
-
282
- return await ViewShot.captureRef(elementRef, captureOptions)
319
+ return await captureRef(elementRef)
283
320
  } catch (error) {
284
- console.error('Failed to capture specific element:', error)
321
+ // Failed to capture specific element - silently continue
285
322
  return null
286
323
  }
287
324
  }
@@ -303,7 +340,7 @@ export class ScreenRecorder {
303
340
  this.captureQuality = Math.max(0.1, Math.min(1.0, quality))
304
341
  }
305
342
 
306
- setCaptureFormat(format: 'png' | 'jpg' | 'webp'): void {
343
+ setCaptureFormat(format: 'png' | 'jpg'): void {
307
344
  this.captureFormat = format
308
345
  }
309
346
 
@@ -311,9 +348,26 @@ export class ScreenRecorder {
311
348
  this.maxCaptures = Math.max(1, max)
312
349
  }
313
350
 
351
+ /**
352
+ * Enable or disable change detection
353
+ * @param enabled - Whether to enable change detection
354
+ */
355
+ setChangeDetection(enabled: boolean): void {
356
+ this.enableChangeDetection = enabled
357
+ }
358
+
359
+ /**
360
+ * Set the hash sample size for change detection
361
+ * @param size - Number of characters to sample from each part of the image
362
+ */
363
+ setHashSampleSize(size: number): void {
364
+ this.hashSampleSize = Math.max(10, Math.min(1000, size))
365
+ }
366
+
314
367
  // Performance monitoring
315
368
  recordScreenPerformance(screenName: string, loadTime: number): void {
316
369
  const event: ScreenEvent = {
370
+ screenName,
317
371
  type: 'screenCapture',
318
372
  timestamp: Date.now(),
319
373
  metadata: {
@@ -324,7 +378,9 @@ export class ScreenRecorder {
324
378
  },
325
379
  }
326
380
 
327
- this.events.push(event); this._sendEvent(event); this._recordOpenTelemetrySpan(event)
381
+ this.events.push(event)
382
+ this._sendEvent(event)
383
+ this._recordOpenTelemetrySpan(event)
328
384
  this.events.push(event)
329
385
  this._sendEvent(event)
330
386
  this._recordOpenTelemetrySpan(event)
@@ -333,6 +389,7 @@ export class ScreenRecorder {
333
389
  // Error tracking
334
390
  recordScreenError(error: Error, screenName?: string): void {
335
391
  const event: ScreenEvent = {
392
+ screenName: screenName || 'unknown',
336
393
  type: 'screenCapture',
337
394
  timestamp: Date.now(),
338
395
  metadata: {
@@ -344,7 +401,9 @@ export class ScreenRecorder {
344
401
  },
345
402
  }
346
403
 
347
- this.events.push(event); this._sendEvent(event); this._recordOpenTelemetrySpan(event)
404
+ this.events.push(event)
405
+ this._sendEvent(event)
406
+ this._recordOpenTelemetrySpan(event)
348
407
  this.events.push(event)
349
408
  this._sendEvent(event)
350
409
  this._recordScreenCaptureError(error)
@@ -371,15 +430,13 @@ export class ScreenRecorder {
371
430
  }
372
431
 
373
432
  if (this.events.length > 0) {
374
- const captureTimes = this.events
375
- .map(event => event.metadata?.captureTime || 0)
376
- .filter(time => time > 0)
433
+ const captureTimes = this.events.map((event) => event.metadata?.captureTime || 0).filter((time) => time > 0)
377
434
 
378
435
  if (captureTimes.length > 0) {
379
436
  stats.averageCaptureTime = captureTimes.reduce((a, b) => a + b, 0) / captureTimes.length
380
437
  }
381
438
 
382
- const successfulCaptures = this.events.filter(event => event.dataUrl).length
439
+ const successfulCaptures = this.events.filter((event) => event.dataUrl).length
383
440
  stats.successRate = (successfulCaptures / this.events.length) * 100
384
441
  }
385
442
 
@@ -406,6 +463,36 @@ export class ScreenRecorder {
406
463
  shutdown(): void {
407
464
  this.stop()
408
465
  this.clearEvents()
409
- console.log('Screen recorder shutdown')
466
+ // Screen recorder shutdown
467
+ }
468
+
469
+ /**
470
+ * Set the viewshot ref for screen capture
471
+ * @param ref - React Native View ref for screen capture
472
+ */
473
+ setViewShotRef(ref: any): void {
474
+ this.viewShotRef = ref
475
+ }
476
+
477
+ /**
478
+ * Force capture screen (useful after touch interactions)
479
+ * This bypasses the change detection and always captures
480
+ */
481
+ forceCapture(): void {
482
+ if (!this.isRecording) {
483
+ return
484
+ }
485
+
486
+ this._captureScreen()
487
+ }
488
+
489
+ /**
490
+ * Record an rrweb event
491
+ * @param event - The rrweb event to record
492
+ */
493
+ recordEvent(event: any): void {
494
+ if (this.eventRecorder) {
495
+ this.eventRecorder.recordEvent(event)
496
+ }
410
497
  }
411
498
  }