@journium/core 0.1.0-alpha.1

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.js ADDED
@@ -0,0 +1,643 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * uuidv7: A JavaScript implementation of UUID version 7
5
+ *
6
+ * Copyright 2021-2024 LiosK
7
+ *
8
+ * @license Apache-2.0
9
+ * @packageDocumentation
10
+ */
11
+ const DIGITS = "0123456789abcdef";
12
+ /** Represents a UUID as a 16-byte byte array. */
13
+ class UUID {
14
+ /** @param bytes - The 16-byte byte array representation. */
15
+ constructor(bytes) {
16
+ this.bytes = bytes;
17
+ }
18
+ /**
19
+ * Creates an object from the internal representation, a 16-byte byte array
20
+ * containing the binary UUID representation in the big-endian byte order.
21
+ *
22
+ * This method does NOT shallow-copy the argument, and thus the created object
23
+ * holds the reference to the underlying buffer.
24
+ *
25
+ * @throws TypeError if the length of the argument is not 16.
26
+ */
27
+ static ofInner(bytes) {
28
+ if (bytes.length !== 16) {
29
+ throw new TypeError("not 128-bit length");
30
+ }
31
+ else {
32
+ return new UUID(bytes);
33
+ }
34
+ }
35
+ /**
36
+ * Builds a byte array from UUIDv7 field values.
37
+ *
38
+ * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
39
+ * @param randA - A 12-bit `rand_a` field value.
40
+ * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
41
+ * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
42
+ * @throws RangeError if any field value is out of the specified range.
43
+ */
44
+ static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
45
+ if (!Number.isInteger(unixTsMs) ||
46
+ !Number.isInteger(randA) ||
47
+ !Number.isInteger(randBHi) ||
48
+ !Number.isInteger(randBLo) ||
49
+ unixTsMs < 0 ||
50
+ randA < 0 ||
51
+ randBHi < 0 ||
52
+ randBLo < 0 ||
53
+ unixTsMs > 281474976710655 ||
54
+ randA > 0xfff ||
55
+ randBHi > 1073741823 ||
56
+ randBLo > 4294967295) {
57
+ throw new RangeError("invalid field value");
58
+ }
59
+ const bytes = new Uint8Array(16);
60
+ bytes[0] = unixTsMs / 2 ** 40;
61
+ bytes[1] = unixTsMs / 2 ** 32;
62
+ bytes[2] = unixTsMs / 2 ** 24;
63
+ bytes[3] = unixTsMs / 2 ** 16;
64
+ bytes[4] = unixTsMs / 2 ** 8;
65
+ bytes[5] = unixTsMs;
66
+ bytes[6] = 0x70 | (randA >>> 8);
67
+ bytes[7] = randA;
68
+ bytes[8] = 0x80 | (randBHi >>> 24);
69
+ bytes[9] = randBHi >>> 16;
70
+ bytes[10] = randBHi >>> 8;
71
+ bytes[11] = randBHi;
72
+ bytes[12] = randBLo >>> 24;
73
+ bytes[13] = randBLo >>> 16;
74
+ bytes[14] = randBLo >>> 8;
75
+ bytes[15] = randBLo;
76
+ return new UUID(bytes);
77
+ }
78
+ /**
79
+ * Builds a byte array from a string representation.
80
+ *
81
+ * This method accepts the following formats:
82
+ *
83
+ * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
84
+ * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
85
+ * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
86
+ * - RFC 9562 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
87
+ *
88
+ * Leading and trailing whitespaces represents an error.
89
+ *
90
+ * @throws SyntaxError if the argument could not parse as a valid UUID string.
91
+ */
92
+ static parse(uuid) {
93
+ var _a, _b, _c, _d;
94
+ let hex = undefined;
95
+ switch (uuid.length) {
96
+ case 32:
97
+ hex = (_a = /^[0-9a-f]{32}$/i.exec(uuid)) === null || _a === void 0 ? void 0 : _a[0];
98
+ break;
99
+ case 36:
100
+ hex =
101
+ (_b = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
102
+ .exec(uuid)) === null || _b === void 0 ? void 0 : _b.slice(1, 6).join("");
103
+ break;
104
+ case 38:
105
+ hex =
106
+ (_c = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
107
+ .exec(uuid)) === null || _c === void 0 ? void 0 : _c.slice(1, 6).join("");
108
+ break;
109
+ case 45:
110
+ hex =
111
+ (_d = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
112
+ .exec(uuid)) === null || _d === void 0 ? void 0 : _d.slice(1, 6).join("");
113
+ break;
114
+ }
115
+ if (hex) {
116
+ const inner = new Uint8Array(16);
117
+ for (let i = 0; i < 16; i += 4) {
118
+ const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
119
+ inner[i + 0] = n >>> 24;
120
+ inner[i + 1] = n >>> 16;
121
+ inner[i + 2] = n >>> 8;
122
+ inner[i + 3] = n;
123
+ }
124
+ return new UUID(inner);
125
+ }
126
+ else {
127
+ throw new SyntaxError("could not parse UUID string");
128
+ }
129
+ }
130
+ /**
131
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
132
+ * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
133
+ */
134
+ toString() {
135
+ let text = "";
136
+ for (let i = 0; i < this.bytes.length; i++) {
137
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
138
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
139
+ if (i === 3 || i === 5 || i === 7 || i === 9) {
140
+ text += "-";
141
+ }
142
+ }
143
+ return text;
144
+ }
145
+ /**
146
+ * @returns The 32-digit hexadecimal representation without hyphens
147
+ * (`0189dcd553117d408db09496a2eef37b`).
148
+ */
149
+ toHex() {
150
+ let text = "";
151
+ for (let i = 0; i < this.bytes.length; i++) {
152
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
153
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
154
+ }
155
+ return text;
156
+ }
157
+ /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
158
+ toJSON() {
159
+ return this.toString();
160
+ }
161
+ /**
162
+ * Reports the variant field value of the UUID or, if appropriate, "NIL" or
163
+ * "MAX".
164
+ *
165
+ * For convenience, this method reports "NIL" or "MAX" if `this` represents
166
+ * the Nil or Max UUID, although the Nil and Max UUIDs are technically
167
+ * subsumed under the variants `0b0` and `0b111`, respectively.
168
+ */
169
+ getVariant() {
170
+ const n = this.bytes[8] >>> 4;
171
+ if (n < 0) {
172
+ throw new Error("unreachable");
173
+ }
174
+ else if (n <= 0b0111) {
175
+ return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
176
+ }
177
+ else if (n <= 0b1011) {
178
+ return "VAR_10";
179
+ }
180
+ else if (n <= 0b1101) {
181
+ return "VAR_110";
182
+ }
183
+ else if (n <= 0b1111) {
184
+ return this.bytes.every((e) => e === 0xff) ? "MAX" : "VAR_RESERVED";
185
+ }
186
+ else {
187
+ throw new Error("unreachable");
188
+ }
189
+ }
190
+ /**
191
+ * Returns the version field value of the UUID or `undefined` if the UUID does
192
+ * not have the variant field value of `0b10`.
193
+ */
194
+ getVersion() {
195
+ return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
196
+ }
197
+ /** Creates an object from `this`. */
198
+ clone() {
199
+ return new UUID(this.bytes.slice(0));
200
+ }
201
+ /** Returns true if `this` is equivalent to `other`. */
202
+ equals(other) {
203
+ return this.compareTo(other) === 0;
204
+ }
205
+ /**
206
+ * Returns a negative integer, zero, or positive integer if `this` is less
207
+ * than, equal to, or greater than `other`, respectively.
208
+ */
209
+ compareTo(other) {
210
+ for (let i = 0; i < 16; i++) {
211
+ const diff = this.bytes[i] - other.bytes[i];
212
+ if (diff !== 0) {
213
+ return Math.sign(diff);
214
+ }
215
+ }
216
+ return 0;
217
+ }
218
+ }
219
+ /**
220
+ * Encapsulates the monotonic counter state.
221
+ *
222
+ * This class provides APIs to utilize a separate counter state from that of the
223
+ * global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
224
+ * the default {@link generate} method, this class has {@link generateOrAbort}
225
+ * that is useful to absolutely guarantee the monotonically increasing order of
226
+ * generated UUIDs. See their respective documentation for details.
227
+ */
228
+ class V7Generator {
229
+ /**
230
+ * Creates a generator object with the default random number generator, or
231
+ * with the specified one if passed as an argument. The specified random
232
+ * number generator should be cryptographically strong and securely seeded.
233
+ */
234
+ constructor(randomNumberGenerator) {
235
+ this.timestamp = 0;
236
+ this.counter = 0;
237
+ this.random = randomNumberGenerator !== null && randomNumberGenerator !== void 0 ? randomNumberGenerator : getDefaultRandom();
238
+ }
239
+ /**
240
+ * Generates a new UUIDv7 object from the current timestamp, or resets the
241
+ * generator upon significant timestamp rollback.
242
+ *
243
+ * This method returns a monotonically increasing UUID by reusing the previous
244
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
245
+ * preceding UUID's. However, when such a clock rollback is considered
246
+ * significant (i.e., by more than ten seconds), this method resets the
247
+ * generator and returns a new UUID based on the given timestamp, breaking the
248
+ * increasing order of UUIDs.
249
+ *
250
+ * See {@link generateOrAbort} for the other mode of generation and
251
+ * {@link generateOrResetCore} for the low-level primitive.
252
+ */
253
+ generate() {
254
+ return this.generateOrResetCore(Date.now(), 10000);
255
+ }
256
+ /**
257
+ * Generates a new UUIDv7 object from the current timestamp, or returns
258
+ * `undefined` upon significant timestamp rollback.
259
+ *
260
+ * This method returns a monotonically increasing UUID by reusing the previous
261
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
262
+ * preceding UUID's. However, when such a clock rollback is considered
263
+ * significant (i.e., by more than ten seconds), this method aborts and
264
+ * returns `undefined` immediately.
265
+ *
266
+ * See {@link generate} for the other mode of generation and
267
+ * {@link generateOrAbortCore} for the low-level primitive.
268
+ */
269
+ generateOrAbort() {
270
+ return this.generateOrAbortCore(Date.now(), 10000);
271
+ }
272
+ /**
273
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
274
+ * generator upon significant timestamp rollback.
275
+ *
276
+ * This method is equivalent to {@link generate} except that it takes a custom
277
+ * timestamp and clock rollback allowance.
278
+ *
279
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
280
+ * considered significant. A suggested value is `10_000` (milliseconds).
281
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
282
+ */
283
+ generateOrResetCore(unixTsMs, rollbackAllowance) {
284
+ let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
285
+ if (value === undefined) {
286
+ // reset state and resume
287
+ this.timestamp = 0;
288
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
289
+ }
290
+ return value;
291
+ }
292
+ /**
293
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
294
+ * `undefined` upon significant timestamp rollback.
295
+ *
296
+ * This method is equivalent to {@link generateOrAbort} except that it takes a
297
+ * custom timestamp and clock rollback allowance.
298
+ *
299
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
300
+ * considered significant. A suggested value is `10_000` (milliseconds).
301
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
302
+ */
303
+ generateOrAbortCore(unixTsMs, rollbackAllowance) {
304
+ const MAX_COUNTER = 4398046511103;
305
+ if (!Number.isInteger(unixTsMs) ||
306
+ unixTsMs < 1 ||
307
+ unixTsMs > 281474976710655) {
308
+ throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
309
+ }
310
+ else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
311
+ throw new RangeError("`rollbackAllowance` out of reasonable range");
312
+ }
313
+ if (unixTsMs > this.timestamp) {
314
+ this.timestamp = unixTsMs;
315
+ this.resetCounter();
316
+ }
317
+ else if (unixTsMs + rollbackAllowance >= this.timestamp) {
318
+ // go on with previous timestamp if new one is not much smaller
319
+ this.counter++;
320
+ if (this.counter > MAX_COUNTER) {
321
+ // increment timestamp at counter overflow
322
+ this.timestamp++;
323
+ this.resetCounter();
324
+ }
325
+ }
326
+ else {
327
+ // abort if clock went backwards to unbearable extent
328
+ return undefined;
329
+ }
330
+ return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
331
+ }
332
+ /** Initializes the counter at a 42-bit random integer. */
333
+ resetCounter() {
334
+ this.counter =
335
+ this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
336
+ }
337
+ /**
338
+ * Generates a new UUIDv4 object utilizing the random number generator inside.
339
+ *
340
+ * @internal
341
+ */
342
+ generateV4() {
343
+ const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
344
+ bytes[6] = 0x40 | (bytes[6] >>> 4);
345
+ bytes[8] = 0x80 | (bytes[8] >>> 2);
346
+ return UUID.ofInner(bytes);
347
+ }
348
+ }
349
+ /** Returns the default random number generator available in the environment. */
350
+ const getDefaultRandom = () => {
351
+ // detect Web Crypto API
352
+ if (typeof crypto !== "undefined" &&
353
+ typeof crypto.getRandomValues !== "undefined") {
354
+ return new BufferedCryptoRandom();
355
+ }
356
+ else {
357
+ // fall back on Math.random() unless the flag is set to true
358
+ if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
359
+ throw new Error("no cryptographically strong RNG available");
360
+ }
361
+ return {
362
+ nextUint32: () => Math.trunc(Math.random() * 65536) * 65536 +
363
+ Math.trunc(Math.random() * 65536),
364
+ };
365
+ }
366
+ };
367
+ /**
368
+ * Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
369
+ * buffer by default to avoid both unbearable throughput decline in some
370
+ * environments and the waste of time and space for unused values.
371
+ */
372
+ class BufferedCryptoRandom {
373
+ constructor() {
374
+ this.buffer = new Uint32Array(8);
375
+ this.cursor = 0xffff;
376
+ }
377
+ nextUint32() {
378
+ if (this.cursor >= this.buffer.length) {
379
+ crypto.getRandomValues(this.buffer);
380
+ this.cursor = 0;
381
+ }
382
+ return this.buffer[this.cursor++];
383
+ }
384
+ }
385
+ let defaultGenerator;
386
+ /**
387
+ * Generates a UUIDv7 string.
388
+ *
389
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
390
+ * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
391
+ */
392
+ const uuidv7 = () => uuidv7obj().toString();
393
+ /** Generates a UUIDv7 object. */
394
+ const uuidv7obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
395
+
396
+ const generateId = () => {
397
+ return Math.random().toString(36).substring(2) + Date.now().toString(36);
398
+ };
399
+ const generateUuidv7 = () => {
400
+ return uuidv7();
401
+ };
402
+ const getCurrentTimestamp = () => {
403
+ return new Date().toISOString();
404
+ };
405
+ const getCurrentUrl = () => {
406
+ if (typeof window !== 'undefined') {
407
+ return window.location.href;
408
+ }
409
+ return '';
410
+ };
411
+ const getPageTitle = () => {
412
+ if (typeof document !== 'undefined') {
413
+ return document.title;
414
+ }
415
+ return '';
416
+ };
417
+ const getReferrer = () => {
418
+ if (typeof document !== 'undefined') {
419
+ return document.referrer;
420
+ }
421
+ return '';
422
+ };
423
+ const isBrowser = () => {
424
+ return typeof window !== 'undefined';
425
+ };
426
+ const isNode = () => {
427
+ var _a;
428
+ return typeof process !== 'undefined' && !!((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
429
+ };
430
+ const fetchRemoteConfig = async (apiHost, token, configEndpoint, fetchFn) => {
431
+ const endpoint = configEndpoint || '/configs';
432
+ const url = `${apiHost}${endpoint}?ingestion_key=${encodeURIComponent(token)}`;
433
+ try {
434
+ let fetch = fetchFn;
435
+ if (!fetch) {
436
+ if (isNode()) {
437
+ // For Node.js environments, expect fetch to be passed in
438
+ throw new Error('Fetch function must be provided in Node.js environment');
439
+ }
440
+ else {
441
+ // Use native fetch in browser
442
+ fetch = window.fetch;
443
+ }
444
+ }
445
+ const response = await fetch(url, {
446
+ method: 'GET',
447
+ headers: {
448
+ 'Content-Type': 'application/json',
449
+ },
450
+ });
451
+ if (!response.ok) {
452
+ throw new Error(`Config fetch failed: ${response.status} ${response.statusText}`);
453
+ }
454
+ const data = await response.json();
455
+ return data;
456
+ }
457
+ catch (error) {
458
+ console.warn('Failed to fetch remote config:', error);
459
+ return null;
460
+ }
461
+ };
462
+ const mergeConfigs = (localConfig, remoteConfig) => {
463
+ if (!remoteConfig) {
464
+ return localConfig;
465
+ }
466
+ // Deep merge remote config into local config
467
+ // Remote config takes precedence over local config
468
+ const merged = { ...localConfig };
469
+ // Handle primitive values
470
+ Object.keys(remoteConfig).forEach(key => {
471
+ if (remoteConfig[key] !== undefined && remoteConfig[key] !== null) {
472
+ if (typeof remoteConfig[key] === 'object' && !Array.isArray(remoteConfig[key])) {
473
+ // Deep merge objects
474
+ merged[key] = {
475
+ ...(merged[key] || {}),
476
+ ...remoteConfig[key]
477
+ };
478
+ }
479
+ else {
480
+ // Override primitive values and arrays
481
+ merged[key] = remoteConfig[key];
482
+ }
483
+ }
484
+ });
485
+ return merged;
486
+ };
487
+
488
+ const DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes in ms
489
+ class BrowserIdentityManager {
490
+ constructor(sessionTimeout, token) {
491
+ this.identity = null;
492
+ this.sessionTimeout = DEFAULT_SESSION_TIMEOUT;
493
+ if (sessionTimeout) {
494
+ this.sessionTimeout = sessionTimeout;
495
+ }
496
+ // Generate storage key with token pattern: jrnm_<token>_journium
497
+ this.storageKey = token ? `jrnm_${token}_journium` : '__journium_identity';
498
+ this.loadOrCreateIdentity();
499
+ }
500
+ loadOrCreateIdentity() {
501
+ if (!this.isBrowser())
502
+ return;
503
+ try {
504
+ const stored = localStorage.getItem(this.storageKey);
505
+ if (stored) {
506
+ const parsedIdentity = JSON.parse(stored);
507
+ // Check if session is expired
508
+ const now = Date.now();
509
+ const sessionAge = now - parsedIdentity.session_timestamp;
510
+ if (sessionAge > this.sessionTimeout) {
511
+ // Session expired, create new session but keep device and distinct IDs
512
+ this.identity = {
513
+ distinct_id: parsedIdentity.distinct_id,
514
+ $device_id: parsedIdentity.$device_id,
515
+ $session_id: generateUuidv7(),
516
+ session_timestamp: now,
517
+ };
518
+ }
519
+ else {
520
+ // Session still valid
521
+ this.identity = parsedIdentity;
522
+ }
523
+ }
524
+ else {
525
+ // First time, create all new IDs
526
+ const newId = generateUuidv7();
527
+ this.identity = {
528
+ distinct_id: newId,
529
+ $device_id: newId,
530
+ $session_id: newId,
531
+ session_timestamp: Date.now(),
532
+ };
533
+ }
534
+ // Save to localStorage
535
+ this.saveIdentity();
536
+ }
537
+ catch (error) {
538
+ console.warn('Journium: Failed to load/create identity:', error);
539
+ // Fallback: create temporary identity without localStorage
540
+ const newId = generateUuidv7();
541
+ this.identity = {
542
+ distinct_id: newId,
543
+ $device_id: newId,
544
+ $session_id: newId,
545
+ session_timestamp: Date.now(),
546
+ };
547
+ }
548
+ }
549
+ saveIdentity() {
550
+ if (!this.isBrowser() || !this.identity)
551
+ return;
552
+ try {
553
+ localStorage.setItem(this.storageKey, JSON.stringify(this.identity));
554
+ }
555
+ catch (error) {
556
+ console.warn('Journium: Failed to save identity to localStorage:', error);
557
+ }
558
+ }
559
+ isBrowser() {
560
+ return typeof window !== 'undefined' && typeof localStorage !== 'undefined';
561
+ }
562
+ getIdentity() {
563
+ return this.identity;
564
+ }
565
+ updateSessionTimeout(timeoutMs) {
566
+ this.sessionTimeout = timeoutMs;
567
+ }
568
+ refreshSession() {
569
+ if (!this.identity)
570
+ return;
571
+ this.identity = {
572
+ ...this.identity,
573
+ $session_id: generateUuidv7(),
574
+ session_timestamp: Date.now(),
575
+ };
576
+ this.saveIdentity();
577
+ }
578
+ getUserAgentInfo() {
579
+ if (!this.isBrowser()) {
580
+ return {
581
+ $raw_user_agent: '',
582
+ $browser: 'Unknown',
583
+ $os: 'Unknown',
584
+ $device_type: 'Unknown',
585
+ };
586
+ }
587
+ const userAgent = navigator.userAgent;
588
+ return {
589
+ $raw_user_agent: userAgent,
590
+ $browser: this.parseBrowser(userAgent),
591
+ $os: this.parseOS(userAgent),
592
+ $device_type: this.parseDeviceType(userAgent),
593
+ };
594
+ }
595
+ parseBrowser(userAgent) {
596
+ if (userAgent.includes('Chrome') && !userAgent.includes('Edg'))
597
+ return 'Chrome';
598
+ if (userAgent.includes('Firefox'))
599
+ return 'Firefox';
600
+ if (userAgent.includes('Safari') && !userAgent.includes('Chrome'))
601
+ return 'Safari';
602
+ if (userAgent.includes('Edg'))
603
+ return 'Edge';
604
+ if (userAgent.includes('Opera') || userAgent.includes('OPR'))
605
+ return 'Opera';
606
+ return 'Unknown';
607
+ }
608
+ parseOS(userAgent) {
609
+ if (userAgent.includes('Windows'))
610
+ return 'Windows';
611
+ if (userAgent.includes('Macintosh') || userAgent.includes('Mac OS'))
612
+ return 'Mac OS';
613
+ if (userAgent.includes('Linux'))
614
+ return 'Linux';
615
+ if (userAgent.includes('Android'))
616
+ return 'Android';
617
+ if (userAgent.includes('iPhone') || userAgent.includes('iPad'))
618
+ return 'iOS';
619
+ return 'Unknown';
620
+ }
621
+ parseDeviceType(userAgent) {
622
+ if (userAgent.includes('Mobile') || userAgent.includes('Android') || userAgent.includes('iPhone')) {
623
+ return 'Mobile';
624
+ }
625
+ if (userAgent.includes('iPad') || userAgent.includes('Tablet')) {
626
+ return 'Tablet';
627
+ }
628
+ return 'Desktop';
629
+ }
630
+ }
631
+
632
+ exports.BrowserIdentityManager = BrowserIdentityManager;
633
+ exports.fetchRemoteConfig = fetchRemoteConfig;
634
+ exports.generateId = generateId;
635
+ exports.generateUuidv7 = generateUuidv7;
636
+ exports.getCurrentTimestamp = getCurrentTimestamp;
637
+ exports.getCurrentUrl = getCurrentUrl;
638
+ exports.getPageTitle = getPageTitle;
639
+ exports.getReferrer = getReferrer;
640
+ exports.isBrowser = isBrowser;
641
+ exports.isNode = isNode;
642
+ exports.mergeConfigs = mergeConfigs;
643
+ //# sourceMappingURL=index.js.map