@nagoorkani/session-capture-core 0.1.0 → 0.1.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @nagoorkani/session-capture-core
2
2
 
3
- Everything the SDK does that is not about a platform. Not published on its own
4
- and not meant to be installed directly — `@nagoorkani/session-capture-expo` and
3
+ Everything the SDK does that is not about a platform. Published, but not meant
4
+ to be installed directly — `@nagoorkani/session-capture-expo` and
5
5
  `@nagoorkani/session-capture-vue` are the packages an application uses.
6
6
 
7
7
  ## What is in here
package/dist/network.d.ts CHANGED
@@ -90,6 +90,8 @@ export interface XhrLike {
90
90
  readonly status: number;
91
91
  readonly responseType: string;
92
92
  readonly responseText: string;
93
+ /** The parsed body for a non-text `responseType` — a Blob on React Native. */
94
+ readonly response?: unknown;
93
95
  }
94
96
  export interface NetworkOptions {
95
97
  /** Requests to this origin are never captured. */
package/dist/network.js CHANGED
@@ -195,18 +195,35 @@ function installXhrCapture(xhr, options) {
195
195
  if (state.body !== undefined) {
196
196
  event.requestBody = state.body;
197
197
  }
198
+ let deferred = null;
198
199
  if (status > 0) {
199
200
  event.status = status;
200
201
  const responseHeaders = parseResponseHeaders(request.getAllResponseHeaders());
201
202
  event.responseHeaders = redactHeaders(responseHeaders, options.maskedKeys);
202
203
  if (options.captureBodies) {
203
- event.responseBody = readResponseBody(request, headerValue(responseHeaders ?? {}, 'content-type'), options.maskedKeys);
204
+ const contentType = headerValue(responseHeaders ?? {}, 'content-type');
205
+ deferred = readBlobBody(request, contentType);
206
+ if (deferred) {
207
+ // Reported when the read lands, the way the fetch path reports
208
+ // after its clone resolves. The app already has its response.
209
+ deferred.then((text) => {
210
+ event.responseBody = captureBody(text, contentType, options.maskedKeys);
211
+ report(options, event);
212
+ }, () => {
213
+ event.responseBody = '[not captured: blob response]';
214
+ report(options, event);
215
+ });
216
+ }
217
+ else {
218
+ event.responseBody = readResponseBody(request, contentType, options.maskedKeys);
219
+ }
204
220
  }
205
221
  }
206
222
  else {
207
223
  event.error = FAILURE_TEXT[state.reason ?? 'error'];
208
224
  }
209
- report(options, event);
225
+ if (!deferred)
226
+ report(options, event);
210
227
  }
211
228
  catch {
212
229
  // As above.
@@ -343,11 +360,7 @@ function cloneForReading(response) {
343
360
  if (typeof response.clone !== 'function')
344
361
  return null;
345
362
  const headers = readHeaders(response) ?? {};
346
- const type = mediaType(headerValue(headers, 'content-type'));
347
- const redactable = type === 'application/json' ||
348
- type.endsWith('+json') ||
349
- type === 'application/x-www-form-urlencoded';
350
- if (!redactable)
363
+ if (!isRedactable(mediaType(headerValue(headers, 'content-type'))))
351
364
  return null;
352
365
  const declared = Number(headerValue(headers, 'content-length'));
353
366
  if (Number.isFinite(declared) && declared > MAX_CLONE_BYTES)
@@ -359,19 +372,66 @@ function cloneForReading(response) {
359
372
  return null;
360
373
  }
361
374
  }
375
+ /** The shapes `captureBody` can walk by key. Anything else is named, not read. */
376
+ function isRedactable(type) {
377
+ return (type === 'application/json' ||
378
+ type.endsWith('+json') ||
379
+ type === 'application/x-www-form-urlencoded');
380
+ }
381
+ /**
382
+ * A promise for a blob response's text, or null when there is nothing to
383
+ * read that way.
384
+ *
385
+ * React Native's `fetch` is a polyfill over XHR that sets `responseType` to
386
+ * `'blob'`, so on a phone *every* `fetch()` response arrives here as a Blob
387
+ * and `responseText` throws. Reading it is an async round-trip, which is why
388
+ * the first version named it instead — and why every JSON response in a
389
+ * mobile session came back as `[not captured: blob response]`. Same rule as
390
+ * the fetch clone: only a type the deny-list can walk, only under the size
391
+ * cap, and never anything that could throw into the host.
392
+ *
393
+ * `Blob.text()` where it exists; React Native's Blob has no `text()`, but it
394
+ * ships a `FileReader`, and that is the path that runs on a device.
395
+ */
396
+ function readBlobBody(request, contentType) {
397
+ if (request.responseType !== 'blob')
398
+ return null;
399
+ if (!isRedactable(mediaType(contentType)))
400
+ return null;
401
+ const blob = request.response;
402
+ if (!blob || typeof blob !== 'object')
403
+ return null;
404
+ if (typeof blob.size === 'number' && blob.size > MAX_CLONE_BYTES)
405
+ return null;
406
+ try {
407
+ if (typeof blob.text === 'function') {
408
+ return blob.text.call(blob);
409
+ }
410
+ const Reader = globalThis.FileReader;
411
+ if (!Reader)
412
+ return null;
413
+ return new Promise((resolve, reject) => {
414
+ const reader = new Reader();
415
+ reader.onload = () => resolve(typeof reader.result === 'string' ? reader.result : '');
416
+ reader.onerror = () => reject(reader.error);
417
+ reader.readAsText(blob);
418
+ });
419
+ }
420
+ catch {
421
+ return null;
422
+ }
423
+ }
362
424
  function describeBody(body) {
363
425
  const name = body?.constructor?.name;
364
426
  return name ? name.toLowerCase() : typeof body;
365
427
  }
366
428
  /**
367
- * Read the response body, when it is readable at all.
429
+ * Read a text response body synchronously.
368
430
  *
369
- * ponytail: text responses only. `responseText` throws unless `responseType`
370
- * is `''` or `'text'`, and React Native's `fetch` sets it to `'blob'` — so
371
- * axios and direct XHR give up their response bodies here and `fetch()` does
372
- * not. Getting a blob would mean an async FileReader round-trip that delays
373
- * the event past `loadend`; that is the upgrade path if it turns out to
374
- * matter.
431
+ * `responseText` throws unless `responseType` is `''` or `'text'`. A blob
432
+ * with a walkable content type has already gone through [`readBlobBody`] by
433
+ * the time this runs; what reaches here as a blob is one that could not be
434
+ * read that way, and is named rather than read.
375
435
  */
376
436
  function readResponseBody(request, contentType, maskedKeys) {
377
437
  // A host XHR that does not implement responseType at all reads as text,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nagoorkani/session-capture-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Platform-free core for the session-capture SDKs",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",