@zaplier/sdk 1.0.0 → 1.0.2

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.esm.js CHANGED
@@ -4830,7 +4830,7 @@ class ResourceSpoofTransport {
4830
4830
  class WebRTCTransport {
4831
4831
  constructor() {
4832
4832
  this.name = 'webrtc';
4833
- this.available = typeof RTCPeerConnection !== 'undefined';
4833
+ this.available = typeof globalThis.RTCPeerConnection !== 'undefined';
4834
4834
  this.connected = false;
4835
4835
  }
4836
4836
  async send(data, endpoint) {
@@ -4869,7 +4869,7 @@ class WebRTCTransport {
4869
4869
  return;
4870
4870
  return new Promise((resolve, reject) => {
4871
4871
  try {
4872
- this.pc = new RTCPeerConnection({
4872
+ this.pc = new globalThis.RTCPeerConnection({
4873
4873
  iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
4874
4874
  });
4875
4875
  this.dataChannel = this.pc.createDataChannel('tracking', {
@@ -6303,30 +6303,9 @@ class ZaplierSDK {
6303
6303
  * Make HTTP request to backend using anti-adblock system
6304
6304
  */
6305
6305
  async makeRequest(endpoint, data, method = "POST") {
6306
- // Use anti-adblock manager if available
6307
- if (this.antiAdblockManager && method === "POST") {
6308
- try {
6309
- const result = await this.antiAdblockManager.send(data, endpoint);
6310
- if (result.success) {
6311
- if (this.config.debug) {
6312
- console.log(`[Zaplier] Request sent via ${result.method} (${result.latency}ms)`);
6313
- }
6314
- // Return success response for compatibility
6315
- return { success: true, method: result.method };
6316
- }
6317
- else {
6318
- throw new Error(`Anti-adblock failed: ${result.error}`);
6319
- }
6320
- }
6321
- catch (error) {
6322
- if (this.config.debug) {
6323
- console.warn("[Zaplier] Anti-adblock failed, falling back to standard fetch");
6324
- }
6325
- // Fall through to standard fetch
6326
- }
6327
- }
6328
- // Fallback to standard fetch
6329
6306
  const url = `${this.config.apiEndpoint}${endpoint}`;
6307
+ // Always try standard fetch first to get the response JSON (which contains visitorId)
6308
+ // Use anti-adblock as fallback if standard fetch is blocked or fails
6330
6309
  const options = {
6331
6310
  method,
6332
6311
  headers: {
@@ -6336,11 +6315,58 @@ class ZaplierSDK {
6336
6315
  if (method === "POST" && data) {
6337
6316
  options.body = JSON.stringify(data);
6338
6317
  }
6339
- const response = await fetch(url, options);
6340
- if (!response.ok) {
6341
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
6318
+ try {
6319
+ // Add timeout to detect if fetch is blocked/hanging
6320
+ const fetchWithTimeout = Promise.race([
6321
+ fetch(url, options),
6322
+ new Promise((_, reject) => setTimeout(() => reject(new Error("Fetch timeout - possibly blocked")), 5000)),
6323
+ ]);
6324
+ const response = await fetchWithTimeout;
6325
+ if (!response.ok) {
6326
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
6327
+ }
6328
+ const jsonResponse = await response.json();
6329
+ // If anti-adblock is enabled, also send via anti-adblock as a backup
6330
+ // (but don't wait for it or use its response)
6331
+ if (this.antiAdblockManager && method === "POST") {
6332
+ // Send in parallel without blocking
6333
+ this.antiAdblockManager.send(data, endpoint).catch(() => {
6334
+ // Silently fail - we already got the response from standard fetch
6335
+ });
6336
+ }
6337
+ return jsonResponse;
6338
+ }
6339
+ catch (error) {
6340
+ // If standard fetch fails (blocked, timeout, or network error), try anti-adblock as fallback
6341
+ if (this.antiAdblockManager && method === "POST") {
6342
+ if (this.config.debug) {
6343
+ console.warn(`[Zaplier] Standard fetch failed (${error instanceof Error ? error.message : String(error)}), trying anti-adblock fallback`);
6344
+ }
6345
+ try {
6346
+ const result = await this.antiAdblockManager.send(data, endpoint);
6347
+ if (result.success) {
6348
+ if (this.config.debug) {
6349
+ console.log(`[Zaplier] Request sent via ${result.method} (${result.latency}ms) as fallback`);
6350
+ }
6351
+ // Event was successfully sent via anti-adblock fallback
6352
+ // Note: visitorId will be obtained on the next successful request
6353
+ // (either standard fetch or anti-adblock) when the backend responds with it
6354
+ // This is acceptable because:
6355
+ // 1. The event was successfully tracked
6356
+ // 2. The visitorId will be available after the next event is sent
6357
+ // 3. Making an additional GET request here could also be blocked
6358
+ return { success: true, method: result.method };
6359
+ }
6360
+ }
6361
+ catch (antiAdblockError) {
6362
+ if (this.config.debug) {
6363
+ console.warn("[Zaplier] Both standard fetch and anti-adblock failed:", antiAdblockError);
6364
+ }
6365
+ }
6366
+ }
6367
+ // Re-throw original error if all methods failed
6368
+ throw error;
6342
6369
  }
6343
- return response.json();
6344
6370
  }
6345
6371
  /**
6346
6372
  * Process queued events