clarity-js 0.7.4 → 0.7.6

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.
@@ -0,0 +1,61 @@
1
+ import { Privacy } from "@clarity-types/core";
2
+ import { Event, Token } from "@clarity-types/data";
3
+ import { Constant, NodeInfo } from "@clarity-types/layout";
4
+ import * as scrub from "@src/core/scrub";
5
+ import { time } from "@src/core/time";
6
+ import * as baseline from "@src/data/baseline";
7
+ import tokenize from "@src/data/token";
8
+ import { queue } from "@src/data/upload";
9
+ import * as snapshot from "@src/insight/snapshot";
10
+ import * as doc from "@src/layout/document";
11
+
12
+ export default async function (type: Event): Promise<void> {
13
+ let eventTime = time()
14
+ let tokens: Token[] = [eventTime, type];
15
+ switch (type) {
16
+ case Event.Document:
17
+ let d = doc.data;
18
+ tokens.push(d.width);
19
+ tokens.push(d.height);
20
+ baseline.track(type, d.width, d.height);
21
+ queue(tokens);
22
+ break;
23
+ case Event.Snapshot:
24
+ let values = snapshot.values;
25
+ // Only encode and queue DOM updates if we have valid updates to report back
26
+ if (values.length > 0) {
27
+ for (let value of values) {
28
+ let privacy = value.metadata.privacy;
29
+ let data: NodeInfo = value.data;
30
+ for (let key of ["tag", "attributes", "value"]) {
31
+ if (data[key]) {
32
+ switch (key) {
33
+ case "tag":
34
+ tokens.push(value.id);
35
+ if (value.parent) { tokens.push(value.parent); }
36
+ if (value.previous) { tokens.push(value.previous); }
37
+ tokens.push(data[key]);
38
+ break;
39
+ case "attributes":
40
+ for (let attr in data[key]) {
41
+ if (data[key][attr] !== undefined) {
42
+ tokens.push(attribute(attr, data[key][attr], privacy));
43
+ }
44
+ }
45
+ break;
46
+ case "value":
47
+ tokens.push(scrub.text(data[key], data.tag, privacy));
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ }
53
+ queue(tokenize(tokens), true);
54
+ }
55
+ break;
56
+ }
57
+ }
58
+
59
+ function attribute(key: string, value: string, privacy: Privacy): string {
60
+ return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
61
+ }
@@ -0,0 +1,112 @@
1
+ import { OffsetDistance, Privacy } from "@clarity-types/core";
2
+ import { Event } from "@clarity-types/data";
3
+ import { Constant, NodeInfo, NodeValue, TargetMetadata } from "@clarity-types/layout";
4
+ import * as doc from "@src/layout/document";
5
+ import encode from "@src/insight/encode";
6
+ import * as interaction from "@src/interaction";
7
+ export let values: NodeValue[] = [];
8
+ let index: number = 1;
9
+ let idMap: WeakMap<Node, number> = null; // Maps node => id.
10
+
11
+ export function start(): void {
12
+ reset();
13
+ doc.start();
14
+ getId(document.documentElement); // Pre-discover ID for page root
15
+ interaction.observe(document);
16
+ }
17
+
18
+ export function stop(): void {
19
+ reset();
20
+ doc.stop();
21
+ }
22
+
23
+ export function compute(): void { /* Intentionally Blank */ }
24
+ export function iframe(): boolean { return false; }
25
+ export function offset(): OffsetDistance { return { x: 0, y: 0 }; }
26
+ export function hashText(): void { /* Intentionally Blank */ }
27
+
28
+ export function target(evt: UIEvent): Node {
29
+ let path = evt.composed && evt.composedPath ? evt.composedPath() : null;
30
+ let node = (path && path.length > 0 ? path[0] : evt.target) as Node;
31
+ return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
32
+ }
33
+
34
+ export function metadata(node: Node): TargetMetadata {
35
+ let output: TargetMetadata = { id: 0, hash: null, privacy: Privacy.Snapshot, node };
36
+ if (node) { output.id = idMap.has(node) ? idMap.get(node) : getId(node); }
37
+ return output;
38
+ }
39
+
40
+ export function snapshot(): void {
41
+ values = [];
42
+ traverse(document);
43
+ encode(Event.Snapshot);
44
+ }
45
+
46
+ function reset(): void {
47
+ idMap = new WeakMap();
48
+ }
49
+
50
+ function traverse(root: Node): void {
51
+ let queue = [root];
52
+ while (queue.length > 0) {
53
+ let attributes = null, tag = null, value = null;
54
+ let node = queue.shift();
55
+ let next = node.firstChild;
56
+ let parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode : null);
57
+
58
+ while (next) {
59
+ queue.push(next);
60
+ next = next.nextSibling;
61
+ }
62
+
63
+ // Process the node
64
+ let type = node.nodeType;
65
+ switch (type) {
66
+ case Node.DOCUMENT_TYPE_NODE:
67
+ let doctype = node as DocumentType;
68
+ tag = Constant.DocumentTag;
69
+ attributes = { name: doctype.name, publicId: doctype.publicId, systemId: doctype.systemId }
70
+ break;
71
+ case Node.TEXT_NODE:
72
+ value = node.nodeValue;
73
+ tag = idMap.get(parent) ? Constant.TextTag : tag;
74
+ break;
75
+ case Node.ELEMENT_NODE:
76
+ let element = node as HTMLElement;
77
+ attributes = getAttributes(element);
78
+ tag = ["NOSCRIPT", "SCRIPT", "STYLE"].indexOf(element.tagName) < 0 ? element.tagName : tag;
79
+ break;
80
+ }
81
+ add(node, parent, { tag, attributes, value });
82
+ }
83
+ }
84
+
85
+ /* Helper Functions - Snapshot Traversal */
86
+ function getAttributes(element: HTMLElement): { [key: string]: string } {
87
+ let output = {};
88
+ let attributes = element.attributes;
89
+ if (attributes && attributes.length > 0) {
90
+ for (let i = 0; i < attributes.length; i++) {
91
+ output[attributes[i].name] = attributes[i].value;
92
+ }
93
+ }
94
+ return output;
95
+ }
96
+
97
+ function getId(node: Node): number {
98
+ if (node === null) { return null; }
99
+ if (idMap.has(node)) { return idMap.get(node); }
100
+ idMap.set(node, index);
101
+ return index++;
102
+ }
103
+
104
+ function add(node: Node, parent: Node, data: NodeInfo): void {
105
+ if (node && data && data.tag) {
106
+ let id = getId(node);
107
+ let parentId = parent ? idMap.get(parent) : null;
108
+ let previous = node.previousSibling ? idMap.get(node.previousSibling) : null;
109
+ let metadata = { active: true, suspend: false, privacy: Privacy.Snapshot, position: null, fraud: null, size: null };
110
+ values.push({ id, parent: parentId, previous, children: [], data, selector: null, hash: null, region: null, metadata });
111
+ }
112
+ }
@@ -5,8 +5,8 @@ import { bind } from "@src/core/event";
5
5
  import { schedule } from "@src/core/task";
6
6
  import { time } from "@src/core/time";
7
7
  import { iframe } from "@src/layout/dom";
8
- import offset from "@src/layout/offset";
9
- import { link, target } from "@src/layout/target";
8
+ import { offset } from "@src/layout/offset";
9
+ import { target } from "@src/layout/target";
10
10
  import encode from "./encode";
11
11
 
12
12
  const UserInputTags = ["input", "textarea", "radio", "button", "canvas"];
@@ -73,6 +73,19 @@ function handler(event: Event, root: Node, evt: MouseEvent): void {
73
73
  }
74
74
  }
75
75
 
76
+ function link(node: Node): HTMLAnchorElement {
77
+ while (node && node !== document) {
78
+ if (node.nodeType === Node.ELEMENT_NODE) {
79
+ let element = node as HTMLElement;
80
+ if (element.tagName === "A") {
81
+ return element as HTMLAnchorElement;
82
+ }
83
+ }
84
+ node = node.parentNode;
85
+ }
86
+ return null;
87
+ }
88
+
76
89
  function text(element: Node): string {
77
90
  let output = null;
78
91
  if (element) {
@@ -47,7 +47,7 @@ export default async function (type: Event, ts: number = null): Promise<void> {
47
47
  for (let entry of click.state) {
48
48
  let cTarget = metadata(entry.data.target as Node, entry.event, entry.data.text);
49
49
  tokens = [entry.time, entry.event];
50
- let cHash = cTarget.hash.join(Constant.Dot);
50
+ let cHash = cTarget.hash ? cTarget.hash.join(Constant.Dot) : Constant.Empty;
51
51
  tokens.push(cTarget.id);
52
52
  tokens.push(entry.data.x);
53
53
  tokens.push(entry.data.y);
@@ -5,7 +5,7 @@ import { schedule } from "@src/core/task";
5
5
  import { time } from "@src/core/time";
6
6
  import { clearTimeout, setTimeout } from "@src/core/timeout";
7
7
  import { iframe } from "@src/layout/dom";
8
- import offset from "@src/layout/offset";
8
+ import { offset } from "@src/layout/offset";
9
9
  import { target } from "@src/layout/target";
10
10
  import encode from "./encode";
11
11
 
@@ -1,6 +1,6 @@
1
1
  import { Event } from "@clarity-types/data";
2
2
  import { DocumentData } from "@clarity-types/layout";
3
- import encode from "./encode";
3
+ import encode from "@src/layout/encode";
4
4
 
5
5
  export let data: DocumentData;
6
6
 
@@ -41,6 +41,6 @@ export function compute(): void {
41
41
  }
42
42
  }
43
43
 
44
- export function end(): void {
44
+ export function stop(): void {
45
45
  reset();
46
46
  }
@@ -106,5 +106,5 @@ function str(input: number): string {
106
106
  }
107
107
 
108
108
  function attribute(key: string, value: string, privacy: Privacy): string {
109
- return `${key}=${scrub.text(value, key, privacy)}`;
109
+ return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
110
110
  }
@@ -20,5 +20,5 @@ export function stop(): void {
20
20
  region.stop();
21
21
  dom.stop();
22
22
  mutation.stop();
23
- doc.end();
23
+ doc.stop();
24
24
  }
@@ -128,7 +128,7 @@ export default function (node: Node, source: Source): Node {
128
128
  case "HEAD":
129
129
  let head = { tag, attributes };
130
130
  let l = insideFrame && node.ownerDocument?.location ? node.ownerDocument.location : location;
131
- head.attributes[Constant.Base] = l.protocol + "//" + l.hostname + l.pathname;
131
+ head.attributes[Constant.Base] = l.protocol + "//" + l.host + l.pathname;
132
132
  dom[call](node, parent, head, source);
133
133
  break;
134
134
  case "BASE":
@@ -138,7 +138,7 @@ export default function (node: Node, source: Source): Node {
138
138
  // We create "a" element so we can generate protocol and hostname for relative paths like "/path/"
139
139
  let a = document.createElement("a");
140
140
  a.href = attributes["href"];
141
- baseHead.data.attributes[Constant.Base] = a.protocol + "//" + a.hostname + a.pathname;
141
+ baseHead.data.attributes[Constant.Base] = a.protocol + "//" + a.host + a.pathname;
142
142
  }
143
143
  break;
144
144
  case "STYLE":
@@ -1,7 +1,7 @@
1
1
  import { OffsetDistance } from "@clarity-types/core";
2
2
  import { iframe } from "@src/layout/dom";
3
3
 
4
- export default function(element: HTMLElement): OffsetDistance {
4
+ export function offset(element: HTMLElement): OffsetDistance {
5
5
  let output: OffsetDistance = { x: 0, y: 0 };
6
6
 
7
7
  // Walk up the chain to ensure we compute offset distance correctly
@@ -13,19 +13,6 @@ export function target(evt: UIEvent): Node {
13
13
  return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
14
14
  }
15
15
 
16
- export function link(node: Node): HTMLAnchorElement {
17
- while (node && node !== document) {
18
- if (node.nodeType === Node.ELEMENT_NODE) {
19
- let element = node as HTMLElement;
20
- if (element.tagName === "A") {
21
- return element as HTMLAnchorElement;
22
- }
23
- }
24
- node = node.parentNode;
25
- }
26
- return null;
27
- }
28
-
29
16
  export function metadata(node: Node, event: Event, text: string = null): TargetMetadata {
30
17
  // If the node is null, we return a reserved value for id: 0. Valid assignment of id begins from 1+.
31
18
  let output: TargetMetadata = { id: 0, hash: null, privacy: Privacy.Text, node };
@@ -1,13 +1,13 @@
1
1
  import { Task, Timer } from "@clarity-types/core";
2
2
  import { Source } from "@clarity-types/layout";
3
3
  import * as task from "@src/core/task";
4
- import processNode from "./node";
4
+ import node from "@src/layout/node";
5
5
 
6
6
  export default async function(root: Node, timer: Timer, source: Source): Promise<void> {
7
7
  let queue = [root];
8
8
  while (queue.length > 0) {
9
- let node = queue.shift();
10
- let next = node.firstChild;
9
+ let entry = queue.shift();
10
+ let next = entry.firstChild;
11
11
 
12
12
  while (next) {
13
13
  queue.push(next);
@@ -22,7 +22,7 @@ export default async function(root: Node, timer: Timer, source: Source): Promise
22
22
  // Check if processing a node gives us a pointer to one of its sub nodes for traversal
23
23
  // E.g. an element node may give us a pointer to traverse shadowDom if shadowRoot property is set
24
24
  // Or, an iframe from the same origin could give a pointer to it's document for traversing contents of iframe.
25
- let subnode = processNode(node, source);
25
+ let subnode = node(entry, source);
26
26
  if (subnode) { queue.push(subnode); }
27
27
  }
28
28
  }
@@ -91,5 +91,5 @@ export function stop(): void {
91
91
  function host(url: string): string {
92
92
  let a = document.createElement("a");
93
93
  a.href = url;
94
- return a.hostname;
94
+ return a.host;
95
95
  }
package/types/core.d.ts CHANGED
@@ -54,7 +54,8 @@ export const enum Privacy {
54
54
  Sensitive = 1,
55
55
  Text = 2,
56
56
  TextImage = 3,
57
- Exclude = 4
57
+ Exclude = 4,
58
+ Snapshot = 5
58
59
  }
59
60
 
60
61
  /* Helper Interfaces */
package/types/data.d.ts CHANGED
@@ -63,7 +63,8 @@ export const enum Event {
63
63
  Submit = 39,
64
64
  Extract = 40,
65
65
  Fraud = 41,
66
- Change = 42
66
+ Change = 42,
67
+ Snapshot = 43
67
68
  }
68
69
 
69
70
  export const enum Metric {
@@ -217,7 +218,8 @@ export const enum Setting {
217
218
  MinUploadDelay = 100, // Minimum time before we are ready to flush events to the server
218
219
  MaxUploadDelay = 30 * Time.Second, // Do flush out payload once every 30s,
219
220
  ExtractLimit = 10000, // Do not extract more than 10000 characters
220
- ChecksumPrecision = 24 // n-bit integer to represent token hash
221
+ ChecksumPrecision = 24, // n-bit integer to represent token hash
222
+ UploadTimeout = 15000 // Timeout in ms for XHR requests
221
223
  }
222
224
 
223
225
  export const enum Character {
@@ -286,7 +288,8 @@ export const enum Constant {
286
288
  ArrayStart = "[",
287
289
  ConditionStart = "{",
288
290
  ConditionEnd = "}",
289
- Seperator = "<SEP>"
291
+ Seperator = "<SEP>",
292
+ Timeout = "Timeout"
290
293
  }
291
294
 
292
295
  export const enum XMLReadyState {
package/types/layout.d.ts CHANGED
@@ -53,6 +53,7 @@ export const enum Constant {
53
53
  Bang = "!",
54
54
  Period = ".",
55
55
  Comma = ",",
56
+ DataAttribute = "data-",
56
57
  MaskData = "data-clarity-mask",
57
58
  UnmaskData = "data-clarity-unmask",
58
59
  RegionData = "data-clarity-region",