@zaplier/sdk 1.6.2 → 1.6.3

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/sdk.js CHANGED
@@ -205,10 +205,19 @@
205
205
  }
206
206
  /**
207
207
  * Generate a visitor ID from fingerprint hash
208
+ * Returns a proper UUID v4 format for database compatibility
208
209
  */
209
210
  function generateVisitorId(fingerprintHash) {
210
- // Use first 16 characters of the hash for visitor ID
211
- return "vis_" + fingerprintHash.substring(0, 16);
211
+ // Generate UUID v4 format from fingerprint hash
212
+ const hash = fingerprintHash.length >= 32 ? fingerprintHash : (fingerprintHash + fingerprintHash + fingerprintHash + fingerprintHash).substring(0, 32);
213
+ // Extract bytes for UUID construction
214
+ const hex = hash.substring(0, 32);
215
+ // Set version 4 and variant bits according to RFC 4122
216
+ const chars = hex.split('');
217
+ chars[12] = '4'; // Version 4
218
+ chars[16] = (parseInt(chars[16] || '0', 16) & 0x3 | 0x8).toString(16); // Variant 10
219
+ // Format as UUID: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
220
+ return `${chars.slice(0, 8).join('')}-${chars.slice(8, 12).join('')}-${chars.slice(12, 16).join('')}-${chars.slice(16, 20).join('')}-${chars.slice(20, 32).join('')}`;
212
221
  }
213
222
  /**
214
223
  * Check if a key represents an unstable component that should be excluded from hashing
@@ -19769,9 +19778,27 @@
19769
19778
  */
19770
19779
  class VisitorIdentityManager {
19771
19780
  generateVisitorId() {
19772
- return "vis_" + Array.from(crypto.getRandomValues(new Uint8Array(16)))
19773
- .map(b => b.toString(16).padStart(2, '0'))
19774
- .join('');
19781
+ // Generate UUID v4 format instead of vis_ prefix
19782
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
19783
+ // Set version 4
19784
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
19785
+ // Set variant
19786
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
19787
+ const hex = Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
19788
+ return `${hex.substr(0, 8)}-${hex.substr(8, 4)}-${hex.substr(12, 4)}-${hex.substr(16, 4)}-${hex.substr(20, 12)}`;
19789
+ }
19790
+ generateVisitorIdFromHash(hash) {
19791
+ // Generate deterministic UUID v4 format from fingerprint hash
19792
+ // This ensures same fingerprint = same visitor ID
19793
+ const hashExtended = hash.length >= 32 ? hash : (hash + hash + hash + hash).substring(0, 32);
19794
+ // Extract hex characters for UUID construction
19795
+ const hex = hashExtended.substring(0, 32);
19796
+ const chars = hex.split('');
19797
+ // Set version 4 and variant bits according to RFC 4122
19798
+ chars[12] = '4'; // Version 4
19799
+ chars[16] = (parseInt(chars[16] || '0', 16) & 0x3 | 0x8).toString(16); // Variant 10
19800
+ // Format as UUID: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
19801
+ return `${chars.slice(0, 8).join('')}-${chars.slice(8, 12).join('')}-${chars.slice(12, 16).join('')}-${chars.slice(16, 20).join('')}-${chars.slice(20, 32).join('')}`;
19775
19802
  }
19776
19803
  generateSessionId() {
19777
19804
  return "ses_" + Array.from(crypto.getRandomValues(new Uint8Array(8)))
@@ -19830,11 +19857,13 @@
19830
19857
  };
19831
19858
  }
19832
19859
  }
19833
- // Create new visitor identity
19834
- const newVisitorId = this.generateVisitorId();
19860
+ // Create new visitor identity (deterministic from fingerprint)
19861
+ // Use stableCoreHash for deterministic visitor ID generation
19862
+ // This ensures same fingerprint = same visitor ID across devices and sessions
19863
+ const deterministicVisitorId = this.generateVisitorIdFromHash(params.stableCoreHash);
19835
19864
  const newSessionId = params.sessionId || this.generateSessionId();
19836
19865
  const newIdentity = {
19837
- visitorId: newVisitorId,
19866
+ visitorId: deterministicVisitorId,
19838
19867
  sessionId: newSessionId,
19839
19868
  stableCoreHash,
19840
19869
  deviceFingerprint: params.deviceFingerprint,
@@ -19845,7 +19874,7 @@
19845
19874
  reused: false,
19846
19875
  };
19847
19876
  // Store in camouflaged format
19848
- const camouflageData = this.createCamouflageData(newVisitorId, newSessionId, stableCoreHash);
19877
+ const camouflageData = this.createCamouflageData(deterministicVisitorId, newSessionId, stableCoreHash);
19849
19878
  const { method: storageMethod } = PersistenceManager.set(STORAGE_KEYS.prefs, JSON.stringify(camouflageData));
19850
19879
  newIdentity.persistenceMethod = storageMethod;
19851
19880
  // Also store device fingerprint cache for faster lookups