@app-swiss-knife/react-native-sdk 0.2.4 → 0.2.7
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/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +18 -1
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +150 -8
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +53 -9
- package/ios/SwissKnifePurchases.swift +16 -1
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +171 -4
- package/lib/commonjs/modules/crash-reporting.js +260 -5
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/commonjs/purchases.js +195 -32
- package/lib/commonjs/purchases.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +260 -5
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/module/purchases.js +196 -33
- package/lib/module/purchases.js.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +39 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/commonjs/purchases.d.ts +65 -13
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts +39 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/purchases.d.ts +65 -13
- package/lib/typescript/module/purchases.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/modules/crash-reporting.ts +274 -4
- package/src/purchases.ts +196 -33
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type { PurchasesConfiguration, LOG_LEVEL, CustomerInfo, PurchasesOfferings, PurchasesPackage, PurchasesStoreProduct, MakePurchaseResult, LogInResult, CustomerInfoUpdateListener } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Guard for surfaces that genuinely need the PURCHASE engine —
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Guard for surfaces that genuinely need the PURCHASE engine — `getOfferings`,
|
|
4
|
+
* `purchasePackage`, `restorePurchases` and friends. Checks the JS "configure
|
|
5
|
+
* was called" flag.
|
|
6
|
+
*
|
|
7
|
+
* The RC-parity identity aliases used to sit here too, on the grounds that the
|
|
8
|
+
* legacy native bridge methods they called meant different things on iOS and
|
|
9
|
+
* Android. They now delegate to `Identity` and sit on
|
|
10
|
+
* {@link throwIfCoreNotConfigured} — see the block above `logIn`.
|
|
6
11
|
*/
|
|
7
12
|
declare function throwIfNotConfigured(): void;
|
|
8
13
|
/**
|
|
@@ -102,23 +107,63 @@ export declare class Purchases {
|
|
|
102
107
|
/** Restore purchases tied to the underlying store account. */
|
|
103
108
|
static restorePurchases(): Promise<CustomerInfo>;
|
|
104
109
|
/**
|
|
105
|
-
* Identify the user
|
|
110
|
+
* Identify the user, merging the anonymous subject into `appUserID`
|
|
111
|
+
* server-side.
|
|
112
|
+
*
|
|
113
|
+
* Exactly `Identity.logIn(appUserID)` with the RevenueCat name. On both
|
|
114
|
+
* platforms this POSTs `(anonymous_id, user_id)` to
|
|
115
|
+
* `/api/sdk/users/identify` and resolves once the backend ack and the
|
|
116
|
+
* post-identify entitlements refresh complete; a rejection means the device
|
|
117
|
+
* is NOT merged server-side and the call must be retried. Repeat calls with
|
|
118
|
+
* the id already in effect are free (no bridge hop, no request).
|
|
106
119
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* `Identity.logIn(
|
|
120
|
+
* Works after `Purchases.configure`, `Purchases.configureCore`, or
|
|
121
|
+
* `Analytics.init` — the store does not have to be ours.
|
|
122
|
+
*
|
|
123
|
+
* @deprecated Wave 1 — prefer `Identity.logIn(userId)`. This alias exists so
|
|
124
|
+
* RevenueCat migrations compile unchanged; it is a pure delegation, so there
|
|
125
|
+
* is nothing to gain by keeping it and one less name to learn by dropping it.
|
|
111
126
|
*/
|
|
112
127
|
static logIn(appUserID: string): Promise<LogInResult>;
|
|
113
128
|
/**
|
|
114
|
-
* Log out the current user and revert to anonymous.
|
|
129
|
+
* Log out the current user and revert to anonymous. The device id survives,
|
|
130
|
+
* so analytics continuity for the same physical install is preserved.
|
|
131
|
+
*
|
|
132
|
+
* Exactly `Identity.logOut()` with the RevenueCat name, plus RC's
|
|
133
|
+
* `CustomerInfo` return: after the logout lands, the post-logout customer
|
|
134
|
+
* info is read back from the store engine. **When no store engine is
|
|
135
|
+
* configured** (`configureCore`, `Analytics.init`) there is nothing to read,
|
|
136
|
+
* and you get an entitlement-free `CustomerInfo` for the anonymous subject —
|
|
137
|
+
* the same record, built in JS, on both platforms. Do not read that as a
|
|
138
|
+
* store error; if you want real entitlements, call `getCustomerInfo()`.
|
|
115
139
|
*
|
|
116
|
-
* @deprecated Wave 1 — prefer `Identity.logOut()
|
|
140
|
+
* @deprecated Wave 1 — prefer `Identity.logOut()`, which does not pretend to
|
|
141
|
+
* return store state.
|
|
117
142
|
*/
|
|
118
143
|
static logOut(): Promise<CustomerInfo>;
|
|
119
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* The current subject id — the identified user id if there is one, else the
|
|
146
|
+
* SDK's anonymous id (`$SKAnonymousID:`-prefixed, stable across launches).
|
|
147
|
+
*
|
|
148
|
+
* Exactly `(await Identity.current()).userId ?? .anonymousId`. Note this is
|
|
149
|
+
* the *Identity* subject on both platforms; on Android the purchase
|
|
150
|
+
* gateway's own anonymous id is a different, per-process value, and this
|
|
151
|
+
* alias deliberately does not report it.
|
|
152
|
+
*
|
|
153
|
+
* @deprecated Wave 1 — prefer `Identity.current()`, which hands back the
|
|
154
|
+
* anonymous id, the user id and `isAnonymous` in one read instead of forcing
|
|
155
|
+
* two calls that can interleave.
|
|
156
|
+
*/
|
|
120
157
|
static getAppUserID(): Promise<string>;
|
|
121
|
-
/**
|
|
158
|
+
/**
|
|
159
|
+
* Whether the current subject is the SDK-generated anonymous id.
|
|
160
|
+
*
|
|
161
|
+
* Exactly `(await Identity.current()).isAnonymous`, so it tracks
|
|
162
|
+
* `Identity.reset()` and `Identity.wipe()` on both platforms.
|
|
163
|
+
*
|
|
164
|
+
* @deprecated Wave 1 — prefer `Identity.current()`; see
|
|
165
|
+
* {@link getAppUserID}.
|
|
166
|
+
*/
|
|
122
167
|
static isAnonymous(): Promise<boolean>;
|
|
123
168
|
/** Set arbitrary subscriber attributes. null clears an attribute. */
|
|
124
169
|
static setAttributes(attributes: Record<string, string | null>): Promise<void>;
|
|
@@ -136,7 +181,14 @@ export declare class Purchases {
|
|
|
136
181
|
static addCustomerInfoUpdateListener(listener: CustomerInfoUpdateListener): void;
|
|
137
182
|
/** Remove a previously-registered CustomerInfo update listener. */
|
|
138
183
|
static removeCustomerInfoUpdateListener(listener: CustomerInfoUpdateListener): void;
|
|
139
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* @internal Test seam: reset singleton state between tests.
|
|
186
|
+
*
|
|
187
|
+
* Also resets `Identity`'s caches, because the RC-parity aliases delegate
|
|
188
|
+
* there: without it, a `Purchases.logIn("u")` in one test would leave
|
|
189
|
+
* `Identity`'s `lastLogIn` primed and the next test's identical call would
|
|
190
|
+
* short-circuit before reaching the (freshly mocked) bridge.
|
|
191
|
+
*/
|
|
140
192
|
static __resetForTests(): void;
|
|
141
193
|
}
|
|
142
194
|
export { throwIfNotConfigured as __throwIfNotConfigured, throwIfCoreNotConfigured as __throwIfCoreNotConfigured, };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"purchases.d.ts","sourceRoot":"","sources":["../../../src/purchases.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"purchases.d.ts","sourceRoot":"","sources":["../../../src/purchases.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,sBAAsB,EACtB,SAAS,EACT,YAAY,EACZ,kBA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only React Native SDK for App Swiss Knife — a drop-in replacement for RevenueCat's react-native-purchases public API.",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/commonjs/index.js",
|
|
@@ -108,6 +108,7 @@
|
|
|
108
108
|
"scripts": {
|
|
109
109
|
"typecheck": "tsc --noEmit",
|
|
110
110
|
"test": "vitest run",
|
|
111
|
+
"check:hermes": "node scripts/hermes-boot-check.mjs",
|
|
111
112
|
"clean": "node -e \"for (const d of ['lib','ios/vendor','android/vendor']) require('fs').rmSync(d,{recursive:true,force:true})\"",
|
|
112
113
|
"vendor:native": "node scripts/vendor-native.mjs",
|
|
113
114
|
"build": "node scripts/vendor-native.mjs && bob build"
|
|
@@ -372,6 +372,190 @@ let _screenshotCaptureOverride:
|
|
|
372
372
|
| null
|
|
373
373
|
| undefined;
|
|
374
374
|
|
|
375
|
+
// MARK: - Identity-derived Subject
|
|
376
|
+
//
|
|
377
|
+
// ## Why the subject is PULLED, never pushed
|
|
378
|
+
//
|
|
379
|
+
// Attaching a person to a crash used to require a `CrashReporting.setUser(...)`
|
|
380
|
+
// call that nothing enforced and nothing validated. Forget it and every crash
|
|
381
|
+
// arrives with `user.id === ""`; the dashboard then reports "Affected users:
|
|
382
|
+
// No identified users" and the integration looks like broken crash reporting
|
|
383
|
+
// when it is actually complete. `Identity.logIn(...)` — the canonical "this is
|
|
384
|
+
// who this is" statement — did not reach this module at all.
|
|
385
|
+
//
|
|
386
|
+
// `modules/analytics.ts` already fixed this exact bug class (see the comment
|
|
387
|
+
// above its `buildEvent`, and `identity.ts` lines 8-16): `user_id` is derived
|
|
388
|
+
// from the native `Identity.current()` snapshot at event-build time rather
|
|
389
|
+
// than being pushed in by whoever calls `logIn`. A pull is immune to
|
|
390
|
+
// initialisation order, and crash reporting is precisely the module that
|
|
391
|
+
// starts before the identity is known — you want the handler armed on the
|
|
392
|
+
// first frame, long before auth state has been read.
|
|
393
|
+
//
|
|
394
|
+
// So: the crash subject is resolved from Identity at report time (and
|
|
395
|
+
// re-resolved at send time, see `flush`), not at login time.
|
|
396
|
+
//
|
|
397
|
+
// ## Deliberately not the anonymous id
|
|
398
|
+
//
|
|
399
|
+
// `Identity.current()` also carries an `anonymousId`, and stamping it into
|
|
400
|
+
// `user.id` would make "affected users" non-zero for every app that never
|
|
401
|
+
// logs in. That both inflates the number and diverges from Analytics, whose
|
|
402
|
+
// `user_id` is the identified subject only. The anonymous id already travels
|
|
403
|
+
// separately on the ingest envelope. Only `userId` is used here.
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Last resolved identified subject id from the native Identity bridge.
|
|
407
|
+
* `null` means "no identified subject" — either genuinely anonymous, or the
|
|
408
|
+
* bridge has not answered yet. Both cases are treated the same: nothing is
|
|
409
|
+
* stamped, which is exactly today's behaviour for apps that never log in.
|
|
410
|
+
*/
|
|
411
|
+
let _identityUserId: string | null = null;
|
|
412
|
+
/** In-flight read, so N events in a burst share one bridge hop. */
|
|
413
|
+
let _identityRefresh: Promise<void> | null = null;
|
|
414
|
+
/** Monotonic read counter — a superseded read must not clobber a newer one. */
|
|
415
|
+
let _identityGeneration = 0;
|
|
416
|
+
/**
|
|
417
|
+
* Test seam (same shape as `_nativeCrashModuleOverride`): `undefined` =
|
|
418
|
+
* resolve from the native bridge, `null` = pretend the Identity bridge is
|
|
419
|
+
* absent. Needed because `require("react-native")` under vitest reaches the
|
|
420
|
+
* real Flow-syntax entry point and throws, so the bridge branch is otherwise
|
|
421
|
+
* unreachable and "we did read Identity" would pass vacuously.
|
|
422
|
+
*/
|
|
423
|
+
let _identityResolverOverride:
|
|
424
|
+
| (() => Promise<{ userId?: string | null }>)
|
|
425
|
+
| null
|
|
426
|
+
| undefined;
|
|
427
|
+
|
|
428
|
+
/** Budget for the send-time identity re-resolve. Kept well under
|
|
429
|
+
* FATAL_SEND_TIMEOUT_MS so a slow bridge can never eat the fatal path's
|
|
430
|
+
* one chance to deliver before the process dies. */
|
|
431
|
+
const IDENTITY_RESOLVE_TIMEOUT_MS = 250;
|
|
432
|
+
|
|
433
|
+
function getIdentityResolver():
|
|
434
|
+
| (() => Promise<{ userId?: string | null }>)
|
|
435
|
+
| null {
|
|
436
|
+
if (_identityResolverOverride !== undefined) {
|
|
437
|
+
return _identityResolverOverride;
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
// Lazy require for the same reason as `getNativeCrashModule()` — a static
|
|
441
|
+
// import would drag react-native's Flow entry point into plain-Node and
|
|
442
|
+
// vitest contexts where this module must stay importable.
|
|
443
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
444
|
+
const mod = require("../native/module") as {
|
|
445
|
+
getNativeModule: () => { identityCurrent?: () => Promise<unknown> };
|
|
446
|
+
};
|
|
447
|
+
const native = mod.getNativeModule();
|
|
448
|
+
if (typeof native?.identityCurrent !== "function") return null;
|
|
449
|
+
return () =>
|
|
450
|
+
native.identityCurrent!() as Promise<{ userId?: string | null }>;
|
|
451
|
+
} catch {
|
|
452
|
+
// Identity bridge not linked (bare Expo app, tests, plain Node).
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Issue a FRESH read of `Identity.current()` into `_identityUserId`.
|
|
459
|
+
*
|
|
460
|
+
* Never throws and never rejects — a failed read leaves the previous value in
|
|
461
|
+
* place rather than blanking a subject we already knew about.
|
|
462
|
+
*
|
|
463
|
+
* A generation counter discards the answer of any read that a newer read has
|
|
464
|
+
* already superseded, so two overlapping reads can never let the older one's
|
|
465
|
+
* (pre-`logIn`) snapshot win on arrival order.
|
|
466
|
+
*/
|
|
467
|
+
function startIdentityRead(): Promise<void> {
|
|
468
|
+
const resolver = getIdentityResolver();
|
|
469
|
+
if (!resolver) return Promise.resolve();
|
|
470
|
+
const generation = ++_identityGeneration;
|
|
471
|
+
// `p` is only read inside the `finally`, which cannot run before the
|
|
472
|
+
// assignment below completes — TS can't see that, hence the explicit
|
|
473
|
+
// `undefined` seed rather than a definite-assignment lie.
|
|
474
|
+
let p: Promise<void> | undefined;
|
|
475
|
+
p = (async () => {
|
|
476
|
+
try {
|
|
477
|
+
const snapshot = await resolver();
|
|
478
|
+
if (generation !== _identityGeneration) return; // superseded
|
|
479
|
+
const userId = snapshot?.userId;
|
|
480
|
+
_identityUserId =
|
|
481
|
+
typeof userId === "string" && userId.length > 0 ? userId : null;
|
|
482
|
+
} catch {
|
|
483
|
+
// Bridge hiccup — keep the last known subject.
|
|
484
|
+
} finally {
|
|
485
|
+
if (_identityRefresh === p) _identityRefresh = null;
|
|
486
|
+
}
|
|
487
|
+
})();
|
|
488
|
+
_identityRefresh = p;
|
|
489
|
+
return p;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Warm the cache. Joins a read that is already in flight — this is only ever
|
|
494
|
+
* used for the opportunistic warm-up at `init`, where an answer a few
|
|
495
|
+
* milliseconds old is fine.
|
|
496
|
+
*/
|
|
497
|
+
function refreshIdentitySubject(): Promise<void> {
|
|
498
|
+
return _identityRefresh ?? startIdentityRead();
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Send-time resolve, capped at `IDENTITY_RESOLVE_TIMEOUT_MS`.
|
|
503
|
+
*
|
|
504
|
+
* Deliberately does NOT join an in-flight read. `init` kicks a warm-up read
|
|
505
|
+
* immediately, and the host typically calls `Identity.logIn(...)` a moment
|
|
506
|
+
* later — a send that piggybacked on that warm-up would be answered with the
|
|
507
|
+
* pre-`logIn` snapshot and ship a subject-less crash, which is the exact bug
|
|
508
|
+
* this whole pull exists to kill. (Found by evaluating the built bundle in
|
|
509
|
+
* Node, not by the unit suite: a `settle()` in the test masked the race.)
|
|
510
|
+
* One fresh read per flush — not per event.
|
|
511
|
+
*
|
|
512
|
+
* Returns an already-resolved promise (and arms no timer) when there is no
|
|
513
|
+
* Identity bridge to ask — apps without the native module, and every test
|
|
514
|
+
* that has not installed the seam, must not pay a timer or a scheduling hop
|
|
515
|
+
* for a question that has no answer.
|
|
516
|
+
*/
|
|
517
|
+
function resolveIdentitySubjectNow(): Promise<void> {
|
|
518
|
+
if (getIdentityResolver() === null) return Promise.resolve();
|
|
519
|
+
const read = startIdentityRead();
|
|
520
|
+
return new Promise<void>((resolve) => {
|
|
521
|
+
const timer = setTimeout(resolve, IDENTITY_RESOLVE_TIMEOUT_MS);
|
|
522
|
+
void read.then(() => {
|
|
523
|
+
clearTimeout(timer);
|
|
524
|
+
resolve();
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Merge the explicit `setUser` override with the Identity-derived subject.
|
|
531
|
+
*
|
|
532
|
+
* Precedence is **field-by-field, explicit wins**:
|
|
533
|
+
* - `id` — `setUser`'s id if set, else `Identity.current().userId`
|
|
534
|
+
* - `email` — explicit only (Identity has no such field)
|
|
535
|
+
* - `username` — explicit only
|
|
536
|
+
*
|
|
537
|
+
* When nothing resolves, `null` is returned and the `user` key is omitted
|
|
538
|
+
* from the wire payload entirely — byte-identical to today for an app that
|
|
539
|
+
* neither logs in nor calls `setUser`.
|
|
540
|
+
*/
|
|
541
|
+
function resolveSubject(): SKCrashUser | null {
|
|
542
|
+
return mergeIdentityInto(_user);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Fill in the Identity-derived `id` on a subject that does not already carry
|
|
547
|
+
* one. Idempotent: a subject that already has an `id` is returned untouched,
|
|
548
|
+
* so applying this at build time and again at send time cannot change it.
|
|
549
|
+
*/
|
|
550
|
+
function mergeIdentityInto(
|
|
551
|
+
user: SKCrashUser | null | undefined
|
|
552
|
+
): SKCrashUser | null {
|
|
553
|
+
if (user?.id) return user;
|
|
554
|
+
const id = _identityUserId;
|
|
555
|
+
if (!id) return user ?? null;
|
|
556
|
+
return { ...(user ?? {}), id };
|
|
557
|
+
}
|
|
558
|
+
|
|
375
559
|
// MARK: - Pending Queue (memory + best-effort disk mirror)
|
|
376
560
|
|
|
377
561
|
function enqueueEvent(event: SKCrashEvent): void {
|
|
@@ -552,6 +736,9 @@ function buildSessionPayload(status: SessionPayload["status"]): SessionPayload {
|
|
|
552
736
|
const contexts = getStaticContexts();
|
|
553
737
|
const os = contexts.os as { name?: string; version?: string } | undefined;
|
|
554
738
|
const device = contexts.device as { model?: string } | undefined;
|
|
739
|
+
// Same pull as events: explicit `setUser` id wins, else the Identity
|
|
740
|
+
// subject. Synchronous, so it reads the cache `flush`/`init` keep warm.
|
|
741
|
+
const subject = resolveSubject();
|
|
555
742
|
return {
|
|
556
743
|
sessionId: _sessionId!,
|
|
557
744
|
status,
|
|
@@ -559,7 +746,7 @@ function buildSessionPayload(status: SessionPayload["status"]): SessionPayload {
|
|
|
559
746
|
errorsCount: _sessionErrorCount,
|
|
560
747
|
releaseVersion: _config?.release,
|
|
561
748
|
environment: _config?.environment,
|
|
562
|
-
...(
|
|
749
|
+
...(subject?.id ? { userId: subject.id } : {}),
|
|
563
750
|
...(os?.name ? { osName: os.name, osVersion: os.version } : {}),
|
|
564
751
|
...(device?.model ? { deviceModel: device.model } : {}),
|
|
565
752
|
};
|
|
@@ -600,6 +787,14 @@ async function postSessionPayload(
|
|
|
600
787
|
}
|
|
601
788
|
}
|
|
602
789
|
|
|
790
|
+
/**
|
|
791
|
+
* Deliberately has NO `await` before `postSessionPayload` — not even to
|
|
792
|
+
* re-resolve the identity. `endSession` nulls `_sessionId` and zeroes the
|
|
793
|
+
* error count the instant this function yields, and callers rely on the POST
|
|
794
|
+
* being issued in the same tick. Session payloads therefore take the subject
|
|
795
|
+
* from the warm cache only (see `buildSessionPayload`); the authoritative
|
|
796
|
+
* re-resolve happens on the event path in `flush`.
|
|
797
|
+
*/
|
|
603
798
|
async function sendSessionEvent(
|
|
604
799
|
status: "started" | "ended" | "crashed"
|
|
605
800
|
): Promise<void> {
|
|
@@ -1004,7 +1199,7 @@ function buildEvent(
|
|
|
1004
1199
|
...(_config?.dist ? { dist: _config.dist } : {}),
|
|
1005
1200
|
tags: { ..._tags },
|
|
1006
1201
|
extra: { ..._extra },
|
|
1007
|
-
user:
|
|
1202
|
+
user: resolveSubject(),
|
|
1008
1203
|
breadcrumbs: _breadcrumbs.snapshot(),
|
|
1009
1204
|
...partial,
|
|
1010
1205
|
contexts,
|
|
@@ -1390,6 +1585,11 @@ export const CrashReporting = {
|
|
|
1390
1585
|
...config,
|
|
1391
1586
|
};
|
|
1392
1587
|
_breadcrumbs = new RingBuffer(_config.maxBreadcrumbs!);
|
|
1588
|
+
// Warm the Identity subject cache. Fire-and-forget: crash reporting must
|
|
1589
|
+
// arm on the first frame and cannot wait on a bridge hop, and every send
|
|
1590
|
+
// path re-resolves anyway — this only shortens the window in which an
|
|
1591
|
+
// early event has to fall back to the send-time pull.
|
|
1592
|
+
void refreshIdentitySubject();
|
|
1393
1593
|
// Every global-slot install is best-effort. `initSwissKnife()` is normally
|
|
1394
1594
|
// called in module scope, so anything that throws here takes the host's
|
|
1395
1595
|
// root module down with it and the app never boots (v0.2.3 did exactly
|
|
@@ -1548,9 +1748,42 @@ export const CrashReporting = {
|
|
|
1548
1748
|
}
|
|
1549
1749
|
},
|
|
1550
1750
|
|
|
1751
|
+
/**
|
|
1752
|
+
* Explicitly override the crash subject.
|
|
1753
|
+
*
|
|
1754
|
+
* **You do not need this to get a subject onto crash reports.** The `id` is
|
|
1755
|
+
* pulled from `Identity.current()` at report time, so a plain
|
|
1756
|
+
* `Identity.logIn("u_42")` is enough — see the "Identity-derived Subject"
|
|
1757
|
+
* section in this file. `setUser` exists for the two things Identity cannot
|
|
1758
|
+
* express: extra fields, and a subject that deliberately differs from the
|
|
1759
|
+
* identity in effect.
|
|
1760
|
+
*
|
|
1761
|
+
* ## Precedence — field-by-field, explicit wins
|
|
1762
|
+
*
|
|
1763
|
+
* | field | source |
|
|
1764
|
+
* | ---------- | --------------------------------------------------- |
|
|
1765
|
+
* | `id` | this call's `id` if set, else `Identity.current().userId` |
|
|
1766
|
+
* | `email` | this call only — Identity has no email |
|
|
1767
|
+
* | `username` | this call only — Identity has no username |
|
|
1768
|
+
*
|
|
1769
|
+
* So `setUser({ email })` after `Identity.logIn("u_42")` yields
|
|
1770
|
+
* `{ id: "u_42", email }`, and `setUser({ id: "other" })` yields
|
|
1771
|
+
* `{ id: "other" }` — the identity is not allowed to overwrite an id the
|
|
1772
|
+
* host stated on purpose.
|
|
1773
|
+
*
|
|
1774
|
+
* `setUser(null)` clears **only this override**. It does not suppress the
|
|
1775
|
+
* Identity-derived id; call `Identity.logOut()` for that. (With no identity
|
|
1776
|
+
* and no override, `user` is omitted from the payload entirely, exactly as
|
|
1777
|
+
* before this pull existed.)
|
|
1778
|
+
*
|
|
1779
|
+
* Resolution happens at report time, never at login time, so the order of
|
|
1780
|
+
* `init` / `logIn` / `setUser` does not matter.
|
|
1781
|
+
*/
|
|
1551
1782
|
setUser(user: SKCrashUser | null): void {
|
|
1552
1783
|
_user = user;
|
|
1553
|
-
// Forward to native
|
|
1784
|
+
// Forward the EXPLICIT override to native, unmerged. The native crash
|
|
1785
|
+
// SDKs run the same precedence against their own `Identity.current`, so
|
|
1786
|
+
// sending a merged subject here would pin the id and defeat their pull.
|
|
1554
1787
|
const nativeMod = getNativeCrashModule();
|
|
1555
1788
|
if (nativeMod) {
|
|
1556
1789
|
try {
|
|
@@ -1599,12 +1832,23 @@ export const CrashReporting = {
|
|
|
1599
1832
|
const baseURL = _config.serverURL || "https://api.swiss-knife.dev";
|
|
1600
1833
|
const url = `${baseURL}/api/sdk/errors/event/batch`;
|
|
1601
1834
|
|
|
1835
|
+
// Re-resolve the subject at SEND time. `buildEvent` is synchronous and
|
|
1836
|
+
// can only read the cached snapshot, so an event captured before the
|
|
1837
|
+
// Identity bridge first answered would otherwise ship subject-less —
|
|
1838
|
+
// which is the whole failure mode this pull exists to kill. Doing it
|
|
1839
|
+
// here makes ordering irrelevant in both directions: `logIn` before
|
|
1840
|
+
// `init` and `init` before `logIn` both end up stamped. Bounded by
|
|
1841
|
+
// IDENTITY_RESOLVE_TIMEOUT_MS so the fatal path keeps its budget.
|
|
1842
|
+
await resolveIdentitySubjectNow();
|
|
1843
|
+
|
|
1602
1844
|
// The /api/sdk/errors/event/batch endpoint:
|
|
1603
1845
|
// - expects `timestamp` as an ISO string (SDK builds it as Date.now() number)
|
|
1604
1846
|
// - rejects `user: null` (schema marks `user` optional, not nullable)
|
|
1605
1847
|
// Coerce both on the wire so the internal SDK shape stays ergonomic.
|
|
1606
1848
|
const serialized = batch.map((e) => {
|
|
1607
|
-
const { user, ...rest } = e;
|
|
1849
|
+
const { user: builtUser, ...rest } = e;
|
|
1850
|
+
// Idempotent: leaves an id that build time already resolved alone.
|
|
1851
|
+
const user = mergeIdentityInto(builtUser);
|
|
1608
1852
|
return {
|
|
1609
1853
|
...rest,
|
|
1610
1854
|
...(user ? { user } : {}),
|
|
@@ -1718,6 +1962,12 @@ export const CrashReporting = {
|
|
|
1718
1962
|
_offlineScreenshots = [];
|
|
1719
1963
|
_screenshotCaptureOverride = undefined;
|
|
1720
1964
|
_nativeCrashModuleOverride = undefined;
|
|
1965
|
+
_identityUserId = null;
|
|
1966
|
+
_identityRefresh = null;
|
|
1967
|
+
// Bump the generation so a read still in flight cannot land after the
|
|
1968
|
+
// reset and repopulate the subject the reset just cleared.
|
|
1969
|
+
_identityGeneration++;
|
|
1970
|
+
_identityResolverOverride = undefined;
|
|
1721
1971
|
_appStateOverride = undefined;
|
|
1722
1972
|
if (_appStateSubscription) {
|
|
1723
1973
|
_appStateSubscription.remove();
|
|
@@ -1788,6 +2038,26 @@ export const CrashReporting = {
|
|
|
1788
2038
|
_nativeCrashModuleOverride = mod;
|
|
1789
2039
|
},
|
|
1790
2040
|
|
|
2041
|
+
/** Test seam: stand in for the native `Identity.current()` bridge call.
|
|
2042
|
+
* `null` = Identity bridge absent; `undefined` = resolve from react-native.
|
|
2043
|
+
* Clears the cached subject so the next resolve goes through the seam. */
|
|
2044
|
+
_setIdentityResolverForTests(
|
|
2045
|
+
resolver:
|
|
2046
|
+
| (() => Promise<{ userId?: string | null }>)
|
|
2047
|
+
| null
|
|
2048
|
+
| undefined
|
|
2049
|
+
): void {
|
|
2050
|
+
_identityResolverOverride = resolver;
|
|
2051
|
+
_identityUserId = null;
|
|
2052
|
+
_identityRefresh = null;
|
|
2053
|
+
_identityGeneration++;
|
|
2054
|
+
},
|
|
2055
|
+
|
|
2056
|
+
/** @internal Current Identity-derived subject id, for assertions. */
|
|
2057
|
+
_identitySubjectForTests(): string | null {
|
|
2058
|
+
return _identityUserId;
|
|
2059
|
+
},
|
|
2060
|
+
|
|
1791
2061
|
/** Test seam: stand in for react-native's `AppState`. */
|
|
1792
2062
|
_setAppStateForTests(
|
|
1793
2063
|
appState:
|