@leanbase-giangnd/js 0.0.3 → 0.0.5

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
@@ -1185,7 +1185,7 @@ const detectDeviceType = function (user_agent) {
1185
1185
  }
1186
1186
  };
1187
1187
 
1188
- var version = "0.0.3";
1188
+ var version = "0.0.5";
1189
1189
  var packageInfo = {
1190
1190
  version: version};
1191
1191
 
@@ -4417,6 +4417,53 @@ class MutationThrottler {
4417
4417
  }
4418
4418
  }
4419
4419
 
4420
+ /**
4421
+ * Leanbase-only SnapshotTransport
4422
+ * Sends gzip-compressed snapshot payloads directly to the /s/ endpoint.
4423
+ * This bypasses `posthog.capture()` and the analytics /batch/ queue.
4424
+ *
4425
+ * Reasoning: session replay snapshots must not enter the batch/persisted
4426
+ * queues managed by @posthog/core. This transport posts directly to the
4427
+ * recorder endpoint and uses the instance.fetch() implementation so it
4428
+ * runs in the same environment the SDK uses.
4429
+ */
4430
+ async function sendSnapshotDirect(instance, url, payload) {
4431
+ const fetchFn = instance && instance.fetch ? instance.fetch.bind(instance) : globalThis.fetch;
4432
+ const body = JSON.stringify(payload);
4433
+ const compressed = fflate.gzipSync(fflate.strToU8(body));
4434
+ const headers = {
4435
+ 'Content-Encoding': 'gzip',
4436
+ 'Content-Type': 'application/json',
4437
+ Accept: 'application/json'
4438
+ };
4439
+ if (!fetchFn) {
4440
+ // best-effort: if no fetch available, fail silently (recorder should not crash app)
4441
+ try {
4442
+ // eslint-disable-next-line no-console
4443
+ console.debug('[SnapshotTransport] no fetch available, dropping snapshot');
4444
+ } catch {}
4445
+ return {
4446
+ status: 0
4447
+ };
4448
+ }
4449
+ try {
4450
+ // Use the instance.fetch wrapper so environment-specific shims are used.
4451
+ return await fetchFn(url, {
4452
+ method: 'POST',
4453
+ body: compressed,
4454
+ headers
4455
+ });
4456
+ } catch (err) {
4457
+ try {
4458
+ // eslint-disable-next-line no-console
4459
+ console.debug('[SnapshotTransport] send failed', err);
4460
+ } catch {}
4461
+ return {
4462
+ status: 0
4463
+ };
4464
+ }
4465
+ }
4466
+
4420
4467
  function simpleHash(str) {
4421
4468
  let hash = 0;
4422
4469
  for (let i = 0; i < str.length; i++) {
@@ -4444,7 +4491,6 @@ const FIVE_MINUTES = ONE_MINUTE * 5;
4444
4491
  const RECORDING_IDLE_THRESHOLD_MS = FIVE_MINUTES;
4445
4492
  const RECORDING_MAX_EVENT_SIZE = ONE_KB * ONE_KB * 0.9; // ~1mb (with some wiggle room)
4446
4493
  const RECORDING_BUFFER_TIMEOUT = 2000; // 2 seconds
4447
- const SESSION_RECORDING_BATCH_KEY = 'recordings';
4448
4494
  const LOGGER_PREFIX$1 = '[SessionRecording]';
4449
4495
  const logger = createLogger(LOGGER_PREFIX$1);
4450
4496
  const ACTIVE_SOURCES = [IncrementalSource.MouseMove, IncrementalSource.MouseInteraction, IncrementalSource.Scroll, IncrementalSource.ViewportResize, IncrementalSource.Input, IncrementalSource.TouchMove, IncrementalSource.MediaInteraction, IncrementalSource.Drag];
@@ -5162,14 +5208,31 @@ class LazyLoadedSessionRecording {
5162
5208
  }, RECORDING_BUFFER_TIMEOUT);
5163
5209
  }
5164
5210
  }
5165
- _captureSnapshot(properties) {
5166
- // :TRICKY: Make sure we batch these requests, use a custom endpoint and don't truncate the strings.
5167
- this._instance.capture('$snapshot', properties, {
5168
- _url: this._snapshotUrl(),
5169
- _noTruncate: true,
5170
- _batchKey: SESSION_RECORDING_BATCH_KEY,
5171
- skip_client_rate_limiting: true
5172
- });
5211
+ async _captureSnapshot(properties) {
5212
+ // Send snapshots directly to the recorder endpoint to avoid the core
5213
+ // analytics batching and persisted queue. The lifecycle gate in
5214
+ // SessionRecording ensures this method is only called when recording
5215
+ // is allowed.
5216
+ try {
5217
+ const url = this._snapshotUrl();
5218
+ // include minimal metadata expected by /s/ endpoint
5219
+ const payload = {
5220
+ $snapshot_bytes: properties.$snapshot_bytes,
5221
+ $snapshot_data: properties.$snapshot_data,
5222
+ $session_id: properties.$session_id,
5223
+ $window_id: properties.$window_id,
5224
+ $lib: properties.$lib,
5225
+ $lib_version: properties.$lib_version,
5226
+ // include distinct_id from persistence for server-side association
5227
+ distinct_id: this._instance.persistence?.get_property('distinct_id')
5228
+ };
5229
+ await sendSnapshotDirect(this._instance, url, payload);
5230
+ } catch (err) {
5231
+ try {
5232
+ // eslint-disable-next-line no-console
5233
+ console.debug('[SessionRecording] snapshot send failed', err);
5234
+ } catch {}
5235
+ }
5173
5236
  }
5174
5237
  _snapshotUrl() {
5175
5238
  const host = this._instance.config.host || '';
@@ -5885,6 +5948,35 @@ class Leanbase extends core.PostHogCore {
5885
5948
  // eslint-disable-next-line no-console
5886
5949
  console.debug('[Leanbase.fetch] parsed.batch.length=', parsed.batch.length, 'hasSnapshot=', hasSnapshot);
5887
5950
  } catch {}
5951
+ // If remote config has explicitly disabled session recording, drop snapshot events
5952
+ try {
5953
+ // Read persisted remote config that SessionRecording stores
5954
+ const persisted = this.get_property(SESSION_RECORDING_REMOTE_CONFIG);
5955
+ const serverAllowsRecording = !(persisted && persisted.enabled === false || this.config.disable_session_recording === true);
5956
+ if (!serverAllowsRecording && hasSnapshot) {
5957
+ // remove snapshot events from the batch before sending to /batch/
5958
+ parsed.batch = parsed.batch.filter(item => !(item && item.event === '$snapshot'));
5959
+ // If no events remain, short-circuit and avoid sending an empty batch
5960
+ if (!parsed.batch.length) {
5961
+ try {
5962
+ // eslint-disable-next-line no-console
5963
+ console.debug('[Leanbase.fetch] sessionRecording disabled, dropping snapshot-only batch');
5964
+ } catch {}
5965
+ return {
5966
+ status: 200,
5967
+ json: async () => ({})
5968
+ };
5969
+ }
5970
+ // re-encode the body so the underlying fetch receives the modified batch
5971
+ try {
5972
+ const newBody = JSON.stringify(parsed);
5973
+ options = {
5974
+ ...options,
5975
+ body: newBody
5976
+ };
5977
+ } catch {}
5978
+ }
5979
+ } catch {}
5888
5980
  if (hasSnapshot) {
5889
5981
  const host = this.config && this.config.host || '';
5890
5982
  const newUrl = host ? `${host.replace(/\/$/, '')}/s/` : url;