@sentry/replay 7.48.0 → 7.49.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.
package/esm/index.js CHANGED
@@ -29,8 +29,8 @@ const ERROR_CHECKOUT_TIME = 60000;
29
29
  const RETRY_BASE_INTERVAL = 5000;
30
30
  const RETRY_MAX_COUNT = 3;
31
31
 
32
- /* The max (uncompressed) size in bytes of a network body. Any body larger than this will be dropped. */
33
- const NETWORK_BODY_MAX_SIZE = 300000;
32
+ /* The max (uncompressed) size in bytes of a network body. Any body larger than this will be truncated. */
33
+ const NETWORK_BODY_MAX_SIZE = 150000;
34
34
 
35
35
  var NodeType$1;
36
36
  (function (NodeType) {
@@ -4069,7 +4069,8 @@ function addBreadcrumbEvent(replay, breadcrumb) {
4069
4069
  timestamp: (breadcrumb.timestamp || 0) * 1000,
4070
4070
  data: {
4071
4071
  tag: 'breadcrumb',
4072
- payload: normalize(breadcrumb),
4072
+ // normalize to max. 10 depth and 1_000 properties per object
4073
+ payload: normalize(breadcrumb, 10, 1000),
4073
4074
  },
4074
4075
  });
4075
4076
 
@@ -4476,6 +4477,393 @@ function handleXhrSpanListener(replay) {
4476
4477
  };
4477
4478
  }
4478
4479
 
4480
+ const OBJ = 10;
4481
+ const OBJ_KEY = 11;
4482
+ const OBJ_KEY_STR = 12;
4483
+ const OBJ_VAL = 13;
4484
+ const OBJ_VAL_STR = 14;
4485
+ const OBJ_VAL_COMPLETED = 15;
4486
+
4487
+ const ARR = 20;
4488
+ const ARR_VAL = 21;
4489
+ const ARR_VAL_STR = 22;
4490
+ const ARR_VAL_COMPLETED = 23;
4491
+
4492
+ const ALLOWED_PRIMITIVES = ['true', 'false', 'null'];
4493
+
4494
+ /**
4495
+ * Complete an incomplete JSON string.
4496
+ * This will ensure that the last element always has a `"~~"` to indicate it was truncated.
4497
+ * For example, `[1,2,` will be completed to `[1,2,"~~"]`
4498
+ * and `{"aa":"b` will be completed to `{"aa":"b~~"}`
4499
+ */
4500
+ function completeJson(incompleteJson, stack) {
4501
+ if (!stack.length) {
4502
+ return incompleteJson;
4503
+ }
4504
+
4505
+ let json = incompleteJson;
4506
+
4507
+ // Most checks are only needed for the last step in the stack
4508
+ const lastPos = stack.length - 1;
4509
+ const lastStep = stack[lastPos];
4510
+
4511
+ json = _fixLastStep(json, lastStep);
4512
+
4513
+ // Complete remaining steps - just add closing brackets
4514
+ for (let i = lastPos; i >= 0; i--) {
4515
+ const step = stack[i];
4516
+
4517
+ switch (step) {
4518
+ case OBJ:
4519
+ json = `${json}}`;
4520
+ break;
4521
+ case ARR:
4522
+ json = `${json}]`;
4523
+ break;
4524
+ }
4525
+ }
4526
+
4527
+ return json;
4528
+ }
4529
+
4530
+ function _fixLastStep(json, lastStep) {
4531
+ switch (lastStep) {
4532
+ // Object cases
4533
+ case OBJ:
4534
+ return `${json}"~~":"~~"`;
4535
+ case OBJ_KEY:
4536
+ return `${json}:"~~"`;
4537
+ case OBJ_KEY_STR:
4538
+ return `${json}~~":"~~"`;
4539
+ case OBJ_VAL:
4540
+ return _maybeFixIncompleteObjValue(json);
4541
+ case OBJ_VAL_STR:
4542
+ return `${json}~~"`;
4543
+ case OBJ_VAL_COMPLETED:
4544
+ return `${json},"~~":"~~"`;
4545
+
4546
+ // Array cases
4547
+ case ARR:
4548
+ return `${json}"~~"`;
4549
+ case ARR_VAL:
4550
+ return _maybeFixIncompleteArrValue(json);
4551
+ case ARR_VAL_STR:
4552
+ return `${json}~~"`;
4553
+ case ARR_VAL_COMPLETED:
4554
+ return `${json},"~~"`;
4555
+ }
4556
+
4557
+ return json;
4558
+ }
4559
+
4560
+ function _maybeFixIncompleteArrValue(json) {
4561
+ const pos = _findLastArrayDelimiter(json);
4562
+
4563
+ if (pos > -1) {
4564
+ const part = json.slice(pos + 1);
4565
+
4566
+ if (ALLOWED_PRIMITIVES.includes(part.trim())) {
4567
+ return `${json},"~~"`;
4568
+ }
4569
+
4570
+ // Everything else is replaced with `"~~"`
4571
+ return `${json.slice(0, pos + 1)}"~~"`;
4572
+ }
4573
+
4574
+ // fallback, this shouldn't happen, to be save
4575
+ return json;
4576
+ }
4577
+
4578
+ function _findLastArrayDelimiter(json) {
4579
+ for (let i = json.length - 1; i >= 0; i--) {
4580
+ const char = json[i];
4581
+
4582
+ if (char === ',' || char === '[') {
4583
+ return i;
4584
+ }
4585
+ }
4586
+
4587
+ return -1;
4588
+ }
4589
+
4590
+ function _maybeFixIncompleteObjValue(json) {
4591
+ const startPos = json.lastIndexOf(':');
4592
+
4593
+ const part = json.slice(startPos + 1);
4594
+
4595
+ if (ALLOWED_PRIMITIVES.includes(part.trim())) {
4596
+ return `${json},"~~":"~~"`;
4597
+ }
4598
+
4599
+ // Everything else is replaced with `"~~"`
4600
+ // This also means we do not have incomplete numbers, e.g `[1` is replaced with `["~~"]`
4601
+ return `${json.slice(0, startPos + 1)}"~~"`;
4602
+ }
4603
+
4604
+ /**
4605
+ * Evaluate an (incomplete) JSON string.
4606
+ */
4607
+ function evaluateJson(json) {
4608
+ const stack = [];
4609
+
4610
+ for (let pos = 0; pos < json.length; pos++) {
4611
+ _evaluateJsonPos(stack, json, pos);
4612
+ }
4613
+
4614
+ return stack;
4615
+ }
4616
+
4617
+ function _evaluateJsonPos(stack, json, pos) {
4618
+ const curStep = stack[stack.length - 1];
4619
+
4620
+ const char = json[pos];
4621
+
4622
+ const whitespaceRegex = /\s/;
4623
+
4624
+ if (whitespaceRegex.test(char)) {
4625
+ return;
4626
+ }
4627
+
4628
+ if (char === '"' && !_isEscaped(json, pos)) {
4629
+ _handleQuote(stack, curStep);
4630
+ return;
4631
+ }
4632
+
4633
+ switch (char) {
4634
+ case '{':
4635
+ _handleObj(stack, curStep);
4636
+ break;
4637
+ case '[':
4638
+ _handleArr(stack, curStep);
4639
+ break;
4640
+ case ':':
4641
+ _handleColon(stack, curStep);
4642
+ break;
4643
+ case ',':
4644
+ _handleComma(stack, curStep);
4645
+ break;
4646
+ case '}':
4647
+ _handleObjClose(stack, curStep);
4648
+ break;
4649
+ case ']':
4650
+ _handleArrClose(stack, curStep);
4651
+ break;
4652
+ }
4653
+ }
4654
+
4655
+ function _handleQuote(stack, curStep) {
4656
+ // End of obj value
4657
+ if (curStep === OBJ_VAL_STR) {
4658
+ stack.pop();
4659
+ stack.push(OBJ_VAL_COMPLETED);
4660
+ return;
4661
+ }
4662
+
4663
+ // End of arr value
4664
+ if (curStep === ARR_VAL_STR) {
4665
+ stack.pop();
4666
+ stack.push(ARR_VAL_COMPLETED);
4667
+ return;
4668
+ }
4669
+
4670
+ // Start of obj value
4671
+ if (curStep === OBJ_VAL) {
4672
+ stack.push(OBJ_VAL_STR);
4673
+ return;
4674
+ }
4675
+
4676
+ // Start of arr value
4677
+ if (curStep === ARR_VAL) {
4678
+ stack.push(ARR_VAL_STR);
4679
+ return;
4680
+ }
4681
+
4682
+ // Start of obj key
4683
+ if (curStep === OBJ) {
4684
+ stack.push(OBJ_KEY_STR);
4685
+ return;
4686
+ }
4687
+
4688
+ // End of obj key
4689
+ if (curStep === OBJ_KEY_STR) {
4690
+ stack.pop();
4691
+ stack.push(OBJ_KEY);
4692
+ return;
4693
+ }
4694
+ }
4695
+
4696
+ function _handleObj(stack, curStep) {
4697
+ // Initial object
4698
+ if (!curStep) {
4699
+ stack.push(OBJ);
4700
+ return;
4701
+ }
4702
+
4703
+ // New object as obj value
4704
+ if (curStep === OBJ_VAL) {
4705
+ stack.push(OBJ);
4706
+ return;
4707
+ }
4708
+
4709
+ // New object as array element
4710
+ if (curStep === ARR_VAL) {
4711
+ stack.push(OBJ);
4712
+ }
4713
+
4714
+ // New object as first array element
4715
+ if (curStep === ARR) {
4716
+ stack.push(OBJ);
4717
+ return;
4718
+ }
4719
+ }
4720
+
4721
+ function _handleArr(stack, curStep) {
4722
+ // Initial array
4723
+ if (!curStep) {
4724
+ stack.push(ARR);
4725
+ stack.push(ARR_VAL);
4726
+ return;
4727
+ }
4728
+
4729
+ // New array as obj value
4730
+ if (curStep === OBJ_VAL) {
4731
+ stack.push(ARR);
4732
+ stack.push(ARR_VAL);
4733
+ return;
4734
+ }
4735
+
4736
+ // New array as array element
4737
+ if (curStep === ARR_VAL) {
4738
+ stack.push(ARR);
4739
+ stack.push(ARR_VAL);
4740
+ }
4741
+
4742
+ // New array as first array element
4743
+ if (curStep === ARR) {
4744
+ stack.push(ARR);
4745
+ stack.push(ARR_VAL);
4746
+ return;
4747
+ }
4748
+ }
4749
+
4750
+ function _handleColon(stack, curStep) {
4751
+ if (curStep === OBJ_KEY) {
4752
+ stack.pop();
4753
+ stack.push(OBJ_VAL);
4754
+ }
4755
+ }
4756
+
4757
+ function _handleComma(stack, curStep) {
4758
+ // Comma after obj value
4759
+ if (curStep === OBJ_VAL) {
4760
+ stack.pop();
4761
+ return;
4762
+ }
4763
+ if (curStep === OBJ_VAL_COMPLETED) {
4764
+ // Pop OBJ_VAL_COMPLETED & OBJ_VAL
4765
+ stack.pop();
4766
+ stack.pop();
4767
+ return;
4768
+ }
4769
+
4770
+ // Comma after arr value
4771
+ if (curStep === ARR_VAL) {
4772
+ // do nothing - basically we'd pop ARR_VAL but add it right back
4773
+ return;
4774
+ }
4775
+
4776
+ if (curStep === ARR_VAL_COMPLETED) {
4777
+ // Pop ARR_VAL_COMPLETED
4778
+ stack.pop();
4779
+
4780
+ // basically we'd pop ARR_VAL but add it right back
4781
+ return;
4782
+ }
4783
+ }
4784
+
4785
+ function _handleObjClose(stack, curStep) {
4786
+ // Empty object {}
4787
+ if (curStep === OBJ) {
4788
+ stack.pop();
4789
+ }
4790
+
4791
+ // Object with element
4792
+ if (curStep === OBJ_VAL) {
4793
+ // Pop OBJ_VAL, OBJ
4794
+ stack.pop();
4795
+ stack.pop();
4796
+ }
4797
+
4798
+ // Obj with element
4799
+ if (curStep === OBJ_VAL_COMPLETED) {
4800
+ // Pop OBJ_VAL_COMPLETED, OBJ_VAL, OBJ
4801
+ stack.pop();
4802
+ stack.pop();
4803
+ stack.pop();
4804
+ }
4805
+
4806
+ // if was obj value, complete it
4807
+ if (stack[stack.length - 1] === OBJ_VAL) {
4808
+ stack.push(OBJ_VAL_COMPLETED);
4809
+ }
4810
+
4811
+ // if was arr value, complete it
4812
+ if (stack[stack.length - 1] === ARR_VAL) {
4813
+ stack.push(ARR_VAL_COMPLETED);
4814
+ }
4815
+ }
4816
+
4817
+ function _handleArrClose(stack, curStep) {
4818
+ // Empty array []
4819
+ if (curStep === ARR) {
4820
+ stack.pop();
4821
+ }
4822
+
4823
+ // Array with element
4824
+ if (curStep === ARR_VAL) {
4825
+ // Pop ARR_VAL, ARR
4826
+ stack.pop();
4827
+ stack.pop();
4828
+ }
4829
+
4830
+ // Array with element
4831
+ if (curStep === ARR_VAL_COMPLETED) {
4832
+ // Pop ARR_VAL_COMPLETED, ARR_VAL, ARR
4833
+ stack.pop();
4834
+ stack.pop();
4835
+ stack.pop();
4836
+ }
4837
+
4838
+ // if was obj value, complete it
4839
+ if (stack[stack.length - 1] === OBJ_VAL) {
4840
+ stack.push(OBJ_VAL_COMPLETED);
4841
+ }
4842
+
4843
+ // if was arr value, complete it
4844
+ if (stack[stack.length - 1] === ARR_VAL) {
4845
+ stack.push(ARR_VAL_COMPLETED);
4846
+ }
4847
+ }
4848
+
4849
+ function _isEscaped(str, pos) {
4850
+ const previousChar = str[pos - 1];
4851
+
4852
+ return previousChar === '\\' && !_isEscaped(str, pos - 1);
4853
+ }
4854
+
4855
+ /* eslint-disable max-lines */
4856
+
4857
+ /**
4858
+ * Takes an incomplete JSON string, and returns a hopefully valid JSON string.
4859
+ * Note that this _can_ fail, so you should check the return value is valid JSON.
4860
+ */
4861
+ function fixJson(incompleteJson) {
4862
+ const stack = evaluateJson(incompleteJson);
4863
+
4864
+ return completeJson(incompleteJson, stack);
4865
+ }
4866
+
4479
4867
  /** Get the size of a body. */
4480
4868
  function getBodySize(
4481
4869
  body,
@@ -4569,21 +4957,6 @@ function makeNetworkReplayBreadcrumb(
4569
4957
  return result;
4570
4958
  }
4571
4959
 
4572
- /** Get either a JSON network body, or a text representation. */
4573
- function getNetworkBody(bodyText) {
4574
- if (!bodyText) {
4575
- return;
4576
- }
4577
-
4578
- try {
4579
- return JSON.parse(bodyText);
4580
- } catch (e2) {
4581
- // return text
4582
- }
4583
-
4584
- return bodyText;
4585
- }
4586
-
4587
4960
  /** Build the request or response part of a replay network breadcrumb. */
4588
4961
  function buildNetworkRequestOrResponse(
4589
4962
  headers,
@@ -4612,11 +4985,11 @@ function buildNetworkRequestOrResponse(
4612
4985
  size: bodySize,
4613
4986
  };
4614
4987
 
4615
- if (bodySize < NETWORK_BODY_MAX_SIZE) {
4616
- info.body = body;
4617
- } else {
4988
+ const { body: normalizedBody, warnings } = normalizeNetworkBody(body);
4989
+ info.body = normalizedBody;
4990
+ if (warnings.length > 0) {
4618
4991
  info._meta = {
4619
- errors: ['MAX_BODY_SIZE_EXCEEDED'],
4992
+ warnings,
4620
4993
  };
4621
4994
  }
4622
4995
 
@@ -4642,6 +5015,48 @@ function _serializeFormData(formData) {
4642
5015
  return new URLSearchParams(formData).toString();
4643
5016
  }
4644
5017
 
5018
+ function normalizeNetworkBody(body)
5019
+
5020
+ {
5021
+ if (!body || typeof body !== 'string') {
5022
+ return {
5023
+ body,
5024
+ warnings: [],
5025
+ };
5026
+ }
5027
+
5028
+ const exceedsSizeLimit = body.length > NETWORK_BODY_MAX_SIZE;
5029
+
5030
+ if (_strIsProbablyJson(body)) {
5031
+ try {
5032
+ const json = exceedsSizeLimit ? fixJson(body.slice(0, NETWORK_BODY_MAX_SIZE)) : body;
5033
+ const normalizedBody = JSON.parse(json);
5034
+ return {
5035
+ body: normalizedBody,
5036
+ warnings: exceedsSizeLimit ? ['JSON_TRUNCATED'] : [],
5037
+ };
5038
+ } catch (e3) {
5039
+ return {
5040
+ body,
5041
+ warnings: ['INVALID_JSON'],
5042
+ };
5043
+ }
5044
+ }
5045
+
5046
+ return {
5047
+ body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body,
5048
+ warnings: exceedsSizeLimit ? ['TEXT_TRUNCATED'] : [],
5049
+ };
5050
+ }
5051
+
5052
+ function _strIsProbablyJson(str) {
5053
+ const first = str[0];
5054
+ const last = str[str.length - 1];
5055
+
5056
+ // Simple check: If this does not start & end with {} or [], it's not JSON
5057
+ return (first === '[' && last === ']') || (first === '{' && last === '}');
5058
+ }
5059
+
4645
5060
  /**
4646
5061
  * Capture a fetch breadcrumb to a replay.
4647
5062
  * This adds additional data (where approriate).
@@ -4733,8 +5148,8 @@ function _getRequestInfo(
4733
5148
 
4734
5149
  // We only want to transmit string or string-like bodies
4735
5150
  const requestBody = _getFetchRequestArgBody(input);
4736
- const body = getNetworkBody(getBodyString(requestBody));
4737
- return buildNetworkRequestOrResponse(headers, requestBodySize, body);
5151
+ const bodyStr = getBodyString(requestBody);
5152
+ return buildNetworkRequestOrResponse(headers, requestBodySize, bodyStr);
4738
5153
  }
4739
5154
 
4740
5155
  async function _getResponseInfo(
@@ -4758,7 +5173,7 @@ async function _getResponseInfo(
4758
5173
  try {
4759
5174
  // We have to clone this, as the body can only be read once
4760
5175
  const res = response.clone();
4761
- const { body, bodyText } = await _parseFetchBody(res);
5176
+ const bodyText = await _parseFetchBody(res);
4762
5177
 
4763
5178
  const size =
4764
5179
  bodyText && bodyText.length && responseBodySize === undefined
@@ -4766,7 +5181,7 @@ async function _getResponseInfo(
4766
5181
  : responseBodySize;
4767
5182
 
4768
5183
  if (captureBodies) {
4769
- return buildNetworkRequestOrResponse(headers, size, body);
5184
+ return buildNetworkRequestOrResponse(headers, size, bodyText);
4770
5185
  }
4771
5186
 
4772
5187
  return buildNetworkRequestOrResponse(headers, size, undefined);
@@ -4776,25 +5191,12 @@ async function _getResponseInfo(
4776
5191
  }
4777
5192
  }
4778
5193
 
4779
- async function _parseFetchBody(
4780
- response,
4781
- ) {
4782
- let bodyText;
4783
-
5194
+ async function _parseFetchBody(response) {
4784
5195
  try {
4785
- bodyText = await response.text();
5196
+ return await response.text();
4786
5197
  } catch (e2) {
4787
- return {};
4788
- }
4789
-
4790
- try {
4791
- const body = JSON.parse(bodyText);
4792
- return { body, bodyText };
4793
- } catch (e3) {
4794
- // just send bodyText
5198
+ return undefined;
4795
5199
  }
4796
-
4797
- return { bodyText, body: bodyText };
4798
5200
  }
4799
5201
 
4800
5202
  function _getFetchRequestArgBody(fetchArgs = []) {
@@ -4927,12 +5329,12 @@ function _prepareXhrData(
4927
5329
  const request = buildNetworkRequestOrResponse(
4928
5330
  requestHeaders,
4929
5331
  requestBodySize,
4930
- options.captureBodies ? getNetworkBody(getBodyString(input)) : undefined,
5332
+ options.captureBodies ? getBodyString(input) : undefined,
4931
5333
  );
4932
5334
  const response = buildNetworkRequestOrResponse(
4933
5335
  responseHeaders,
4934
5336
  responseBodySize,
4935
- options.captureBodies ? getNetworkBody(hint.xhr.responseText) : undefined,
5337
+ options.captureBodies ? hint.xhr.responseText : undefined,
4936
5338
  );
4937
5339
 
4938
5340
  return {