// // LegacyJetstreamFrame.swift // JetstreamKit // import CoreATProtocol import Foundation /// One frame of the original `/subscribe` wire, spoken by tangled's index and /// Bluesky's `jetstream1` and `jetstream2` hosts. struct LegacyJetstreamFrame: Decodable, Sendable { struct Commit: Decodable, Sendable { let rev: String let operation: JetstreamCommit.Operation let collection: String let rkey: String let record: JSONValue? let cid: String? /// Sent only by an index, as false on a historical replay. A plain /// Jetstream never sends it, so absent counts as live. let live: Bool? } let did: String let timeMicros: Int64 let kind: String let commit: Commit? private enum CodingKeys: String, CodingKey { case did case timeMicros = "time_us" case kind case commit } static func decode(_ text: String) throws -> LegacyJetstreamFrame { try JSONDecoder().decode(LegacyJetstreamFrame.self, from: Data(text.utf8)) } /// The frame as the commit a v2 host would have sent, which is the same /// commit minus the sequence number. Other kinds of frame have no /// counterpart here and are skipped. var jetstreamCommit: JetstreamCommit? { guard kind == "commit", let commit else { return nil } let time = JetstreamTime.timestamp(microseconds: timeMicros) return JetstreamCommit( seq: nil, did: did, time: time, witnessedAt: time, rev: commit.rev, operation: commit.operation, collection: commit.collection, rkey: commit.rkey, record: commit.record, cid: commit.cid ) } }