Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(mobile): cover the iOS request bridge with a Swift package
Elian Doranf675b62e53d9
diff --git a/.claude/skills/developing-capacitor-mobile/SKILL.md b/.claude/skills/developing-capacitor-mobile/SKILL.mdindex 40f5d0c8db..d35663c3ee 100644--- a/.claude/skills/developing-capacitor-mobile/SKILL.md+++ b/.claude/skills/developing-capacitor-mobile/SKILL.md@@ -20,7 +20,8 @@ apps/mobile/android/app/src/main/java/org/triliumnotes/trilium/TriliumWebViewClient.java # routes requests to TriliumRequestBridge and NativeHttpProxy, builds the WebResourceResponseTriliumFileSink.java # binary file-write channel (WebMessageListener + ArrayBuffer, Android only)apps/mobile/ios/App/App/ViewController.swift # re-types the capacitor:// scheme handler; keyboard handling, --tn-keyboard-gap-apps/mobile/ios/App/App/TriliumRequestBridge.swift # TriliumAssetHandler + TriliumRequestBridge: local API requests answered by the page+apps/mobile/ios/App/App/TriliumRequestBridge.swift # WebKit adapter: TriliumAssetHandler, script message handler, WebKitSchemeTask+apps/mobile/ios/RequestBroker/ # Swift package: RequestBroker.swift (request logic, no WebKit; the App target compiles it) + its testsapps/standalone/src/main.ts # boot: native HTTP handler and native request bridge if `Capacitor` in window, else the SWapps/standalone/src/native-request-bridge.ts # page side of both TriliumRequestBridges: localFetch() → head + chunksapps/standalone/src/services/capacitor_http_handler.ts # NativeHttpHandler: Android proxy probe + CapacitorHttp fallback@@ -175,7 +176,7 @@ pnpm --filter @triliumnext/mobile open:android # Android Studiopnpm --filter @triliumnext/mobile run:ios | open:ios # Xcode (macOS)```-CI: `mobile.yml` (pull requests) builds a debug APK via `.github/actions/build-mobile` and an unsigned iOS **Simulator** `.app` on macOS; `nightly.yml` calls the same action with `nightly: "true"` for the signed `assembleRelease` build under the `.nightly` app id. The action runs the Android JVM unit tests before assembling, and `mobile.yml` uploads their JaCoCo report to Codecov under the `mobile` flag; the JS unit tests live in the standalone suite.+CI: `mobile.yml` (pull requests) builds a debug APK via `.github/actions/build-mobile` and an unsigned iOS **Simulator** `.app` on macOS; `nightly.yml` calls the same action with `nightly: "true"` for the signed `assembleRelease` build under the `.nightly` app id. The action runs the Android JVM unit tests before assembling, and `mobile.yml` uploads their JaCoCo report to Codecov under the `mobile` flag; a separate Ubuntu job runs `swift test` on `apps/mobile/ios/RequestBroker`; the JS unit tests live in the standalone suite.**Debugging on a device:** a *release*-type build (what `nightly.yml` produces) suppresses WebViewconsole→logcat forwarding, so anything logged from JS is invisible to `adb logcat` and log-based@@ -210,4 +211,13 @@ The project needs `cap sync android` (or `cap update android`) once first, for t- **Only Android-free classes are testable.** Unit tests compile against `android.jar`, whose classes (`Uri`, `Handler`, `Log`, `Base64`, `WebResourceResponse`) are stubs that throw "Method not mocked". Logic therefore lives in classes that import none of them (`LocalRequestBroker`, `NativeHttpProxy`, `InterceptedResponse`), with the WebView, the main thread, base64 and logging passed in; the `Trilium*` classes are thin adapters with no tests. Keep new native logic on the same side of that line.- **`org.json` comes from a `testImplementation` dependency**, since `android.jar`'s copy is a stub too. **`com.sun.net.httpserver` is not on the test classpath**, so `NativeHttpProxyTest` serves canned responses from a `ServerSocket` (`StubServer`).- **`minSdk` is 24**: `String.join` and `java.util.Base64` need API 26, so production code avoids them; tests can use them.-- Swift has no harness in the repo; keep logic there minimal and mirror the protocol on the JS side, where it is testable.+The iOS request logic has XCTest tests in a Swift package, `apps/mobile/ios/RequestBroker/`:++```bash+cd apps/mobile/ios/RequestBroker+swift test # macOS, or Linux with the official swift.org toolchain+```++- **The App target compiles the package's source.** `RequestBroker.swift` is referenced from `project.pbxproj` by relative path (`../../RequestBroker/Sources/RequestBroker/RequestBroker.swift`), not imported as a package, so it lands in the App module next to `TriliumRequestBridge.swift`. The package sits outside `App/` so Xcode and `cap sync` never see its `Package.swift`.+- **Same rule as Android: logic stays out of the WebKit side.** `RequestBroker` imports only Foundation (plus `FoundationNetworking` on Linux, for `HTTPURLResponse`) and talks to a `SchemeTask` protocol; script evaluation, scheduling and logging are passed in. `TriliumRequestBridge.swift` keeps only what needs WebKit and has no tests.+- **nixpkgs' Swift cannot run `swift test` as is.** It lacks `libIndexStore.so`, which Linux test discovery reads, and its manifest runner does not find `libdispatch.so`. Copy the package elsewhere, add a `Tests/LinuxMain.swift` calling `XCTMain` with a hand-written `allTests` list, and run `swift test --disable-index-store` in `nix-shell -p swift swiftpm swiftPackages.{Foundation,XCTest,Dispatch}` with `LD_LIBRARY_PATH` pointing at those packages' libraries. Do not commit a `LinuxMain.swift`: CI's toolchain discovers tests itself.diff --git a/.github/workflows/mobile.yml b/.github/workflows/mobile.ymlindex 06c613b73a..bcdb2130df 100644--- a/.github/workflows/mobile.yml+++ b/.github/workflows/mobile.yml@@ -38,6 +38,21 @@ jobs:disable_search: truefail_ci_if_error: false+ test_ios_request_broker:+ name: Test iOS request broker+ runs-on: ubuntu-latest+ steps:+ - name: Checkout the repository+ uses: actions/checkout@v7+ with:+ persist-credentials: false++ - name: Run the RequestBroker tests+ working-directory: apps/mobile/ios/RequestBroker+ run: |+ swift --version+ swift test+build_ios:name: Build iOS App# iOS builds require Xcode, which is only available on macOS runners.diff --git a/apps/mobile/ios/App/App.xcodeproj/project.pbxproj b/apps/mobile/ios/App/App.xcodeproj/project.pbxprojindex 541d5b00cf..5ca2c34174 100644--- a/apps/mobile/ios/App/App.xcodeproj/project.pbxproj+++ b/apps/mobile/ios/App/App.xcodeproj/project.pbxproj@@ -17,6 +17,7 @@50B271D11FEDC1A000F3C39B /* public in Resources */ = {isa = PBXBuildFile; fileRef = 50B271D01FEDC1A000F3C39B /* public */; };6DB328362F98030500787D66 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB328352F98030500787D66 /* ViewController.swift */; };6DB328412F98050000787D66 /* TriliumRequestBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB328402F98050000787D66 /* TriliumRequestBridge.swift */; };+ 6DB328432F98050000787D66 /* RequestBroker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB328422F98050000787D66 /* RequestBroker.swift */; };/* End PBXBuildFile section *//* Begin PBXFileReference section */@@ -31,6 +32,7 @@50B271D01FEDC1A000F3C39B /* public */ = {isa = PBXFileReference; lastKnownFileType = folder; path = public; sourceTree = "<group>"; };6DB328352F98030500787D66 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };6DB328402F98050000787D66 /* TriliumRequestBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TriliumRequestBridge.swift; sourceTree = "<group>"; };+ 6DB328422F98050000787D66 /* RequestBroker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = RequestBroker.swift; path = ../../RequestBroker/Sources/RequestBroker/RequestBroker.swift; sourceTree = "<group>"; };958DCC722DB07C7200EA8C5F /* debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = debug.xcconfig; path = ../debug.xcconfig; sourceTree = SOURCE_ROOT; };/* End PBXFileReference section */@@ -68,6 +70,7 @@children = (6DB328352F98030500787D66 /* ViewController.swift */,6DB328402F98050000787D66 /* TriliumRequestBridge.swift */,+ 6DB328422F98050000787D66 /* RequestBroker.swift */,50379B222058CBB4000EE86E /* capacitor.config.json */,504EC3071FED79650016851F /* AppDelegate.swift */,504EC30B1FED79650016851F /* Main.storyboard */,@@ -163,6 +166,7 @@files = (6DB328362F98030500787D66 /* ViewController.swift in Sources */,6DB328412F98050000787D66 /* TriliumRequestBridge.swift in Sources */,+ 6DB328432F98050000787D66 /* RequestBroker.swift in Sources */,504EC3081FED79650016851F /* AppDelegate.swift in Sources */,);runOnlyForDeploymentPostprocessing = 0;diff --git a/apps/mobile/ios/App/App/TriliumRequestBridge.swift b/apps/mobile/ios/App/App/TriliumRequestBridge.swiftindex 0476651de8..8e74dac5a9 100644--- a/apps/mobile/ios/App/App/TriliumRequestBridge.swift+++ b/apps/mobile/ios/App/App/TriliumRequestBridge.swift@@ -2,16 +2,6 @@ import Capacitorimport Foundationimport WebKit-/// Must match `isLocalApiRequest()` in apps/standalone/src/local-bridge.ts.-private let localApiPrefixes = ["/bootstrap", "/api/", "/sync/", "/search/", "/custom/"]--func isLocalApiPath(_ path: String) -> Bool {- if path == "/share" || (path.hasPrefix("/share/") && !path.hasPrefix("/share/assets/")) {- return true- }- return localApiPrefixes.contains { path.hasPrefix($0) }-}-/// Capacitor's `WebViewAssetHandler` for `capacitor://`, re-typed by/// `ViewController.webView(with:configuration:)`. It sees every request on the scheme, whichever/// part of the engine issued it (fetch, XHR, images, fonts, media, iframes), hands local API@@ -25,7 +15,7 @@ final class TriliumAssetHandler: WebViewAssetHandler {if let url = request.url, url == request.mainDocumentURL {TriliumRequestBridge.shared.pageStarted()}- if let path = request.url?.path, isLocalApiPath(path) {+ if let path = request.url?.path, RequestBroker.isLocalApiPath(path) {TriliumRequestBridge.shared.start(urlSchemeTask, in: webView)return}@@ -42,210 +32,65 @@ final class TriliumAssetHandler: WebViewAssetHandler {/// Answers local API requests from the page's SQLite worker, in place of a service worker.///-/// A request goes to the page through `window.__triliumNativeRequest()`, and the page replies-/// over the `triliumRequestBridge` script message handler; the protocol is described in-/// apps/standalone/src/native-request-bridge.ts. Bodies arrive as base64 chunks, since script-/// messages cannot carry ArrayBuffers. Requests that arrive before the page sends `ready` are-/// queued, and a new main document fails whatever the previous page left unanswered.-///-/// WebKit calls the scheme handler, the message handler and `evaluateJavaScript` completions on-/// the main thread, so all state stays there.+/// Connects `RequestBroker`, which holds the request logic, to WebKit: `evaluateJavaScript`+/// delivers requests, and the `triliumRequestBridge` script message handler carries the page's+/// replies.final class TriliumRequestBridge: NSObject, WKScriptMessageHandler {static let shared = TriliumRequestBridge()static let messageHandlerName = "triliumRequestBridge"- /// Matches the service worker's limit in sw.ts: the worker can be inside a long synchronous- /// stretch (a VACUUM of a large database) before it answers.- private static let headTimeout: TimeInterval = 270- private static let errorHeader = "x-trilium-bridge-error"-- private final class PendingRequest {- let task: WKURLSchemeTask- let script: String- var headSent = false-- init(task: WKURLSchemeTask, script: String) {- self.task = task- self.script = script- }- }-- private var pending: [Int: PendingRequest] = [:]- private var taskIds: [ObjectIdentifier: Int] = [:]- private var queuedIds: [Int] = []- private var pageReady = false- private var nextId = 0private weak var webView: WKWebView?+ private lazy var broker = RequestBroker(+ evaluate: { [weak self] script, accepted in+ guard let webView = self?.webView else {+ accepted(false)+ return+ }+ webView.evaluateJavaScript(script) { result, error in+ accepted(error == nil && (result as? Bool) == true)+ }+ },+ schedule: { delay, work in+ DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: work)+ },+ log: { message in+ NSLog("[TriliumRequestBridge] %@", message)+ })+func start(_ task: WKURLSchemeTask, in webView: WKWebView) {self.webView = webView- let request = task.request- let method = (request.httpMethod ?? "GET").uppercased()- guard method == "GET" || method == "HEAD" else {- // Android's bridge cannot see request bodies, so neither platform forwards them; the- // page sends these through standaloneApi.localFetch instead.- respondWithError(task, status: 405,- message: "\(method) cannot be answered natively: the request body is not available")- return- }-- nextId += 1- let id = nextId- let payload: [String: Any] = [- "id": id,- "method": method,- "url": request.url?.absoluteString ?? "",- "headers": request.allHTTPHeaderFields ?? [:],- "binary": false- ]- guard let data = try? JSONSerialization.data(withJSONObject: payload),- let json = String(data: data, encoding: .utf8) else {- respondWithError(task, status: 500, message: "Cannot encode the request")- return- }- // U+2028 and U+2029 are valid in JSON strings but end a line in JavaScript source.- let literal = json- .replacingOccurrences(of: "\u{2028}", with: "\\u2028")- .replacingOccurrences(of: "\u{2029}", with: "\\u2029")-- pending[id] = PendingRequest(- task: task,- script: "window.__triliumNativeRequest ? (window.__triliumNativeRequest(\(literal)), true) : false")- taskIds[ObjectIdentifier(task)] = id- if pageReady {- dispatch(id)- } else {- queuedIds.append(id)- }-- DispatchQueue.main.asyncAfter(deadline: .now() + Self.headTimeout) { [weak self] in- guard let self = self, let request = self.pending[id], !request.headSent else { return }- self.fail(id, status: 504, message: "The local worker did not answer in time")- }+ broker.start(WebKitSchemeTask(task))}- /// Returns true when the task was one of ours. A stopped task must not be completed, so it- /// leaves the pending map before WebKit returns from `stop`.+ /// Returns true when the task was one of ours.func stop(_ task: WKURLSchemeTask) -> Bool {- guard let id = taskIds.removeValue(forKey: ObjectIdentifier(task)) else {- return false- }- pending.removeValue(forKey: id)- return true+ broker.stop(id: ObjectIdentifier(task))}func pageStarted() {- pageReady = false- queuedIds.removeAll()- for id in Array(pending.keys) {- fail(id, message: "The page reloaded before answering")- }+ broker.pageStarted()}func userContentController(_ userContentController: WKUserContentController,didReceive message: WKScriptMessage) {- guard message.frameInfo.isMainFrame,- let text = message.body as? String,- let data = text.data(using: .utf8),- let body = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],- let type = body["type"] as? String else {- return- }- if type == "ready" {- onPageReady()- return- }-- guard let id = body["id"] as? Int, let request = pending[id] else { return }- switch type {- case "head":- receiveHead(id, request, status: body["status"] as? Int ?? 200,- headers: body["headers"] as? [String: String] ?? [:])- case "chunk":- if request.headSent, let encoded = body["data"] as? String,- let bytes = Data(base64Encoded: encoded) {- request.task.didReceive(bytes)- }- case "end":- if take(id) != nil, request.headSent {- request.task.didFinish()- }- case "error":- fail(id, message: body["message"] as? String ?? "The local request failed")- default:- break- }- }-- private func onPageReady() {- pageReady = true- let ids = queuedIds- queuedIds.removeAll()- for id in ids {- dispatch(id)- }- }-- private func dispatch(_ id: Int) {- guard let request = pending[id], let webView = webView else { return }- webView.evaluateJavaScript(request.script) { [weak self] result, error in- if error != nil || (result as? Bool) != true {- self?.fail(id, message: "The page has no request handler")- }- }- }-- private func receiveHead(_ id: Int, _ request: PendingRequest, status: Int, headers: [String: String]) {- // WebKit does not follow a redirect from a scheme task; the page turns redirects into a- // 200 already.- guard (100...599).contains(status), !(300...399).contains(status),- let url = request.task.request.url,- let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1",- headerFields: headers) else {- fail(id, message: "The local worker answered with status \(status)")- return- }- request.task.didReceive(response)- request.headSent = true+ guard message.frameInfo.isMainFrame, let text = message.body as? String else { return }+ broker.receive(text)}+}- /// Before the head, answers with an error response the page can read; after it, fails the- /// load.- private func fail(_ id: Int, status: Int = 502, message: String) {- guard let request = take(id) else { return }- NSLog("[TriliumRequestBridge] %@", message)- if request.headSent {- request.task.didFailWithError(NSError(- domain: "TriliumRequestBridge", code: status,- userInfo: [NSLocalizedDescriptionKey: message]))- } else {- respondWithError(request.task, status: status, message: message)- }- }+/// A `WKURLSchemeTask` as `RequestBroker` sees it.+private final class WebKitSchemeTask: SchemeTask {+ private let task: WKURLSchemeTask- private func take(_ id: Int) -> PendingRequest? {- guard let request = pending.removeValue(forKey: id) else { return nil }- taskIds.removeValue(forKey: ObjectIdentifier(request.task))- return request+ init(_ task: WKURLSchemeTask) {+ self.task = task}- private func respondWithError(_ task: WKURLSchemeTask, status: Int, message: String) {- let body = Data(message.utf8)- let headerValue = String(message.unicodeScalars.map {- (0x20...0x7e).contains($0.value) ? Character($0) : " "- })- guard let url = task.request.url,- let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: [- "Content-Type": "text/plain; charset=utf-8",- "Content-Length": String(body.count),- Self.errorHeader: headerValue- ]) else {- task.didFailWithError(NSError(domain: "TriliumRequestBridge", code: status,- userInfo: [NSLocalizedDescriptionKey: message]))- return- }- task.didReceive(response)- task.didReceive(body)- task.didFinish()- }+ var id: ObjectIdentifier { ObjectIdentifier(task) }+ var request: URLRequest { task.request }+ func didReceive(_ response: URLResponse) { task.didReceive(response) }+ func didReceive(_ data: Data) { task.didReceive(data) }+ func didFinish() { task.didFinish() }+ func didFailWithError(_ error: Error) { task.didFailWithError(error) }}diff --git a/apps/mobile/ios/RequestBroker/.gitignore b/apps/mobile/ios/RequestBroker/.gitignorenew file mode 100644index 0000000000..0ca9c3807d--- /dev/null+++ b/apps/mobile/ios/RequestBroker/.gitignore@@ -0,0 +1,3 @@+.build/+.swiftpm/+Package.resolveddiff --git a/apps/mobile/ios/RequestBroker/Package.swift b/apps/mobile/ios/RequestBroker/Package.swiftnew file mode 100644index 0000000000..152d941370--- /dev/null+++ b/apps/mobile/ios/RequestBroker/Package.swift@@ -0,0 +1,13 @@+// swift-tools-version:5.9+import PackageDescription++// Unit tests for the iOS request bridge's logic, which the App target compiles from+// Sources/RequestBroker/RequestBroker.swift. It imports no WebKit, so `swift test` also runs on+// Linux.+let package = Package(+ name: "RequestBroker",+ targets: [+ .target(name: "RequestBroker"),+ .testTarget(name: "RequestBrokerTests", dependencies: ["RequestBroker"])+ ]+)diff --git a/apps/mobile/ios/RequestBroker/Sources/RequestBroker/RequestBroker.swift b/apps/mobile/ios/RequestBroker/Sources/RequestBroker/RequestBroker.swiftnew file mode 100644index 0000000000..04ba71ac95--- /dev/null+++ b/apps/mobile/ios/RequestBroker/Sources/RequestBroker/RequestBroker.swift@@ -0,0 +1,252 @@+import Foundation+#if canImport(FoundationNetworking)+import FoundationNetworking+#endif++/// The part of a `WKURLSchemeTask` that `RequestBroker` uses, so that it runs without WebKit.+protocol SchemeTask: AnyObject {+ /// Identifies the WebKit task, which `RequestBroker.stop(id:)` receives.+ var id: ObjectIdentifier { get }+ var request: URLRequest { get }+ func didReceive(_ response: URLResponse)+ func didReceive(_ data: Data)+ func didFinish()+ func didFailWithError(_ error: Error)+}++/// Must match `isLocalApiRequest()` in apps/standalone/src/local-bridge.ts.+private let localApiPrefixes = ["/bootstrap", "/api/", "/sync/", "/search/", "/custom/"]++/// Tracks the local API requests `TriliumRequestBridge` forwards to the page, from dispatch until+/// the page's reply completes their scheme tasks. It imports no WebKit, so it runs in+/// `swift test`; the bridge supplies script evaluation, scheduling and logging.+///+/// A request goes to the page through `window.__triliumNativeRequest()`, and the page's JSON+/// replies arrive through `receive(_:)`; the protocol is described in+/// apps/standalone/src/native-request-bridge.ts. Requests that arrive before the page sends+/// `ready` are queued, and `pageStarted()` fails whatever the previous page left unanswered.+///+/// WebKit calls the scheme handler, the message handler and `evaluateJavaScript` completions on+/// the main thread, so the broker expects to be used from that thread only.+final class RequestBroker {+ /// Runs a script in the page and reports whether it returned `true`.+ typealias Evaluate = (_ script: String, _ accepted: @escaping (Bool) -> Void) -> Void+ /// Runs the work on the main thread after the delay.+ typealias Schedule = (_ delay: TimeInterval, _ work: @escaping () -> Void) -> Void++ static let errorHeader = "x-trilium-bridge-error"++ private final class PendingRequest {+ let task: SchemeTask+ let script: String+ var headSent = false++ init(task: SchemeTask, script: String) {+ self.task = task+ self.script = script+ }+ }++ private let evaluate: Evaluate+ private let schedule: Schedule+ private let log: (String) -> Void+ private let headTimeout: TimeInterval++ private var pending: [Int: PendingRequest] = [:]+ private var taskIds: [ObjectIdentifier: Int] = [:]+ private var queuedIds: [Int] = []+ private var pageReady = false+ private var nextId = 0++ /// - Parameter headTimeout: how long the page has to send the head; the default matches the+ /// service worker's limit in sw.ts, since the worker can be inside a long synchronous+ /// stretch (a VACUUM of a large database) before it answers.+ init(evaluate: @escaping Evaluate, schedule: @escaping Schedule, log: @escaping (String) -> Void,+ headTimeout: TimeInterval = 270) {+ self.evaluate = evaluate+ self.schedule = schedule+ self.log = log+ self.headTimeout = headTimeout+ }++ /// Whether the path belongs to the local worker rather than to the app's bundled files.+ static func isLocalApiPath(_ path: String) -> Bool {+ if path == "/share" || (path.hasPrefix("/share/") && !path.hasPrefix("/share/assets/")) {+ return true+ }+ return localApiPrefixes.contains { path.hasPrefix($0) }+ }++ /// U+2028 and U+2029 are valid in JSON strings but end a line in JavaScript source.+ static func escapeLineSeparators(_ json: String) -> String {+ json.replacingOccurrences(of: "\u{2028}", with: "\\u2028")+ .replacingOccurrences(of: "\u{2029}", with: "\\u2029")+ }++ func start(_ task: SchemeTask) {+ let request = task.request+ let method = (request.httpMethod ?? "GET").uppercased()+ guard method == "GET" || method == "HEAD" else {+ // Android's bridge cannot see request bodies, so neither platform forwards them; the+ // page sends these through standaloneApi.localFetch instead.+ respondWithError(task, status: 405,+ message: "\(method) cannot be answered natively: the request body is not available")+ return+ }++ nextId += 1+ let id = nextId+ let payload: [String: Any] = [+ "id": id,+ "method": method,+ "url": request.url?.absoluteString ?? "",+ "headers": request.allHTTPHeaderFields ?? [:],+ "binary": false+ ]+ guard let data = try? JSONSerialization.data(withJSONObject: payload),+ let json = String(data: data, encoding: .utf8) else {+ respondWithError(task, status: 500, message: "Cannot encode the request")+ return+ }+ let literal = Self.escapeLineSeparators(json)++ pending[id] = PendingRequest(+ task: task,+ script: "window.__triliumNativeRequest ? (window.__triliumNativeRequest(\(literal)), true) : false")+ taskIds[task.id] = id+ if pageReady {+ dispatch(id)+ } else {+ queuedIds.append(id)+ }++ schedule(headTimeout) { [weak self] in+ guard let self = self, let request = self.pending[id], !request.headSent else { return }+ self.fail(id, status: 504, message: "The local worker did not answer in time")+ }+ }++ /// Returns true when the task was one of ours. A stopped task must not be completed, so it+ /// leaves the pending map before WebKit returns from `stop`.+ func stop(id: ObjectIdentifier) -> Bool {+ guard let requestId = taskIds.removeValue(forKey: id) else {+ return false+ }+ pending.removeValue(forKey: requestId)+ return true+ }++ func pageStarted() {+ pageReady = false+ queuedIds.removeAll()+ for id in pending.keys.sorted() {+ fail(id, message: "The page reloaded before answering")+ }+ }++ /// A JSON message from the page.+ func receive(_ message: String) {+ guard let data = message.data(using: .utf8),+ let body = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],+ let type = body["type"] as? String else {+ log("Malformed message from the page")+ return+ }+ if type == "ready" {+ onPageReady()+ return+ }++ guard let id = body["id"] as? Int, let request = pending[id] else { return }+ switch type {+ case "head":+ receiveHead(id, request, status: body["status"] as? Int ?? 200,+ headers: body["headers"] as? [String: String] ?? [:])+ case "chunk":+ if request.headSent, let encoded = body["data"] as? String,+ let bytes = Data(base64Encoded: encoded) {+ request.task.didReceive(bytes)+ }+ case "end":+ if take(id) != nil, request.headSent {+ request.task.didFinish()+ }+ case "error":+ fail(id, message: body["message"] as? String ?? "The local request failed")+ default:+ log("Unknown message type: \(type)")+ }+ }++ private func onPageReady() {+ pageReady = true+ let ids = queuedIds+ queuedIds.removeAll()+ for id in ids {+ dispatch(id)+ }+ }++ private func dispatch(_ id: Int) {+ guard let request = pending[id] else { return }+ evaluate(request.script) { [weak self] accepted in+ if !accepted {+ self?.fail(id, message: "The page has no request handler")+ }+ }+ }++ private func receiveHead(_ id: Int, _ request: PendingRequest, status: Int, headers: [String: String]) {+ // WebKit does not follow a redirect from a scheme task; the page turns redirects into a+ // 200 already.+ guard (100...599).contains(status), !(300...399).contains(status),+ let url = request.task.request.url,+ let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1",+ headerFields: headers) else {+ fail(id, message: "The local worker answered with status \(status)")+ return+ }+ request.task.didReceive(response)+ request.headSent = true+ }++ /// Before the head, answers with an error response the page can read; after it, fails the+ /// load.+ private func fail(_ id: Int, status: Int = 502, message: String) {+ guard let request = take(id) else { return }+ log(message)+ if request.headSent {+ request.task.didFailWithError(NSError(+ domain: "TriliumRequestBridge", code: status,+ userInfo: [NSLocalizedDescriptionKey: message]))+ } else {+ respondWithError(request.task, status: status, message: message)+ }+ }++ private func take(_ id: Int) -> PendingRequest? {+ guard let request = pending.removeValue(forKey: id) else { return nil }+ taskIds.removeValue(forKey: request.task.id)+ return request+ }++ private func respondWithError(_ task: SchemeTask, status: Int, message: String) {+ let body = Data(message.utf8)+ let headerValue = String(message.unicodeScalars.map {+ (0x20...0x7e).contains($0.value) ? Character($0) : " "+ })+ guard let url = task.request.url,+ let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: [+ "Content-Type": "text/plain; charset=utf-8",+ "Content-Length": String(body.count),+ Self.errorHeader: headerValue+ ]) else {+ task.didFailWithError(NSError(domain: "TriliumRequestBridge", code: status,+ userInfo: [NSLocalizedDescriptionKey: message]))+ return+ }+ task.didReceive(response)+ task.didReceive(body)+ task.didFinish()+ }+}diff --git a/apps/mobile/ios/RequestBroker/Tests/RequestBrokerTests/RequestBrokerTests.swift b/apps/mobile/ios/RequestBroker/Tests/RequestBrokerTests/RequestBrokerTests.swiftnew file mode 100644index 0000000000..8894fe7c82--- /dev/null+++ b/apps/mobile/ios/RequestBroker/Tests/RequestBrokerTests/RequestBrokerTests.swift@@ -0,0 +1,243 @@+import Foundation+#if canImport(FoundationNetworking)+import FoundationNetworking+#endif+import XCTest+@testable import RequestBroker++final class RequestBrokerTests: XCTestCase {+ private var scripts: [String] = []+ private var logs: [String] = []+ private var timers: [() -> Void] = []+ private var pageAccepts = true+ private var broker: RequestBroker!++ override func setUp() {+ super.setUp()+ scripts = []+ logs = []+ timers = []+ pageAccepts = true+ broker = RequestBroker(+ evaluate: { [unowned self] script, accepted in+ scripts.append(script)+ accepted(pageAccepts)+ },+ schedule: { [unowned self] _, work in timers.append(work) },+ log: { [unowned self] message in logs.append(message) })+ }++ func testRecognizesTheWorkerPaths() {+ for path in ["/bootstrap", "/api/notes", "/sync/changed", "/search/x", "/custom/hook", "/share", "/share/abc"] {+ XCTAssertTrue(RequestBroker.isLocalApiPath(path), path)+ }+ for path in ["/share/assets/style.css", "/sharex", "/index.html", "/apix"] {+ XCTAssertFalse(RequestBroker.isLocalApiPath(path), path)+ }+ }++ func testRejectsMethodsThatCarryABody() {+ let task = FakeTask("capacitor://localhost/api/notes", method: "post")+ broker.start(task)++ let message = "POST cannot be answered natively: the request body is not available"+ XCTAssertEqual(task.events, ["response 405", "data \(message.utf8.count) bytes", "finish"])+ XCTAssertEqual(task.response?.value(forHTTPHeaderField: RequestBroker.errorHeader), message)+ XCTAssertEqual(task.bodyText, message)+ XCTAssertTrue(scripts.isEmpty)+ }++ func testQueuesUntilReadyThenStreamsTheBody() throws {+ let task = FakeTask("capacitor://localhost/api/images/x", headers: ["Range": "bytes=0-"])+ broker.start(task)+ XCTAssertTrue(scripts.isEmpty, "nothing is dispatched before the page is ready")++ broker.receive(#"{"type":"ready"}"#)+ XCTAssertEqual(scripts.count, 1)+ let script = scripts[0]+ XCTAssertTrue(script.hasPrefix("window.__triliumNativeRequest ? (window.__triliumNativeRequest({"), script)+ XCTAssertTrue(script.hasSuffix("), true) : false"), script)+ let payload = try payloadOf(script)+ XCTAssertEqual(payload["method"] as? String, "GET")+ XCTAssertEqual(payload["binary"] as? Bool, false)+ XCTAssertEqual(payload["url"] as? String, "capacitor://localhost/api/images/x")+ XCTAssertEqual((payload["headers"] as? [String: String])?["Range"], "bytes=0-")+ let id = try XCTUnwrap(payload["id"] as? Int)++ broker.receive(#"{"type":"head","id":\#(id),"status":206,"headers":{"Content-Type":"image/png"}}"#)+ broker.receive(#"{"type":"chunk","id":\#(id),"data":"\#(base64("hel"))"}"#)+ broker.receive(#"{"type":"chunk","id":\#(id),"data":"\#(base64("lo"))"}"#)+ broker.receive(#"{"type":"end","id":\#(id)}"#)++ XCTAssertEqual(task.events, ["response 206", "data 3 bytes", "data 2 bytes", "finish"])+ XCTAssertEqual(task.response?.value(forHTTPHeaderField: "Content-Type"), "image/png")+ XCTAssertEqual(task.bodyText, "hello")+ XCTAssertTrue(broker.stop(id: task.id) == false, "a finished task leaves the broker")+ }++ func testDispatchesAtOnceWhenThePageIsReady() throws {+ broker.receive(#"{"type":"ready"}"#)+ let task = FakeTask("capacitor://localhost/api/notes", method: "HEAD")+ broker.start(task)++ XCTAssertEqual(try payloadOf(XCTUnwrap(scripts.first))["method"] as? String, "HEAD")+ }++ func testAnswersAnErrorBeforeTheHeadAsABadGateway() throws {+ let (task, id) = try startReady()+ broker.receive(#"{"type":"error","id":\#(id),"message":"café closed"}"#)++ XCTAssertEqual(task.events.first, "response 502")+ XCTAssertEqual(task.events.last, "finish")+ XCTAssertEqual(task.response?.value(forHTTPHeaderField: RequestBroker.errorHeader), "caf closed")+ XCTAssertEqual(task.bodyText, "café closed")+ XCTAssertEqual(logs, ["café closed"])+ }++ func testFailsTheLoadOnAnErrorAfterTheHead() throws {+ let (task, id) = try startReady()+ broker.receive(#"{"type":"head","id":\#(id),"status":200}"#)+ broker.receive(#"{"type":"error","id":\#(id)}"#)++ XCTAssertEqual(task.events, ["response 200", "failure The local request failed"])+ }++ func testFailsARequestThePageCannotTake() {+ pageAccepts = false+ broker.receive(#"{"type":"ready"}"#)+ let task = FakeTask("capacitor://localhost/api/x")+ broker.start(task)++ XCTAssertEqual(task.events.first, "response 502")+ XCTAssertEqual(task.bodyText, "The page has no request handler")+ }++ func testFailsPendingRequestsOnReloadAndWaitsForTheNewPage() throws {+ let (first, _) = try startReady()+ broker.pageStarted()+ XCTAssertEqual(first.events.first, "response 502")+ XCTAssertEqual(first.bodyText, "The page reloaded before answering")++ let second = FakeTask("capacitor://localhost/api/b")+ broker.start(second)+ XCTAssertEqual(scripts.count, 1, "the reloaded page has not announced itself yet")++ broker.receive(#"{"type":"ready"}"#)+ let id = try XCTUnwrap(payloadOf(scripts[1])["id"] as? Int)+ broker.receive(#"{"type":"head","id":\#(id),"status":204}"#)+ broker.receive(#"{"type":"end","id":\#(id)}"#)+ XCTAssertEqual(second.events, ["response 204", "finish"])+ }++ func testTimesOutOnlyARequestStillWaitingForItsHead() throws {+ let (slow, _) = try startReady()+ let (answered, id) = try startReady()+ broker.receive(#"{"type":"head","id":\#(id),"status":200}"#)++ for timer in timers {+ timer()+ }++ XCTAssertEqual(slow.events.first, "response 504")+ XCTAssertEqual(slow.bodyText, "The local worker did not answer in time")+ XCTAssertEqual(answered.events, ["response 200"])+ }++ func testStopForgetsTheTaskSoItIsNeverCompleted() throws {+ let (task, id) = try startReady()++ XCTAssertTrue(broker.stop(id: task.id))+ XCTAssertFalse(broker.stop(id: task.id))+ broker.receive(#"{"type":"head","id":\#(id),"status":200}"#)+ broker.receive(#"{"type":"end","id":\#(id)}"#)+ broker.pageStarted()++ XCTAssertTrue(task.events.isEmpty)+ }++ func testRejectsStatusesASchemeTaskCannotCarry() throws {+ for status in [302, 99, 600] {+ let (task, id) = try startReady()+ broker.receive(#"{"type":"head","id":\#(id),"status":\#(status)}"#)++ XCTAssertEqual(task.events.first, "response 502")+ XCTAssertEqual(task.bodyText, "The local worker answered with status \(status)")+ }+ }++ func testIgnoresMessagesItCannotPlace() throws {+ let (task, id) = try startReady()+ broker.receive("not json")+ broker.receive(#"{"type":"head","id":999,"status":200}"#)+ broker.receive(#"{"type":"chunk","id":\#(id),"data":"\#(base64("early"))"}"#)+ broker.receive(#"{"type":"surprise","id":\#(id)}"#)++ XCTAssertEqual(logs, ["Malformed message from the page", "Unknown message type: surprise"])+ XCTAssertTrue(task.events.isEmpty, "a chunk before the head is dropped")+ }++ func testEscapesLineSeparatorsForTheScript() {+ XCTAssertEqual(RequestBroker.escapeLineSeparators("{\"url\":\"a\u{2028}b\u{2029}c\"}"),+ "{\"url\":\"a\\u2028b\\u2029c\"}")+ }++ // MARK: - Helpers++ /// Starts a GET on a ready page and returns its task with the id the page would answer.+ private func startReady() throws -> (FakeTask, Int) {+ broker.receive(#"{"type":"ready"}"#)+ let task = FakeTask("capacitor://localhost/api/x")+ broker.start(task)+ let id = try XCTUnwrap(payloadOf(XCTUnwrap(scripts.last))["id"] as? Int)+ return (task, id)+ }++ private func payloadOf(_ script: String) throws -> [String: Any] {+ let start = try XCTUnwrap(script.range(of: "window.__triliumNativeRequest(", options: .backwards))+ let end = try XCTUnwrap(script.range(of: "), true) : false", options: .backwards))+ let json = String(script[start.upperBound..<end.lowerBound])+ return try XCTUnwrap(JSONSerialization.jsonObject(with: Data(json.utf8)) as? [String: Any])+ }++ private func base64(_ text: String) -> String {+ Data(text.utf8).base64EncodedString()+ }+}++/// Records every call a scheme task receives, in order.+private final class FakeTask: SchemeTask {+ let request: URLRequest+ private(set) var events: [String] = []+ private(set) var response: HTTPURLResponse?+ private var body = Data()++ var id: ObjectIdentifier { ObjectIdentifier(self) }+ var bodyText: String { String(decoding: body, as: UTF8.self) }++ init(_ url: String, method: String = "GET", headers: [String: String] = [:]) {+ var request = URLRequest(url: URL(string: url) ?? URL(fileURLWithPath: "/"))+ request.httpMethod = method+ for (name, value) in headers {+ request.setValue(value, forHTTPHeaderField: name)+ }+ self.request = request+ }++ func didReceive(_ response: URLResponse) {+ self.response = response as? HTTPURLResponse+ events.append("response \(self.response?.statusCode ?? 0)")+ }++ func didReceive(_ data: Data) {+ body.append(data)+ events.append("data \(data.count) bytes")+ }++ func didFinish() {+ events.append("finish")+ }++ func didFailWithError(_ error: Error) {+ events.append("failure \(error.localizedDescription)")+ }+}