Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: ci
on:
pull_request:
concurrency:
group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true
jobs:
swift-test:
runs-on: macos-26
steps:
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1
- run: swift test
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
e2e:
runs-on: macos-26
steps:
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1
- uses: jdx/mise-action@1648a7812b9aeae629881980618f079932869151 # 4.0.1
- run: corepack enable
- run: yarn install --immutable
working-directory: e2e
- run: yarn test
working-directory: e2e
13 changes: 9 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// swift-tools-version: 6.3
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let checksum = "<CHECKSUM>"
let tag = "<TAG>"
let checksum = "8184364fc8f2e5b624debe4023f5c219e32190fecffc49a1cb39c535b41f88fd"
let tag = "prerelease/4513cab"
let url =
"https://github.com/webview-bundle/webview-bundle/releases/download/\(tag)/WebViewBundleFFI.xcframework.zip"

Expand All @@ -21,7 +21,12 @@ let package = Package(
.binaryTarget(name: "WebViewBundleFFI", url: url, checksum: checksum),
.target(
name: "WebViewBundle",
dependencies: [.target(name: "WebViewBundleFFI")]
dependencies: [.target(name: "WebViewBundleFFI")],
linkerSettings: [
.linkedFramework("SystemConfiguration"),
.linkedFramework("Security"),
.linkedFramework("CoreFoundation"),
]
),
.testTarget(
name: "WebViewBundleTests",
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ Tags follow the upstream convention: `ffi/<version>` for releases and
`prerelease/<sha>` for prereleases. Run `node scripts/install.mjs --help` for
all options.

## TestApp & E2E

`TestApp/` is a tuist-generated SwiftUI app that serves a **real builtin `.wvb`**
to a `WKWebView` through the `testapp://` scheme, and `e2e/` drives it with
Appium.

```sh
cd e2e && yarn install && yarn e2e
```

## License

MIT License
38 changes: 38 additions & 0 deletions Sources/WebViewBundle/Conversions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

extension HttpMethod {
/// Maps a `URLRequest.httpMethod` string to the FFI ``HttpMethod``.
/// Defaults to `.get` for unknown or missing methods.
static func from(_ method: String?) -> HttpMethod {
switch method?.uppercased() {
case "GET": return .get
case "HEAD": return .head
case "OPTIONS": return .options
case "POST": return .post
case "PUT": return .put
case "PATCH": return .patch
case "DELETE": return .delete
case "TRACE": return .trace
case "CONNECT": return .connect
default: return .get
}
}
}

extension HttpResponse {
/// Builds an `HTTPURLResponse` for `url` from this response's status and
/// headers.
func makeURLResponse(url: URL) -> HTTPURLResponse {
HTTPURLResponse(
url: url,
statusCode: Int(status),
httpVersion: "HTTP/1.1",
headerFields: headers
) ?? HTTPURLResponse(
url: url,
statusCode: Int(status),
httpVersion: "HTTP/1.1",
headerFields: nil
)!
}
}
17 changes: 17 additions & 0 deletions Sources/WebViewBundle/RequestHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

/// Common shape of the UniFFI request handlers used by the WebView integration.
///
/// Both ``BundleUrlHandler`` and ``LocalUrlHandler`` (generated into this module
/// by UniFFI) already expose this exact `handle` signature, so they conform
/// without extra code.
protocol WebViewBundleRequestHandler: AnyObject, Sendable {
func handle(
method: HttpMethod,
uri: String,
headers: [String: String]?
) async throws -> HttpResponse
}

extension BundleUrlHandler: WebViewBundleRequestHandler {}
extension LocalUrlHandler: WebViewBundleRequestHandler {}
76 changes: 76 additions & 0 deletions Sources/WebViewBundle/Source.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Foundation

/// Options for building a ``BundleSource`` with sensible iOS/macOS defaults.
///
/// Every field is optional and falls back to a platform default when omitted:
/// - `builtinDir` → `<app resources>/bundles` (read-only, shipped with the app).
/// - `remoteDir` → `<Application Support>/<bundle id>/bundles` (writable, holds
/// bundles downloaded at runtime).
public struct SourceOptions: Sendable {
public var builtinDir: String?
public var remoteDir: String?
public var builtinManifestFilepath: String?
public var remoteManifestFilepath: String?

public init(
builtinDir: String? = nil,
remoteDir: String? = nil,
builtinManifestFilepath: String? = nil,
remoteManifestFilepath: String? = nil
) {
self.builtinDir = builtinDir
self.remoteDir = remoteDir
self.builtinManifestFilepath = builtinManifestFilepath
self.remoteManifestFilepath = remoteManifestFilepath
}
}

extension BundleSource {
/// Builds a ``BundleSource`` from ``SourceOptions``, filling in default
/// directories and creating the writable `remoteDir`.
public static func make(_ options: SourceOptions = SourceOptions()) throws -> BundleSource {
let builtinDir = options.builtinDir ?? defaultBuiltinDir()
let remoteDir = options.remoteDir ?? defaultRemoteDir()
try FileManager.default.createDirectory(
atPath: remoteDir,
withIntermediateDirectories: true
)
return BundleSource(config: BundleSourceConfig(
builtinDir: builtinDir,
remoteDir: remoteDir,
builtinManifestFilepath: options.builtinManifestFilepath,
remoteManifestFilepath: options.remoteManifestFilepath
))
}

/// `<app resources>/bundles` — the read-only directory shipped with the app.
public static func defaultBuiltinDir() -> String {
// `Foundation.Bundle` because unqualified `Bundle` resolves to the FFI's
// own bundle type in this module.
let base = Foundation.Bundle.main.resourceURL ?? Foundation.Bundle.main.bundleURL
return base.appendingPathComponent("bundles").path
}

/// `<Application Support>/<bundle id>/bundles` — the writable directory for
/// bundles downloaded at runtime. Falls back to caches/temporary if
/// Application Support is unavailable.
///
/// Scoped under the bundle id because macOS's Application Support is shared
/// (unlike iOS's per-app sandbox), so two apps linking this SDK would
/// otherwise clobber one `bundles` directory.
public static func defaultRemoteDir() -> String {
let fm = FileManager.default
let base = (try? fm.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)) ?? fm.urls(for: .cachesDirectory, in: .userDomainMask).first
?? fm.temporaryDirectory
let appId = Foundation.Bundle.main.bundleIdentifier ?? "WebViewBundle"
return base
.appendingPathComponent(appId)
.appendingPathComponent("bundles")
.path
}
}
Loading