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