@nagoorkani/session-capture-core 0.1.3 → 0.1.5

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/core.d.ts CHANGED
@@ -17,7 +17,7 @@ import { type CaptureEvent, type EventBatch, type EventType, type InitOptions, t
17
17
  * still showed a bug fixed in 0.1.1 could not be told apart from one that had
18
18
  * never been updated, from the dashboard or from the logs.
19
19
  */
20
- export declare const SDK_VERSION = "0.1.3";
20
+ export declare const SDK_VERSION = "0.1.5";
21
21
  /**
22
22
  * Idle bound. Nothing for this long and the next event opens a new session.
23
23
  *
@@ -111,6 +111,12 @@ export declare class SdkCore {
111
111
  applyConfig(config: RemoteConfig): void;
112
112
  /** Events discarded because remote config disabled capture. Diagnostics. */
113
113
  private dropOnKill;
114
+ /**
115
+ * Who `identify` last named, repeated on every later `session_start` so a
116
+ * rotated session is not anonymous. Memory only: a relaunch or reload
117
+ * forgets it, and the host calls `identify` again once it knows the user.
118
+ */
119
+ private user;
114
120
  get droppedOnKill(): number;
115
121
  /** The config currently in force. */
116
122
  get remoteConfig(): RemoteConfig;
@@ -159,6 +165,8 @@ export declare class SdkCore {
159
165
  mark(name: string): void;
160
166
  /** Record time from SDK load to app-ready. */
161
167
  markReady(): void;
168
+ /** Attach an end user to this session and every later one in this run. */
169
+ identify(userId: string, traits?: Record<string, unknown>): void;
162
170
  screen(name: string): void;
163
171
  /** Called by the platform lifecycle glue. */
164
172
  onAppStateChange(state: 'active' | 'background'): void;
package/dist/core.js CHANGED
@@ -21,7 +21,7 @@ import { DEFAULT_CONFIG, SCHEMA_VERSION, } from "./types.js";
21
21
  * still showed a bug fixed in 0.1.1 could not be told apart from one that had
22
22
  * never been updated, from the dashboard or from the logs.
23
23
  */
24
- export const SDK_VERSION = '0.1.3';
24
+ export const SDK_VERSION = '0.1.5';
25
25
  /**
26
26
  * Idle bound. Nothing for this long and the next event opens a new session.
27
27
  *
@@ -61,6 +61,12 @@ export class SdkCore {
61
61
  this.sessionListeners = new Set();
62
62
  /** Events discarded because remote config disabled capture. Diagnostics. */
63
63
  this.dropOnKill = 0;
64
+ /**
65
+ * Who `identify` last named, repeated on every later `session_start` so a
66
+ * rotated session is not anonymous. Memory only: a relaunch or reload
67
+ * forgets it, and the host calls `identify` again once it knows the user.
68
+ */
69
+ this.user = null;
64
70
  this.now = deps.now ?? Date.now;
65
71
  this.onFlush = deps.onFlush ?? (() => { });
66
72
  this.deviceContext = deps.deviceContext ?? (() => ({}));
@@ -83,6 +89,11 @@ export class SdkCore {
83
89
  this.options = options;
84
90
  this.enabled = options.enabled ?? true;
85
91
  this.started = true;
92
+ if (options.appUserId) {
93
+ this.user = options.appUserEmail
94
+ ? { userId: options.appUserId, email: options.appUserEmail }
95
+ : { userId: options.appUserId };
96
+ }
86
97
  this.sessions = new SessionManager({
87
98
  timeoutMs: SESSION_TIMEOUT_MS,
88
99
  initial: this.restoreSession(),
@@ -102,6 +113,12 @@ export class SdkCore {
102
113
  // its session row from this event (section 20); without it the first
103
114
  // screen view would arrive for a session nothing has announced.
104
115
  this.startSessionIfNeeded();
116
+ // A session restored across a reload announces nothing, so name the user
117
+ // explicitly. ponytail: a fresh session gets this twice (session_start and
118
+ // here); harmless, the worker takes the latest.
119
+ if (this.user) {
120
+ this.capture('user_action', { action: 'identify', ...this.user });
121
+ }
105
122
  }
106
123
  /**
107
124
  * Whether capture is currently active.
@@ -329,6 +346,12 @@ export class SdkCore {
329
346
  markReady() {
330
347
  this.capture('performance', { ...timeToReady(this.now()) });
331
348
  }
349
+ /** Attach an end user to this session and every later one in this run. */
350
+ identify(userId, traits) {
351
+ const email = typeof traits?.email === 'string' ? traits.email : undefined;
352
+ this.user = email ? { userId, email } : { userId };
353
+ this.capture('user_action', { action: 'identify', userId, ...traits });
354
+ }
332
355
  screen(name) {
333
356
  if (!this.isEnabled() || !this.sessions)
334
357
  return;
@@ -390,7 +413,7 @@ export class SdkCore {
390
413
  announce(sessionId) {
391
414
  // Device context rides on session_start — it is the only event the worker
392
415
  // reads it from, and every field is optional there.
393
- this.capture('session_start', { ...this.deviceContext() });
416
+ this.capture('session_start', { ...this.deviceContext(), ...this.user });
394
417
  for (const listener of this.sessionListeners) {
395
418
  try {
396
419
  listener(sessionId);
package/dist/network.js CHANGED
@@ -36,7 +36,16 @@
36
36
  * counted twice. `whatwg-fetch` does not, and it is the only XHR-backed fetch
37
37
  * in play.
38
38
  */
39
- import { redact } from "./redact.js";
39
+ import { maskValues, redact } from "./redact.js";
40
+ function asRequest(input) {
41
+ const candidate = input;
42
+ return candidate !== null &&
43
+ typeof candidate === 'object' &&
44
+ typeof candidate.url === 'string' &&
45
+ typeof candidate.method === 'string'
46
+ ? candidate
47
+ : null;
48
+ }
40
49
  const PATCH_MARKER = '__sessionCapturePatched';
41
50
  /** Strip query strings before capture: they routinely carry tokens. */
42
51
  export function sanitizeUrl(url) {
@@ -298,7 +307,8 @@ function captureBody(raw, contentType, maskedKeys) {
298
307
  const type = mediaType(contentType);
299
308
  if (type === 'application/json' || type.endsWith('+json')) {
300
309
  try {
301
- return truncate(JSON.stringify(redact(JSON.parse(raw), maskedKeys)));
310
+ // Masked before truncating, so a cut cannot leave half an email behind unmasked.
311
+ return truncate(maskValues(JSON.stringify(redact(JSON.parse(raw), maskedKeys))));
302
312
  }
303
313
  catch {
304
314
  // Never fall back to the raw text: a body that does not parse is a body
@@ -316,7 +326,8 @@ function captureBody(raw, contentType, maskedKeys) {
316
326
  });
317
327
  const out = new URLSearchParams();
318
328
  for (const [key, value] of Object.entries(redact(fields, maskedKeys))) {
319
- out.append(key, value);
329
+ // Per value: encoding turns `@` into `%40`, which the scan would miss.
330
+ out.append(key, maskValues(value));
320
331
  }
321
332
  return truncate(out.toString());
322
333
  }
@@ -437,6 +448,20 @@ function readResponseBody(request, contentType, maskedKeys) {
437
448
  // A host XHR that does not implement responseType at all reads as text,
438
449
  // rather than as `[not captured: undefined response]`.
439
450
  const type = request.responseType ?? '';
451
+ if (type === 'json') {
452
+ // Already parsed by the browser, so `responseText` throws. Re-serialised
453
+ // and sent through the same redaction as a text JSON body; null is a
454
+ // body that did not parse.
455
+ try {
456
+ const parsed = request.response;
457
+ if (parsed === null || parsed === undefined)
458
+ return '[not captured: malformed json]';
459
+ return captureBody(JSON.stringify(parsed), 'application/json', maskedKeys);
460
+ }
461
+ catch {
462
+ return undefined;
463
+ }
464
+ }
440
465
  if (type !== '' && type !== 'text') {
441
466
  return `[not captured: ${type} response]`;
442
467
  }
@@ -477,12 +502,30 @@ function installFetchCapture(options) {
477
502
  return () => { };
478
503
  }
479
504
  const patched = async function patchedFetch(input, init) {
480
- const url = typeof input === 'string' ? input : String(input);
481
- const method = (init?.method ?? 'GET').toUpperCase();
505
+ // `String(request)` is "[object Request]", which is what this used to
506
+ // record — with GET and no headers — for every call made through one.
507
+ const request = asRequest(input);
508
+ const url = request ? request.url : typeof input === 'string' ? input : String(input);
509
+ const method = (init?.method ?? request?.method ?? 'GET').toUpperCase();
510
+ // `init.headers` replaces a Request's headers rather than merging, as in fetch itself.
511
+ const headers = init?.headers ?? request?.headers;
482
512
  if (!shouldCapture(url, options.ingestEndpoint, options.urlDenyList)) {
483
513
  return original(input, init);
484
514
  }
485
515
  const started = now();
516
+ // A Request's body is a stream the call below consumes, so a copy has to
517
+ // be taken first. Read asynchronously and attached at report time; the
518
+ // app's response is never held for it.
519
+ let requestBodyRead = null;
520
+ if (options.captureBodies && init?.body === undefined && request?.body && typeof request.clone === 'function') {
521
+ try {
522
+ requestBodyRead = request.clone().text();
523
+ }
524
+ catch {
525
+ requestBodyRead = null;
526
+ }
527
+ }
528
+ const requestType = headerValue(headersToObject(headers) ?? {}, 'content-type');
486
529
  // An XHR-backed fetch does its open/send inside this call, synchronously,
487
530
  // so `servedByXhr` is settled by the time the promise comes back.
488
531
  let pending;
@@ -496,7 +539,7 @@ function installFetchCapture(options) {
496
539
  }
497
540
  const claimed = servedByXhr;
498
541
  const requestBody = options.captureBodies
499
- ? captureBody(init?.body, headerValue(headersToObject(init?.headers) ?? {}, 'content-type'), options.maskedKeys)
542
+ ? captureBody(init?.body, requestType, options.maskedKeys)
500
543
  : undefined;
501
544
  try {
502
545
  const response = await pending;
@@ -507,7 +550,7 @@ function installFetchCapture(options) {
507
550
  method,
508
551
  status: response.status,
509
552
  durationMs: now() - started,
510
- requestHeaders: redactHeaders(init?.headers, options.maskedKeys),
553
+ requestHeaders: redactHeaders(headers, options.maskedKeys),
511
554
  responseHeaders: redactHeaders(readHeaders(response), options.maskedKeys),
512
555
  };
513
556
  if (requestBody !== undefined)
@@ -516,11 +559,16 @@ function installFetchCapture(options) {
516
559
  // that goes through a clone and is reported when it resolves. The app
517
560
  // gets its response either way, now, untouched.
518
561
  const copy = options.captureBodies ? cloneForReading(response) : null;
519
- if (copy) {
520
- copy.then((text) => {
521
- event.responseBody = captureBody(text, headerValue(readHeaders(response) ?? {}, 'content-type'), options.maskedKeys);
522
- report(options, event);
523
- }, () => report(options, event));
562
+ if (copy || requestBodyRead) {
563
+ const responseType = headerValue(readHeaders(response) ?? {}, 'content-type');
564
+ void Promise.all([
565
+ requestBodyRead?.then((text) => {
566
+ event.requestBody = captureBody(text, requestType, options.maskedKeys);
567
+ }, () => { }),
568
+ copy?.then((text) => {
569
+ event.responseBody = captureBody(text, responseType, options.maskedKeys);
570
+ }, () => { }),
571
+ ]).then(() => report(options, event));
524
572
  }
525
573
  else {
526
574
  report(options, event);
@@ -534,7 +582,7 @@ function installFetchCapture(options) {
534
582
  method,
535
583
  durationMs: now() - started,
536
584
  error: error instanceof Error ? error.message : 'network error',
537
- requestHeaders: redactHeaders(init?.headers, options.maskedKeys),
585
+ requestHeaders: redactHeaders(headers, options.maskedKeys),
538
586
  };
539
587
  if (requestBody !== undefined)
540
588
  event.requestBody = requestBody;
package/dist/redact.d.ts CHANGED
@@ -17,3 +17,19 @@ export declare const REDACTED = "[redacted]";
17
17
  * shipping a new app build.
18
18
  */
19
19
  export declare function redact(value: unknown, maskedKeys?: readonly string[]): unknown;
20
+ /**
21
+ * Mask personal data by *value*, inside captured request and response bodies.
22
+ *
23
+ * Key redaction cannot see `{"note": "mail me at ada@example.com"}`. Bodies
24
+ * are captured by default, so this pass runs on every body before it is
25
+ * stored: emails, JWTs and card numbers (15–19 digits passing Luhn) become
26
+ * `*`, one per character, so the shape of the payload stays readable.
27
+ *
28
+ * Bodies only. Session start carries `appUserEmail` on purpose, and running
29
+ * this over all event data would erase it.
30
+ *
31
+ * Scanned by hand rather than by regex so `crates/event-schema/src/redact.rs`
32
+ * can run the identical algorithm without a regex dependency. The two must
33
+ * agree: change one, change the other, and both test suites restate the cases.
34
+ */
35
+ export declare function maskValues(text: string): string;
package/dist/redact.js CHANGED
@@ -59,3 +59,113 @@ function walk(value, extra, depth, seen) {
59
59
  }
60
60
  return out;
61
61
  }
62
+ /**
63
+ * Mask personal data by *value*, inside captured request and response bodies.
64
+ *
65
+ * Key redaction cannot see `{"note": "mail me at ada@example.com"}`. Bodies
66
+ * are captured by default, so this pass runs on every body before it is
67
+ * stored: emails, JWTs and card numbers (15–19 digits passing Luhn) become
68
+ * `*`, one per character, so the shape of the payload stays readable.
69
+ *
70
+ * Bodies only. Session start carries `appUserEmail` on purpose, and running
71
+ * this over all event data would erase it.
72
+ *
73
+ * Scanned by hand rather than by regex so `crates/event-schema/src/redact.rs`
74
+ * can run the identical algorithm without a regex dependency. The two must
75
+ * agree: change one, change the other, and both test suites restate the cases.
76
+ */
77
+ export function maskValues(text) {
78
+ const chars = text.split('');
79
+ maskEmails(chars);
80
+ maskJwts(chars);
81
+ maskCards(chars);
82
+ return chars.join('');
83
+ }
84
+ const isDigit = (c) => c !== undefined && c >= '0' && c <= '9';
85
+ const isAlpha = (c) => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
86
+ const isAlnum = (c) => isDigit(c) || isAlpha(c);
87
+ const isLocal = (c) => isAlnum(c) || (c !== undefined && '._%+-'.includes(c));
88
+ const isDomain = (c) => isAlnum(c) || c === '.' || c === '-';
89
+ const isJwt = (c) => isAlnum(c) || c === '_' || c === '-' || c === '.';
90
+ function star(chars, from, to) {
91
+ for (let i = from; i < to; i++)
92
+ chars[i] = '*';
93
+ }
94
+ function maskEmails(chars) {
95
+ for (let at = 0; at < chars.length; at++) {
96
+ if (chars[at] !== '@')
97
+ continue;
98
+ let start = at;
99
+ while (start > 0 && isLocal(chars[start - 1]))
100
+ start--;
101
+ let end = at + 1;
102
+ while (end < chars.length && isDomain(chars[end]))
103
+ end++;
104
+ while (end > at + 1 && (chars[end - 1] === '.' || chars[end - 1] === '-'))
105
+ end--;
106
+ const domain = chars.slice(at + 1, end).join('');
107
+ const dot = domain.lastIndexOf('.');
108
+ const tld = dot === -1 ? '' : domain.slice(dot + 1);
109
+ if (start < at && dot > 0 && tld.length >= 2 && tld.split('').every(isAlpha)) {
110
+ star(chars, start, end);
111
+ at = end - 1;
112
+ }
113
+ }
114
+ }
115
+ function maskJwts(chars) {
116
+ for (let i = 0; i + 3 <= chars.length; i++) {
117
+ if (chars[i] !== 'e' || chars[i + 1] !== 'y' || chars[i + 2] !== 'J')
118
+ continue;
119
+ if (i > 0 && isJwt(chars[i - 1]))
120
+ continue;
121
+ let end = i;
122
+ while (end < chars.length && isJwt(chars[end]))
123
+ end++;
124
+ while (end > i && chars[end - 1] === '.')
125
+ end--;
126
+ const parts = chars.slice(i, end).join('').split('.');
127
+ if (parts.length === 3 && parts.every((part) => part.length > 0)) {
128
+ star(chars, i, end);
129
+ i = end - 1;
130
+ }
131
+ }
132
+ }
133
+ function maskCards(chars) {
134
+ for (let i = 0; i < chars.length; i++) {
135
+ if (!isDigit(chars[i]) || (i > 0 && isAlnum(chars[i - 1])))
136
+ continue;
137
+ const digits = [];
138
+ let j = i;
139
+ let end = i;
140
+ while (j < chars.length) {
141
+ if (isDigit(chars[j])) {
142
+ digits.push(Number(chars[j]));
143
+ j++;
144
+ end = j;
145
+ }
146
+ else if ((chars[j] === ' ' || chars[j] === '-') && isDigit(chars[j - 1]) && isDigit(chars[j + 1])) {
147
+ j++;
148
+ }
149
+ else {
150
+ break;
151
+ }
152
+ }
153
+ if (digits.length >= 15 && digits.length <= 19 && !isAlnum(chars[end]) && luhn(digits)) {
154
+ star(chars, i, end);
155
+ }
156
+ i = end - 1;
157
+ }
158
+ }
159
+ function luhn(digits) {
160
+ let sum = 0;
161
+ for (let k = 0; k < digits.length; k++) {
162
+ let d = digits[digits.length - 1 - k];
163
+ if (k % 2 === 1) {
164
+ d *= 2;
165
+ if (d > 9)
166
+ d -= 9;
167
+ }
168
+ sum += d;
169
+ }
170
+ return sum % 10 === 0;
171
+ }
package/dist/types.d.ts CHANGED
@@ -66,6 +66,13 @@ export interface InitOptions {
66
66
  captureNetwork?: boolean;
67
67
  /** Overridden by remote config when it loads. */
68
68
  debug?: boolean;
69
+ /**
70
+ * Your app's own user, when it is known at launch. Recorded on every session
71
+ * as `appUserId` / `appUserEmail`; `identify()` replaces it after a login.
72
+ * Omit for anonymous users.
73
+ */
74
+ appUserId?: string;
75
+ appUserEmail?: string;
69
76
  }
70
77
  /**
71
78
  * Delivered by `GET /v1/config`, cached to disk, refetched periodically.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nagoorkani/session-capture-core",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Platform-free core for the session-capture SDKs",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",