@multiplayer-app/session-recorder-react-native 0.0.1-alpha.6 → 0.0.1-alpha.8
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/RRWEB_INTEGRATION.md +336 -0
- package/VIEWSHOT_INTEGRATION_TEST.md +123 -0
- package/copy-react-native-dist.sh +38 -0
- package/dist/components/GestureCaptureWrapper.d.ts +6 -0
- package/dist/components/GestureCaptureWrapper.js +1 -0
- package/dist/components/GestureCaptureWrapper.js.map +1 -0
- package/dist/config/constants.d.ts +0 -1
- package/dist/config/constants.js +1 -1
- package/dist/config/constants.js.map +1 -1
- package/dist/context/SessionRecorderContext.d.ts +1 -2
- package/dist/context/SessionRecorderContext.js +1 -1
- package/dist/context/SessionRecorderContext.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/otel/helpers.d.ts +4 -4
- package/dist/otel/helpers.js +1 -1
- package/dist/otel/helpers.js.map +1 -1
- package/dist/otel/index.js +1 -1
- package/dist/otel/index.js.map +1 -1
- package/dist/otel/instrumentations/index.d.ts +2 -3
- package/dist/otel/instrumentations/index.js +1 -1
- package/dist/otel/instrumentations/index.js.map +1 -1
- package/dist/otel/instrumentations/reactNativeInstrumentation.js +1 -1
- package/dist/otel/instrumentations/reactNativeInstrumentation.js.map +1 -1
- package/dist/otel/instrumentations/reactNavigationInstrumentation.d.ts +1 -0
- package/dist/otel/instrumentations/reactNavigationInstrumentation.js +1 -1
- package/dist/otel/instrumentations/reactNavigationInstrumentation.js.map +1 -1
- package/dist/recorder/eventExporter.d.ts +21 -0
- package/dist/recorder/eventExporter.js +1 -0
- package/dist/recorder/eventExporter.js.map +1 -0
- package/dist/recorder/gestureHandlerRecorder.d.ts +19 -0
- package/dist/recorder/gestureHandlerRecorder.js +1 -0
- package/dist/recorder/gestureHandlerRecorder.js.map +1 -0
- package/dist/recorder/gestureRecorder.d.ts +69 -3
- package/dist/recorder/gestureRecorder.js +1 -1
- package/dist/recorder/gestureRecorder.js.map +1 -1
- package/dist/recorder/index.d.ts +59 -6
- package/dist/recorder/index.js +1 -1
- package/dist/recorder/index.js.map +1 -1
- package/dist/recorder/navigationTracker.js +1 -1
- package/dist/recorder/navigationTracker.js.map +1 -1
- package/dist/recorder/screenRecorder.d.ts +83 -4
- package/dist/recorder/screenRecorder.js +1 -1
- package/dist/recorder/screenRecorder.js.map +1 -1
- package/dist/services/api.service.js.map +1 -1
- package/dist/services/storage.service.js.map +1 -1
- package/dist/session-recorder.d.ts +42 -2
- package/dist/session-recorder.js +1 -1
- package/dist/session-recorder.js.map +1 -1
- package/dist/types/index.d.ts +32 -0
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/rrweb.d.ts +118 -0
- package/dist/types/rrweb.js +1 -0
- package/dist/types/rrweb.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/logger.d.ts +112 -0
- package/dist/utils/logger.js +1 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/rrweb-events.d.ts +65 -0
- package/dist/utils/rrweb-events.js +1 -0
- package/dist/utils/rrweb-events.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/example-usage.tsx +174 -0
- package/package.json +5 -2
- package/src/components/GestureCaptureWrapper.tsx +110 -0
- package/src/config/constants.ts +3 -3
- package/src/context/SessionRecorderContext.tsx +106 -34
- package/src/index.ts +1 -0
- package/src/otel/helpers.ts +38 -20
- package/src/otel/index.ts +7 -3
- package/src/otel/instrumentations/index.ts +82 -40
- package/src/otel/instrumentations/reactNativeInstrumentation.ts +2 -1
- package/src/otel/instrumentations/reactNavigationInstrumentation.ts +5 -0
- package/src/recorder/eventExporter.ts +141 -0
- package/src/recorder/gestureHandlerRecorder.ts +157 -0
- package/src/recorder/gestureRecorder.ts +198 -3
- package/src/recorder/index.ts +130 -24
- package/src/recorder/navigationTracker.ts +2 -0
- package/src/recorder/screenRecorder.ts +261 -22
- package/src/services/api.service.ts +1 -8
- package/src/services/storage.service.ts +1 -0
- package/src/session-recorder.ts +97 -11
- package/src/types/index.ts +45 -1
- package/src/utils/index.ts +2 -0
- package/src/utils/logger.ts +225 -0
- package/src/utils/rrweb-events.ts +311 -0
- package/src/version.ts +1 -1
|
@@ -1,22 +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
|
import { Dimensions } from 'react-native'
|
|
4
5
|
import { captureRef } from 'react-native-view-shot'
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
import {
|
|
7
|
+
createRecordingMetaEvent,
|
|
8
|
+
createFullSnapshotEvent,
|
|
9
|
+
createIncrementalSnapshotWithImageUpdate as createIncrementalSnapshotUtil,
|
|
10
|
+
generateScreenHash,
|
|
11
|
+
logger,
|
|
12
|
+
} from '../utils'
|
|
13
|
+
|
|
14
|
+
export class ScreenRecorder implements EventRecorder {
|
|
7
15
|
private config?: RecorderConfig
|
|
8
16
|
private isRecording = false
|
|
9
17
|
private events: ScreenEvent[] = []
|
|
10
18
|
private captureInterval?: NodeJS.Timeout
|
|
11
19
|
private captureCount: number = 0
|
|
12
20
|
private maxCaptures: number = 100 // Limit captures to prevent memory issues
|
|
13
|
-
private captureQuality: number = 0.
|
|
14
|
-
private captureFormat: 'png' | 'jpg'
|
|
21
|
+
private captureQuality: number = 0.3
|
|
22
|
+
private captureFormat: 'png' | 'jpg' = 'jpg'
|
|
15
23
|
private screenDimensions: { width: number; height: number } | null = null
|
|
16
24
|
private currentScreen: string | null = null
|
|
17
|
-
|
|
18
|
-
|
|
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 {
|
|
19
35
|
this.config = config
|
|
36
|
+
this.eventRecorder = eventRecorder
|
|
20
37
|
this._getScreenDimensions()
|
|
21
38
|
}
|
|
22
39
|
|
|
@@ -24,7 +41,19 @@ export class ScreenRecorder {
|
|
|
24
41
|
this.isRecording = true
|
|
25
42
|
this.events = []
|
|
26
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
|
+
|
|
27
52
|
this._startPeriodicCapture()
|
|
53
|
+
|
|
54
|
+
// Capture initial screen immediately
|
|
55
|
+
this._captureScreen()
|
|
56
|
+
|
|
28
57
|
// Screen recording started
|
|
29
58
|
}
|
|
30
59
|
|
|
@@ -58,7 +87,7 @@ export class ScreenRecorder {
|
|
|
58
87
|
clearInterval(this.captureInterval)
|
|
59
88
|
}
|
|
60
89
|
|
|
61
|
-
// Capture screen every 5 seconds
|
|
90
|
+
// Capture screen every 5 seconds (reduced frequency)
|
|
62
91
|
this.captureInterval = setInterval(() => {
|
|
63
92
|
this._captureScreen()
|
|
64
93
|
}, 5000)
|
|
@@ -74,9 +103,171 @@ export class ScreenRecorder {
|
|
|
74
103
|
private async _captureScreen(): Promise<void> {
|
|
75
104
|
if (!this.isRecording || this.captureCount >= this.maxCaptures) return
|
|
76
105
|
|
|
106
|
+
try {
|
|
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 && 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++
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
this._recordScreenCaptureError(error as Error)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private async _captureScreenBase64(): Promise<string | null> {
|
|
137
|
+
try {
|
|
138
|
+
if (!this.viewShotRef) {
|
|
139
|
+
logger.warn('ScreenRecorder', 'ViewShot ref not available for screen capture')
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
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
|
|
151
|
+
} catch (error) {
|
|
152
|
+
logger.error('ScreenRecorder', 'Failed to capture screen', error)
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
77
155
|
}
|
|
78
156
|
|
|
157
|
+
private _createAndEmitFullSnapshotEvent(base64Image: string): void {
|
|
158
|
+
if (!this.screenDimensions) return
|
|
159
|
+
|
|
160
|
+
// Use the new createFullSnapshot method
|
|
161
|
+
const fullSnapshotEvent = this.createFullSnapshot(base64Image)
|
|
162
|
+
this.recordEvent(fullSnapshotEvent)
|
|
163
|
+
}
|
|
164
|
+
|
|
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
|
+
}
|
|
174
|
+
|
|
175
|
+
const { width, height } = this.screenDimensions
|
|
176
|
+
this.nodeIdCounter = 1
|
|
177
|
+
|
|
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
|
+
)
|
|
79
186
|
|
|
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 = 2 // First element node is the image
|
|
190
|
+
|
|
191
|
+
return fullSnapshotEvent
|
|
192
|
+
}
|
|
193
|
+
|
|
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, imageNodeId: number): eventWithTime {
|
|
201
|
+
return createIncrementalSnapshotUtil(
|
|
202
|
+
base64Image,
|
|
203
|
+
imageNodeId,
|
|
204
|
+
this.captureFormat,
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Get the current image node ID
|
|
210
|
+
* @returns The current image node ID or null if not set
|
|
211
|
+
*/
|
|
212
|
+
getCurrentImageNodeId(): number | null {
|
|
213
|
+
return this.currentImageNodeId
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Update the screen with a new image using incremental snapshot
|
|
218
|
+
* @param base64Image - New base64 encoded image data
|
|
219
|
+
* @returns true if update was successful, false otherwise
|
|
220
|
+
*/
|
|
221
|
+
updateScreenWithIncrementalSnapshot(base64Image: string): boolean {
|
|
222
|
+
if (!this.currentImageNodeId) {
|
|
223
|
+
logger.warn('ScreenRecorder', 'No image node ID available for incremental update')
|
|
224
|
+
return false
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const incrementalEvent = this.createIncrementalSnapshotWithImageUpdate(base64Image, this.currentImageNodeId)
|
|
228
|
+
this.recordEvent(incrementalEvent)
|
|
229
|
+
return true
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Force a full snapshot (useful when screen dimensions change or for debugging)
|
|
234
|
+
* @param base64Image - Base64 encoded image data
|
|
235
|
+
*/
|
|
236
|
+
forceFullSnapshot(base64Image: string): void {
|
|
237
|
+
this._createAndEmitFullSnapshotEvent(base64Image)
|
|
238
|
+
this.lastScreenCapture = base64Image
|
|
239
|
+
this.lastScreenHash = this._generateScreenHash(base64Image)
|
|
240
|
+
this.captureCount++
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Check if the screen has changed by comparing with the previous capture
|
|
245
|
+
* @param currentBase64 - Current screen capture as base64
|
|
246
|
+
* @returns true if screen has changed, false otherwise
|
|
247
|
+
*/
|
|
248
|
+
private _hasScreenChanged(currentBase64: string): boolean {
|
|
249
|
+
// If this is the first capture, consider it changed
|
|
250
|
+
if (!this.lastScreenCapture) {
|
|
251
|
+
return true
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Generate hash for current capture
|
|
255
|
+
const currentHash = this._generateScreenHash(currentBase64)
|
|
256
|
+
|
|
257
|
+
// Compare with previous hash
|
|
258
|
+
return currentHash !== this.lastScreenHash
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Generate a simple hash for screen comparison
|
|
263
|
+
* This is a lightweight hash that focuses on the beginning and end of the base64 string
|
|
264
|
+
* to detect changes without doing a full comparison
|
|
265
|
+
* @param base64Image - Base64 encoded image
|
|
266
|
+
* @returns Hash string for comparison
|
|
267
|
+
*/
|
|
268
|
+
private _generateScreenHash(base64Image: string): string {
|
|
269
|
+
return generateScreenHash(base64Image, this.hashSampleSize)
|
|
270
|
+
}
|
|
80
271
|
|
|
81
272
|
private _sendEvent(event: ScreenEvent): void {
|
|
82
273
|
// Screen event recorded
|
|
@@ -125,14 +316,14 @@ export class ScreenRecorder {
|
|
|
125
316
|
}
|
|
126
317
|
}
|
|
127
318
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
319
|
+
async captureSpecificElement(
|
|
320
|
+
elementRef: any,
|
|
321
|
+
options?: {
|
|
322
|
+
format?: 'png' | 'jpg' | 'webp'
|
|
323
|
+
quality?: number
|
|
324
|
+
},
|
|
325
|
+
): Promise<string | null> {
|
|
133
326
|
try {
|
|
134
|
-
|
|
135
|
-
|
|
136
327
|
return await captureRef(elementRef)
|
|
137
328
|
} catch (error) {
|
|
138
329
|
// Failed to capture specific element - silently continue
|
|
@@ -157,7 +348,7 @@ export class ScreenRecorder {
|
|
|
157
348
|
this.captureQuality = Math.max(0.1, Math.min(1.0, quality))
|
|
158
349
|
}
|
|
159
350
|
|
|
160
|
-
setCaptureFormat(format: 'png' | 'jpg'
|
|
351
|
+
setCaptureFormat(format: 'png' | 'jpg'): void {
|
|
161
352
|
this.captureFormat = format
|
|
162
353
|
}
|
|
163
354
|
|
|
@@ -165,6 +356,22 @@ export class ScreenRecorder {
|
|
|
165
356
|
this.maxCaptures = Math.max(1, max)
|
|
166
357
|
}
|
|
167
358
|
|
|
359
|
+
/**
|
|
360
|
+
* Enable or disable change detection
|
|
361
|
+
* @param enabled - Whether to enable change detection
|
|
362
|
+
*/
|
|
363
|
+
setChangeDetection(enabled: boolean): void {
|
|
364
|
+
this.enableChangeDetection = enabled
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Set the hash sample size for change detection
|
|
369
|
+
* @param size - Number of characters to sample from each part of the image
|
|
370
|
+
*/
|
|
371
|
+
setHashSampleSize(size: number): void {
|
|
372
|
+
this.hashSampleSize = Math.max(10, Math.min(1000, size))
|
|
373
|
+
}
|
|
374
|
+
|
|
168
375
|
// Performance monitoring
|
|
169
376
|
recordScreenPerformance(screenName: string, loadTime: number): void {
|
|
170
377
|
const event: ScreenEvent = {
|
|
@@ -179,7 +386,9 @@ export class ScreenRecorder {
|
|
|
179
386
|
},
|
|
180
387
|
}
|
|
181
388
|
|
|
182
|
-
this.events.push(event)
|
|
389
|
+
this.events.push(event)
|
|
390
|
+
this._sendEvent(event)
|
|
391
|
+
this._recordOpenTelemetrySpan(event)
|
|
183
392
|
this.events.push(event)
|
|
184
393
|
this._sendEvent(event)
|
|
185
394
|
this._recordOpenTelemetrySpan(event)
|
|
@@ -200,7 +409,9 @@ export class ScreenRecorder {
|
|
|
200
409
|
},
|
|
201
410
|
}
|
|
202
411
|
|
|
203
|
-
this.events.push(event)
|
|
412
|
+
this.events.push(event)
|
|
413
|
+
this._sendEvent(event)
|
|
414
|
+
this._recordOpenTelemetrySpan(event)
|
|
204
415
|
this.events.push(event)
|
|
205
416
|
this._sendEvent(event)
|
|
206
417
|
this._recordScreenCaptureError(error)
|
|
@@ -227,15 +438,13 @@ export class ScreenRecorder {
|
|
|
227
438
|
}
|
|
228
439
|
|
|
229
440
|
if (this.events.length > 0) {
|
|
230
|
-
const captureTimes = this.events
|
|
231
|
-
.map(event => event.metadata?.captureTime || 0)
|
|
232
|
-
.filter(time => time > 0)
|
|
441
|
+
const captureTimes = this.events.map((event) => event.metadata?.captureTime || 0).filter((time) => time > 0)
|
|
233
442
|
|
|
234
443
|
if (captureTimes.length > 0) {
|
|
235
444
|
stats.averageCaptureTime = captureTimes.reduce((a, b) => a + b, 0) / captureTimes.length
|
|
236
445
|
}
|
|
237
446
|
|
|
238
|
-
const successfulCaptures = this.events.filter(event => event.dataUrl).length
|
|
447
|
+
const successfulCaptures = this.events.filter((event) => event.dataUrl).length
|
|
239
448
|
stats.successRate = (successfulCaptures / this.events.length) * 100
|
|
240
449
|
}
|
|
241
450
|
|
|
@@ -264,4 +473,34 @@ export class ScreenRecorder {
|
|
|
264
473
|
this.clearEvents()
|
|
265
474
|
// Screen recorder shutdown
|
|
266
475
|
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Set the viewshot ref for screen capture
|
|
479
|
+
* @param ref - React Native View ref for screen capture
|
|
480
|
+
*/
|
|
481
|
+
setViewShotRef(ref: any): void {
|
|
482
|
+
this.viewShotRef = ref
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Force capture screen (useful after touch interactions)
|
|
487
|
+
* This bypasses the change detection and always captures
|
|
488
|
+
*/
|
|
489
|
+
forceCapture(): void {
|
|
490
|
+
if (!this.isRecording) {
|
|
491
|
+
return
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
this._captureScreen()
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Record an rrweb event
|
|
499
|
+
* @param event - The rrweb event to record
|
|
500
|
+
*/
|
|
501
|
+
recordEvent(event: any): void {
|
|
502
|
+
if (this.eventRecorder) {
|
|
503
|
+
this.eventRecorder.recordEvent(event)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
267
506
|
}
|
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import { SessionRecorderOptions, IResourceAttributes, ISessionAttributes } from '../types'
|
|
2
2
|
|
|
3
|
-
// Type definitions for fetch API
|
|
4
|
-
type RequestInit = {
|
|
5
|
-
method?: string
|
|
6
|
-
headers?: Record<string, string>
|
|
7
|
-
body?: string
|
|
8
|
-
signal?: AbortSignal
|
|
9
|
-
}
|
|
10
|
-
|
|
11
3
|
export interface StartSessionRequest {
|
|
12
4
|
name?: string
|
|
13
5
|
stoppedAt?: string | number
|
|
@@ -37,6 +29,7 @@ export class ApiService {
|
|
|
37
29
|
}
|
|
38
30
|
|
|
39
31
|
init(config: SessionRecorderOptions): void {
|
|
32
|
+
|
|
40
33
|
this.config = {
|
|
41
34
|
...this.config,
|
|
42
35
|
...config,
|
|
@@ -87,6 +87,7 @@ export class StorageService {
|
|
|
87
87
|
saveSessionState(state: SessionState): void {
|
|
88
88
|
try {
|
|
89
89
|
StorageService.cache.sessionState = state
|
|
90
|
+
|
|
90
91
|
AsyncStorage.setItem(StorageService.SESSION_STATE_KEY, state).catch(error => {
|
|
91
92
|
// Failed to persist session state - silently continue
|
|
92
93
|
})
|
package/src/session-recorder.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
|
|
2
2
|
import { SessionType } from '@multiplayer-app/session-recorder-common'
|
|
3
|
+
import { Observable } from 'lib0/observable'
|
|
3
4
|
|
|
4
5
|
import { TracerReactNativeSDK } from './otel'
|
|
5
6
|
import { RecorderReactNativeSDK } from './recorder'
|
|
7
|
+
import { logger } from './utils'
|
|
6
8
|
|
|
7
9
|
import {
|
|
8
10
|
ISession,
|
|
9
11
|
SessionState,
|
|
10
12
|
ISessionRecorder,
|
|
11
13
|
SessionRecorderConfigs,
|
|
12
|
-
SessionRecorderOptions
|
|
14
|
+
SessionRecorderOptions,
|
|
15
|
+
RRWebEvent,
|
|
16
|
+
EventRecorder
|
|
13
17
|
} from './types'
|
|
14
18
|
import { getFormattedDate, isSessionActive, getNavigatorInfo } from './utils'
|
|
15
19
|
import { setMaxCapturingHttpPayloadSize, setShouldRecordHttpData } from './patch/xhr'
|
|
@@ -20,8 +24,11 @@ import { ApiService, StartSessionRequest, StopSessionRequest } from './services/
|
|
|
20
24
|
|
|
21
25
|
// Utility functions for React Native
|
|
22
26
|
|
|
27
|
+
type SessionRecorderEvents =
|
|
28
|
+
| 'state-change'
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
|
|
31
|
+
class SessionRecorder extends Observable<SessionRecorderEvents> implements ISessionRecorder, EventRecorder {
|
|
25
32
|
private _isInitialized = false
|
|
26
33
|
private _configs: SessionRecorderConfigs | null = null
|
|
27
34
|
private _apiService = new ApiService()
|
|
@@ -61,6 +68,7 @@ class SessionRecorder implements ISessionRecorder {
|
|
|
61
68
|
}
|
|
62
69
|
set sessionState(state: SessionState | null) {
|
|
63
70
|
this._sessionState = state
|
|
71
|
+
this.emit('state-change', [state || SessionState.stopped])
|
|
64
72
|
if (state) {
|
|
65
73
|
this._storageService.saveSessionState(state)
|
|
66
74
|
}
|
|
@@ -108,14 +116,15 @@ class SessionRecorder implements ISessionRecorder {
|
|
|
108
116
|
* Initialize debugger with default or custom configurations
|
|
109
117
|
*/
|
|
110
118
|
constructor() {
|
|
119
|
+
super()
|
|
111
120
|
// Initialize with stored session data if available
|
|
112
|
-
|
|
121
|
+
StorageService.initialize()
|
|
113
122
|
}
|
|
114
123
|
|
|
115
124
|
private async _loadStoredSessionData(): Promise<void> {
|
|
116
125
|
try {
|
|
126
|
+
await StorageService.initialize()
|
|
117
127
|
const storedData = await this._storageService.getAllSessionData()
|
|
118
|
-
|
|
119
128
|
if (isSessionActive(storedData.sessionObject, storedData.sessionType === SessionType.CONTINUOUS)) {
|
|
120
129
|
this.session = storedData.sessionObject
|
|
121
130
|
this.sessionId = storedData.sessionId
|
|
@@ -128,7 +137,7 @@ class SessionRecorder implements ISessionRecorder {
|
|
|
128
137
|
this.sessionType = SessionType.PLAIN
|
|
129
138
|
}
|
|
130
139
|
} catch (error) {
|
|
131
|
-
|
|
140
|
+
logger.error('SessionRecorder', 'Failed to load stored session data', error)
|
|
132
141
|
this.session = null
|
|
133
142
|
this.sessionId = null
|
|
134
143
|
this.sessionState = null
|
|
@@ -142,18 +151,20 @@ class SessionRecorder implements ISessionRecorder {
|
|
|
142
151
|
*/
|
|
143
152
|
public async init(configs: SessionRecorderOptions): Promise<void> {
|
|
144
153
|
this._configs = getSessionRecorderConfig({ ...this._configs, ...configs })
|
|
145
|
-
|
|
146
154
|
this._isInitialized = true
|
|
147
155
|
this._checkOperation('init')
|
|
148
|
-
|
|
149
|
-
await StorageService.initialize()
|
|
150
|
-
|
|
156
|
+
await this._loadStoredSessionData()
|
|
151
157
|
setMaxCapturingHttpPayloadSize(this._configs.maxCapturingHttpPayloadSize || DEFAULT_MAX_HTTP_CAPTURING_PAYLOAD_SIZE)
|
|
152
158
|
setShouldRecordHttpData(!this._configs.captureBody, this._configs.captureHeaders)
|
|
153
159
|
|
|
154
|
-
this._tracer.init(this._configs)
|
|
155
|
-
this._apiService.init(this._configs)
|
|
156
160
|
|
|
161
|
+
try {
|
|
162
|
+
this._apiService.init(this._configs)
|
|
163
|
+
this._tracer.init(this._configs)
|
|
164
|
+
|
|
165
|
+
} catch (error) {
|
|
166
|
+
logger.error('SessionRecorder', 'Failed to initialize API service', error)
|
|
167
|
+
}
|
|
157
168
|
if (this._configs.apiKey) {
|
|
158
169
|
this._recorder.init(this._configs)
|
|
159
170
|
}
|
|
@@ -510,6 +521,81 @@ class SessionRecorder implements ISessionRecorder {
|
|
|
510
521
|
this._session.updatedAt = new Date().toISOString()
|
|
511
522
|
}
|
|
512
523
|
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Record a custom rrweb event
|
|
527
|
+
* Note: Screen capture and touch events are recorded automatically when session is started
|
|
528
|
+
* @param event - The rrweb event to record
|
|
529
|
+
*/
|
|
530
|
+
recordEvent(event: RRWebEvent): void {
|
|
531
|
+
if (!this._isInitialized || this.sessionState !== SessionState.started) {
|
|
532
|
+
return
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Forward the event to the recorder SDK
|
|
536
|
+
this._recorder.recordEvent(event)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Record touch start event (internal use - touch recording is automatic)
|
|
541
|
+
* @param x - X coordinate
|
|
542
|
+
* @param y - Y coordinate
|
|
543
|
+
* @param target - Target element identifier
|
|
544
|
+
* @param pressure - Touch pressure
|
|
545
|
+
* @internal
|
|
546
|
+
*/
|
|
547
|
+
recordTouchStart(x: number, y: number, target?: string, pressure?: number): void {
|
|
548
|
+
if (!this._isInitialized || this.sessionState !== SessionState.started) {
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// Forward to gesture recorder
|
|
553
|
+
this._recorder.recordTouchStart(x, y, target, pressure)
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Record touch move event (internal use - touch recording is automatic)
|
|
558
|
+
* @param x - X coordinate
|
|
559
|
+
* @param y - Y coordinate
|
|
560
|
+
* @param target - Target element identifier
|
|
561
|
+
* @param pressure - Touch pressure
|
|
562
|
+
* @internal
|
|
563
|
+
*/
|
|
564
|
+
recordTouchMove(x: number, y: number, target?: string, pressure?: number): void {
|
|
565
|
+
if (!this._isInitialized || this.sessionState !== SessionState.started) {
|
|
566
|
+
return
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Forward to gesture recorder
|
|
570
|
+
this._recorder.recordTouchMove(x, y, target, pressure)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Record touch end event (internal use - touch recording is automatic)
|
|
575
|
+
* @param x - X coordinate
|
|
576
|
+
* @param y - Y coordinate
|
|
577
|
+
* @param target - Target element identifier
|
|
578
|
+
* @param pressure - Touch pressure
|
|
579
|
+
* @internal
|
|
580
|
+
*/
|
|
581
|
+
recordTouchEnd(x: number, y: number, target?: string, pressure?: number): void {
|
|
582
|
+
if (!this._isInitialized || this.sessionState !== SessionState.started) {
|
|
583
|
+
return
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Forward to gesture recorder
|
|
587
|
+
this._recorder.recordTouchEnd(x, y, target, pressure)
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Set the viewshot ref for screen capture
|
|
592
|
+
* @param ref - React Native View ref for screen capture
|
|
593
|
+
*/
|
|
594
|
+
setViewShotRef(ref: any): void {
|
|
595
|
+
if (this._recorder) {
|
|
596
|
+
this._recorder.setViewShotRef(ref)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
513
599
|
}
|
|
514
600
|
|
|
515
601
|
export default new SessionRecorder()
|
package/src/types/index.ts
CHANGED
|
@@ -1,2 +1,46 @@
|
|
|
1
1
|
export * from './session-recorder'
|
|
2
|
-
export * from './session'
|
|
2
|
+
export * from './session'
|
|
3
|
+
|
|
4
|
+
// Re-export rrweb types for convenience
|
|
5
|
+
export { EventType, IncrementalSource, MouseInteractions as MouseInteractionType } from 'rrweb'
|
|
6
|
+
export type { eventWithTime, serializedNodeWithId, mouseInteractionData, metaEvent } from '@rrweb/types'
|
|
7
|
+
|
|
8
|
+
// Import types for use in this file
|
|
9
|
+
import type { MouseInteractions } from 'rrweb'
|
|
10
|
+
import type { eventWithTime, mouseInteractionData, metaEvent } from '@rrweb/types'
|
|
11
|
+
|
|
12
|
+
// React Native specific types
|
|
13
|
+
export interface TouchInteractionData {
|
|
14
|
+
type: MouseInteractions
|
|
15
|
+
id: number
|
|
16
|
+
x: number
|
|
17
|
+
y: number
|
|
18
|
+
pressure?: number
|
|
19
|
+
target?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ReactNativeScreenData {
|
|
23
|
+
width: number
|
|
24
|
+
height: number
|
|
25
|
+
base64Image: string
|
|
26
|
+
timestamp: number
|
|
27
|
+
screenName?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ReactNativeTouchData {
|
|
31
|
+
pageX: number
|
|
32
|
+
pageY: number
|
|
33
|
+
target?: string
|
|
34
|
+
pressure?: number
|
|
35
|
+
timestamp: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Type aliases for convenience
|
|
39
|
+
export type RRWebEvent = eventWithTime
|
|
40
|
+
export type MouseInteractionData = mouseInteractionData
|
|
41
|
+
export type MetaEvent = metaEvent
|
|
42
|
+
|
|
43
|
+
// Event recording interface
|
|
44
|
+
export interface EventRecorder {
|
|
45
|
+
recordEvent(event: RRWebEvent): void
|
|
46
|
+
}
|