|
| 1 | +import type { DeviceContext, Event, EventProcessor, Hub, Integration, OsContext } from '@sentry/types'; |
| 2 | + |
| 3 | +import { getExpoDevice } from '../utils/expomodules'; |
| 4 | + |
| 5 | +/** Load device context from expo modules. */ |
| 6 | +export class ExpoContext implements Integration { |
| 7 | + /** |
| 8 | + * @inheritDoc |
| 9 | + */ |
| 10 | + public static id: string = 'ExpoContext'; |
| 11 | + |
| 12 | + /** |
| 13 | + * @inheritDoc |
| 14 | + */ |
| 15 | + public name: string = ExpoContext.id; |
| 16 | + |
| 17 | + /** |
| 18 | + * @inheritDoc |
| 19 | + */ |
| 20 | + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 21 | + addGlobalEventProcessor(async (event: Event) => { |
| 22 | + const self = getCurrentHub().getIntegration(ExpoContext); |
| 23 | + if (!self) { |
| 24 | + return event; |
| 25 | + } |
| 26 | + |
| 27 | + const expoDeviceContext = getExpoDeviceContext(); |
| 28 | + if (expoDeviceContext) { |
| 29 | + event.contexts = event.contexts || {}; |
| 30 | + event.contexts.device = { ...expoDeviceContext, ...event.contexts.device }; |
| 31 | + } |
| 32 | + |
| 33 | + const expoOsContext = getExpoOsContext(); |
| 34 | + if (expoOsContext) { |
| 35 | + event.contexts = event.contexts || {}; |
| 36 | + event.contexts.os = { ...expoOsContext, ...event.contexts.os }; |
| 37 | + } |
| 38 | + |
| 39 | + return event; |
| 40 | + }); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Returns the Expo Device context if present |
| 46 | + */ |
| 47 | +function getExpoDeviceContext(): DeviceContext | undefined { |
| 48 | + const expoDevice = getExpoDevice(); |
| 49 | + |
| 50 | + if (!expoDevice) { |
| 51 | + return undefined; |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + name: expoDevice.deviceName, |
| 56 | + simulator: !expoDevice?.isDevice, |
| 57 | + model: expoDevice.modelName, |
| 58 | + manufacturer: expoDevice.manufacturer, |
| 59 | + memory_size: expoDevice.totalMemory, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Returns the Expo OS context if present |
| 65 | + */ |
| 66 | +function getExpoOsContext(): OsContext | undefined { |
| 67 | + const expoDevice = getExpoDevice(); |
| 68 | + |
| 69 | + if (!expoDevice) { |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + build: expoDevice.osBuildId, |
| 75 | + version: expoDevice.osVersion, |
| 76 | + name: expoDevice.osName, |
| 77 | + }; |
| 78 | +} |
0 commit comments