@leanbase-giangnd/js 0.0.2 → 0.0.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/dist/index.cjs +60 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +60 -6
- package/dist/index.mjs.map +1 -1
- package/dist/leanbase.iife.js +8050 -7732
- package/dist/leanbase.iife.js.map +1 -1
- package/package.json +1 -1
- package/src/leanbase.ts +72 -4
- package/src/types/fflate.d.ts +1 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
package/src/leanbase.ts
CHANGED
|
@@ -32,6 +32,7 @@ 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'
|
|
@@ -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
|
-
|
|
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,83 @@ export class Leanbase extends PostHogCore {
|
|
|
221
223
|
if (isPost && isBatchEndpoint && options && options.body) {
|
|
222
224
|
let parsed: any = null
|
|
223
225
|
try {
|
|
224
|
-
const
|
|
225
|
-
|
|
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
|
+
|
|
232
296
|
if (hasSnapshot) {
|
|
233
297
|
const host = (this.config && this.config.host) || ''
|
|
234
298
|
const newUrl = host ? `${host.replace(/\/$/, '')}/s/` : url
|
|
299
|
+
try {
|
|
300
|
+
// eslint-disable-next-line no-console
|
|
301
|
+
console.debug('[Leanbase.fetch] routing snapshot batch to', newUrl)
|
|
302
|
+
} catch {}
|
|
235
303
|
return fetchFn(newUrl, options)
|
|
236
304
|
}
|
|
237
305
|
}
|
package/src/types/fflate.d.ts
CHANGED
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '0.0.
|
|
1
|
+
export const version = '0.0.3'
|