@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/package.json CHANGED
@@ -1,47 +1,48 @@
1
1
  {
2
- "name": "@leanbase-giangnd/js",
3
- "version": "0.0.2",
4
- "description": "Leanbase browser SDK - event tracking, autocapture, and session replay",
5
- "repository": {
6
- "type": "git",
7
- "directory": "packages/leanbase"
8
- },
9
- "author": "leanbase",
10
- "license": "Copyrighted by Leanflag Limited",
11
- "main": "dist/index.cjs",
12
- "module": "dist/index.mjs",
13
- "types": "dist/index.d.ts",
14
- "unpkg": "dist/leanbase.iife.js",
15
- "jsdelivr": "dist/leanbase.iife.js",
16
- "files": [
17
- "dist",
18
- "src",
19
- "README.md"
20
- ],
21
- "publishConfig": {
22
- "access": "public"
23
- },
24
- "dependencies": {
25
- "@rrweb/record": "2.0.0-alpha.17",
26
- "fflate": "^0.4.8",
27
- "@posthog/core": "1.3.1"
28
- },
29
- "devDependencies": {
30
- "jest": "^29.7.0",
31
- "jest-environment-jsdom": "^29.7.0",
32
- "rollup": "^4.44.1",
33
- "rimraf": "^6.0.1",
34
- "@posthog-tooling/rollup-utils": "1.0.0",
35
- "@posthog-tooling/tsconfig-base": "1.0.0"
36
- },
37
- "scripts": {
38
- "clean": "rimraf dist coverage",
39
- "test:unit": "jest -c jest.config.js",
40
- "lint": "eslint src test",
41
- "lint:fix": "eslint src test --fix",
42
- "prebuild": "node -p \"'export const version = \\'' + require('./package.json').version + '\\''\" > src/version.ts",
43
- "build": "rollup -c",
44
- "dev": "rollup -c -w",
45
- "package": "mkdir -p ../../target && pnpm pack --pack-destination ../../target"
46
- }
47
- }
2
+ "name": "@leanbase-giangnd/js",
3
+ "version": "0.0.4",
4
+ "description": "Leanbase browser SDK - event tracking, autocapture, and session replay",
5
+ "repository": {
6
+ "type": "git",
7
+ "directory": "packages/leanbase"
8
+ },
9
+ "author": "leanbase",
10
+ "license": "Copyrighted by Leanflag Limited",
11
+ "main": "dist/index.cjs",
12
+ "module": "dist/index.mjs",
13
+ "types": "dist/index.d.ts",
14
+ "unpkg": "dist/leanbase.iife.js",
15
+ "jsdelivr": "dist/leanbase.iife.js",
16
+ "scripts": {
17
+ "clean": "rimraf dist coverage",
18
+ "test:unit": "jest -c jest.config.js",
19
+ "lint": "eslint src test",
20
+ "lint:fix": "eslint src test --fix",
21
+ "prebuild": "node -p \"'export const version = \\'' + require('./package.json').version + '\\''\" > src/version.ts",
22
+ "build": "rollup -c",
23
+ "dev": "rollup -c -w",
24
+ "prepublishOnly": "pnpm lint && pnpm test:unit && pnpm build",
25
+ "package": "mkdir -p ../../target && pnpm pack --pack-destination ../../target"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "src",
30
+ "README.md"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "@posthog/core": "workspace:*",
37
+ "@rrweb/record": "2.0.0-alpha.17",
38
+ "fflate": "^0.4.8"
39
+ },
40
+ "devDependencies": {
41
+ "@posthog-tooling/tsconfig-base": "workspace:*",
42
+ "@posthog-tooling/rollup-utils": "workspace:*",
43
+ "jest": "catalog:",
44
+ "jest-environment-jsdom": "catalog:",
45
+ "rollup": "catalog:",
46
+ "rimraf": "^6.0.1"
47
+ }
48
+ }
package/src/leanbase.ts CHANGED
@@ -32,10 +32,11 @@ import {
32
32
  navigator,
33
33
  userAgent,
34
34
  } from './utils'
35
+ import { decompressSync, strFromU8 } from 'fflate'
35
36
  import Config from './config'
36
37
  import { Autocapture } from './autocapture'
37
38
  import { logger } from './leanbase-logger'
38
- import { COOKIELESS_MODE_FLAG_PROPERTY, USER_STATE } from './constants'
39
+ import { COOKIELESS_MODE_FLAG_PROPERTY, USER_STATE, SESSION_RECORDING_REMOTE_CONFIG } from './constants'
39
40
  import { getEventProperties } from './utils/event-utils'
40
41
  import { SessionIdManager } from './sessionid'
41
42
  import { SessionPropsManager } from './session-props'
@@ -209,11 +210,12 @@ export class Leanbase extends PostHogCore {
209
210
  this.sessionRecording?.onRemoteConfig(config)
210
211
  }
211
212
 
212
- fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
213
+ async fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
213
214
  const fetchFn = getFetch()
214
215
  if (!fetchFn) {
215
- return Promise.reject(new Error('Fetch API is not available in this environment.'))
216
+ throw new Error('Fetch API is not available in this environment.')
216
217
  }
218
+
217
219
  try {
218
220
  const isPost = !options.method || options.method.toUpperCase() === 'POST'
219
221
  const isBatchEndpoint = typeof url === 'string' && url.endsWith('/batch/')
@@ -221,17 +223,113 @@ export class Leanbase extends PostHogCore {
221
223
  if (isPost && isBatchEndpoint && options && options.body) {
222
224
  let parsed: any = null
223
225
  try {
224
- const bodyString = typeof options.body === 'string' ? options.body : String(options.body)
225
- parsed = JSON.parse(bodyString)
226
+ const headers = (options.headers || {}) as Record<string, any>
227
+ const contentEncoding = (
228
+ headers['Content-Encoding'] ||
229
+ headers['content-encoding'] ||
230
+ ''
231
+ ).toLowerCase()
232
+
233
+ const toUint8 = async (body: any): Promise<Uint8Array | null> => {
234
+ if (typeof body === 'string') return new TextEncoder().encode(body)
235
+ if (typeof Blob !== 'undefined' && body instanceof Blob) {
236
+ const ab = await body.arrayBuffer()
237
+ return new Uint8Array(ab)
238
+ }
239
+ if (body instanceof ArrayBuffer) return new Uint8Array(body)
240
+ if (ArrayBuffer.isView(body)) return new Uint8Array((body as any).buffer ?? body)
241
+ try {
242
+ return new TextEncoder().encode(String(body))
243
+ } catch {
244
+ return null
245
+ }
246
+ }
247
+
248
+ if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
249
+ const u8 = await toUint8(options.body)
250
+ if (u8) {
251
+ try {
252
+ const dec = decompressSync(u8)
253
+ const s = strFromU8(dec)
254
+ parsed = JSON.parse(s)
255
+ } catch {
256
+ parsed = null
257
+ }
258
+ }
259
+ } else {
260
+ if (typeof options.body === 'string') {
261
+ parsed = JSON.parse(options.body)
262
+ } else {
263
+ const u8 = await toUint8(options.body)
264
+ if (u8) {
265
+ try {
266
+ parsed = JSON.parse(new TextDecoder().decode(u8))
267
+ } catch {
268
+ parsed = null
269
+ }
270
+ } else {
271
+ try {
272
+ parsed = JSON.parse(String(options.body))
273
+ } catch {
274
+ parsed = null
275
+ }
276
+ }
277
+ }
278
+ }
226
279
  } catch {
227
280
  parsed = null
228
281
  }
229
282
 
230
283
  if (parsed && isArray(parsed.batch)) {
231
284
  const hasSnapshot = parsed.batch.some((item: any) => item && item.event === '$snapshot')
285
+ // Debug logging to help diagnose routing issues
286
+ try {
287
+ // eslint-disable-next-line no-console
288
+ console.debug(
289
+ '[Leanbase.fetch] parsed.batch.length=',
290
+ parsed.batch.length,
291
+ 'hasSnapshot=',
292
+ hasSnapshot
293
+ )
294
+ } catch {}
295
+
296
+ // If remote config has explicitly disabled session recording, drop snapshot events
297
+ try {
298
+ // Read persisted remote config that SessionRecording stores
299
+ const persisted: any = this.get_property(SESSION_RECORDING_REMOTE_CONFIG)
300
+ const serverAllowsRecording = !(
301
+ (persisted && persisted.enabled === false) ||
302
+ this.config.disable_session_recording === true
303
+ )
304
+
305
+ if (!serverAllowsRecording && hasSnapshot) {
306
+ // remove snapshot events from the batch before sending to /batch/
307
+ parsed.batch = parsed.batch.filter((item: any) => !(item && item.event === '$snapshot'))
308
+ // If no events remain, short-circuit and avoid sending an empty batch
309
+ if (!parsed.batch.length) {
310
+ try {
311
+ // eslint-disable-next-line no-console
312
+ console.debug(
313
+ '[Leanbase.fetch] sessionRecording disabled, dropping snapshot-only batch'
314
+ )
315
+ } catch {}
316
+ return { status: 200, json: async () => ({}) } as any
317
+ }
318
+ // re-encode the body so the underlying fetch receives the modified batch
319
+ try {
320
+ const newBody = JSON.stringify(parsed)
321
+ options = { ...options, body: newBody }
322
+ } catch {}
323
+ }
324
+ } catch {}
325
+
232
326
  if (hasSnapshot) {
233
327
  const host = (this.config && this.config.host) || ''
234
328
  const newUrl = host ? `${host.replace(/\/$/, '')}/s/` : url
329
+ try {
330
+ // eslint-disable-next-line no-console
331
+ console.debug('[Leanbase.fetch] routing snapshot batch to', newUrl)
332
+ } catch {}
235
333
  return fetchFn(newUrl, options)
236
334
  }
237
335
  }
@@ -1,5 +1,6 @@
1
1
  declare module 'fflate' {
2
2
  export const gzipSync: (...args: any[]) => any
3
3
  export const strFromU8: (...args: any[]) => any
4
+ export const decompressSync: (...args: any[]) => any
4
5
  export const strToU8: (...args: any[]) => any
5
6
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '0.0.2'
1
+ export const version = '0.0.4'
package/LICENSE DELETED
@@ -1,37 +0,0 @@
1
- Copyright 2025 Leanflag Limited
2
- This project is a fork of PostHog/posthog-js.
3
-
4
- All rights reserved. This software is proprietary to Leanflag Limited and may only be used for Leanflag Limited products and internal purposes. Redistribution, modification, or commercial use outside of Leanflag Limited is strictly prohibited without explicit written permission from Leanflag Limited.
5
-
6
-
7
- ---
8
-
9
- Copyright 2020 Posthog / Hiberly, Inc.
10
-
11
- Copyright 2015 Mixpanel, Inc.
12
-
13
-
14
- Some files in this codebase contain code from getsentry/sentry-javascript by Software, Inc. dba Sentry.
15
- In such cases it is explicitly stated in the file header. This license only applies to the relevant code in such cases.
16
-
17
- MIT License
18
-
19
- Copyright (c) 2012 Functional Software, Inc. dba Sentry
20
-
21
- Permission is hereby granted, free of charge, to any person obtaining a copy of
22
- this software and associated documentation files (the "Software"), to deal in
23
- the Software without restriction, including without limitation the rights to
24
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
25
- of the Software, and to permit persons to whom the Software is furnished to do
26
- so, subject to the following conditions:
27
-
28
- The above copyright notice and this permission notice shall be included in all
29
- copies or substantial portions of the Software.
30
-
31
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
- SOFTWARE.