Skip to content

Commit

Permalink
Adding more precise detection Expo Go
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Mar 1, 2024
1 parent 254b02c commit 77d4e67
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/realm/src/platform/react-native/expo-go-detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2024 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { Platform } from "react-native";

export function isExpoGo() {
try {
// @ts-expect-error - This depends on unstable Expo implementation details
return typeof expo === "object" && expo.modules["ExponentConstants"].executionEnvironment === "storeClient";
} catch {
return false;
}
}

export class RealmInExpoGoError extends Error {
constructor() {
const runCommand = `npx expo run:${Platform.OS}`;
super(
`'realm' was imported from the Expo Go app, but unfortunately Expo Go doesn't contain the native module for the 'realm' package - consider using an Expo development build instead:\n\nnpm install expo-dev-client\n${runCommand}\n\nRead more: https://docs.expo.dev/develop/development-builds/introduction/`,
);
}
}

if (isExpoGo()) {
throw new RealmInExpoGoError();
}
1 change: 1 addition & 0 deletions packages/realm/src/platform/react-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import "./expo-go-detection";
import "./binding";
import "./fs";
import "./device-info";
Expand Down

0 comments on commit 77d4e67

Please sign in to comment.