@sagepilot-ai/react-native-events-addon 0.5.0 → 0.5.1
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/dist/index.js +100 -39
- package/dist/index.mjs +100 -39
- package/package.json +6 -33
package/dist/index.js
CHANGED
|
@@ -27,6 +27,25 @@ module.exports = __toCommonJS(index_exports);
|
|
|
27
27
|
// src/installation.ts
|
|
28
28
|
var import_react_native2 = require("react-native");
|
|
29
29
|
|
|
30
|
+
// ../sdk-installation-client/src/capture-policy.ts
|
|
31
|
+
var kinds = /* @__PURE__ */ new Set(["navigation", "interaction", "network", "lifecycle", "screen"]);
|
|
32
|
+
var popupActions = /* @__PURE__ */ new Set(["popup_shown", "popup_submitted", "popup_dismissed"]);
|
|
33
|
+
var evidencePath = /^(?:data(?:\.[A-Za-z_][A-Za-z0-9_]*|\.[0-9]+){1,12}|(?:request|response):\$(?:\.[A-Za-z_][A-Za-z0-9_]*|\[[0-9]{1,2}\]){1,12})$/;
|
|
34
|
+
function object(value) {
|
|
35
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
function condition(value) {
|
|
38
|
+
if (!object(value) || typeof value.path !== "string" || value.path.length > 255 || !evidencePath.test(value.path)) return false;
|
|
39
|
+
if ("sha256" in value) return !("equals" in value) && typeof value.sha256 === "string" && /^[a-f0-9]{64}$/.test(value.sha256);
|
|
40
|
+
return "equals" in value && (value.equals === null || typeof value.equals === "boolean" || typeof value.equals === "number" && Number.isFinite(value.equals));
|
|
41
|
+
}
|
|
42
|
+
function validateCapturePolicy(value) {
|
|
43
|
+
if (!object(value) || !Array.isArray(value.rules) || value.rules.length > 32 || !Array.isArray(value.popup_actions) || value.popup_actions.length > 3 || !value.popup_actions.every((action) => popupActions.has(action)) || !(value.discovery === null || object(value.discovery) && typeof value.discovery.ends_at === "string" && Number.isFinite(Date.parse(value.discovery.ends_at))) || !value.rules.every((rule) => object(rule) && typeof rule.kind === "string" && kinds.has(rule.kind) && (rule.guard === null || rule.guard === "network_success" && rule.kind === "network" || rule.guard === "cart_click" && rule.kind === "interaction") && Array.isArray(rule.conditions) && rule.conditions.length >= 1 && rule.conditions.length <= 12 && rule.conditions.every(condition))) {
|
|
44
|
+
throw new Error("SDK session response contains an invalid capture policy.");
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
30
49
|
// ../sdk-installation-client/src/index.ts
|
|
31
50
|
var SagepilotInstallationError = class extends Error {
|
|
32
51
|
constructor(code, message, options = {}) {
|
|
@@ -145,31 +164,44 @@ var SagepilotInstallationClient = class {
|
|
|
145
164
|
this.startup = startup;
|
|
146
165
|
return startup;
|
|
147
166
|
}
|
|
148
|
-
/** Renews
|
|
167
|
+
/** Renews capture, using separate browser proof to recover a linked web visitor after expiry. */
|
|
149
168
|
async refresh(recoverAnonymous = false) {
|
|
150
169
|
if (this.pending) return this.pending;
|
|
151
170
|
if (!this.anonymousId) throw new Error("Start the installation client before renewing it.");
|
|
152
171
|
const generation = this.generation, prior = this.current;
|
|
153
172
|
const pending = (async () => {
|
|
154
173
|
let renewing = prior;
|
|
174
|
+
let resumed = false;
|
|
155
175
|
let value;
|
|
156
176
|
try {
|
|
157
177
|
value = await this.requestSession(prior?.session_token);
|
|
158
178
|
} catch (error) {
|
|
159
179
|
if (!recoverAnonymous || !prior || this.options.platform !== "web" || generation !== this.generation || !(error instanceof SagepilotInstallationError) || error.status !== 401) throw error;
|
|
160
180
|
renewing = null;
|
|
161
|
-
|
|
181
|
+
if (prior.browser_resume_token) {
|
|
182
|
+
try {
|
|
183
|
+
value = await this.requestSession(prior.browser_resume_token, true);
|
|
184
|
+
resumed = true;
|
|
185
|
+
} catch (resumeError) {
|
|
186
|
+
if (generation !== this.generation || !(resumeError instanceof SagepilotInstallationError) || ![401, 403, 422].includes(resumeError.status ?? 0)) throw resumeError;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!resumed) value = await this.requestSession();
|
|
162
190
|
}
|
|
163
191
|
const next = this.validateSession(value);
|
|
164
192
|
if (generation !== this.generation) throw new Error("SDK session startup was cancelled.");
|
|
165
|
-
if (renewing && (next.session_id !== renewing.session_id || next.customer_id !== renewing.customer_id) || !renewing && (next.customer_id !== null || next.session_id === prior?.session_id)) {
|
|
193
|
+
if (renewing && (next.session_id !== renewing.session_id || next.customer_id !== renewing.customer_id) || !renewing && (next.customer_id !== null || !resumed && next.session_id === prior?.session_id)) {
|
|
166
194
|
throw new Error("SDK session renewal changed capture ownership.");
|
|
167
195
|
}
|
|
168
|
-
if (
|
|
196
|
+
if (resumed && next.browser_resume_token !== prior?.browser_resume_token) {
|
|
197
|
+
throw new Error("SDK browser recovery returned invalid proof.");
|
|
198
|
+
}
|
|
199
|
+
if (renewing && !next.browser_resume_token) next.browser_resume_token = renewing.browser_resume_token;
|
|
200
|
+
if (prior && next.session_id !== prior.session_id) this.owners.delete(prior.session_id);
|
|
169
201
|
this.current = next;
|
|
170
202
|
this.remember(next);
|
|
171
203
|
try {
|
|
172
|
-
await this.options.storage?.setItem(this.storageKey, JSON.stringify(next));
|
|
204
|
+
await this.options.storage?.setItem(this.storageKey, JSON.stringify({ ...next, capture_policy: void 0 }));
|
|
173
205
|
} catch {
|
|
174
206
|
}
|
|
175
207
|
return { ...next };
|
|
@@ -179,18 +211,23 @@ var SagepilotInstallationClient = class {
|
|
|
179
211
|
this.pending = pending;
|
|
180
212
|
return pending;
|
|
181
213
|
}
|
|
182
|
-
/**
|
|
183
|
-
requestSession(token) {
|
|
214
|
+
/** Sends only the capture token for renewal or the distinct credential for browser recovery. */
|
|
215
|
+
requestSession(token, resumeBrowser = false) {
|
|
216
|
+
const web = this.options.platform === "web";
|
|
184
217
|
return this.request(`${this.apiHost}/platform-api/v1/sdk/sessions`, {
|
|
185
218
|
method: "POST",
|
|
186
|
-
headers:
|
|
219
|
+
headers: {
|
|
220
|
+
...token ? { Authorization: `Bearer ${token}` } : {},
|
|
221
|
+
...web ? { "X-Sagepilot-Capture-Policy": "1" } : {}
|
|
222
|
+
},
|
|
187
223
|
body: JSON.stringify({
|
|
188
224
|
key: `${this.workspaceId}:${this.installationId}`,
|
|
189
225
|
anonymous_id: this.anonymousId,
|
|
190
226
|
platform: this.options.platform,
|
|
191
|
-
...this.options.appId ? { app_id: this.options.appId } : {}
|
|
227
|
+
...this.options.appId ? { app_id: this.options.appId } : {},
|
|
228
|
+
...resumeBrowser ? { resume_browser: true } : {}
|
|
192
229
|
})
|
|
193
|
-
});
|
|
230
|
+
}, 15e3, web ? 192 * 1024 : 65536);
|
|
194
231
|
}
|
|
195
232
|
/** Supplies a short-lived native upload authorization only for the original capture owner. */
|
|
196
233
|
async authorize(owner) {
|
|
@@ -214,24 +251,42 @@ var SagepilotInstallationClient = class {
|
|
|
214
251
|
} };
|
|
215
252
|
}
|
|
216
253
|
/** Posts one bounded immutable batch; collectors own backoff and retain missing/retryable receipts. */
|
|
217
|
-
async upload(observations, owner) {
|
|
254
|
+
async upload(observations, owner, stillEligible) {
|
|
218
255
|
if (!observations.length || observations.length > 100) throw new Error("SDK batches require 1\u2013100 observations.");
|
|
219
256
|
for (const item of observations) {
|
|
220
257
|
if (item.owner.session_id !== owner.session_id || item.owner.customer_id !== owner.customer_id) throw new Error("SDK batch mixes capture ownership.");
|
|
221
258
|
}
|
|
222
259
|
const revision = this.uploadRevision;
|
|
223
260
|
const auth = await this.authorize(owner);
|
|
224
|
-
if (revision !== this.uploadRevision) throw new Error("SDK upload was cancelled.");
|
|
261
|
+
if (revision !== this.uploadRevision || stillEligible?.() === false) throw new Error("SDK upload was cancelled.");
|
|
225
262
|
const body = JSON.stringify({ schema_version: 1, observations });
|
|
226
263
|
if (new TextEncoder().encode(body).byteLength > 128 * 1024) throw new Error("SDK batch exceeds the upload limit.");
|
|
227
264
|
return validateSagepilotInstallationReceipt(await this.request(auth.url, { method: "POST", headers: auth.headers, body }), observations.map((item) => item.event_id));
|
|
228
265
|
}
|
|
229
|
-
/** Submits
|
|
266
|
+
/** Submits contact and adopts only a scoped customer session returned by the server. */
|
|
230
267
|
async submitContact(input) {
|
|
231
268
|
if (!UUID.test(input.submission_id) || !Number.isSafeInteger(input.popup_revision) || input.popup_revision < 1 || typeof input.marketing_consent !== "boolean") throw new Error("Invalid popup submission.");
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
269
|
+
if (this.transitioning) throw new Error("An SDK identity operation is already running.");
|
|
270
|
+
this.transitioning = true;
|
|
271
|
+
const generation = this.generation, prior = this.current;
|
|
272
|
+
try {
|
|
273
|
+
const { page, ...contact } = input;
|
|
274
|
+
const body = prior?.popup_submission_events === true ? {
|
|
275
|
+
...contact,
|
|
276
|
+
server_submission_event: true,
|
|
277
|
+
...prior.popup_submission_page === true && page ? { page } : {}
|
|
278
|
+
} : contact;
|
|
279
|
+
const result = await this.authenticated("/contact", body);
|
|
280
|
+
if (!result || !["recorded", "verification_required"].includes(result.status) || result.status === "verification_required" && (!result.challenge_id || !UUID.test(result.challenge_id) || !Number.isFinite(Date.parse(result.expires_at ?? "")))) throw new Error("Invalid popup submission response.");
|
|
281
|
+
const { session, ...acknowledgement } = result;
|
|
282
|
+
if (session != null) {
|
|
283
|
+
if (result.status !== "recorded" || prior?.customer_id !== null) throw new Error("Invalid popup customer session.");
|
|
284
|
+
await this.adoptTransition(session, prior, generation, true);
|
|
285
|
+
}
|
|
286
|
+
return acknowledgement;
|
|
287
|
+
} finally {
|
|
288
|
+
this.transitioning = false;
|
|
289
|
+
}
|
|
235
290
|
}
|
|
236
291
|
/** Adopts customer authority only from the server's completed verification operation. */
|
|
237
292
|
async verifyContact(challengeId, code) {
|
|
@@ -244,7 +299,7 @@ var SagepilotInstallationClient = class {
|
|
|
244
299
|
}
|
|
245
300
|
/** Keeps a bounded credential cache separate from observations so old anonymous batches retain their owner. */
|
|
246
301
|
remember(session) {
|
|
247
|
-
this.owners.set(session.session_id, { ...session });
|
|
302
|
+
this.owners.set(session.session_id, { ...session, capture_policy: void 0, browser_resume_token: void 0 });
|
|
248
303
|
while (this.owners.size > 4) this.owners.delete(this.owners.keys().next().value);
|
|
249
304
|
try {
|
|
250
305
|
void Promise.resolve(this.options.storage?.setItem(this.storageKey + ":owners", JSON.stringify([...this.owners.values()]))).catch(() => {
|
|
@@ -266,24 +321,29 @@ var SagepilotInstallationClient = class {
|
|
|
266
321
|
this.transitioning = true;
|
|
267
322
|
const generation = this.generation, prior = this.current;
|
|
268
323
|
try {
|
|
269
|
-
|
|
270
|
-
if (generation !== this.generation || !prior || next.session_id === prior.session_id || (identified ? next.customer_id === null : next.customer_id !== null)) throw new Error("Invalid SDK identity transition.");
|
|
271
|
-
this.generation++;
|
|
272
|
-
this.cancelPending();
|
|
273
|
-
this.pending = null;
|
|
274
|
-
if (path === "/logout") this.owners.delete(prior.session_id);
|
|
275
|
-
else this.remember(prior);
|
|
276
|
-
this.current = next;
|
|
277
|
-
this.remember(next);
|
|
278
|
-
try {
|
|
279
|
-
await this.options.storage?.setItem(this.storageKey, JSON.stringify(next));
|
|
280
|
-
} catch {
|
|
281
|
-
}
|
|
282
|
-
return { ...next };
|
|
324
|
+
return await this.adoptTransition(await this.authenticated(path, body), prior, generation, identified);
|
|
283
325
|
} finally {
|
|
284
326
|
this.transitioning = false;
|
|
285
327
|
}
|
|
286
328
|
}
|
|
329
|
+
/** Installs one new server owner and retains earlier credentials unless the owner logged out. */
|
|
330
|
+
async adoptTransition(value, prior, generation, identified) {
|
|
331
|
+
const next = this.validateSession(value);
|
|
332
|
+
if (generation !== this.generation || !prior || next.session_id === prior.session_id || (identified ? next.customer_id === null : next.customer_id !== null)) throw new Error("Invalid SDK identity transition.");
|
|
333
|
+
next.capture_policy ?? (next.capture_policy = prior.capture_policy);
|
|
334
|
+
this.generation++;
|
|
335
|
+
this.cancelPending();
|
|
336
|
+
this.pending = null;
|
|
337
|
+
if (identified) this.remember(prior);
|
|
338
|
+
else this.owners.delete(prior.session_id);
|
|
339
|
+
this.current = next;
|
|
340
|
+
this.remember(next);
|
|
341
|
+
try {
|
|
342
|
+
await this.options.storage?.setItem(this.storageKey, JSON.stringify({ ...next, capture_policy: void 0 }));
|
|
343
|
+
} catch {
|
|
344
|
+
}
|
|
345
|
+
return { ...next };
|
|
346
|
+
}
|
|
287
347
|
/** Cancels requests on permission/ownership boundaries while preserving the current session for safe renewal. */
|
|
288
348
|
cancelPending() {
|
|
289
349
|
this.uploadRevision++;
|
|
@@ -299,18 +359,19 @@ var SagepilotInstallationClient = class {
|
|
|
299
359
|
this.startup = null;
|
|
300
360
|
this.owners.clear();
|
|
301
361
|
}
|
|
302
|
-
/** Rejects foreign
|
|
362
|
+
/** Rejects foreign or malformed session responses before credentials are used. */
|
|
303
363
|
validateSession(value) {
|
|
304
364
|
const session = value;
|
|
305
|
-
if (!session || session.workspace_id !== this.workspaceId || session.installation_id !== this.installationId || session.platform !== this.options.platform || session.anonymous_id !== this.anonymousId || session.customer_id !== null && (typeof session.customer_id !== "string" || !UUID.test(session.customer_id)) || !UUID.test(session.session_id) || typeof session.session_token !== "string" || session.session_token.length > 16384 || !session.session_token || !Number.isFinite(Date.parse(session.expires_at)) || typeof session.collection_enabled !== "boolean" || session.visitor_cookie_enabled !== void 0 && typeof session.visitor_cookie_enabled !== "boolean" || !Number.isSafeInteger(session.revision)) {
|
|
365
|
+
if (!session || session.workspace_id !== this.workspaceId || session.installation_id !== this.installationId || session.platform !== this.options.platform || session.anonymous_id !== this.anonymousId || session.customer_id !== null && (typeof session.customer_id !== "string" || !UUID.test(session.customer_id)) || !UUID.test(session.session_id) || typeof session.session_token !== "string" || session.session_token.length > 16384 || !session.session_token || session.browser_resume_token != null && (session.platform !== "web" || typeof session.browser_resume_token !== "string" || !session.browser_resume_token || session.browser_resume_token.length > 8192) || !Number.isFinite(Date.parse(session.expires_at)) || typeof session.collection_enabled !== "boolean" || session.visitor_cookie_enabled !== void 0 && typeof session.visitor_cookie_enabled !== "boolean" || !Number.isSafeInteger(session.revision)) {
|
|
306
366
|
throw new Error("SDK session response has invalid scope or ownership.");
|
|
307
367
|
}
|
|
308
368
|
const url = safeURL(session.ingestion_url);
|
|
309
369
|
if (url.pathname !== "/events/v1/sdk/observations") throw new Error("SDK ingestion route is invalid.");
|
|
310
|
-
|
|
370
|
+
const capturePolicy = session.platform === "web" && session.capture_policy != null ? validateCapturePolicy(session.capture_policy) : void 0;
|
|
371
|
+
return { ...session, capture_policy: capturePolicy };
|
|
311
372
|
}
|
|
312
373
|
/** Performs bounded SDK HTTP requests without retrying mutations or following credential redirects. */
|
|
313
|
-
async request(url, init, timeoutMs = 15e3) {
|
|
374
|
+
async request(url, init, timeoutMs = 15e3, maxResponseBytes = 65536) {
|
|
314
375
|
const controller = new AbortController();
|
|
315
376
|
this.requests.add(controller);
|
|
316
377
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -325,7 +386,7 @@ var SagepilotInstallationClient = class {
|
|
|
325
386
|
});
|
|
326
387
|
throw new SagepilotInstallationError("invalid_response", `SDK request failed with HTTP ${response.status}.`, { status: response.status });
|
|
327
388
|
}
|
|
328
|
-
if (Number(response.headers.get("content-length") ?? 0) >
|
|
389
|
+
if (Number(response.headers.get("content-length") ?? 0) > maxResponseBytes) throw new Error("SDK response exceeds limit.");
|
|
329
390
|
let text = "";
|
|
330
391
|
if (response.body?.getReader) {
|
|
331
392
|
const reader = response.body.getReader(), decoder = new TextDecoder();
|
|
@@ -335,7 +396,7 @@ var SagepilotInstallationClient = class {
|
|
|
335
396
|
const part = await reader.read();
|
|
336
397
|
if (part.done) break;
|
|
337
398
|
bytes += part.value.byteLength;
|
|
338
|
-
if (bytes >
|
|
399
|
+
if (bytes > maxResponseBytes) {
|
|
339
400
|
await reader.cancel();
|
|
340
401
|
throw new Error("SDK response exceeds limit.");
|
|
341
402
|
}
|
|
@@ -347,7 +408,7 @@ var SagepilotInstallationClient = class {
|
|
|
347
408
|
}
|
|
348
409
|
} else {
|
|
349
410
|
text = await response.text();
|
|
350
|
-
if (text.
|
|
411
|
+
if (new TextEncoder().encode(text).byteLength > maxResponseBytes) throw new Error("SDK response exceeds limit.");
|
|
351
412
|
}
|
|
352
413
|
return JSON.parse(text);
|
|
353
414
|
} catch (error) {
|
|
@@ -648,7 +709,7 @@ function observeNetwork(options) {
|
|
|
648
709
|
|
|
649
710
|
// package.json
|
|
650
711
|
var name = "@sagepilot-ai/react-native-events-addon";
|
|
651
|
-
var version = "0.5.
|
|
712
|
+
var version = "0.5.1";
|
|
652
713
|
|
|
653
714
|
// src/collector.ts
|
|
654
715
|
var activeCollector = null;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
// src/installation.ts
|
|
2
2
|
import { NativeModules as NativeModules2, Platform as Platform2 } from "react-native";
|
|
3
3
|
|
|
4
|
+
// ../sdk-installation-client/src/capture-policy.ts
|
|
5
|
+
var kinds = /* @__PURE__ */ new Set(["navigation", "interaction", "network", "lifecycle", "screen"]);
|
|
6
|
+
var popupActions = /* @__PURE__ */ new Set(["popup_shown", "popup_submitted", "popup_dismissed"]);
|
|
7
|
+
var evidencePath = /^(?:data(?:\.[A-Za-z_][A-Za-z0-9_]*|\.[0-9]+){1,12}|(?:request|response):\$(?:\.[A-Za-z_][A-Za-z0-9_]*|\[[0-9]{1,2}\]){1,12})$/;
|
|
8
|
+
function object(value) {
|
|
9
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
function condition(value) {
|
|
12
|
+
if (!object(value) || typeof value.path !== "string" || value.path.length > 255 || !evidencePath.test(value.path)) return false;
|
|
13
|
+
if ("sha256" in value) return !("equals" in value) && typeof value.sha256 === "string" && /^[a-f0-9]{64}$/.test(value.sha256);
|
|
14
|
+
return "equals" in value && (value.equals === null || typeof value.equals === "boolean" || typeof value.equals === "number" && Number.isFinite(value.equals));
|
|
15
|
+
}
|
|
16
|
+
function validateCapturePolicy(value) {
|
|
17
|
+
if (!object(value) || !Array.isArray(value.rules) || value.rules.length > 32 || !Array.isArray(value.popup_actions) || value.popup_actions.length > 3 || !value.popup_actions.every((action) => popupActions.has(action)) || !(value.discovery === null || object(value.discovery) && typeof value.discovery.ends_at === "string" && Number.isFinite(Date.parse(value.discovery.ends_at))) || !value.rules.every((rule) => object(rule) && typeof rule.kind === "string" && kinds.has(rule.kind) && (rule.guard === null || rule.guard === "network_success" && rule.kind === "network" || rule.guard === "cart_click" && rule.kind === "interaction") && Array.isArray(rule.conditions) && rule.conditions.length >= 1 && rule.conditions.length <= 12 && rule.conditions.every(condition))) {
|
|
18
|
+
throw new Error("SDK session response contains an invalid capture policy.");
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
4
23
|
// ../sdk-installation-client/src/index.ts
|
|
5
24
|
var SagepilotInstallationError = class extends Error {
|
|
6
25
|
constructor(code, message, options = {}) {
|
|
@@ -119,31 +138,44 @@ var SagepilotInstallationClient = class {
|
|
|
119
138
|
this.startup = startup;
|
|
120
139
|
return startup;
|
|
121
140
|
}
|
|
122
|
-
/** Renews
|
|
141
|
+
/** Renews capture, using separate browser proof to recover a linked web visitor after expiry. */
|
|
123
142
|
async refresh(recoverAnonymous = false) {
|
|
124
143
|
if (this.pending) return this.pending;
|
|
125
144
|
if (!this.anonymousId) throw new Error("Start the installation client before renewing it.");
|
|
126
145
|
const generation = this.generation, prior = this.current;
|
|
127
146
|
const pending = (async () => {
|
|
128
147
|
let renewing = prior;
|
|
148
|
+
let resumed = false;
|
|
129
149
|
let value;
|
|
130
150
|
try {
|
|
131
151
|
value = await this.requestSession(prior?.session_token);
|
|
132
152
|
} catch (error) {
|
|
133
153
|
if (!recoverAnonymous || !prior || this.options.platform !== "web" || generation !== this.generation || !(error instanceof SagepilotInstallationError) || error.status !== 401) throw error;
|
|
134
154
|
renewing = null;
|
|
135
|
-
|
|
155
|
+
if (prior.browser_resume_token) {
|
|
156
|
+
try {
|
|
157
|
+
value = await this.requestSession(prior.browser_resume_token, true);
|
|
158
|
+
resumed = true;
|
|
159
|
+
} catch (resumeError) {
|
|
160
|
+
if (generation !== this.generation || !(resumeError instanceof SagepilotInstallationError) || ![401, 403, 422].includes(resumeError.status ?? 0)) throw resumeError;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!resumed) value = await this.requestSession();
|
|
136
164
|
}
|
|
137
165
|
const next = this.validateSession(value);
|
|
138
166
|
if (generation !== this.generation) throw new Error("SDK session startup was cancelled.");
|
|
139
|
-
if (renewing && (next.session_id !== renewing.session_id || next.customer_id !== renewing.customer_id) || !renewing && (next.customer_id !== null || next.session_id === prior?.session_id)) {
|
|
167
|
+
if (renewing && (next.session_id !== renewing.session_id || next.customer_id !== renewing.customer_id) || !renewing && (next.customer_id !== null || !resumed && next.session_id === prior?.session_id)) {
|
|
140
168
|
throw new Error("SDK session renewal changed capture ownership.");
|
|
141
169
|
}
|
|
142
|
-
if (
|
|
170
|
+
if (resumed && next.browser_resume_token !== prior?.browser_resume_token) {
|
|
171
|
+
throw new Error("SDK browser recovery returned invalid proof.");
|
|
172
|
+
}
|
|
173
|
+
if (renewing && !next.browser_resume_token) next.browser_resume_token = renewing.browser_resume_token;
|
|
174
|
+
if (prior && next.session_id !== prior.session_id) this.owners.delete(prior.session_id);
|
|
143
175
|
this.current = next;
|
|
144
176
|
this.remember(next);
|
|
145
177
|
try {
|
|
146
|
-
await this.options.storage?.setItem(this.storageKey, JSON.stringify(next));
|
|
178
|
+
await this.options.storage?.setItem(this.storageKey, JSON.stringify({ ...next, capture_policy: void 0 }));
|
|
147
179
|
} catch {
|
|
148
180
|
}
|
|
149
181
|
return { ...next };
|
|
@@ -153,18 +185,23 @@ var SagepilotInstallationClient = class {
|
|
|
153
185
|
this.pending = pending;
|
|
154
186
|
return pending;
|
|
155
187
|
}
|
|
156
|
-
/**
|
|
157
|
-
requestSession(token) {
|
|
188
|
+
/** Sends only the capture token for renewal or the distinct credential for browser recovery. */
|
|
189
|
+
requestSession(token, resumeBrowser = false) {
|
|
190
|
+
const web = this.options.platform === "web";
|
|
158
191
|
return this.request(`${this.apiHost}/platform-api/v1/sdk/sessions`, {
|
|
159
192
|
method: "POST",
|
|
160
|
-
headers:
|
|
193
|
+
headers: {
|
|
194
|
+
...token ? { Authorization: `Bearer ${token}` } : {},
|
|
195
|
+
...web ? { "X-Sagepilot-Capture-Policy": "1" } : {}
|
|
196
|
+
},
|
|
161
197
|
body: JSON.stringify({
|
|
162
198
|
key: `${this.workspaceId}:${this.installationId}`,
|
|
163
199
|
anonymous_id: this.anonymousId,
|
|
164
200
|
platform: this.options.platform,
|
|
165
|
-
...this.options.appId ? { app_id: this.options.appId } : {}
|
|
201
|
+
...this.options.appId ? { app_id: this.options.appId } : {},
|
|
202
|
+
...resumeBrowser ? { resume_browser: true } : {}
|
|
166
203
|
})
|
|
167
|
-
});
|
|
204
|
+
}, 15e3, web ? 192 * 1024 : 65536);
|
|
168
205
|
}
|
|
169
206
|
/** Supplies a short-lived native upload authorization only for the original capture owner. */
|
|
170
207
|
async authorize(owner) {
|
|
@@ -188,24 +225,42 @@ var SagepilotInstallationClient = class {
|
|
|
188
225
|
} };
|
|
189
226
|
}
|
|
190
227
|
/** Posts one bounded immutable batch; collectors own backoff and retain missing/retryable receipts. */
|
|
191
|
-
async upload(observations, owner) {
|
|
228
|
+
async upload(observations, owner, stillEligible) {
|
|
192
229
|
if (!observations.length || observations.length > 100) throw new Error("SDK batches require 1\u2013100 observations.");
|
|
193
230
|
for (const item of observations) {
|
|
194
231
|
if (item.owner.session_id !== owner.session_id || item.owner.customer_id !== owner.customer_id) throw new Error("SDK batch mixes capture ownership.");
|
|
195
232
|
}
|
|
196
233
|
const revision = this.uploadRevision;
|
|
197
234
|
const auth = await this.authorize(owner);
|
|
198
|
-
if (revision !== this.uploadRevision) throw new Error("SDK upload was cancelled.");
|
|
235
|
+
if (revision !== this.uploadRevision || stillEligible?.() === false) throw new Error("SDK upload was cancelled.");
|
|
199
236
|
const body = JSON.stringify({ schema_version: 1, observations });
|
|
200
237
|
if (new TextEncoder().encode(body).byteLength > 128 * 1024) throw new Error("SDK batch exceeds the upload limit.");
|
|
201
238
|
return validateSagepilotInstallationReceipt(await this.request(auth.url, { method: "POST", headers: auth.headers, body }), observations.map((item) => item.event_id));
|
|
202
239
|
}
|
|
203
|
-
/** Submits
|
|
240
|
+
/** Submits contact and adopts only a scoped customer session returned by the server. */
|
|
204
241
|
async submitContact(input) {
|
|
205
242
|
if (!UUID.test(input.submission_id) || !Number.isSafeInteger(input.popup_revision) || input.popup_revision < 1 || typeof input.marketing_consent !== "boolean") throw new Error("Invalid popup submission.");
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
243
|
+
if (this.transitioning) throw new Error("An SDK identity operation is already running.");
|
|
244
|
+
this.transitioning = true;
|
|
245
|
+
const generation = this.generation, prior = this.current;
|
|
246
|
+
try {
|
|
247
|
+
const { page, ...contact } = input;
|
|
248
|
+
const body = prior?.popup_submission_events === true ? {
|
|
249
|
+
...contact,
|
|
250
|
+
server_submission_event: true,
|
|
251
|
+
...prior.popup_submission_page === true && page ? { page } : {}
|
|
252
|
+
} : contact;
|
|
253
|
+
const result = await this.authenticated("/contact", body);
|
|
254
|
+
if (!result || !["recorded", "verification_required"].includes(result.status) || result.status === "verification_required" && (!result.challenge_id || !UUID.test(result.challenge_id) || !Number.isFinite(Date.parse(result.expires_at ?? "")))) throw new Error("Invalid popup submission response.");
|
|
255
|
+
const { session, ...acknowledgement } = result;
|
|
256
|
+
if (session != null) {
|
|
257
|
+
if (result.status !== "recorded" || prior?.customer_id !== null) throw new Error("Invalid popup customer session.");
|
|
258
|
+
await this.adoptTransition(session, prior, generation, true);
|
|
259
|
+
}
|
|
260
|
+
return acknowledgement;
|
|
261
|
+
} finally {
|
|
262
|
+
this.transitioning = false;
|
|
263
|
+
}
|
|
209
264
|
}
|
|
210
265
|
/** Adopts customer authority only from the server's completed verification operation. */
|
|
211
266
|
async verifyContact(challengeId, code) {
|
|
@@ -218,7 +273,7 @@ var SagepilotInstallationClient = class {
|
|
|
218
273
|
}
|
|
219
274
|
/** Keeps a bounded credential cache separate from observations so old anonymous batches retain their owner. */
|
|
220
275
|
remember(session) {
|
|
221
|
-
this.owners.set(session.session_id, { ...session });
|
|
276
|
+
this.owners.set(session.session_id, { ...session, capture_policy: void 0, browser_resume_token: void 0 });
|
|
222
277
|
while (this.owners.size > 4) this.owners.delete(this.owners.keys().next().value);
|
|
223
278
|
try {
|
|
224
279
|
void Promise.resolve(this.options.storage?.setItem(this.storageKey + ":owners", JSON.stringify([...this.owners.values()]))).catch(() => {
|
|
@@ -240,24 +295,29 @@ var SagepilotInstallationClient = class {
|
|
|
240
295
|
this.transitioning = true;
|
|
241
296
|
const generation = this.generation, prior = this.current;
|
|
242
297
|
try {
|
|
243
|
-
|
|
244
|
-
if (generation !== this.generation || !prior || next.session_id === prior.session_id || (identified ? next.customer_id === null : next.customer_id !== null)) throw new Error("Invalid SDK identity transition.");
|
|
245
|
-
this.generation++;
|
|
246
|
-
this.cancelPending();
|
|
247
|
-
this.pending = null;
|
|
248
|
-
if (path === "/logout") this.owners.delete(prior.session_id);
|
|
249
|
-
else this.remember(prior);
|
|
250
|
-
this.current = next;
|
|
251
|
-
this.remember(next);
|
|
252
|
-
try {
|
|
253
|
-
await this.options.storage?.setItem(this.storageKey, JSON.stringify(next));
|
|
254
|
-
} catch {
|
|
255
|
-
}
|
|
256
|
-
return { ...next };
|
|
298
|
+
return await this.adoptTransition(await this.authenticated(path, body), prior, generation, identified);
|
|
257
299
|
} finally {
|
|
258
300
|
this.transitioning = false;
|
|
259
301
|
}
|
|
260
302
|
}
|
|
303
|
+
/** Installs one new server owner and retains earlier credentials unless the owner logged out. */
|
|
304
|
+
async adoptTransition(value, prior, generation, identified) {
|
|
305
|
+
const next = this.validateSession(value);
|
|
306
|
+
if (generation !== this.generation || !prior || next.session_id === prior.session_id || (identified ? next.customer_id === null : next.customer_id !== null)) throw new Error("Invalid SDK identity transition.");
|
|
307
|
+
next.capture_policy ?? (next.capture_policy = prior.capture_policy);
|
|
308
|
+
this.generation++;
|
|
309
|
+
this.cancelPending();
|
|
310
|
+
this.pending = null;
|
|
311
|
+
if (identified) this.remember(prior);
|
|
312
|
+
else this.owners.delete(prior.session_id);
|
|
313
|
+
this.current = next;
|
|
314
|
+
this.remember(next);
|
|
315
|
+
try {
|
|
316
|
+
await this.options.storage?.setItem(this.storageKey, JSON.stringify({ ...next, capture_policy: void 0 }));
|
|
317
|
+
} catch {
|
|
318
|
+
}
|
|
319
|
+
return { ...next };
|
|
320
|
+
}
|
|
261
321
|
/** Cancels requests on permission/ownership boundaries while preserving the current session for safe renewal. */
|
|
262
322
|
cancelPending() {
|
|
263
323
|
this.uploadRevision++;
|
|
@@ -273,18 +333,19 @@ var SagepilotInstallationClient = class {
|
|
|
273
333
|
this.startup = null;
|
|
274
334
|
this.owners.clear();
|
|
275
335
|
}
|
|
276
|
-
/** Rejects foreign
|
|
336
|
+
/** Rejects foreign or malformed session responses before credentials are used. */
|
|
277
337
|
validateSession(value) {
|
|
278
338
|
const session = value;
|
|
279
|
-
if (!session || session.workspace_id !== this.workspaceId || session.installation_id !== this.installationId || session.platform !== this.options.platform || session.anonymous_id !== this.anonymousId || session.customer_id !== null && (typeof session.customer_id !== "string" || !UUID.test(session.customer_id)) || !UUID.test(session.session_id) || typeof session.session_token !== "string" || session.session_token.length > 16384 || !session.session_token || !Number.isFinite(Date.parse(session.expires_at)) || typeof session.collection_enabled !== "boolean" || session.visitor_cookie_enabled !== void 0 && typeof session.visitor_cookie_enabled !== "boolean" || !Number.isSafeInteger(session.revision)) {
|
|
339
|
+
if (!session || session.workspace_id !== this.workspaceId || session.installation_id !== this.installationId || session.platform !== this.options.platform || session.anonymous_id !== this.anonymousId || session.customer_id !== null && (typeof session.customer_id !== "string" || !UUID.test(session.customer_id)) || !UUID.test(session.session_id) || typeof session.session_token !== "string" || session.session_token.length > 16384 || !session.session_token || session.browser_resume_token != null && (session.platform !== "web" || typeof session.browser_resume_token !== "string" || !session.browser_resume_token || session.browser_resume_token.length > 8192) || !Number.isFinite(Date.parse(session.expires_at)) || typeof session.collection_enabled !== "boolean" || session.visitor_cookie_enabled !== void 0 && typeof session.visitor_cookie_enabled !== "boolean" || !Number.isSafeInteger(session.revision)) {
|
|
280
340
|
throw new Error("SDK session response has invalid scope or ownership.");
|
|
281
341
|
}
|
|
282
342
|
const url = safeURL(session.ingestion_url);
|
|
283
343
|
if (url.pathname !== "/events/v1/sdk/observations") throw new Error("SDK ingestion route is invalid.");
|
|
284
|
-
|
|
344
|
+
const capturePolicy = session.platform === "web" && session.capture_policy != null ? validateCapturePolicy(session.capture_policy) : void 0;
|
|
345
|
+
return { ...session, capture_policy: capturePolicy };
|
|
285
346
|
}
|
|
286
347
|
/** Performs bounded SDK HTTP requests without retrying mutations or following credential redirects. */
|
|
287
|
-
async request(url, init, timeoutMs = 15e3) {
|
|
348
|
+
async request(url, init, timeoutMs = 15e3, maxResponseBytes = 65536) {
|
|
288
349
|
const controller = new AbortController();
|
|
289
350
|
this.requests.add(controller);
|
|
290
351
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -299,7 +360,7 @@ var SagepilotInstallationClient = class {
|
|
|
299
360
|
});
|
|
300
361
|
throw new SagepilotInstallationError("invalid_response", `SDK request failed with HTTP ${response.status}.`, { status: response.status });
|
|
301
362
|
}
|
|
302
|
-
if (Number(response.headers.get("content-length") ?? 0) >
|
|
363
|
+
if (Number(response.headers.get("content-length") ?? 0) > maxResponseBytes) throw new Error("SDK response exceeds limit.");
|
|
303
364
|
let text = "";
|
|
304
365
|
if (response.body?.getReader) {
|
|
305
366
|
const reader = response.body.getReader(), decoder = new TextDecoder();
|
|
@@ -309,7 +370,7 @@ var SagepilotInstallationClient = class {
|
|
|
309
370
|
const part = await reader.read();
|
|
310
371
|
if (part.done) break;
|
|
311
372
|
bytes += part.value.byteLength;
|
|
312
|
-
if (bytes >
|
|
373
|
+
if (bytes > maxResponseBytes) {
|
|
313
374
|
await reader.cancel();
|
|
314
375
|
throw new Error("SDK response exceeds limit.");
|
|
315
376
|
}
|
|
@@ -321,7 +382,7 @@ var SagepilotInstallationClient = class {
|
|
|
321
382
|
}
|
|
322
383
|
} else {
|
|
323
384
|
text = await response.text();
|
|
324
|
-
if (text.
|
|
385
|
+
if (new TextEncoder().encode(text).byteLength > maxResponseBytes) throw new Error("SDK response exceeds limit.");
|
|
325
386
|
}
|
|
326
387
|
return JSON.parse(text);
|
|
327
388
|
} catch (error) {
|
|
@@ -622,7 +683,7 @@ function observeNetwork(options) {
|
|
|
622
683
|
|
|
623
684
|
// package.json
|
|
624
685
|
var name = "@sagepilot-ai/react-native-events-addon";
|
|
625
|
-
var version = "0.5.
|
|
686
|
+
var version = "0.5.1";
|
|
626
687
|
|
|
627
688
|
// src/collector.ts
|
|
628
689
|
var activeCollector = null;
|
package/package.json
CHANGED
|
@@ -1,43 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sagepilot-ai/react-native-events-addon",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Optional native Android/iOS automatic app observations for Sagepilot",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
8
|
"react-native": "./dist/index.js",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
|
-
"exports": {
|
|
11
|
-
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.mjs",
|
|
14
|
-
"require": "./dist/index.js"
|
|
15
|
-
},
|
|
16
|
-
"./package.json": "./package.json"
|
|
17
|
-
},
|
|
18
|
-
"files": [
|
|
19
|
-
"android/build.gradle",
|
|
20
|
-
"android/src",
|
|
21
|
-
"ios",
|
|
22
|
-
"SagepilotEventsAddon.podspec",
|
|
23
|
-
"react-native.config.js",
|
|
24
|
-
"dist",
|
|
25
|
-
"README.md",
|
|
26
|
-
"LICENSE.md"
|
|
27
|
-
],
|
|
10
|
+
"exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.js" }, "./package.json": "./package.json" },
|
|
11
|
+
"files": ["android/build.gradle", "android/src", "ios", "SagepilotEventsAddon.podspec", "react-native.config.js", "dist", "README.md", "LICENSE.md"],
|
|
28
12
|
"sideEffects": false,
|
|
29
|
-
"scripts": {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"prepublishOnly": "npm run typecheck && npm run build"
|
|
33
|
-
},
|
|
34
|
-
"peerDependencies": {
|
|
35
|
-
"react": ">=18",
|
|
36
|
-
"react-native": ">=0.81.5"
|
|
37
|
-
},
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@sagepilot-ai/sdk-installation-client": "0.0.0",
|
|
40
|
-
"tsup": "^8.3.5",
|
|
41
|
-
"typescript": "^5.7.2"
|
|
42
|
-
}
|
|
13
|
+
"scripts": { "build": "tsup", "typecheck": "tsc --noEmit", "prepublishOnly": "npm run typecheck && npm run build" },
|
|
14
|
+
"peerDependencies": { "react": ">=18", "react-native": ">=0.81.5" },
|
|
15
|
+
"devDependencies": { "@sagepilot-ai/sdk-installation-client": "0.0.0", "tsup": "^8.3.5", "typescript": "^5.7.2" }
|
|
43
16
|
}
|