|
| 1 | +internal import AblyPlugin |
| 2 | +import CryptoKit |
| 3 | +import Foundation |
| 4 | + |
| 5 | +/// Helpers for creating a new LiveObject. |
| 6 | +/// |
| 7 | +/// These generate an object ID and the `ObjectMessage` needed to create the LiveObject. |
| 8 | +internal enum ObjectCreationHelpers { |
| 9 | + /// The metadata that `createCounter` needs in order to request that Realtime create a LiveCounter and to populate the local objects pool. |
| 10 | + internal struct CounterCreationOperation { |
| 11 | + /// The generated object ID. Needed for populating the local objects pool. |
| 12 | + /// |
| 13 | + /// We include this property separately as a non-nil value, instead of expecting the caller to fish the nullable value out of ``objectMessage``. |
| 14 | + internal var objectID: String |
| 15 | + |
| 16 | + /// The operation that should be merged into any created LiveCounter. |
| 17 | + /// |
| 18 | + /// We include this property separately as a non-nil value, instead of expecting the caller to fish the nullable value out of ``objectMessage``. |
| 19 | + internal var operation: ObjectOperation |
| 20 | + |
| 21 | + /// The ObjectMessage that must be sent in order for Realtime to create the object. |
| 22 | + internal var objectMessage: OutboundObjectMessage |
| 23 | + } |
| 24 | + |
| 25 | + /// The metadata that `createMap` needs in order to request that Realtime create a LiveMap and to populate the local objects pool. |
| 26 | + internal struct MapCreationOperation { |
| 27 | + /// The generated object ID. Needed for populating the local objects pool. |
| 28 | + /// |
| 29 | + /// We include this property separately as a non-nil value, instead of expecting the caller to fish the nullable value out of ``objectMessage``. |
| 30 | + internal var objectID: String |
| 31 | + |
| 32 | + /// The operation that should be merged into any created LiveMap. |
| 33 | + /// |
| 34 | + /// We include this property separately as a non-nil value, instead of expecting the caller to fish the nullable value out of ``objectMessage``. |
| 35 | + internal var operation: ObjectOperation |
| 36 | + |
| 37 | + /// The ObjectMessage that must be sent in order for Realtime to create the object. |
| 38 | + internal var objectMessage: OutboundObjectMessage |
| 39 | + |
| 40 | + /// The semantics that should be used for the created LiveMap. |
| 41 | + /// |
| 42 | + /// We include this property separately as a non-nil value, instead of expecting the caller to fish the nullable value out of ``objectMessage``. |
| 43 | + internal var semantics: ObjectsMapSemantics |
| 44 | + } |
| 45 | + |
| 46 | + /// Creates a `COUNTER_CREATE` `ObjectMessage` for the `RealtimeObjects.createCounter` method per RTO12f. |
| 47 | + /// |
| 48 | + /// - Parameters: |
| 49 | + /// - count: The initial count for the new LiveCounter object |
| 50 | + /// - timestamp: The timestamp to use for the generated object ID. |
| 51 | + internal static func creationOperationForLiveCounter( |
| 52 | + count: Double, |
| 53 | + timestamp: Date, |
| 54 | + ) -> CounterCreationOperation { |
| 55 | + // RTO12f2: Create initial value for the new LiveCounter |
| 56 | + let initialValue = PartialObjectOperation( |
| 57 | + counter: WireObjectsCounter(count: NSNumber(value: count)), |
| 58 | + ) |
| 59 | + |
| 60 | + // RTO12f3: Create an initial value JSON string as described in RTO13 |
| 61 | + let initialValueJSONString = createInitialValueJSONString(from: initialValue) |
| 62 | + |
| 63 | + // RTO12f4: Create a unique nonce as a random string |
| 64 | + let nonce = generateNonce() |
| 65 | + |
| 66 | + // RTO12f5: Get the current server time (using the provided timestamp) |
| 67 | + let serverTime = timestamp |
| 68 | + |
| 69 | + // RTO12f6: Create an objectId for the new LiveCounter object as described in RTO14 |
| 70 | + let objectId = createObjectID( |
| 71 | + type: "counter", |
| 72 | + initialValue: initialValueJSONString, |
| 73 | + nonce: nonce, |
| 74 | + timestamp: serverTime, |
| 75 | + ) |
| 76 | + |
| 77 | + // RTO12f7-12: Set ObjectMessage.operation fields |
| 78 | + let operation = ObjectOperation( |
| 79 | + action: .known(.counterCreate), |
| 80 | + objectId: objectId, |
| 81 | + counter: WireObjectsCounter(count: NSNumber(value: count)), |
| 82 | + nonce: nonce, |
| 83 | + initialValue: initialValueJSONString, |
| 84 | + ) |
| 85 | + |
| 86 | + // Create the OutboundObjectMessage |
| 87 | + let objectMessage = OutboundObjectMessage( |
| 88 | + operation: operation, |
| 89 | + ) |
| 90 | + |
| 91 | + return CounterCreationOperation( |
| 92 | + objectID: objectId, |
| 93 | + operation: operation, |
| 94 | + objectMessage: objectMessage, |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + /// Creates a `MAP_CREATE` `ObjectMessage` for the `RealtimeObjects.createMap` method per RTO11f. |
| 99 | + /// |
| 100 | + /// - Parameters: |
| 101 | + /// - entries: The initial entries for the new LiveMap object |
| 102 | + /// - timestamp: The timestamp to use for the generated object ID. |
| 103 | + internal static func creationOperationForLiveMap( |
| 104 | + entries: [String: InternalLiveMapValue], |
| 105 | + timestamp: Date, |
| 106 | + ) -> MapCreationOperation { |
| 107 | + // RTO11f4: Create initial value for the new LiveMap |
| 108 | + let mapEntries = entries.mapValues { liveMapValue -> ObjectsMapEntry in |
| 109 | + ObjectsMapEntry(data: liveMapValue.toObjectData) |
| 110 | + } |
| 111 | + |
| 112 | + let initialValue = PartialObjectOperation( |
| 113 | + map: ObjectsMap( |
| 114 | + semantics: .known(.lww), |
| 115 | + entries: mapEntries, |
| 116 | + ), |
| 117 | + ) |
| 118 | + |
| 119 | + // RTO11f5: Create an initial value JSON string as described in RTO13 |
| 120 | + let initialValueJSONString = createInitialValueJSONString(from: initialValue) |
| 121 | + |
| 122 | + // RTO11f6: Create a unique nonce as a random string |
| 123 | + let nonce = generateNonce() |
| 124 | + |
| 125 | + // RTO11f7: Get the current server time (using the provided timestamp) |
| 126 | + let serverTime = timestamp |
| 127 | + |
| 128 | + // RTO11f8: Create an objectId for the new LiveMap object as described in RTO14 |
| 129 | + let objectId = createObjectID( |
| 130 | + type: "map", |
| 131 | + initialValue: initialValueJSONString, |
| 132 | + nonce: nonce, |
| 133 | + timestamp: serverTime, |
| 134 | + ) |
| 135 | + |
| 136 | + // RTO11f9-13: Set ObjectMessage.operation fields |
| 137 | + let semantics = ObjectsMapSemantics.lww |
| 138 | + let operation = ObjectOperation( |
| 139 | + action: .known(.mapCreate), |
| 140 | + objectId: objectId, |
| 141 | + map: ObjectsMap( |
| 142 | + semantics: .known(semantics), |
| 143 | + entries: mapEntries, |
| 144 | + ), |
| 145 | + nonce: nonce, |
| 146 | + initialValue: initialValueJSONString, |
| 147 | + ) |
| 148 | + |
| 149 | + // Create the OutboundObjectMessage |
| 150 | + let objectMessage = OutboundObjectMessage( |
| 151 | + operation: operation, |
| 152 | + ) |
| 153 | + |
| 154 | + return MapCreationOperation( |
| 155 | + objectID: objectId, |
| 156 | + operation: operation, |
| 157 | + objectMessage: objectMessage, |
| 158 | + semantics: semantics, |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + // MARK: - Private Helper Methods |
| 163 | + |
| 164 | + /// Creates an initial value JSON string from a PartialObjectOperation, per RTO13. |
| 165 | + private static func createInitialValueJSONString(from initialValue: PartialObjectOperation) -> String { |
| 166 | + // RTO13b: Encode the initial value using OM4 encoding |
| 167 | + let partialWireObjectOperation = initialValue.toWire(format: .json) |
| 168 | + let jsonObject = partialWireObjectOperation.toWireObject.mapValues { wireValue in |
| 169 | + do { |
| 170 | + return try wireValue.toJSONValue |
| 171 | + } catch { |
| 172 | + // By using `format: .json` we've requested a type that should be JSON-encodable, so if it isn't then it's a programmer error. (We can't reason about it statically though because of our choice to use a general-purpose WireValue type; maybe could improve upon this in the future.) |
| 173 | + preconditionFailure("Failed to convert WireValue \(wireValue) to JSONValue when encoding initialValue") |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // RTO13c |
| 178 | + return JSONObjectOrArray.object(jsonObject).toJSONString |
| 179 | + } |
| 180 | + |
| 181 | + /// Creates an Object ID for a new LiveObject instance, per RTO14. |
| 182 | + internal static func testsOnly_createObjectID( |
| 183 | + type: String, |
| 184 | + initialValue: String, |
| 185 | + nonce: String, |
| 186 | + timestamp: Date, |
| 187 | + ) -> String { |
| 188 | + createObjectID( |
| 189 | + type: type, |
| 190 | + initialValue: initialValue, |
| 191 | + nonce: nonce, |
| 192 | + timestamp: timestamp, |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + /// Creates an Object ID for a new LiveObject instance, per RTO14. |
| 197 | + private static func createObjectID( |
| 198 | + type: String, |
| 199 | + initialValue: String, |
| 200 | + nonce: String, |
| 201 | + timestamp: Date, |
| 202 | + ) -> String { |
| 203 | + // RTO14b1: Generate a SHA-256 digest |
| 204 | + let hash = SHA256.hash(data: Data("\(initialValue):\(nonce)".utf8)) |
| 205 | + |
| 206 | + // RTO14b2: Base64URL-encode the generated digest |
| 207 | + let base64URLHash = Data(hash).base64EncodedString() |
| 208 | + .replacingOccurrences(of: "+", with: "-") |
| 209 | + .replacingOccurrences(of: "/", with: "_") |
| 210 | + .replacingOccurrences(of: "=", with: "") |
| 211 | + |
| 212 | + // RTO14c: Return an Object ID in the format [type]:[hash]@[timestamp] |
| 213 | + let timestampMillis = Int(timestamp.timeIntervalSince1970 * 1000) |
| 214 | + return "\(type):\(base64URLHash)@\(timestampMillis)" |
| 215 | + } |
| 216 | + |
| 217 | + /// Generates a unique nonce as a random string, per RTO11f6 and RTO12f4. |
| 218 | + private static func generateNonce() -> String { |
| 219 | + // TODO: confirm if there's any specific rules here: https://github.com/ably/specification/pull/353/files#r2228252389 |
| 220 | + let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 221 | + return String((0 ..< 16).map { _ in letters.randomElement()! }) |
| 222 | + } |
| 223 | +} |
0 commit comments