@leanbase-giangnd/js 0.0.2 → 0.0.4

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.4";
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,99 @@ 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 {}
5886
+ // If remote config has explicitly disabled session recording, drop snapshot events
5887
+ try {
5888
+ // Read persisted remote config that SessionRecording stores
5889
+ const persisted = this.get_property(SESSION_RECORDING_REMOTE_CONFIG);
5890
+ const serverAllowsRecording = !(persisted && persisted.enabled === false || this.config.disable_session_recording === true);
5891
+ if (!serverAllowsRecording && hasSnapshot) {
5892
+ // remove snapshot events from the batch before sending to /batch/
5893
+ parsed.batch = parsed.batch.filter(item => !(item && item.event === '$snapshot'));
5894
+ // If no events remain, short-circuit and avoid sending an empty batch
5895
+ if (!parsed.batch.length) {
5896
+ try {
5897
+ // eslint-disable-next-line no-console
5898
+ console.debug('[Leanbase.fetch] sessionRecording disabled, dropping snapshot-only batch');
5899
+ } catch {}
5900
+ return {
5901
+ status: 200,
5902
+ json: async () => ({})
5903
+ };
5904
+ }
5905
+ // re-encode the body so the underlying fetch receives the modified batch
5906
+ try {
5907
+ const newBody = JSON.stringify(parsed);
5908
+ options = {
5909
+ ...options,
5910
+ body: newBody
5911
+ };
5912
+ } catch {}
5913
+ }
5914
+ } catch {}
5836
5915
  if (hasSnapshot) {
5837
5916
  const host = this.config && this.config.host || '';
5838
5917
  const newUrl = host ? `${host.replace(/\/$/, '')}/s/` : url;
5918
+ try {
5919
+ // eslint-disable-next-line no-console
5920
+ console.debug('[Leanbase.fetch] routing snapshot batch to', newUrl);
5921
+ } catch {}
5839
5922
  return fetchFn(newUrl, options);
5840
5923
  }
5841
5924
  }