iOS app asks ipx in protobuf, not JSON (#469)

The badge's and CarPlay's reads (/api/feeds, the entry lists) and the
player's writes (position, flags) now go as application/x-protobuf, the
messages generated from crates/wire/proto by ios/scripts/proto.sh into
Sources/Wire, committed so the build needs no protoc. SwiftProtobuf is
pinned to the generator's version, 1.41.0, whose code refuses another.

A read whose answer is not protobuf counts as no answer: a server from
before #465 answers JSON whatever it is asked, and JSON can decode as a
message of nothing, which would have set the badge to zero.

The outbox keeps its stored key, `json`, for the body, and marks the
new writes protobuf, so places saved offline by an older build are
still sent, as JSON.

Built for the simulator with Xcode 27.2; not run against a server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ray Slakinski
2026-11-16 19:00:10 -05:00
parent a96be2f14c
commit baa6661622
13 changed files with 6118 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
import Foundation
import Network
import SwiftProtobuf
/// Writes the player made with no connection, sent when there is one (#425): where you got to in
/// an episode, and that you finished it. In a browser the page's service worker holds these; the
@@ -10,7 +11,13 @@ import Network
enum Outbox {
struct Write: Codable, Equatable {
var path: String
var json: Data
var body: Data
/// The body is protobuf (#469). Nil in a write queued by a build before it, whose body is
/// JSON, and is sent as JSON.
var protobuf: Bool?
// `json` was the body's name when it could only be JSON; kept, so those writes still read.
enum CodingKeys: String, CodingKey { case path, body = "json", protobuf }
}
private static let key = "ipx.outbox"
@@ -24,11 +31,11 @@ enum Outbox {
/// A write that never arrived. Only the player's own two kinds are kept: anything else, such
/// as adding a feed, fails offline as it always has.
static func add(path: String, json: [String: Any]) {
static func add(path: String, _ body: any SwiftProtobuf.Message) {
guard path.hasSuffix("/position") || path.hasSuffix("/flags"),
let data = try? JSONSerialization.data(withJSONObject: json, options: [.sortedKeys]) else { return }
let data = try? body.serializedData() else { return }
var all = pending.filter { $0.path != path }
all.append(Write(path: path, json: data))
all.append(Write(path: path, body: data, protobuf: true))
pending = all
}
@@ -46,9 +53,9 @@ enum Outbox {
guard let url = Server.resolveForPlayer(w.path) else { return }
var req = URLRequest(url: url)
req.httpMethod = "POST"
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.setValue(w.protobuf == true ? API.protobuf : "application/json", forHTTPHeaderField: "Content-Type")
Credentials.headers.forEach { req.setValue($1, forHTTPHeaderField: $0) }
req.httpBody = w.json
req.httpBody = w.body
guard (try? await session.data(for: req)) != nil else { return }
// Removed by what it is, not where it was: a newer write may have replaced it while
// this one was on its way.