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