@leanbase-giangnd/js 0.0.2 → 0.0.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/index.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@posthog/core');
4
- var record = require('@rrweb/record');
5
4
  var fflate = require('fflate');
5
+ var record = require('@rrweb/record');
6
6
 
7
7
  const breaker = {};
8
8
  const ArrayProto = Array.prototype;
@@ -1185,7 +1185,7 @@ const detectDeviceType = function (user_agent) {
1185
1185
  }
1186
1186
  };
1187
1187
 
1188
- var version = "0.0.2";
1188
+ var version = "0.0.3";
1189
1189
  var packageInfo = {
1190
1190
  version: version};
1191
1191
 
@@ -5817,10 +5817,10 @@ class Leanbase extends core.PostHogCore {
5817
5817
  this.replayAutocapture?.onRemoteConfig(config);
5818
5818
  this.sessionRecording?.onRemoteConfig(config);
5819
5819
  }
5820
- fetch(url, options) {
5820
+ async fetch(url, options) {
5821
5821
  const fetchFn = core.getFetch();
5822
5822
  if (!fetchFn) {
5823
- return Promise.reject(new Error('Fetch API is not available in this environment.'));
5823
+ throw new Error('Fetch API is not available in this environment.');
5824
5824
  }
5825
5825
  try {
5826
5826
  const isPost = !options.method || options.method.toUpperCase() === 'POST';
@@ -5828,16 +5828,70 @@ class Leanbase extends core.PostHogCore {
5828
5828
  if (isPost && isBatchEndpoint && options && options.body) {
5829
5829
  let parsed = null;
5830
5830
  try {
5831
- const bodyString = typeof options.body === 'string' ? options.body : String(options.body);
5832
- parsed = JSON.parse(bodyString);
5831
+ const headers = options.headers || {};
5832
+ const contentEncoding = (headers['Content-Encoding'] || headers['content-encoding'] || '').toLowerCase();
5833
+ const toUint8 = async body => {
5834
+ if (typeof body === 'string') return new TextEncoder().encode(body);
5835
+ if (typeof Blob !== 'undefined' && body instanceof Blob) {
5836
+ const ab = await body.arrayBuffer();
5837
+ return new Uint8Array(ab);
5838
+ }
5839
+ if (body instanceof ArrayBuffer) return new Uint8Array(body);
5840
+ if (ArrayBuffer.isView(body)) return new Uint8Array(body.buffer ?? body);
5841
+ try {
5842
+ return new TextEncoder().encode(String(body));
5843
+ } catch {
5844
+ return null;
5845
+ }
5846
+ };
5847
+ if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
5848
+ const u8 = await toUint8(options.body);
5849
+ if (u8) {
5850
+ try {
5851
+ const dec = fflate.decompressSync(u8);
5852
+ const s = fflate.strFromU8(dec);
5853
+ parsed = JSON.parse(s);
5854
+ } catch {
5855
+ parsed = null;
5856
+ }
5857
+ }
5858
+ } else {
5859
+ if (typeof options.body === 'string') {
5860
+ parsed = JSON.parse(options.body);
5861
+ } else {
5862
+ const u8 = await toUint8(options.body);
5863
+ if (u8) {
5864
+ try {
5865
+ parsed = JSON.parse(new TextDecoder().decode(u8));
5866
+ } catch {
5867
+ parsed = null;
5868
+ }
5869
+ } else {
5870
+ try {
5871
+ parsed = JSON.parse(String(options.body));
5872
+ } catch {
5873
+ parsed = null;
5874
+ }
5875
+ }
5876
+ }
5877
+ }
5833
5878
  } catch {
5834
5879
  parsed = null;
5835
5880
  }
5836
5881
  if (parsed && core.isArray(parsed.batch)) {
5837
5882
  const hasSnapshot = parsed.batch.some(item => item && item.event === '$snapshot');
5883
+ // Debug logging to help diagnose routing issues
5884
+ try {
5885
+ // eslint-disable-next-line no-console
5886
+ console.debug('[Leanbase.fetch] parsed.batch.length=', parsed.batch.length, 'hasSnapshot=', hasSnapshot);
5887
+ } catch {}
5838
5888
  if (hasSnapshot) {
5839
5889
  const host = this.config && this.config.host || '';
5840
5890
  const newUrl = host ? `${host.replace(/\/$/, '')}/s/` : url;
5891
+ try {
5892
+ // eslint-disable-next-line no-console
5893
+ console.debug('[Leanbase.fetch] routing snapshot batch to', newUrl);
5894
+ } catch {}
5841
5895
  return fetchFn(newUrl, options);
5842
5896
  }
5843
5897
  }