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