@goliapkg/sentori-react-native 1.0.0-rc.8 → 1.0.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.
Files changed (60) hide show
  1. package/bin/sentori-rn-upload-source-bundle.cjs +193 -0
  2. package/lib/capture.d.ts.map +1 -1
  3. package/lib/capture.js +9 -0
  4. package/lib/capture.js.map +1 -1
  5. package/lib/heartbeat.d.ts +9 -0
  6. package/lib/heartbeat.d.ts.map +1 -0
  7. package/lib/heartbeat.js +140 -0
  8. package/lib/heartbeat.js.map +1 -0
  9. package/lib/index.d.ts +15 -0
  10. package/lib/index.d.ts.map +1 -1
  11. package/lib/index.js +15 -0
  12. package/lib/index.js.map +1 -1
  13. package/lib/init.d.ts +6 -0
  14. package/lib/init.d.ts.map +1 -1
  15. package/lib/init.js +18 -0
  16. package/lib/init.js.map +1 -1
  17. package/lib/install-id.d.ts +17 -0
  18. package/lib/install-id.d.ts.map +1 -0
  19. package/lib/install-id.js +125 -0
  20. package/lib/install-id.js.map +1 -0
  21. package/lib/navigation.d.ts +1 -0
  22. package/lib/navigation.d.ts.map +1 -1
  23. package/lib/navigation.js +20 -0
  24. package/lib/navigation.js.map +1 -1
  25. package/lib/replay.d.ts +27 -9
  26. package/lib/replay.d.ts.map +1 -1
  27. package/lib/replay.js +209 -167
  28. package/lib/replay.js.map +1 -1
  29. package/lib/report-security.d.ts +40 -0
  30. package/lib/report-security.d.ts.map +1 -0
  31. package/lib/report-security.js +159 -0
  32. package/lib/report-security.js.map +1 -0
  33. package/lib/track.d.ts +34 -0
  34. package/lib/track.d.ts.map +1 -0
  35. package/lib/track.js +98 -0
  36. package/lib/track.js.map +1 -0
  37. package/lib/transport.d.ts +15 -0
  38. package/lib/transport.d.ts.map +1 -1
  39. package/lib/transport.js +23 -0
  40. package/lib/transport.js.map +1 -1
  41. package/lib/trust-score.d.ts +20 -0
  42. package/lib/trust-score.d.ts.map +1 -0
  43. package/lib/trust-score.js +151 -0
  44. package/lib/trust-score.js.map +1 -0
  45. package/package.json +6 -2
  46. package/src/__tests__/install-id.test.ts +60 -0
  47. package/src/__tests__/replay-encoding.test.ts +237 -0
  48. package/src/__tests__/report-security.test.ts +106 -0
  49. package/src/__tests__/track.test.ts +91 -0
  50. package/src/capture.ts +8 -0
  51. package/src/heartbeat.ts +158 -0
  52. package/src/index.ts +24 -0
  53. package/src/init.ts +23 -0
  54. package/src/install-id.ts +146 -0
  55. package/src/navigation.ts +26 -0
  56. package/src/replay.ts +258 -176
  57. package/src/report-security.ts +165 -0
  58. package/src/track.ts +114 -0
  59. package/src/transport.ts +35 -0
  60. package/src/trust-score.ts +176 -0
@@ -0,0 +1,165 @@
1
+ // v1.1 chunk S2 — security event reporting.
2
+ //
3
+ // `sentori.reportSecurity(kind, data)` POSTs a single security event
4
+ // to `/v1/security:report`. Helpers wrap common kinds with the right
5
+ // payload shape so dashboards can rely on it without coordinating
6
+ // schemas with every host app.
7
+ //
8
+ // Why a separate API from `captureException` / `track`: security
9
+ // reports have different retention + access patterns (the trust
10
+ // scoring engine in S3 reads them on a hot path) and conflating
11
+ // them with errors would pollute issue grouping. Single endpoint,
12
+ // no batching: pin mismatches and root-detection signals are
13
+ // low-volume by nature (one per app-lifetime in most cases).
14
+
15
+ import { getConfig, isInitialized } from './config';
16
+ import { peekInstallId } from './install-id';
17
+ import { getCurrentUserId } from './capture';
18
+
19
+ const SDK_VERSION = '0.0.0';
20
+
21
+ export type SecurityReportData = Record<string, unknown>;
22
+
23
+ /**
24
+ * Report an arbitrary security signal. Fire-and-forget; resolves
25
+ * with the server-assigned event id on success or `null` on any
26
+ * failure (network down, server unhappy, SDK not initialised).
27
+ *
28
+ * Use the dedicated helpers (`reportPinMismatch`, future
29
+ * `reportRootDetected`, …) when their shape applies — the dashboard
30
+ * renders kind-specific panels off the well-known kinds.
31
+ */
32
+ export async function reportSecurity(
33
+ kind: string,
34
+ data: SecurityReportData = {},
35
+ ): Promise<null | string> {
36
+ if (!isInitialized()) return null;
37
+ if (typeof kind !== 'string' || kind.length === 0 || kind.length > 100) {
38
+ return null;
39
+ }
40
+ if (Object.keys(data).length > 40) return null;
41
+
42
+ const config = getConfig();
43
+ if (!config) return null;
44
+ const body = {
45
+ kind,
46
+ data,
47
+ ts: new Date().toISOString(),
48
+ userId: getCurrentUserId(),
49
+ installId: peekInstallId() ?? undefined,
50
+ release: config.release,
51
+ environment: config.environment,
52
+ };
53
+ try {
54
+ const resp = await fetch(`${config.ingestUrl}/v1/security:report`, {
55
+ body: JSON.stringify(body),
56
+ headers: {
57
+ Authorization: `Bearer ${config.token}`,
58
+ 'Content-Type': 'application/json',
59
+ 'Sentori-Sdk': `react-native/${SDK_VERSION}`,
60
+ },
61
+ method: 'POST',
62
+ });
63
+ if (!resp.ok) return null;
64
+ const parsed = (await resp.json().catch(() => null)) as null | { id?: string };
65
+ return parsed?.id ?? null;
66
+ } catch {
67
+ return null;
68
+ }
69
+ }
70
+
71
+ /**
72
+ * v1.1 chunk S4 — link the current device's user / install to a
73
+ * federated identity (e.g. a Google sub or Apple `sub`). Idempotent;
74
+ * call on every sign-in. Posts to `/v1/security/link`. The dashboard
75
+ * uses this to stitch the same user across projects in the Posture
76
+ * cross-project view.
77
+ *
78
+ * Privacy: only the opaque OAuth `subject` value travels. Never pass
79
+ * the email, display name, avatar, or any other identity attribute.
80
+ */
81
+ export async function linkFederatedIdentity(args: {
82
+ provider: string;
83
+ subject: string;
84
+ userId?: string;
85
+ }): Promise<boolean> {
86
+ if (!args || typeof args.provider !== 'string' || args.provider.length === 0) return false;
87
+ if (typeof args.subject !== 'string' || args.subject.length === 0) return false;
88
+ if (!isInitialized()) return false;
89
+ const config = getConfig();
90
+ if (!config) return false;
91
+ const installId = peekInstallId() ?? undefined;
92
+ const body = {
93
+ provider: args.provider,
94
+ subject: args.subject,
95
+ userId: args.userId ?? getCurrentUserId(),
96
+ installId,
97
+ };
98
+ try {
99
+ const resp = await fetch(`${config.ingestUrl}/v1/security/link`, {
100
+ body: JSON.stringify(body),
101
+ headers: {
102
+ Authorization: `Bearer ${config.token}`,
103
+ 'Content-Type': 'application/json',
104
+ 'Sentori-Sdk': `react-native/${SDK_VERSION}`,
105
+ },
106
+ method: 'POST',
107
+ });
108
+ return resp.ok;
109
+ } catch {
110
+ return false;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * TLS certificate pin mismatch — caller observed a server cert that
116
+ * didn't match the configured pin set. Posts `kind = 'pin.mismatch'`
117
+ * with the expected + observed pin (or hash) so the dashboard's
118
+ * Pin anomaly panel can cluster reports by server.
119
+ */
120
+ export async function reportPinMismatch(args: {
121
+ expected: string;
122
+ observed: string;
123
+ /** Hostname the SDK was connecting to. Used by the dashboard to
124
+ * cluster reports per server. */
125
+ serverName: string;
126
+ }): Promise<null | string> {
127
+ if (!args || typeof args.serverName !== 'string' || args.serverName.length === 0) {
128
+ return null;
129
+ }
130
+ // The serverName rides on the top-level envelope (column-typed on
131
+ // the server) so dashboard queries don't need to crack the data
132
+ // JSONB. We still echo it inside `data` for self-contained payloads.
133
+ if (!isInitialized()) return null;
134
+ const config = getConfig();
135
+ if (!config) return null;
136
+ const body = {
137
+ kind: 'pin.mismatch',
138
+ serverName: args.serverName,
139
+ ts: new Date().toISOString(),
140
+ userId: getCurrentUserId(),
141
+ installId: peekInstallId() ?? undefined,
142
+ release: config.release,
143
+ environment: config.environment,
144
+ data: {
145
+ expected: args.expected,
146
+ observed: args.observed,
147
+ },
148
+ };
149
+ try {
150
+ const resp = await fetch(`${config.ingestUrl}/v1/security:report`, {
151
+ body: JSON.stringify(body),
152
+ headers: {
153
+ Authorization: `Bearer ${config.token}`,
154
+ 'Content-Type': 'application/json',
155
+ 'Sentori-Sdk': `react-native/${SDK_VERSION}`,
156
+ },
157
+ method: 'POST',
158
+ });
159
+ if (!resp.ok) return null;
160
+ const parsed = (await resp.json().catch(() => null)) as null | { id?: string };
161
+ return parsed?.id ?? null;
162
+ } catch {
163
+ return null;
164
+ }
165
+ }
package/src/track.ts ADDED
@@ -0,0 +1,114 @@
1
+ // v1.1 chunk B — analytics `track` events on the SDK side.
2
+ //
3
+ // `sentori.track(name, props?)` pushes a single typed analytics event
4
+ // into a fixed-size ring. A timer flushes the ring every 30 s (or
5
+ // when the buffer hits 500) to `POST /v1/track:batch`. Best-effort;
6
+ // a flush failure drops the batch — analytics aren't critical
7
+ // telemetry.
8
+ //
9
+ // Why not one fetch per event: a busy app emitting `$pageview` +
10
+ // custom funnel events every nav would saturate the JS thread and
11
+ // the host's outbound connection pool. The same batching shape the
12
+ // metrics module uses keeps `track()` cheap to call from render
13
+ // hooks.
14
+ //
15
+ // Auto-pageview (`$pageview`) is emitted from `useTraceNavigation`
16
+ // in navigation.ts whenever react-navigation swaps the active route.
17
+ // Hosts that don't use react-navigation can call
18
+ // `sentori.track('$pageview', { route: 'Cart' })` themselves.
19
+
20
+ import { getCurrentUserId } from './capture';
21
+ import { getConfig, isInitialized } from './config';
22
+ import { sendTrackBatch } from './transport';
23
+
24
+ export type TrackProps = Record<string, unknown>;
25
+
26
+ export type TrackEvent = {
27
+ environment?: string;
28
+ name: string;
29
+ props?: TrackProps;
30
+ release?: string;
31
+ route?: string;
32
+ sessionId?: string;
33
+ ts: string;
34
+ userId?: string;
35
+ };
36
+
37
+ const MAX_BUFFER = 500;
38
+ const NAME_MAX = 200;
39
+ const PROPS_KEYS_MAX = 40;
40
+ const FLUSH_INTERVAL_MS = 30_000;
41
+
42
+ let _buf: TrackEvent[] = [];
43
+ let _timer: null | ReturnType<typeof setInterval> = null;
44
+
45
+ /**
46
+ * Record a typed analytics event. Cheap to call from render hooks —
47
+ * pushes into a 500-slot ring drained every 30 s (or on overflow) by
48
+ * the transport flusher.
49
+ *
50
+ * Reserved names start with `$` (e.g. `$pageview`) and are emitted by
51
+ * the SDK itself; you can still call `track('$pageview', …)` from app
52
+ * code to backfill routes the auto-instrumentation missed.
53
+ *
54
+ * Server caps: name ≤ 200 chars, ≤ 40 prop keys. Calls exceeding the
55
+ * cap are dropped client-side (no throw) so app code can fire-and-
56
+ * forget without try/catch.
57
+ */
58
+ export function track(name: string, props?: TrackProps, route?: string): void {
59
+ if (!isInitialized()) return;
60
+ if (typeof name !== 'string' || name.length === 0 || name.length > NAME_MAX) {
61
+ return;
62
+ }
63
+ if (props && Object.keys(props).length > PROPS_KEYS_MAX) {
64
+ return;
65
+ }
66
+ const config = getConfig();
67
+ const ev: TrackEvent = {
68
+ name,
69
+ props,
70
+ release: config?.release,
71
+ environment: config?.environment,
72
+ route,
73
+ ts: new Date().toISOString(),
74
+ userId: getCurrentUserId(),
75
+ };
76
+ _buf.push(ev);
77
+ if (_buf.length >= MAX_BUFFER) {
78
+ void flushTrack();
79
+ }
80
+ }
81
+
82
+ export async function flushTrack(): Promise<void> {
83
+ if (_buf.length === 0) return;
84
+ const config = getConfig();
85
+ if (!config) return;
86
+ const batch = _buf;
87
+ _buf = [];
88
+ await sendTrackBatch(config.ingestUrl, config.token, batch);
89
+ }
90
+
91
+ /**
92
+ * Start the 30 s flush timer. Called once from `init()`. Idempotent.
93
+ * `__resetTrackForTests` is exposed for vitest / bun:test teardown.
94
+ */
95
+ export function startTrackTimer(): void {
96
+ if (_timer !== null) return;
97
+ _timer = setInterval(() => {
98
+ void flushTrack();
99
+ }, FLUSH_INTERVAL_MS);
100
+ // Don't keep the process alive solely for this timer.
101
+ (_timer as unknown as { unref?: () => void }).unref?.();
102
+ }
103
+
104
+ export function __peekTrackBuffer(): readonly TrackEvent[] {
105
+ return _buf;
106
+ }
107
+
108
+ export function __resetTrackForTests(): void {
109
+ if (_timer !== null) {
110
+ clearInterval(_timer);
111
+ _timer = null;
112
+ }
113
+ _buf = [];
114
+ }
package/src/transport.ts CHANGED
@@ -263,6 +263,41 @@ export const sendSessionPing = async (
263
263
  }
264
264
  };
265
265
 
266
+ /**
267
+ * v1.1 chunk B — flush a batched set of `sentori.track()` analytics
268
+ * events. The host SDK batches calls into a fixed-size ring drained
269
+ * every 30 s. Best-effort; a failure drops the batch.
270
+ */
271
+ export const sendTrackBatch = async (
272
+ ingestUrl: string,
273
+ token: string,
274
+ events: Array<{
275
+ environment?: string;
276
+ name: string;
277
+ props?: Record<string, unknown>;
278
+ release?: string;
279
+ route?: string;
280
+ sessionId?: string;
281
+ ts: string;
282
+ userId?: string;
283
+ }>,
284
+ ): Promise<void> => {
285
+ if (events.length === 0) return;
286
+ try {
287
+ await fetch(`${ingestUrl}/v1/track:batch`, {
288
+ body: JSON.stringify({ events }),
289
+ headers: {
290
+ Authorization: `Bearer ${token}`,
291
+ 'Content-Type': 'application/json',
292
+ 'Sentori-Sdk': `react-native/${SDK_VERSION}`,
293
+ },
294
+ method: 'POST',
295
+ });
296
+ } catch {
297
+ // best-effort
298
+ }
299
+ };
300
+
266
301
  /**
267
302
  * v0.8.3 — flush a batched set of custom metrics. The host SDK
268
303
  * batches recordMetric() calls into a fixed-size ring (drained on
@@ -0,0 +1,176 @@
1
+ // v1.1 chunk S3 — `sentori.queryTrustScore()` for the host app.
2
+ //
3
+ // Returns the device's current trust score (0–100, higher = healthier)
4
+ // plus the per-kind signal mix that produced it. The host app uses
5
+ // this to decide whether to step up authentication, decline a
6
+ // purchase, or simply log a soft warning.
7
+ //
8
+ // Caching:
9
+ // L1 — process-memory, ~30 s. Same process should not re-fetch on
10
+ // every screen render; the score moves on a "minutes" cadence.
11
+ // L2 — install-id-derived AsyncStorage entry, ~5 minutes. Lets the
12
+ // host paint a cached score on cold start without waiting for
13
+ // the network. Stale entries are still served; the SDK
14
+ // re-fetches in the background.
15
+ //
16
+ // Fail-soft posture: if the network's down or the SDK isn't
17
+ // initialised, returns the cached value (if any), else the safe
18
+ // default (score = 100). The host should never see a thrown error
19
+ // from this call — the score system is supposed to be invisible.
20
+
21
+ import { isAnyNativeModuleLinked } from './native-loader';
22
+ import { getConfig, isInitialized } from './config';
23
+ import { getInstallId } from './install-id';
24
+
25
+ const SDK_VERSION = '0.0.0';
26
+ const STORAGE_KEY = '@sentori/trust-score';
27
+ const L1_TTL_MS = 30_000;
28
+ const L2_TTL_MS = 5 * 60_000;
29
+
30
+ export type TrustSignal = {
31
+ count: number;
32
+ kind: string;
33
+ weight: number;
34
+ };
35
+
36
+ export type TrustScore = {
37
+ computedAt: string;
38
+ installId: string;
39
+ score: number;
40
+ signals: TrustSignal[];
41
+ };
42
+
43
+ type CacheEntry = { fetchedAt: number; score: TrustScore };
44
+
45
+ type AsyncStorageLike = {
46
+ getItem: (key: string) => Promise<null | string>;
47
+ setItem: (key: string, value: string) => Promise<void>;
48
+ };
49
+
50
+ let _l1: null | CacheEntry = null;
51
+ let _inflight: null | Promise<TrustScore> = null;
52
+
53
+ function loadAsyncStorage(): AsyncStorageLike | null {
54
+ if (!isAnyNativeModuleLinked(['RNCAsyncStorage', 'AsyncStorageModule'])) return null;
55
+ try {
56
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
57
+ const mod = require('@react-native-async-storage/async-storage') as {
58
+ default?: AsyncStorageLike;
59
+ };
60
+ return mod.default ?? (mod as unknown as AsyncStorageLike);
61
+ } catch {
62
+ return null;
63
+ }
64
+ }
65
+
66
+ function safeDefault(installId: string): TrustScore {
67
+ return {
68
+ computedAt: new Date().toISOString(),
69
+ installId,
70
+ score: 100,
71
+ signals: [],
72
+ };
73
+ }
74
+
75
+ async function readL2(): Promise<null | CacheEntry> {
76
+ const storage = loadAsyncStorage();
77
+ if (!storage) return null;
78
+ try {
79
+ const raw = await storage.getItem(STORAGE_KEY);
80
+ if (!raw) return null;
81
+ const parsed = JSON.parse(raw) as CacheEntry;
82
+ if (
83
+ typeof parsed?.fetchedAt !== 'number' ||
84
+ typeof parsed?.score?.score !== 'number'
85
+ ) {
86
+ return null;
87
+ }
88
+ return parsed;
89
+ } catch {
90
+ return null;
91
+ }
92
+ }
93
+
94
+ async function writeL2(entry: CacheEntry): Promise<void> {
95
+ const storage = loadAsyncStorage();
96
+ if (!storage) return;
97
+ try {
98
+ await storage.setItem(STORAGE_KEY, JSON.stringify(entry));
99
+ } catch {
100
+ // best-effort
101
+ }
102
+ }
103
+
104
+ async function fetchFresh(installId: string): Promise<TrustScore> {
105
+ const config = getConfig();
106
+ if (!config) return safeDefault(installId);
107
+ try {
108
+ const resp = await fetch(
109
+ `${config.ingestUrl}/v1/security/score?installId=${encodeURIComponent(installId)}`,
110
+ {
111
+ headers: {
112
+ Authorization: `Bearer ${config.token}`,
113
+ 'Sentori-Sdk': `react-native/${SDK_VERSION}`,
114
+ },
115
+ method: 'GET',
116
+ }
117
+ );
118
+ if (!resp.ok) return safeDefault(installId);
119
+ const parsed = (await resp.json()) as TrustScore;
120
+ return parsed;
121
+ } catch {
122
+ return safeDefault(installId);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Resolve the device's trust score. Always resolves — never throws,
128
+ * never rejects. The two-layer cache means the first call after cold
129
+ * start usually serves a < 1 ms result while a background refresh
130
+ * keeps the value current.
131
+ */
132
+ export async function queryTrustScore(): Promise<TrustScore> {
133
+ if (!isInitialized()) {
134
+ const id = await getInstallId();
135
+ return safeDefault(id);
136
+ }
137
+ // L1 hit
138
+ const now = Date.now();
139
+ if (_l1 && now - _l1.fetchedAt < L1_TTL_MS) {
140
+ return _l1.score;
141
+ }
142
+ // Coalesce concurrent calls onto the same inflight fetch.
143
+ if (_inflight) return _inflight;
144
+
145
+ _inflight = (async () => {
146
+ try {
147
+ const installId = await getInstallId();
148
+ // L2 hit — serve immediately, kick off background refresh
149
+ const l2 = await readL2();
150
+ if (l2 && now - l2.fetchedAt < L2_TTL_MS && l2.score.installId === installId) {
151
+ _l1 = l2;
152
+ // background refresh (don't block caller)
153
+ void (async () => {
154
+ const fresh = await fetchFresh(installId);
155
+ const entry: CacheEntry = { fetchedAt: Date.now(), score: fresh };
156
+ _l1 = entry;
157
+ await writeL2(entry);
158
+ })();
159
+ return l2.score;
160
+ }
161
+ const fresh = await fetchFresh(installId);
162
+ const entry: CacheEntry = { fetchedAt: Date.now(), score: fresh };
163
+ _l1 = entry;
164
+ await writeL2(entry);
165
+ return fresh;
166
+ } finally {
167
+ _inflight = null;
168
+ }
169
+ })();
170
+ return _inflight;
171
+ }
172
+
173
+ export function __resetTrustScoreForTests(): void {
174
+ _l1 = null;
175
+ _inflight = null;
176
+ }