rrweb.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import type {fullSnapshotEvent, incrementalSnapshotEvent, serializedNodeWithId} from 'sentry/utils/replays/types';
  2. import {EventType, NodeType} from 'sentry/utils/replays/types';
  3. interface FullSnapshotEvent extends fullSnapshotEvent {
  4. timestamp: number;
  5. }
  6. interface IncrementalSnapshotEvent extends incrementalSnapshotEvent {
  7. timestamp: number;
  8. }
  9. const nextRRWebId = (function () {
  10. let __rrwebID = 0;
  11. return () => ++__rrwebID;
  12. })();
  13. export function RRWebInitFrameEventsFixture({
  14. height = 600,
  15. href = 'http://localhost/',
  16. timestamp,
  17. width = 800,
  18. }: {
  19. timestamp: Date;
  20. height?: number;
  21. href?: string;
  22. width?: number;
  23. }) {
  24. return [
  25. {
  26. type: EventType.DomContentLoaded,
  27. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  28. },
  29. {
  30. type: EventType.Load,
  31. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  32. },
  33. {
  34. type: EventType.Meta,
  35. data: {href, width, height},
  36. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  37. },
  38. ];
  39. }
  40. export function RRWebFullSnapshotFrameEventFixture({
  41. timestamp,
  42. childNodes = [],
  43. }: {
  44. timestamp: Date;
  45. childNodes?: serializedNodeWithId[];
  46. }): FullSnapshotEvent {
  47. return {
  48. type: EventType.FullSnapshot,
  49. timestamp: timestamp.getTime(),
  50. data: {
  51. initialOffset: {top: 0, left: 0},
  52. node: {
  53. type: NodeType.Document,
  54. id: 0,
  55. childNodes: [
  56. RRWebDOMFrameFixture({
  57. tagName: 'body',
  58. attributes: {
  59. style:
  60. 'margin:0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu;',
  61. },
  62. childNodes,
  63. }),
  64. ],
  65. },
  66. },
  67. };
  68. }
  69. export function RRWebIncrementalSnapshotFrameEventFixture({
  70. timestamp,
  71. data,
  72. }: {
  73. timestamp: Date;
  74. data: incrementalSnapshotEvent['data'];
  75. }): IncrementalSnapshotEvent {
  76. return {
  77. type: EventType.IncrementalSnapshot,
  78. timestamp: timestamp.getTime(),
  79. data,
  80. }
  81. }
  82. export function RRWebDOMFrameFixture({
  83. id,
  84. tagName,
  85. attributes,
  86. childNodes,
  87. textContent,
  88. }: {
  89. attributes?: Record<string, string>;
  90. childNodes?: serializedNodeWithId[];
  91. id?: number;
  92. tagName?: string;
  93. textContent?: string;
  94. }): serializedNodeWithId {
  95. id = id ?? nextRRWebId();
  96. if (tagName) {
  97. return {
  98. type: NodeType.Element,
  99. id,
  100. tagName,
  101. attributes: attributes ?? {},
  102. childNodes: childNodes ?? [],
  103. };
  104. }
  105. return {
  106. type: NodeType.Text,
  107. id,
  108. textContent: textContent ?? '',
  109. };
  110. }
  111. export function RRWebHelloWorldFrameFixture() {
  112. return RRWebDOMFrameFixture({
  113. tagName: 'div',
  114. childNodes: [
  115. RRWebDOMFrameFixture({
  116. tagName: 'h1',
  117. attributes: {style: 'text-align: center;'},
  118. childNodes: [
  119. RRWebDOMFrameFixture({
  120. textContent: 'Hello World',
  121. }),
  122. ],
  123. }),
  124. ],
  125. });
  126. }