|
| 1 | +import type { Event, Integration } from '@sentry/core'; |
| 2 | +import { isExpo } from '../utils/environment'; |
| 3 | +import type { ExpoConstants } from '../utils/expoglobalobject'; |
| 4 | +import { getExpoConstants } from '../utils/expomodules'; |
| 5 | + |
| 6 | +const INTEGRATION_NAME = 'ExpoConstants'; |
| 7 | + |
| 8 | +export const EXPO_CONSTANTS_CONTEXT_KEY = 'expo_constants'; |
| 9 | + |
| 10 | +/** Load Expo Constants as event context. */ |
| 11 | +export const expoConstantsIntegration = (): Integration => { |
| 12 | + let _expoConstantsContextCached: ExpoConstantsContext | undefined; |
| 13 | + |
| 14 | + function processEvent(event: Event): Event { |
| 15 | + if (!isExpo()) { |
| 16 | + return event; |
| 17 | + } |
| 18 | + |
| 19 | + event.contexts = event.contexts || {}; |
| 20 | + event.contexts[EXPO_CONSTANTS_CONTEXT_KEY] = { |
| 21 | + ...getExpoConstantsContextCached(), |
| 22 | + }; |
| 23 | + |
| 24 | + return event; |
| 25 | + } |
| 26 | + |
| 27 | + function getExpoConstantsContextCached(): ExpoConstantsContext { |
| 28 | + if (_expoConstantsContextCached) { |
| 29 | + return _expoConstantsContextCached; |
| 30 | + } |
| 31 | + |
| 32 | + return (_expoConstantsContextCached = getExpoConstantsContext()); |
| 33 | + } |
| 34 | + |
| 35 | + return { |
| 36 | + name: INTEGRATION_NAME, |
| 37 | + processEvent, |
| 38 | + }; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * @internal Exposed for testing purposes |
| 43 | + */ |
| 44 | +export function getExpoConstantsContext(): ExpoConstantsContext { |
| 45 | + const expoConstants = getExpoConstants(); |
| 46 | + if (!expoConstants) { |
| 47 | + return {}; |
| 48 | + } |
| 49 | + |
| 50 | + const context: ExpoConstantsContext = {}; |
| 51 | + |
| 52 | + addStringField(context, 'execution_environment', expoConstants.executionEnvironment); |
| 53 | + addStringField(context, 'app_ownership', expoConstants.appOwnership); |
| 54 | + addBooleanField(context, 'debug_mode', expoConstants.debugMode); |
| 55 | + addStringField(context, 'expo_version', expoConstants.expoVersion); |
| 56 | + addStringField(context, 'expo_runtime_version', expoConstants.expoRuntimeVersion); |
| 57 | + addStringField(context, 'session_id', expoConstants.sessionId); |
| 58 | + addNumberField(context, 'status_bar_height', expoConstants.statusBarHeight); |
| 59 | + |
| 60 | + addExpoConfigFields(context, expoConstants); |
| 61 | + addEasConfigFields(context, expoConstants); |
| 62 | + |
| 63 | + return context; |
| 64 | +} |
| 65 | + |
| 66 | +function addStringField( |
| 67 | + context: ExpoConstantsContext, |
| 68 | + key: keyof ExpoConstantsContext, |
| 69 | + value: string | null | undefined, |
| 70 | +): void { |
| 71 | + if (typeof value === 'string' && value) { |
| 72 | + (context as Record<string, unknown>)[key] = value; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function addBooleanField( |
| 77 | + context: ExpoConstantsContext, |
| 78 | + key: keyof ExpoConstantsContext, |
| 79 | + value: boolean | undefined, |
| 80 | +): void { |
| 81 | + if (typeof value === 'boolean') { |
| 82 | + (context as Record<string, unknown>)[key] = value; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function addNumberField( |
| 87 | + context: ExpoConstantsContext, |
| 88 | + key: keyof ExpoConstantsContext, |
| 89 | + value: number | undefined, |
| 90 | +): void { |
| 91 | + if (typeof value === 'number') { |
| 92 | + (context as Record<string, unknown>)[key] = value; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function addExpoConfigFields(context: ExpoConstantsContext, expoConstants: ExpoConstants): void { |
| 97 | + if (!expoConstants.expoConfig) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + addStringField(context, 'app_name', expoConstants.expoConfig.name); |
| 102 | + addStringField(context, 'app_slug', expoConstants.expoConfig.slug); |
| 103 | + addStringField(context, 'app_version', expoConstants.expoConfig.version); |
| 104 | + addStringField(context, 'expo_sdk_version', expoConstants.expoConfig.sdkVersion); |
| 105 | +} |
| 106 | + |
| 107 | +function addEasConfigFields(context: ExpoConstantsContext, expoConstants: ExpoConstants): void { |
| 108 | + if (!expoConstants.easConfig) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + addStringField(context, 'eas_project_id', expoConstants.easConfig.projectId); |
| 113 | +} |
| 114 | + |
| 115 | +type ExpoConstantsContext = Partial<{ |
| 116 | + execution_environment: string; |
| 117 | + app_ownership: string; |
| 118 | + debug_mode: boolean; |
| 119 | + expo_version: string; |
| 120 | + expo_runtime_version: string; |
| 121 | + session_id: string; |
| 122 | + status_bar_height: number; |
| 123 | + app_name: string; |
| 124 | + app_slug: string; |
| 125 | + app_version: string; |
| 126 | + expo_sdk_version?: string; |
| 127 | + eas_project_id: string; |
| 128 | +}>; |
0 commit comments