@app-swiss-knife/react-native-sdk 0.3.0 → 0.3.2
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/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +127 -6
- 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 +27 -1
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +56 -7
- 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/modules/crash-reporting.js +199 -10
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +199 -10
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/modules/crash-reporting.ts +200 -11
|
@@ -347,6 +347,21 @@ let _tags: Record<string, string> = {};
|
|
|
347
347
|
let _extra: Record<string, unknown> = {};
|
|
348
348
|
let _user: SKCrashUser | null = null;
|
|
349
349
|
let _pendingEvents: SKCrashEvent[] = [];
|
|
350
|
+
/**
|
|
351
|
+
* Events handed to an in-flight POST: already out of `_pendingEvents`, not
|
|
352
|
+
* yet acknowledged by the server. Keyed by `eventId` because two flushes
|
|
353
|
+
* can be in the air at once — the fatal path fires its own bounded send
|
|
354
|
+
* while a debounced one is still running — and each must settle only its
|
|
355
|
+
* own batch.
|
|
356
|
+
*
|
|
357
|
+
* This exists because `persistQueue()` mirrors the whole queue by
|
|
358
|
+
* overwriting a single storage key. Deriving that snapshot from
|
|
359
|
+
* `_pendingEvents` alone made an in-flight batch invisible to it, so the
|
|
360
|
+
* next `enqueueEvent()` — one more tap, landing mid-send — rewrote the key
|
|
361
|
+
* WITHOUT it. The first event then existed only in a local `const batch`
|
|
362
|
+
* and died with the process: two taps, two captured errors, one row.
|
|
363
|
+
*/
|
|
364
|
+
let _inFlightEvents: Map<string, SKCrashEvent> = new Map();
|
|
350
365
|
let _flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
351
366
|
let _originalGlobalHandler: ((error: unknown, isFatal?: boolean) => void) | null = null;
|
|
352
367
|
let _sessionId: string | null = null;
|
|
@@ -572,12 +587,41 @@ function enqueueEvent(event: SKCrashEvent): void {
|
|
|
572
587
|
|
|
573
588
|
/** Put failed sends back AHEAD of anything captured since; drop-oldest beyond cap. */
|
|
574
589
|
function requeueEvents(events: SKCrashEvent[]): void {
|
|
590
|
+
// Back on the pending queue means no longer in flight. Clear first, or
|
|
591
|
+
// `persistQueue()` below counts them from both sides and the mirror
|
|
592
|
+
// carries each event twice.
|
|
593
|
+
clearInFlight(events);
|
|
575
594
|
const merged = [...events, ..._pendingEvents];
|
|
576
595
|
discardScreenshotsFor(merged.slice(0, -MAX_PENDING_EVENTS));
|
|
577
596
|
_pendingEvents = merged.slice(-MAX_PENDING_EVENTS);
|
|
578
597
|
persistQueue();
|
|
579
598
|
}
|
|
580
599
|
|
|
600
|
+
/**
|
|
601
|
+
* A batch has reached a terminal outcome — acknowledged, requeued, or
|
|
602
|
+
* deliberately dropped — so it stops counting towards the disk mirror.
|
|
603
|
+
*
|
|
604
|
+
* Deliberately does NOT persist: every caller is about to write the
|
|
605
|
+
* mirror itself (via `requeueEvents` or its own `persistQueue`), and two
|
|
606
|
+
* overlapping fire-and-forget `setItem` calls on one key can land out of
|
|
607
|
+
* order.
|
|
608
|
+
*/
|
|
609
|
+
function clearInFlight(events: SKCrashEvent[]): void {
|
|
610
|
+
for (const event of events) _inFlightEvents.delete(event.eventId);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** First occurrence wins, so in-flight events keep their place at the front. */
|
|
614
|
+
function dedupeEventsById(events: SKCrashEvent[]): SKCrashEvent[] {
|
|
615
|
+
const seen = new Set<string>();
|
|
616
|
+
const out: SKCrashEvent[] = [];
|
|
617
|
+
for (const event of events) {
|
|
618
|
+
if (seen.has(event.eventId)) continue;
|
|
619
|
+
seen.add(event.eventId);
|
|
620
|
+
out.push(event);
|
|
621
|
+
}
|
|
622
|
+
return out;
|
|
623
|
+
}
|
|
624
|
+
|
|
581
625
|
/** Events that will never deliver must not pin their screenshots in memory. */
|
|
582
626
|
function discardScreenshotsFor(dropped: SKCrashEvent[]): void {
|
|
583
627
|
for (const event of dropped) {
|
|
@@ -602,13 +646,24 @@ function maxCacheItems(): number {
|
|
|
602
646
|
* The slice respects `maxCacheItems`: persisted screenshots occupy cache
|
|
603
647
|
* slots too, so the event slice shrinks by however many screenshots are
|
|
604
648
|
* currently stored (newest events win — drop-oldest).
|
|
649
|
+
*
|
|
650
|
+
* IN-FLIGHT EVENTS COUNT. This writes the whole queue to one key, so
|
|
651
|
+
* whatever it leaves out is erased. An event that a POST is currently
|
|
652
|
+
* carrying is not delivered yet — until the server acknowledges it, it
|
|
653
|
+
* still has to survive a process kill exactly like a pending one. Leaving
|
|
654
|
+
* those out is what lost an event: capture, flush, capture again, and the
|
|
655
|
+
* second capture's `persistQueue()` overwrote the key without the first.
|
|
605
656
|
*/
|
|
606
657
|
function persistQueue(): void {
|
|
607
658
|
const storage = getOfflineStorage();
|
|
608
659
|
if (!storage) return;
|
|
609
660
|
const eventCap = Math.max(0, maxCacheItems() - _offlineScreenshots.length);
|
|
661
|
+
// In-flight events are older than anything pending, so they lead and
|
|
662
|
+
// drop-oldest still trims from the front. Dedupe defensively: a batch
|
|
663
|
+
// mid-requeue can briefly be reachable from both sides.
|
|
664
|
+
const live = dedupeEventsById([..._inFlightEvents.values(), ..._pendingEvents]);
|
|
610
665
|
// NB: slice(-0) would return the WHOLE array — guard the zero-cap case.
|
|
611
|
-
const snapshot = eventCap > 0 ?
|
|
666
|
+
const snapshot = eventCap > 0 ? live.slice(-eventCap) : [];
|
|
612
667
|
void (async () => {
|
|
613
668
|
try {
|
|
614
669
|
if (_restorePromise) await _restorePromise;
|
|
@@ -684,23 +739,53 @@ interface BatchResultEntry {
|
|
|
684
739
|
}
|
|
685
740
|
|
|
686
741
|
/**
|
|
687
|
-
*
|
|
742
|
+
* What we managed to learn from a 2xx batch response.
|
|
743
|
+
*
|
|
744
|
+
* `undecodable` is deliberately NOT folded into `parsed` with an empty
|
|
745
|
+
* `retryable` list: "the server accepted everything" and "we could not read
|
|
746
|
+
* the server's answer" are different facts, and only the first one licenses
|
|
747
|
+
* deleting the batch.
|
|
748
|
+
*/
|
|
749
|
+
type BatchOutcome =
|
|
750
|
+
| { kind: "parsed"; retryable: SKCrashEvent[] }
|
|
751
|
+
| { kind: "undecodable"; preview: string };
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Read the batch response `{ accepted, results: [{index, eventId, status}] }`
|
|
688
755
|
* and map 'retryable' entries back to their events (by eventId, falling back
|
|
689
|
-
* to index).
|
|
690
|
-
*
|
|
756
|
+
* to index). 'accepted', 'duplicate', 'rejected' and a legacy response shape
|
|
757
|
+
* are all terminal.
|
|
758
|
+
*
|
|
759
|
+
* A NON-EMPTY body that will not parse returns `undecodable`. It used to
|
|
760
|
+
* return "nothing is retryable", which the caller could not tell apart from a
|
|
761
|
+
* clean ack — so a truncated or corrupted 200 silently destroyed the batch.
|
|
762
|
+
* An empty body still counts as an ack: some deployments answer 200 with no
|
|
763
|
+
* content, and there is nothing there to have been a rejection.
|
|
691
764
|
*/
|
|
692
|
-
async function
|
|
765
|
+
async function readBatchOutcome(
|
|
693
766
|
res: Response,
|
|
694
767
|
batch: SKCrashEvent[]
|
|
695
|
-
): Promise<
|
|
768
|
+
): Promise<BatchOutcome> {
|
|
769
|
+
// Read as text first — `res.json()` cannot distinguish "empty body" from
|
|
770
|
+
// "malformed body", and that distinction is the whole point here.
|
|
771
|
+
let raw: string;
|
|
772
|
+
try {
|
|
773
|
+
raw = await res.text();
|
|
774
|
+
} catch (e) {
|
|
775
|
+
// The body stream itself failed mid-read: a truncated response by
|
|
776
|
+
// another name, so it is ambiguous for the same reason.
|
|
777
|
+
return { kind: "undecodable", preview: String(e).slice(0, 200) };
|
|
778
|
+
}
|
|
779
|
+
if (raw.trim() === "") return { kind: "parsed", retryable: [] };
|
|
780
|
+
|
|
696
781
|
let parsed: { results?: BatchResultEntry[] } | null = null;
|
|
697
782
|
try {
|
|
698
|
-
parsed =
|
|
783
|
+
parsed = JSON.parse(raw) as { results?: BatchResultEntry[] };
|
|
699
784
|
} catch {
|
|
700
|
-
return
|
|
785
|
+
return { kind: "undecodable", preview: raw.slice(0, 200) };
|
|
701
786
|
}
|
|
702
787
|
const results = parsed?.results;
|
|
703
|
-
if (!Array.isArray(results)) return [];
|
|
788
|
+
if (!Array.isArray(results)) return { kind: "parsed", retryable: [] };
|
|
704
789
|
const retryable: SKCrashEvent[] = [];
|
|
705
790
|
for (const entry of results) {
|
|
706
791
|
if (!entry || entry.status !== "retryable") continue;
|
|
@@ -712,7 +797,50 @@ async function extractRetryableEvents(
|
|
|
712
797
|
const event = byId ?? byIndex;
|
|
713
798
|
if (event && !retryable.includes(event)) retryable.push(event);
|
|
714
799
|
}
|
|
715
|
-
return retryable;
|
|
800
|
+
return { kind: "parsed", retryable };
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// MARK: - Ambiguous-response budget
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* How many consecutive undecodable-2xx responses the SAME batch may collect
|
|
807
|
+
* before we stop retrying it.
|
|
808
|
+
*
|
|
809
|
+
* The bound is deliberate. Without it a backend permanently answering 200
|
|
810
|
+
* with garbage would keep the batch in `_pendingEvents` forever, re-sending
|
|
811
|
+
* it on every scheduled flush, while newly captured crashes pile in behind it
|
|
812
|
+
* until `maxCacheItems` starts dropping the OLDEST — reintroducing silent
|
|
813
|
+
* loss from the other end. 5 attempts, each spaced by the existing
|
|
814
|
+
* exponential backoff (1s doubling to a 60s ceiling), is several minutes of
|
|
815
|
+
* retrying before anything is given up on.
|
|
816
|
+
*/
|
|
817
|
+
const MAX_AMBIGUOUS_ATTEMPTS = 5;
|
|
818
|
+
|
|
819
|
+
/** Consecutive undecodable-2xx answers for `_ambiguousBatchKey`. */
|
|
820
|
+
let _ambiguousAttempts = 0;
|
|
821
|
+
/** Identity of the batch those attempts belong to — the budget is per-batch,
|
|
822
|
+
* not a global "server has been flaky lately" tally. */
|
|
823
|
+
let _ambiguousBatchKey: string | null = null;
|
|
824
|
+
|
|
825
|
+
function batchKey(batch: SKCrashEvent[]): string {
|
|
826
|
+
return batch.map((e) => e.eventId).join(",");
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** Record one ambiguous answer for `batch`; returns the running count. */
|
|
830
|
+
function noteAmbiguousResponse(batch: SKCrashEvent[]): number {
|
|
831
|
+
const key = batchKey(batch);
|
|
832
|
+
if (key === _ambiguousBatchKey) {
|
|
833
|
+
_ambiguousAttempts += 1;
|
|
834
|
+
} else {
|
|
835
|
+
_ambiguousBatchKey = key;
|
|
836
|
+
_ambiguousAttempts = 1;
|
|
837
|
+
}
|
|
838
|
+
return _ambiguousAttempts;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
function clearAmbiguousStreak(): void {
|
|
842
|
+
_ambiguousAttempts = 0;
|
|
843
|
+
_ambiguousBatchKey = null;
|
|
716
844
|
}
|
|
717
845
|
|
|
718
846
|
// MARK: - Session Tracking
|
|
@@ -1828,6 +1956,10 @@ export const CrashReporting = {
|
|
|
1828
1956
|
|
|
1829
1957
|
const batch = [..._pendingEvents];
|
|
1830
1958
|
_pendingEvents = [];
|
|
1959
|
+
// The batch has left `_pendingEvents` but nothing has acknowledged it
|
|
1960
|
+
// yet, so it must stay in the disk mirror. Every exit below settles
|
|
1961
|
+
// it — see the `finally` for the backstop.
|
|
1962
|
+
for (const event of batch) _inFlightEvents.set(event.eventId, event);
|
|
1831
1963
|
|
|
1832
1964
|
const baseURL = _config.serverURL || "https://api.swiss-knife.dev";
|
|
1833
1965
|
const url = `${baseURL}/api/sdk/errors/event/batch`;
|
|
@@ -1878,11 +2010,56 @@ export const CrashReporting = {
|
|
|
1878
2010
|
});
|
|
1879
2011
|
|
|
1880
2012
|
if (res.ok) {
|
|
2013
|
+
const outcome = await readBatchOutcome(res, batch);
|
|
2014
|
+
|
|
2015
|
+
if (outcome.kind === "undecodable") {
|
|
2016
|
+
// A 2xx we cannot read is NOT an ack. We have no evidence the
|
|
2017
|
+
// server stored anything, so the batch goes back on the queue and
|
|
2018
|
+
// is retried under the normal backoff — the same treatment a 5xx
|
|
2019
|
+
// gets, because we know exactly as much as we do about a 5xx.
|
|
2020
|
+
//
|
|
2021
|
+
// Note the backoff reset stays in the decodable branch below: an
|
|
2022
|
+
// ambiguous answer must not reset the schedule it is about to use.
|
|
2023
|
+
const attempts = noteAmbiguousResponse(batch);
|
|
2024
|
+
if (attempts >= MAX_AMBIGUOUS_ATTEMPTS) {
|
|
2025
|
+
// Budget spent. Unlike the native SDKs there is no dead-letter
|
|
2026
|
+
// file to park these in, so the batch is dropped — but LOUDLY
|
|
2027
|
+
// and only after a bounded, documented number of attempts,
|
|
2028
|
+
// rather than silently on the first bad byte.
|
|
2029
|
+
clearAmbiguousStreak();
|
|
2030
|
+
console.warn(
|
|
2031
|
+
`[SwissKnife CrashReporting] dropping ${batch.length} event(s) after ` +
|
|
2032
|
+
`${attempts} undecodable HTTP ${res.status} responses; last body: ${outcome.preview}`
|
|
2033
|
+
);
|
|
2034
|
+
for (const e of batch) {
|
|
2035
|
+
_screenshotPromises.delete(e.eventId);
|
|
2036
|
+
removeOfflineScreenshot(e.eventId);
|
|
2037
|
+
}
|
|
2038
|
+
clearInFlight(batch);
|
|
2039
|
+
persistQueue();
|
|
2040
|
+
return;
|
|
2041
|
+
}
|
|
2042
|
+
console.warn(
|
|
2043
|
+
`[SwissKnife CrashReporting] undecodable HTTP ${res.status} response ` +
|
|
2044
|
+
`(attempt ${attempts}/${MAX_AMBIGUOUS_ATTEMPTS}) — requeueing ${batch.length} event(s); ` +
|
|
2045
|
+
`body: ${outcome.preview}`
|
|
2046
|
+
);
|
|
2047
|
+
const delayMs = applyBackoff(res.headers.get("Retry-After"));
|
|
2048
|
+
requeueEvents(batch);
|
|
2049
|
+
if (!options?.timeoutMs) scheduledFlush(delayMs);
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
|
|
1881
2053
|
_backoffMs = BACKOFF_INITIAL_MS;
|
|
1882
2054
|
_backoffUntil = 0;
|
|
2055
|
+
clearAmbiguousStreak();
|
|
1883
2056
|
// Per-event statuses: requeue ONLY 'retryable' (transient server
|
|
1884
2057
|
// failure); 'accepted'/'duplicate'/'rejected' are terminal.
|
|
1885
|
-
const retryable =
|
|
2058
|
+
const retryable = outcome.retryable;
|
|
2059
|
+
// The server has answered on the whole batch, so nothing in it is
|
|
2060
|
+
// in flight any more — including the retryable part, which
|
|
2061
|
+
// `requeueEvents` is about to put back on the pending queue.
|
|
2062
|
+
clearInFlight(batch);
|
|
1886
2063
|
if (retryable.length > 0) {
|
|
1887
2064
|
requeueEvents(retryable);
|
|
1888
2065
|
scheduledFlush();
|
|
@@ -1918,6 +2095,7 @@ export const CrashReporting = {
|
|
|
1918
2095
|
_screenshotPromises.delete(e.eventId);
|
|
1919
2096
|
removeOfflineScreenshot(e.eventId);
|
|
1920
2097
|
}
|
|
2098
|
+
clearInFlight(batch);
|
|
1921
2099
|
persistQueue();
|
|
1922
2100
|
} catch (e) {
|
|
1923
2101
|
console.warn("[SwissKnife CrashReporting] flush error:", e);
|
|
@@ -1926,6 +2104,15 @@ export const CrashReporting = {
|
|
|
1926
2104
|
if (!options?.timeoutMs) scheduledFlush(delayMs);
|
|
1927
2105
|
} finally {
|
|
1928
2106
|
if (timer) clearTimeout(timer);
|
|
2107
|
+
// Backstop. Every branch above settles the batch, but an entry left
|
|
2108
|
+
// behind by one that does not would pin its event in the disk
|
|
2109
|
+
// mirror forever and re-deliver it on every launch. Requeue rather
|
|
2110
|
+
// than drop: the same trade the native SDKs make — a duplicate is
|
|
2111
|
+
// visible and deduped server-side by the `errorEventSeen` ledger, a
|
|
2112
|
+
// silently lost crash report is neither.
|
|
2113
|
+
if (batch.some((event) => _inFlightEvents.has(event.eventId))) {
|
|
2114
|
+
requeueEvents(batch);
|
|
2115
|
+
}
|
|
1929
2116
|
}
|
|
1930
2117
|
},
|
|
1931
2118
|
|
|
@@ -1938,6 +2125,7 @@ export const CrashReporting = {
|
|
|
1938
2125
|
_extra = {};
|
|
1939
2126
|
_user = null;
|
|
1940
2127
|
_pendingEvents = [];
|
|
2128
|
+
_inFlightEvents = new Map();
|
|
1941
2129
|
if (_flushTimer !== null) {
|
|
1942
2130
|
clearTimeout(_flushTimer);
|
|
1943
2131
|
_flushTimer = null;
|
|
@@ -1948,6 +2136,7 @@ export const CrashReporting = {
|
|
|
1948
2136
|
_sessionErrorCount = 0;
|
|
1949
2137
|
_backoffMs = BACKOFF_INITIAL_MS;
|
|
1950
2138
|
_backoffUntil = 0;
|
|
2139
|
+
clearAmbiguousStreak();
|
|
1951
2140
|
if (_disableRejectionTracking) {
|
|
1952
2141
|
// Teardown is the mirror image of install and can fail the same way —
|
|
1953
2142
|
// restoring a slot on a frozen host object, or an engine `enable` that
|