Skip to content

Commit

Permalink
[dev-launcher] Fix port -1 error when no port in bundle url (expo#19136)
Browse files Browse the repository at this point in the history
# Why

when load js bundle from a URL without an explicit port, e.g. http://www.example.com/ , the app will crash.
the root cause is that `android.net.Uri.getPort()` will return -1 when there's no port.

related facebook/react-native#34705 and ENG-5932

# How

use `okhttp3.HttpUrl.defaultPort(scheme)` as fallback when port is -1.

since the parameter in `injectReactInterceptor` is already android.net.Uri, i don't want to use HttpUrl to parse it again. only change the default port would be lower risk.

# Test Plan

test on bare-expo
1. [enable dev-launcher](https://github.com/expo/expo/blob/7331e13192c5b798ec67ec180949e1ef4e4da228/apps/bare-expo/android/app/src/main/java/dev/expo/payments/MainApplication.java#L25)
2. `EXPO_NO_DEFAULT_PORT=1 npx expo start --tunnel`
3. in dev-launcher, load the bundle url, e.g. `exp://nquwmbi.kudochien.19000.exp.direct`
  • Loading branch information
Kudo authored and Ddv0623 committed Sep 26, 2022
1 parent a54778d commit 55272ec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/expo-dev-launcher/CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@

- Remove the deprecated `Linking.removeEventListener` in expo-dev-launcher bundle. ([#18939](https://github.com/expo/expo/pull/18939) by [@kudo](https://github.com/kudo))
- Fixed the incompatibility with react-native-v8 on Android. ([#19117](https://github.com/expo/expo/pull/19117) by [@kudo](https://github.com/kudo))
- Fixed crash when loading bundle without explicit port on Android. ([#19136](https://github.com/expo/expo/pull/19136) by [@kudo](https://github.com/kudo))

### 💡 Others

Expand Down
Expand Up @@ -9,13 +9,15 @@ import com.facebook.react.bridge.JSBundleLoader
import expo.interfaces.devmenu.annotations.ContainsDevMenuExtension
import expo.modules.devlauncher.react.DevLauncherDevSupportManagerSwapper
import expo.modules.devlauncher.react.DevLauncherInternalSettings
import okhttp3.HttpUrl

fun injectReactInterceptor(
context: Context,
reactNativeHost: ReactNativeHost,
url: Uri
): Boolean {
val debugServerHost = url.host + ":" + url.port
val port = if (url.port != -1) url.port else HttpUrl.defaultPort(url.scheme)
val debugServerHost = url.host + ":" + port
// We need to remove "/" which is added to begin of the path by the Uri
// and the bundle type
val appBundleName = if (url.path.isNullOrEmpty()) {
Expand Down

0 comments on commit 55272ec

Please sign in to comment.