@coveo/relay 0.3.3 → 0.4.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/lib/relay.cjs CHANGED
@@ -26,18 +26,6 @@ async function emit(params) {
26
26
  await callEventApi(params);
27
27
  }
28
28
 
29
- /**
30
- * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
31
- */
32
- function getClientId(environment) {
33
- return environment.generateUUID();
34
- }
35
- function createClientIdManager(environment) {
36
- return {
37
- clientId: getClientId(environment),
38
- };
39
- }
40
-
41
29
  const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
42
30
 
43
31
  let poolPtr = rnds8Pool.length;
@@ -50,6 +38,12 @@ function rng() {
50
38
  return rnds8Pool.slice(poolPtr, poolPtr += 16);
51
39
  }
52
40
 
41
+ var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
42
+
43
+ function validate$1(uuid) {
44
+ return typeof uuid === 'string' && REGEX.test(uuid);
45
+ }
46
+
53
47
  /**
54
48
  * Convert array of 16 byte values to UUID string format of the form:
55
49
  * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
@@ -95,6 +89,70 @@ function v4(options, buf, offset) {
95
89
  return unsafeStringify(rnds);
96
90
  }
97
91
 
92
+ function getClientId(environment) {
93
+ const storage = environment.storage;
94
+ const key = "visitorId";
95
+ const existingClientId = storage.getItem(key);
96
+ const clientId = existingClientId && validate$1(existingClientId)
97
+ ? existingClientId
98
+ : environment.generateUUID();
99
+ storage.setItem(key, clientId);
100
+ return clientId;
101
+ }
102
+ function createClientIdManager(environment) {
103
+ return {
104
+ clientId: getClientId(environment),
105
+ };
106
+ }
107
+
108
+ const cookieManager = createCookieManager();
109
+ function createCookieManager() {
110
+ const prefix = "coveo_";
111
+ const getDomain = (host) => {
112
+ const parts = host.split(".").slice(-2);
113
+ return parts.length == 2 ? parts.join(".") : "";
114
+ };
115
+ return {
116
+ getItem(key) {
117
+ const cookiePrefix = `${prefix}${key}=`;
118
+ const cookieArray = document.cookie.split(";");
119
+ for (const cookie of cookieArray) {
120
+ const prettifyCookie = cookie.replace(/^\s+/, "");
121
+ if (prettifyCookie.lastIndexOf(cookiePrefix, 0) === 0) {
122
+ return prettifyCookie.substring(cookiePrefix.length, prettifyCookie.length);
123
+ }
124
+ }
125
+ return null;
126
+ },
127
+ setItem(key, data, expire) {
128
+ const domain = getDomain(window.location.hostname);
129
+ const expireSection = `;expires=${new Date(new Date().getTime() + expire).toUTCString()}`;
130
+ const domainSection = domain ? `;domain=${domain}` : "";
131
+ document.cookie = `${prefix}${key}=${data}${expireSection}${domainSection};path=/;SameSite=Lax`;
132
+ },
133
+ removeItem(key) {
134
+ this.setItem(key, "", -1);
135
+ },
136
+ };
137
+ }
138
+
139
+ function createBrowserStorage() {
140
+ return {
141
+ getItem(key) {
142
+ return cookieManager.getItem(key) || localStorage.getItem(key);
143
+ },
144
+ removeItem(key) {
145
+ cookieManager.removeItem(key);
146
+ localStorage.removeItem(key);
147
+ },
148
+ setItem(key, data) {
149
+ const oneYear = 31556952000;
150
+ localStorage.setItem(key, data);
151
+ cookieManager.setItem(key, data, oneYear);
152
+ },
153
+ };
154
+ }
155
+
98
156
  function getReferrerUrl() {
99
157
  const referrer = document.referrer;
100
158
  return referrer === "" ? null : referrer;
@@ -107,6 +165,21 @@ function buildBrowserEnvironment() {
107
165
  getUrl: () => window.location.href,
108
166
  getUserAgent: () => navigator.userAgent,
109
167
  generateUUID: () => v4(),
168
+ storage: createBrowserStorage(),
169
+ };
170
+ }
171
+
172
+ function createNullStorage() {
173
+ return {
174
+ getItem() {
175
+ return null;
176
+ },
177
+ removeItem() {
178
+ return;
179
+ },
180
+ setItem() {
181
+ return;
182
+ },
110
183
  };
111
184
  }
112
185
 
@@ -118,6 +191,7 @@ function buildNodeEnvironment() {
118
191
  getUrl: () => null,
119
192
  getUserAgent: () => null,
120
193
  generateUUID: () => v4(),
194
+ storage: createNullStorage(),
121
195
  };
122
196
  }
123
197
 
@@ -133,7 +207,7 @@ function isBrowser() {
133
207
  }
134
208
  }
135
209
 
136
- const version = "0.3.3" ;
210
+ const version = "0.4.0" ;
137
211
 
138
212
  function getEventConfig(config) {
139
213
  const { trackingId } = config;
package/lib/relay.js CHANGED
@@ -22,18 +22,6 @@ async function emit(params) {
22
22
  await callEventApi(params);
23
23
  }
24
24
 
25
- /**
26
- * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
27
- */
28
- function getClientId(environment) {
29
- return environment.generateUUID();
30
- }
31
- function createClientIdManager(environment) {
32
- return {
33
- clientId: getClientId(environment),
34
- };
35
- }
36
-
37
25
  // Unique ID creation requires a high quality random # generator. In the browser we therefore
38
26
  // require the crypto API and do not support built-in fallback to lower quality random number
39
27
  // generators (like Math.random()).
@@ -53,6 +41,12 @@ function rng() {
53
41
  return getRandomValues(rnds8);
54
42
  }
55
43
 
44
+ var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
45
+
46
+ function validate$1(uuid) {
47
+ return typeof uuid === 'string' && REGEX.test(uuid);
48
+ }
49
+
56
50
  /**
57
51
  * Convert array of 16 byte values to UUID string format of the form:
58
52
  * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
@@ -99,6 +93,70 @@ function v4(options, buf, offset) {
99
93
  return unsafeStringify(rnds);
100
94
  }
101
95
 
96
+ function getClientId(environment) {
97
+ const storage = environment.storage;
98
+ const key = "visitorId";
99
+ const existingClientId = storage.getItem(key);
100
+ const clientId = existingClientId && validate$1(existingClientId)
101
+ ? existingClientId
102
+ : environment.generateUUID();
103
+ storage.setItem(key, clientId);
104
+ return clientId;
105
+ }
106
+ function createClientIdManager(environment) {
107
+ return {
108
+ clientId: getClientId(environment),
109
+ };
110
+ }
111
+
112
+ const cookieManager = createCookieManager();
113
+ function createCookieManager() {
114
+ const prefix = "coveo_";
115
+ const getDomain = (host) => {
116
+ const parts = host.split(".").slice(-2);
117
+ return parts.length == 2 ? parts.join(".") : "";
118
+ };
119
+ return {
120
+ getItem(key) {
121
+ const cookiePrefix = `${prefix}${key}=`;
122
+ const cookieArray = document.cookie.split(";");
123
+ for (const cookie of cookieArray) {
124
+ const prettifyCookie = cookie.replace(/^\s+/, "");
125
+ if (prettifyCookie.lastIndexOf(cookiePrefix, 0) === 0) {
126
+ return prettifyCookie.substring(cookiePrefix.length, prettifyCookie.length);
127
+ }
128
+ }
129
+ return null;
130
+ },
131
+ setItem(key, data, expire) {
132
+ const domain = getDomain(window.location.hostname);
133
+ const expireSection = `;expires=${new Date(new Date().getTime() + expire).toUTCString()}`;
134
+ const domainSection = domain ? `;domain=${domain}` : "";
135
+ document.cookie = `${prefix}${key}=${data}${expireSection}${domainSection};path=/;SameSite=Lax`;
136
+ },
137
+ removeItem(key) {
138
+ this.setItem(key, "", -1);
139
+ },
140
+ };
141
+ }
142
+
143
+ function createBrowserStorage() {
144
+ return {
145
+ getItem(key) {
146
+ return cookieManager.getItem(key) || localStorage.getItem(key);
147
+ },
148
+ removeItem(key) {
149
+ cookieManager.removeItem(key);
150
+ localStorage.removeItem(key);
151
+ },
152
+ setItem(key, data) {
153
+ const oneYear = 31556952000;
154
+ localStorage.setItem(key, data);
155
+ cookieManager.setItem(key, data, oneYear);
156
+ },
157
+ };
158
+ }
159
+
102
160
  function getReferrerUrl() {
103
161
  const referrer = document.referrer;
104
162
  return referrer === "" ? null : referrer;
@@ -111,6 +169,21 @@ function buildBrowserEnvironment() {
111
169
  getUrl: () => window.location.href,
112
170
  getUserAgent: () => navigator.userAgent,
113
171
  generateUUID: () => v4(),
172
+ storage: createBrowserStorage(),
173
+ };
174
+ }
175
+
176
+ function createNullStorage() {
177
+ return {
178
+ getItem() {
179
+ return null;
180
+ },
181
+ removeItem() {
182
+ return;
183
+ },
184
+ setItem() {
185
+ return;
186
+ },
114
187
  };
115
188
  }
116
189
 
@@ -122,6 +195,7 @@ function buildNodeEnvironment() {
122
195
  getUrl: () => null,
123
196
  getUserAgent: () => null,
124
197
  generateUUID: () => v4(),
198
+ storage: createNullStorage(),
125
199
  };
126
200
  }
127
201
 
@@ -137,7 +211,7 @@ function isBrowser() {
137
211
  }
138
212
  }
139
213
 
140
- const version = "0.3.3" ;
214
+ const version = "0.4.0" ;
141
215
 
142
216
  function getEventConfig(config) {
143
217
  const { trackingId } = config;
package/lib/relay.mjs CHANGED
@@ -24,18 +24,6 @@ async function emit(params) {
24
24
  await callEventApi(params);
25
25
  }
26
26
 
27
- /**
28
- * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
29
- */
30
- function getClientId(environment) {
31
- return environment.generateUUID();
32
- }
33
- function createClientIdManager(environment) {
34
- return {
35
- clientId: getClientId(environment),
36
- };
37
- }
38
-
39
27
  const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
40
28
 
41
29
  let poolPtr = rnds8Pool.length;
@@ -48,6 +36,12 @@ function rng() {
48
36
  return rnds8Pool.slice(poolPtr, poolPtr += 16);
49
37
  }
50
38
 
39
+ var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
40
+
41
+ function validate$1(uuid) {
42
+ return typeof uuid === 'string' && REGEX.test(uuid);
43
+ }
44
+
51
45
  /**
52
46
  * Convert array of 16 byte values to UUID string format of the form:
53
47
  * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
@@ -93,6 +87,70 @@ function v4(options, buf, offset) {
93
87
  return unsafeStringify(rnds);
94
88
  }
95
89
 
90
+ function getClientId(environment) {
91
+ const storage = environment.storage;
92
+ const key = "visitorId";
93
+ const existingClientId = storage.getItem(key);
94
+ const clientId = existingClientId && validate$1(existingClientId)
95
+ ? existingClientId
96
+ : environment.generateUUID();
97
+ storage.setItem(key, clientId);
98
+ return clientId;
99
+ }
100
+ function createClientIdManager(environment) {
101
+ return {
102
+ clientId: getClientId(environment),
103
+ };
104
+ }
105
+
106
+ const cookieManager = createCookieManager();
107
+ function createCookieManager() {
108
+ const prefix = "coveo_";
109
+ const getDomain = (host) => {
110
+ const parts = host.split(".").slice(-2);
111
+ return parts.length == 2 ? parts.join(".") : "";
112
+ };
113
+ return {
114
+ getItem(key) {
115
+ const cookiePrefix = `${prefix}${key}=`;
116
+ const cookieArray = document.cookie.split(";");
117
+ for (const cookie of cookieArray) {
118
+ const prettifyCookie = cookie.replace(/^\s+/, "");
119
+ if (prettifyCookie.lastIndexOf(cookiePrefix, 0) === 0) {
120
+ return prettifyCookie.substring(cookiePrefix.length, prettifyCookie.length);
121
+ }
122
+ }
123
+ return null;
124
+ },
125
+ setItem(key, data, expire) {
126
+ const domain = getDomain(window.location.hostname);
127
+ const expireSection = `;expires=${new Date(new Date().getTime() + expire).toUTCString()}`;
128
+ const domainSection = domain ? `;domain=${domain}` : "";
129
+ document.cookie = `${prefix}${key}=${data}${expireSection}${domainSection};path=/;SameSite=Lax`;
130
+ },
131
+ removeItem(key) {
132
+ this.setItem(key, "", -1);
133
+ },
134
+ };
135
+ }
136
+
137
+ function createBrowserStorage() {
138
+ return {
139
+ getItem(key) {
140
+ return cookieManager.getItem(key) || localStorage.getItem(key);
141
+ },
142
+ removeItem(key) {
143
+ cookieManager.removeItem(key);
144
+ localStorage.removeItem(key);
145
+ },
146
+ setItem(key, data) {
147
+ const oneYear = 31556952000;
148
+ localStorage.setItem(key, data);
149
+ cookieManager.setItem(key, data, oneYear);
150
+ },
151
+ };
152
+ }
153
+
96
154
  function getReferrerUrl() {
97
155
  const referrer = document.referrer;
98
156
  return referrer === "" ? null : referrer;
@@ -105,6 +163,21 @@ function buildBrowserEnvironment() {
105
163
  getUrl: () => window.location.href,
106
164
  getUserAgent: () => navigator.userAgent,
107
165
  generateUUID: () => v4(),
166
+ storage: createBrowserStorage(),
167
+ };
168
+ }
169
+
170
+ function createNullStorage() {
171
+ return {
172
+ getItem() {
173
+ return null;
174
+ },
175
+ removeItem() {
176
+ return;
177
+ },
178
+ setItem() {
179
+ return;
180
+ },
108
181
  };
109
182
  }
110
183
 
@@ -116,6 +189,7 @@ function buildNodeEnvironment() {
116
189
  getUrl: () => null,
117
190
  getUserAgent: () => null,
118
191
  generateUUID: () => v4(),
192
+ storage: createNullStorage(),
119
193
  };
120
194
  }
121
195
 
@@ -131,7 +205,7 @@ function isBrowser() {
131
205
  }
132
206
  }
133
207
 
134
- const version = "0.3.3" ;
208
+ const version = "0.4.0" ;
135
209
 
136
210
  function getEventConfig(config) {
137
211
  const { trackingId } = config;
@@ -1 +1 @@
1
- {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../../../src/client-id/client-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAEzD,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB;AASD,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,WAAW,GACvB,eAAe,CAIjB"}
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../../../src/client-id/client-id.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAEzD,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAeD,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,WAAW,GACvB,eAAe,CAIjB"}
@@ -1 +1 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../../../src/environment/browser/browser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAQ7C,wBAAgB,uBAAuB,IAAI,WAAW,CASrD"}
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../../../src/environment/browser/browser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAS7C,wBAAgB,uBAAuB,IAAI,WAAW,CAUrD"}
@@ -0,0 +1,7 @@
1
+ export interface CookieManager {
2
+ getItem: (key: string) => string | null;
3
+ removeItem: (key: string) => void;
4
+ setItem: (key: string, data: string, expire: number) => void;
5
+ }
6
+ export declare const cookieManager: CookieManager;
7
+ //# sourceMappingURL=cookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../../../src/environment/browser/storage/cookie.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IACxC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9D;AAED,eAAO,MAAM,aAAa,EAAE,aAAqC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { Storage } from "../../storage";
2
+ export declare function createBrowserStorage(): Storage;
3
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../../../src/environment/browser/storage/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,wBAAgB,oBAAoB,IAAI,OAAO,CAiB9C"}
@@ -1,3 +1,4 @@
1
+ import { Storage } from "./storage";
1
2
  export interface Environment {
2
3
  runtime: "browser" | "node";
3
4
  fetch: (url: string, init?: RequestInit) => Promise<Response>;
@@ -5,6 +6,7 @@ export interface Environment {
5
6
  getUrl: () => string | null;
6
7
  getUserAgent: () => string | null;
7
8
  generateUUID: () => string;
9
+ storage: Storage;
8
10
  }
9
11
  export declare function currentEnvironment(): Environment;
10
12
  //# sourceMappingURL=environment.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../../src/environment/environment.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC;IAC5B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9D,cAAc,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACpC,MAAM,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAClC,YAAY,EAAE,MAAM,MAAM,CAAC;CAC5B;AAED,wBAAgB,kBAAkB,IAAI,WAAW,CAEhD"}
1
+ {"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../../src/environment/environment.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC;IAC5B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9D,cAAc,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACpC,MAAM,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAClC,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,kBAAkB,IAAI,WAAW,CAEhD"}
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../../src/environment/node/node.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,wBAAgB,oBAAoB,IAAI,WAAW,CASlD"}
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../../src/environment/node/node.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,wBAAgB,oBAAoB,IAAI,WAAW,CAUlD"}
@@ -0,0 +1,7 @@
1
+ export interface Storage {
2
+ getItem: (key: string) => string | null;
3
+ removeItem: (key: string) => void;
4
+ setItem: (key: string, data: string) => void;
5
+ }
6
+ export declare function createNullStorage(): Storage;
7
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/environment/storage.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IACxC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9C;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAY3C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coveo/relay",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "description": "A library for sending analytics events using Coveo's Event protocol.",
5
5
  "files": [
6
6
  "lib/**/*"