posthog-node 1.2.0 → 2.0.0-alpha2
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/README.md +5 -20
- package/index.ts +4 -0
- package/lib/index.cjs.js +1292 -0
- package/lib/index.cjs.js.map +1 -0
- package/lib/index.d.ts +118 -0
- package/lib/index.esm.js +1283 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/node_modules/tslib/tslib.es6.d.ts +35 -0
- package/lib/posthog-core/src/eventemitter.d.ts +8 -0
- package/lib/posthog-core/src/index.d.ts +84 -0
- package/lib/posthog-core/src/lz-string.d.ts +8 -0
- package/lib/posthog-core/src/types.d.ts +68 -0
- package/lib/posthog-core/src/utils.d.ts +13 -0
- package/lib/posthog-node/index.d.ts +3 -0
- package/lib/posthog-node/src/posthog-node.d.ts +20 -0
- package/lib/posthog-node/src/types.d.ts +83 -0
- package/package.json +35 -67
- package/src/posthog-node.ts +125 -0
- package/src/types.ts +99 -0
- package/test/posthog-node.spec.ts +92 -0
- package/tsconfig.json +6 -0
- package/LICENSE +0 -21
- package/cli.js +0 -72
- package/event-validation.js +0 -105
- package/feature-flags.js +0 -165
- package/index.d.ts +0 -102
- package/index.js +0 -360
package/lib/index.esm.js
ADDED
|
@@ -0,0 +1,1283 @@
|
|
|
1
|
+
import undici from 'undici';
|
|
2
|
+
|
|
3
|
+
/******************************************************************************
|
|
4
|
+
Copyright (c) Microsoft Corporation.
|
|
5
|
+
|
|
6
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
7
|
+
purpose with or without fee is hereby granted.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
***************************************************************************** */
|
|
17
|
+
/* global Reflect, Promise */
|
|
18
|
+
var extendStatics = function (d, b) {
|
|
19
|
+
extendStatics = Object.setPrototypeOf ||
|
|
20
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
21
|
+
function (d, b) { for (var p in b)
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(b, p))
|
|
23
|
+
d[p] = b[p]; };
|
|
24
|
+
return extendStatics(d, b);
|
|
25
|
+
};
|
|
26
|
+
function __extends(d, b) {
|
|
27
|
+
if (typeof b !== "function" && b !== null)
|
|
28
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
29
|
+
extendStatics(d, b);
|
|
30
|
+
function __() { this.constructor = d; }
|
|
31
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
32
|
+
}
|
|
33
|
+
var __assign = function () {
|
|
34
|
+
__assign = Object.assign || function __assign(t) {
|
|
35
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
36
|
+
s = arguments[i];
|
|
37
|
+
for (var p in s)
|
|
38
|
+
if (Object.prototype.hasOwnProperty.call(s, p))
|
|
39
|
+
t[p] = s[p];
|
|
40
|
+
}
|
|
41
|
+
return t;
|
|
42
|
+
};
|
|
43
|
+
return __assign.apply(this, arguments);
|
|
44
|
+
};
|
|
45
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
46
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
47
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
48
|
+
function fulfilled(value) { try {
|
|
49
|
+
step(generator.next(value));
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
} }
|
|
54
|
+
function rejected(value) { try {
|
|
55
|
+
step(generator["throw"](value));
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
reject(e);
|
|
59
|
+
} }
|
|
60
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
61
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function __generator(thisArg, body) {
|
|
65
|
+
var _ = { label: 0, sent: function () { if (t[0] & 1)
|
|
66
|
+
throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
67
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
|
|
68
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
69
|
+
function step(op) {
|
|
70
|
+
if (f)
|
|
71
|
+
throw new TypeError("Generator is already executing.");
|
|
72
|
+
while (_)
|
|
73
|
+
try {
|
|
74
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
|
|
75
|
+
return t;
|
|
76
|
+
if (y = 0, t)
|
|
77
|
+
op = [op[0] & 2, t.value];
|
|
78
|
+
switch (op[0]) {
|
|
79
|
+
case 0:
|
|
80
|
+
case 1:
|
|
81
|
+
t = op;
|
|
82
|
+
break;
|
|
83
|
+
case 4:
|
|
84
|
+
_.label++;
|
|
85
|
+
return { value: op[1], done: false };
|
|
86
|
+
case 5:
|
|
87
|
+
_.label++;
|
|
88
|
+
y = op[1];
|
|
89
|
+
op = [0];
|
|
90
|
+
continue;
|
|
91
|
+
case 7:
|
|
92
|
+
op = _.ops.pop();
|
|
93
|
+
_.trys.pop();
|
|
94
|
+
continue;
|
|
95
|
+
default:
|
|
96
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
97
|
+
_ = 0;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
101
|
+
_.label = op[1];
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
105
|
+
_.label = t[1];
|
|
106
|
+
t = op;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if (t && _.label < t[2]) {
|
|
110
|
+
_.label = t[2];
|
|
111
|
+
_.ops.push(op);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (t[2])
|
|
115
|
+
_.ops.pop();
|
|
116
|
+
_.trys.pop();
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
op = body.call(thisArg, _);
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
op = [6, e];
|
|
123
|
+
y = 0;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
f = t = 0;
|
|
127
|
+
}
|
|
128
|
+
if (op[0] & 5)
|
|
129
|
+
throw op[1];
|
|
130
|
+
return { value: op[0] ? op[1] : void 0, done: true };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
var version = "2.0.0-alpha2";
|
|
135
|
+
|
|
136
|
+
var PostHogPersistedProperty;
|
|
137
|
+
(function (PostHogPersistedProperty) {
|
|
138
|
+
PostHogPersistedProperty["DistinctId"] = "distinct_id";
|
|
139
|
+
PostHogPersistedProperty["Props"] = "props";
|
|
140
|
+
PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
|
|
141
|
+
PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
|
|
142
|
+
PostHogPersistedProperty["Queue"] = "queue";
|
|
143
|
+
PostHogPersistedProperty["OptedOut"] = "opted_out";
|
|
144
|
+
PostHogPersistedProperty["SessionId"] = "session_id";
|
|
145
|
+
PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
|
|
146
|
+
})(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
|
|
147
|
+
|
|
148
|
+
function assert(truthyValue, message) {
|
|
149
|
+
if (!truthyValue) {
|
|
150
|
+
throw new Error(message);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function removeTrailingSlash(url) {
|
|
154
|
+
return url === null || url === void 0 ? void 0 : url.replace(/\/+$/, '');
|
|
155
|
+
}
|
|
156
|
+
function retriable(fn, props) {
|
|
157
|
+
if (props === void 0) { props = {}; }
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
var _a, retryCount, _b, retryDelay, _c, retryCheck, lastError, i, res, e_1;
|
|
160
|
+
return __generator(this, function (_d) {
|
|
161
|
+
switch (_d.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
_a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ? 1000 : _b, _c = props.retryCheck, retryCheck = _c === void 0 ? function () { return true; } : _c;
|
|
164
|
+
lastError = null;
|
|
165
|
+
i = 0;
|
|
166
|
+
_d.label = 1;
|
|
167
|
+
case 1:
|
|
168
|
+
if (!(i < retryCount + 1)) return [3 /*break*/, 8];
|
|
169
|
+
_d.label = 2;
|
|
170
|
+
case 2:
|
|
171
|
+
_d.trys.push([2, 4, , 5]);
|
|
172
|
+
return [4 /*yield*/, fn()];
|
|
173
|
+
case 3:
|
|
174
|
+
res = _d.sent();
|
|
175
|
+
return [2 /*return*/, res];
|
|
176
|
+
case 4:
|
|
177
|
+
e_1 = _d.sent();
|
|
178
|
+
lastError = e_1;
|
|
179
|
+
if (!retryCheck(e_1)) {
|
|
180
|
+
throw e_1;
|
|
181
|
+
}
|
|
182
|
+
return [3 /*break*/, 5];
|
|
183
|
+
case 5: return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
|
|
184
|
+
case 6:
|
|
185
|
+
_d.sent();
|
|
186
|
+
_d.label = 7;
|
|
187
|
+
case 7:
|
|
188
|
+
i++;
|
|
189
|
+
return [3 /*break*/, 1];
|
|
190
|
+
case 8: throw lastError;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// https://stackoverflow.com/a/8809472
|
|
196
|
+
function generateUUID(globalThis) {
|
|
197
|
+
// Public Domain/MIT
|
|
198
|
+
var d = new Date().getTime(); //Timestamp
|
|
199
|
+
var d2 = (globalThis && globalThis.performance && globalThis.performance.now && globalThis.performance.now() * 1000) || 0; //Time in microseconds since page-load or 0 if unsupported
|
|
200
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
201
|
+
var r = Math.random() * 16; //random number between 0 and 16
|
|
202
|
+
if (d > 0) {
|
|
203
|
+
//Use timestamp until depleted
|
|
204
|
+
r = (d + r) % 16 | 0;
|
|
205
|
+
d = Math.floor(d / 16);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
//Use microseconds since page-load if supported
|
|
209
|
+
r = (d2 + r) % 16 | 0;
|
|
210
|
+
d2 = Math.floor(d2 / 16);
|
|
211
|
+
}
|
|
212
|
+
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function currentTimestamp() {
|
|
216
|
+
return new Date().getTime();
|
|
217
|
+
}
|
|
218
|
+
function currentISOTime() {
|
|
219
|
+
return new Date().toISOString();
|
|
220
|
+
}
|
|
221
|
+
function safeSetTimeout(fn, timeout) {
|
|
222
|
+
// NOTE: we use this so rarely that it is totally fine to do `safeSetTimeout(fn, 0)``
|
|
223
|
+
// rather than setImmediate.
|
|
224
|
+
var t = setTimeout(fn, timeout);
|
|
225
|
+
// We unref if available to prevent Node.js hanging on exit
|
|
226
|
+
(t === null || t === void 0 ? void 0 : t.unref) && (t === null || t === void 0 ? void 0 : t.unref());
|
|
227
|
+
return t;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
|
|
231
|
+
// This work is free. You can redistribute it and/or modify it
|
|
232
|
+
// under the terms of the WTFPL, Version 2
|
|
233
|
+
// For more information see LICENSE.txt or http://www.wtfpl.net/
|
|
234
|
+
//
|
|
235
|
+
// For more information, the home page:
|
|
236
|
+
// http://pieroxy.net/blog/pages/lz-string/testing.html
|
|
237
|
+
//
|
|
238
|
+
// LZ-based compression algorithm, version 1.4.4
|
|
239
|
+
// private property
|
|
240
|
+
var f = String.fromCharCode;
|
|
241
|
+
var keyStrBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
242
|
+
var baseReverseDic = {};
|
|
243
|
+
function getBaseValue(alphabet, character) {
|
|
244
|
+
if (!baseReverseDic[alphabet]) {
|
|
245
|
+
baseReverseDic[alphabet] = {};
|
|
246
|
+
for (var i = 0; i < alphabet.length; i++) {
|
|
247
|
+
baseReverseDic[alphabet][alphabet.charAt(i)] = i;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return baseReverseDic[alphabet][character];
|
|
251
|
+
}
|
|
252
|
+
var LZString = {
|
|
253
|
+
compressToBase64: function (input) {
|
|
254
|
+
if (input == null) {
|
|
255
|
+
return '';
|
|
256
|
+
}
|
|
257
|
+
var res = LZString._compress(input, 6, function (a) {
|
|
258
|
+
return keyStrBase64.charAt(a);
|
|
259
|
+
});
|
|
260
|
+
switch (res.length % 4 // To produce valid Base64
|
|
261
|
+
) {
|
|
262
|
+
default: // When could this happen ?
|
|
263
|
+
case 0:
|
|
264
|
+
return res;
|
|
265
|
+
case 1:
|
|
266
|
+
return res + '===';
|
|
267
|
+
case 2:
|
|
268
|
+
return res + '==';
|
|
269
|
+
case 3:
|
|
270
|
+
return res + '=';
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
decompressFromBase64: function (input) {
|
|
274
|
+
if (input == null) {
|
|
275
|
+
return '';
|
|
276
|
+
}
|
|
277
|
+
if (input == '') {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
return LZString._decompress(input.length, 32, function (index) {
|
|
281
|
+
return getBaseValue(keyStrBase64, input.charAt(index));
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
compress: function (uncompressed) {
|
|
285
|
+
return LZString._compress(uncompressed, 16, function (a) {
|
|
286
|
+
return f(a);
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
_compress: function (uncompressed, bitsPerChar, getCharFromInt) {
|
|
290
|
+
if (uncompressed == null) {
|
|
291
|
+
return '';
|
|
292
|
+
}
|
|
293
|
+
var context_dictionary = {}, context_dictionaryToCreate = {}, context_data = [];
|
|
294
|
+
var i, value, context_c = '', context_wc = '', context_w = '', context_enlargeIn = 2, // Compensate for the first entry which should not count
|
|
295
|
+
context_dictSize = 3, context_numBits = 2, context_data_val = 0, context_data_position = 0, ii;
|
|
296
|
+
for (ii = 0; ii < uncompressed.length; ii += 1) {
|
|
297
|
+
context_c = uncompressed.charAt(ii);
|
|
298
|
+
if (!Object.prototype.hasOwnProperty.call(context_dictionary, context_c)) {
|
|
299
|
+
context_dictionary[context_c] = context_dictSize++;
|
|
300
|
+
context_dictionaryToCreate[context_c] = true;
|
|
301
|
+
}
|
|
302
|
+
context_wc = context_w + context_c;
|
|
303
|
+
if (Object.prototype.hasOwnProperty.call(context_dictionary, context_wc)) {
|
|
304
|
+
context_w = context_wc;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
|
|
308
|
+
if (context_w.charCodeAt(0) < 256) {
|
|
309
|
+
for (i = 0; i < context_numBits; i++) {
|
|
310
|
+
context_data_val = context_data_val << 1;
|
|
311
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
312
|
+
context_data_position = 0;
|
|
313
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
314
|
+
context_data_val = 0;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
context_data_position++;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
value = context_w.charCodeAt(0);
|
|
321
|
+
for (i = 0; i < 8; i++) {
|
|
322
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
323
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
324
|
+
context_data_position = 0;
|
|
325
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
326
|
+
context_data_val = 0;
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
context_data_position++;
|
|
330
|
+
}
|
|
331
|
+
value = value >> 1;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
value = 1;
|
|
336
|
+
for (i = 0; i < context_numBits; i++) {
|
|
337
|
+
context_data_val = (context_data_val << 1) | value;
|
|
338
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
339
|
+
context_data_position = 0;
|
|
340
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
341
|
+
context_data_val = 0;
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
context_data_position++;
|
|
345
|
+
}
|
|
346
|
+
value = 0;
|
|
347
|
+
}
|
|
348
|
+
value = context_w.charCodeAt(0);
|
|
349
|
+
for (i = 0; i < 16; i++) {
|
|
350
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
351
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
352
|
+
context_data_position = 0;
|
|
353
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
354
|
+
context_data_val = 0;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
context_data_position++;
|
|
358
|
+
}
|
|
359
|
+
value = value >> 1;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
context_enlargeIn--;
|
|
363
|
+
if (context_enlargeIn == 0) {
|
|
364
|
+
context_enlargeIn = Math.pow(2, context_numBits);
|
|
365
|
+
context_numBits++;
|
|
366
|
+
}
|
|
367
|
+
delete context_dictionaryToCreate[context_w];
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
value = context_dictionary[context_w];
|
|
371
|
+
for (i = 0; i < context_numBits; i++) {
|
|
372
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
373
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
374
|
+
context_data_position = 0;
|
|
375
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
376
|
+
context_data_val = 0;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
context_data_position++;
|
|
380
|
+
}
|
|
381
|
+
value = value >> 1;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
context_enlargeIn--;
|
|
385
|
+
if (context_enlargeIn == 0) {
|
|
386
|
+
context_enlargeIn = Math.pow(2, context_numBits);
|
|
387
|
+
context_numBits++;
|
|
388
|
+
}
|
|
389
|
+
// Add wc to the dictionary.
|
|
390
|
+
context_dictionary[context_wc] = context_dictSize++;
|
|
391
|
+
context_w = String(context_c);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// Output the code for w.
|
|
395
|
+
if (context_w !== '') {
|
|
396
|
+
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
|
|
397
|
+
if (context_w.charCodeAt(0) < 256) {
|
|
398
|
+
for (i = 0; i < context_numBits; i++) {
|
|
399
|
+
context_data_val = context_data_val << 1;
|
|
400
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
401
|
+
context_data_position = 0;
|
|
402
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
403
|
+
context_data_val = 0;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
context_data_position++;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
value = context_w.charCodeAt(0);
|
|
410
|
+
for (i = 0; i < 8; i++) {
|
|
411
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
412
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
413
|
+
context_data_position = 0;
|
|
414
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
415
|
+
context_data_val = 0;
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
context_data_position++;
|
|
419
|
+
}
|
|
420
|
+
value = value >> 1;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
value = 1;
|
|
425
|
+
for (i = 0; i < context_numBits; i++) {
|
|
426
|
+
context_data_val = (context_data_val << 1) | value;
|
|
427
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
428
|
+
context_data_position = 0;
|
|
429
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
430
|
+
context_data_val = 0;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
context_data_position++;
|
|
434
|
+
}
|
|
435
|
+
value = 0;
|
|
436
|
+
}
|
|
437
|
+
value = context_w.charCodeAt(0);
|
|
438
|
+
for (i = 0; i < 16; i++) {
|
|
439
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
440
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
441
|
+
context_data_position = 0;
|
|
442
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
443
|
+
context_data_val = 0;
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
context_data_position++;
|
|
447
|
+
}
|
|
448
|
+
value = value >> 1;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
context_enlargeIn--;
|
|
452
|
+
if (context_enlargeIn == 0) {
|
|
453
|
+
context_enlargeIn = Math.pow(2, context_numBits);
|
|
454
|
+
context_numBits++;
|
|
455
|
+
}
|
|
456
|
+
delete context_dictionaryToCreate[context_w];
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
value = context_dictionary[context_w];
|
|
460
|
+
for (i = 0; i < context_numBits; i++) {
|
|
461
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
462
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
463
|
+
context_data_position = 0;
|
|
464
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
465
|
+
context_data_val = 0;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
context_data_position++;
|
|
469
|
+
}
|
|
470
|
+
value = value >> 1;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
context_enlargeIn--;
|
|
474
|
+
if (context_enlargeIn == 0) {
|
|
475
|
+
context_enlargeIn = Math.pow(2, context_numBits);
|
|
476
|
+
context_numBits++;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// Mark the end of the stream
|
|
480
|
+
value = 2;
|
|
481
|
+
for (i = 0; i < context_numBits; i++) {
|
|
482
|
+
context_data_val = (context_data_val << 1) | (value & 1);
|
|
483
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
484
|
+
context_data_position = 0;
|
|
485
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
486
|
+
context_data_val = 0;
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
context_data_position++;
|
|
490
|
+
}
|
|
491
|
+
value = value >> 1;
|
|
492
|
+
}
|
|
493
|
+
// Flush the last char
|
|
494
|
+
while (true) {
|
|
495
|
+
context_data_val = context_data_val << 1;
|
|
496
|
+
if (context_data_position == bitsPerChar - 1) {
|
|
497
|
+
context_data.push(getCharFromInt(context_data_val));
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
context_data_position++;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return context_data.join('');
|
|
505
|
+
},
|
|
506
|
+
decompress: function (compressed) {
|
|
507
|
+
if (compressed == null) {
|
|
508
|
+
return '';
|
|
509
|
+
}
|
|
510
|
+
if (compressed == '') {
|
|
511
|
+
return null;
|
|
512
|
+
}
|
|
513
|
+
return LZString._decompress(compressed.length, 32768, function (index) {
|
|
514
|
+
return compressed.charCodeAt(index);
|
|
515
|
+
});
|
|
516
|
+
},
|
|
517
|
+
_decompress: function (length, resetValue, getNextValue) {
|
|
518
|
+
var dictionary = [], result = [], data = { val: getNextValue(0), position: resetValue, index: 1 };
|
|
519
|
+
var enlargeIn = 4, dictSize = 4, numBits = 3, entry = '', i, w, bits, resb, maxpower, power, c;
|
|
520
|
+
for (i = 0; i < 3; i += 1) {
|
|
521
|
+
dictionary[i] = i;
|
|
522
|
+
}
|
|
523
|
+
bits = 0;
|
|
524
|
+
maxpower = Math.pow(2, 2);
|
|
525
|
+
power = 1;
|
|
526
|
+
while (power != maxpower) {
|
|
527
|
+
resb = data.val & data.position;
|
|
528
|
+
data.position >>= 1;
|
|
529
|
+
if (data.position == 0) {
|
|
530
|
+
data.position = resetValue;
|
|
531
|
+
data.val = getNextValue(data.index++);
|
|
532
|
+
}
|
|
533
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
534
|
+
power <<= 1;
|
|
535
|
+
}
|
|
536
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
537
|
+
switch ((bits)) {
|
|
538
|
+
case 0:
|
|
539
|
+
bits = 0;
|
|
540
|
+
maxpower = Math.pow(2, 8);
|
|
541
|
+
power = 1;
|
|
542
|
+
while (power != maxpower) {
|
|
543
|
+
resb = data.val & data.position;
|
|
544
|
+
data.position >>= 1;
|
|
545
|
+
if (data.position == 0) {
|
|
546
|
+
data.position = resetValue;
|
|
547
|
+
data.val = getNextValue(data.index++);
|
|
548
|
+
}
|
|
549
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
550
|
+
power <<= 1;
|
|
551
|
+
}
|
|
552
|
+
c = f(bits);
|
|
553
|
+
break;
|
|
554
|
+
case 1:
|
|
555
|
+
bits = 0;
|
|
556
|
+
maxpower = Math.pow(2, 16);
|
|
557
|
+
power = 1;
|
|
558
|
+
while (power != maxpower) {
|
|
559
|
+
resb = data.val & data.position;
|
|
560
|
+
data.position >>= 1;
|
|
561
|
+
if (data.position == 0) {
|
|
562
|
+
data.position = resetValue;
|
|
563
|
+
data.val = getNextValue(data.index++);
|
|
564
|
+
}
|
|
565
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
566
|
+
power <<= 1;
|
|
567
|
+
}
|
|
568
|
+
c = f(bits);
|
|
569
|
+
break;
|
|
570
|
+
case 2:
|
|
571
|
+
return '';
|
|
572
|
+
}
|
|
573
|
+
dictionary[3] = c;
|
|
574
|
+
w = c;
|
|
575
|
+
result.push(c);
|
|
576
|
+
while (true) {
|
|
577
|
+
if (data.index > length) {
|
|
578
|
+
return '';
|
|
579
|
+
}
|
|
580
|
+
bits = 0;
|
|
581
|
+
maxpower = Math.pow(2, numBits);
|
|
582
|
+
power = 1;
|
|
583
|
+
while (power != maxpower) {
|
|
584
|
+
resb = data.val & data.position;
|
|
585
|
+
data.position >>= 1;
|
|
586
|
+
if (data.position == 0) {
|
|
587
|
+
data.position = resetValue;
|
|
588
|
+
data.val = getNextValue(data.index++);
|
|
589
|
+
}
|
|
590
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
591
|
+
power <<= 1;
|
|
592
|
+
}
|
|
593
|
+
switch ((c = bits)) {
|
|
594
|
+
case 0:
|
|
595
|
+
bits = 0;
|
|
596
|
+
maxpower = Math.pow(2, 8);
|
|
597
|
+
power = 1;
|
|
598
|
+
while (power != maxpower) {
|
|
599
|
+
resb = data.val & data.position;
|
|
600
|
+
data.position >>= 1;
|
|
601
|
+
if (data.position == 0) {
|
|
602
|
+
data.position = resetValue;
|
|
603
|
+
data.val = getNextValue(data.index++);
|
|
604
|
+
}
|
|
605
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
606
|
+
power <<= 1;
|
|
607
|
+
}
|
|
608
|
+
dictionary[dictSize++] = f(bits);
|
|
609
|
+
c = dictSize - 1;
|
|
610
|
+
enlargeIn--;
|
|
611
|
+
break;
|
|
612
|
+
case 1:
|
|
613
|
+
bits = 0;
|
|
614
|
+
maxpower = Math.pow(2, 16);
|
|
615
|
+
power = 1;
|
|
616
|
+
while (power != maxpower) {
|
|
617
|
+
resb = data.val & data.position;
|
|
618
|
+
data.position >>= 1;
|
|
619
|
+
if (data.position == 0) {
|
|
620
|
+
data.position = resetValue;
|
|
621
|
+
data.val = getNextValue(data.index++);
|
|
622
|
+
}
|
|
623
|
+
bits |= (resb > 0 ? 1 : 0) * power;
|
|
624
|
+
power <<= 1;
|
|
625
|
+
}
|
|
626
|
+
dictionary[dictSize++] = f(bits);
|
|
627
|
+
c = dictSize - 1;
|
|
628
|
+
enlargeIn--;
|
|
629
|
+
break;
|
|
630
|
+
case 2:
|
|
631
|
+
return result.join('');
|
|
632
|
+
}
|
|
633
|
+
if (enlargeIn == 0) {
|
|
634
|
+
enlargeIn = Math.pow(2, numBits);
|
|
635
|
+
numBits++;
|
|
636
|
+
}
|
|
637
|
+
if (dictionary[c]) {
|
|
638
|
+
entry = dictionary[c];
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
if (c === dictSize) {
|
|
642
|
+
entry = w + w.charAt(0);
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
return null;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
result.push(entry);
|
|
649
|
+
// Add w+entry[0] to the dictionary.
|
|
650
|
+
dictionary[dictSize++] = w + entry.charAt(0);
|
|
651
|
+
enlargeIn--;
|
|
652
|
+
w = entry;
|
|
653
|
+
if (enlargeIn == 0) {
|
|
654
|
+
enlargeIn = Math.pow(2, numBits);
|
|
655
|
+
numBits++;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
var SimpleEventEmitter = /** @class */ (function () {
|
|
662
|
+
function SimpleEventEmitter() {
|
|
663
|
+
this.events = {};
|
|
664
|
+
this.events = {};
|
|
665
|
+
}
|
|
666
|
+
SimpleEventEmitter.prototype.on = function (event, listener) {
|
|
667
|
+
var _this = this;
|
|
668
|
+
if (!this.events[event]) {
|
|
669
|
+
this.events[event] = [];
|
|
670
|
+
}
|
|
671
|
+
this.events[event].push(listener);
|
|
672
|
+
return function () {
|
|
673
|
+
_this.events[event] = _this.events[event].filter(function (x) { return x !== listener; });
|
|
674
|
+
};
|
|
675
|
+
};
|
|
676
|
+
SimpleEventEmitter.prototype.emit = function (event, payload) {
|
|
677
|
+
if (!this.events[event]) {
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
for (var _i = 0, _a = this.events[event]; _i < _a.length; _i++) {
|
|
681
|
+
var listener = _a[_i];
|
|
682
|
+
listener(payload);
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
return SimpleEventEmitter;
|
|
686
|
+
}());
|
|
687
|
+
|
|
688
|
+
var PostHogCore = /** @class */ (function () {
|
|
689
|
+
function PostHogCore(apiKey, options) {
|
|
690
|
+
var _this = this;
|
|
691
|
+
var _a, _b, _c, _d, _e, _f;
|
|
692
|
+
this.flagCallReported = {};
|
|
693
|
+
// internal
|
|
694
|
+
this._events = new SimpleEventEmitter();
|
|
695
|
+
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
696
|
+
this.apiKey = apiKey;
|
|
697
|
+
this.host = removeTrailingSlash((options === null || options === void 0 ? void 0 : options.host) || 'https://app.posthog.com');
|
|
698
|
+
this.flushAt = (options === null || options === void 0 ? void 0 : options.flushAt) ? Math.max(options === null || options === void 0 ? void 0 : options.flushAt, 1) : 20;
|
|
699
|
+
this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
|
|
700
|
+
this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
|
|
701
|
+
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
702
|
+
this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
|
|
703
|
+
// If enable is explicitly set to false we override the optout
|
|
704
|
+
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
|
|
705
|
+
this._retryOptions = {
|
|
706
|
+
retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3,
|
|
707
|
+
retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000,
|
|
708
|
+
};
|
|
709
|
+
this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
|
|
710
|
+
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
711
|
+
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
712
|
+
safeSetTimeout(function () {
|
|
713
|
+
void _this.reloadFeatureFlagsAsync();
|
|
714
|
+
}, 1);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
718
|
+
var featureFlags = this.getFeatureFlags();
|
|
719
|
+
return {
|
|
720
|
+
$lib: this.getLibraryId(),
|
|
721
|
+
$lib_version: this.getLibraryVersion(),
|
|
722
|
+
$active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined,
|
|
723
|
+
$enabled_feature_flags: featureFlags,
|
|
724
|
+
};
|
|
725
|
+
};
|
|
726
|
+
Object.defineProperty(PostHogCore.prototype, "props", {
|
|
727
|
+
// NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
|
|
728
|
+
get: function () {
|
|
729
|
+
if (!this._props) {
|
|
730
|
+
this._props = this.getPersistedProperty(PostHogPersistedProperty.Props);
|
|
731
|
+
}
|
|
732
|
+
return this._props || {};
|
|
733
|
+
},
|
|
734
|
+
set: function (val) {
|
|
735
|
+
this._props = val;
|
|
736
|
+
},
|
|
737
|
+
enumerable: false,
|
|
738
|
+
configurable: true
|
|
739
|
+
});
|
|
740
|
+
Object.defineProperty(PostHogCore.prototype, "optedOut", {
|
|
741
|
+
get: function () {
|
|
742
|
+
var _a, _b;
|
|
743
|
+
return (_b = (_a = this.getPersistedProperty(PostHogPersistedProperty.OptedOut)) !== null && _a !== void 0 ? _a : this._optoutOverride) !== null && _b !== void 0 ? _b : false;
|
|
744
|
+
},
|
|
745
|
+
enumerable: false,
|
|
746
|
+
configurable: true
|
|
747
|
+
});
|
|
748
|
+
PostHogCore.prototype.optIn = function () {
|
|
749
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
750
|
+
};
|
|
751
|
+
PostHogCore.prototype.optOut = function () {
|
|
752
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
753
|
+
};
|
|
754
|
+
PostHogCore.prototype.on = function (event, cb) {
|
|
755
|
+
return this._events.on(event, cb);
|
|
756
|
+
};
|
|
757
|
+
PostHogCore.prototype.reset = function () {
|
|
758
|
+
for (var key in PostHogPersistedProperty) {
|
|
759
|
+
this.setPersistedProperty(PostHogPersistedProperty[key], null);
|
|
760
|
+
}
|
|
761
|
+
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
|
|
762
|
+
};
|
|
763
|
+
PostHogCore.prototype.buildPayload = function (payload) {
|
|
764
|
+
return {
|
|
765
|
+
distinct_id: payload.distinct_id || this.getDistinctId(),
|
|
766
|
+
event: payload.event,
|
|
767
|
+
properties: __assign(__assign(__assign(__assign({}, this.props), (payload.properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() }),
|
|
768
|
+
};
|
|
769
|
+
};
|
|
770
|
+
PostHogCore.prototype.getSessionId = function () {
|
|
771
|
+
var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
|
|
772
|
+
var sessionTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp) || 0;
|
|
773
|
+
if (!sessionId || Date.now() - sessionTimestamp > this._sessionExpirationTimeSeconds * 1000) {
|
|
774
|
+
sessionId = generateUUID(globalThis);
|
|
775
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionId, sessionId);
|
|
776
|
+
}
|
|
777
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, Date.now());
|
|
778
|
+
return sessionId;
|
|
779
|
+
};
|
|
780
|
+
PostHogCore.prototype.getDistinctId = function () {
|
|
781
|
+
var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
|
|
782
|
+
if (!distinctId) {
|
|
783
|
+
distinctId = generateUUID(globalThis);
|
|
784
|
+
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
785
|
+
}
|
|
786
|
+
return distinctId;
|
|
787
|
+
};
|
|
788
|
+
PostHogCore.prototype.register = function (properties) {
|
|
789
|
+
this.props = __assign(__assign({}, this.props), properties);
|
|
790
|
+
this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
|
|
791
|
+
};
|
|
792
|
+
PostHogCore.prototype.unregister = function (property) {
|
|
793
|
+
delete this.props[property];
|
|
794
|
+
this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
|
|
795
|
+
};
|
|
796
|
+
/***
|
|
797
|
+
*** TRACKING
|
|
798
|
+
***/
|
|
799
|
+
PostHogCore.prototype.identify = function (distinctId, properties) {
|
|
800
|
+
distinctId = distinctId || this.getDistinctId();
|
|
801
|
+
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
802
|
+
this.groups(properties.$groups);
|
|
803
|
+
}
|
|
804
|
+
var payload = __assign(__assign({}, this.buildPayload({
|
|
805
|
+
distinct_id: distinctId,
|
|
806
|
+
event: '$identify',
|
|
807
|
+
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }),
|
|
808
|
+
})), { $set: properties });
|
|
809
|
+
if (distinctId !== this.getDistinctId()) {
|
|
810
|
+
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
811
|
+
}
|
|
812
|
+
this.enqueue('identify', payload);
|
|
813
|
+
return this;
|
|
814
|
+
};
|
|
815
|
+
PostHogCore.prototype.capture = function (event, properties) {
|
|
816
|
+
// NOTE: Legacy nodejs implementation uses groups
|
|
817
|
+
if (properties && properties['groups']) {
|
|
818
|
+
properties.$groups = properties.groups;
|
|
819
|
+
delete properties.groups;
|
|
820
|
+
}
|
|
821
|
+
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
822
|
+
this.groups(properties.$groups);
|
|
823
|
+
}
|
|
824
|
+
var payload = this.buildPayload({ event: event, properties: properties });
|
|
825
|
+
this.enqueue('capture', payload);
|
|
826
|
+
return this;
|
|
827
|
+
};
|
|
828
|
+
PostHogCore.prototype.alias = function (alias) {
|
|
829
|
+
var distinctId = this.getDistinctId();
|
|
830
|
+
var payload = this.buildPayload({
|
|
831
|
+
event: '$create_alias',
|
|
832
|
+
properties: {
|
|
833
|
+
distinct_id: distinctId,
|
|
834
|
+
alias: alias,
|
|
835
|
+
},
|
|
836
|
+
});
|
|
837
|
+
this.enqueue('alias', payload);
|
|
838
|
+
return this;
|
|
839
|
+
};
|
|
840
|
+
PostHogCore.prototype.autocapture = function (eventType, elements, properties) {
|
|
841
|
+
if (properties === void 0) { properties = {}; }
|
|
842
|
+
var payload = this.buildPayload({
|
|
843
|
+
event: '$autocapture',
|
|
844
|
+
properties: __assign(__assign({}, properties), { $event_type: eventType, $elements: elements }),
|
|
845
|
+
});
|
|
846
|
+
this.enqueue('autocapture', payload);
|
|
847
|
+
return this;
|
|
848
|
+
};
|
|
849
|
+
/***
|
|
850
|
+
*** GROUPS
|
|
851
|
+
***/
|
|
852
|
+
PostHogCore.prototype.groups = function (groups) {
|
|
853
|
+
// Get persisted groups
|
|
854
|
+
var existingGroups = this.props.$groups || {};
|
|
855
|
+
// NOTE: Should we do the same for groups listed in identify / capture?
|
|
856
|
+
this.register({
|
|
857
|
+
$groups: __assign(__assign({}, existingGroups), groups),
|
|
858
|
+
});
|
|
859
|
+
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) {
|
|
860
|
+
void this.reloadFeatureFlagsAsync();
|
|
861
|
+
}
|
|
862
|
+
return this;
|
|
863
|
+
};
|
|
864
|
+
PostHogCore.prototype.group = function (groupType, groupKey, groupProperties) {
|
|
865
|
+
var _a;
|
|
866
|
+
this.groups((_a = {},
|
|
867
|
+
_a[groupType] = groupKey,
|
|
868
|
+
_a));
|
|
869
|
+
if (groupProperties) {
|
|
870
|
+
this.groupIdentify(groupType, groupKey, groupProperties);
|
|
871
|
+
}
|
|
872
|
+
return this;
|
|
873
|
+
};
|
|
874
|
+
PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties) {
|
|
875
|
+
var payload = {
|
|
876
|
+
event: '$groupidentify',
|
|
877
|
+
distinctId: "$".concat(groupType, "_").concat(groupKey),
|
|
878
|
+
properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, this.getCommonEventProperties()),
|
|
879
|
+
};
|
|
880
|
+
this.enqueue('capture', payload);
|
|
881
|
+
return this;
|
|
882
|
+
};
|
|
883
|
+
/***
|
|
884
|
+
*** FEATURE FLAGS
|
|
885
|
+
***/
|
|
886
|
+
PostHogCore.prototype.decideAsync = function () {
|
|
887
|
+
if (this._decideResponsePromise) {
|
|
888
|
+
return this._decideResponsePromise;
|
|
889
|
+
}
|
|
890
|
+
return this._decideAsync();
|
|
891
|
+
};
|
|
892
|
+
PostHogCore.prototype._decideAsync = function () {
|
|
893
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
894
|
+
var url, distinctId, groups, fetchOptions;
|
|
895
|
+
var _this = this;
|
|
896
|
+
return __generator(this, function (_a) {
|
|
897
|
+
url = "".concat(this.host, "/decide/?v=2");
|
|
898
|
+
distinctId = this.getDistinctId();
|
|
899
|
+
groups = this.props.$groups || {};
|
|
900
|
+
fetchOptions = {
|
|
901
|
+
method: 'POST',
|
|
902
|
+
headers: { 'Content-Type': 'application/json' },
|
|
903
|
+
body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }),
|
|
904
|
+
};
|
|
905
|
+
this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
|
|
906
|
+
.then(function (r) { return r.json(); })
|
|
907
|
+
.then(function (res) {
|
|
908
|
+
if (res.featureFlags) {
|
|
909
|
+
_this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
|
|
910
|
+
}
|
|
911
|
+
_this._events.emit('featureflags', res.featureFlags);
|
|
912
|
+
return res;
|
|
913
|
+
});
|
|
914
|
+
return [2 /*return*/, this._decideResponsePromise];
|
|
915
|
+
});
|
|
916
|
+
});
|
|
917
|
+
};
|
|
918
|
+
PostHogCore.prototype.getFeatureFlag = function (key, defaultResult) {
|
|
919
|
+
var _a;
|
|
920
|
+
if (defaultResult === void 0) { defaultResult = false; }
|
|
921
|
+
var featureFlags = this.getFeatureFlags();
|
|
922
|
+
if (!featureFlags) {
|
|
923
|
+
// If we haven't loaded flags yet we respond undefined to indicate this
|
|
924
|
+
return undefined;
|
|
925
|
+
}
|
|
926
|
+
if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
|
|
927
|
+
this.flagCallReported[key] = true;
|
|
928
|
+
this.capture('$feature_flag_called', {
|
|
929
|
+
$feature_flag: key,
|
|
930
|
+
$feature_flag_response: featureFlags[key],
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
// If we have flags we either return the value (true or string) or the defaultResult
|
|
934
|
+
return (_a = featureFlags[key]) !== null && _a !== void 0 ? _a : defaultResult;
|
|
935
|
+
};
|
|
936
|
+
PostHogCore.prototype.getFeatureFlags = function () {
|
|
937
|
+
var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
938
|
+
var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
|
|
939
|
+
if (!overriddenFlags) {
|
|
940
|
+
return flags;
|
|
941
|
+
}
|
|
942
|
+
flags = flags || {};
|
|
943
|
+
for (var key in overriddenFlags) {
|
|
944
|
+
if (!overriddenFlags[key]) {
|
|
945
|
+
delete flags[key];
|
|
946
|
+
}
|
|
947
|
+
else {
|
|
948
|
+
flags[key] = overriddenFlags[key];
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
return flags;
|
|
952
|
+
};
|
|
953
|
+
PostHogCore.prototype.isFeatureEnabled = function (key, defaultResult) {
|
|
954
|
+
var _a;
|
|
955
|
+
if (defaultResult === void 0) { defaultResult = false; }
|
|
956
|
+
var flag = (_a = this.getFeatureFlag(key, defaultResult)) !== null && _a !== void 0 ? _a : defaultResult;
|
|
957
|
+
return !!flag;
|
|
958
|
+
};
|
|
959
|
+
PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
|
|
960
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
961
|
+
var _this = this;
|
|
962
|
+
return __generator(this, function (_a) {
|
|
963
|
+
switch (_a.label) {
|
|
964
|
+
case 0:
|
|
965
|
+
clearTimeout(this._decideTimer);
|
|
966
|
+
if (this._decidePollInterval) {
|
|
967
|
+
this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
|
|
968
|
+
}
|
|
969
|
+
this._decideResponsePromise = undefined;
|
|
970
|
+
return [4 /*yield*/, this.decideAsync()];
|
|
971
|
+
case 1: return [2 /*return*/, (_a.sent()).featureFlags];
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
});
|
|
975
|
+
};
|
|
976
|
+
// When listening to feature flags polling is active
|
|
977
|
+
PostHogCore.prototype.onFeatureFlags = function (cb) {
|
|
978
|
+
var _this = this;
|
|
979
|
+
if (!this._decideTimer) {
|
|
980
|
+
void this.reloadFeatureFlagsAsync();
|
|
981
|
+
}
|
|
982
|
+
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
983
|
+
var flags;
|
|
984
|
+
return __generator(this, function (_a) {
|
|
985
|
+
flags = this.getFeatureFlags();
|
|
986
|
+
if (flags) {
|
|
987
|
+
cb(flags);
|
|
988
|
+
}
|
|
989
|
+
return [2 /*return*/];
|
|
990
|
+
});
|
|
991
|
+
}); });
|
|
992
|
+
};
|
|
993
|
+
PostHogCore.prototype.overrideFeatureFlag = function (flags) {
|
|
994
|
+
if (flags === null) {
|
|
995
|
+
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
|
|
996
|
+
}
|
|
997
|
+
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
|
|
998
|
+
};
|
|
999
|
+
/***
|
|
1000
|
+
*** QUEUEING AND FLUSHING
|
|
1001
|
+
***/
|
|
1002
|
+
PostHogCore.prototype.enqueue = function (type, _message) {
|
|
1003
|
+
var _this = this;
|
|
1004
|
+
if (this.optedOut) {
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: _message.timestamp ? _message.timestamp : currentISOTime() });
|
|
1008
|
+
if (message.distinctId) {
|
|
1009
|
+
message.distinct_id = message.distinctId;
|
|
1010
|
+
delete message.distinctId;
|
|
1011
|
+
}
|
|
1012
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1013
|
+
queue.push({ message: message });
|
|
1014
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1015
|
+
this._events.emit(type, message);
|
|
1016
|
+
// Flush queued events if we meet the flushAt length
|
|
1017
|
+
if (queue.length >= this.flushAt) {
|
|
1018
|
+
this.flush();
|
|
1019
|
+
}
|
|
1020
|
+
if (this.flushInterval && !this._flushTimer) {
|
|
1021
|
+
this._flushTimer = safeSetTimeout(function () { return _this.flush(); }, this.flushInterval);
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
PostHogCore.prototype.flushAsync = function () {
|
|
1025
|
+
var _this = this;
|
|
1026
|
+
return new Promise(function (resolve, reject) {
|
|
1027
|
+
_this.flush(function (err, data) { return (err ? reject(err) : resolve(data)); });
|
|
1028
|
+
});
|
|
1029
|
+
};
|
|
1030
|
+
PostHogCore.prototype.flush = function (callback) {
|
|
1031
|
+
var _this = this;
|
|
1032
|
+
if (this.optedOut) {
|
|
1033
|
+
return callback && safeSetTimeout(callback, 0);
|
|
1034
|
+
}
|
|
1035
|
+
if (this._flushTimer) {
|
|
1036
|
+
clearTimeout(this._flushTimer);
|
|
1037
|
+
this._flushTimer = null;
|
|
1038
|
+
}
|
|
1039
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1040
|
+
if (!queue.length) {
|
|
1041
|
+
return callback && safeSetTimeout(callback, 0);
|
|
1042
|
+
}
|
|
1043
|
+
var items = queue.splice(0, this.flushAt);
|
|
1044
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1045
|
+
var messages = items.map(function (item) { return item.message; });
|
|
1046
|
+
var data = {
|
|
1047
|
+
api_key: this.apiKey,
|
|
1048
|
+
batch: messages,
|
|
1049
|
+
sent_at: currentISOTime(),
|
|
1050
|
+
};
|
|
1051
|
+
var done = function (err) {
|
|
1052
|
+
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1053
|
+
_this._events.emit('flush', messages);
|
|
1054
|
+
};
|
|
1055
|
+
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1056
|
+
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1057
|
+
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1058
|
+
// but browsers such as Chrome and Safari have not caught up.
|
|
1059
|
+
this.getCustomUserAgent();
|
|
1060
|
+
var payload = JSON.stringify(data);
|
|
1061
|
+
var url = this.captureMode === 'form'
|
|
1062
|
+
? "".concat(this.host, "/e/?ip=1&_=").concat(currentTimestamp(), "&v=").concat(this.getLibraryVersion())
|
|
1063
|
+
: "".concat(this.host, "/batch/");
|
|
1064
|
+
var fetchOptions = this.captureMode === 'form'
|
|
1065
|
+
? {
|
|
1066
|
+
method: 'POST',
|
|
1067
|
+
mode: 'no-cors',
|
|
1068
|
+
credentials: 'omit',
|
|
1069
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1070
|
+
body: "data=".concat(encodeURIComponent(LZString.compressToBase64(payload)), "&compression=lz64"),
|
|
1071
|
+
}
|
|
1072
|
+
: {
|
|
1073
|
+
method: 'POST',
|
|
1074
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1075
|
+
body: payload,
|
|
1076
|
+
};
|
|
1077
|
+
this.fetchWithRetry(url, fetchOptions)
|
|
1078
|
+
.then(function () { return done(); })
|
|
1079
|
+
.catch(function (err) {
|
|
1080
|
+
if (err.response) {
|
|
1081
|
+
var error = new Error(err.response.statusText);
|
|
1082
|
+
return done(error);
|
|
1083
|
+
}
|
|
1084
|
+
done(err);
|
|
1085
|
+
});
|
|
1086
|
+
};
|
|
1087
|
+
PostHogCore.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1088
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1089
|
+
var _this = this;
|
|
1090
|
+
return __generator(this, function (_a) {
|
|
1091
|
+
return [2 /*return*/, retriable(function () { return _this.fetch(url, options); }, retryOptions || this._retryOptions)];
|
|
1092
|
+
});
|
|
1093
|
+
});
|
|
1094
|
+
};
|
|
1095
|
+
PostHogCore.prototype.shutdownAsync = function () {
|
|
1096
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1097
|
+
return __generator(this, function (_a) {
|
|
1098
|
+
switch (_a.label) {
|
|
1099
|
+
case 0:
|
|
1100
|
+
clearTimeout(this._decideTimer);
|
|
1101
|
+
clearTimeout(this._flushTimer);
|
|
1102
|
+
return [4 /*yield*/, this.flushAsync()];
|
|
1103
|
+
case 1:
|
|
1104
|
+
_a.sent();
|
|
1105
|
+
return [2 /*return*/];
|
|
1106
|
+
}
|
|
1107
|
+
});
|
|
1108
|
+
});
|
|
1109
|
+
};
|
|
1110
|
+
PostHogCore.prototype.shutdown = function () {
|
|
1111
|
+
void this.shutdownAsync();
|
|
1112
|
+
};
|
|
1113
|
+
return PostHogCore;
|
|
1114
|
+
}());
|
|
1115
|
+
|
|
1116
|
+
var PostHog =
|
|
1117
|
+
/** @class */
|
|
1118
|
+
function (_super) {
|
|
1119
|
+
__extends(PostHog, _super);
|
|
1120
|
+
|
|
1121
|
+
function PostHog(apiKey, options) {
|
|
1122
|
+
if (options === void 0) {
|
|
1123
|
+
options = {};
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
var _this = this;
|
|
1127
|
+
|
|
1128
|
+
options.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'json';
|
|
1129
|
+
options.preloadFeatureFlags = false; // Don't preload as this makes no sense without a distinctId
|
|
1130
|
+
|
|
1131
|
+
_this = _super.call(this, apiKey, options) || this;
|
|
1132
|
+
_this._memoryStorage = {};
|
|
1133
|
+
return _this;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
PostHog.prototype.getPersistedProperty = function (key) {
|
|
1137
|
+
return this._memoryStorage[key];
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
PostHog.prototype.setPersistedProperty = function (key, value) {
|
|
1141
|
+
this._memoryStorage[key] = value !== null ? value : undefined;
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
PostHog.prototype.getSessionId = function () {
|
|
1145
|
+
// Sessions don't make sense for Node
|
|
1146
|
+
return undefined;
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
PostHog.prototype.fetch = function (url, options) {
|
|
1150
|
+
return undici.fetch(url, options);
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
PostHog.prototype.getLibraryId = function () {
|
|
1154
|
+
return 'posthog-node';
|
|
1155
|
+
};
|
|
1156
|
+
|
|
1157
|
+
PostHog.prototype.getLibraryVersion = function () {
|
|
1158
|
+
return version;
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
PostHog.prototype.getCustomUserAgent = function () {
|
|
1162
|
+
return "posthog-node/".concat(version);
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
return PostHog;
|
|
1166
|
+
}(PostHogCore); // The actual exported Nodejs API.
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
var PostHogGlobal =
|
|
1170
|
+
/** @class */
|
|
1171
|
+
function () {
|
|
1172
|
+
function PostHogGlobal(apiKey, options) {
|
|
1173
|
+
if (options === void 0) {
|
|
1174
|
+
options = {};
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
options.decidePollInterval = 0; // Forcefully set to 0 so we don't auto-reload
|
|
1178
|
+
|
|
1179
|
+
this._sharedClient = new PostHog(apiKey, options);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
PostHogGlobal.prototype.reInit = function (distinctId) {
|
|
1183
|
+
// Remove all state except the queue
|
|
1184
|
+
var queue = this._sharedClient.getPersistedProperty(PostHogPersistedProperty.Queue);
|
|
1185
|
+
|
|
1186
|
+
this._sharedClient.reset();
|
|
1187
|
+
|
|
1188
|
+
this._sharedClient.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1189
|
+
|
|
1190
|
+
this._sharedClient.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
PostHogGlobal.prototype.capture = function (_a) {
|
|
1194
|
+
var distinctId = _a.distinctId,
|
|
1195
|
+
event = _a.event,
|
|
1196
|
+
properties = _a.properties,
|
|
1197
|
+
groups = _a.groups;
|
|
1198
|
+
this.reInit(distinctId);
|
|
1199
|
+
|
|
1200
|
+
if (groups) {
|
|
1201
|
+
this._sharedClient.groups(groups);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
this._sharedClient.capture(event, properties);
|
|
1205
|
+
};
|
|
1206
|
+
|
|
1207
|
+
PostHogGlobal.prototype.identify = function (_a) {
|
|
1208
|
+
var distinctId = _a.distinctId,
|
|
1209
|
+
properties = _a.properties;
|
|
1210
|
+
this.reInit(distinctId);
|
|
1211
|
+
|
|
1212
|
+
this._sharedClient.identify(distinctId, properties);
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
PostHogGlobal.prototype.alias = function (data) {
|
|
1216
|
+
this.reInit(data.distinctId);
|
|
1217
|
+
|
|
1218
|
+
this._sharedClient.alias(data.alias);
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
PostHogGlobal.prototype.getFeatureFlag = function (key, distinctId, groups) {
|
|
1222
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1223
|
+
return __generator(this, function (_a) {
|
|
1224
|
+
switch (_a.label) {
|
|
1225
|
+
case 0:
|
|
1226
|
+
this.reInit(distinctId);
|
|
1227
|
+
|
|
1228
|
+
if (groups) {
|
|
1229
|
+
this._sharedClient.groups(groups);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
return [4
|
|
1233
|
+
/*yield*/
|
|
1234
|
+
, this._sharedClient.reloadFeatureFlagsAsync()];
|
|
1235
|
+
|
|
1236
|
+
case 1:
|
|
1237
|
+
_a.sent();
|
|
1238
|
+
|
|
1239
|
+
return [2
|
|
1240
|
+
/*return*/
|
|
1241
|
+
, this._sharedClient.getFeatureFlag(key)];
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
});
|
|
1245
|
+
};
|
|
1246
|
+
|
|
1247
|
+
PostHogGlobal.prototype.isFeatureEnabled = function (key, distinctId, defaultResult, groups) {
|
|
1248
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1249
|
+
var feat;
|
|
1250
|
+
return __generator(this, function (_a) {
|
|
1251
|
+
feat = this.getFeatureFlag(key, distinctId, groups);
|
|
1252
|
+
return [2
|
|
1253
|
+
/*return*/
|
|
1254
|
+
, !!feat || defaultResult || false];
|
|
1255
|
+
});
|
|
1256
|
+
});
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
PostHogGlobal.prototype.groupIdentify = function (_a) {
|
|
1260
|
+
var groupType = _a.groupType,
|
|
1261
|
+
groupKey = _a.groupKey,
|
|
1262
|
+
properties = _a.properties;
|
|
1263
|
+
|
|
1264
|
+
this._sharedClient.groupIdentify(groupType, groupKey, properties);
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1267
|
+
PostHogGlobal.prototype.reloadFeatureFlags = function () {
|
|
1268
|
+
throw new Error('Method not implemented.');
|
|
1269
|
+
};
|
|
1270
|
+
|
|
1271
|
+
PostHogGlobal.prototype.shutdown = function () {
|
|
1272
|
+
void this._sharedClient.shutdownAsync();
|
|
1273
|
+
};
|
|
1274
|
+
|
|
1275
|
+
PostHogGlobal.prototype.shutdownAsync = function () {
|
|
1276
|
+
return this._sharedClient.shutdownAsync();
|
|
1277
|
+
};
|
|
1278
|
+
|
|
1279
|
+
return PostHogGlobal;
|
|
1280
|
+
}();
|
|
1281
|
+
|
|
1282
|
+
export { PostHogGlobal, PostHogGlobal as default };
|
|
1283
|
+
//# sourceMappingURL=index.esm.js.map
|