posthog-node 5.74.0 → 6.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/MIGRATION.md +556 -0
- package/README.md +3 -1
- package/dist/ai-capture/routing.d.ts +0 -3
- package/dist/ai-capture/routing.d.ts.map +1 -1
- package/dist/ai-capture/routing.js +2 -14
- package/dist/ai-capture/routing.mjs +1 -4
- package/dist/capture-lane/lane.d.ts +109 -0
- package/dist/capture-lane/lane.d.ts.map +1 -0
- package/dist/capture-lane/lane.js +308 -0
- package/dist/capture-lane/lane.mjs +270 -0
- package/dist/capture-lane/limits.d.ts +7 -0
- package/dist/capture-lane/limits.d.ts.map +1 -0
- package/dist/{capture-v1/config.js → capture-lane/limits.js} +13 -11
- package/dist/capture-lane/limits.mjs +4 -0
- package/dist/capture-lane/settings.d.ts +30 -0
- package/dist/capture-lane/settings.d.ts.map +1 -0
- package/dist/capture-lane/settings.js +100 -0
- package/dist/capture-lane/settings.mjs +47 -0
- package/dist/capture-v1/compression.d.ts +18 -0
- package/dist/capture-v1/compression.d.ts.map +1 -0
- package/dist/capture-v1/compression.js +70 -0
- package/dist/capture-v1/compression.mjs +26 -0
- package/dist/capture-v1/errors.d.ts +23 -7
- package/dist/capture-v1/errors.d.ts.map +1 -1
- package/dist/capture-v1/errors.js +37 -8
- package/dist/capture-v1/errors.mjs +29 -6
- package/dist/capture-v1/sender.d.ts +63 -23
- package/dist/capture-v1/sender.d.ts.map +1 -1
- package/dist/capture-v1/sender.js +281 -74
- package/dist/capture-v1/sender.mjs +275 -77
- package/dist/capture-v1/serialize.d.ts +28 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +82 -0
- package/dist/capture-v1/serialize.mjs +38 -0
- package/dist/capture-v1/transform.d.ts +10 -25
- package/dist/capture-v1/transform.d.ts.map +1 -1
- package/dist/capture-v1/transform.js +33 -64
- package/dist/capture-v1/transform.mjs +28 -53
- package/dist/capture-v1/types.d.ts +16 -14
- package/dist/capture-v1/types.d.ts.map +1 -1
- package/dist/capture-v1/types.js +27 -1
- package/dist/capture-v1/types.mjs +3 -1
- package/dist/capture-v1/uuid.d.ts +9 -0
- package/dist/capture-v1/uuid.d.ts.map +1 -0
- package/dist/capture-v1/uuid.js +75 -0
- package/dist/capture-v1/uuid.mjs +34 -0
- package/dist/client.d.ts +201 -94
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +456 -228
- package/dist/client.mjs +455 -227
- package/dist/compression.node.d.ts +13 -0
- package/dist/compression.node.d.ts.map +1 -0
- package/dist/{capture-v1/routing.js → compression.node.js} +23 -16
- package/dist/compression.node.mjs +18 -0
- package/dist/entrypoints/index.node.d.ts +2 -0
- package/dist/entrypoints/index.node.d.ts.map +1 -1
- package/dist/entrypoints/index.node.js +11 -4
- package/dist/entrypoints/index.node.mjs +4 -0
- package/dist/event-defaults.d.ts +46 -0
- package/dist/event-defaults.d.ts.map +1 -0
- package/dist/event-defaults.js +141 -0
- package/dist/event-defaults.mjs +79 -0
- package/dist/exports.d.ts +3 -0
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +22 -11
- package/dist/exports.mjs +1 -0
- package/dist/extensions/context/context.d.ts +0 -1
- package/dist/extensions/context/context.d.ts.map +1 -1
- package/dist/extensions/context/context.js +4 -3
- package/dist/extensions/context/context.mjs +4 -3
- package/dist/extensions/context/event-context.d.ts.map +1 -1
- package/dist/extensions/context/event-context.js +3 -0
- package/dist/extensions/context/event-context.mjs +3 -0
- package/dist/extensions/context/types.d.ts +9 -1
- package/dist/extensions/context/types.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.d.ts +1 -1
- package/dist/extensions/error-tracking/index.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.js +71 -7
- package/dist/extensions/error-tracking/index.mjs +75 -11
- package/dist/extensions/express.d.ts +8 -0
- package/dist/extensions/express.d.ts.map +1 -1
- package/dist/extensions/express.js +3 -3
- package/dist/extensions/express.mjs +4 -4
- package/dist/extensions/fetch-request.d.ts.map +1 -1
- package/dist/extensions/fetch-request.js +2 -2
- package/dist/extensions/fetch-request.mjs +3 -3
- package/dist/extensions/nestjs.d.ts.map +1 -1
- package/dist/extensions/nestjs.js +7 -4
- package/dist/extensions/nestjs.mjs +8 -5
- package/dist/extensions/tracing-headers.d.ts +7 -0
- package/dist/extensions/tracing-headers.d.ts.map +1 -1
- package/dist/extensions/tracing-headers.js +11 -1
- package/dist/extensions/tracing-headers.mjs +9 -2
- package/dist/flag-events.d.ts +1 -1
- package/dist/flag-events.d.ts.map +1 -1
- package/dist/flag-events.js +2 -2
- package/dist/flag-events.mjs +2 -2
- package/dist/types.d.ts +191 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +5 -4
- package/src/ai-capture/routing.ts +0 -6
- package/src/capture-lane/lane.ts +414 -0
- package/src/capture-lane/limits.ts +8 -0
- package/src/capture-lane/settings.ts +119 -0
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +50 -11
- package/src/capture-v1/sender.ts +405 -111
- package/src/capture-v1/serialize.ts +77 -0
- package/src/capture-v1/transform.ts +59 -92
- package/src/capture-v1/types.ts +19 -15
- package/src/capture-v1/uuid.ts +53 -0
- package/src/client.ts +845 -422
- package/src/compression.node.ts +30 -0
- package/src/entrypoints/index.node.ts +6 -0
- package/src/event-defaults.ts +177 -0
- package/src/exports.ts +3 -0
- package/src/extensions/context/context.ts +4 -4
- package/src/extensions/context/event-context.ts +3 -1
- package/src/extensions/context/types.ts +10 -1
- package/src/extensions/error-tracking/index.ts +51 -7
- package/src/extensions/express.ts +12 -3
- package/src/extensions/fetch-request.ts +7 -3
- package/src/extensions/nestjs.ts +9 -5
- package/src/extensions/tracing-headers.ts +26 -7
- package/src/flag-events.ts +5 -2
- package/src/types.ts +211 -44
- package/src/version.ts +1 -1
- package/dist/ai-capture/batching.d.ts +0 -11
- package/dist/ai-capture/batching.d.ts.map +0 -1
- package/dist/ai-capture/batching.js +0 -77
- package/dist/ai-capture/batching.mjs +0 -36
- package/dist/capture-v1/config.d.ts +0 -11
- package/dist/capture-v1/config.d.ts.map +0 -1
- package/dist/capture-v1/config.mjs +0 -8
- package/dist/capture-v1/routing.d.ts +0 -21
- package/dist/capture-v1/routing.d.ts.map +0 -1
- package/dist/capture-v1/routing.mjs +0 -7
- package/src/ai-capture/batching.ts +0 -47
- package/src/capture-v1/config.ts +0 -18
- package/src/capture-v1/routing.ts +0 -28
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { type Logger, type PostHogEventProperties } from '@posthog/core';
|
|
2
|
+
import { CaptureError } from '../capture-v1/errors';
|
|
3
|
+
import type { V1SendOptions } from '../capture-v1/sender';
|
|
4
|
+
import { type SerializedV1Event } from '../capture-v1/serialize';
|
|
5
|
+
import type { CaptureEndpoint } from '../capture-v1/types';
|
|
6
|
+
/** What a lane needs from its sender. `V1CaptureSender` implements it. */
|
|
7
|
+
export interface CaptureLaneSender {
|
|
8
|
+
send<T extends SerializedV1Event>(events: T[], options?: V1SendOptions): Promise<T[]>;
|
|
9
|
+
}
|
|
10
|
+
export interface CaptureLaneConfig {
|
|
11
|
+
endpoint: CaptureEndpoint;
|
|
12
|
+
/** Queue length that starts a flush. */
|
|
13
|
+
flushAt: number;
|
|
14
|
+
/** Delay before a flush of queued events; 0 turns the timer off. */
|
|
15
|
+
flushIntervalMs: number;
|
|
16
|
+
maxBatchSize: number;
|
|
17
|
+
/** When the queue holds this many events, a new event drops the oldest. */
|
|
18
|
+
maxQueueSize: number;
|
|
19
|
+
/** A batch stops before an event that would take its serialized bytes past this. */
|
|
20
|
+
targetBatchBytes: number;
|
|
21
|
+
/** Events larger than this are dropped before sending. */
|
|
22
|
+
maxEventBytes: number;
|
|
23
|
+
/** Whether `maxEventBytes` applies to the whole serialized event or to its `properties` only. */
|
|
24
|
+
maxEventBytesOf: 'event' | 'properties';
|
|
25
|
+
}
|
|
26
|
+
export interface CaptureLaneHooks {
|
|
27
|
+
sender: CaptureLaneSender;
|
|
28
|
+
/**
|
|
29
|
+
* Events this lane dropped without sending, at most one report per flush cycle. `summary` is
|
|
30
|
+
* one line with counts and causes only, for when nothing listens.
|
|
31
|
+
*/
|
|
32
|
+
onLocalDrops: (errors: CaptureError[], summary: string) => void;
|
|
33
|
+
/** Failures the sender did not handle itself. */
|
|
34
|
+
onError: (error: unknown) => void;
|
|
35
|
+
/** The messages one flush cycle sent. */
|
|
36
|
+
onFlush: (messages: PostHogEventProperties[]) => void;
|
|
37
|
+
/** Each flush the lane starts on its own, from its timer or `flushAt`. */
|
|
38
|
+
onBackgroundFlush?: (flush: Promise<void>) => void;
|
|
39
|
+
logger: Logger;
|
|
40
|
+
now?: () => number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* One capture endpoint's in-memory queue, flush timer, `flushAt` trigger, batching and
|
|
44
|
+
* in-flight send. Lanes share nothing, so a slow or failing lane never delays, drops
|
|
45
|
+
* or resends another lane's events.
|
|
46
|
+
*
|
|
47
|
+
* Every event is serialized once, on enqueue. Batches are cut by count and by
|
|
48
|
+
* serialized bytes, keeping queue order. An event whose last attempt got no HTTP
|
|
49
|
+
* response goes back to the head of the queue for the next flush; every other
|
|
50
|
+
* failure is final and reported by the sender.
|
|
51
|
+
*/
|
|
52
|
+
export declare class CaptureLane {
|
|
53
|
+
private readonly config;
|
|
54
|
+
private readonly hooks;
|
|
55
|
+
private readonly now;
|
|
56
|
+
private queue;
|
|
57
|
+
private flushTimer?;
|
|
58
|
+
private activeFlush;
|
|
59
|
+
private queuedFlush;
|
|
60
|
+
private abortController;
|
|
61
|
+
private abortTimer?;
|
|
62
|
+
private localDrops;
|
|
63
|
+
private settledCount;
|
|
64
|
+
constructor(config: CaptureLaneConfig, hooks: CaptureLaneHooks);
|
|
65
|
+
get length(): number;
|
|
66
|
+
enqueue(message: PostHogEventProperties): void;
|
|
67
|
+
/** Sends one event now, without queueing. Delivery failures are reported, not thrown. */
|
|
68
|
+
sendImmediate(message: PostHogEventProperties): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Drops an event the client could not read before it reached the lane, as `unserializable`. A
|
|
71
|
+
* queued event's drop is reported with the next flush cycle and an immediate one at once. An
|
|
72
|
+
* event with no uuid yet is counted but not listed.
|
|
73
|
+
*/
|
|
74
|
+
dropUnserializable(uuid: string | undefined, immediate: boolean): void;
|
|
75
|
+
/**
|
|
76
|
+
* Serializes the event and checks its name, ids and size. A value that throws while it is read
|
|
77
|
+
* or written (a getter or `toJSON`) drops only this event. The error is not kept, because its
|
|
78
|
+
* message can carry event data.
|
|
79
|
+
*/
|
|
80
|
+
private check;
|
|
81
|
+
/** Sends everything queued when the returned flush starts. Never rejects. */
|
|
82
|
+
flush(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Flushes until the queue is empty, a cycle settles nothing (every send was requeued),
|
|
85
|
+
* or `deadlineMs` passes. At the deadline, in-flight and later sends are aborted.
|
|
86
|
+
*/
|
|
87
|
+
drain(deadlineMs: number): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Ends a shutdown: reports and discards what is still queued, and re-arms the lane so
|
|
90
|
+
* calls made after shutdown still send.
|
|
91
|
+
*/
|
|
92
|
+
finishShutdown(): void;
|
|
93
|
+
private startFlush;
|
|
94
|
+
private flushInBackground;
|
|
95
|
+
private runFlushCycle;
|
|
96
|
+
private takeBatch;
|
|
97
|
+
private requeue;
|
|
98
|
+
private isTooLarge;
|
|
99
|
+
private recordLocalDrop;
|
|
100
|
+
private addToTally;
|
|
101
|
+
private reportLocalDrops;
|
|
102
|
+
/** Reports `tally` and the events still queued at the end of a shutdown together, in one report. */
|
|
103
|
+
private reportTally;
|
|
104
|
+
private armFlushTimer;
|
|
105
|
+
private clearFlushTimer;
|
|
106
|
+
private abortAt;
|
|
107
|
+
private safely;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=lane.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lane.d.ts","sourceRoot":"","sources":["../../src/capture-lane/lane.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,308 @@
|
|
|
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
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
CaptureLane: ()=>CaptureLane
|
|
32
|
+
});
|
|
33
|
+
const core_namespaceObject = require("@posthog/core");
|
|
34
|
+
const errors_js_namespaceObject = require("../capture-v1/errors.js");
|
|
35
|
+
const serialize_js_namespaceObject = require("../capture-v1/serialize.js");
|
|
36
|
+
const MAX_REPORTED_LOCAL_DROPS = 100;
|
|
37
|
+
const emptyTally = ()=>({
|
|
38
|
+
queueFull: 0,
|
|
39
|
+
tooLarge: 0,
|
|
40
|
+
invalid: 0,
|
|
41
|
+
unserializable: 0,
|
|
42
|
+
sample: []
|
|
43
|
+
});
|
|
44
|
+
const LOCAL_DROP_DETAILS = {
|
|
45
|
+
queueFull: 'queue full',
|
|
46
|
+
tooLarge: 'event too large',
|
|
47
|
+
invalid: 'invalid event',
|
|
48
|
+
unserializable: 'unserializable event'
|
|
49
|
+
};
|
|
50
|
+
class CaptureLane {
|
|
51
|
+
constructor(config, hooks){
|
|
52
|
+
this.queue = [];
|
|
53
|
+
this.activeFlush = null;
|
|
54
|
+
this.queuedFlush = null;
|
|
55
|
+
this.abortController = new AbortController();
|
|
56
|
+
this.localDrops = emptyTally();
|
|
57
|
+
this.settledCount = 0;
|
|
58
|
+
this.config = config;
|
|
59
|
+
this.hooks = hooks;
|
|
60
|
+
this.now = hooks.now ?? Date.now;
|
|
61
|
+
}
|
|
62
|
+
get length() {
|
|
63
|
+
return this.queue.length;
|
|
64
|
+
}
|
|
65
|
+
enqueue(message) {
|
|
66
|
+
const checked = this.check(message);
|
|
67
|
+
if ('drop' in checked) {
|
|
68
|
+
this.recordLocalDrop(checked.uuid, checked.drop);
|
|
69
|
+
this.armFlushTimer();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (this.queue.length >= this.config.maxQueueSize) this.recordLocalDrop(this.queue.shift().uuid, 'queueFull');
|
|
73
|
+
this.queue.push({
|
|
74
|
+
...checked.event,
|
|
75
|
+
message
|
|
76
|
+
});
|
|
77
|
+
if (this.queue.length >= this.config.flushAt) this.flushInBackground();
|
|
78
|
+
else this.armFlushTimer();
|
|
79
|
+
}
|
|
80
|
+
async sendImmediate(message) {
|
|
81
|
+
const checked = this.check(message);
|
|
82
|
+
if ('drop' in checked) {
|
|
83
|
+
const tally = emptyTally();
|
|
84
|
+
this.addToTally(tally, checked.uuid, checked.drop);
|
|
85
|
+
this.reportTally(tally);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
await this.hooks.sender.send([
|
|
89
|
+
checked.event
|
|
90
|
+
], {
|
|
91
|
+
signal: this.abortController.signal
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
dropUnserializable(uuid, immediate) {
|
|
95
|
+
if (immediate) {
|
|
96
|
+
const tally = emptyTally();
|
|
97
|
+
this.addToTally(tally, uuid, 'unserializable');
|
|
98
|
+
this.reportTally(tally);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.recordLocalDrop(uuid, 'unserializable');
|
|
102
|
+
this.armFlushTimer();
|
|
103
|
+
}
|
|
104
|
+
check(message) {
|
|
105
|
+
let event;
|
|
106
|
+
try {
|
|
107
|
+
event = (0, serialize_js_namespaceObject.serializeV1Event)(message);
|
|
108
|
+
if ((0, serialize_js_namespaceObject.wireProblem)(message)) return {
|
|
109
|
+
uuid: event.uuid,
|
|
110
|
+
drop: 'invalid'
|
|
111
|
+
};
|
|
112
|
+
} catch {
|
|
113
|
+
return {
|
|
114
|
+
uuid: String(message.uuid),
|
|
115
|
+
drop: 'unserializable'
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return this.isTooLarge(event) ? {
|
|
119
|
+
uuid: event.uuid,
|
|
120
|
+
drop: 'tooLarge'
|
|
121
|
+
} : {
|
|
122
|
+
event
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
flush() {
|
|
126
|
+
if (this.queuedFlush) return this.queuedFlush;
|
|
127
|
+
if (!this.activeFlush) return this.startFlush();
|
|
128
|
+
const queued = this.activeFlush.then(()=>{
|
|
129
|
+
this.queuedFlush = null;
|
|
130
|
+
return this.startFlush();
|
|
131
|
+
});
|
|
132
|
+
this.queuedFlush = queued;
|
|
133
|
+
return queued;
|
|
134
|
+
}
|
|
135
|
+
async drain(deadlineMs) {
|
|
136
|
+
this.abortAt(deadlineMs);
|
|
137
|
+
let settledBefore;
|
|
138
|
+
do {
|
|
139
|
+
settledBefore = this.settledCount;
|
|
140
|
+
await this.flush();
|
|
141
|
+
}while (this.queue.length > 0 && this.settledCount !== settledBefore && !this.abortController.signal.aborted);
|
|
142
|
+
}
|
|
143
|
+
finishShutdown() {
|
|
144
|
+
clearTimeout(this.abortTimer);
|
|
145
|
+
this.abortTimer = void 0;
|
|
146
|
+
this.clearFlushTimer();
|
|
147
|
+
this.reportLocalDrops(this.queue.splice(0));
|
|
148
|
+
if (this.abortController.signal.aborted) this.abortController = new AbortController();
|
|
149
|
+
}
|
|
150
|
+
startFlush() {
|
|
151
|
+
const flush = this.runFlushCycle().finally(()=>{
|
|
152
|
+
if (this.activeFlush === flush) this.activeFlush = null;
|
|
153
|
+
});
|
|
154
|
+
this.activeFlush = flush;
|
|
155
|
+
return flush;
|
|
156
|
+
}
|
|
157
|
+
flushInBackground() {
|
|
158
|
+
if (this.queuedFlush) return;
|
|
159
|
+
const flush = this.flush();
|
|
160
|
+
this.safely(()=>this.hooks.onBackgroundFlush?.(flush));
|
|
161
|
+
}
|
|
162
|
+
async runFlushCycle() {
|
|
163
|
+
this.clearFlushTimer();
|
|
164
|
+
await Promise.resolve();
|
|
165
|
+
const budget = this.queue.length;
|
|
166
|
+
const sent = [];
|
|
167
|
+
let taken = 0;
|
|
168
|
+
try {
|
|
169
|
+
while(taken < budget && this.queue.length > 0){
|
|
170
|
+
const batch = this.takeBatch();
|
|
171
|
+
taken += batch.length;
|
|
172
|
+
let unsent;
|
|
173
|
+
try {
|
|
174
|
+
unsent = await this.hooks.sender.send(batch, {
|
|
175
|
+
signal: this.abortController.signal,
|
|
176
|
+
keepUnsent: true
|
|
177
|
+
});
|
|
178
|
+
} catch (error) {
|
|
179
|
+
this.settledCount += batch.length;
|
|
180
|
+
this.safely(()=>this.hooks.onError(error));
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
this.settledCount += batch.length - unsent.length;
|
|
184
|
+
const requeued = new Set(unsent);
|
|
185
|
+
for (const event of batch)if (!requeued.has(event)) sent.push(event.message);
|
|
186
|
+
if (unsent.length > 0) {
|
|
187
|
+
this.requeue(unsent);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
this.safely(()=>this.hooks.onError(error));
|
|
193
|
+
} finally{
|
|
194
|
+
if (sent.length > 0) this.safely(()=>this.hooks.onFlush(sent));
|
|
195
|
+
this.reportLocalDrops();
|
|
196
|
+
if (this.queue.length > 0) this.armFlushTimer();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
takeBatch() {
|
|
200
|
+
let count = 0;
|
|
201
|
+
let bytes = 0;
|
|
202
|
+
while(count < this.queue.length && count < this.config.maxBatchSize){
|
|
203
|
+
const next = this.queue[count];
|
|
204
|
+
if (count > 0 && bytes + next.bytes > this.config.targetBatchBytes) break;
|
|
205
|
+
bytes += next.bytes;
|
|
206
|
+
count++;
|
|
207
|
+
}
|
|
208
|
+
return this.queue.splice(0, count);
|
|
209
|
+
}
|
|
210
|
+
requeue(events) {
|
|
211
|
+
this.queue.unshift(...events);
|
|
212
|
+
while(this.queue.length > this.config.maxQueueSize)this.recordLocalDrop(this.queue.shift().uuid, 'queueFull');
|
|
213
|
+
this.hooks.logger.debug(`Kept ${events.length} event(s) for ${this.config.endpoint} after a network failure; the next flush sends them`);
|
|
214
|
+
}
|
|
215
|
+
isTooLarge(event) {
|
|
216
|
+
const bytes = 'properties' === this.config.maxEventBytesOf ? event.propertiesBytes : event.bytes;
|
|
217
|
+
return bytes > this.config.maxEventBytes;
|
|
218
|
+
}
|
|
219
|
+
recordLocalDrop(uuid, reason) {
|
|
220
|
+
this.addToTally(this.localDrops, uuid, reason);
|
|
221
|
+
}
|
|
222
|
+
addToTally(tally, uuid, reason) {
|
|
223
|
+
tally[reason]++;
|
|
224
|
+
if (void 0 !== uuid && tally.sample.length < MAX_REPORTED_LOCAL_DROPS) tally.sample.push({
|
|
225
|
+
uuid,
|
|
226
|
+
details: LOCAL_DROP_DETAILS[reason]
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
reportLocalDrops(leftovers = []) {
|
|
230
|
+
const tally = this.localDrops;
|
|
231
|
+
this.localDrops = emptyTally();
|
|
232
|
+
this.reportTally(tally, leftovers);
|
|
233
|
+
}
|
|
234
|
+
reportTally(tally, leftovers = []) {
|
|
235
|
+
const errors = [];
|
|
236
|
+
const loss = new errors_js_namespaceObject.CaptureLossTally();
|
|
237
|
+
const { endpoint } = this.config;
|
|
238
|
+
if (leftovers.length > 0) {
|
|
239
|
+
const error = new errors_js_namespaceObject.CaptureError({
|
|
240
|
+
endpoint,
|
|
241
|
+
requestId: '',
|
|
242
|
+
drops: [],
|
|
243
|
+
retryExhausted: leftovers.map((event)=>event.uuid)
|
|
244
|
+
});
|
|
245
|
+
error.message = `${leftovers.length} event(s) for ${endpoint} were still queued when shutdown ended and were not sent`;
|
|
246
|
+
errors.push(error);
|
|
247
|
+
loss.add('still queued when shutdown ended', leftovers.length);
|
|
248
|
+
}
|
|
249
|
+
const total = tally.queueFull + tally.tooLarge + tally.invalid + tally.unserializable;
|
|
250
|
+
if (total > 0) {
|
|
251
|
+
const { maxQueueSize, maxEventBytes, maxEventBytesOf } = this.config;
|
|
252
|
+
const reasons = [];
|
|
253
|
+
if (tally.queueFull > 0) reasons.push(`${tally.queueFull} because the queue was full (${maxQueueSize} events)`);
|
|
254
|
+
if (tally.tooLarge > 0) {
|
|
255
|
+
const measured = 'properties' === maxEventBytesOf ? 'properties larger' : 'larger';
|
|
256
|
+
reasons.push(`${tally.tooLarge} with ${measured} than ${maxEventBytes} bytes`);
|
|
257
|
+
}
|
|
258
|
+
if (tally.invalid > 0) reasons.push(`${tally.invalid} with a missing, empty or malformed event name or id`);
|
|
259
|
+
if (tally.unserializable > 0) reasons.push(`${tally.unserializable} with a value that could not be serialized`);
|
|
260
|
+
const error = new errors_js_namespaceObject.CaptureError({
|
|
261
|
+
endpoint,
|
|
262
|
+
requestId: '',
|
|
263
|
+
drops: tally.sample,
|
|
264
|
+
retryExhausted: []
|
|
265
|
+
});
|
|
266
|
+
error.message = `Dropped ${total} event(s) for ${endpoint} before sending: ${reasons.join(', ')}`;
|
|
267
|
+
errors.push(error);
|
|
268
|
+
loss.add(`dropped because the queue was full (${maxQueueSize} events)`, tally.queueFull);
|
|
269
|
+
const measured = 'properties' === maxEventBytesOf ? 'their properties were' : 'they were';
|
|
270
|
+
loss.add(`dropped because ${measured} larger than ${maxEventBytes} bytes`, tally.tooLarge);
|
|
271
|
+
loss.add('dropped because the event name or an id was missing, empty or malformed', tally.invalid);
|
|
272
|
+
loss.add('dropped because a value could not be serialized', tally.unserializable);
|
|
273
|
+
}
|
|
274
|
+
if (errors.length > 0) this.safely(()=>this.hooks.onLocalDrops(errors, (0, errors_js_namespaceObject.describeCaptureLoss)(endpoint, loss)));
|
|
275
|
+
}
|
|
276
|
+
armFlushTimer() {
|
|
277
|
+
if (this.flushTimer || !this.config.flushIntervalMs) return;
|
|
278
|
+
this.flushTimer = (0, core_namespaceObject.safeSetTimeout)(()=>{
|
|
279
|
+
this.flushTimer = void 0;
|
|
280
|
+
this.flushInBackground();
|
|
281
|
+
}, this.config.flushIntervalMs);
|
|
282
|
+
}
|
|
283
|
+
clearFlushTimer() {
|
|
284
|
+
clearTimeout(this.flushTimer);
|
|
285
|
+
this.flushTimer = void 0;
|
|
286
|
+
}
|
|
287
|
+
abortAt(deadlineMs) {
|
|
288
|
+
clearTimeout(this.abortTimer);
|
|
289
|
+
const abort = ()=>this.abortController.abort(new Error('PostHog shutdown deadline reached'));
|
|
290
|
+
const delayMs = deadlineMs - this.now();
|
|
291
|
+
if (delayMs <= 0) return void abort();
|
|
292
|
+
this.abortTimer = (0, core_namespaceObject.safeSetTimeout)(abort, delayMs);
|
|
293
|
+
}
|
|
294
|
+
safely(fn) {
|
|
295
|
+
try {
|
|
296
|
+
fn();
|
|
297
|
+
} catch (error) {
|
|
298
|
+
this.hooks.logger.error('Capture lane hook failed:', error);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
exports.CaptureLane = __webpack_exports__.CaptureLane;
|
|
303
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
304
|
+
"CaptureLane"
|
|
305
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
306
|
+
Object.defineProperty(exports, '__esModule', {
|
|
307
|
+
value: true
|
|
308
|
+
});
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { safeSetTimeout } from "@posthog/core";
|
|
2
|
+
import { CaptureError, CaptureLossTally, describeCaptureLoss } from "../capture-v1/errors.mjs";
|
|
3
|
+
import { serializeV1Event, wireProblem } from "../capture-v1/serialize.mjs";
|
|
4
|
+
const MAX_REPORTED_LOCAL_DROPS = 100;
|
|
5
|
+
const emptyTally = ()=>({
|
|
6
|
+
queueFull: 0,
|
|
7
|
+
tooLarge: 0,
|
|
8
|
+
invalid: 0,
|
|
9
|
+
unserializable: 0,
|
|
10
|
+
sample: []
|
|
11
|
+
});
|
|
12
|
+
const LOCAL_DROP_DETAILS = {
|
|
13
|
+
queueFull: 'queue full',
|
|
14
|
+
tooLarge: 'event too large',
|
|
15
|
+
invalid: 'invalid event',
|
|
16
|
+
unserializable: 'unserializable event'
|
|
17
|
+
};
|
|
18
|
+
class CaptureLane {
|
|
19
|
+
constructor(config, hooks){
|
|
20
|
+
this.queue = [];
|
|
21
|
+
this.activeFlush = null;
|
|
22
|
+
this.queuedFlush = null;
|
|
23
|
+
this.abortController = new AbortController();
|
|
24
|
+
this.localDrops = emptyTally();
|
|
25
|
+
this.settledCount = 0;
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.hooks = hooks;
|
|
28
|
+
this.now = hooks.now ?? Date.now;
|
|
29
|
+
}
|
|
30
|
+
get length() {
|
|
31
|
+
return this.queue.length;
|
|
32
|
+
}
|
|
33
|
+
enqueue(message) {
|
|
34
|
+
const checked = this.check(message);
|
|
35
|
+
if ('drop' in checked) {
|
|
36
|
+
this.recordLocalDrop(checked.uuid, checked.drop);
|
|
37
|
+
this.armFlushTimer();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (this.queue.length >= this.config.maxQueueSize) this.recordLocalDrop(this.queue.shift().uuid, 'queueFull');
|
|
41
|
+
this.queue.push({
|
|
42
|
+
...checked.event,
|
|
43
|
+
message
|
|
44
|
+
});
|
|
45
|
+
if (this.queue.length >= this.config.flushAt) this.flushInBackground();
|
|
46
|
+
else this.armFlushTimer();
|
|
47
|
+
}
|
|
48
|
+
async sendImmediate(message) {
|
|
49
|
+
const checked = this.check(message);
|
|
50
|
+
if ('drop' in checked) {
|
|
51
|
+
const tally = emptyTally();
|
|
52
|
+
this.addToTally(tally, checked.uuid, checked.drop);
|
|
53
|
+
this.reportTally(tally);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
await this.hooks.sender.send([
|
|
57
|
+
checked.event
|
|
58
|
+
], {
|
|
59
|
+
signal: this.abortController.signal
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
dropUnserializable(uuid, immediate) {
|
|
63
|
+
if (immediate) {
|
|
64
|
+
const tally = emptyTally();
|
|
65
|
+
this.addToTally(tally, uuid, 'unserializable');
|
|
66
|
+
this.reportTally(tally);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.recordLocalDrop(uuid, 'unserializable');
|
|
70
|
+
this.armFlushTimer();
|
|
71
|
+
}
|
|
72
|
+
check(message) {
|
|
73
|
+
let event;
|
|
74
|
+
try {
|
|
75
|
+
event = serializeV1Event(message);
|
|
76
|
+
if (wireProblem(message)) return {
|
|
77
|
+
uuid: event.uuid,
|
|
78
|
+
drop: 'invalid'
|
|
79
|
+
};
|
|
80
|
+
} catch {
|
|
81
|
+
return {
|
|
82
|
+
uuid: String(message.uuid),
|
|
83
|
+
drop: 'unserializable'
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return this.isTooLarge(event) ? {
|
|
87
|
+
uuid: event.uuid,
|
|
88
|
+
drop: 'tooLarge'
|
|
89
|
+
} : {
|
|
90
|
+
event
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
flush() {
|
|
94
|
+
if (this.queuedFlush) return this.queuedFlush;
|
|
95
|
+
if (!this.activeFlush) return this.startFlush();
|
|
96
|
+
const queued = this.activeFlush.then(()=>{
|
|
97
|
+
this.queuedFlush = null;
|
|
98
|
+
return this.startFlush();
|
|
99
|
+
});
|
|
100
|
+
this.queuedFlush = queued;
|
|
101
|
+
return queued;
|
|
102
|
+
}
|
|
103
|
+
async drain(deadlineMs) {
|
|
104
|
+
this.abortAt(deadlineMs);
|
|
105
|
+
let settledBefore;
|
|
106
|
+
do {
|
|
107
|
+
settledBefore = this.settledCount;
|
|
108
|
+
await this.flush();
|
|
109
|
+
}while (this.queue.length > 0 && this.settledCount !== settledBefore && !this.abortController.signal.aborted);
|
|
110
|
+
}
|
|
111
|
+
finishShutdown() {
|
|
112
|
+
clearTimeout(this.abortTimer);
|
|
113
|
+
this.abortTimer = void 0;
|
|
114
|
+
this.clearFlushTimer();
|
|
115
|
+
this.reportLocalDrops(this.queue.splice(0));
|
|
116
|
+
if (this.abortController.signal.aborted) this.abortController = new AbortController();
|
|
117
|
+
}
|
|
118
|
+
startFlush() {
|
|
119
|
+
const flush = this.runFlushCycle().finally(()=>{
|
|
120
|
+
if (this.activeFlush === flush) this.activeFlush = null;
|
|
121
|
+
});
|
|
122
|
+
this.activeFlush = flush;
|
|
123
|
+
return flush;
|
|
124
|
+
}
|
|
125
|
+
flushInBackground() {
|
|
126
|
+
if (this.queuedFlush) return;
|
|
127
|
+
const flush = this.flush();
|
|
128
|
+
this.safely(()=>this.hooks.onBackgroundFlush?.(flush));
|
|
129
|
+
}
|
|
130
|
+
async runFlushCycle() {
|
|
131
|
+
this.clearFlushTimer();
|
|
132
|
+
await Promise.resolve();
|
|
133
|
+
const budget = this.queue.length;
|
|
134
|
+
const sent = [];
|
|
135
|
+
let taken = 0;
|
|
136
|
+
try {
|
|
137
|
+
while(taken < budget && this.queue.length > 0){
|
|
138
|
+
const batch = this.takeBatch();
|
|
139
|
+
taken += batch.length;
|
|
140
|
+
let unsent;
|
|
141
|
+
try {
|
|
142
|
+
unsent = await this.hooks.sender.send(batch, {
|
|
143
|
+
signal: this.abortController.signal,
|
|
144
|
+
keepUnsent: true
|
|
145
|
+
});
|
|
146
|
+
} catch (error) {
|
|
147
|
+
this.settledCount += batch.length;
|
|
148
|
+
this.safely(()=>this.hooks.onError(error));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
this.settledCount += batch.length - unsent.length;
|
|
152
|
+
const requeued = new Set(unsent);
|
|
153
|
+
for (const event of batch)if (!requeued.has(event)) sent.push(event.message);
|
|
154
|
+
if (unsent.length > 0) {
|
|
155
|
+
this.requeue(unsent);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch (error) {
|
|
160
|
+
this.safely(()=>this.hooks.onError(error));
|
|
161
|
+
} finally{
|
|
162
|
+
if (sent.length > 0) this.safely(()=>this.hooks.onFlush(sent));
|
|
163
|
+
this.reportLocalDrops();
|
|
164
|
+
if (this.queue.length > 0) this.armFlushTimer();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
takeBatch() {
|
|
168
|
+
let count = 0;
|
|
169
|
+
let bytes = 0;
|
|
170
|
+
while(count < this.queue.length && count < this.config.maxBatchSize){
|
|
171
|
+
const next = this.queue[count];
|
|
172
|
+
if (count > 0 && bytes + next.bytes > this.config.targetBatchBytes) break;
|
|
173
|
+
bytes += next.bytes;
|
|
174
|
+
count++;
|
|
175
|
+
}
|
|
176
|
+
return this.queue.splice(0, count);
|
|
177
|
+
}
|
|
178
|
+
requeue(events) {
|
|
179
|
+
this.queue.unshift(...events);
|
|
180
|
+
while(this.queue.length > this.config.maxQueueSize)this.recordLocalDrop(this.queue.shift().uuid, 'queueFull');
|
|
181
|
+
this.hooks.logger.debug(`Kept ${events.length} event(s) for ${this.config.endpoint} after a network failure; the next flush sends them`);
|
|
182
|
+
}
|
|
183
|
+
isTooLarge(event) {
|
|
184
|
+
const bytes = 'properties' === this.config.maxEventBytesOf ? event.propertiesBytes : event.bytes;
|
|
185
|
+
return bytes > this.config.maxEventBytes;
|
|
186
|
+
}
|
|
187
|
+
recordLocalDrop(uuid, reason) {
|
|
188
|
+
this.addToTally(this.localDrops, uuid, reason);
|
|
189
|
+
}
|
|
190
|
+
addToTally(tally, uuid, reason) {
|
|
191
|
+
tally[reason]++;
|
|
192
|
+
if (void 0 !== uuid && tally.sample.length < MAX_REPORTED_LOCAL_DROPS) tally.sample.push({
|
|
193
|
+
uuid,
|
|
194
|
+
details: LOCAL_DROP_DETAILS[reason]
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
reportLocalDrops(leftovers = []) {
|
|
198
|
+
const tally = this.localDrops;
|
|
199
|
+
this.localDrops = emptyTally();
|
|
200
|
+
this.reportTally(tally, leftovers);
|
|
201
|
+
}
|
|
202
|
+
reportTally(tally, leftovers = []) {
|
|
203
|
+
const errors = [];
|
|
204
|
+
const loss = new CaptureLossTally();
|
|
205
|
+
const { endpoint } = this.config;
|
|
206
|
+
if (leftovers.length > 0) {
|
|
207
|
+
const error = new CaptureError({
|
|
208
|
+
endpoint,
|
|
209
|
+
requestId: '',
|
|
210
|
+
drops: [],
|
|
211
|
+
retryExhausted: leftovers.map((event)=>event.uuid)
|
|
212
|
+
});
|
|
213
|
+
error.message = `${leftovers.length} event(s) for ${endpoint} were still queued when shutdown ended and were not sent`;
|
|
214
|
+
errors.push(error);
|
|
215
|
+
loss.add('still queued when shutdown ended', leftovers.length);
|
|
216
|
+
}
|
|
217
|
+
const total = tally.queueFull + tally.tooLarge + tally.invalid + tally.unserializable;
|
|
218
|
+
if (total > 0) {
|
|
219
|
+
const { maxQueueSize, maxEventBytes, maxEventBytesOf } = this.config;
|
|
220
|
+
const reasons = [];
|
|
221
|
+
if (tally.queueFull > 0) reasons.push(`${tally.queueFull} because the queue was full (${maxQueueSize} events)`);
|
|
222
|
+
if (tally.tooLarge > 0) {
|
|
223
|
+
const measured = 'properties' === maxEventBytesOf ? 'properties larger' : 'larger';
|
|
224
|
+
reasons.push(`${tally.tooLarge} with ${measured} than ${maxEventBytes} bytes`);
|
|
225
|
+
}
|
|
226
|
+
if (tally.invalid > 0) reasons.push(`${tally.invalid} with a missing, empty or malformed event name or id`);
|
|
227
|
+
if (tally.unserializable > 0) reasons.push(`${tally.unserializable} with a value that could not be serialized`);
|
|
228
|
+
const error = new CaptureError({
|
|
229
|
+
endpoint,
|
|
230
|
+
requestId: '',
|
|
231
|
+
drops: tally.sample,
|
|
232
|
+
retryExhausted: []
|
|
233
|
+
});
|
|
234
|
+
error.message = `Dropped ${total} event(s) for ${endpoint} before sending: ${reasons.join(', ')}`;
|
|
235
|
+
errors.push(error);
|
|
236
|
+
loss.add(`dropped because the queue was full (${maxQueueSize} events)`, tally.queueFull);
|
|
237
|
+
const measured = 'properties' === maxEventBytesOf ? 'their properties were' : 'they were';
|
|
238
|
+
loss.add(`dropped because ${measured} larger than ${maxEventBytes} bytes`, tally.tooLarge);
|
|
239
|
+
loss.add('dropped because the event name or an id was missing, empty or malformed', tally.invalid);
|
|
240
|
+
loss.add('dropped because a value could not be serialized', tally.unserializable);
|
|
241
|
+
}
|
|
242
|
+
if (errors.length > 0) this.safely(()=>this.hooks.onLocalDrops(errors, describeCaptureLoss(endpoint, loss)));
|
|
243
|
+
}
|
|
244
|
+
armFlushTimer() {
|
|
245
|
+
if (this.flushTimer || !this.config.flushIntervalMs) return;
|
|
246
|
+
this.flushTimer = safeSetTimeout(()=>{
|
|
247
|
+
this.flushTimer = void 0;
|
|
248
|
+
this.flushInBackground();
|
|
249
|
+
}, this.config.flushIntervalMs);
|
|
250
|
+
}
|
|
251
|
+
clearFlushTimer() {
|
|
252
|
+
clearTimeout(this.flushTimer);
|
|
253
|
+
this.flushTimer = void 0;
|
|
254
|
+
}
|
|
255
|
+
abortAt(deadlineMs) {
|
|
256
|
+
clearTimeout(this.abortTimer);
|
|
257
|
+
const abort = ()=>this.abortController.abort(new Error('PostHog shutdown deadline reached'));
|
|
258
|
+
const delayMs = deadlineMs - this.now();
|
|
259
|
+
if (delayMs <= 0) return void abort();
|
|
260
|
+
this.abortTimer = safeSetTimeout(abort, delayMs);
|
|
261
|
+
}
|
|
262
|
+
safely(fn) {
|
|
263
|
+
try {
|
|
264
|
+
fn();
|
|
265
|
+
} catch (error) {
|
|
266
|
+
this.hooks.logger.error('Capture lane hook failed:', error);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
export { CaptureLane };
|