@posthog/core 1.1.0 → 1.2.0

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.
Files changed (126) hide show
  1. package/dist/error-tracking/chunk-ids.js +1 -1
  2. package/dist/error-tracking/chunk-ids.mjs +1 -1
  3. package/dist/error-tracking/coercers/error-event-coercer.js +4 -5
  4. package/dist/error-tracking/coercers/error-event-coercer.mjs +4 -5
  5. package/dist/error-tracking/coercers/event-coercer.js +1 -2
  6. package/dist/error-tracking/coercers/event-coercer.mjs +1 -2
  7. package/dist/error-tracking/coercers/object-coercer.js +1 -2
  8. package/dist/error-tracking/coercers/object-coercer.mjs +1 -2
  9. package/dist/error-tracking/coercers/primitive-coercer.js +1 -2
  10. package/dist/error-tracking/coercers/primitive-coercer.mjs +1 -2
  11. package/dist/error-tracking/coercers/promise-rejection-event.js +4 -5
  12. package/dist/error-tracking/coercers/promise-rejection-event.mjs +4 -5
  13. package/dist/error-tracking/coercers/string-coercer.js +3 -4
  14. package/dist/error-tracking/coercers/string-coercer.mjs +3 -4
  15. package/dist/error-tracking/coercers/utils.js +2 -4
  16. package/dist/error-tracking/coercers/utils.mjs +2 -4
  17. package/dist/error-tracking/error-properties-builder.js +11 -15
  18. package/dist/error-tracking/error-properties-builder.mjs +11 -15
  19. package/dist/error-tracking/parsers/index.js +2 -4
  20. package/dist/error-tracking/parsers/index.mjs +2 -4
  21. package/dist/error-tracking/parsers/node.js +3 -5
  22. package/dist/error-tracking/parsers/node.mjs +3 -5
  23. package/dist/error-tracking/utils.js +4 -4
  24. package/dist/error-tracking/utils.mjs +4 -4
  25. package/dist/eventemitter.js +4 -4
  26. package/dist/eventemitter.mjs +4 -4
  27. package/dist/featureFlagUtils.js +20 -45
  28. package/dist/featureFlagUtils.mjs +20 -45
  29. package/dist/gzip.js +1 -2
  30. package/dist/gzip.mjs +1 -2
  31. package/dist/index.d.ts +4 -366
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +54 -1225
  34. package/dist/index.mjs +5 -1190
  35. package/dist/posthog-core-stateless.d.ts +204 -0
  36. package/dist/posthog-core-stateless.d.ts.map +1 -0
  37. package/dist/posthog-core-stateless.js +675 -0
  38. package/dist/posthog-core-stateless.mjs +632 -0
  39. package/dist/posthog-core.d.ts +171 -0
  40. package/dist/posthog-core.d.ts.map +1 -0
  41. package/dist/posthog-core.js +554 -0
  42. package/dist/posthog-core.mjs +520 -0
  43. package/dist/testing/PostHogCoreTestClient.d.ts +2 -1
  44. package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
  45. package/dist/testing/PostHogCoreTestClient.js +9 -11
  46. package/dist/testing/PostHogCoreTestClient.mjs +8 -10
  47. package/dist/testing/test-utils.js +1 -1
  48. package/dist/testing/test-utils.mjs +1 -1
  49. package/dist/utils/bucketed-rate-limiter.js +8 -12
  50. package/dist/utils/bucketed-rate-limiter.mjs +8 -12
  51. package/dist/utils/index.js +3 -3
  52. package/dist/utils/index.mjs +3 -3
  53. package/dist/utils/type-utils.js +1 -1
  54. package/dist/utils/type-utils.mjs +1 -1
  55. package/dist/vendor/uuidv7.js +12 -16
  56. package/dist/vendor/uuidv7.mjs +12 -16
  57. package/package.json +3 -2
  58. package/src/__tests__/featureFlagUtils.spec.ts +427 -0
  59. package/src/__tests__/gzip.spec.ts +69 -0
  60. package/src/__tests__/posthog.ai.spec.ts +110 -0
  61. package/src/__tests__/posthog.capture.spec.ts +91 -0
  62. package/src/__tests__/posthog.core.spec.ts +135 -0
  63. package/src/__tests__/posthog.debug.spec.ts +36 -0
  64. package/src/__tests__/posthog.enqueue.spec.ts +93 -0
  65. package/src/__tests__/posthog.featureflags.spec.ts +1106 -0
  66. package/src/__tests__/posthog.featureflags.v1.spec.ts +922 -0
  67. package/src/__tests__/posthog.flush.spec.ts +237 -0
  68. package/src/__tests__/posthog.gdpr.spec.ts +50 -0
  69. package/src/__tests__/posthog.groups.spec.ts +96 -0
  70. package/src/__tests__/posthog.identify.spec.ts +194 -0
  71. package/src/__tests__/posthog.init.spec.ts +110 -0
  72. package/src/__tests__/posthog.listeners.spec.ts +51 -0
  73. package/src/__tests__/posthog.register.spec.ts +47 -0
  74. package/src/__tests__/posthog.reset.spec.ts +76 -0
  75. package/src/__tests__/posthog.sessions.spec.ts +63 -0
  76. package/src/__tests__/posthog.setProperties.spec.ts +102 -0
  77. package/src/__tests__/posthog.shutdown.spec.ts +88 -0
  78. package/src/__tests__/utils.spec.ts +36 -0
  79. package/src/error-tracking/chunk-ids.ts +58 -0
  80. package/src/error-tracking/coercers/dom-exception-coercer.ts +38 -0
  81. package/src/error-tracking/coercers/error-coercer.ts +36 -0
  82. package/src/error-tracking/coercers/error-event-coercer.ts +24 -0
  83. package/src/error-tracking/coercers/event-coercer.ts +19 -0
  84. package/src/error-tracking/coercers/index.ts +8 -0
  85. package/src/error-tracking/coercers/object-coercer.ts +76 -0
  86. package/src/error-tracking/coercers/primitive-coercer.ts +19 -0
  87. package/src/error-tracking/coercers/promise-rejection-event.spec.ts +77 -0
  88. package/src/error-tracking/coercers/promise-rejection-event.ts +53 -0
  89. package/src/error-tracking/coercers/string-coercer.spec.ts +26 -0
  90. package/src/error-tracking/coercers/string-coercer.ts +31 -0
  91. package/src/error-tracking/coercers/utils.ts +33 -0
  92. package/src/error-tracking/error-properties-builder.coerce.spec.ts +202 -0
  93. package/src/error-tracking/error-properties-builder.parse.spec.ts +30 -0
  94. package/src/error-tracking/error-properties-builder.ts +169 -0
  95. package/src/error-tracking/index.ts +5 -0
  96. package/src/error-tracking/parsers/base.ts +29 -0
  97. package/src/error-tracking/parsers/chrome.ts +53 -0
  98. package/src/error-tracking/parsers/gecko.ts +38 -0
  99. package/src/error-tracking/parsers/index.ts +104 -0
  100. package/src/error-tracking/parsers/node.ts +111 -0
  101. package/src/error-tracking/parsers/opera.ts +18 -0
  102. package/src/error-tracking/parsers/react-native.ts +0 -0
  103. package/src/error-tracking/parsers/safari.ts +33 -0
  104. package/src/error-tracking/parsers/winjs.ts +12 -0
  105. package/src/error-tracking/types.ts +107 -0
  106. package/src/error-tracking/utils.ts +39 -0
  107. package/src/eventemitter.ts +27 -0
  108. package/src/featureFlagUtils.ts +192 -0
  109. package/src/gzip.ts +29 -0
  110. package/src/index.ts +8 -0
  111. package/src/posthog-core-stateless.ts +1226 -0
  112. package/src/posthog-core.ts +958 -0
  113. package/src/testing/PostHogCoreTestClient.ts +91 -0
  114. package/src/testing/index.ts +2 -0
  115. package/src/testing/test-utils.ts +47 -0
  116. package/src/types.ts +544 -0
  117. package/src/utils/bucketed-rate-limiter.spec.ts +33 -0
  118. package/src/utils/bucketed-rate-limiter.ts +85 -0
  119. package/src/utils/index.ts +98 -0
  120. package/src/utils/number-utils.spec.ts +89 -0
  121. package/src/utils/number-utils.ts +30 -0
  122. package/src/utils/promise-queue.spec.ts +55 -0
  123. package/src/utils/promise-queue.ts +30 -0
  124. package/src/utils/string-utils.ts +23 -0
  125. package/src/utils/type-utils.ts +134 -0
  126. package/src/vendor/uuidv7.ts +479 -0
@@ -0,0 +1,479 @@
1
+ // vendor from: https://github.com/LiosK/uuidv7/blob/f30b7a7faff73afbce0b27a46c638310f96912ba/src/index.ts
2
+ // https://github.com/LiosK/uuidv7#license
3
+
4
+ /**
5
+ * uuidv7: An experimental implementation of the proposed UUID Version 7
6
+ *
7
+ * @license Apache-2.0
8
+ * @copyright 2021-2023 LiosK
9
+ * @packageDocumentation
10
+ */
11
+
12
+ const DIGITS = "0123456789abcdef";
13
+
14
+ /** Represents a UUID as a 16-byte byte array. */
15
+ export class UUID {
16
+ /** @param bytes - The 16-byte byte array representation. */
17
+ private constructor(readonly bytes: Readonly<Uint8Array>) {}
18
+
19
+ /**
20
+ * Creates an object from the internal representation, a 16-byte byte array
21
+ * containing the binary UUID representation in the big-endian byte order.
22
+ *
23
+ * This method does NOT shallow-copy the argument, and thus the created object
24
+ * holds the reference to the underlying buffer.
25
+ *
26
+ * @throws TypeError if the length of the argument is not 16.
27
+ */
28
+ static ofInner(bytes: Readonly<Uint8Array>): UUID {
29
+ if (bytes.length !== 16) {
30
+ throw new TypeError("not 128-bit length");
31
+ } else {
32
+ return new UUID(bytes);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Builds a byte array from UUIDv7 field values.
38
+ *
39
+ * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
40
+ * @param randA - A 12-bit `rand_a` field value.
41
+ * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
42
+ * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
43
+ * @throws RangeError if any field value is out of the specified range.
44
+ */
45
+ static fromFieldsV7(
46
+ unixTsMs: number,
47
+ randA: number,
48
+ randBHi: number,
49
+ randBLo: number,
50
+ ): UUID {
51
+ if (
52
+ !Number.isInteger(unixTsMs) ||
53
+ !Number.isInteger(randA) ||
54
+ !Number.isInteger(randBHi) ||
55
+ !Number.isInteger(randBLo) ||
56
+ unixTsMs < 0 ||
57
+ randA < 0 ||
58
+ randBHi < 0 ||
59
+ randBLo < 0 ||
60
+ unixTsMs > 0xffff_ffff_ffff ||
61
+ randA > 0xfff ||
62
+ randBHi > 0x3fff_ffff ||
63
+ randBLo > 0xffff_ffff
64
+ ) {
65
+ throw new RangeError("invalid field value");
66
+ }
67
+
68
+ const bytes = new Uint8Array(16);
69
+ bytes[0] = unixTsMs / 2 ** 40;
70
+ bytes[1] = unixTsMs / 2 ** 32;
71
+ bytes[2] = unixTsMs / 2 ** 24;
72
+ bytes[3] = unixTsMs / 2 ** 16;
73
+ bytes[4] = unixTsMs / 2 ** 8;
74
+ bytes[5] = unixTsMs;
75
+ bytes[6] = 0x70 | (randA >>> 8);
76
+ bytes[7] = randA;
77
+ bytes[8] = 0x80 | (randBHi >>> 24);
78
+ bytes[9] = randBHi >>> 16;
79
+ bytes[10] = randBHi >>> 8;
80
+ bytes[11] = randBHi;
81
+ bytes[12] = randBLo >>> 24;
82
+ bytes[13] = randBLo >>> 16;
83
+ bytes[14] = randBLo >>> 8;
84
+ bytes[15] = randBLo;
85
+ return new UUID(bytes);
86
+ }
87
+
88
+ /**
89
+ * Builds a byte array from a string representation.
90
+ *
91
+ * This method accepts the following formats:
92
+ *
93
+ * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
94
+ * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
95
+ * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
96
+ * - RFC 4122 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
97
+ *
98
+ * Leading and trailing whitespaces represents an error.
99
+ *
100
+ * @throws SyntaxError if the argument could not parse as a valid UUID string.
101
+ */
102
+ static parse(uuid: string): UUID {
103
+ let hex: string | undefined = undefined;
104
+ switch (uuid.length) {
105
+ case 32:
106
+ hex = /^[0-9a-f]{32}$/i.exec(uuid)?.[0];
107
+ break;
108
+ case 36:
109
+ hex =
110
+ /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
111
+ .exec(uuid)
112
+ ?.slice(1, 6)
113
+ .join("");
114
+ break;
115
+ case 38:
116
+ hex =
117
+ /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
118
+ .exec(uuid)
119
+ ?.slice(1, 6)
120
+ .join("");
121
+ break;
122
+ case 45:
123
+ hex =
124
+ /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
125
+ .exec(uuid)
126
+ ?.slice(1, 6)
127
+ .join("");
128
+ break;
129
+ default:
130
+ break;
131
+ }
132
+
133
+ if (hex) {
134
+ const inner = new Uint8Array(16);
135
+ for (let i = 0; i < 16; i += 4) {
136
+ const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
137
+ inner[i + 0] = n >>> 24;
138
+ inner[i + 1] = n >>> 16;
139
+ inner[i + 2] = n >>> 8;
140
+ inner[i + 3] = n;
141
+ }
142
+ return new UUID(inner);
143
+ } else {
144
+ throw new SyntaxError("could not parse UUID string");
145
+ }
146
+ }
147
+
148
+ /**
149
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
150
+ * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
151
+ */
152
+ toString(): string {
153
+ let text = "";
154
+ for (let i = 0; i < this.bytes.length; i++) {
155
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
156
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
157
+ if (i === 3 || i === 5 || i === 7 || i === 9) {
158
+ text += "-";
159
+ }
160
+ }
161
+ return text;
162
+ }
163
+
164
+ /**
165
+ * @returns The 32-digit hexadecimal representation without hyphens
166
+ * (`0189dcd553117d408db09496a2eef37b`).
167
+ */
168
+ toHex(): string {
169
+ let text = "";
170
+ for (let i = 0; i < this.bytes.length; i++) {
171
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
172
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
173
+ }
174
+ return text;
175
+ }
176
+
177
+ /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
178
+ toJSON(): string {
179
+ return this.toString();
180
+ }
181
+
182
+ /**
183
+ * Reports the variant field value of the UUID or, if appropriate, "NIL" or
184
+ * "MAX".
185
+ *
186
+ * For convenience, this method reports "NIL" or "MAX" if `this` represents
187
+ * the Nil or Max UUID, although the Nil and Max UUIDs are technically
188
+ * subsumed under the variants `0b0` and `0b111`, respectively.
189
+ */
190
+ getVariant():
191
+ | "VAR_0"
192
+ | "VAR_10"
193
+ | "VAR_110"
194
+ | "VAR_RESERVED"
195
+ | "NIL"
196
+ | "MAX" {
197
+ const n = this.bytes[8] >>> 4;
198
+ if (n < 0) {
199
+ throw new Error("unreachable");
200
+ } else if (n <= 0b0111) {
201
+ return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
202
+ } else if (n <= 0b1011) {
203
+ return "VAR_10";
204
+ } else if (n <= 0b1101) {
205
+ return "VAR_110";
206
+ } else if (n <= 0b1111) {
207
+ return this.bytes.every((e) => e === 0xff) ? "MAX" : "VAR_RESERVED";
208
+ } else {
209
+ throw new Error("unreachable");
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Returns the version field value of the UUID or `undefined` if the UUID does
215
+ * not have the variant field value of `0b10`.
216
+ */
217
+ getVersion(): number | undefined {
218
+ return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
219
+ }
220
+
221
+ /** Creates an object from `this`. */
222
+ clone(): UUID {
223
+ return new UUID(this.bytes.slice(0));
224
+ }
225
+
226
+ /** Returns true if `this` is equivalent to `other`. */
227
+ equals(other: UUID): boolean {
228
+ return this.compareTo(other) === 0;
229
+ }
230
+
231
+ /**
232
+ * Returns a negative integer, zero, or positive integer if `this` is less
233
+ * than, equal to, or greater than `other`, respectively.
234
+ */
235
+ compareTo(other: UUID): number {
236
+ for (let i = 0; i < 16; i++) {
237
+ const diff = this.bytes[i] - other.bytes[i];
238
+ if (diff !== 0) {
239
+ return Math.sign(diff);
240
+ }
241
+ }
242
+ return 0;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Encapsulates the monotonic counter state.
248
+ *
249
+ * This class provides APIs to utilize a separate counter state from that of the
250
+ * global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
251
+ * the default {@link generate} method, this class has {@link generateOrAbort}
252
+ * that is useful to absolutely guarantee the monotonically increasing order of
253
+ * generated UUIDs. See their respective documentation for details.
254
+ */
255
+ export class V7Generator {
256
+ private timestamp = 0;
257
+ private counter = 0;
258
+
259
+ /** The random number generator used by the generator. */
260
+ private readonly random: { nextUint32(): number };
261
+
262
+ /**
263
+ * Creates a generator object with the default random number generator, or
264
+ * with the specified one if passed as an argument. The specified random
265
+ * number generator should be cryptographically strong and securely seeded.
266
+ */
267
+ constructor(randomNumberGenerator?: {
268
+ /** Returns a 32-bit random unsigned integer. */
269
+ nextUint32(): number;
270
+ }) {
271
+ this.random = randomNumberGenerator ?? getDefaultRandom();
272
+ }
273
+
274
+ /**
275
+ * Generates a new UUIDv7 object from the current timestamp, or resets the
276
+ * generator upon significant timestamp rollback.
277
+ *
278
+ * This method returns a monotonically increasing UUID by reusing the previous
279
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
280
+ * preceding UUID's. However, when such a clock rollback is considered
281
+ * significant (i.e., by more than ten seconds), this method resets the
282
+ * generator and returns a new UUID based on the given timestamp, breaking the
283
+ * increasing order of UUIDs.
284
+ *
285
+ * See {@link generateOrAbort} for the other mode of generation and
286
+ * {@link generateOrResetCore} for the low-level primitive.
287
+ */
288
+ generate(): UUID {
289
+ return this.generateOrResetCore(Date.now(), 10_000);
290
+ }
291
+
292
+ /**
293
+ * Generates a new UUIDv7 object from the current timestamp, or returns
294
+ * `undefined` upon significant timestamp rollback.
295
+ *
296
+ * This method returns a monotonically increasing UUID by reusing the previous
297
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
298
+ * preceding UUID's. However, when such a clock rollback is considered
299
+ * significant (i.e., by more than ten seconds), this method aborts and
300
+ * returns `undefined` immediately.
301
+ *
302
+ * See {@link generate} for the other mode of generation and
303
+ * {@link generateOrAbortCore} for the low-level primitive.
304
+ */
305
+ generateOrAbort(): UUID | undefined {
306
+ return this.generateOrAbortCore(Date.now(), 10_000);
307
+ }
308
+
309
+ /**
310
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
311
+ * generator upon significant timestamp rollback.
312
+ *
313
+ * This method is equivalent to {@link generate} except that it takes a custom
314
+ * timestamp and clock rollback allowance.
315
+ *
316
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
317
+ * considered significant. A suggested value is `10_000` (milliseconds).
318
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
319
+ */
320
+ generateOrResetCore(unixTsMs: number, rollbackAllowance: number): UUID {
321
+ let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
322
+ if (value === undefined) {
323
+ // reset state and resume
324
+ this.timestamp = 0;
325
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance)!;
326
+ }
327
+ return value;
328
+ }
329
+
330
+ /**
331
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
332
+ * `undefined` upon significant timestamp rollback.
333
+ *
334
+ * This method is equivalent to {@link generateOrAbort} except that it takes a
335
+ * custom timestamp and clock rollback allowance.
336
+ *
337
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
338
+ * considered significant. A suggested value is `10_000` (milliseconds).
339
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
340
+ */
341
+ generateOrAbortCore(
342
+ unixTsMs: number,
343
+ rollbackAllowance: number,
344
+ ): UUID | undefined {
345
+ const MAX_COUNTER = 0x3ff_ffff_ffff;
346
+
347
+ if (
348
+ !Number.isInteger(unixTsMs) ||
349
+ unixTsMs < 1 ||
350
+ unixTsMs > 0xffff_ffff_ffff
351
+ ) {
352
+ throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
353
+ } else if (rollbackAllowance < 0 || rollbackAllowance > 0xffff_ffff_ffff) {
354
+ throw new RangeError("`rollbackAllowance` out of reasonable range");
355
+ }
356
+
357
+ if (unixTsMs > this.timestamp) {
358
+ this.timestamp = unixTsMs;
359
+ this.resetCounter();
360
+ } else if (unixTsMs + rollbackAllowance >= this.timestamp) {
361
+ // go on with previous timestamp if new one is not much smaller
362
+ this.counter++;
363
+ if (this.counter > MAX_COUNTER) {
364
+ // increment timestamp at counter overflow
365
+ this.timestamp++;
366
+ this.resetCounter();
367
+ }
368
+ } else {
369
+ // abort if clock went backwards to unbearable extent
370
+ return undefined;
371
+ }
372
+
373
+ return UUID.fromFieldsV7(
374
+ this.timestamp,
375
+ Math.trunc(this.counter / 2 ** 30),
376
+ this.counter & (2 ** 30 - 1),
377
+ this.random.nextUint32(),
378
+ );
379
+ }
380
+
381
+ /** Initializes the counter at a 42-bit random integer. */
382
+ private resetCounter(): void {
383
+ this.counter =
384
+ this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
385
+ }
386
+
387
+ /**
388
+ * Generates a new UUIDv4 object utilizing the random number generator inside.
389
+ *
390
+ * @internal
391
+ */
392
+ generateV4(): UUID {
393
+ const bytes = new Uint8Array(
394
+ Uint32Array.of(
395
+ this.random.nextUint32(),
396
+ this.random.nextUint32(),
397
+ this.random.nextUint32(),
398
+ this.random.nextUint32(),
399
+ ).buffer,
400
+ );
401
+ bytes[6] = 0x40 | (bytes[6] >>> 4);
402
+ bytes[8] = 0x80 | (bytes[8] >>> 2);
403
+ return UUID.ofInner(bytes);
404
+ }
405
+ }
406
+
407
+ /** A global flag to force use of cryptographically strong RNG. */
408
+ // declare const UUIDV7_DENY_WEAK_RNG: boolean;
409
+
410
+ /** Returns the default random number generator available in the environment. */
411
+ const getDefaultRandom = (): { nextUint32(): number } => {
412
+ // fix: crypto isn't available in react-native, always use Math.random
413
+
414
+ // // detect Web Crypto API
415
+ // if (
416
+ // typeof crypto !== "undefined" &&
417
+ // typeof crypto.getRandomValues !== "undefined"
418
+ // ) {
419
+ // return new BufferedCryptoRandom();
420
+ // } else {
421
+ // // fall back on Math.random() unless the flag is set to true
422
+ // if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
423
+ // throw new Error("no cryptographically strong RNG available");
424
+ // }
425
+ // return {
426
+ // nextUint32: (): number =>
427
+ // Math.trunc(Math.random() * 0x1_0000) * 0x1_0000 +
428
+ // Math.trunc(Math.random() * 0x1_0000),
429
+ // };
430
+ // }
431
+ return {
432
+ nextUint32: (): number =>
433
+ Math.trunc(Math.random() * 0x1_0000) * 0x1_0000 +
434
+ Math.trunc(Math.random() * 0x1_0000),
435
+ };
436
+ };
437
+
438
+ // /**
439
+ // * Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
440
+ // * buffer by default to avoid both unbearable throughput decline in some
441
+ // * environments and the waste of time and space for unused values.
442
+ // */
443
+ // class BufferedCryptoRandom {
444
+ // private readonly buffer = new Uint32Array(8);
445
+ // private cursor = 0xffff;
446
+ // nextUint32(): number {
447
+ // if (this.cursor >= this.buffer.length) {
448
+ // crypto.getRandomValues(this.buffer);
449
+ // this.cursor = 0;
450
+ // }
451
+ // return this.buffer[this.cursor++];
452
+ // }
453
+ // }
454
+
455
+ let defaultGenerator: V7Generator | undefined;
456
+
457
+ /**
458
+ * Generates a UUIDv7 string.
459
+ *
460
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
461
+ * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
462
+ */
463
+ export const uuidv7 = (): string => uuidv7obj().toString();
464
+
465
+ /** Generates a UUIDv7 object. */
466
+ export const uuidv7obj = (): UUID =>
467
+ (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
468
+
469
+ /**
470
+ * Generates a UUIDv4 string.
471
+ *
472
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
473
+ * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
474
+ */
475
+ export const uuidv4 = (): string => uuidv4obj().toString();
476
+
477
+ /** Generates a UUIDv4 object. */
478
+ export const uuidv4obj = (): UUID =>
479
+ (defaultGenerator || (defaultGenerator = new V7Generator())).generateV4();