@leanbase-giangnd/js 0.2.2 → 0.2.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/package.json CHANGED
@@ -1,47 +1,48 @@
1
1
  {
2
- "name": "@leanbase-giangnd/js",
3
- "version": "0.2.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/tsconfig-base": "1.0.0",
35
- "@posthog-tooling/rollup-utils": "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.2.3",
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
+ }
@@ -759,6 +759,12 @@ export class LazyLoadedSessionRecording {
759
759
  this._makeSamplingDecision(this.sessionId)
760
760
  await this._startRecorder()
761
761
 
762
+ // If rrweb failed to load/start, do not proceed further.
763
+ // This prevents installing listeners that assume rrweb is active.
764
+ if (!this.isStarted) {
765
+ return
766
+ }
767
+
762
768
  // calling addEventListener multiple times is safe and will not add duplicates
763
769
  addEventListener(window, 'beforeunload', this._onBeforeUnload)
764
770
  addEventListener(window, 'offline', this._onOffline)
@@ -1418,13 +1424,19 @@ export class LazyLoadedSessionRecording {
1418
1424
  })
1419
1425
 
1420
1426
  const activePlugins = this._gatherRRWebPlugins()
1421
- this._stopRrweb = rrwebRecord({
1422
- emit: (event) => {
1423
- this.onRRwebEmit(event)
1424
- },
1425
- plugins: activePlugins,
1426
- ...sessionRecordingOptions,
1427
- })
1427
+ try {
1428
+ this._stopRrweb = rrwebRecord({
1429
+ emit: (event) => {
1430
+ this.onRRwebEmit(event)
1431
+ },
1432
+ plugins: activePlugins,
1433
+ ...sessionRecordingOptions,
1434
+ })
1435
+ } catch (e) {
1436
+ logger.error('failed to start rrweb recorder', e)
1437
+ this._stopRrweb = undefined
1438
+ return
1439
+ }
1428
1440
 
1429
1441
  // We reset the last activity timestamp, resetting the idle timer
1430
1442
  this._lastActivityTimestamp = Date.now()
@@ -213,22 +213,29 @@ export class SessionRecording {
213
213
  // If extensions provide an init function, use it. Otherwise, fall back to the local LazyLoadedSessionRecording
214
214
  if (assignableWindow.__PosthogExtensions__?.initSessionRecording) {
215
215
  if (!this._lazyLoadedSessionRecording) {
216
- this._lazyLoadedSessionRecording = assignableWindow.__PosthogExtensions__?.initSessionRecording(
217
- this._instance
218
- )
219
- ;(this._lazyLoadedSessionRecording as any)._forceAllowLocalhostNetworkCapture =
220
- this._forceAllowLocalhostNetworkCapture
216
+ const maybeRecording = assignableWindow.__PosthogExtensions__?.initSessionRecording(this._instance)
217
+ if (maybeRecording && typeof (maybeRecording as any).start === 'function') {
218
+ this._lazyLoadedSessionRecording = maybeRecording
219
+ ;(this._lazyLoadedSessionRecording as any)._forceAllowLocalhostNetworkCapture =
220
+ this._forceAllowLocalhostNetworkCapture
221
+ } else {
222
+ log.warn(
223
+ 'initSessionRecording was present but did not return a recorder instance; falling back to local recorder'
224
+ )
225
+ }
221
226
  }
222
227
 
223
- try {
224
- const maybePromise: any = this._lazyLoadedSessionRecording!.start(startReason)
225
- if (maybePromise && typeof maybePromise.catch === 'function') {
226
- maybePromise.catch((e: any) => logger.error('error starting session recording', e))
228
+ if (this._lazyLoadedSessionRecording) {
229
+ try {
230
+ const maybePromise: any = this._lazyLoadedSessionRecording.start(startReason)
231
+ if (maybePromise && typeof maybePromise.catch === 'function') {
232
+ maybePromise.catch((e: any) => logger.error('error starting session recording', e))
233
+ }
234
+ } catch (e: any) {
235
+ logger.error('error starting session recording', e)
227
236
  }
228
- } catch (e: any) {
229
- logger.error('error starting session recording', e)
237
+ return
230
238
  }
231
- return
232
239
  }
233
240
 
234
241
  if (!this._lazyLoadedSessionRecording) {
package/src/leanbase.ts CHANGED
@@ -93,6 +93,10 @@ export class Leanbase extends PostHogCore {
93
93
  consent!: ConsentManager
94
94
  sessionRecording?: SessionRecording
95
95
  isRemoteConfigLoaded?: boolean
96
+ private _remoteConfigLoadAttempted: boolean = false
97
+ private _remoteConfigResolved: boolean = false
98
+ private _featureFlagsResolved: boolean = false
99
+ private _maybeStartedSessionRecording: boolean = false
96
100
  personProcessingSetOncePropertiesSent = false
97
101
  isLoaded: boolean = false
98
102
  initialPageviewCaptured: boolean
@@ -130,11 +134,21 @@ export class Leanbase extends PostHogCore {
130
134
 
131
135
  if (this.sessionManager && this.config.cookieless_mode !== 'always') {
132
136
  this.sessionRecording = new SessionRecording(this)
133
- this.sessionRecording.startIfEnabledOrStop()
134
137
  }
135
138
 
139
+ // Start session recording only once flags + remote config have been resolved.
140
+ // This matches the PostHog browser SDK where replay activation is driven by remote config and flags.
136
141
  if (this.config.preloadFeatureFlags !== false) {
137
- this.reloadFeatureFlags()
142
+ this.reloadFeatureFlags({
143
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
144
+ cb: (_err) => {
145
+ this._featureFlagsResolved = true
146
+ this._maybeStartSessionRecording()
147
+ },
148
+ })
149
+ } else {
150
+ // If feature flags preload is explicitly disabled, treat this requirement as satisfied.
151
+ this._featureFlagsResolved = true
138
152
  }
139
153
 
140
154
  this.config.loaded?.(this)
@@ -146,9 +160,25 @@ export class Leanbase extends PostHogCore {
146
160
  }, 1)
147
161
  }
148
162
 
149
- addEventListener(document, 'DOMContentLoaded', () => {
150
- this.loadRemoteConfig()
151
- })
163
+ const triggerRemoteConfigLoad = (reason: 'immediate' | 'dom' | 'no-document') => {
164
+ logger.info(`remote config load triggered via ${reason}`)
165
+ void this.loadRemoteConfig()
166
+ }
167
+
168
+ if (document) {
169
+ if (document.readyState === 'loading') {
170
+ logger.info('remote config load deferred until DOMContentLoaded')
171
+ const onDomReady = () => {
172
+ document?.removeEventListener('DOMContentLoaded', onDomReady)
173
+ triggerRemoteConfigLoad('dom')
174
+ }
175
+ addEventListener(document, 'DOMContentLoaded', onDomReady, { once: true } as any)
176
+ } else {
177
+ triggerRemoteConfigLoad('immediate')
178
+ }
179
+ } else {
180
+ triggerRemoteConfigLoad('no-document')
181
+ }
152
182
  addEventListener(window, 'onpagehide' in self ? 'pagehide' : 'unload', this.capturePageLeave.bind(this), {
153
183
  passive: false,
154
184
  })
@@ -191,11 +221,20 @@ export class Leanbase extends PostHogCore {
191
221
  }
192
222
 
193
223
  async loadRemoteConfig() {
194
- if (!this.isRemoteConfigLoaded) {
224
+ if (this._remoteConfigLoadAttempted) {
225
+ return
226
+ }
227
+ this._remoteConfigLoadAttempted = true
228
+
229
+ try {
195
230
  const remoteConfig = await this.reloadRemoteConfigAsync()
196
231
  if (remoteConfig) {
197
232
  this.onRemoteConfig(remoteConfig as RemoteConfig)
198
233
  }
234
+ } finally {
235
+ // Regardless of success/failure, we consider remote config "resolved" so replay isn't blocked forever.
236
+ this._remoteConfigResolved = true
237
+ this._maybeStartSessionRecording()
199
238
  }
200
239
  }
201
240
 
@@ -210,6 +249,29 @@ export class Leanbase extends PostHogCore {
210
249
  this.isRemoteConfigLoaded = true
211
250
  this.replayAutocapture?.onRemoteConfig(config)
212
251
  this.sessionRecording?.onRemoteConfig(config)
252
+
253
+ // Remote config has been applied; allow replay start if flags are also ready.
254
+ this._remoteConfigResolved = true
255
+ this._maybeStartSessionRecording()
256
+ }
257
+
258
+ private _maybeStartSessionRecording(): void {
259
+ if (this._maybeStartedSessionRecording) {
260
+ return
261
+ }
262
+ if (!this.sessionRecording) {
263
+ return
264
+ }
265
+ if (!this._featureFlagsResolved || !this._remoteConfigResolved) {
266
+ return
267
+ }
268
+
269
+ this._maybeStartedSessionRecording = true
270
+ try {
271
+ this.sessionRecording.startIfEnabledOrStop()
272
+ } catch (e) {
273
+ logger.error('Failed to start session recording', e)
274
+ }
213
275
  }
214
276
 
215
277
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '0.2.2'
1
+ export const version = '0.2.3'
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.