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