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