Skip to content

Commit

Permalink
fix(web): TurboModuleRegistry should not be imported in web apps (#3610)
Browse files Browse the repository at this point in the history
* fix(web): `TurboModuleRegistry` should not be imported in web applications

* fix lint

* fix ts3.8 tests

* Update CHANGELOG.md

* fix lint
  • Loading branch information
krystofwoldrich authored Mar 5, 2024
1 parent 9d18666 commit d19819c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- expo-upload-sourcemaps now works on Windows ([#3643](https://github.com/getsentry/sentry-react-native/pull/3643))
- Option `enabled: false` ensures no events are sent ([#3606](https://github.com/getsentry/sentry-react-native/pull/3606))
- Ignore JSON response when retrieving source context from local Expo Dev Server ([#3611](https://github.com/getsentry/sentry-react-native/pull/3611))
- Reduce waning messages spam when a property in Expo plugin configuration is missing ([#3631](https://github.com/getsentry/sentry-react-native/pull/3631))
- `TurboModuleRegistry` should not be imported in web applications ([#3610](https://github.com/getsentry/sentry-react-native/pull/3610))

### Dependencies

Expand Down
2 changes: 2 additions & 0 deletions src/js/utils/rnlibraries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { TurboModuleRegistry } from 'react-native';
import { version as RNV } from 'react-native/Libraries/Core/ReactNativeVersion';

import type * as ReactNative from '../vendor/react-native';
Expand Down Expand Up @@ -33,4 +34,5 @@ export const ReactNativeLibraries: Required<ReactNativeLibrariesInterface> = {
ReactNativeVersion: {
version: RNV,
},
TurboModuleRegistry,
};
1 change: 1 addition & 0 deletions src/js/utils/rnlibrariesinterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export interface ReactNativeLibrariesInterface {
ReactNativeVersion?: {
version: typeof RNV;
};
TurboModuleRegistry?: ReactNative.TurboModuleRegistry;
}
11 changes: 11 additions & 0 deletions src/js/vendor/react-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ export type DevServerInfo = {
fullBundleUrl?: string;
bundleLoadedFromServer: boolean;
};

// Adapted from https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/TurboModule/RCTExport.d.ts#L10
type TurboModule = {
getConstants?(): object;
};

// Adapted from https://github.com/facebook/react-native/blob/d09c02f9e2d468e4d0bde51890e312ae7003a3e6/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.d.ts#L12
export type TurboModuleRegistry = {
get<T extends TurboModule>(name: string): T | null;
getEnforcing<T extends TurboModule>(name: string): T;
};
5 changes: 3 additions & 2 deletions src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
User,
} from '@sentry/types';
import { logger, normalize, SentryError } from '@sentry/utils';
import { NativeModules, Platform, TurboModuleRegistry } from 'react-native';
import { NativeModules, Platform } from 'react-native';

import { isHardCrash } from './misc';
import type {
Expand All @@ -27,10 +27,11 @@ import type * as Hermes from './profiling/hermes';
import type { NativeAndroidProfileEvent, NativeProfileEvent } from './profiling/nativeTypes';
import type { RequiredKeysUser } from './user';
import { isTurboModuleEnabled } from './utils/environment';
import { ReactNativeLibraries } from './utils/rnlibraries';
import { base64StringFromByteArray, utf8ToBytes } from './vendor';

const RNSentry: Spec | undefined = isTurboModuleEnabled()
? TurboModuleRegistry.get<Spec>('RNSentry')
? ReactNativeLibraries.TurboModuleRegistry && ReactNativeLibraries.TurboModuleRegistry.get<Spec>('RNSentry')
: NativeModules.RNSentry;

export interface Screenshot {
Expand Down

0 comments on commit d19819c

Please sign in to comment.