@app-swiss-knife/react-native-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -0
- package/SwissKnifePurchases.podspec +40 -0
- package/android/build.gradle +89 -0
- package/android/gradle.properties +3 -0
- package/android/src/main/AndroidManifest.xml +26 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +216 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingPackage.kt +17 -0
- package/android/src/main/java/com/swissknife/purchases/Conversions.kt +15 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +371 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +497 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt +25 -0
- package/android/vendor/GENERATED.md +10 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +533 -0
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +326 -0
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +296 -0
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +503 -0
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +188 -0
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +204 -0
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +447 -0
- package/android/vendor/java/com/appswissknife/context/AppLifecycle.kt +120 -0
- package/android/vendor/java/com/appswissknife/context/Clock.kt +70 -0
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +138 -0
- package/android/vendor/java/com/appswissknife/context/IDs.kt +55 -0
- package/android/vendor/java/com/appswissknife/context/Reachability.kt +145 -0
- package/android/vendor/java/com/appswissknife/context/Session.kt +192 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +945 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKScreenshot.kt +201 -0
- package/android/vendor/java/com/appswissknife/device/DeviceID.kt +172 -0
- package/android/vendor/java/com/appswissknife/diagnostics/SKLog.kt +97 -0
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +495 -0
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +522 -0
- package/android/vendor/java/com/appswissknife/models/CustomerInfo.kt +67 -0
- package/android/vendor/java/com/appswissknife/models/Enums.kt +35 -0
- package/android/vendor/java/com/appswissknife/models/Offerings.kt +80 -0
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +89 -0
- package/android/vendor/java/com/appswissknife/models/StoreTransaction.kt +17 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +507 -0
- package/android/vendor/java/com/appswissknife/net/ExponentialBackoff.kt +69 -0
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +582 -0
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +320 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +219 -0
- package/android/vendor/java/com/appswissknife/performance/SKFrameTracker.kt +94 -0
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +428 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +560 -0
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +100 -0
- package/android/vendor/java/com/appswissknife/purchases/PriceEnrichmentProvider.kt +36 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +120 -0
- package/android/vendor/res/xml/swissknife_data_extraction_rules.xml +20 -0
- package/ios/SwissKnifeCrashReporting.m +32 -0
- package/ios/SwissKnifeCrashReporting.swift +186 -0
- package/ios/SwissKnifeJSON.swift +347 -0
- package/ios/SwissKnifePurchases.m +124 -0
- package/ios/SwissKnifePurchases.swift +523 -0
- package/ios/vendor/GENERATED.md +10 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +488 -0
- package/ios/vendor/SwissKnifeiOS/AttributesClient.swift +460 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +307 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionNetwork.swift +92 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionState.swift +141 -0
- package/ios/vendor/SwissKnifeiOS/Context/AppLifecycle.swift +130 -0
- package/ios/vendor/SwissKnifeiOS/Context/Clock.swift +100 -0
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +190 -0
- package/ios/vendor/SwissKnifeiOS/Context/IDs.swift +64 -0
- package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +121 -0
- package/ios/vendor/SwissKnifeiOS/Context/Session.swift +223 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +1287 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKScreenshot.swift +175 -0
- package/ios/vendor/SwissKnifeiOS/DeviceID.swift +161 -0
- package/ios/vendor/SwissKnifeiOS/Diagnostics/SKLog.swift +128 -0
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +323 -0
- package/ios/vendor/SwissKnifeiOS/Identity/IdentityNetwork.swift +70 -0
- package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +329 -0
- package/ios/vendor/SwissKnifeiOS/Net/ExponentialBackoff.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +681 -0
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +409 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKFrameTracker.swift +89 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +440 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKURLSessionDelegate.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +646 -0
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +458 -0
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +374 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Analytics.swift +46 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +30 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Session.swift +47 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +417 -0
- package/ios/vendor/SwissKnifeiOS/Types.swift +496 -0
- package/lib/commonjs/attribution.js +55 -0
- package/lib/commonjs/attribution.js.map +1 -0
- package/lib/commonjs/identity.js +126 -0
- package/lib/commonjs/identity.js.map +1 -0
- package/lib/commonjs/index.js +118 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +646 -0
- package/lib/commonjs/modules/analytics.js.map +1 -0
- package/lib/commonjs/modules/attachments.js +169 -0
- package/lib/commonjs/modules/attachments.js.map +1 -0
- package/lib/commonjs/modules/crash-reporting.js +1327 -0
- package/lib/commonjs/modules/crash-reporting.js.map +1 -0
- package/lib/commonjs/modules/ids.js +50 -0
- package/lib/commonjs/modules/ids.js.map +1 -0
- package/lib/commonjs/modules/log.js +59 -0
- package/lib/commonjs/modules/log.js.map +1 -0
- package/lib/commonjs/modules/offline-store.js +44 -0
- package/lib/commonjs/modules/offline-store.js.map +1 -0
- package/lib/commonjs/modules/performance.js +494 -0
- package/lib/commonjs/modules/performance.js.map +1 -0
- package/lib/commonjs/native/module.js +41 -0
- package/lib/commonjs/native/module.js.map +1 -0
- package/lib/commonjs/native/spec.js +26 -0
- package/lib/commonjs/native/spec.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/purchases.js +194 -0
- package/lib/commonjs/purchases.js.map +1 -0
- package/lib/commonjs/types.js +236 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/attribution.js +51 -0
- package/lib/module/attribution.js.map +1 -0
- package/lib/module/identity.js +122 -0
- package/lib/module/identity.js.map +1 -0
- package/lib/module/index.js +36 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/modules/analytics.js +642 -0
- package/lib/module/modules/analytics.js.map +1 -0
- package/lib/module/modules/attachments.js +162 -0
- package/lib/module/modules/attachments.js.map +1 -0
- package/lib/module/modules/crash-reporting.js +1320 -0
- package/lib/module/modules/crash-reporting.js.map +1 -0
- package/lib/module/modules/ids.js +44 -0
- package/lib/module/modules/ids.js.map +1 -0
- package/lib/module/modules/log.js +54 -0
- package/lib/module/modules/log.js.map +1 -0
- package/lib/module/modules/offline-store.js +39 -0
- package/lib/module/modules/offline-store.js.map +1 -0
- package/lib/module/modules/performance.js +491 -0
- package/lib/module/modules/performance.js.map +1 -0
- package/lib/module/native/module.js +34 -0
- package/lib/module/native/module.js.map +1 -0
- package/lib/module/native/spec.js +22 -0
- package/lib/module/native/spec.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/purchases.js +187 -0
- package/lib/module/purchases.js.map +1 -0
- package/lib/module/types.js +230 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/commonjs/attribution.d.ts +24 -0
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -0
- package/lib/typescript/commonjs/identity.d.ts +37 -0
- package/lib/typescript/commonjs/identity.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +15 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +127 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts +50 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/ids.d.ts +17 -0
- package/lib/typescript/commonjs/modules/ids.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/log.d.ts +12 -0
- package/lib/typescript/commonjs/modules/log.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts +9 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +75 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/module.d.ts +6 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/spec.d.ts +119 -0
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/purchases.d.ts +68 -0
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -0
- package/lib/typescript/commonjs/types.d.ts +384 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -0
- package/lib/typescript/module/attribution.d.ts +24 -0
- package/lib/typescript/module/attribution.d.ts.map +1 -0
- package/lib/typescript/module/identity.d.ts +37 -0
- package/lib/typescript/module/identity.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +15 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +127 -0
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/module/modules/attachments.d.ts +50 -0
- package/lib/typescript/module/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/module/modules/ids.d.ts +17 -0
- package/lib/typescript/module/modules/ids.d.ts.map +1 -0
- package/lib/typescript/module/modules/log.d.ts +12 -0
- package/lib/typescript/module/modules/log.d.ts.map +1 -0
- package/lib/typescript/module/modules/offline-store.d.ts +9 -0
- package/lib/typescript/module/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +75 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -0
- package/lib/typescript/module/native/module.d.ts +6 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -0
- package/lib/typescript/module/native/spec.d.ts +119 -0
- package/lib/typescript/module/native/spec.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/purchases.d.ts +68 -0
- package/lib/typescript/module/purchases.d.ts.map +1 -0
- package/lib/typescript/module/types.d.ts +384 -0
- package/lib/typescript/module/types.d.ts.map +1 -0
- package/package.json +115 -0
- package/react-native.config.js +23 -0
- package/src/attribution.ts +56 -0
- package/src/identity.ts +133 -0
- package/src/index.ts +54 -0
- package/src/modules/analytics.ts +689 -0
- package/src/modules/attachments.ts +222 -0
- package/src/modules/crash-reporting.ts +1577 -0
- package/src/modules/ids.ts +41 -0
- package/src/modules/log.ts +56 -0
- package/src/modules/offline-store.ts +51 -0
- package/src/modules/performance.ts +589 -0
- package/src/native/module.ts +47 -0
- package/src/native/spec.ts +183 -0
- package/src/purchases.ts +214 -0
- package/src/types.ts +470 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Net/OfflineQueueStore.swift
|
|
2
|
+
//
|
|
3
|
+
// Wave 3 unified Net layer — generic append-only JSONL disk-backed queue.
|
|
4
|
+
// Generalized from the legacy `Analytics/EventQueueStore` (Round E deleted
|
|
5
|
+
// the wrapper; this generic is now the only on-disk-queue implementation).
|
|
6
|
+
// Any feature (analytics, crashes, transactions, paywall events, ...) can
|
|
7
|
+
// persist its outgoing items across crashes, force-quits, and background
|
|
8
|
+
// terminations through this.
|
|
9
|
+
//
|
|
10
|
+
// STORAGE STRATEGY — `cachesDirectory/SwissKnife/<name>/`:
|
|
11
|
+
// - `cachesDirectory` is excluded from iCloud backups by default and not
|
|
12
|
+
// exposed via Files.app. iOS may purge it under disk pressure, which
|
|
13
|
+
// for outgoing queues is acceptable: dropping a batch is preferable to
|
|
14
|
+
// filling the user's disk. The backend dedupes per-item via
|
|
15
|
+
// `event_id`, so any double-delivery on a corner case is harmless.
|
|
16
|
+
// - Each queue gets its own subdirectory keyed by `name`, so a future
|
|
17
|
+
// "crash queue" doesn't conflict with the "analytics queue".
|
|
18
|
+
//
|
|
19
|
+
// ENCODING — append-only JSONL:
|
|
20
|
+
// - One self-describing JSON object per line. No outer array, no
|
|
21
|
+
// trailing-comma headaches, no need to rewrite the whole file on
|
|
22
|
+
// every append. Crash-mid-write means the last line may be partial;
|
|
23
|
+
// `readAll()` tolerates and skips any non-parseable line, and rewrites
|
|
24
|
+
// the file with only the well-formed entries.
|
|
25
|
+
//
|
|
26
|
+
// CRASH SAFETY:
|
|
27
|
+
// - `append()` opens for append, writes, `synchronize()`s, then closes.
|
|
28
|
+
// - `removeAll(matching:)` writes a filtered copy to a `.tmp` sibling
|
|
29
|
+
// and atomically renames over the original — POSIX rename guarantees
|
|
30
|
+
// readers see the old file or the new file, never partial.
|
|
31
|
+
//
|
|
32
|
+
// THREAD SAFETY:
|
|
33
|
+
// - All public methods dispatch onto a serial queue. Disk I/O is slow
|
|
34
|
+
// enough that a serial queue is the right tradeoff over a
|
|
35
|
+
// reader/writer lock; we never need concurrent reads on these queues.
|
|
36
|
+
|
|
37
|
+
import Foundation
|
|
38
|
+
import os.log
|
|
39
|
+
|
|
40
|
+
/// Defaults shared across all generic instantiations of OfflineQueueStore.
|
|
41
|
+
/// Swift forbids `static let` on generic classes, so the constants live
|
|
42
|
+
/// here as enum-namespaced globals instead of `OfflineQueueStore.Type`
|
|
43
|
+
/// statics. Tests reach for these via `OfflineQueueStoreDefaults.*`.
|
|
44
|
+
internal enum OfflineQueueStoreDefaults {
|
|
45
|
+
/// Default hard ceiling on number of items. Overflow drops oldest.
|
|
46
|
+
static let maxItems: Int = 10_000
|
|
47
|
+
/// Default hard ceiling on bytes on disk. Overflow drops oldest.
|
|
48
|
+
static let maxBytes: Int64 = 10 * 1024 * 1024 // 10 MB
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Generic disk-backed FIFO queue of `Codable` items. Used by IngestClient
|
|
52
|
+
/// for the unified event stream, and by any feature that needs durable
|
|
53
|
+
/// persistence of outgoing items across process death.
|
|
54
|
+
///
|
|
55
|
+
/// `T` must be `Codable` AND `Identifiable` with `String` IDs — the ID
|
|
56
|
+
/// drives both idempotency (server-side dedup keyed off the ID) and
|
|
57
|
+
/// removal-after-flush (caller passes back the set of IDs that succeeded).
|
|
58
|
+
internal final class OfflineQueueStore<T: Codable & Identifiable> where T.ID == String {
|
|
59
|
+
|
|
60
|
+
private let queue: DispatchQueue
|
|
61
|
+
private let fileURL: URL
|
|
62
|
+
private let deadLetterURL: URL
|
|
63
|
+
private let maxItems: Int
|
|
64
|
+
private let maxBytes: Int64
|
|
65
|
+
private let log: OSLog
|
|
66
|
+
private let encoder: JSONEncoder
|
|
67
|
+
private let decoder: JSONDecoder
|
|
68
|
+
|
|
69
|
+
/// When `true` the store never touches disk — all items live in
|
|
70
|
+
/// `memoryItems` for the process lifetime and are lost on termination.
|
|
71
|
+
/// This is the last-ditch fallback used when no writable directory can
|
|
72
|
+
/// be obtained (sandboxed simulator, disk pressure). Data loss is
|
|
73
|
+
/// acceptable for outgoing queues; a startup crash is not — see the
|
|
74
|
+
/// `IngestClient.shared` rationale.
|
|
75
|
+
private let inMemory: Bool
|
|
76
|
+
/// Backing store for `inMemory` mode. Guarded by `queue` like everything
|
|
77
|
+
/// else, so plain mutation under `queue.sync` is safe.
|
|
78
|
+
private var memoryItems: [T] = []
|
|
79
|
+
private var memoryDeadLetter: [T] = []
|
|
80
|
+
|
|
81
|
+
/// Production initializer — `cachesDirectory/SwissKnife/<name>/items.jsonl`.
|
|
82
|
+
convenience init(name: String) throws {
|
|
83
|
+
let caches = try FileManager.default.url(
|
|
84
|
+
for: .cachesDirectory,
|
|
85
|
+
in: .userDomainMask,
|
|
86
|
+
appropriateFor: nil,
|
|
87
|
+
create: true
|
|
88
|
+
)
|
|
89
|
+
let dir = caches
|
|
90
|
+
.appendingPathComponent("SwissKnife", isDirectory: true)
|
|
91
|
+
.appendingPathComponent(name, isDirectory: true)
|
|
92
|
+
try Self.ensureDirectory(dir)
|
|
93
|
+
try self.init(directory: dir, name: name)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Non-throwing in-memory store. Never touches disk and therefore can
|
|
97
|
+
/// never fail to initialize — the guaranteed-safe fallback for callers
|
|
98
|
+
/// (e.g. `IngestClient.shared`) that must not crash at startup.
|
|
99
|
+
init(inMemoryName name: String) {
|
|
100
|
+
self.inMemory = true
|
|
101
|
+
// Disk URLs are unused in in-memory mode but must be non-optional;
|
|
102
|
+
// point them at a tmp path that is never read or written.
|
|
103
|
+
let placeholder = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
104
|
+
.appendingPathComponent("SwissKnife-inmem-\(name)", isDirectory: true)
|
|
105
|
+
self.fileURL = placeholder.appendingPathComponent("items.jsonl", isDirectory: false)
|
|
106
|
+
self.deadLetterURL = placeholder.appendingPathComponent("dead-letter.jsonl", isDirectory: false)
|
|
107
|
+
self.maxItems = OfflineQueueStoreDefaults.maxItems
|
|
108
|
+
self.maxBytes = OfflineQueueStoreDefaults.maxBytes
|
|
109
|
+
self.queue = DispatchQueue(label: "com.swissknife.queue.\(name)", qos: .utility)
|
|
110
|
+
self.log = OSLog(subsystem: "com.swissknife.net", category: "OfflineQueueStore.\(name).inmem")
|
|
111
|
+
self.encoder = JSONEncoder()
|
|
112
|
+
self.decoder = JSONDecoder()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// Designated initializer — tests pass a temp dir.
|
|
116
|
+
init(
|
|
117
|
+
directory: URL,
|
|
118
|
+
name: String = "default",
|
|
119
|
+
maxItems: Int = OfflineQueueStoreDefaults.maxItems,
|
|
120
|
+
maxBytes: Int64 = OfflineQueueStoreDefaults.maxBytes,
|
|
121
|
+
encoder: JSONEncoder = JSONEncoder(),
|
|
122
|
+
decoder: JSONDecoder = JSONDecoder()
|
|
123
|
+
) throws {
|
|
124
|
+
self.inMemory = false
|
|
125
|
+
try Self.ensureDirectory(directory)
|
|
126
|
+
self.fileURL = directory.appendingPathComponent("items.jsonl", isDirectory: false)
|
|
127
|
+
self.deadLetterURL = directory.appendingPathComponent("dead-letter.jsonl", isDirectory: false)
|
|
128
|
+
self.maxItems = maxItems
|
|
129
|
+
self.maxBytes = maxBytes
|
|
130
|
+
self.queue = DispatchQueue(label: "com.swissknife.queue.\(name)", qos: .utility)
|
|
131
|
+
self.log = OSLog(subsystem: "com.swissknife.net", category: "OfflineQueueStore.\(name)")
|
|
132
|
+
self.encoder = encoder
|
|
133
|
+
self.decoder = decoder
|
|
134
|
+
if !FileManager.default.fileExists(atPath: fileURL.path) {
|
|
135
|
+
FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// MARK: - Public API
|
|
140
|
+
|
|
141
|
+
/// Append one item. Synchronous — when this returns the item is on
|
|
142
|
+
/// disk (`synchronize()`'d). Throws on I/O failure; callers usually
|
|
143
|
+
/// swallow because the enqueue contract is fire-and-forget.
|
|
144
|
+
func append(_ item: T) throws {
|
|
145
|
+
try queue.sync {
|
|
146
|
+
if inMemory {
|
|
147
|
+
memoryItems.append(item)
|
|
148
|
+
enforceLimitsInMemoryLocked()
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
try appendLocked(item)
|
|
152
|
+
try enforceLimitsLocked()
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Append a batch. Cheaper than calling `append` N times because the
|
|
157
|
+
/// limit-enforcement walk only fires once at the end.
|
|
158
|
+
func appendAll(_ items: [T]) throws {
|
|
159
|
+
try queue.sync {
|
|
160
|
+
if inMemory {
|
|
161
|
+
memoryItems.append(contentsOf: items)
|
|
162
|
+
enforceLimitsInMemoryLocked()
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
for item in items { try appendLocked(item) }
|
|
166
|
+
try enforceLimitsLocked()
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// Read all currently-persisted items, oldest first. Silently skips
|
|
171
|
+
/// any line that fails to decode and rewrites the file to drop the
|
|
172
|
+
/// bad line. Better to lose one item than wedge the queue.
|
|
173
|
+
func readAll() throws -> [T] {
|
|
174
|
+
try queue.sync {
|
|
175
|
+
if inMemory { return memoryItems }
|
|
176
|
+
return try readAllLocked()
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Remove items whose IDs are in `ids`. Atomic via `.tmp + rename`.
|
|
181
|
+
func remove(ids: Set<String>) throws {
|
|
182
|
+
try queue.sync {
|
|
183
|
+
if inMemory {
|
|
184
|
+
memoryItems.removeAll { ids.contains($0.id) }
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
let remaining = try readAllLocked().filter { !ids.contains($0.id) }
|
|
188
|
+
try writeAllLocked(remaining)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// Number of items currently persisted.
|
|
193
|
+
func count() throws -> Int {
|
|
194
|
+
try queue.sync {
|
|
195
|
+
if inMemory { return memoryItems.count }
|
|
196
|
+
return try readAllLocked().count
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/// Byte count of the items file. Used by limit-enforcement and tests.
|
|
201
|
+
func size() throws -> Int64 {
|
|
202
|
+
try queue.sync {
|
|
203
|
+
if inMemory { return 0 }
|
|
204
|
+
return try fileSizeLocked()
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// Move items into the dead-letter file. Best-effort: any IO failure
|
|
209
|
+
/// is logged and swallowed because the live queue must keep moving
|
|
210
|
+
/// even if dead-letter persistence fails. Caller is still responsible
|
|
211
|
+
/// for `remove(ids:)` on the live queue afterwards.
|
|
212
|
+
func deadLetter(_ items: [T]) {
|
|
213
|
+
queue.sync {
|
|
214
|
+
if inMemory {
|
|
215
|
+
memoryDeadLetter.append(contentsOf: items)
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
do {
|
|
219
|
+
try appendToDeadLetterLocked(items)
|
|
220
|
+
} catch {
|
|
221
|
+
os_log(
|
|
222
|
+
"Failed to write %d items to dead-letter file: %{public}@",
|
|
223
|
+
log: log,
|
|
224
|
+
type: .error,
|
|
225
|
+
items.count,
|
|
226
|
+
String(describing: error)
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/// Read dead-lettered items. Used by tests and by potential future
|
|
233
|
+
/// recovery flows (e.g. re-queue from dead-letter after a backend
|
|
234
|
+
/// schema fix).
|
|
235
|
+
func readDeadLetter() throws -> [T] {
|
|
236
|
+
try queue.sync {
|
|
237
|
+
if inMemory { return memoryDeadLetter }
|
|
238
|
+
guard FileManager.default.fileExists(atPath: deadLetterURL.path) else { return [] }
|
|
239
|
+
let data = try Data(contentsOf: deadLetterURL)
|
|
240
|
+
return try parseJSONL(data)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// MARK: - Locked helpers (must hold `queue`)
|
|
245
|
+
|
|
246
|
+
private func appendLocked(_ item: T) throws {
|
|
247
|
+
let json = try encoder.encode(item)
|
|
248
|
+
var line = Data()
|
|
249
|
+
line.append(json)
|
|
250
|
+
line.append(0x0A) // newline
|
|
251
|
+
let handle = try FileHandle(forWritingTo: fileURL)
|
|
252
|
+
defer { try? handle.close() }
|
|
253
|
+
try handle.seekToEnd()
|
|
254
|
+
try handle.write(contentsOf: line)
|
|
255
|
+
try handle.synchronize()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private func readAllLocked() throws -> [T] {
|
|
259
|
+
guard FileManager.default.fileExists(atPath: fileURL.path) else { return [] }
|
|
260
|
+
let data = try Data(contentsOf: fileURL)
|
|
261
|
+
guard !data.isEmpty else { return [] }
|
|
262
|
+
let (items, sawMalformed) = parseJSONLTolerant(data)
|
|
263
|
+
if sawMalformed {
|
|
264
|
+
// Rewrite to drop the bad line(s) permanently.
|
|
265
|
+
try writeAllLocked(items)
|
|
266
|
+
}
|
|
267
|
+
return items
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private func writeAllLocked(_ items: [T]) throws {
|
|
271
|
+
let tmp = fileURL.appendingPathExtension("tmp")
|
|
272
|
+
if FileManager.default.fileExists(atPath: tmp.path) {
|
|
273
|
+
try FileManager.default.removeItem(at: tmp)
|
|
274
|
+
}
|
|
275
|
+
FileManager.default.createFile(atPath: tmp.path, contents: nil, attributes: nil)
|
|
276
|
+
let handle = try FileHandle(forWritingTo: tmp)
|
|
277
|
+
for item in items {
|
|
278
|
+
let json = try encoder.encode(item)
|
|
279
|
+
// Use the throwing `write(contentsOf:)`, NOT the deprecated
|
|
280
|
+
// `write(_:)` — the latter raises an uncatchable ObjC NSException on
|
|
281
|
+
// I/O failure (e.g. disk full mid-compaction) which SIGABRTs the
|
|
282
|
+
// host app. This path runs on every flush (remove/limit/self-repair).
|
|
283
|
+
try handle.write(contentsOf: json)
|
|
284
|
+
try handle.write(contentsOf: Data([0x0A]))
|
|
285
|
+
}
|
|
286
|
+
try handle.synchronize()
|
|
287
|
+
try handle.close()
|
|
288
|
+
_ = try FileManager.default.replaceItemAt(fileURL, withItemAt: tmp)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private func fileSizeLocked() throws -> Int64 {
|
|
292
|
+
guard FileManager.default.fileExists(atPath: fileURL.path) else { return 0 }
|
|
293
|
+
let attrs = try FileManager.default.attributesOfItem(atPath: fileURL.path)
|
|
294
|
+
return (attrs[.size] as? NSNumber)?.int64Value ?? 0
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private func enforceLimitsLocked() throws {
|
|
298
|
+
let size = try fileSizeLocked()
|
|
299
|
+
// Cheap path — see EventQueueStore for the rationale.
|
|
300
|
+
if size <= maxBytes, size < Int64(maxItems) * 2 { return }
|
|
301
|
+
var all = try readAllLocked()
|
|
302
|
+
if all.count <= maxItems, try fileSizeLocked() <= maxBytes { return }
|
|
303
|
+
if all.count > maxItems {
|
|
304
|
+
let drop = all.count - maxItems
|
|
305
|
+
os_log(
|
|
306
|
+
"Queue exceeded %d items; dropping %d oldest",
|
|
307
|
+
log: log,
|
|
308
|
+
type: .info,
|
|
309
|
+
maxItems,
|
|
310
|
+
drop
|
|
311
|
+
)
|
|
312
|
+
all.removeFirst(drop)
|
|
313
|
+
}
|
|
314
|
+
try writeAllLocked(all)
|
|
315
|
+
while try fileSizeLocked() > maxBytes, !all.isEmpty {
|
|
316
|
+
let drop = max(1, all.count / 10)
|
|
317
|
+
os_log(
|
|
318
|
+
"Queue exceeded %lld bytes; dropping %d oldest",
|
|
319
|
+
log: log,
|
|
320
|
+
type: .info,
|
|
321
|
+
maxBytes,
|
|
322
|
+
drop
|
|
323
|
+
)
|
|
324
|
+
all.removeFirst(min(drop, all.count))
|
|
325
|
+
try writeAllLocked(all)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/// In-memory analogue of `enforceLimitsLocked`. Only the item-count
|
|
330
|
+
/// ceiling applies — there is no byte budget when nothing hits disk.
|
|
331
|
+
private func enforceLimitsInMemoryLocked() {
|
|
332
|
+
if memoryItems.count > maxItems {
|
|
333
|
+
let drop = memoryItems.count - maxItems
|
|
334
|
+
os_log(
|
|
335
|
+
"In-memory queue exceeded %d items; dropping %d oldest",
|
|
336
|
+
log: log,
|
|
337
|
+
type: .info,
|
|
338
|
+
maxItems,
|
|
339
|
+
drop
|
|
340
|
+
)
|
|
341
|
+
memoryItems.removeFirst(drop)
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private func appendToDeadLetterLocked(_ items: [T]) throws {
|
|
346
|
+
if !FileManager.default.fileExists(atPath: deadLetterURL.path) {
|
|
347
|
+
FileManager.default.createFile(atPath: deadLetterURL.path, contents: nil, attributes: nil)
|
|
348
|
+
}
|
|
349
|
+
let handle = try FileHandle(forWritingTo: deadLetterURL)
|
|
350
|
+
defer { try? handle.close() }
|
|
351
|
+
try handle.seekToEnd()
|
|
352
|
+
for item in items {
|
|
353
|
+
let json = try encoder.encode(item)
|
|
354
|
+
try handle.write(contentsOf: json)
|
|
355
|
+
try handle.write(contentsOf: Data([0x0A]))
|
|
356
|
+
}
|
|
357
|
+
try handle.synchronize()
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// MARK: - JSONL parsing
|
|
361
|
+
|
|
362
|
+
/// Tolerant decode — silently drops malformed lines and returns
|
|
363
|
+
/// `(items, sawMalformed)`. Used by `readAllLocked` for self-repair.
|
|
364
|
+
private func parseJSONLTolerant(_ data: Data) -> ([T], Bool) {
|
|
365
|
+
guard let text = String(data: data, encoding: .utf8) else {
|
|
366
|
+
os_log("items.jsonl is not valid UTF-8; resetting", log: log, type: .error)
|
|
367
|
+
return ([], true)
|
|
368
|
+
}
|
|
369
|
+
var well: [T] = []
|
|
370
|
+
var sawMalformed = false
|
|
371
|
+
for raw in text.split(separator: "\n", omittingEmptySubsequences: true) {
|
|
372
|
+
guard
|
|
373
|
+
let lineData = String(raw).data(using: .utf8),
|
|
374
|
+
let item = try? decoder.decode(T.self, from: lineData)
|
|
375
|
+
else {
|
|
376
|
+
sawMalformed = true
|
|
377
|
+
continue
|
|
378
|
+
}
|
|
379
|
+
well.append(item)
|
|
380
|
+
}
|
|
381
|
+
return (well, sawMalformed)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/// Strict decode — throws on any malformed line. Used for the
|
|
385
|
+
/// dead-letter file which should always be clean (we wrote it).
|
|
386
|
+
private func parseJSONL(_ data: Data) throws -> [T] {
|
|
387
|
+
guard let text = String(data: data, encoding: .utf8) else { return [] }
|
|
388
|
+
var items: [T] = []
|
|
389
|
+
for raw in text.split(separator: "\n", omittingEmptySubsequences: true) {
|
|
390
|
+
if let lineData = String(raw).data(using: .utf8) {
|
|
391
|
+
items.append(try decoder.decode(T.self, from: lineData))
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return items
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private static func ensureDirectory(_ url: URL) throws {
|
|
398
|
+
var isDir: ObjCBool = false
|
|
399
|
+
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir) {
|
|
400
|
+
if isDir.boolValue { return }
|
|
401
|
+
throw NSError(
|
|
402
|
+
domain: "com.swissknife.net",
|
|
403
|
+
code: 1,
|
|
404
|
+
userInfo: [NSLocalizedDescriptionKey: "Path exists and is not a directory: \(url.path)"]
|
|
405
|
+
)
|
|
406
|
+
}
|
|
407
|
+
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Performance/SKFrameTracker.swift
|
|
2
|
+
//
|
|
3
|
+
// Tracks slow and frozen frames via CADisplayLink for performance transactions.
|
|
4
|
+
// Slow frame: > 16.67ms (missed 60fps deadline)
|
|
5
|
+
// Frozen frame: > 700ms (perceived freeze)
|
|
6
|
+
|
|
7
|
+
import Foundation
|
|
8
|
+
|
|
9
|
+
#if canImport(UIKit)
|
|
10
|
+
import UIKit
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
#if canImport(QuartzCore)
|
|
14
|
+
import QuartzCore
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
public final class SKFrameTracker {
|
|
18
|
+
|
|
19
|
+
public static let shared = SKFrameTracker()
|
|
20
|
+
|
|
21
|
+
#if canImport(QuartzCore) && canImport(UIKit)
|
|
22
|
+
private var displayLink: CADisplayLink?
|
|
23
|
+
#endif
|
|
24
|
+
|
|
25
|
+
private var lastTimestamp: CFTimeInterval = 0
|
|
26
|
+
|
|
27
|
+
/// Frames that took > 16.67ms but <= 700ms to render.
|
|
28
|
+
public private(set) var slowFrameCount: Int = 0
|
|
29
|
+
|
|
30
|
+
/// Frames that took > 700ms to render.
|
|
31
|
+
public private(set) var frozenFrameCount: Int = 0
|
|
32
|
+
|
|
33
|
+
/// Total frames observed since tracking started.
|
|
34
|
+
public private(set) var totalFrameCount: Int = 0
|
|
35
|
+
|
|
36
|
+
private init() {}
|
|
37
|
+
|
|
38
|
+
// MARK: - Start / Stop
|
|
39
|
+
|
|
40
|
+
public func start() {
|
|
41
|
+
#if canImport(QuartzCore) && canImport(UIKit)
|
|
42
|
+
guard displayLink == nil else { return }
|
|
43
|
+
displayLink = CADisplayLink(target: self, selector: #selector(tick(_:)))
|
|
44
|
+
displayLink?.add(to: .main, forMode: .common)
|
|
45
|
+
#endif
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public func stop() {
|
|
49
|
+
#if canImport(QuartzCore) && canImport(UIKit)
|
|
50
|
+
displayLink?.invalidate()
|
|
51
|
+
displayLink = nil
|
|
52
|
+
#endif
|
|
53
|
+
lastTimestamp = 0
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public func reset() {
|
|
57
|
+
slowFrameCount = 0
|
|
58
|
+
frozenFrameCount = 0
|
|
59
|
+
totalFrameCount = 0
|
|
60
|
+
lastTimestamp = 0
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// MARK: - Tick
|
|
64
|
+
|
|
65
|
+
#if canImport(QuartzCore) && canImport(UIKit)
|
|
66
|
+
@objc private func tick(_ link: CADisplayLink) {
|
|
67
|
+
if lastTimestamp > 0 {
|
|
68
|
+
let duration = link.timestamp - lastTimestamp
|
|
69
|
+
totalFrameCount += 1
|
|
70
|
+
if duration > 0.7 {
|
|
71
|
+
frozenFrameCount += 1
|
|
72
|
+
} else if duration > 0.01667 {
|
|
73
|
+
slowFrameCount += 1
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
lastTimestamp = link.timestamp
|
|
77
|
+
}
|
|
78
|
+
#endif
|
|
79
|
+
|
|
80
|
+
// MARK: - Attach to Transaction
|
|
81
|
+
|
|
82
|
+
/// Attach current frame counts to a transaction as data fields.
|
|
83
|
+
/// Call this right before finishing the transaction.
|
|
84
|
+
public func attachToTransaction(_ tx: SKTransaction) {
|
|
85
|
+
tx.setData("frames_total", value: "\(totalFrameCount)")
|
|
86
|
+
tx.setData("frames_slow", value: "\(slowFrameCount)")
|
|
87
|
+
tx.setData("frames_frozen", value: "\(frozenFrameCount)")
|
|
88
|
+
}
|
|
89
|
+
}
|