@posthog/browser 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/ARCHITECTURE.md +559 -0
  2. package/LICENSE +417 -0
  3. package/README.md +302 -2
  4. package/dist/analytics-buffer.d.ts +4 -0
  5. package/dist/analytics-buffer.js +200 -0
  6. package/dist/analytics-buffer.mjs +162 -0
  7. package/dist/analytics-delivery.d.ts +3 -0
  8. package/dist/analytics-delivery.js +231 -0
  9. package/dist/analytics-delivery.mjs +193 -0
  10. package/dist/analytics-internal.d.ts +64 -0
  11. package/dist/analytics-internal.js +52 -0
  12. package/dist/analytics-internal.mjs +5 -0
  13. package/dist/analytics-options.d.ts +14 -0
  14. package/dist/analytics-options.js +18 -0
  15. package/dist/analytics-options.mjs +1 -0
  16. package/dist/analytics.d.ts +5 -0
  17. package/dist/analytics.js +42 -0
  18. package/dist/analytics.mjs +4 -0
  19. package/dist/autocapture-options.d.ts +32 -0
  20. package/dist/autocapture-options.js +49 -0
  21. package/dist/autocapture-options.mjs +11 -0
  22. package/dist/autocapture.d.ts +5 -0
  23. package/dist/autocapture.js +108 -0
  24. package/dist/autocapture.mjs +70 -0
  25. package/dist/automatic-analytics.d.ts +3 -0
  26. package/dist/automatic-analytics.js +44 -0
  27. package/dist/automatic-analytics.mjs +6 -0
  28. package/dist/automatic-surveys.d.ts +2 -0
  29. package/dist/automatic-surveys.js +41 -0
  30. package/dist/automatic-surveys.mjs +3 -0
  31. package/dist/bot-filter.d.ts +2 -0
  32. package/dist/bot-filter.js +87 -0
  33. package/dist/bot-filter.mjs +49 -0
  34. package/dist/browser-client.d.ts +30 -0
  35. package/dist/browser-client.js +18 -0
  36. package/dist/browser-client.mjs +1 -0
  37. package/dist/capture-summary.d.ts +3 -0
  38. package/dist/capture-summary.js +63 -0
  39. package/dist/capture-summary.mjs +22 -0
  40. package/dist/capture-v1.d.ts +105 -0
  41. package/dist/capture-v1.js +781 -0
  42. package/dist/capture-v1.mjs +722 -0
  43. package/dist/core.d.ts +7 -0
  44. package/dist/core.js +49 -0
  45. package/dist/core.mjs +5 -0
  46. package/dist/event-buffer.d.ts +41 -0
  47. package/dist/event-buffer.js +158 -0
  48. package/dist/event-buffer.mjs +120 -0
  49. package/dist/extensions/registry.d.ts +16 -0
  50. package/dist/extensions/registry.js +128 -0
  51. package/dist/extensions/registry.mjs +90 -0
  52. package/dist/flags-internal.d.ts +8 -0
  53. package/dist/flags-internal.js +18 -0
  54. package/dist/flags-internal.mjs +1 -0
  55. package/dist/flags-options.d.ts +22 -0
  56. package/dist/flags-options.js +18 -0
  57. package/dist/flags-options.mjs +1 -0
  58. package/dist/flags-token.d.ts +15 -0
  59. package/dist/flags-token.js +40 -0
  60. package/dist/flags-token.mjs +2 -0
  61. package/dist/flags.d.ts +6 -0
  62. package/dist/flags.js +163 -0
  63. package/dist/flags.mjs +122 -0
  64. package/dist/id.d.ts +1 -0
  65. package/dist/id.js +61 -0
  66. package/dist/id.mjs +23 -0
  67. package/dist/index.d.ts +11 -0
  68. package/dist/index.js +119 -0
  69. package/dist/index.mjs +75 -0
  70. package/dist/lane.d.ts +57 -0
  71. package/dist/lane.js +363 -0
  72. package/dist/lane.mjs +325 -0
  73. package/dist/logger.d.ts +4 -0
  74. package/dist/logger.js +57 -0
  75. package/dist/logger.mjs +19 -0
  76. package/dist/logs-internal.d.ts +7 -0
  77. package/dist/logs-internal.js +18 -0
  78. package/dist/logs-internal.mjs +1 -0
  79. package/dist/logs-options.d.ts +11 -0
  80. package/dist/logs-options.js +57 -0
  81. package/dist/logs-options.mjs +19 -0
  82. package/dist/logs.d.ts +6 -0
  83. package/dist/logs.js +140 -0
  84. package/dist/logs.mjs +102 -0
  85. package/dist/named-error.d.ts +7 -0
  86. package/dist/named-error.js +49 -0
  87. package/dist/named-error.mjs +11 -0
  88. package/dist/posthog.d.ts +3 -0
  89. package/dist/posthog.js +823 -0
  90. package/dist/posthog.mjs +785 -0
  91. package/dist/rate-limiter.d.ts +19 -0
  92. package/dist/rate-limiter.js +89 -0
  93. package/dist/rate-limiter.mjs +51 -0
  94. package/dist/remote-config.d.ts +3 -0
  95. package/dist/remote-config.js +47 -0
  96. package/dist/remote-config.mjs +9 -0
  97. package/dist/request.d.ts +14 -0
  98. package/dist/request.js +130 -0
  99. package/dist/request.mjs +89 -0
  100. package/dist/state.d.ts +82 -0
  101. package/dist/state.js +625 -0
  102. package/dist/state.mjs +581 -0
  103. package/dist/surveys-extension.d.ts +9 -0
  104. package/dist/surveys-extension.js +272 -0
  105. package/dist/surveys-extension.mjs +234 -0
  106. package/dist/surveys-internal.d.ts +12 -0
  107. package/dist/surveys-internal.js +18 -0
  108. package/dist/surveys-internal.mjs +1 -0
  109. package/dist/surveys-options.d.ts +18 -0
  110. package/dist/surveys-options.js +42 -0
  111. package/dist/surveys-options.mjs +4 -0
  112. package/dist/surveys.d.ts +7 -0
  113. package/dist/surveys.js +48 -0
  114. package/dist/surveys.mjs +7 -0
  115. package/dist/types.d.ts +114 -0
  116. package/dist/types.js +18 -0
  117. package/dist/types.mjs +1 -0
  118. package/dist/version.d.ts +1 -0
  119. package/dist/version.js +40 -0
  120. package/dist/version.mjs +2 -0
  121. package/package.json +88 -3
package/dist/lane.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { EventBuffer } from './event-buffer';
2
+ export interface LaneDeliveryContext {
3
+ readonly signal: AbortSignal | undefined;
4
+ canContinue(): boolean;
5
+ }
6
+ export interface LaneDeliveryResult<E> {
7
+ readonly retry: readonly E[];
8
+ }
9
+ export interface LaneDelivery<E> {
10
+ readonly batchSize?: number;
11
+ readonly flushAt?: number;
12
+ readonly flushInterval?: number;
13
+ canDeliver?(): boolean;
14
+ deliver(events: readonly E[], context: LaneDeliveryContext): Promise<LaneDeliveryResult<E> | void>;
15
+ teardown?(events: readonly E[], maxBytes: number): void;
16
+ }
17
+ /** Delivery scheduling and finite flush barriers for an existing event buffer. */
18
+ export declare class Lane<E> {
19
+ private readonly _buffer;
20
+ private readonly _onError;
21
+ private readonly _onAvailable;
22
+ private readonly _flushWaiters;
23
+ private _delivery;
24
+ private _drain;
25
+ private _abort;
26
+ private _timer;
27
+ private _retryBlocked;
28
+ private _settledId;
29
+ private _driveEpoch;
30
+ private _activeEpoch;
31
+ private _forceTarget;
32
+ constructor(_buffer: EventBuffer<E>, _onError?: (error: unknown) => void, _onAvailable?: () => void);
33
+ attach(delivery: LaneDelivery<E>): void;
34
+ flush(): Promise<void>;
35
+ /** Pauses scheduled work while preserving active and queued accounting. */
36
+ pause(): void;
37
+ /** Redrives retained work after a connectivity transition. */
38
+ retryNow(): void;
39
+ /** Synchronously initiates best-effort teardown delivery without mutating retained work. */
40
+ teardown(maxBytes: number): void;
41
+ dispose(): Promise<void>;
42
+ private _cancelActive;
43
+ private _clearTimer;
44
+ private _trimQueue;
45
+ private _updateSettled;
46
+ private _reportError;
47
+ private _reportAvailable;
48
+ private _resolveFlushWaiters;
49
+ private _readNumber;
50
+ private _readFlushAt;
51
+ private _readFlushInterval;
52
+ private _readBatchSize;
53
+ private _canDeliver;
54
+ private _armTimer;
55
+ private _schedule;
56
+ private _beginDrive;
57
+ }
package/dist/lane.js ADDED
@@ -0,0 +1,363 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ class Lane {
31
+ constructor(_buffer, _onError = ()=>{}, _onAvailable = ()=>{}){
32
+ this._buffer = _buffer;
33
+ this._onError = _onError;
34
+ this._onAvailable = _onAvailable;
35
+ this._flushWaiters = [];
36
+ this._retryBlocked = false;
37
+ this._settledId = 0;
38
+ this._driveEpoch = 0;
39
+ this._activeEpoch = 0;
40
+ _buffer.onChange = (change)=>{
41
+ if ('enqueue' === change) {
42
+ if (this._retryBlocked && _buffer._queue.length >= this._readFlushAt(this._delivery)) this._retryBlocked = false;
43
+ this._schedule();
44
+ } else if ('purge' === change) {
45
+ this._retryBlocked = false;
46
+ this._forceTarget = void 0;
47
+ this._clearTimer();
48
+ this._cancelActive();
49
+ } else {
50
+ this._updateSettled();
51
+ if ('discard' === change && 0 === _buffer._queue.length) this._clearTimer();
52
+ if ('cleared' === change) this._resolveFlushWaiters(true);
53
+ }
54
+ };
55
+ }
56
+ attach(delivery) {
57
+ if (this._buffer._disposed) throw new Error('The lane is disposed');
58
+ if (this._delivery) throw new Error('A delivery policy is already installed');
59
+ this._delivery = delivery;
60
+ this._retryBlocked = false;
61
+ this._buffer._expireQueue(this._buffer._now());
62
+ this._schedule();
63
+ }
64
+ flush() {
65
+ this._buffer._expireQueue(this._buffer._now());
66
+ const attached = this._delivery;
67
+ if (!attached || !this._buffer._activeEntries && 0 === this._buffer._queue.length || !this._canDeliver(attached)) return Promise.resolve();
68
+ const target = this._buffer._nextId;
69
+ this._retryBlocked = false;
70
+ this._clearTimer();
71
+ this._forceTarget = Math.max(this._forceTarget ?? 0, target);
72
+ const epoch = this._drain ? this._activeEpoch : this._beginDrive(true);
73
+ if (!epoch) return Promise.resolve();
74
+ return new Promise((resolve)=>{
75
+ this._flushWaiters.push([
76
+ target,
77
+ epoch,
78
+ resolve
79
+ ]);
80
+ this._resolveFlushWaiters(false);
81
+ });
82
+ }
83
+ pause() {
84
+ this._clearTimer();
85
+ }
86
+ retryNow() {
87
+ if (this._buffer._disposed || 0 === this._buffer._queue.length) return;
88
+ this._retryBlocked = false;
89
+ this._clearTimer();
90
+ if (!this._drain) this._beginDrive(true);
91
+ }
92
+ teardown(maxBytes) {
93
+ this._buffer._expireQueue(this._buffer._now());
94
+ const attached = this._delivery;
95
+ if (this._buffer._disposed || !attached || !this._canDeliver(attached)) return;
96
+ let teardown;
97
+ try {
98
+ teardown = attached.teardown;
99
+ } catch (error) {
100
+ this._reportError(error);
101
+ return;
102
+ }
103
+ if (!teardown) return;
104
+ const entries = [
105
+ ...this._buffer._activeEntries ?? [],
106
+ ...this._buffer._queue
107
+ ];
108
+ if (0 === entries.length) return;
109
+ try {
110
+ teardown(entries.map((entry)=>entry[1]), Math.max(0, Math.floor(Number.isFinite(maxBytes) ? maxBytes : 0)));
111
+ } catch (error) {
112
+ this._reportError(error);
113
+ }
114
+ }
115
+ async dispose() {
116
+ if (this._buffer._disposed) return;
117
+ this._delivery = void 0;
118
+ this._buffer.dispose();
119
+ await this._drain;
120
+ }
121
+ _cancelActive() {
122
+ try {
123
+ this._abort?.abort();
124
+ } catch {}
125
+ }
126
+ _clearTimer() {
127
+ const timer = this._timer;
128
+ this._timer = void 0;
129
+ if (void 0 !== timer) try {
130
+ globalThis.clearTimeout(timer);
131
+ } catch {}
132
+ }
133
+ _trimQueue() {
134
+ let remove = 0;
135
+ let bytes = this._buffer._queuedBytes;
136
+ while(remove < this._buffer._queue.length && (this._buffer._queue.length - remove > this._buffer._capacity || bytes > this._buffer._maxBytes)){
137
+ bytes -= this._buffer._queue[remove][2];
138
+ remove++;
139
+ }
140
+ if (remove > 0) {
141
+ const entries = this._buffer._queue.splice(0, remove);
142
+ this._buffer._queuedBytes = bytes;
143
+ this._buffer._drop(entries, 'overflow');
144
+ }
145
+ }
146
+ _updateSettled() {
147
+ const activeId = this._buffer._activeEntries?.[0]?.[0];
148
+ const queuedId = this._buffer._queue[0]?.[0];
149
+ const firstPending = void 0 === activeId ? queuedId : void 0 === queuedId ? activeId : Math.min(activeId, queuedId);
150
+ this._settledId = void 0 === firstPending ? this._buffer._nextId : firstPending - 1;
151
+ if (void 0 !== this._forceTarget && this._settledId >= this._forceTarget) this._forceTarget = void 0;
152
+ if (!this._drain) this._resolveFlushWaiters(false);
153
+ }
154
+ _reportError(error) {
155
+ try {
156
+ this._onError(error);
157
+ } catch {}
158
+ }
159
+ _reportAvailable() {
160
+ try {
161
+ this._onAvailable();
162
+ } catch (error) {
163
+ this._reportError(error);
164
+ }
165
+ }
166
+ _resolveFlushWaiters(force) {
167
+ for(let index = this._flushWaiters.length - 1; index >= 0; index--){
168
+ const [target, epoch, resolve] = this._flushWaiters[index];
169
+ let complete = force;
170
+ if (!complete) {
171
+ const active = this._buffer._activeEntries?.[0];
172
+ if (active && active[0] <= target) complete = false;
173
+ else {
174
+ const queued = this._buffer._queue[0];
175
+ complete = !queued || queued[0] > target || queued[4] >= epoch;
176
+ }
177
+ }
178
+ if (complete) {
179
+ this._flushWaiters.splice(index, 1);
180
+ resolve();
181
+ }
182
+ }
183
+ }
184
+ _readNumber(get, fallback, minimum) {
185
+ try {
186
+ const value = get();
187
+ return Math.max(minimum, Math.floor(Number.isFinite(value) ? value : fallback));
188
+ } catch (error) {
189
+ this._reportError(error);
190
+ return fallback;
191
+ }
192
+ }
193
+ _readFlushAt(attached) {
194
+ return attached ? this._readNumber(()=>attached.flushAt, 1, 1) : 1;
195
+ }
196
+ _readFlushInterval(attached) {
197
+ return this._readNumber(()=>attached.flushInterval, 0, 0);
198
+ }
199
+ _readBatchSize(attached) {
200
+ try {
201
+ const value = attached.batchSize;
202
+ return void 0 === value ? this._buffer._queue.length : Math.max(1, Math.floor(Number.isFinite(value) ? value : 1));
203
+ } catch (error) {
204
+ this._reportError(error);
205
+ return 1;
206
+ }
207
+ }
208
+ _canDeliver(attached) {
209
+ try {
210
+ return attached.canDeliver?.() ?? true;
211
+ } catch (error) {
212
+ this._reportError(error);
213
+ return false;
214
+ }
215
+ }
216
+ _armTimer(attached) {
217
+ if (void 0 !== this._timer || this._buffer._disposed || 0 === this._buffer._queue.length || !this._canDeliver(attached)) return;
218
+ const interval = this._readFlushInterval(attached);
219
+ if (interval <= 0) return;
220
+ const head = this._buffer._queue[0];
221
+ const dueAt = head[5] || head[3] + interval;
222
+ const delay = Math.max(0, dueAt - this._buffer._now());
223
+ const generation = this._buffer._generation;
224
+ try {
225
+ this._timer = globalThis.setTimeout(()=>{
226
+ this._timer = void 0;
227
+ if (!this._buffer._disposed && this._delivery === attached && this._buffer._generation === generation && this._canDeliver(attached)) {
228
+ this._retryBlocked = false;
229
+ this._beginDrive(true);
230
+ }
231
+ }, delay);
232
+ } catch (error) {
233
+ this._reportError(error);
234
+ }
235
+ }
236
+ _schedule() {
237
+ this._buffer._expireQueue(this._buffer._now());
238
+ const attached = this._delivery;
239
+ if (this._buffer._disposed || this._drain || !attached || 0 === this._buffer._queue.length) {
240
+ if (0 === this._buffer._queue.length) this._clearTimer();
241
+ return;
242
+ }
243
+ if (!this._canDeliver(attached)) return void this._clearTimer();
244
+ const forced = void 0 !== this._forceTarget && this._buffer._queue[0][0] <= this._forceTarget;
245
+ if (forced || !this._retryBlocked && this._buffer._queue.length >= this._readFlushAt(attached)) {
246
+ this._clearTimer();
247
+ this._beginDrive(false);
248
+ } else this._armTimer(attached);
249
+ }
250
+ _beginDrive(allowOne) {
251
+ const attached = this._delivery;
252
+ if (this._buffer._disposed || this._drain || !attached || 0 === this._buffer._queue.length || !this._canDeliver(attached)) return 0;
253
+ const epoch = ++this._driveEpoch;
254
+ const generation = this._buffer._generation;
255
+ let controller;
256
+ try {
257
+ controller = new AbortController();
258
+ } catch {}
259
+ this._abort = controller;
260
+ this._activeEpoch = epoch;
261
+ const canContinue = ()=>!this._buffer._disposed && this._delivery === attached && this._buffer._generation === generation && !(controller?.signal.aborted ?? false);
262
+ this._drain = Promise.resolve().then(async ()=>{
263
+ let first = true;
264
+ while(canContinue() && this._canDeliver(attached)){
265
+ this._buffer._expireQueue(this._buffer._now());
266
+ const head = this._buffer._queue[0];
267
+ if (!head || head[4] >= epoch) break;
268
+ const forced = void 0 !== this._forceTarget && head[0] <= this._forceTarget;
269
+ const threshold = !this._retryBlocked && this._buffer._queue.length >= this._readFlushAt(attached);
270
+ if (!forced && !threshold && !(first && allowOne)) break;
271
+ first = false;
272
+ let batchSize = this._readBatchSize(attached);
273
+ if (forced && void 0 !== this._forceTarget) {
274
+ let throughTarget = 0;
275
+ while(throughTarget < this._buffer._queue.length && this._buffer._queue[throughTarget][0] <= this._forceTarget)throughTarget++;
276
+ batchSize = Math.min(batchSize, Math.max(throughTarget, 1));
277
+ }
278
+ const entries = this._buffer._takeQueue(batchSize);
279
+ if (0 === entries.length) break;
280
+ this._buffer._activeEntries = entries;
281
+ this._buffer._activeBytes = entries.reduce((total, entry)=>total + entry[2], 0);
282
+ let result;
283
+ let deliveryFailed = false;
284
+ try {
285
+ result = await attached.deliver(entries.map((entry)=>entry[1]), {
286
+ signal: controller?.signal,
287
+ canContinue
288
+ });
289
+ } catch (error) {
290
+ deliveryFailed = true;
291
+ result = {
292
+ retry: entries.map((entry)=>entry[1])
293
+ };
294
+ this._reportError(error);
295
+ }
296
+ let retryEntries = [];
297
+ try {
298
+ const retry = result?.retry;
299
+ if (Array.isArray(retry)) {
300
+ const selected = [];
301
+ const length = Math.min(retry.length, entries.length);
302
+ for(let index = 0; index < length; index++){
303
+ const retryEvent = retry[index];
304
+ const entry = entries.find((candidate)=>candidate[1] === retryEvent && !selected.includes(candidate));
305
+ if (entry) selected.push(entry);
306
+ }
307
+ retryEntries = entries.filter((entry)=>selected.includes(entry));
308
+ } else if (deliveryFailed) retryEntries = entries;
309
+ } catch (error) {
310
+ retryEntries = entries;
311
+ this._reportError(error);
312
+ }
313
+ this._buffer._activeEntries = void 0;
314
+ this._buffer._activeBytes = 0;
315
+ if (retryEntries.length > 0 && !this._buffer._disposed && canContinue()) {
316
+ const now = this._buffer._now();
317
+ const expired = [];
318
+ const retained = [];
319
+ const retryAfter = now + this._readFlushInterval(attached);
320
+ for (const entry of retryEntries){
321
+ entry[4] = epoch;
322
+ entry[5] = retryAfter;
323
+ (this._buffer._isExpired(entry, now) ? expired : retained).push(entry);
324
+ }
325
+ const completionGeneration = this._buffer._generation;
326
+ this._buffer._queue.unshift(...retained);
327
+ this._buffer._queuedBytes += retained.reduce((total, entry)=>total + entry[2], 0);
328
+ if (expired.length > 0) this._buffer._reportDrop(expired.length, 'expired');
329
+ if (completionGeneration === this._buffer._generation) {
330
+ this._trimQueue();
331
+ this._retryBlocked = true;
332
+ this._forceTarget = void 0;
333
+ this._updateSettled();
334
+ }
335
+ } else this._updateSettled();
336
+ this._reportAvailable();
337
+ if (retryEntries.length > 0) break;
338
+ this._resolveFlushWaiters(false);
339
+ }
340
+ }).finally(()=>{
341
+ if (this._abort === controller) this._abort = void 0;
342
+ this._activeEpoch = 0;
343
+ this._drain = void 0;
344
+ this._resolveFlushWaiters(false);
345
+ if (!this._delivery) {
346
+ this._forceTarget = void 0;
347
+ this._resolveFlushWaiters(true);
348
+ }
349
+ this._schedule();
350
+ });
351
+ return epoch;
352
+ }
353
+ }
354
+ __webpack_require__.d(__webpack_exports__, {
355
+ Lane: ()=>Lane
356
+ });
357
+ exports.Lane = __webpack_exports__.Lane;
358
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
359
+ "Lane"
360
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
361
+ Object.defineProperty(exports, '__esModule', {
362
+ value: true
363
+ });