|
| 1 | +// |
| 2 | +// StorageJS.swift |
| 3 | +// AnalyticsLive |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 10/23/25. |
| 6 | +// |
| 7 | +import Foundation |
| 8 | +import Substrata |
| 9 | + |
| 10 | +internal class StorageJS: JSExport { |
| 11 | + static let NULL_SENTINEL = "__SENTINEL_NSDEFAULTS_NULL__" |
| 12 | + var userDefaults = UserDefaults(suiteName: "live.analytics.storage") |
| 13 | + required init() { |
| 14 | + super.init() |
| 15 | + exportMethod(named: "getValue", function: getValue) |
| 16 | + exportMethod(named: "setValue", function: setValue) |
| 17 | + exportMethod(named: "removeValue", function: removeValue) |
| 18 | + } |
| 19 | + |
| 20 | + func setValue(args: [JSConvertible?]) throws -> JSConvertible? { |
| 21 | + guard let key = args.typed(as: String.self, index: 0) else { return nil } |
| 22 | + let value: JSConvertible? = args.index(1) |
| 23 | + if isBasicType(value: value) { |
| 24 | + // Sanitize NSNull values before storing |
| 25 | + let sanitized = sanitizeForUserDefaults(value as Any) |
| 26 | + |
| 27 | + // need to do some type checking cuz some things easily translate to strings, |
| 28 | + // and we need to validate that it's something UserDefaults can actually take. |
| 29 | + if let v = sanitized as? Bool { |
| 30 | + userDefaults?.set(v, forKey: key) |
| 31 | + } else if let v = sanitized as? NSNumber { |
| 32 | + userDefaults?.set(v, forKey: key) |
| 33 | + } else if let v = sanitized as? Date { |
| 34 | + userDefaults?.set(v, forKey: key) |
| 35 | + } else if let v = sanitized as? String { |
| 36 | + userDefaults?.set(v, forKey: key) |
| 37 | + } else if let v = sanitized as? [String: Any] { |
| 38 | + userDefaults?.set(v, forKey: key) |
| 39 | + } else if let v = sanitized as? [Any] { |
| 40 | + userDefaults?.set(v, forKey: key) |
| 41 | + } |
| 42 | + // queries to userDefaults happen async, so make sure we're done before moving on. |
| 43 | + // we're already on a background thread. |
| 44 | + userDefaults?.synchronize() |
| 45 | + } |
| 46 | + return nil; // translates to Undefined in JS |
| 47 | + } |
| 48 | + |
| 49 | + func getValue(args: [JSConvertible?]) throws -> JSConvertible? { |
| 50 | + guard let key = args.typed(as: String.self, index: 0) else { return nil } |
| 51 | + guard let value = userDefaults?.value(forKey: key) else { return nil } |
| 52 | + |
| 53 | + // Desanitize NSNull sentinel values |
| 54 | + let desanitized = desanitizeFromUserDefaults(value) |
| 55 | + |
| 56 | + return convertToJSConvertible(desanitized) |
| 57 | + } |
| 58 | + |
| 59 | + func removeValue(args: [JSConvertible?]) throws -> JSConvertible? { |
| 60 | + guard let key = args.typed(as: String.self, index: 0) else { return nil } |
| 61 | + userDefaults?.removeObject(forKey: key) |
| 62 | + // queries to userDefaults happen async, so make sure we're done before moving on. |
| 63 | + // we're already on a background thread. |
| 64 | + userDefaults?.synchronize() |
| 65 | + return nil; // undefined in js |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extension StorageJS { |
| 70 | + internal func isBasicType<T>(value: T?) -> Bool { |
| 71 | + var result = false |
| 72 | + if value == nil { |
| 73 | + result = true |
| 74 | + } else { |
| 75 | + switch value { |
| 76 | + case is NSNull: |
| 77 | + fallthrough |
| 78 | + case is Array<Any>: |
| 79 | + fallthrough |
| 80 | + case is Dictionary<String, Any>: |
| 81 | + fallthrough |
| 82 | + case is Decimal: |
| 83 | + fallthrough |
| 84 | + case is NSNumber: |
| 85 | + fallthrough |
| 86 | + case is Bool: |
| 87 | + fallthrough |
| 88 | + case is Date: |
| 89 | + fallthrough |
| 90 | + case is String: |
| 91 | + result = true |
| 92 | + default: |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + return result |
| 97 | + } |
| 98 | + |
| 99 | + func convertToJSConvertible(_ value: Any) -> JSConvertible? { |
| 100 | + // Fast path - already JSConvertible |
| 101 | + if let v = value as? JSConvertible { |
| 102 | + return v |
| 103 | + } |
| 104 | + |
| 105 | + // Handle NSNull explicitly |
| 106 | + if value is NSNull { |
| 107 | + return NSNull() |
| 108 | + } |
| 109 | + |
| 110 | + // Foundation -> Swift bridging (check BEFORE Swift types) |
| 111 | + if let v = value as? NSNumber { |
| 112 | + // Check if it's actually a boolean |
| 113 | + let objCType = String(cString: v.objCType) |
| 114 | + if objCType == "c" || objCType == "B" { |
| 115 | + return v.boolValue |
| 116 | + } |
| 117 | + // Otherwise treat as number |
| 118 | + return v.doubleValue |
| 119 | + } |
| 120 | + if let v = value as? NSString { |
| 121 | + return v as String |
| 122 | + } |
| 123 | + |
| 124 | + // Direct Swift types |
| 125 | + if let v = value as? Bool { return v } |
| 126 | + if let v = value as? Int { return v } |
| 127 | + if let v = value as? Double { return v } |
| 128 | + if let v = value as? String { return v } |
| 129 | + if let v = value as? Date { return v } |
| 130 | + |
| 131 | + // Arrays - recursively convert each element |
| 132 | + if let array = value as? [Any] { |
| 133 | + let converted = array.compactMap { convertToJSConvertible($0) } |
| 134 | + return converted.isEmpty && !array.isEmpty ? nil : converted |
| 135 | + } |
| 136 | + |
| 137 | + // Dictionaries - recursively convert values |
| 138 | + if let dict = value as? [String: Any] { |
| 139 | + var converted: [String: JSConvertible] = [:] |
| 140 | + for (key, val) in dict { |
| 141 | + if let convertedVal = convertToJSConvertible(val) { |
| 142 | + converted[key] = convertedVal |
| 143 | + } |
| 144 | + } |
| 145 | + return converted.isEmpty && !dict.isEmpty ? nil : converted |
| 146 | + } |
| 147 | + |
| 148 | + return nil |
| 149 | + } |
| 150 | + |
| 151 | + func sanitizeForUserDefaults(_ value: Any) -> Any? { |
| 152 | + if value is NSNull { |
| 153 | + return StorageJS.NULL_SENTINEL |
| 154 | + } |
| 155 | + if let array = value as? [Any] { |
| 156 | + return array.map { sanitizeForUserDefaults($0) ?? StorageJS.NULL_SENTINEL } |
| 157 | + } |
| 158 | + if let dict = value as? [String: Any] { |
| 159 | + return dict.mapValues { sanitizeForUserDefaults($0) ?? StorageJS.NULL_SENTINEL } |
| 160 | + } |
| 161 | + return value |
| 162 | + } |
| 163 | + |
| 164 | + func desanitizeFromUserDefaults(_ value: Any) -> Any { |
| 165 | + if let str = value as? String, str == StorageJS.NULL_SENTINEL { |
| 166 | + return NSNull() |
| 167 | + } |
| 168 | + if let array = value as? [Any] { |
| 169 | + return array.map { desanitizeFromUserDefaults($0) } |
| 170 | + } |
| 171 | + if let dict = value as? [String: Any] { |
| 172 | + return dict.mapValues { desanitizeFromUserDefaults($0) } |
| 173 | + } |
| 174 | + return value |
| 175 | + } |
| 176 | +} |
0 commit comments