posthog-node 3.6.1 → 3.6.2

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/lib/index.esm.js CHANGED
@@ -154,7 +154,7 @@ function __spreadArray(to, from, pack) {
154
154
  return to.concat(ar || Array.prototype.slice.call(from));
155
155
  }
156
156
 
157
- var version = "3.6.1";
157
+ var version = "3.6.2";
158
158
 
159
159
  var PostHogPersistedProperty;
160
160
  (function (PostHogPersistedProperty) {
@@ -223,26 +223,6 @@ function retriable(fn, props) {
223
223
  });
224
224
  });
225
225
  }
226
- // https://stackoverflow.com/a/8809472
227
- function generateUUID(globalThis) {
228
- // Public Domain/MIT
229
- var d = new Date().getTime(); //Timestamp
230
- var d2 = (globalThis && globalThis.performance && globalThis.performance.now && globalThis.performance.now() * 1000) || 0; //Time in microseconds since page-load or 0 if unsupported
231
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
232
- var r = Math.random() * 16; //random number between 0 and 16
233
- if (d > 0) {
234
- //Use timestamp until depleted
235
- r = (d + r) % 16 | 0;
236
- d = Math.floor(d / 16);
237
- }
238
- else {
239
- //Use microseconds since page-load if supported
240
- r = (d2 + r) % 16 | 0;
241
- d2 = Math.floor(d2 / 16);
242
- }
243
- return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
244
- });
245
- }
246
226
  function currentTimestamp() {
247
227
  return new Date().getTime();
248
228
  }
@@ -717,6 +697,401 @@ var SimpleEventEmitter = /** @class */ (function () {
717
697
  return SimpleEventEmitter;
718
698
  }());
719
699
 
700
+ /**
701
+ * uuidv7: An experimental implementation of the proposed UUID Version 7
702
+ *
703
+ * @license Apache-2.0
704
+ * @copyright 2021-2023 LiosK
705
+ * @packageDocumentation
706
+ */
707
+ var DIGITS = "0123456789abcdef";
708
+ /** Represents a UUID as a 16-byte byte array. */
709
+ var UUID = /** @class */ (function () {
710
+ /** @param bytes - The 16-byte byte array representation. */
711
+ function UUID(bytes) {
712
+ this.bytes = bytes;
713
+ }
714
+ /**
715
+ * Creates an object from the internal representation, a 16-byte byte array
716
+ * containing the binary UUID representation in the big-endian byte order.
717
+ *
718
+ * This method does NOT shallow-copy the argument, and thus the created object
719
+ * holds the reference to the underlying buffer.
720
+ *
721
+ * @throws TypeError if the length of the argument is not 16.
722
+ */
723
+ UUID.ofInner = function (bytes) {
724
+ if (bytes.length !== 16) {
725
+ throw new TypeError("not 128-bit length");
726
+ }
727
+ else {
728
+ return new UUID(bytes);
729
+ }
730
+ };
731
+ /**
732
+ * Builds a byte array from UUIDv7 field values.
733
+ *
734
+ * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
735
+ * @param randA - A 12-bit `rand_a` field value.
736
+ * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
737
+ * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
738
+ * @throws RangeError if any field value is out of the specified range.
739
+ */
740
+ UUID.fromFieldsV7 = function (unixTsMs, randA, randBHi, randBLo) {
741
+ if (!Number.isInteger(unixTsMs) ||
742
+ !Number.isInteger(randA) ||
743
+ !Number.isInteger(randBHi) ||
744
+ !Number.isInteger(randBLo) ||
745
+ unixTsMs < 0 ||
746
+ randA < 0 ||
747
+ randBHi < 0 ||
748
+ randBLo < 0 ||
749
+ unixTsMs > 281474976710655 ||
750
+ randA > 0xfff ||
751
+ randBHi > 1073741823 ||
752
+ randBLo > 4294967295) {
753
+ throw new RangeError("invalid field value");
754
+ }
755
+ var bytes = new Uint8Array(16);
756
+ bytes[0] = unixTsMs / Math.pow(2, 40);
757
+ bytes[1] = unixTsMs / Math.pow(2, 32);
758
+ bytes[2] = unixTsMs / Math.pow(2, 24);
759
+ bytes[3] = unixTsMs / Math.pow(2, 16);
760
+ bytes[4] = unixTsMs / Math.pow(2, 8);
761
+ bytes[5] = unixTsMs;
762
+ bytes[6] = 0x70 | (randA >>> 8);
763
+ bytes[7] = randA;
764
+ bytes[8] = 0x80 | (randBHi >>> 24);
765
+ bytes[9] = randBHi >>> 16;
766
+ bytes[10] = randBHi >>> 8;
767
+ bytes[11] = randBHi;
768
+ bytes[12] = randBLo >>> 24;
769
+ bytes[13] = randBLo >>> 16;
770
+ bytes[14] = randBLo >>> 8;
771
+ bytes[15] = randBLo;
772
+ return new UUID(bytes);
773
+ };
774
+ /**
775
+ * Builds a byte array from a string representation.
776
+ *
777
+ * This method accepts the following formats:
778
+ *
779
+ * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
780
+ * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
781
+ * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
782
+ * - RFC 4122 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
783
+ *
784
+ * Leading and trailing whitespaces represents an error.
785
+ *
786
+ * @throws SyntaxError if the argument could not parse as a valid UUID string.
787
+ */
788
+ UUID.parse = function (uuid) {
789
+ var _a, _b, _c, _d;
790
+ var hex = undefined;
791
+ switch (uuid.length) {
792
+ case 32:
793
+ hex = (_a = /^[0-9a-f]{32}$/i.exec(uuid)) === null || _a === void 0 ? void 0 : _a[0];
794
+ break;
795
+ case 36:
796
+ hex =
797
+ (_b = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
798
+ .exec(uuid)) === null || _b === void 0 ? void 0 : _b.slice(1, 6).join("");
799
+ break;
800
+ case 38:
801
+ hex =
802
+ (_c = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
803
+ .exec(uuid)) === null || _c === void 0 ? void 0 : _c.slice(1, 6).join("");
804
+ break;
805
+ case 45:
806
+ hex =
807
+ (_d = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
808
+ .exec(uuid)) === null || _d === void 0 ? void 0 : _d.slice(1, 6).join("");
809
+ break;
810
+ }
811
+ if (hex) {
812
+ var inner = new Uint8Array(16);
813
+ for (var i = 0; i < 16; i += 4) {
814
+ var n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
815
+ inner[i + 0] = n >>> 24;
816
+ inner[i + 1] = n >>> 16;
817
+ inner[i + 2] = n >>> 8;
818
+ inner[i + 3] = n;
819
+ }
820
+ return new UUID(inner);
821
+ }
822
+ else {
823
+ throw new SyntaxError("could not parse UUID string");
824
+ }
825
+ };
826
+ /**
827
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
828
+ * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
829
+ */
830
+ UUID.prototype.toString = function () {
831
+ var text = "";
832
+ for (var i = 0; i < this.bytes.length; i++) {
833
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
834
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
835
+ if (i === 3 || i === 5 || i === 7 || i === 9) {
836
+ text += "-";
837
+ }
838
+ }
839
+ return text;
840
+ };
841
+ /**
842
+ * @returns The 32-digit hexadecimal representation without hyphens
843
+ * (`0189dcd553117d408db09496a2eef37b`).
844
+ */
845
+ UUID.prototype.toHex = function () {
846
+ var text = "";
847
+ for (var i = 0; i < this.bytes.length; i++) {
848
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
849
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
850
+ }
851
+ return text;
852
+ };
853
+ /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
854
+ UUID.prototype.toJSON = function () {
855
+ return this.toString();
856
+ };
857
+ /**
858
+ * Reports the variant field value of the UUID or, if appropriate, "NIL" or
859
+ * "MAX".
860
+ *
861
+ * For convenience, this method reports "NIL" or "MAX" if `this` represents
862
+ * the Nil or Max UUID, although the Nil and Max UUIDs are technically
863
+ * subsumed under the variants `0b0` and `0b111`, respectively.
864
+ */
865
+ UUID.prototype.getVariant = function () {
866
+ var n = this.bytes[8] >>> 4;
867
+ if (n < 0) {
868
+ throw new Error("unreachable");
869
+ }
870
+ else if (n <= 7) {
871
+ return this.bytes.every(function (e) { return e === 0; }) ? "NIL" : "VAR_0";
872
+ }
873
+ else if (n <= 11) {
874
+ return "VAR_10";
875
+ }
876
+ else if (n <= 13) {
877
+ return "VAR_110";
878
+ }
879
+ else if (n <= 15) {
880
+ return this.bytes.every(function (e) { return e === 0xff; }) ? "MAX" : "VAR_RESERVED";
881
+ }
882
+ else {
883
+ throw new Error("unreachable");
884
+ }
885
+ };
886
+ /**
887
+ * Returns the version field value of the UUID or `undefined` if the UUID does
888
+ * not have the variant field value of `0b10`.
889
+ */
890
+ UUID.prototype.getVersion = function () {
891
+ return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
892
+ };
893
+ /** Creates an object from `this`. */
894
+ UUID.prototype.clone = function () {
895
+ return new UUID(this.bytes.slice(0));
896
+ };
897
+ /** Returns true if `this` is equivalent to `other`. */
898
+ UUID.prototype.equals = function (other) {
899
+ return this.compareTo(other) === 0;
900
+ };
901
+ /**
902
+ * Returns a negative integer, zero, or positive integer if `this` is less
903
+ * than, equal to, or greater than `other`, respectively.
904
+ */
905
+ UUID.prototype.compareTo = function (other) {
906
+ for (var i = 0; i < 16; i++) {
907
+ var diff = this.bytes[i] - other.bytes[i];
908
+ if (diff !== 0) {
909
+ return Math.sign(diff);
910
+ }
911
+ }
912
+ return 0;
913
+ };
914
+ return UUID;
915
+ }());
916
+ /**
917
+ * Encapsulates the monotonic counter state.
918
+ *
919
+ * This class provides APIs to utilize a separate counter state from that of the
920
+ * global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
921
+ * the default {@link generate} method, this class has {@link generateOrAbort}
922
+ * that is useful to absolutely guarantee the monotonically increasing order of
923
+ * generated UUIDs. See their respective documentation for details.
924
+ */
925
+ var V7Generator = /** @class */ (function () {
926
+ /**
927
+ * Creates a generator object with the default random number generator, or
928
+ * with the specified one if passed as an argument. The specified random
929
+ * number generator should be cryptographically strong and securely seeded.
930
+ */
931
+ function V7Generator(randomNumberGenerator) {
932
+ this.timestamp = 0;
933
+ this.counter = 0;
934
+ this.random = randomNumberGenerator !== null && randomNumberGenerator !== void 0 ? randomNumberGenerator : getDefaultRandom();
935
+ }
936
+ /**
937
+ * Generates a new UUIDv7 object from the current timestamp, or resets the
938
+ * generator upon significant timestamp rollback.
939
+ *
940
+ * This method returns a monotonically increasing UUID by reusing the previous
941
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
942
+ * preceding UUID's. However, when such a clock rollback is considered
943
+ * significant (i.e., by more than ten seconds), this method resets the
944
+ * generator and returns a new UUID based on the given timestamp, breaking the
945
+ * increasing order of UUIDs.
946
+ *
947
+ * See {@link generateOrAbort} for the other mode of generation and
948
+ * {@link generateOrResetCore} for the low-level primitive.
949
+ */
950
+ V7Generator.prototype.generate = function () {
951
+ return this.generateOrResetCore(Date.now(), 10000);
952
+ };
953
+ /**
954
+ * Generates a new UUIDv7 object from the current timestamp, or returns
955
+ * `undefined` upon significant timestamp rollback.
956
+ *
957
+ * This method returns a monotonically increasing UUID by reusing the previous
958
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
959
+ * preceding UUID's. However, when such a clock rollback is considered
960
+ * significant (i.e., by more than ten seconds), this method aborts and
961
+ * returns `undefined` immediately.
962
+ *
963
+ * See {@link generate} for the other mode of generation and
964
+ * {@link generateOrAbortCore} for the low-level primitive.
965
+ */
966
+ V7Generator.prototype.generateOrAbort = function () {
967
+ return this.generateOrAbortCore(Date.now(), 10000);
968
+ };
969
+ /**
970
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
971
+ * generator upon significant timestamp rollback.
972
+ *
973
+ * This method is equivalent to {@link generate} except that it takes a custom
974
+ * timestamp and clock rollback allowance.
975
+ *
976
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
977
+ * considered significant. A suggested value is `10_000` (milliseconds).
978
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
979
+ */
980
+ V7Generator.prototype.generateOrResetCore = function (unixTsMs, rollbackAllowance) {
981
+ var value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
982
+ if (value === undefined) {
983
+ // reset state and resume
984
+ this.timestamp = 0;
985
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
986
+ }
987
+ return value;
988
+ };
989
+ /**
990
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
991
+ * `undefined` upon significant timestamp rollback.
992
+ *
993
+ * This method is equivalent to {@link generateOrAbort} except that it takes a
994
+ * custom timestamp and clock rollback allowance.
995
+ *
996
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
997
+ * considered significant. A suggested value is `10_000` (milliseconds).
998
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
999
+ */
1000
+ V7Generator.prototype.generateOrAbortCore = function (unixTsMs, rollbackAllowance) {
1001
+ var MAX_COUNTER = 4398046511103;
1002
+ if (!Number.isInteger(unixTsMs) ||
1003
+ unixTsMs < 1 ||
1004
+ unixTsMs > 281474976710655) {
1005
+ throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
1006
+ }
1007
+ else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
1008
+ throw new RangeError("`rollbackAllowance` out of reasonable range");
1009
+ }
1010
+ if (unixTsMs > this.timestamp) {
1011
+ this.timestamp = unixTsMs;
1012
+ this.resetCounter();
1013
+ }
1014
+ else if (unixTsMs + rollbackAllowance >= this.timestamp) {
1015
+ // go on with previous timestamp if new one is not much smaller
1016
+ this.counter++;
1017
+ if (this.counter > MAX_COUNTER) {
1018
+ // increment timestamp at counter overflow
1019
+ this.timestamp++;
1020
+ this.resetCounter();
1021
+ }
1022
+ }
1023
+ else {
1024
+ // abort if clock went backwards to unbearable extent
1025
+ return undefined;
1026
+ }
1027
+ return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / Math.pow(2, 30)), this.counter & (Math.pow(2, 30) - 1), this.random.nextUint32());
1028
+ };
1029
+ /** Initializes the counter at a 42-bit random integer. */
1030
+ V7Generator.prototype.resetCounter = function () {
1031
+ this.counter =
1032
+ this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
1033
+ };
1034
+ /**
1035
+ * Generates a new UUIDv4 object utilizing the random number generator inside.
1036
+ *
1037
+ * @internal
1038
+ */
1039
+ V7Generator.prototype.generateV4 = function () {
1040
+ var bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
1041
+ bytes[6] = 0x40 | (bytes[6] >>> 4);
1042
+ bytes[8] = 0x80 | (bytes[8] >>> 2);
1043
+ return UUID.ofInner(bytes);
1044
+ };
1045
+ return V7Generator;
1046
+ }());
1047
+ /** Returns the default random number generator available in the environment. */
1048
+ var getDefaultRandom = function () {
1049
+ // detect Web Crypto API
1050
+ if (typeof crypto !== "undefined" &&
1051
+ typeof crypto.getRandomValues !== "undefined") {
1052
+ return new BufferedCryptoRandom();
1053
+ }
1054
+ else {
1055
+ // fall back on Math.random() unless the flag is set to true
1056
+ if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
1057
+ throw new Error("no cryptographically strong RNG available");
1058
+ }
1059
+ return {
1060
+ nextUint32: function () { return Math.trunc(Math.random() * 65536) * 65536 +
1061
+ Math.trunc(Math.random() * 65536); },
1062
+ };
1063
+ }
1064
+ };
1065
+ /**
1066
+ * Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
1067
+ * buffer by default to avoid both unbearable throughput decline in some
1068
+ * environments and the waste of time and space for unused values.
1069
+ */
1070
+ var BufferedCryptoRandom = /** @class */ (function () {
1071
+ function BufferedCryptoRandom() {
1072
+ this.buffer = new Uint32Array(8);
1073
+ this.cursor = 0xffff;
1074
+ }
1075
+ BufferedCryptoRandom.prototype.nextUint32 = function () {
1076
+ if (this.cursor >= this.buffer.length) {
1077
+ crypto.getRandomValues(this.buffer);
1078
+ this.cursor = 0;
1079
+ }
1080
+ return this.buffer[this.cursor++];
1081
+ };
1082
+ return BufferedCryptoRandom;
1083
+ }());
1084
+ var defaultGenerator;
1085
+ /**
1086
+ * Generates a UUIDv7 string.
1087
+ *
1088
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
1089
+ * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
1090
+ */
1091
+ var uuidv7 = function () { return uuidv7obj().toString(); };
1092
+ /** Generates a UUIDv7 object. */
1093
+ var uuidv7obj = function () { return (defaultGenerator || (defaultGenerator = new V7Generator())).generate(); };
1094
+
720
1095
  var PostHogFetchHttpError = /** @class */ (function (_super) {
721
1096
  __extends(PostHogFetchHttpError, _super);
722
1097
  function PostHogFetchHttpError(response) {
@@ -808,7 +1183,7 @@ var PostHogCoreStateless = /** @class */ (function () {
808
1183
  };
809
1184
  PostHogCoreStateless.prototype.addPendingPromise = function (promise) {
810
1185
  var _this = this;
811
- var promiseUUID = generateUUID();
1186
+ var promiseUUID = uuidv7();
812
1187
  this.pendingPromises[promiseUUID] = promise;
813
1188
  promise.finally(function () {
814
1189
  delete _this.pendingPromises[promiseUUID];
@@ -1012,7 +1387,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1012
1387
  this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
1013
1388
  return;
1014
1389
  }
1015
- var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime(), uuid: (options === null || options === void 0 ? void 0 : options.uuid) ? options.uuid : generateUUID(globalThis) });
1390
+ var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime(), uuid: (options === null || options === void 0 ? void 0 : options.uuid) ? options.uuid : uuidv7() });
1016
1391
  var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
1017
1392
  if (addGeoipDisableProperty) {
1018
1393
  if (!message.properties) {
@@ -1277,7 +1652,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1277
1652
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
1278
1653
  var sessionTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp) || 0;
1279
1654
  if (!sessionId || Date.now() - sessionTimestamp > this._sessionExpirationTimeSeconds * 1000) {
1280
- sessionId = generateUUID(globalThis);
1655
+ sessionId = uuidv7();
1281
1656
  this.setPersistedProperty(PostHogPersistedProperty.SessionId, sessionId);
1282
1657
  }
1283
1658
  this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, Date.now());
@@ -1289,7 +1664,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1289
1664
  PostHogCore.prototype.getAnonymousId = function () {
1290
1665
  var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
1291
1666
  if (!anonId) {
1292
- anonId = generateUUID(globalThis);
1667
+ anonId = uuidv7();
1293
1668
  this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
1294
1669
  }
1295
1670
  return anonId;