2023-11-29 11:41:18 +01:00
|
|
|
import { eventWithTime } from '@rrweb/types'
|
2023-12-15 12:47:52 +01:00
|
|
|
import { captureException, captureMessage } from '@sentry/react'
|
2023-11-29 11:41:18 +01:00
|
|
|
import Ajv, { ErrorObject } from 'ajv'
|
|
|
|
|
|
|
|
import { mobileEventWithTime } from './mobile.types'
|
|
|
|
import mobileSchema from './schema/mobile/rr-mobile-schema.json'
|
|
|
|
import webSchema from './schema/web/rr-web-schema.json'
|
2024-01-26 17:14:14 +01:00
|
|
|
import { makeCustomEvent, makeFullEvent, makeIncrementalEvent, makeMetaEvent } from './transformer/transformers'
|
2023-11-29 11:41:18 +01:00
|
|
|
|
|
|
|
const ajv = new Ajv({
|
|
|
|
allowUnionTypes: true,
|
|
|
|
}) // options can be passed, e.g. {allErrors: true}
|
|
|
|
|
|
|
|
const transformers: Record<number, (x: any) => eventWithTime> = {
|
|
|
|
2: makeFullEvent,
|
2023-12-01 13:19:36 +01:00
|
|
|
3: makeIncrementalEvent,
|
|
|
|
4: makeMetaEvent,
|
2023-12-19 15:16:37 +01:00
|
|
|
5: makeCustomEvent,
|
2023-11-29 11:41:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const mobileSchemaValidator = ajv.compile(mobileSchema)
|
|
|
|
|
|
|
|
export function validateFromMobile(data: unknown): {
|
|
|
|
isValid: boolean
|
|
|
|
errors: ErrorObject[] | null | undefined
|
|
|
|
} {
|
|
|
|
const isValid = mobileSchemaValidator(data)
|
|
|
|
return {
|
|
|
|
isValid,
|
|
|
|
errors: isValid ? null : mobileSchemaValidator.errors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const webSchemaValidator = ajv.compile(webSchema)
|
|
|
|
|
|
|
|
function couldBeEventWithTime(x: unknown): x is eventWithTime | mobileEventWithTime {
|
|
|
|
return typeof x === 'object' && x !== null && 'type' in x && 'timestamp' in x
|
|
|
|
}
|
|
|
|
|
2023-12-15 12:47:52 +01:00
|
|
|
export function transformEventToWeb(event: unknown, validateTransformation?: boolean): eventWithTime {
|
2023-11-29 19:55:13 +01:00
|
|
|
// the transformation needs to never break a recording itself
|
|
|
|
// so, we default to returning what we received
|
|
|
|
// replacing it only if there's a valid transformation
|
|
|
|
let result = event as eventWithTime
|
|
|
|
try {
|
|
|
|
if (couldBeEventWithTime(event)) {
|
|
|
|
const transformer = transformers[event.type]
|
|
|
|
if (transformer) {
|
|
|
|
const transformed = transformer(event)
|
2023-12-15 12:47:52 +01:00
|
|
|
if (validateTransformation) {
|
|
|
|
validateAgainstWebSchema(transformed)
|
|
|
|
}
|
2023-11-29 19:55:13 +01:00
|
|
|
result = transformed
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-15 12:47:52 +01:00
|
|
|
captureMessage(`No type in event`, { extra: { event } })
|
2023-11-29 19:55:13 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
captureException(e, { extra: { event } })
|
2023-11-29 11:41:18 +01:00
|
|
|
}
|
2023-11-29 19:55:13 +01:00
|
|
|
return result
|
2023-11-29 11:41:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function transformToWeb(mobileData: (eventWithTime | mobileEventWithTime)[]): eventWithTime[] {
|
|
|
|
return mobileData.reduce((acc, event) => {
|
|
|
|
const transformed = transformEventToWeb(event)
|
2023-11-29 19:55:13 +01:00
|
|
|
acc.push(transformed ? transformed : (event as eventWithTime))
|
2023-11-29 11:41:18 +01:00
|
|
|
return acc
|
|
|
|
}, [] as eventWithTime[])
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateAgainstWebSchema(data: unknown): boolean {
|
|
|
|
const validationResult = webSchemaValidator(data)
|
|
|
|
if (!validationResult) {
|
|
|
|
// we are passing all data through this validation now and don't know how safe the schema is
|
2023-12-15 12:47:52 +01:00
|
|
|
captureMessage('transformation did not match schema', {
|
2023-11-29 19:55:13 +01:00
|
|
|
extra: { data, errors: webSchemaValidator.errors },
|
|
|
|
})
|
2023-11-29 11:41:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return validationResult
|
|
|
|
}
|