@infinitewatch/web-core 1.0.0 → 1.1.1

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/dist/index.cjs CHANGED
@@ -33,14 +33,16 @@ var getMutationData = (event) => {
33
33
  if (!data || data.source !== 0) return null;
34
34
  return data;
35
35
  };
36
- var LIB_VERSION = "v43.1";
36
+ var LIB_VERSION = "v43.4";
37
37
  var EXCLUDED_ORGANIZATIONS = {
38
- NORTHIUS: "68f7bc5a815d853ecb2ce914"
38
+ // Add organizations here as needed:
39
+ // EXAMPLE_ORG: 'organization_id_here',
39
40
  };
40
41
  var EXCLUDED_ORG_IDS = new Set(Object.values(EXCLUDED_ORGANIZATIONS));
41
42
  var DEFAULT_CONFIG = {
42
43
  baseUrl: "https://ingest.infinitewatch.ai",
43
44
  endpoint: "https://ingest.infinitewatch.ai/v1/ingest",
45
+ endpointInsights: "https://iw-backend-live.fly.dev",
44
46
  endpointConfig: "https://ingest.infinitewatch.ai/v1/ingest/config",
45
47
  sessionId: null,
46
48
  userId: null,
@@ -111,6 +113,31 @@ var getDocument = () => {
111
113
  };
112
114
  var isBrowser = () => !!(getWindow() && getDocument());
113
115
  var now = () => Date.now();
116
+ var getCrypto = () => {
117
+ const win = getWindow();
118
+ if (win?.crypto) {
119
+ return win.crypto;
120
+ }
121
+ if (typeof globalThis !== "undefined" && "crypto" in globalThis) {
122
+ return globalThis.crypto;
123
+ }
124
+ return void 0;
125
+ };
126
+ var randomString = (length) => {
127
+ const cryptoApi = getCrypto();
128
+ if (!cryptoApi) {
129
+ return `${now().toString(36)}${"0".repeat(length)}`.slice(0, length);
130
+ }
131
+ const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
132
+ const bytes = new Uint8Array(length);
133
+ cryptoApi.getRandomValues(bytes);
134
+ let result = "";
135
+ for (let i = 0; i < bytes.length; i += 1) {
136
+ const byte = bytes[i] ?? 0;
137
+ result += alphabet[byte % alphabet.length];
138
+ }
139
+ return result;
140
+ };
114
141
  var isOrganizationExcluded = (orgId) => {
115
142
  return !!orgId && EXCLUDED_ORG_IDS.has(orgId);
116
143
  };
@@ -159,12 +186,12 @@ var createWebCore = () => {
159
186
  }
160
187
  };
161
188
  const generateSessionId = () => {
162
- const nextId = `session_${now()}_${Math.random().toString(36).slice(2, 11)}`;
189
+ const nextId = `session_${now()}_${randomString(9)}`;
163
190
  log("Generated new session ID:", nextId);
164
191
  return nextId;
165
192
  };
166
193
  const generateUserId = () => {
167
- const nextId = `user_${Math.random().toString(36).slice(2, 11)}`;
194
+ const nextId = `user_${randomString(9)}`;
168
195
  log("Generated new user ID:", nextId);
169
196
  return nextId;
170
197
  };
@@ -360,17 +387,26 @@ var createWebCore = () => {
360
387
  if (sessionHeartbeatTimer) {
361
388
  clearInterval(sessionHeartbeatTimer);
362
389
  }
363
- if (config.sessionHeartbeatInterval) {
364
- sessionHeartbeatTimer = setInterval(() => {
365
- if (isSessionActive) {
366
- if (config.refresh) {
367
- updateSessionActivity();
368
- } else {
369
- saveSessionToStorage();
390
+ const checkInterval = config.sessionHeartbeatInterval || 6e4;
391
+ sessionHeartbeatTimer = setInterval(() => {
392
+ if (isSessionActive) {
393
+ if (sessionStartTime) {
394
+ const totalDuration = now() - sessionStartTime;
395
+ if (totalDuration > config.sessionTimeout) {
396
+ log(
397
+ `Session expired (exceeded max duration of ${config.sessionTimeout / 6e4} minutes), stopping session`
398
+ );
399
+ stopRecordingSession();
400
+ return;
370
401
  }
371
402
  }
372
- }, config.sessionHeartbeatInterval);
373
- }
403
+ if (config.refresh) {
404
+ updateSessionActivity();
405
+ } else {
406
+ saveSessionToStorage();
407
+ }
408
+ }
409
+ }, checkInterval);
374
410
  };
375
411
  const stopSessionHeartbeat = () => {
376
412
  if (sessionHeartbeatTimer) {
@@ -764,34 +800,18 @@ var createWebCore = () => {
764
800
  return;
765
801
  }
766
802
  sessionId = hardSessionId || sessionId;
767
- const event = {
768
- type: 9999,
769
- timestamp: now(),
770
- data: {}
771
- };
772
- const payload = {
773
- session_id: sessionId,
774
- user_id: userId,
775
- organization_id: organizationId,
776
- events: [event],
777
- client_version: LIB_VERSION
778
- };
779
- if (externalId) {
780
- payload.external_id = externalId;
803
+ if (!sessionId || !organizationId) {
804
+ return;
781
805
  }
806
+ const base = (config.endpointInsights || config.baseUrl || "").toString().replace(/\/$/, "");
807
+ if (!base) return;
808
+ const endpointUrl = `${base}/v1/sessions/${encodeURIComponent(sessionId)}/insights`;
809
+ const payload = { organization_id: organizationId };
782
810
  const payloadString = JSON.stringify(payload);
783
- const payloadSizeKB = new Blob([payloadString]).size / BYTES_IN_KILOBYTE;
784
811
  try {
785
- const endpointUrl = buildEndpointUrl(
786
- config.endpoint,
787
- "f",
788
- false,
789
- payloadSizeKB,
790
- false
791
- );
792
812
  const response = await fetch(endpointUrl, {
793
813
  method: "POST",
794
- headers: { "Content-Type": "text/plain" },
814
+ headers: { "Content-Type": "application/json" },
795
815
  body: payloadString,
796
816
  mode: "cors",
797
817
  credentials: "omit"
@@ -914,7 +934,8 @@ var createWebCore = () => {
914
934
  const IDLE_MS = 15e3;
915
935
  const BLOCKED_NET_HOSTS = /* @__PURE__ */ new Set([
916
936
  "ingest.infinitewatch.ai",
917
- "ingest.humanbehavior.co"
937
+ "ingest.humanbehavior.co",
938
+ "iw-backend-live.fly.dev"
918
939
  ]);
919
940
  let __iw_lastUserIntentAt = now();
920
941
  let __iw_intentInstalled = false;