@posthog/core 1.0.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.
- package/dist/eventemitter.d.ts +9 -0
- package/dist/eventemitter.d.ts.map +1 -0
- package/dist/eventemitter.js +52 -0
- package/dist/eventemitter.mjs +18 -0
- package/dist/featureFlagUtils.d.ts +35 -0
- package/dist/featureFlagUtils.d.ts.map +1 -0
- package/dist/featureFlagUtils.js +193 -0
- package/dist/featureFlagUtils.mjs +138 -0
- package/dist/gzip.d.ts +10 -0
- package/dist/gzip.d.ts.map +1 -0
- package/dist/gzip.js +56 -0
- package/dist/gzip.mjs +19 -0
- package/dist/index.d.ts +261 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1305 -0
- package/dist/index.mjs +1198 -0
- package/dist/testing/PostHogCoreTestClient.d.ts +24 -0
- package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -0
- package/dist/testing/PostHogCoreTestClient.js +95 -0
- package/dist/testing/PostHogCoreTestClient.mjs +58 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +69 -0
- package/dist/testing/index.mjs +2 -0
- package/dist/testing/test-utils.d.ts +6 -0
- package/dist/testing/test-utils.d.ts.map +1 -0
- package/dist/testing/test-utils.js +75 -0
- package/dist/testing/test-utils.mjs +29 -0
- package/dist/types.d.ts +441 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +163 -0
- package/dist/types.mjs +99 -0
- package/dist/utils.d.ts +24 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +115 -0
- package/dist/utils.mjs +51 -0
- package/dist/vendor/uuidv7.d.ts +180 -0
- package/dist/vendor/uuidv7.d.ts.map +1 -0
- package/dist/vendor/uuidv7.js +223 -0
- package/dist/vendor/uuidv7.js.LICENSE.txt +7 -0
- package/dist/vendor/uuidv7.mjs +174 -0
- package/dist/vendor/uuidv7.mjs.LICENSE.txt +7 -0
- package/package.json +52 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/*! For license information please see uuidv7.js.LICENSE.txt */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __webpack_require__ = {};
|
|
4
|
+
(()=>{
|
|
5
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
6
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: definition[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.r = (exports1)=>{
|
|
17
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
18
|
+
value: 'Module'
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
21
|
+
value: true
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
})();
|
|
25
|
+
var __webpack_exports__ = {};
|
|
26
|
+
__webpack_require__.r(__webpack_exports__);
|
|
27
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
28
|
+
UUID: ()=>UUID,
|
|
29
|
+
V7Generator: ()=>V7Generator,
|
|
30
|
+
uuidv4: ()=>uuidv4,
|
|
31
|
+
uuidv4obj: ()=>uuidv4obj,
|
|
32
|
+
uuidv7: ()=>uuidv7,
|
|
33
|
+
uuidv7obj: ()=>uuidv7obj
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* uuidv7: An experimental implementation of the proposed UUID Version 7
|
|
37
|
+
*
|
|
38
|
+
* @license Apache-2.0
|
|
39
|
+
* @copyright 2021-2023 LiosK
|
|
40
|
+
* @packageDocumentation
|
|
41
|
+
*/ const DIGITS = "0123456789abcdef";
|
|
42
|
+
class UUID {
|
|
43
|
+
static ofInner(bytes) {
|
|
44
|
+
if (16 === bytes.length) return new UUID(bytes);
|
|
45
|
+
throw new TypeError("not 128-bit length");
|
|
46
|
+
}
|
|
47
|
+
static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
|
|
48
|
+
if (!Number.isInteger(unixTsMs) || !Number.isInteger(randA) || !Number.isInteger(randBHi) || !Number.isInteger(randBLo) || unixTsMs < 0 || randA < 0 || randBHi < 0 || randBLo < 0 || unixTsMs > 0xffffffffffff || randA > 0xfff || randBHi > 0x3fffffff || randBLo > 0xffffffff) throw new RangeError("invalid field value");
|
|
49
|
+
const bytes = new Uint8Array(16);
|
|
50
|
+
bytes[0] = unixTsMs / 2 ** 40;
|
|
51
|
+
bytes[1] = unixTsMs / 2 ** 32;
|
|
52
|
+
bytes[2] = unixTsMs / 2 ** 24;
|
|
53
|
+
bytes[3] = unixTsMs / 2 ** 16;
|
|
54
|
+
bytes[4] = unixTsMs / 256;
|
|
55
|
+
bytes[5] = unixTsMs;
|
|
56
|
+
bytes[6] = 0x70 | randA >>> 8;
|
|
57
|
+
bytes[7] = randA;
|
|
58
|
+
bytes[8] = 0x80 | randBHi >>> 24;
|
|
59
|
+
bytes[9] = randBHi >>> 16;
|
|
60
|
+
bytes[10] = randBHi >>> 8;
|
|
61
|
+
bytes[11] = randBHi;
|
|
62
|
+
bytes[12] = randBLo >>> 24;
|
|
63
|
+
bytes[13] = randBLo >>> 16;
|
|
64
|
+
bytes[14] = randBLo >>> 8;
|
|
65
|
+
bytes[15] = randBLo;
|
|
66
|
+
return new UUID(bytes);
|
|
67
|
+
}
|
|
68
|
+
static parse(uuid) {
|
|
69
|
+
let hex;
|
|
70
|
+
switch(uuid.length){
|
|
71
|
+
case 32:
|
|
72
|
+
var _exec;
|
|
73
|
+
hex = null == (_exec = /^[0-9a-f]{32}$/i.exec(uuid)) ? void 0 : _exec[0];
|
|
74
|
+
break;
|
|
75
|
+
case 36:
|
|
76
|
+
var _exec1;
|
|
77
|
+
hex = null == (_exec1 = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)) ? void 0 : _exec1.slice(1, 6).join("");
|
|
78
|
+
break;
|
|
79
|
+
case 38:
|
|
80
|
+
var _exec2;
|
|
81
|
+
hex = null == (_exec2 = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i.exec(uuid)) ? void 0 : _exec2.slice(1, 6).join("");
|
|
82
|
+
break;
|
|
83
|
+
case 45:
|
|
84
|
+
var _exec3;
|
|
85
|
+
hex = null == (_exec3 = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)) ? void 0 : _exec3.slice(1, 6).join("");
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
if (hex) {
|
|
91
|
+
const inner = new Uint8Array(16);
|
|
92
|
+
for(let i = 0; i < 16; i += 4){
|
|
93
|
+
const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
|
|
94
|
+
inner[i + 0] = n >>> 24;
|
|
95
|
+
inner[i + 1] = n >>> 16;
|
|
96
|
+
inner[i + 2] = n >>> 8;
|
|
97
|
+
inner[i + 3] = n;
|
|
98
|
+
}
|
|
99
|
+
return new UUID(inner);
|
|
100
|
+
}
|
|
101
|
+
throw new SyntaxError("could not parse UUID string");
|
|
102
|
+
}
|
|
103
|
+
toString() {
|
|
104
|
+
let text = "";
|
|
105
|
+
for(let i = 0; i < this.bytes.length; i++){
|
|
106
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
107
|
+
text += DIGITS.charAt(0xf & this.bytes[i]);
|
|
108
|
+
if (3 === i || 5 === i || 7 === i || 9 === i) text += "-";
|
|
109
|
+
}
|
|
110
|
+
return text;
|
|
111
|
+
}
|
|
112
|
+
toHex() {
|
|
113
|
+
let text = "";
|
|
114
|
+
for(let i = 0; i < this.bytes.length; i++){
|
|
115
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
116
|
+
text += DIGITS.charAt(0xf & this.bytes[i]);
|
|
117
|
+
}
|
|
118
|
+
return text;
|
|
119
|
+
}
|
|
120
|
+
toJSON() {
|
|
121
|
+
return this.toString();
|
|
122
|
+
}
|
|
123
|
+
getVariant() {
|
|
124
|
+
const n = this.bytes[8] >>> 4;
|
|
125
|
+
if (n < 0) throw new Error("unreachable");
|
|
126
|
+
if (n <= 7) return this.bytes.every((e)=>0 === e) ? "NIL" : "VAR_0";
|
|
127
|
+
if (n <= 11) return "VAR_10";
|
|
128
|
+
if (n <= 13) return "VAR_110";
|
|
129
|
+
if (n <= 15) return this.bytes.every((e)=>0xff === e) ? "MAX" : "VAR_RESERVED";
|
|
130
|
+
else throw new Error("unreachable");
|
|
131
|
+
}
|
|
132
|
+
getVersion() {
|
|
133
|
+
return "VAR_10" === this.getVariant() ? this.bytes[6] >>> 4 : void 0;
|
|
134
|
+
}
|
|
135
|
+
clone() {
|
|
136
|
+
return new UUID(this.bytes.slice(0));
|
|
137
|
+
}
|
|
138
|
+
equals(other) {
|
|
139
|
+
return 0 === this.compareTo(other);
|
|
140
|
+
}
|
|
141
|
+
compareTo(other) {
|
|
142
|
+
for(let i = 0; i < 16; i++){
|
|
143
|
+
const diff = this.bytes[i] - other.bytes[i];
|
|
144
|
+
if (0 !== diff) return Math.sign(diff);
|
|
145
|
+
}
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
constructor(bytes){
|
|
149
|
+
this.bytes = bytes;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
class V7Generator {
|
|
153
|
+
generate() {
|
|
154
|
+
return this.generateOrResetCore(Date.now(), 10000);
|
|
155
|
+
}
|
|
156
|
+
generateOrAbort() {
|
|
157
|
+
return this.generateOrAbortCore(Date.now(), 10000);
|
|
158
|
+
}
|
|
159
|
+
generateOrResetCore(unixTsMs, rollbackAllowance) {
|
|
160
|
+
let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
161
|
+
if (void 0 === value) {
|
|
162
|
+
this.timestamp = 0;
|
|
163
|
+
value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
164
|
+
}
|
|
165
|
+
return value;
|
|
166
|
+
}
|
|
167
|
+
generateOrAbortCore(unixTsMs, rollbackAllowance) {
|
|
168
|
+
const MAX_COUNTER = 0x3ffffffffff;
|
|
169
|
+
if (!Number.isInteger(unixTsMs) || unixTsMs < 1 || unixTsMs > 0xffffffffffff) throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
|
|
170
|
+
if (rollbackAllowance < 0 || rollbackAllowance > 0xffffffffffff) throw new RangeError("`rollbackAllowance` out of reasonable range");
|
|
171
|
+
if (unixTsMs > this.timestamp) {
|
|
172
|
+
this.timestamp = unixTsMs;
|
|
173
|
+
this.resetCounter();
|
|
174
|
+
} else {
|
|
175
|
+
if (!(unixTsMs + rollbackAllowance >= this.timestamp)) return;
|
|
176
|
+
this.counter++;
|
|
177
|
+
if (this.counter > MAX_COUNTER) {
|
|
178
|
+
this.timestamp++;
|
|
179
|
+
this.resetCounter();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & 2 ** 30 - 1, this.random.nextUint32());
|
|
183
|
+
}
|
|
184
|
+
resetCounter() {
|
|
185
|
+
this.counter = 0x400 * this.random.nextUint32() + (0x3ff & this.random.nextUint32());
|
|
186
|
+
}
|
|
187
|
+
generateV4() {
|
|
188
|
+
const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
|
|
189
|
+
bytes[6] = 0x40 | bytes[6] >>> 4;
|
|
190
|
+
bytes[8] = 0x80 | bytes[8] >>> 2;
|
|
191
|
+
return UUID.ofInner(bytes);
|
|
192
|
+
}
|
|
193
|
+
constructor(randomNumberGenerator){
|
|
194
|
+
this.timestamp = 0;
|
|
195
|
+
this.counter = 0;
|
|
196
|
+
this.random = null != randomNumberGenerator ? randomNumberGenerator : getDefaultRandom();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const getDefaultRandom = ()=>({
|
|
200
|
+
nextUint32: ()=>0x10000 * Math.trunc(0x10000 * Math.random()) + Math.trunc(0x10000 * Math.random())
|
|
201
|
+
});
|
|
202
|
+
let defaultGenerator;
|
|
203
|
+
const uuidv7 = ()=>uuidv7obj().toString();
|
|
204
|
+
const uuidv7obj = ()=>(defaultGenerator || (defaultGenerator = new V7Generator())).generate();
|
|
205
|
+
const uuidv4 = ()=>uuidv4obj().toString();
|
|
206
|
+
const uuidv4obj = ()=>(defaultGenerator || (defaultGenerator = new V7Generator())).generateV4();
|
|
207
|
+
exports.UUID = __webpack_exports__.UUID;
|
|
208
|
+
exports.V7Generator = __webpack_exports__.V7Generator;
|
|
209
|
+
exports.uuidv4 = __webpack_exports__.uuidv4;
|
|
210
|
+
exports.uuidv4obj = __webpack_exports__.uuidv4obj;
|
|
211
|
+
exports.uuidv7 = __webpack_exports__.uuidv7;
|
|
212
|
+
exports.uuidv7obj = __webpack_exports__.uuidv7obj;
|
|
213
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
214
|
+
"UUID",
|
|
215
|
+
"V7Generator",
|
|
216
|
+
"uuidv4",
|
|
217
|
+
"uuidv4obj",
|
|
218
|
+
"uuidv7",
|
|
219
|
+
"uuidv7obj"
|
|
220
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
221
|
+
Object.defineProperty(exports, '__esModule', {
|
|
222
|
+
value: true
|
|
223
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/*! For license information please see uuidv7.mjs.LICENSE.txt */
|
|
2
|
+
/**
|
|
3
|
+
* uuidv7: An experimental implementation of the proposed UUID Version 7
|
|
4
|
+
*
|
|
5
|
+
* @license Apache-2.0
|
|
6
|
+
* @copyright 2021-2023 LiosK
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/ const DIGITS = "0123456789abcdef";
|
|
9
|
+
class UUID {
|
|
10
|
+
static ofInner(bytes) {
|
|
11
|
+
if (16 === bytes.length) return new UUID(bytes);
|
|
12
|
+
throw new TypeError("not 128-bit length");
|
|
13
|
+
}
|
|
14
|
+
static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
|
|
15
|
+
if (!Number.isInteger(unixTsMs) || !Number.isInteger(randA) || !Number.isInteger(randBHi) || !Number.isInteger(randBLo) || unixTsMs < 0 || randA < 0 || randBHi < 0 || randBLo < 0 || unixTsMs > 0xffffffffffff || randA > 0xfff || randBHi > 0x3fffffff || randBLo > 0xffffffff) throw new RangeError("invalid field value");
|
|
16
|
+
const bytes = new Uint8Array(16);
|
|
17
|
+
bytes[0] = unixTsMs / 2 ** 40;
|
|
18
|
+
bytes[1] = unixTsMs / 2 ** 32;
|
|
19
|
+
bytes[2] = unixTsMs / 2 ** 24;
|
|
20
|
+
bytes[3] = unixTsMs / 2 ** 16;
|
|
21
|
+
bytes[4] = unixTsMs / 256;
|
|
22
|
+
bytes[5] = unixTsMs;
|
|
23
|
+
bytes[6] = 0x70 | randA >>> 8;
|
|
24
|
+
bytes[7] = randA;
|
|
25
|
+
bytes[8] = 0x80 | randBHi >>> 24;
|
|
26
|
+
bytes[9] = randBHi >>> 16;
|
|
27
|
+
bytes[10] = randBHi >>> 8;
|
|
28
|
+
bytes[11] = randBHi;
|
|
29
|
+
bytes[12] = randBLo >>> 24;
|
|
30
|
+
bytes[13] = randBLo >>> 16;
|
|
31
|
+
bytes[14] = randBLo >>> 8;
|
|
32
|
+
bytes[15] = randBLo;
|
|
33
|
+
return new UUID(bytes);
|
|
34
|
+
}
|
|
35
|
+
static parse(uuid) {
|
|
36
|
+
let hex;
|
|
37
|
+
switch(uuid.length){
|
|
38
|
+
case 32:
|
|
39
|
+
var _exec;
|
|
40
|
+
hex = null == (_exec = /^[0-9a-f]{32}$/i.exec(uuid)) ? void 0 : _exec[0];
|
|
41
|
+
break;
|
|
42
|
+
case 36:
|
|
43
|
+
var _exec1;
|
|
44
|
+
hex = null == (_exec1 = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)) ? void 0 : _exec1.slice(1, 6).join("");
|
|
45
|
+
break;
|
|
46
|
+
case 38:
|
|
47
|
+
var _exec2;
|
|
48
|
+
hex = null == (_exec2 = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i.exec(uuid)) ? void 0 : _exec2.slice(1, 6).join("");
|
|
49
|
+
break;
|
|
50
|
+
case 45:
|
|
51
|
+
var _exec3;
|
|
52
|
+
hex = null == (_exec3 = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)) ? void 0 : _exec3.slice(1, 6).join("");
|
|
53
|
+
break;
|
|
54
|
+
default:
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
if (hex) {
|
|
58
|
+
const inner = new Uint8Array(16);
|
|
59
|
+
for(let i = 0; i < 16; i += 4){
|
|
60
|
+
const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
|
|
61
|
+
inner[i + 0] = n >>> 24;
|
|
62
|
+
inner[i + 1] = n >>> 16;
|
|
63
|
+
inner[i + 2] = n >>> 8;
|
|
64
|
+
inner[i + 3] = n;
|
|
65
|
+
}
|
|
66
|
+
return new UUID(inner);
|
|
67
|
+
}
|
|
68
|
+
throw new SyntaxError("could not parse UUID string");
|
|
69
|
+
}
|
|
70
|
+
toString() {
|
|
71
|
+
let text = "";
|
|
72
|
+
for(let i = 0; i < this.bytes.length; i++){
|
|
73
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
74
|
+
text += DIGITS.charAt(0xf & this.bytes[i]);
|
|
75
|
+
if (3 === i || 5 === i || 7 === i || 9 === i) text += "-";
|
|
76
|
+
}
|
|
77
|
+
return text;
|
|
78
|
+
}
|
|
79
|
+
toHex() {
|
|
80
|
+
let text = "";
|
|
81
|
+
for(let i = 0; i < this.bytes.length; i++){
|
|
82
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
83
|
+
text += DIGITS.charAt(0xf & this.bytes[i]);
|
|
84
|
+
}
|
|
85
|
+
return text;
|
|
86
|
+
}
|
|
87
|
+
toJSON() {
|
|
88
|
+
return this.toString();
|
|
89
|
+
}
|
|
90
|
+
getVariant() {
|
|
91
|
+
const n = this.bytes[8] >>> 4;
|
|
92
|
+
if (n < 0) throw new Error("unreachable");
|
|
93
|
+
if (n <= 7) return this.bytes.every((e)=>0 === e) ? "NIL" : "VAR_0";
|
|
94
|
+
if (n <= 11) return "VAR_10";
|
|
95
|
+
if (n <= 13) return "VAR_110";
|
|
96
|
+
if (n <= 15) return this.bytes.every((e)=>0xff === e) ? "MAX" : "VAR_RESERVED";
|
|
97
|
+
else throw new Error("unreachable");
|
|
98
|
+
}
|
|
99
|
+
getVersion() {
|
|
100
|
+
return "VAR_10" === this.getVariant() ? this.bytes[6] >>> 4 : void 0;
|
|
101
|
+
}
|
|
102
|
+
clone() {
|
|
103
|
+
return new UUID(this.bytes.slice(0));
|
|
104
|
+
}
|
|
105
|
+
equals(other) {
|
|
106
|
+
return 0 === this.compareTo(other);
|
|
107
|
+
}
|
|
108
|
+
compareTo(other) {
|
|
109
|
+
for(let i = 0; i < 16; i++){
|
|
110
|
+
const diff = this.bytes[i] - other.bytes[i];
|
|
111
|
+
if (0 !== diff) return Math.sign(diff);
|
|
112
|
+
}
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
constructor(bytes){
|
|
116
|
+
this.bytes = bytes;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
class V7Generator {
|
|
120
|
+
generate() {
|
|
121
|
+
return this.generateOrResetCore(Date.now(), 10000);
|
|
122
|
+
}
|
|
123
|
+
generateOrAbort() {
|
|
124
|
+
return this.generateOrAbortCore(Date.now(), 10000);
|
|
125
|
+
}
|
|
126
|
+
generateOrResetCore(unixTsMs, rollbackAllowance) {
|
|
127
|
+
let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
128
|
+
if (void 0 === value) {
|
|
129
|
+
this.timestamp = 0;
|
|
130
|
+
value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
generateOrAbortCore(unixTsMs, rollbackAllowance) {
|
|
135
|
+
const MAX_COUNTER = 0x3ffffffffff;
|
|
136
|
+
if (!Number.isInteger(unixTsMs) || unixTsMs < 1 || unixTsMs > 0xffffffffffff) throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
|
|
137
|
+
if (rollbackAllowance < 0 || rollbackAllowance > 0xffffffffffff) throw new RangeError("`rollbackAllowance` out of reasonable range");
|
|
138
|
+
if (unixTsMs > this.timestamp) {
|
|
139
|
+
this.timestamp = unixTsMs;
|
|
140
|
+
this.resetCounter();
|
|
141
|
+
} else {
|
|
142
|
+
if (!(unixTsMs + rollbackAllowance >= this.timestamp)) return;
|
|
143
|
+
this.counter++;
|
|
144
|
+
if (this.counter > MAX_COUNTER) {
|
|
145
|
+
this.timestamp++;
|
|
146
|
+
this.resetCounter();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & 2 ** 30 - 1, this.random.nextUint32());
|
|
150
|
+
}
|
|
151
|
+
resetCounter() {
|
|
152
|
+
this.counter = 0x400 * this.random.nextUint32() + (0x3ff & this.random.nextUint32());
|
|
153
|
+
}
|
|
154
|
+
generateV4() {
|
|
155
|
+
const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
|
|
156
|
+
bytes[6] = 0x40 | bytes[6] >>> 4;
|
|
157
|
+
bytes[8] = 0x80 | bytes[8] >>> 2;
|
|
158
|
+
return UUID.ofInner(bytes);
|
|
159
|
+
}
|
|
160
|
+
constructor(randomNumberGenerator){
|
|
161
|
+
this.timestamp = 0;
|
|
162
|
+
this.counter = 0;
|
|
163
|
+
this.random = null != randomNumberGenerator ? randomNumberGenerator : getDefaultRandom();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const getDefaultRandom = ()=>({
|
|
167
|
+
nextUint32: ()=>0x10000 * Math.trunc(0x10000 * Math.random()) + Math.trunc(0x10000 * Math.random())
|
|
168
|
+
});
|
|
169
|
+
let defaultGenerator;
|
|
170
|
+
const uuidv7 = ()=>uuidv7obj().toString();
|
|
171
|
+
const uuidv7obj = ()=>(defaultGenerator || (defaultGenerator = new V7Generator())).generate();
|
|
172
|
+
const uuidv4 = ()=>uuidv4obj().toString();
|
|
173
|
+
const uuidv4obj = ()=>(defaultGenerator || (defaultGenerator = new V7Generator())).generateV4();
|
|
174
|
+
export { UUID, V7Generator, uuidv4, uuidv4obj, uuidv7, uuidv7obj };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@posthog/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/PostHog/posthog-js.git",
|
|
11
|
+
"directory": "packages/core"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"require": "./dist/index.js",
|
|
20
|
+
"import": "./dist/index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./vendor/*": {
|
|
23
|
+
"types": "./dist/vendor/*.d.ts",
|
|
24
|
+
"require": "./dist/vendor/*.js",
|
|
25
|
+
"import": "./dist/vendor/*.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./testing": {
|
|
28
|
+
"types": "./dist/testing/index.d.ts",
|
|
29
|
+
"require": "./dist/testing/index.js",
|
|
30
|
+
"import": "./dist/testing/index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./utils": {
|
|
33
|
+
"types": "./dist/utils.d.ts",
|
|
34
|
+
"require": "./dist/utils.js",
|
|
35
|
+
"import": "./dist/utils.mjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@rslib/core": "^0.10.5",
|
|
40
|
+
"jest": "^29.7.0",
|
|
41
|
+
"@posthog-tooling/tsconfig-base": "1.0.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "rimraf dist",
|
|
45
|
+
"lint": "eslint src test",
|
|
46
|
+
"lint:fix": "eslint src test --fix",
|
|
47
|
+
"build": "rslib build",
|
|
48
|
+
"dev": "rslib build -w",
|
|
49
|
+
"test:unit": "jest",
|
|
50
|
+
"package": "pnpm pack --out $PACKAGE_DEST/%s.tgz"
|
|
51
|
+
}
|
|
52
|
+
}
|