react-native-minimal-webview 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +35 -31
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +5 -11
  4. package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +17 -11
  5. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +83 -125
  6. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +16 -31
  7. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +18 -19
  8. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +24 -38
  9. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +24 -36
  10. package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +19 -20
  11. package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +41 -37
  12. package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +14 -18
  13. package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +12 -23
  14. package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +0 -1
  15. package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +1 -4
  16. package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +34 -24
  17. package/build/WebView.d.ts +11 -2
  18. package/build/WebView.d.ts.map +1 -1
  19. package/build/WebView.js +45 -71
  20. package/build/WebView.js.map +1 -1
  21. package/build/bridge/createBridge.d.ts +1 -5
  22. package/build/bridge/createBridge.d.ts.map +1 -1
  23. package/build/bridge/createBridge.js +3 -15
  24. package/build/bridge/createBridge.js.map +1 -1
  25. package/build/bridge/endpoint.d.ts +1 -2
  26. package/build/bridge/endpoint.d.ts.map +1 -1
  27. package/build/bridge/endpoint.js +5 -6
  28. package/build/bridge/endpoint.js.map +1 -1
  29. package/build/bridge/matchesOrigin.d.ts +11 -0
  30. package/build/bridge/matchesOrigin.d.ts.map +1 -0
  31. package/build/bridge/matchesOrigin.js +26 -0
  32. package/build/bridge/matchesOrigin.js.map +1 -0
  33. package/build/bridge/origins.d.ts +2 -13
  34. package/build/bridge/origins.d.ts.map +1 -1
  35. package/build/bridge/origins.js +1 -30
  36. package/build/bridge/origins.js.map +1 -1
  37. package/build/bridge/pageClient.generated.d.ts +1 -1
  38. package/build/bridge/pageClient.generated.d.ts.map +1 -1
  39. package/build/bridge/pageClient.generated.js +2 -2
  40. package/build/bridge/pageClient.generated.js.map +1 -1
  41. package/build/bridge/protocol.d.ts +3 -5
  42. package/build/bridge/protocol.d.ts.map +1 -1
  43. package/build/bridge/protocol.js +12 -14
  44. package/build/bridge/protocol.js.map +1 -1
  45. package/build/index.d.ts +1 -1
  46. package/build/index.js +1 -1
  47. package/build/navigationRules.d.ts +0 -5
  48. package/build/navigationRules.d.ts.map +1 -1
  49. package/build/navigationRules.js +3 -12
  50. package/build/navigationRules.js.map +1 -1
  51. package/build/reactNativeWebViewDefaults.d.ts +0 -4
  52. package/build/reactNativeWebViewDefaults.d.ts.map +1 -1
  53. package/build/reactNativeWebViewDefaults.js +0 -4
  54. package/build/reactNativeWebViewDefaults.js.map +1 -1
  55. package/build/reactNativeWebViewMessages.d.ts.map +1 -1
  56. package/build/reactNativeWebViewMessages.js +0 -1
  57. package/build/reactNativeWebViewMessages.js.map +1 -1
  58. package/build/refStore.d.ts +0 -2
  59. package/build/refStore.d.ts.map +1 -1
  60. package/build/refStore.js +0 -6
  61. package/build/refStore.js.map +1 -1
  62. package/build/types.d.ts +55 -39
  63. package/build/types.d.ts.map +1 -1
  64. package/ios/Asks.swift +6 -9
  65. package/ios/DelegateProxy.swift +4 -18
  66. package/ios/DownloadName.swift +0 -2
  67. package/ios/Downloads.swift +0 -2
  68. package/ios/Hooks.swift +4 -6
  69. package/ios/Loads.swift +37 -19
  70. package/ios/MinimalWebView+Dialogs.swift +5 -7
  71. package/ios/MinimalWebView+Navigation.swift +23 -49
  72. package/ios/MinimalWebView+Scripts.swift +10 -17
  73. package/ios/MinimalWebView.podspec +1 -2
  74. package/ios/MinimalWebView.swift +63 -128
  75. package/ios/MinimalWebViewModule.swift +1 -18
  76. package/ios/NavigationRules.swift +2 -9
  77. package/ios/OriginRules.swift +4 -13
  78. package/ios/PlatformSettings.swift +9 -30
  79. package/ios/ReadAccess.swift +0 -2
  80. package/ios/Records.swift +18 -0
  81. package/ios/Recovery.swift +2 -8
  82. package/ios/SharedCookies.swift +10 -25
  83. package/mocks/MinimalWebView.ts +5 -0
  84. package/package.json +11 -10
  85. package/src/WebView.tsx +58 -80
  86. package/src/bridge/createBridge.ts +5 -20
  87. package/src/bridge/endpoint.ts +6 -9
  88. package/src/bridge/matchesOrigin.ts +27 -0
  89. package/src/bridge/origins.ts +2 -30
  90. package/src/bridge/pageClient.generated.ts +2 -2
  91. package/src/bridge/pageClient.ts +1 -7
  92. package/src/bridge/protocol.ts +16 -18
  93. package/src/index.ts +1 -1
  94. package/src/navigationRules.ts +6 -12
  95. package/src/reactNativeWebViewDefaults.ts +0 -4
  96. package/src/reactNativeWebViewMessages.ts +0 -1
  97. package/src/refStore.ts +0 -7
  98. package/src/types.ts +55 -39
@@ -2,33 +2,17 @@ import ObjectiveC
2
2
  import UIKit
3
3
  import WebKit
4
4
 
5
- /// The `ios` settings prop, `{ configuration: {…}, preferences: {…}, webpagePreferences: {…}, webView: {…},
6
- /// scrollView: {…} }`, applied by Objective-C property name through Key-Value Coding, so a public property this
7
- /// package doesn't know (one a newer WebKit adds) needs no code here. `configuration` is read once, when the WKWebView
8
- /// is created; the other roots apply live, to `webView.configuration.preferences`,
9
- /// `webView.configuration.defaultWebpagePreferences`, the WKWebView and its scroll view.
10
- ///
11
- /// A value must fit the property's type (coerce); anything else is rejected before KVC runs: a wrong value in an object-typed property can take the process down
12
- /// (WebKit asserts), and an unknown key raises an Objective-C exception Swift can't catch. Underscore (private)
13
- /// properties are refused, and the only private keys accepted are the two react-native-webview sets by KVC
14
- /// (`privateBools`).
15
- ///
16
- /// `get` reads a property of a root and `call` invokes a method on one by selector, through MRWInvoke (MRWInvoke.h),
17
- /// which sets the arguments by the method's signature. For those two, `websiteDataStore` and `httpCookieStore` are
18
- /// roots as well.
19
5
  final class PlatformSettings {
20
- /// "root.key" -> the last value applied or rejected, so an unchanged value is neither applied nor reported again.
21
6
  private var applied: [String: NSObject] = [:]
22
- /// "root.key" of a live root -> its value before the first write (NSNull for nil), set back when the key is removed.
23
7
  private var originals: [String: Any] = [:]
24
8
 
25
9
  /// Applies `settings`. While creating (`configuration` given, no WebView yet) only the `configuration` root applies;
26
- /// live, every other root does (JS remounts the view for a new `configuration`), and a key applied before and missing
27
- /// now goes back to its value before the first write. Returns the rejected keys.
10
+ /// live, every other root does, and a key applied before and missing now goes back to its value before the first
11
+ /// write. Returns the rejected keys.
28
12
  func apply(_ settings: [String: Any], webView: WKWebView?, configuration: WKWebViewConfiguration? = nil) -> [[String: String]] {
29
13
  var rejected: [[String: String]] = []
30
14
  let creating = configuration != nil
31
- // Before the new values, so restoring one (a scroll view's inset) can't undo another (its offset).
15
+ // Restoring a scroll view's inset can move its offset.
32
16
  if !creating { rejected += restore(removedFrom: settings, webView: webView) }
33
17
  for (rootName, entries) in settings {
34
18
  guard let entries = entries as? [String: Any] else {
@@ -67,8 +51,6 @@ final class PlatformSettings {
67
51
  guard let original = originals.removeValue(forKey: id), let dot = id.firstIndex(of: "."),
68
52
  let object = root(String(id[..<dot]), webView: webView, configuration: nil) else { continue }
69
53
  let key = String(id[id.index(after: dot)...])
70
- // shortcut: a null_resettable property (underPageBackgroundColor) comes back as the value read, not as "follow the
71
- // default"; set nil for those if one matters.
72
54
  if let reason = __MRWCatchException({ object.setValue(original is NSNull ? nil : original, forKey: key) }) {
73
55
  failed.append(["key": id, "reason": "removed, but restoring it failed: \(reason)"])
74
56
  }
@@ -126,10 +108,8 @@ final class PlatformSettings {
126
108
  return String(decoding: data, as: UTF8.self)
127
109
  }
128
110
 
129
- /// Selector prefixes `call` refuses: what the ref and the core own (every load, navigation, script, download and
130
- /// delegate selector), private API, the plumbing that would reach either (KVC, KVO's willChange/didChange,
131
- /// performSelector), object lifecycle (init, copy, new: a re-initialized live object, or a +1 result MRWInvoke would
132
- /// leak) and reference counting. `mutable` covers `mutableCopy` and KVC's `mutableArrayValueForKey:` and its kin.
111
+ /// KVC, KVO and performSelector reach any selector; `init`, `copy`, `new` and `mutableCopy` return a +1 object
112
+ /// MRWInvoke would leak.
133
113
  private static let refused = [
134
114
  "_", "load", "go", "reload", "stop", "evaluateJavaScript", "callAsyncJavaScript", "startDownload", "resumeDownload",
135
115
  "setInteractionState", "setNavigationDelegate", "setUIDelegate", "setDelegate", "setValue", "setValuesForKeys", "valueForKey",
@@ -152,8 +132,7 @@ final class PlatformSettings {
152
132
  }
153
133
 
154
134
  /// Sets one property, or returns why not; with `original`, first keeps the value it replaces under that id unless one
155
- /// is kept already. The property's type encoding (`TB`, `TQ`, `T@"NSString"`, `T{UIEdgeInsets=dddd}`, …) decides what
156
- /// the value may be.
135
+ /// is kept already.
157
136
  private func set(_ object: NSObject, _ key: String, _ value: Any, original id: String?) -> String? {
158
137
  guard !key.hasPrefix("_"), !key.contains(".") else { return "private and nested keys are not allowed" }
159
138
  guard let attributes = Self.attributes(object, key) else { return "no such property" }
@@ -174,14 +153,14 @@ final class PlatformSettings {
174
153
  return String(cString: property_getAttributes(property)!).split(separator: ",")
175
154
  }
176
155
 
177
- /// KVC reaches these through WebKit's `_set…` accessors: WebKit declares no property by these names. They widen what
178
- /// `file:` pages read: other `file:` URLs, and any URL.
156
+ /// KVC reaches these through WebKit's `_set…` accessors: WebKit declares no property by these names.
179
157
  private static let privateBools: [String: AnyClass] = [
180
158
  "allowFileAccessFromFileURLs": WKPreferences.self,
181
159
  "allowUniversalAccessFromFileURLs": WKWebViewConfiguration.self,
182
160
  ]
183
161
 
184
- /// `value` as KVC must get it for a property of type encoding `type`; NSNull stands for nil. Nil: doesn't fit.
162
+ /// `value` as KVC must get it for a property of type encoding `type`; NSNull stands for nil. Nil: doesn't fit. A value
163
+ /// of the wrong class in an object-typed property can take the process down (WebKit asserts).
185
164
  private static func coerce(_ value: Any, _ type: String) -> Any? {
186
165
  let isBool = CFGetTypeID(value as CFTypeRef) == CFBooleanGetTypeID()
187
166
  let number = isBool ? nil : value as? NSNumber
@@ -1,6 +1,5 @@
1
1
  import Foundation
2
2
 
3
- /// The read root of a `file:` load the app starts, from `source.readAccessRoot`.
4
3
  enum ReadAccess {
5
4
  /// `root` when it is a `file:` URL that contains `file`, else `file`'s directory and a warning that says why `root`
6
5
  /// didn't apply. Always a file URL: loadFileURL raises an exception (a crash) for any other.
@@ -10,7 +9,6 @@ enum ReadAccess {
10
9
  guard let url = URL(string: root), url.isFileURL else {
11
10
  return (directory, "source.readAccessRoot \(root) is not a file: URL, so the file's directory applies")
12
11
  }
13
- // Components, not a string prefix: `..` is resolved, and /a/site2 isn't inside /a/site.
14
12
  guard file.standardizedFileURL.pathComponents.starts(with: url.standardizedFileURL.pathComponents) else {
15
13
  return (directory, "source.readAccessRoot \(root) doesn't contain \(file.absoluteString), so the file's directory applies")
16
14
  }
@@ -0,0 +1,18 @@
1
+ import ExpoModulesCore
2
+
3
+ // A prop that fails to convert is dropped whole (ExpoFabricView's try?), so every field is optional.
4
+
5
+ struct WebViewSource: Record {
6
+ @Field var uri: String? = nil
7
+ @Field var headers: [String: String]? = nil
8
+ @Field var body: String? = nil
9
+ @Field var readAccessRoot: String? = nil
10
+ @Field var html: String? = nil
11
+ @Field var baseUrl: String? = nil
12
+ }
13
+
14
+ struct InjectedScript: Record {
15
+ @Field var code: String? = nil
16
+ @Field var allFrames: Bool? = nil
17
+ }
18
+
@@ -1,10 +1,6 @@
1
1
  import Foundation
2
2
 
3
- /// Crash recovery's choices that need no WebView, as on Android (`showsSource` and `crashAllowsReload` in
4
- /// MinimalWebView.kt). Cases: src/__tests__/vectors/recovery.json.
5
3
  enum Recovery {
6
- /// Whether the committed `url` is the page of the `source` with this `uri`: the same `Loads.canonical` form up to
7
- /// trailing slashes.
8
4
  static func showsSource(_ uri: String, _ url: String) -> Bool {
9
5
  func page(_ url: String) -> String {
10
6
  (URL(string: url).map(Loads.canonical) ?? url).replacingOccurrences(of: "/+$", with: "", options: .regularExpression)
@@ -12,15 +8,13 @@ enum Recovery {
12
8
  return page(uri) == page(url)
13
9
  }
14
10
 
15
- /// Records a web content process death at `now` in `times`, and says whether to reload after it: not after more than
16
- /// `maxCrashesPerWindow` in `crashWindow`, a death exactly `crashWindow` back included. `now` is in seconds of
17
- /// `ProcessInfo.systemUptime`, which a change of device time doesn't move.
11
+ /// Records a web content process death at `now`, in seconds of `ProcessInfo.systemUptime`, in `times`, and says
12
+ /// whether to reload after it.
18
13
  static func crashAllowsReload(_ times: inout [TimeInterval], now: TimeInterval) -> Bool {
19
14
  times = times.filter { now - $0 <= crashWindow } + [now]
20
15
  return times.count <= maxCrashesPerWindow
21
16
  }
22
17
  }
23
18
 
24
- // shortcut: a fixed crash-loop guard, make it a prop if an app needs a different policy
25
19
  private let maxCrashesPerWindow = 3
26
20
  private let crashWindow: TimeInterval = 60
@@ -1,30 +1,22 @@
1
1
  import WebKit
2
2
 
3
- /// The `sharedCookies` prop. In the core, not a hook: the copy before the first `source` load is something nothing
4
- /// outside the core can hold.
5
3
  final class SharedCookies: NSObject, WKHTTPCookieStoreObserver {
6
- private weak var store: WKHTTPCookieStore?
7
- private var enabled = false
4
+ private weak var observed: WKHTTPCookieStore?
8
5
  private var copyScheduled = false
9
- /// The WebView's cookies at the last copy: one gone from it since is deleted from native too.
10
6
  private var copied: [HTTPCookie] = []
11
7
 
12
- /// Once, before the first `set`: the view's one WKWebView, which a crash keeps.
13
- func didCreate(_ webView: WKWebView) {
14
- store = webView.configuration.websiteDataStore.httpCookieStore
8
+ func set(_ enabled: Bool, for store: WKHTTPCookieStore) {
9
+ let next = enabled ? store : nil
10
+ guard next !== observed else { return }
11
+ observed?.remove(self)
12
+ next?.add(self)
13
+ observed = next
15
14
  }
16
15
 
17
- func set(_ enabled: Bool) {
18
- guard enabled != self.enabled else { return }
19
- self.enabled = enabled
20
- if enabled { store?.add(self) } else { store?.remove(self) }
21
- }
22
-
23
- /// Work that must finish before a `source` load: calls `done` once the native cookies are in the WebView.
16
+ /// Calls `done` once the native cookies are in the WebView.
24
17
  func prepare(done: @escaping () -> Void) {
25
- guard enabled, let store else { return done() }
18
+ guard let store = observed else { return done() }
26
19
  let group = DispatchGroup()
27
- // shortcut: one setCookie per cookie, use iOS 26's batch setCookies once the minimum allows
28
20
  for cookie in HTTPCookieStorage.shared.cookies ?? [] {
29
21
  group.enter()
30
22
  store.setCookie(cookie) { group.leave() }
@@ -32,27 +24,20 @@ final class SharedCookies: NSObject, WKHTTPCookieStoreObserver {
32
24
  group.notify(queue: .main, execute: done)
33
25
  }
34
26
 
35
- /// One copy per main-queue turn: `prepare`'s setCookie calls, or a page setting many cookies, each fire this.
36
27
  func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
37
28
  guard !copyScheduled else { return }
38
29
  copyScheduled = true
39
30
  DispatchQueue.main.async { [weak self] in
40
31
  guard let self else { return }
41
- // Cleared before the read: a change after it schedules the next copy.
42
32
  self.copyScheduled = false
43
- self.store?.getAllCookies { [weak self] cookies in
33
+ self.observed?.getAllCookies { [weak self] cookies in
44
34
  guard let self else { return }
45
35
  let key = { (cookie: HTTPCookie) in [cookie.name, cookie.domain, cookie.path] }
46
36
  let present = Set(cookies.map(key))
47
- // Deleted or expired in the WebView (a logout): native drops it too. Cookies only native set stay.
48
37
  self.copied.filter { !present.contains(key($0)) }.forEach(HTTPCookieStorage.shared.deleteCookie)
49
38
  cookies.forEach(HTTPCookieStorage.shared.setCookie)
50
39
  self.copied = cookies
51
40
  }
52
41
  }
53
42
  }
54
-
55
- func detach() {
56
- store?.remove(self)
57
- }
58
43
  }
@@ -0,0 +1,5 @@
1
+ // jest-expo's stand-in for the native module in an app's tests: it loads mocks/<native module name> by itself.
2
+ export const defaultUserAgent = 'Mozilla/5.0';
3
+ export function warmUp() {}
4
+ export async function preconnect(_url: string) {}
5
+ export async function setWebContentsDebuggingEnabled(_enabled: boolean) {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-minimal-webview",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "A React Native WebView that hands you the iOS and Android WebView APIs, instead of wrapping each one in a prop",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -21,16 +21,17 @@
21
21
  "android/build.gradle",
22
22
  "android/consumer-rules.pro",
23
23
  "android/src/main",
24
- "expo-module.config.json"
24
+ "expo-module.config.json",
25
+ "mocks"
25
26
  ],
26
27
  "scripts": {
27
28
  "build": "tsc && tsc -p extensions",
28
29
  "clean": "rm -rf build extensions/build",
29
- "pretest": "npm run build:page-client -- --check && npm run gen:platform-types -- --check && npm run gen:defaults -- --check",
30
+ "pretest": "node --test '.github/scripts/*.test.mts' && npm run build:page-client -- --check && npm run gen:platform-types -- --check && npm run gen:defaults -- --check",
30
31
  "test": "jest",
31
32
  "lint": "eslint --max-warnings 0 src extensions example .github && prettier --check .",
32
33
  "prepare": "npm run clean && npm run build",
33
- "check": "npm test && npm run lint && knip --config .config/knip.json && tsc --noEmit -p src/__tests__/tsconfig.json && tsc --noEmit -p extensions/src/__tests__/tsconfig.json && (cd example && npx tsc --noEmit) && node .github/scripts/check-doc-samples.mts && npm run prepare && npm run docs:check && tsc --noEmit -p .github/scripts && tsc --noEmit -p example/maestro/scripts",
34
+ "check": "npm test && npm run lint && knip --config .config/knip.json && tsc --noEmit -p src/__tests__/tsconfig.json && tsc --noEmit -p extensions/src/__tests__/tsconfig.json && (cd example && npx tsc --noEmit) && node .github/scripts/check-doc-samples.mts && node .github/scripts/check-expo-set.mts && node .github/scripts/check-cache-keys.mts && npm run prepare && npm run docs:check && tsc --noEmit -p .github/scripts && tsc --noEmit -p example/maestro/scripts",
34
35
  "mutation": "stryker run .config/stryker.config.json",
35
36
  "build:page-client": "node .github/scripts/build-page-client.mts",
36
37
  "gen:platform-types": "node .github/scripts/gen-platform-types.mts",
@@ -57,8 +58,8 @@
57
58
  "homepage": "https://github.com/RasmusKard/react-native-minimal-webview#readme",
58
59
  "devDependencies": {
59
60
  "@babel/core": "^7.29.13",
60
- "@rn-cross-platform-docs/cli": "2.0.0",
61
- "@rn-cross-platform-docs/zensical": "2.0.0",
61
+ "@rn-cross-platform-docs/cli": "2.0.2",
62
+ "@rn-cross-platform-docs/zensical": "2.0.2",
62
63
  "@stryker-mutator/core": "10.1.1",
63
64
  "@stryker-mutator/jest-runner": "10.1.1",
64
65
  "@types/jest": "^30.0.0",
@@ -67,16 +68,16 @@
67
68
  "eslint": "~9.39.4",
68
69
  "eslint-config-universe": "^16.1.2",
69
70
  "eslint-plugin-sonarjs": "4.2.2",
70
- "expo": "~58.0.25",
71
+ "expo": "~58.0.26",
71
72
  "globals": "^17.14.0",
72
73
  "jest": "^30.0.0",
73
- "jest-expo": "~58.0.14",
74
- "knip": "6.45.1",
74
+ "jest-expo": "~58.0.15",
75
+ "knip": "6.46.1",
75
76
  "prettier": "^3.10.1",
76
77
  "react": "19.3.0",
77
78
  "react-native": "0.88.1",
78
79
  "test-renderer": "1.3.1",
79
- "tsdoc-defaults": "^1.0.2",
80
+ "tsdoc-defaults": "^1.0.3",
80
81
  "typedoc": "0.28.20",
81
82
  "typedoc-plugin-markdown": "4.14.1",
82
83
  "typescript": "~6.0.3"