diff --git a/Package.swift b/Package.swift index 0dba679..6d0d362 100644 --- a/Package.swift +++ b/Package.swift @@ -24,7 +24,7 @@ let package = Package( path: "Source", resources: [ .copy("Turbo/WebView/turbo.js"), - .copy("Bridge/strada.js") + .copy("Bridge/bridge.js") ] ), .testTarget( diff --git a/Source/Bridge/Bridge.swift b/Source/Bridge/Bridge.swift index 99e9761..b454c43 100644 --- a/Source/Bridge/Bridge.swift +++ b/Source/Bridge/Bridge.swift @@ -112,7 +112,7 @@ public final class Bridge: Bridgable { private let bridgeGlobal = "window.nativeBridge" /// The webkit.messageHandlers name - private let scriptHandlerName = "strada" + private let scriptHandlerName = "bridge" @MainActor private func callBridgeFunction(_ function: JavaScriptBridgeFunction, arguments: [Any]) async throws { @@ -137,7 +137,7 @@ public final class Bridge: Bridgable { private func makeUserScript() -> WKUserScript? { guard - let path = Bundle.module.path(forResource: "strada", ofType: "js") + let path = Bundle.module.path(forResource: "bridge", ofType: "js") else { return nil } @@ -146,7 +146,7 @@ public final class Bridge: Bridgable { let source = try String(contentsOfFile: path) return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: true) } catch { - assertionFailure("Could not open strada.js: \(error)") + assertionFailure("Could not open bridge.js: \(error)") return nil } } diff --git a/Source/Bridge/Strada.swift b/Source/Bridge/UserAgent.swift similarity index 91% rename from Source/Bridge/Strada.swift rename to Source/Bridge/UserAgent.swift index f624795..2207979 100644 --- a/Source/Bridge/Strada.swift +++ b/Source/Bridge/UserAgent.swift @@ -1,6 +1,6 @@ import Foundation -public enum Strada { +public enum UserAgent { public static func userAgentSubstring(for componentTypes: [BridgeComponent.Type]) -> String { let components = componentTypes.map { $0.name }.joined(separator: " ") return "bridge-components: [\(components)]" diff --git a/Source/Bridge/strada.js b/Source/Bridge/bridge.js similarity index 82% rename from Source/Bridge/strada.js rename to Source/Bridge/bridge.js index 58f582a..0958c78 100644 --- a/Source/Bridge/strada.js +++ b/Source/Bridge/bridge.js @@ -36,7 +36,7 @@ } notifyBridgeOfSupportedComponentsUpdate() { - if (this.isStradaAvailable) { + if (this.isWebBridgeAvailable) { this.webBridge.adapterDidUpdateSupportedComponents() } } @@ -47,7 +47,7 @@ // Reply to web with message. replyWith(message) { - if (this.isStradaAvailable) { + if (this.isWebBridgeAvailable) { this.webBridge.receive(message) } } @@ -64,15 +64,17 @@ // Native handler postMessage(message) { - webkit.messageHandlers.strada.postMessage(message) + webkit.messageHandlers.bridge.postMessage(message) } - get isStradaAvailable() { - return window.Strada + get isWebBridgeAvailable() { + // Fallback to Strada for legacy Strada web JavaScript. + return window.Bridge ?? window.Strada } get webBridge() { - return window.Strada.web + // Fallback to Strada for legacy Strada web JavaScript. + return window.Bridge?.web ?? window.Strada.web } } diff --git a/Source/Hotwire.swift b/Source/Hotwire.swift index 8fd90dc..859ac62 100644 --- a/Source/Hotwire.swift +++ b/Source/Hotwire.swift @@ -13,7 +13,7 @@ public enum Hotwire { HotwireWebViewController(url: url) } - Hotwire.config.userAgent += " \(Strada.userAgentSubstring(for: componentTypes))" + Hotwire.config.userAgent += " \(UserAgent.userAgentSubstring(for: componentTypes))" bridgeComponentTypes = componentTypes Hotwire.config.makeCustomWebView = { configuration in diff --git a/Tests/Bridge/JavaScriptTests.swift b/Tests/Bridge/JavaScriptTests.swift index 8318f14..4da2cb3 100644 --- a/Tests/Bridge/JavaScriptTests.swift +++ b/Tests/Bridge/JavaScriptTests.swift @@ -33,8 +33,8 @@ class JavaScriptTests: XCTestCase { } func testToStringWithNestedObject() throws { - let javaScript = JavaScript(object: "window.strada", functionName: "test") - XCTAssertEqual(try javaScript.toString(), "window.strada.test()") + let javaScript = JavaScript(object: "window.bridge", functionName: "test") + XCTAssertEqual(try javaScript.toString(), "window.bridge.test()") } func testToStringWithInvalidArgumentTypeThrowsError() { diff --git a/Tests/Bridge/UserAgentTests.swift b/Tests/Bridge/UserAgentTests.swift index bbe623c..c9caf9d 100644 --- a/Tests/Bridge/UserAgentTests.swift +++ b/Tests/Bridge/UserAgentTests.swift @@ -4,12 +4,12 @@ import XCTest class UserAgentTests: XCTestCase { func testUserAgentSubstringWithTwoComponents() { - let userAgentSubstring = Strada.userAgentSubstring(for: [OneBridgeComponent.self, TwoBridgeComponent.self]) + let userAgentSubstring = UserAgent.userAgentSubstring(for: [OneBridgeComponent.self, TwoBridgeComponent.self]) XCTAssertEqual(userAgentSubstring, "bridge-components: [one two]") } func testUserAgentSubstringWithNoComponents() { - let userAgentSubstring = Strada.userAgentSubstring(for: []) + let userAgentSubstring = UserAgent.userAgentSubstring(for: []) XCTAssertEqual(userAgentSubstring, "bridge-components: []") } }