Skip to content

Commit 86c077e

Browse files
committed
fix: fail early for non-extension bundle path
1 parent 594e60a commit 86c077e

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import Foundation
22

33
enum BrownfieldBundlePathResolver {
4-
static func resourceComponents(from bundlePath: String) -> (resourceName: String, fileExtension: String) {
5-
let resourceURLComponents = bundlePath.components(separatedBy: ".")
6-
let withoutLast = resourceURLComponents.dropLast()
7-
let resourceName = withoutLast.joined(separator: ".")
8-
let fileExtension = resourceURLComponents.last ?? ""
4+
enum Error: Swift.Error {
5+
case invalidBundlePath(String)
6+
}
7+
8+
static func resourceComponents(from bundlePath: String) throws -> (
9+
resourceName: String,
10+
fileExtension: String
11+
) {
12+
let fileExtension = (bundlePath as NSString).pathExtension
13+
let resourceName = (bundlePath as NSString).deletingPathExtension
14+
15+
guard !fileExtension.isEmpty, !resourceName.isEmpty else {
16+
throw Error.invalidBundlePath(bundlePath)
17+
}
18+
919
return (resourceName, fileExtension)
1020
}
1121
}

packages/react-native-brownfield/ios/ExpoHostRuntime.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ class ExpoHostRuntimeDelegate: ExpoReactNativeFactoryDelegate {
125125
return RCTBundleURLProvider.sharedSettings().jsBundleURL(
126126
forBundleRoot: entryFile)
127127
#else
128-
let (resourceName, fileExtension) = BrownfieldBundlePathResolver.resourceComponents(
129-
from: bundlePath
130-
)
131-
132-
return bundle.url(forResource: resourceName, withExtension: fileExtension)
128+
do {
129+
let (resourceName, fileExtension) = try BrownfieldBundlePathResolver.resourceComponents(
130+
from: bundlePath
131+
)
132+
return bundle.url(forResource: resourceName, withExtension: fileExtension)
133+
} catch {
134+
assertionFailure("Invalid bundlePath '\(bundlePath)': \(error)")
135+
return nil
136+
}
133137
#endif
134138
}
135139
}

packages/react-native-brownfield/ios/ReactNativeHostRuntime.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
2222
#if DEBUG
2323
return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: entryFile)
2424
#else
25-
let (resourceName, fileExtension) = BrownfieldBundlePathResolver.resourceComponents(
26-
from: bundlePath
27-
)
28-
29-
return bundle.url(forResource: resourceName, withExtension: fileExtension)
25+
do {
26+
let (resourceName, fileExtension) = try BrownfieldBundlePathResolver.resourceComponents(
27+
from: bundlePath
28+
)
29+
return bundle.url(forResource: resourceName, withExtension: fileExtension)
30+
} catch {
31+
assertionFailure("Invalid bundlePath '\(bundlePath)': \(error)")
32+
return nil
33+
}
3034
#endif
3135
}
3236
}

0 commit comments

Comments
 (0)