@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,100 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Context/Clock.swift
|
|
2
|
+
//
|
|
3
|
+
// Round-A cross-cutting primitive — Clock with server-time skew correction.
|
|
4
|
+
//
|
|
5
|
+
// The host device's wall clock cannot be trusted absolutely — users edit
|
|
6
|
+
// system time, devices boot before NTP sync, time zones change mid-flight.
|
|
7
|
+
// For analytics timestamps that's "ok-ish" (event ordering is preserved
|
|
8
|
+
// within a session) but for retention / cohort / time-series the absolute
|
|
9
|
+
// instant matters.
|
|
10
|
+
//
|
|
11
|
+
// This primitive lets the SDK gradually self-correct using the backend's
|
|
12
|
+
// HTTP `Date` response header as a reference. On every successful HTTP
|
|
13
|
+
// response, BackendHttpClient observes the `Date` header and calls
|
|
14
|
+
// `Clock.applyServerTime(serverDate, localObservedAt:)`. The Clock
|
|
15
|
+
// computes `skew = serverDate - localObservedAt` and adds that offset
|
|
16
|
+
// to every subsequent `Clock.now()`.
|
|
17
|
+
//
|
|
18
|
+
// Properties of this scheme
|
|
19
|
+
// -------------------------
|
|
20
|
+
// - **Gradual convergence.** First few SDK launches return uncorrected
|
|
21
|
+
// `Date()`; once any backend response lands the skew is set. New
|
|
22
|
+
// observations replace the previous skew (we don't average) — the
|
|
23
|
+
// server is the authority, the most recent observation is best.
|
|
24
|
+
// - **In-memory only.** A persisted skew across launches would defeat
|
|
25
|
+
// "user fixed their system clock between sessions." Cheap to re-learn
|
|
26
|
+
// on each launch.
|
|
27
|
+
// - **Bounded.** No artificial cap, but `Date()` is a `TimeInterval`
|
|
28
|
+
// (Double) — even pathological 10-year skew is fine arithmetically.
|
|
29
|
+
//
|
|
30
|
+
// Test seam
|
|
31
|
+
// ---------
|
|
32
|
+
// `_setSkewForTesting` and `_resetForTesting` give tests deterministic
|
|
33
|
+
// control without forcing them through the BackendHttpClient `Date`
|
|
34
|
+
// header path.
|
|
35
|
+
|
|
36
|
+
import Foundation
|
|
37
|
+
|
|
38
|
+
/// Time source with optional server-corrected skew.
|
|
39
|
+
internal enum Clock {
|
|
40
|
+
|
|
41
|
+
private static let lock = DispatchQueue(label: "com.swissknife.context.clock")
|
|
42
|
+
private static var _skew: TimeInterval = 0
|
|
43
|
+
|
|
44
|
+
/// Current corrected wall-clock instant. If no skew has been observed
|
|
45
|
+
/// yet, identical to `Date()`.
|
|
46
|
+
static func now() -> Date {
|
|
47
|
+
let s = lock.sync { _skew }
|
|
48
|
+
return Date().addingTimeInterval(s)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Apply a new observation from a backend HTTP `Date` header.
|
|
52
|
+
/// `serverDate` is what the backend claimed the time was;
|
|
53
|
+
/// `localObservedAt` is the device's local `Date()` taken right when
|
|
54
|
+
/// the response landed. Skew is the difference.
|
|
55
|
+
static func applyServerTime(_ serverDate: Date, localObservedAt: Date) {
|
|
56
|
+
let skew = serverDate.timeIntervalSince(localObservedAt)
|
|
57
|
+
lock.sync { _skew = skew }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// Currently-applied skew in seconds. Positive = server is ahead of
|
|
61
|
+
/// the device.
|
|
62
|
+
static var skew: TimeInterval {
|
|
63
|
+
lock.sync { _skew }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// MARK: - Test seams
|
|
67
|
+
|
|
68
|
+
/// Inject a fixed skew. Production MUST NOT call.
|
|
69
|
+
internal static func _setSkewForTesting(_ skew: TimeInterval) {
|
|
70
|
+
lock.sync { _skew = skew }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// Reset skew to zero.
|
|
74
|
+
internal static func _resetForTesting() {
|
|
75
|
+
lock.sync { _skew = 0 }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// MARK: - Header parsing
|
|
79
|
+
|
|
80
|
+
/// Parse an HTTP `Date` header (RFC 7231 IMF-fixdate form, e.g.
|
|
81
|
+
/// `"Sun, 06 Nov 1994 08:49:37 GMT"`). Returns nil if absent /
|
|
82
|
+
/// unparseable. We use the cached formatter to avoid hot-path
|
|
83
|
+
/// allocation.
|
|
84
|
+
static func parseHTTPDate(_ raw: String?) -> Date? {
|
|
85
|
+
guard let raw = raw?.trimmingCharacters(in: .whitespaces), !raw.isEmpty else {
|
|
86
|
+
return nil
|
|
87
|
+
}
|
|
88
|
+
return httpDateFormatter.date(from: raw)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// RFC 7231 IMF-fixdate formatter. Locked to en_US_POSIX and GMT so
|
|
92
|
+
/// it's deterministic regardless of host settings.
|
|
93
|
+
private static let httpDateFormatter: DateFormatter = {
|
|
94
|
+
let df = DateFormatter()
|
|
95
|
+
df.locale = Locale(identifier: "en_US_POSIX")
|
|
96
|
+
df.timeZone = TimeZone(identifier: "GMT")
|
|
97
|
+
df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
|
|
98
|
+
return df
|
|
99
|
+
}()
|
|
100
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Context/DeviceContext.swift
|
|
2
|
+
//
|
|
3
|
+
// Round-A cross-cutting primitive #1 — DeviceContext.
|
|
4
|
+
//
|
|
5
|
+
// Single source of truth for "what is this device / app / OS?" metadata that
|
|
6
|
+
// every IngestItem needs stamped onto its outgoing envelope. Before this
|
|
7
|
+
// primitive each module (Analytics, Crash, Performance, Attribution) reached
|
|
8
|
+
// for `ProcessInfo.processInfo.operatingSystemVersionString`,
|
|
9
|
+
// `Bundle.main.infoDictionary["CFBundleShortVersionString"]`,
|
|
10
|
+
// `UIDevice.current.systemName`, etc. directly — leading to inconsistent
|
|
11
|
+
// field names, drift between modules, and three different ways to spell
|
|
12
|
+
// "iPhone15,2".
|
|
13
|
+
//
|
|
14
|
+
// Design notes
|
|
15
|
+
// ------------
|
|
16
|
+
// - **Memoized lazily.** Every field is stable for the lifetime of the
|
|
17
|
+
// process (locale and timezone CAN change at runtime via user settings,
|
|
18
|
+
// but we accept the staleness — the next process launch picks up the
|
|
19
|
+
// new value and the IngestItems already in the queue are tagged with
|
|
20
|
+
// the value at the time they were enqueued anyway).
|
|
21
|
+
// - **Device model uses `sysctlbyname("hw.machine")`.** `UIDevice.current.model`
|
|
22
|
+
// returns the user-visible "iPhone" string; the wire field we want is
|
|
23
|
+
// the hardware identifier ("iPhone15,2") which `sysctlbyname` returns.
|
|
24
|
+
// This works on iOS device and the iOS Simulator (where it returns
|
|
25
|
+
// "arm64" / "x86_64" — acceptable; we don't need to distinguish in the
|
|
26
|
+
// simulator).
|
|
27
|
+
// - **No UIKit dependency.** Tests run on macOS via `swift test`; using
|
|
28
|
+
// UIDevice would force a `#if canImport(UIKit)` everywhere. We get OS
|
|
29
|
+
// name + version from `ProcessInfo`, which works cross-platform.
|
|
30
|
+
// - **`sdkVersion` is a build-time constant.** Hardcoded here; bump
|
|
31
|
+
// whenever the SDK is released. There is no `Bundle(for: ...).infoDictionary`
|
|
32
|
+
// path that gives the SwissKnife SDK's own version reliably from inside
|
|
33
|
+
// the SPM target (`Bundle.module` is for resources, not version).
|
|
34
|
+
// - **Thread-safe.** The memo cache is a single struct value populated
|
|
35
|
+
// once under a `DispatchQueue.sync` and never mutated again. Subsequent
|
|
36
|
+
// reads are lock-free after warmup via a separate atomic-isLoaded check
|
|
37
|
+
// would over-engineer this — the `sync` cost on a tiny value-copy is
|
|
38
|
+
// negligible compared to the dictionary build inside.
|
|
39
|
+
|
|
40
|
+
import Foundation
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// MARK: - DeviceContext (wire shape)
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
/// Snapshot of device / OS / app metadata. Sent on every IngestItem envelope
|
|
47
|
+
/// as part of `ContextSnapshot.device`.
|
|
48
|
+
///
|
|
49
|
+
/// Field naming uses snake_case via `CodingKeys` so the JSON wire shape
|
|
50
|
+
/// matches what swiss-ingest's downstream consumers (analytics events,
|
|
51
|
+
/// crash reports, performance traces) already key off — they all use
|
|
52
|
+
/// snake_case in the existing schemas.
|
|
53
|
+
internal struct DeviceContext: Codable, Sendable, Equatable {
|
|
54
|
+
/// Hardware identifier, e.g. "iPhone15,2", "iPad13,4", "arm64" (Simulator).
|
|
55
|
+
let deviceModel: String
|
|
56
|
+
/// "iOS" / "macOS" / "tvOS". Currently we always derive this from
|
|
57
|
+
/// `ProcessInfo` so it could be "macOS" when tests run on the Mac CLI;
|
|
58
|
+
/// production iOS apps always see "iOS".
|
|
59
|
+
let osName: String
|
|
60
|
+
/// OS version string, e.g. "18.3" — `ProcessInfo.operatingSystemVersion`.
|
|
61
|
+
let osVersion: String
|
|
62
|
+
/// `CFBundleShortVersionString` from the HOST app's main bundle.
|
|
63
|
+
let appVersion: String
|
|
64
|
+
/// `CFBundleVersion` from the HOST app's main bundle.
|
|
65
|
+
let appBuild: String
|
|
66
|
+
/// Locale identifier, e.g. "en_US".
|
|
67
|
+
let locale: String
|
|
68
|
+
/// IANA timezone identifier, e.g. "America/Los_Angeles".
|
|
69
|
+
let timezone: String
|
|
70
|
+
/// SwissKnife SDK version (build-time constant, see `Self.sdkVersionConstant`).
|
|
71
|
+
let sdkVersion: String
|
|
72
|
+
/// Host-app bundle identifier, e.g. "com.example.MyApp". Nil only when
|
|
73
|
+
/// `Bundle.main.bundleIdentifier` is itself nil (test-host edge cases).
|
|
74
|
+
let bundleIdentifier: String?
|
|
75
|
+
|
|
76
|
+
enum CodingKeys: String, CodingKey {
|
|
77
|
+
case deviceModel = "device_model"
|
|
78
|
+
case osName = "os_name"
|
|
79
|
+
case osVersion = "os_version"
|
|
80
|
+
case appVersion = "app_version"
|
|
81
|
+
case appBuild = "app_build"
|
|
82
|
+
case locale
|
|
83
|
+
case timezone
|
|
84
|
+
case sdkVersion = "sdk_version"
|
|
85
|
+
case bundleIdentifier = "bundle_identifier"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
// MARK: - Provider — memoized accessor
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
/// Lazy memoized accessor over `DeviceContext.current()`. Production callers
|
|
94
|
+
/// use `DeviceContextProvider.current()`; tests can reset the cache via
|
|
95
|
+
/// `_resetForTesting()` when they want to assert the build-from-scratch path.
|
|
96
|
+
internal enum DeviceContextProvider {
|
|
97
|
+
|
|
98
|
+
/// Hardcoded constant — bump on every SDK release.
|
|
99
|
+
/// Round A baseline. The build pipeline (`swiss-build`) will eventually
|
|
100
|
+
/// inject this via `-D` flag, but for now keep it in source so we don't
|
|
101
|
+
/// have to thread build settings through the SPM Package.swift.
|
|
102
|
+
static let sdkVersionConstant: String = "0.1.0"
|
|
103
|
+
|
|
104
|
+
private static let cacheLock = DispatchQueue(label: "com.swissknife.context.device")
|
|
105
|
+
private static var cached: DeviceContext?
|
|
106
|
+
|
|
107
|
+
/// Returns the current device context, computing and memoizing on
|
|
108
|
+
/// first call. Thread-safe.
|
|
109
|
+
static func current() -> DeviceContext {
|
|
110
|
+
cacheLock.sync {
|
|
111
|
+
if let cached { return cached }
|
|
112
|
+
let fresh = buildSnapshot()
|
|
113
|
+
cached = fresh
|
|
114
|
+
return fresh
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// Test seam — drop the memoized snapshot so the next `current()` call
|
|
119
|
+
/// rebuilds from scratch. Production code MUST NOT call this.
|
|
120
|
+
static func _resetForTesting() {
|
|
121
|
+
cacheLock.sync { cached = nil }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// MARK: - Internal builders
|
|
125
|
+
|
|
126
|
+
private static func buildSnapshot() -> DeviceContext {
|
|
127
|
+
let info = Bundle.main.infoDictionary
|
|
128
|
+
let appVersion = (info?["CFBundleShortVersionString"] as? String) ?? "unknown"
|
|
129
|
+
let appBuild = (info?["CFBundleVersion"] as? String) ?? "unknown"
|
|
130
|
+
|
|
131
|
+
return DeviceContext(
|
|
132
|
+
deviceModel: hardwareIdentifier(),
|
|
133
|
+
osName: osName(),
|
|
134
|
+
osVersion: osVersionString(),
|
|
135
|
+
appVersion: appVersion,
|
|
136
|
+
appBuild: appBuild,
|
|
137
|
+
locale: Locale.current.identifier,
|
|
138
|
+
timezone: TimeZone.current.identifier,
|
|
139
|
+
sdkVersion: sdkVersionConstant,
|
|
140
|
+
bundleIdentifier: Bundle.main.bundleIdentifier
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Resolve the hardware model identifier via `sysctlbyname("hw.machine")`.
|
|
145
|
+
/// On Simulator returns "arm64" / "x86_64" — acceptable for analytics
|
|
146
|
+
/// purposes since the simulator has no meaningful "device model" anyway.
|
|
147
|
+
/// Returns "unknown" if the sysctl call fails (should not happen in
|
|
148
|
+
/// practice but we never want to crash on telemetry).
|
|
149
|
+
private static func hardwareIdentifier() -> String {
|
|
150
|
+
var size = 0
|
|
151
|
+
if sysctlbyname("hw.machine", nil, &size, nil, 0) != 0 { return "unknown" }
|
|
152
|
+
var machine = [CChar](repeating: 0, count: size)
|
|
153
|
+
if sysctlbyname("hw.machine", &machine, &size, nil, 0) != 0 { return "unknown" }
|
|
154
|
+
let result = String(cString: machine)
|
|
155
|
+
return result.isEmpty ? "unknown" : result
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// OS name. On iOS-derived processes `ProcessInfo` returns "iOS"-like
|
|
159
|
+
/// values; on macOS test host we get "macOS". We hardcode iOS for the
|
|
160
|
+
/// device build path and fall back to `ProcessInfo` otherwise — the
|
|
161
|
+
/// test target shouldn't claim to be iOS when it's clearly running on
|
|
162
|
+
/// the Mac CLI.
|
|
163
|
+
private static func osName() -> String {
|
|
164
|
+
#if os(iOS)
|
|
165
|
+
return "iOS"
|
|
166
|
+
#elseif os(macOS)
|
|
167
|
+
return "macOS"
|
|
168
|
+
#elseif os(tvOS)
|
|
169
|
+
return "tvOS"
|
|
170
|
+
#elseif os(watchOS)
|
|
171
|
+
return "watchOS"
|
|
172
|
+
#else
|
|
173
|
+
return "unknown"
|
|
174
|
+
#endif
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// OS version. `ProcessInfo.operatingSystemVersionString` returns a
|
|
178
|
+
/// human-readable string like "Version 18.3 (Build 22D63)"; we want the
|
|
179
|
+
/// raw "18.3" form for parsing. Use `operatingSystemVersion` (struct)
|
|
180
|
+
/// to get major.minor.patch.
|
|
181
|
+
private static func osVersionString() -> String {
|
|
182
|
+
let v = ProcessInfo.processInfo.operatingSystemVersion
|
|
183
|
+
// Drop the patch component when zero — both "18.3" and "18.3.1"
|
|
184
|
+
// are valid wire forms but the .0 patch is noise.
|
|
185
|
+
if v.patchVersion == 0 {
|
|
186
|
+
return "\(v.majorVersion).\(v.minorVersion)"
|
|
187
|
+
}
|
|
188
|
+
return "\(v.majorVersion).\(v.minorVersion).\(v.patchVersion)"
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Context/IDs.swift
|
|
2
|
+
//
|
|
3
|
+
// Round-A cross-cutting primitive — unified ID generation.
|
|
4
|
+
//
|
|
5
|
+
// Centralises every callsite that historically reached for `UUID().uuidString`
|
|
6
|
+
// to mint an identifier. Today the implementation is a thin wrapper, but
|
|
7
|
+
// having one funnel point lets us:
|
|
8
|
+
// - Swap in UUIDv7 (sortable / time-prefixed) later without grepping every
|
|
9
|
+
// module.
|
|
10
|
+
// - Reason about which IDs are externally visible (event_id is the backend
|
|
11
|
+
// dedupe key) vs. purely internal (request_id is for log correlation).
|
|
12
|
+
// - Stub generation deterministically in tests via injection.
|
|
13
|
+
//
|
|
14
|
+
// Three named flavours so call sites read intentionally even though the
|
|
15
|
+
// implementation is identical today. This is the same pattern as
|
|
16
|
+
// `UUID() vs URL()`-style strong typing — naming makes the call sites
|
|
17
|
+
// self-documenting.
|
|
18
|
+
|
|
19
|
+
import Foundation
|
|
20
|
+
|
|
21
|
+
/// Unified ID factory. Internal — call sites in IngestClient, Identity,
|
|
22
|
+
/// Session, AttributesClient, CrashReporting, Performance, and Attribution
|
|
23
|
+
/// route through here.
|
|
24
|
+
internal enum IDs {
|
|
25
|
+
|
|
26
|
+
/// Production generator — the single canonical UUID call site for
|
|
27
|
+
/// the SDK. Stored as a `@Sendable` closure so `_setGeneratorForTesting`
|
|
28
|
+
/// can swap a deterministic counter-backed factory and
|
|
29
|
+
/// `_resetForTesting` can restore the original without re-typing
|
|
30
|
+
/// `UUID().uuidString` at multiple call sites.
|
|
31
|
+
private static let defaultGenerator: @Sendable () -> String = { UUID().uuidString }
|
|
32
|
+
|
|
33
|
+
private static let lock = DispatchQueue(label: "com.swissknife.context.ids")
|
|
34
|
+
private static var generator: @Sendable () -> String = defaultGenerator
|
|
35
|
+
|
|
36
|
+
/// Idempotency key for an individual telemetry event. The backend
|
|
37
|
+
/// dedupes against `(app_project_id, event_id)` so the same id MUST
|
|
38
|
+
/// NOT be reused across logically distinct events.
|
|
39
|
+
static func eventId() -> String { generate() }
|
|
40
|
+
|
|
41
|
+
/// Per-HTTP-request correlation id. Surfaces in BackendHttpClient logs
|
|
42
|
+
/// and `X-Request-Id` headers (future). Not currently visible to the
|
|
43
|
+
/// backend.
|
|
44
|
+
static func requestId() -> String { generate() }
|
|
45
|
+
|
|
46
|
+
/// Per-session uuid. Used by `Session` on restart.
|
|
47
|
+
static func sessionId() -> String { generate() }
|
|
48
|
+
|
|
49
|
+
private static func generate() -> String {
|
|
50
|
+
lock.sync { generator() }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// MARK: - Test seams
|
|
54
|
+
|
|
55
|
+
/// Inject a deterministic generator. Production MUST NOT call this.
|
|
56
|
+
internal static func _setGeneratorForTesting(_ gen: @escaping @Sendable () -> String) {
|
|
57
|
+
lock.sync { generator = gen }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// Restore the production generator.
|
|
61
|
+
internal static func _resetForTesting() {
|
|
62
|
+
lock.sync { generator = defaultGenerator }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Context/Reachability.swift
|
|
2
|
+
//
|
|
3
|
+
// Round-A cross-cutting primitive — network reachability state.
|
|
4
|
+
//
|
|
5
|
+
// Single source of "is the device online?" for the SDK. Used by IngestClient
|
|
6
|
+
// to pause the flush loop while offline (no point hammering the retry
|
|
7
|
+
// budget when DNS will fail anyway) and resume immediately when the network
|
|
8
|
+
// comes back. Future use cases: Performance metric gating (don't trace
|
|
9
|
+
// expected-failed requests as anomalies), CrashReporting (delay flush of
|
|
10
|
+
// stored crashes until we have a viable path).
|
|
11
|
+
//
|
|
12
|
+
// Implementation
|
|
13
|
+
// --------------
|
|
14
|
+
// `Network.NWPathMonitor` is the Foundation-blessed primitive here. It
|
|
15
|
+
// runs on a private GCD queue and posts `NWPath` updates. We bridge those
|
|
16
|
+
// updates into a simple `isReachable: Bool` and broadcast TRANSITIONS to
|
|
17
|
+
// observers — they're notified only when the bit flips, not on every
|
|
18
|
+
// no-op update. This matters because NWPathMonitor occasionally emits
|
|
19
|
+
// duplicate `.satisfied` events.
|
|
20
|
+
//
|
|
21
|
+
// Cross-platform
|
|
22
|
+
// --------------
|
|
23
|
+
// Network.framework is available on iOS 12+ and macOS 10.14+ — within our
|
|
24
|
+
// support floor (iOS 16, macOS 12). No `#if available` gating needed.
|
|
25
|
+
|
|
26
|
+
import Foundation
|
|
27
|
+
import Network
|
|
28
|
+
|
|
29
|
+
/// Single shared reachability monitor. Production callers go through
|
|
30
|
+
/// `Reachability.shared`; tests construct a private instance and drive
|
|
31
|
+
/// transitions via `_setReachableForTesting`.
|
|
32
|
+
internal final class Reachability: @unchecked Sendable {
|
|
33
|
+
|
|
34
|
+
/// Shared production instance. Boots a real NWPathMonitor.
|
|
35
|
+
static let shared: Reachability = Reachability()
|
|
36
|
+
|
|
37
|
+
private let lock = DispatchQueue(label: "com.swissknife.context.reachability")
|
|
38
|
+
private var observers: [UUID: (Bool) -> Void] = [:]
|
|
39
|
+
private var current: Bool = true
|
|
40
|
+
private let monitor: NWPathMonitor?
|
|
41
|
+
private let monitorQueue: DispatchQueue
|
|
42
|
+
|
|
43
|
+
/// Convenience init for production: spins up a real NWPathMonitor.
|
|
44
|
+
init() {
|
|
45
|
+
self.monitor = NWPathMonitor()
|
|
46
|
+
self.monitorQueue = DispatchQueue(label: "com.swissknife.context.reachability.monitor")
|
|
47
|
+
self.current = true // optimistic — assume online until proven otherwise
|
|
48
|
+
self.monitor?.pathUpdateHandler = { [weak self] path in
|
|
49
|
+
let reachable = (path.status == .satisfied)
|
|
50
|
+
self?.update(reachable: reachable)
|
|
51
|
+
}
|
|
52
|
+
self.monitor?.start(queue: monitorQueue)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Test init — skips NWPathMonitor entirely so tests don't depend on
|
|
56
|
+
/// the runtime's network state.
|
|
57
|
+
init(startMonitoring: Bool) {
|
|
58
|
+
if startMonitoring {
|
|
59
|
+
self.monitor = NWPathMonitor()
|
|
60
|
+
self.monitorQueue = DispatchQueue(label: "com.swissknife.context.reachability.monitor")
|
|
61
|
+
self.current = true
|
|
62
|
+
self.monitor?.pathUpdateHandler = { [weak self] path in
|
|
63
|
+
let reachable = (path.status == .satisfied)
|
|
64
|
+
self?.update(reachable: reachable)
|
|
65
|
+
}
|
|
66
|
+
self.monitor?.start(queue: monitorQueue)
|
|
67
|
+
} else {
|
|
68
|
+
self.monitor = nil
|
|
69
|
+
self.monitorQueue = DispatchQueue(label: "com.swissknife.context.reachability.monitor.idle")
|
|
70
|
+
self.current = true
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
deinit {
|
|
75
|
+
monitor?.cancel()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Current reachability state. Reads are cheap (lock-protected single
|
|
79
|
+
/// bool).
|
|
80
|
+
var isReachable: Bool {
|
|
81
|
+
lock.sync { current }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Subscribe to reachability transitions. Fired only when the state
|
|
85
|
+
/// CHANGES (no-op duplicate `.satisfied` events from NWPathMonitor
|
|
86
|
+
/// are coalesced). Returns a token for `removeObserver`.
|
|
87
|
+
@discardableResult
|
|
88
|
+
func addObserver(_ block: @escaping (Bool) -> Void) -> UUID {
|
|
89
|
+
let token = UUID()
|
|
90
|
+
lock.sync { observers[token] = block }
|
|
91
|
+
return token
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
func removeObserver(_ token: UUID) {
|
|
95
|
+
lock.sync { _ = observers.removeValue(forKey: token) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// MARK: - Internal
|
|
99
|
+
|
|
100
|
+
private func update(reachable: Bool) {
|
|
101
|
+
let (changed, snapshot): (Bool, [(Bool) -> Void]) = lock.sync {
|
|
102
|
+
let changed = (reachable != current)
|
|
103
|
+
current = reachable
|
|
104
|
+
return (changed, Array(observers.values))
|
|
105
|
+
}
|
|
106
|
+
guard changed else { return }
|
|
107
|
+
for observer in snapshot { observer(reachable) }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// MARK: - Test seams
|
|
111
|
+
|
|
112
|
+
/// Drive a synthetic reachability transition. Production MUST NOT call.
|
|
113
|
+
internal func _setReachableForTesting(_ reachable: Bool) {
|
|
114
|
+
update(reachable: reachable)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Drop all observers — used by tests that want a clean slate.
|
|
118
|
+
internal func _resetObserversForTesting() {
|
|
119
|
+
lock.sync { observers.removeAll() }
|
|
120
|
+
}
|
|
121
|
+
}
|