@app-swiss-knife/react-native-sdk 0.3.1 → 0.4.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/CHANGELOG.md +82 -0
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +315 -41
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +13 -4
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +229 -36
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +6 -0
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +186 -56
- package/ios/vendor/SwissKnifeiOS/Net/BackendBackoffGate.swift +182 -0
- package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +164 -8
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +418 -58
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/modules/crash-reporting.js +525 -35
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +525 -35
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +1 -1
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +32 -7
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +1 -1
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts +32 -7
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +2 -0
- package/src/modules/crash-reporting.ts +625 -68
|
@@ -23,6 +23,11 @@
|
|
|
23
23
|
// - Distinguishes 4xx (terminal, `backendRejected`) from 5xx /
|
|
24
24
|
// network-level errors (retriable, `backendUnavailable`). 429 is
|
|
25
25
|
// retried with respect for the `Retry-After` header.
|
|
26
|
+
// - Two independent budgets, and both are needed. `maxRetries` bounds
|
|
27
|
+
// how many times we try; `operationDeadline` bounds how long the
|
|
28
|
+
// whole thing may take. Attempts alone are not a time bound — four
|
|
29
|
+
// 20s stalls is 87s of a user staring at a spinner — so every call
|
|
30
|
+
// goes through `performWithDeadline`.
|
|
26
31
|
// - Decoding is done by the caller via generics so each feature owns its
|
|
27
32
|
// own response shape — no monolithic response wrapper.
|
|
28
33
|
|
|
@@ -66,10 +71,47 @@ internal protocol BackendTransport: Sendable {
|
|
|
66
71
|
}
|
|
67
72
|
|
|
68
73
|
internal final class URLSessionBackendTransport: BackendTransport, @unchecked Sendable {
|
|
74
|
+
|
|
75
|
+
/// Inactivity ceiling for a single request. Foundation's default is 60s,
|
|
76
|
+
/// and `URLSession.shared` inherits it — which is why this is now set
|
|
77
|
+
/// explicitly rather than left to the platform. Android's transport
|
|
78
|
+
/// (`UrlConnectionBackendTransport`) budgets 10s connect + 10s read;
|
|
79
|
+
/// URLSession has no separate connect timeout — `timeoutIntervalForRequest`
|
|
80
|
+
/// is one inactivity timer spanning the whole exchange — so 20s is the
|
|
81
|
+
/// closest honest equivalent of Android's two 10s phases, and the two
|
|
82
|
+
/// platforms now give up on a stalled backend at the same point instead
|
|
83
|
+
/// of 60s apart.
|
|
84
|
+
static let requestTimeout: TimeInterval = 20
|
|
85
|
+
|
|
86
|
+
/// Whole-transfer ceiling. Foundation's default is **7 days**, which is
|
|
87
|
+
/// only defensible for background uploads. Nothing in this SDK long-polls,
|
|
88
|
+
/// streams, or transfers a large body: every call is a small JSON
|
|
89
|
+
/// request/response, and the batch envelope is capped at 100 items. 60s
|
|
90
|
+
/// leaves generous headroom for a slow-but-alive connection while keeping
|
|
91
|
+
/// a wedged transfer from outliving the retry budget that is supposed to
|
|
92
|
+
/// be handling it.
|
|
93
|
+
static let resourceTimeout: TimeInterval = 60
|
|
94
|
+
|
|
69
95
|
private let session: URLSession
|
|
70
96
|
|
|
71
|
-
|
|
72
|
-
|
|
97
|
+
/// `nil` builds the timeout-configured default session. Tests inject
|
|
98
|
+
/// their own; `URLSession.shared` is deliberately NOT the default any
|
|
99
|
+
/// more, because its configuration is process-global and cannot carry
|
|
100
|
+
/// SDK-specific timeouts without changing them for the host app too.
|
|
101
|
+
init(session: URLSession? = nil) {
|
|
102
|
+
self.session = session ?? Self.makeDefaultSession()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// Read access to the configured session — tests assert the timeouts
|
|
106
|
+
/// are actually applied rather than silently inherited from
|
|
107
|
+
/// `URLSession.shared`.
|
|
108
|
+
internal var sessionForTesting: URLSession { session }
|
|
109
|
+
|
|
110
|
+
private static func makeDefaultSession() -> URLSession {
|
|
111
|
+
let configuration = URLSessionConfiguration.default
|
|
112
|
+
configuration.timeoutIntervalForRequest = requestTimeout
|
|
113
|
+
configuration.timeoutIntervalForResource = resourceTimeout
|
|
114
|
+
return URLSession(configuration: configuration)
|
|
73
115
|
}
|
|
74
116
|
|
|
75
117
|
func perform(request: URLRequest) async throws -> BackendHttpResponse {
|
|
@@ -113,13 +155,61 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
113
155
|
/// Shared, lazily-configured production instance. Tests should
|
|
114
156
|
/// construct their own with an injected `BackendTransport` rather than
|
|
115
157
|
/// mutating this one.
|
|
116
|
-
|
|
158
|
+
///
|
|
159
|
+
/// This is the ONLY instance wired to `BackendBackoffGate.shared`. A
|
|
160
|
+
/// test-constructed client gets no gate unless it asks for one, so a
|
|
161
|
+
/// scripted 503 in one test cannot leave a backoff window armed for
|
|
162
|
+
/// the next.
|
|
163
|
+
static let shared = BackendHttpClient(backoffGate: .shared)
|
|
164
|
+
|
|
165
|
+
/// Wall-clock ceiling for ONE logical backend operation — every
|
|
166
|
+
/// attempt of the retry loop plus the backoff between them.
|
|
167
|
+
///
|
|
168
|
+
/// Without it nobody owns the total and the budget multiplies out:
|
|
169
|
+
/// `timeoutIntervalForRequest` is 20s of inactivity PER ATTEMPT and
|
|
170
|
+
/// `maxRetries` of 3 means four attempts, so a backend that accepts
|
|
171
|
+
/// the connection and then says nothing held `fetchOfferings` for
|
|
172
|
+
/// ~87s before it returned an error. A paywall that takes a minute
|
|
173
|
+
/// and a half to admit it failed has already lost the customer — the
|
|
174
|
+
/// SDK is meant to fall back to its cache long before that.
|
|
175
|
+
///
|
|
176
|
+
/// 30s is picked against the two bounds that actually constrain it:
|
|
177
|
+
/// - Floor — it must comfortably outlast a genuine transient. The
|
|
178
|
+
/// expensive shape is a `Retry-After`: a 503 with
|
|
179
|
+
/// `Retry-After: 5` plus round trips is ~6s, and three jittered
|
|
180
|
+
/// `1s * 2^n` backoffs total under 8s even at the top of the
|
|
181
|
+
/// jitter band. 30s clears both several times over, so nothing
|
|
182
|
+
/// that would have succeeded now fails.
|
|
183
|
+
/// - Ceiling — it must sit under `resourceTimeout` (60s) so that
|
|
184
|
+
/// this deadline, not Foundation's whole-transfer fallback, is
|
|
185
|
+
/// what ends a wedged operation; and it must fit inside the few
|
|
186
|
+
/// seconds of execution the OS grants after
|
|
187
|
+
/// `didEnterBackground`, where a flush is one of the callers.
|
|
188
|
+
///
|
|
189
|
+
/// One value for every call site, because there is no legitimately
|
|
190
|
+
/// long one to protect. Every route this client speaks —
|
|
191
|
+
/// `/offerings`, `/entitlements/list`, `/subscriptions/validate`,
|
|
192
|
+
/// `/users/identify`, `/users/attributes`, `/attribution/install`,
|
|
193
|
+
/// `/ingest` — is a small JSON request with a small JSON response,
|
|
194
|
+
/// and the ingest envelope is capped at 100 items. Nothing here
|
|
195
|
+
/// long-polls, streams, or uploads a file. Should one ever appear,
|
|
196
|
+
/// give it its own deadline rather than raising this one.
|
|
197
|
+
static let defaultOperationDeadline: TimeInterval = 30
|
|
117
198
|
|
|
118
199
|
private let transport: BackendTransport
|
|
119
200
|
private let backoff: ExponentialBackoff
|
|
120
201
|
private let configResolver: @Sendable () -> (apiKey: String, baseURL: String?)
|
|
121
202
|
private let encoder: JSONEncoder
|
|
122
203
|
private let decoder: JSONDecoder
|
|
204
|
+
private let operationDeadline: TimeInterval
|
|
205
|
+
|
|
206
|
+
/// Client-wide "do not send before T" window this client REPORTS into.
|
|
207
|
+
/// It never reads it: the gate holds back background telemetry only,
|
|
208
|
+
/// and every call that comes through here — offerings, entitlements,
|
|
209
|
+
/// purchases, identify — is either user-visible or on the critical
|
|
210
|
+
/// path of one. See `BackendBackoffGate` for the full rationale on who
|
|
211
|
+
/// arms and who clears.
|
|
212
|
+
private let backoffGate: BackendBackoffGate?
|
|
123
213
|
|
|
124
214
|
init(
|
|
125
215
|
transport: BackendTransport = URLSessionBackendTransport(),
|
|
@@ -127,13 +217,22 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
127
217
|
configResolver: @escaping @Sendable () -> (apiKey: String, baseURL: String?)
|
|
128
218
|
= { SwissKnife.identityConfigSnapshot() },
|
|
129
219
|
encoder: JSONEncoder = JSONEncoder(),
|
|
130
|
-
decoder: JSONDecoder = JSONDecoder()
|
|
220
|
+
decoder: JSONDecoder = JSONDecoder(),
|
|
221
|
+
// Tests shorten this so a deadline case runs in milliseconds
|
|
222
|
+
// instead of half a minute. `<= 0` disables the deadline
|
|
223
|
+
// entirely — only for tests that need the raw retry loop.
|
|
224
|
+
operationDeadline: TimeInterval = BackendHttpClient.defaultOperationDeadline,
|
|
225
|
+
// Defaults to nil — no gate — so only `.shared` feeds the shared
|
|
226
|
+
// window. Tests that exercise the gate pass their own instance.
|
|
227
|
+
backoffGate: BackendBackoffGate? = nil
|
|
131
228
|
) {
|
|
132
229
|
self.transport = transport
|
|
133
230
|
self.backoff = backoff
|
|
134
231
|
self.configResolver = configResolver
|
|
135
232
|
self.encoder = encoder
|
|
136
233
|
self.decoder = decoder
|
|
234
|
+
self.operationDeadline = operationDeadline
|
|
235
|
+
self.backoffGate = backoffGate
|
|
137
236
|
}
|
|
138
237
|
|
|
139
238
|
// MARK: - Public API
|
|
@@ -158,7 +257,7 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
158
257
|
maxRetries: Int = 3
|
|
159
258
|
) async throws -> Data {
|
|
160
259
|
let request = try makeRequest(method: "GET", path: path, query: query, body: nil)
|
|
161
|
-
let response = try await
|
|
260
|
+
let response = try await performWithDeadline(request: request, maxRetries: maxRetries)
|
|
162
261
|
return response.body
|
|
163
262
|
}
|
|
164
263
|
|
|
@@ -183,7 +282,7 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
183
282
|
) async throws -> Int {
|
|
184
283
|
let bodyData = try encoder.encode(body)
|
|
185
284
|
let request = try makeRequest(method: "POST", path: path, query: [], body: bodyData)
|
|
186
|
-
let response = try await
|
|
285
|
+
let response = try await performWithDeadline(request: request, maxRetries: maxRetries)
|
|
187
286
|
return response.statusCode
|
|
188
287
|
}
|
|
189
288
|
|
|
@@ -195,7 +294,7 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
195
294
|
maxRetries: Int = 3
|
|
196
295
|
) async throws -> Data {
|
|
197
296
|
let request = try makeRequest(method: "POST", path: path, query: [], body: body)
|
|
198
|
-
let response = try await
|
|
297
|
+
let response = try await performWithDeadline(request: request, maxRetries: maxRetries)
|
|
199
298
|
return response.body
|
|
200
299
|
}
|
|
201
300
|
|
|
@@ -227,10 +326,54 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
227
326
|
|
|
228
327
|
// MARK: - Retry loop
|
|
229
328
|
|
|
329
|
+
/// Run one logical operation — the whole retry loop — under
|
|
330
|
+
/// `operationDeadline`, and surface whichever finishes first.
|
|
331
|
+
///
|
|
332
|
+
/// A race rather than an elapsed-time check between attempts, because
|
|
333
|
+
/// a check between attempts cannot interrupt the attempt that is
|
|
334
|
+
/// actually stuck: the ~87s `fetchOfferings` was four 20s stalls, and
|
|
335
|
+
/// every one of them was already past the budget by the time control
|
|
336
|
+
/// came back to the loop. Cancelling the loser is what makes the
|
|
337
|
+
/// number honest — `URLSession`'s async API tears the request down on
|
|
338
|
+
/// task cancellation, so a stalled connection is closed rather than
|
|
339
|
+
/// left running detached.
|
|
340
|
+
private func performWithDeadline(
|
|
341
|
+
request: URLRequest,
|
|
342
|
+
maxRetries: Int
|
|
343
|
+
) async throws -> BackendHttpResponse {
|
|
344
|
+
guard operationDeadline > 0 else {
|
|
345
|
+
return try await performWithRetries(request: request, maxRetries: maxRetries)
|
|
346
|
+
}
|
|
347
|
+
let deadline = operationDeadline
|
|
348
|
+
return try await withThrowingTaskGroup(of: BackendHttpResponse.self) { group in
|
|
349
|
+
group.addTask { [self] in
|
|
350
|
+
try await performWithRetries(request: request, maxRetries: maxRetries)
|
|
351
|
+
}
|
|
352
|
+
group.addTask {
|
|
353
|
+
try await Task.sleep(nanoseconds: UInt64(deadline * 1_000_000_000))
|
|
354
|
+
throw PurchasesError.networkError(
|
|
355
|
+
underlying: "Operation exceeded the \(Int(deadline))s deadline"
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
// Cancels the loser on every exit path, thrown or returned.
|
|
359
|
+
// The group awaits both children as it unwinds; the cancelled
|
|
360
|
+
// one returns immediately and its error is discarded.
|
|
361
|
+
defer { group.cancelAll() }
|
|
362
|
+
guard let first = try await group.next() else {
|
|
363
|
+
throw PurchasesError.networkError(underlying: "Operation produced no result")
|
|
364
|
+
}
|
|
365
|
+
return first
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
230
369
|
/// Drive the bounded retry loop. Returns on 2xx, throws `backendRejected`
|
|
231
370
|
/// on 4xx (no retries — 4xx is terminal), throws `backendUnavailable`
|
|
232
371
|
/// after exhausting the retry budget for 5xx / network errors. 429 is
|
|
233
372
|
/// retried up to `maxRetries` with respect for the `Retry-After` header.
|
|
373
|
+
///
|
|
374
|
+
/// Bounded by attempts only — the wall-clock bound is
|
|
375
|
+
/// `performWithDeadline`, which cancels this task when the budget runs
|
|
376
|
+
/// out. Never call this directly from a public path.
|
|
234
377
|
private func performWithRetries(
|
|
235
378
|
request: URLRequest,
|
|
236
379
|
maxRetries: Int
|
|
@@ -238,6 +381,11 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
238
381
|
var attempt = 0
|
|
239
382
|
var lastError: Error?
|
|
240
383
|
while true {
|
|
384
|
+
// Cancellation means the deadline fired (or the caller walked
|
|
385
|
+
// away). Bail out rather than letting the `catch` below read a
|
|
386
|
+
// cancelled attempt as one more network blip and retry it —
|
|
387
|
+
// that would keep the doomed operation alive past its budget.
|
|
388
|
+
try Task.checkCancellation()
|
|
241
389
|
do {
|
|
242
390
|
let response = try await transport.perform(request: request)
|
|
243
391
|
if (200...299).contains(response.statusCode) {
|
|
@@ -249,6 +397,10 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
249
397
|
return response
|
|
250
398
|
}
|
|
251
399
|
if response.statusCode == 429 || response.statusCode >= 500 {
|
|
400
|
+
// Client-wide, and BEFORE the retry decision: the
|
|
401
|
+
// server's "not yet" outlives this request whether or
|
|
402
|
+
// not this request has budget left to use it.
|
|
403
|
+
backoffGate?.noteRetryable(retryAfter: response.retryAfterSeconds)
|
|
252
404
|
if attempt < maxRetries {
|
|
253
405
|
try await sleep(forAttempt: attempt, honoring: response.retryAfterSeconds)
|
|
254
406
|
attempt += 1
|
|
@@ -269,7 +421,11 @@ internal final class BackendHttpClient: @unchecked Sendable {
|
|
|
269
421
|
} catch let error as PurchasesError {
|
|
270
422
|
throw error
|
|
271
423
|
} catch {
|
|
272
|
-
// Network-level error (DNS, TLS, timeout) — retry.
|
|
424
|
+
// Network-level error (DNS, TLS, timeout) — retry. No
|
|
425
|
+
// header to honour, so the gate escalates on its own
|
|
426
|
+
// schedule; a dead backend must not be hammered by every
|
|
427
|
+
// queue drain that happens to come along.
|
|
428
|
+
backoffGate?.noteRetryable(retryAfter: nil)
|
|
273
429
|
if attempt < maxRetries {
|
|
274
430
|
try await sleep(forAttempt: attempt, honoring: nil)
|
|
275
431
|
attempt += 1
|