-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add expoUpdatesListenerIntegration that records breadcrumbs for Expo Updates lifecycle events
#5795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alwx
wants to merge
4
commits into
main
Choose a base branch
from
alwx/feature/expo-updates-listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+448
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
packages/core/src/js/integrations/expoupdateslistener.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { addBreadcrumb, debug, type Integration, type SeverityLevel } from '@sentry/core'; | ||
| import type { ReactNativeClient } from '../client'; | ||
| import { isExpo, isExpoGo } from '../utils/environment'; | ||
|
|
||
| const INTEGRATION_NAME = 'ExpoUpdatesListener'; | ||
|
|
||
| const BREADCRUMB_CATEGORY = 'expo.updates'; | ||
|
|
||
| /** | ||
| * Describes the state machine context from `expo-updates`. | ||
| * We define our own minimal type to avoid a hard dependency on `expo-updates`. | ||
| */ | ||
| interface UpdatesNativeStateMachineContext { | ||
| isChecking: boolean; | ||
| isDownloading: boolean; | ||
| isUpdateAvailable: boolean; | ||
| isUpdatePending: boolean; | ||
| isRestarting: boolean; | ||
| latestManifest?: { id?: string }; | ||
| downloadedManifest?: { id?: string }; | ||
| rollback?: { commitTime: string }; | ||
| checkError?: Error; | ||
| downloadError?: Error; | ||
| } | ||
|
|
||
| interface UpdatesNativeStateChangeEvent { | ||
| context: UpdatesNativeStateMachineContext; | ||
| } | ||
|
|
||
| /** | ||
| * Tries to load `expo-updates` and retrieve `addUpdatesStateChangeListener`. | ||
| * Returns `undefined` if `expo-updates` is not installed. | ||
| */ | ||
| function getAddUpdatesStateChangeListener(): | ||
| | ((listener: (event: UpdatesNativeStateChangeEvent) => void) => void) | ||
| | undefined { | ||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-member-access | ||
| const addListener = require('expo-updates').addUpdatesStateChangeListener; | ||
| if (typeof addListener === 'function') { | ||
| return addListener as (listener: (event: UpdatesNativeStateChangeEvent) => void) => void; | ||
| } | ||
| } catch (_) { | ||
| // that happens when expo-updates is not installed | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| interface StateTransition { | ||
| field: keyof UpdatesNativeStateMachineContext; | ||
| message: string; | ||
| level: SeverityLevel; | ||
| getData?: (ctx: UpdatesNativeStateMachineContext) => Record<string, unknown> | undefined; | ||
| } | ||
|
|
||
| const STATE_TRANSITIONS: StateTransition[] = [ | ||
| { field: 'isChecking', message: 'Checking for update', level: 'info' }, | ||
| { | ||
| field: 'isUpdateAvailable', | ||
| message: 'Update available', | ||
| level: 'info', | ||
| getData: ctx => { | ||
| const updateId = ctx.latestManifest?.id; | ||
| return updateId ? { updateId } : undefined; | ||
| }, | ||
| }, | ||
| { field: 'isDownloading', message: 'Downloading update', level: 'info' }, | ||
| { | ||
| field: 'isUpdatePending', | ||
| message: 'Update downloaded', | ||
| level: 'info', | ||
| getData: ctx => { | ||
| const updateId = ctx.downloadedManifest?.id; | ||
| return updateId ? { updateId } : undefined; | ||
| }, | ||
| }, | ||
| { | ||
| field: 'checkError', | ||
| message: 'Update check failed', | ||
| level: 'error', | ||
| getData: ctx => ({ | ||
| error: (ctx.checkError as Error).message || String(ctx.checkError), | ||
| }), | ||
| }, | ||
| { | ||
| field: 'downloadError', | ||
| message: 'Update download failed', | ||
| level: 'error', | ||
| getData: ctx => ({ | ||
| error: (ctx.downloadError as Error).message || String(ctx.downloadError), | ||
| }), | ||
| }, | ||
| { | ||
| field: 'rollback', | ||
| message: 'Rollback directive received', | ||
| level: 'warning', | ||
| getData: ctx => ({ | ||
| commitTime: ctx.rollback!.commitTime, | ||
| }), | ||
| }, | ||
| { field: 'isRestarting', message: 'Restarting for update', level: 'info' }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Listens to Expo Updates native state machine changes and records | ||
| * breadcrumbs for meaningful transitions such as checking for updates, | ||
| * downloading updates, errors, rollbacks, and restarts. | ||
| */ | ||
| export const expoUpdatesListenerIntegration = (): Integration => { | ||
| function setup(client: ReactNativeClient): void { | ||
| client.on('afterInit', () => { | ||
| if (!isExpo() || isExpoGo()) { | ||
| return; | ||
| } | ||
|
|
||
| const addListener = getAddUpdatesStateChangeListener(); | ||
| if (!addListener) { | ||
| debug.log('[ExpoUpdatesListener] expo-updates is not available, skipping.'); | ||
| return; | ||
| } | ||
|
|
||
| let previousContext: Partial<UpdatesNativeStateMachineContext> = {}; | ||
|
|
||
| addListener((event: UpdatesNativeStateChangeEvent) => { | ||
| const ctx = event.context; | ||
| handleStateChange(previousContext, ctx); | ||
| previousContext = ctx; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Compares previous and current state machine contexts and emits | ||
| * breadcrumbs for meaningful transitions (falsy→truthy). | ||
| * | ||
| * @internal Exposed for testing purposes | ||
| */ | ||
| export function handleStateChange( | ||
| previous: Partial<UpdatesNativeStateMachineContext>, | ||
| current: UpdatesNativeStateMachineContext, | ||
| ): void { | ||
| for (const transition of STATE_TRANSITIONS) { | ||
| if (!previous[transition.field] && current[transition.field]) { | ||
| addBreadcrumb({ | ||
| category: BREADCRUMB_CATEGORY, | ||
| message: transition.message, | ||
| level: transition.level, | ||
| data: transition.getData?.(current), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Listener callback lacks try/catch, risking app crash
Medium Severity
The
addListenercallback fromexpo-updatesis not wrapped in atry/catch. Ifevent.contextis unexpectedly null/undefined, or if anygetDatafunction throws (e.g., the non-null assertionctx.rollback!.commitTimeat line 98, or theas Errorcast at lines 82/90), the exception propagates unhandled intoexpo-updatesinternals and could crash the host app. Per project rules, SDK instrumentation errors must never crash the host application — dangerous paths needtry/catchwith graceful fallback. This violates the rule from the rules file requiring error paths to be handled explicitly.Triggered by project rule: PR Review Guidelines for Cursor Bot