@keploy/capture-sdk 3.0.1
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/LICENSE +202 -0
- package/README.md +482 -0
- package/dist/auto.d.ts +8 -0
- package/dist/errors.d.ts +252 -0
- package/dist/identity.d.ts +94 -0
- package/dist/index.d.ts +104 -0
- package/dist/index.js +2354 -0
- package/dist/index.mjs +2297 -0
- package/dist/network.d.ts +90 -0
- package/dist/privacy.d.ts +36 -0
- package/dist/recorder.d.ts +164 -0
- package/dist/redact.d.ts +47 -0
- package/dist/session-id.d.ts +32 -0
- package/dist/transport.d.ts +332 -0
- package/dist/types.d.ts +363 -0
- package/package.json +51 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import type { SessionEvent, SessionMetadata } from "./types";
|
|
2
|
+
import type { AnyCaptureError } from "./errors";
|
|
3
|
+
import type { CaptureIdentity } from "./identity";
|
|
4
|
+
/** A request body plus its size on the wire, which the keepalive gate needs. */
|
|
5
|
+
interface WireBody {
|
|
6
|
+
body: BodyInit;
|
|
7
|
+
bytes: number;
|
|
8
|
+
encoding: string | null;
|
|
9
|
+
}
|
|
10
|
+
/** Exported for its test; not part of the package surface (index.ts does not re-export it). */
|
|
11
|
+
export declare function compressPayload(data: string): Promise<WireBody>;
|
|
12
|
+
/**
|
|
13
|
+
* The most events the ingest route accepts in one batch:
|
|
14
|
+
* sdkwire.MaxEventsPerBatch in api-server. A batch over it is refused
|
|
15
|
+
* with a 400 naming `events`, so the transport never builds one.
|
|
16
|
+
*/
|
|
17
|
+
export declare const MAX_EVENTS_PER_BATCH = 5000;
|
|
18
|
+
/**
|
|
19
|
+
* The most bytes the ingest route accepts in one body, DECODED:
|
|
20
|
+
* uiIngestMaxBodyBytes in api-server (10 MiB; the same cap applies to
|
|
21
|
+
* the wire, and compressPayload sends whichever of gzip and identity
|
|
22
|
+
* is smaller, so the wire is never larger than the JSON). A body over
|
|
23
|
+
* it is a terminal 413
|
|
24
|
+
* `decoded request body exceeds N bytes`, so a flight is formed
|
|
25
|
+
* against this as well as the count -- MEASURED before the byte cut:
|
|
26
|
+
* batchSize 5000 of 2 KB network events made one 10.3 MiB POST, and
|
|
27
|
+
* the DEFAULT batchSize 50 of 220 KB responses (inside maxBodySize)
|
|
28
|
+
* made 10.5 MiB, each ending the capture with a remedy that blamed a
|
|
29
|
+
* version mismatch. An event that alone exceeds it is dropped at
|
|
30
|
+
* enqueue: the server can never take it.
|
|
31
|
+
*/
|
|
32
|
+
export declare const MAX_BATCH_BYTES: number;
|
|
33
|
+
export interface TransportConfig {
|
|
34
|
+
endpoint: string;
|
|
35
|
+
apiKey: string;
|
|
36
|
+
/** Events per POST; clamped to MAX_EVENTS_PER_BATCH. */
|
|
37
|
+
batchSize: number;
|
|
38
|
+
batchInterval: number;
|
|
39
|
+
maxQueueSize: number;
|
|
40
|
+
/**
|
|
41
|
+
* Called once when capture stops for good, AFTER the capture has
|
|
42
|
+
* ended and the singleton is released, so a handler that calls
|
|
43
|
+
* KeployCapture.init() again gets a fresh instance rather than the
|
|
44
|
+
* dead one. Optional, and the console error happens regardless: an
|
|
45
|
+
* integrator who does not supply one must still find out. The
|
|
46
|
+
* argument is a CaptureError: `code` is the contract's KUI_E_* for
|
|
47
|
+
* what happened, `serverMessage` is the server's own sentence,
|
|
48
|
+
* `remedy` is what to do about it, and `message` is the prose a human
|
|
49
|
+
* read before. A program should branch on `code`.
|
|
50
|
+
*/
|
|
51
|
+
onError?: (err: AnyCaptureError) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Called once when a terminal rejection has ended this transport, so
|
|
54
|
+
* the owner can end the CAPTURE.
|
|
55
|
+
*
|
|
56
|
+
* Distinct from {@link onError}, which is the integrator's. This one
|
|
57
|
+
* is the orchestrator's: stopping the transport leaves rrweb
|
|
58
|
+
* recording, fetch and XHR patched, the session timer armed and the
|
|
59
|
+
* singleton set, so without it a revoked key produced a page that
|
|
60
|
+
* went on paying for a capture that could never post again — and a
|
|
61
|
+
* later init() handed back the dead instance.
|
|
62
|
+
*/
|
|
63
|
+
onCaptureEnd?: () => void;
|
|
64
|
+
}
|
|
65
|
+
export declare class Transport {
|
|
66
|
+
private config;
|
|
67
|
+
private queue;
|
|
68
|
+
/**
|
|
69
|
+
* Flights that failed to send and must go out again AS THEY WERE.
|
|
70
|
+
* Drained ahead of the event queue by run(). Never merged back into
|
|
71
|
+
* `queue`: re-splicing a dispatched flight's events with newer ones
|
|
72
|
+
* would put already-stored events under a new batchId, which is the
|
|
73
|
+
* duplication the id exists to prevent -- and re-using the id over a
|
|
74
|
+
* different event set is the 409 the server answers instead.
|
|
75
|
+
*/
|
|
76
|
+
private retryQueue;
|
|
77
|
+
/** Only used by the no-randomUUID fallback of mintBatchId(). */
|
|
78
|
+
private batchCounter;
|
|
79
|
+
private flushTimer;
|
|
80
|
+
/** The flush in progress, or null. One at a time: batches stay ordered. */
|
|
81
|
+
private current;
|
|
82
|
+
private sessionId;
|
|
83
|
+
private metadata;
|
|
84
|
+
private captureIdentity;
|
|
85
|
+
private online;
|
|
86
|
+
private started;
|
|
87
|
+
/** The stamp the next queued event gets; see enqueue(). */
|
|
88
|
+
private nextSeq;
|
|
89
|
+
/** Set by a drain request that arrived while a flush was running. */
|
|
90
|
+
private drainPending;
|
|
91
|
+
/**
|
|
92
|
+
* Set when an event over the keepalive quota was queued while the
|
|
93
|
+
* browser said it was offline, so the online handler drains for it
|
|
94
|
+
* instead of taking one batch. Cleared once the queue has run empty.
|
|
95
|
+
*/
|
|
96
|
+
private rushDeferred;
|
|
97
|
+
private inflightKeepaliveBytes;
|
|
98
|
+
/**
|
|
99
|
+
* Bytes of a request carrying no events: the fixed cost every request
|
|
100
|
+
* pays, so an event's own size can be measured against the quota.
|
|
101
|
+
*/
|
|
102
|
+
private envelopeBytes;
|
|
103
|
+
/** Set by the first event JSON.stringify refused; see reportUnserialisable(). */
|
|
104
|
+
private reportedUnserialisable;
|
|
105
|
+
private reportedOversized;
|
|
106
|
+
/**
|
|
107
|
+
* Each queued event's JSON bytes, measured once at enqueue (the
|
|
108
|
+
* keepalive rush needs the number there anyway) and read wherever a
|
|
109
|
+
* cut is sized: the timed flight and the unload flush.
|
|
110
|
+
*/
|
|
111
|
+
private readonly bytesOf;
|
|
112
|
+
/** The batch run() is working on, so the unload flush can take it back. */
|
|
113
|
+
private inFlight;
|
|
114
|
+
constructor(config: TransportConfig);
|
|
115
|
+
/**
|
|
116
|
+
* Report a condition that ends the capture, and end it.
|
|
117
|
+
*
|
|
118
|
+
* NOT a throw: this runs inside a flush that nobody awaits, so a throw
|
|
119
|
+
* becomes an unhandled rejection and is exactly as invisible as the
|
|
120
|
+
* bare `return` it replaces. An integrator gets the callback; everyone
|
|
121
|
+
* else gets a console error, which is the floor.
|
|
122
|
+
*/
|
|
123
|
+
private fail;
|
|
124
|
+
start(sessionId: string, metadata: SessionMetadata, captureIdentity?: CaptureIdentity | null): void;
|
|
125
|
+
/**
|
|
126
|
+
* Stop accepting events and send what is queued.
|
|
127
|
+
*
|
|
128
|
+
* Resolves when the final flush has settled — a caller that needs to
|
|
129
|
+
* know the tail arrived (a test, an integrator about to navigate)
|
|
130
|
+
* can await it; nothing requires it.
|
|
131
|
+
*/
|
|
132
|
+
stop(): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Removes the unload and hidden-tab listeners once there is nothing
|
|
135
|
+
* left for them to send: after stop(), the first time the queue is
|
|
136
|
+
* empty. Called wherever the queue can become empty — the drain
|
|
137
|
+
* stop() started, a later drain (a hidden tab, or fail() on a
|
|
138
|
+
* terminal rejection), the unload flush itself — because a listener
|
|
139
|
+
* kept past that point is a leak for the life of the document: a
|
|
140
|
+
* drain that failed on logout kept them, and the hidden-tab drain
|
|
141
|
+
* that later emptied the queue removed nothing, so the Transport was
|
|
142
|
+
* retained by pagehide, beforeunload and visibilitychange for as
|
|
143
|
+
* long as the document lived. Idempotent; a no-op while started or
|
|
144
|
+
* while events remain.
|
|
145
|
+
*/
|
|
146
|
+
private disarmWhenDrained;
|
|
147
|
+
enqueue(event: SessionEvent): void;
|
|
148
|
+
/**
|
|
149
|
+
* Say that an event was dropped because it could not be serialised —
|
|
150
|
+
* ONCE per transport, and the line says so.
|
|
151
|
+
*
|
|
152
|
+
* Not a throw: the caller is rrweb's emit callback or the
|
|
153
|
+
* integrator's own code, and the SDK has no business ending their
|
|
154
|
+
* turn. Not `onError` either: that one is reserved for a terminal
|
|
155
|
+
* condition that ENDS the capture, and this one does not — the
|
|
156
|
+
* session goes on without this event.
|
|
157
|
+
*
|
|
158
|
+
* Once, because a custom event emitted from a render or a timer would
|
|
159
|
+
* otherwise write a console line every few milliseconds for the life
|
|
160
|
+
* of the page: the SDK degrading the page it is there to record. The
|
|
161
|
+
* first line carries the event's id and the reason, which is what a
|
|
162
|
+
* developer needs to find the emitter; the rest would only repeat it.
|
|
163
|
+
*/
|
|
164
|
+
private reportUnserialisable;
|
|
165
|
+
/**
|
|
166
|
+
* Say that an event was dropped because, alone in a body, it is over
|
|
167
|
+
* the server's decoded cap -- ONCE per transport, like
|
|
168
|
+
* reportUnserialisable and for the same reasons: not a throw into the
|
|
169
|
+
* emitter's turn, not `onError` (the capture goes on), and not a line
|
|
170
|
+
* per event.
|
|
171
|
+
*/
|
|
172
|
+
private reportOversized;
|
|
173
|
+
/** The request body for `events`, with everything a batch carries. */
|
|
174
|
+
private payload;
|
|
175
|
+
/**
|
|
176
|
+
* Send queued events.
|
|
177
|
+
*
|
|
178
|
+
* `drain` sends EVERYTHING queued — batch after batch, each one sized
|
|
179
|
+
* like a timed batch, so the server's body limit is the same one a
|
|
180
|
+
* timed batch faces — and if a flush is already running it is picked
|
|
181
|
+
* up by that flush when its current batch finishes. A drain that
|
|
182
|
+
* silently returned because a batch happened to be in flight would
|
|
183
|
+
* leave a hidden tab holding events it may never get another chance
|
|
184
|
+
* to send. A drain also ignores the `offline` flag: it is the last
|
|
185
|
+
* attempt, the browser's offline signal is advisory, and a fetch that
|
|
186
|
+
* fails retries and re-queues anyway. The promise resolves when the
|
|
187
|
+
* running flush, drain included, has settled.
|
|
188
|
+
*/
|
|
189
|
+
flush(drain?: boolean): Promise<void>;
|
|
190
|
+
private run;
|
|
191
|
+
private sendWithRetry;
|
|
192
|
+
/**
|
|
193
|
+
* The last flush, issued while the document is being torn down.
|
|
194
|
+
*
|
|
195
|
+
* THIS WAS navigator.sendBeacon, AND EVERY SESSION LOST ITS TAIL.
|
|
196
|
+
* sendBeacon cannot set headers, so the batch arrived at the ingest
|
|
197
|
+
* endpoint with no X-API-Key and was refused with 401 — after the
|
|
198
|
+
* events had been spliced out of the queue, and with no response for
|
|
199
|
+
* anything here to read. MEASURED in Chromium 145.0.7632.6 with a
|
|
200
|
+
* cross-origin endpoint, 5 runs per scenario: the beacon's
|
|
201
|
+
* application/json Blob also forces a CORS preflight the beacon does
|
|
202
|
+
* not survive, 0/5 delivered on navigate-away and 0/5 on tab close.
|
|
203
|
+
*
|
|
204
|
+
* fetch with keepalive:true is what sendBeacon is specified as, plus
|
|
205
|
+
* headers: same measurement, 5/5 and 5/5, key attached. The request
|
|
206
|
+
* is dispatched SYNCHRONOUSLY inside the unload handler — no
|
|
207
|
+
* compression, because CompressionStream is async and an await here
|
|
208
|
+
* lands after the document is gone.
|
|
209
|
+
*
|
|
210
|
+
* THE PAYLOAD IS CUT AT THE KEEPALIVE QUOTA, oldest events first. A
|
|
211
|
+
* keepalive request over the quota is refused by the browser before
|
|
212
|
+
* it leaves, and one refused request loses everything; one cut at the
|
|
213
|
+
* quota ships the prefix that fits. The quota is on the SUM of
|
|
214
|
+
* in-flight keepalive bodies, so a timed batch still on the wire is
|
|
215
|
+
* subtracted.
|
|
216
|
+
*
|
|
217
|
+
* THE CUT IS AT THE FIRST EVENT THAT DOES NOT FIT, in order — not
|
|
218
|
+
* skip-and-continue. An event over the quota on its own (a network
|
|
219
|
+
* event with a large body; an rrweb FullSnapshot of a big page) holds
|
|
220
|
+
* back everything queued behind it, small or not. Not for order's
|
|
221
|
+
* sake — every event carries its `seq` and the server sorts by it, so
|
|
222
|
+
* a document that survives its pagehide (a back/forward-cache entry)
|
|
223
|
+
* and later ships the held-back event from its timer is stored in
|
|
224
|
+
* order regardless. For the node map: the flow engine's extractor.ts
|
|
225
|
+
* builds it from the FullSnapshot, and every incremental event
|
|
226
|
+
* behind a snapshot names its nodes by that snapshot's ids. Shipped
|
|
227
|
+
* without it, they are resolved against a map that does not hold
|
|
228
|
+
* their nodes, and selectorFromNode answers `body` for a node it
|
|
229
|
+
* cannot find — a click on `body` in the generated test, silently,
|
|
230
|
+
* where the session ending at the snapshot would have been the
|
|
231
|
+
* truth. So what does not fit holds back what depends on it.
|
|
232
|
+
*
|
|
233
|
+
* Events that do not fit stay in the queue: there is no way to send
|
|
234
|
+
* them from a dying page, and no way to report it — this is the one
|
|
235
|
+
* flush that is fire-and-forget, and only because the document is
|
|
236
|
+
* leaving. Which is why an event that can never fit is not left to
|
|
237
|
+
* wait here: enqueue() sends it as soon as it is queued.
|
|
238
|
+
*/
|
|
239
|
+
/**
|
|
240
|
+
* Take back the batch run() is holding, if no request for it has been
|
|
241
|
+
* dispatched yet.
|
|
242
|
+
*
|
|
243
|
+
* THE GAP THIS CLOSES. run() splices a batch out of the queue and
|
|
244
|
+
* hands it to sendWithRetry, and a request does not leave the browser
|
|
245
|
+
* straight away: compressPayload is asynchronous, and after a failed
|
|
246
|
+
* attempt sendWithRetry sleeps 0.5–1 s, then 1–2 s, before the next
|
|
247
|
+
* one. Through all of that the events are in neither the queue nor a
|
|
248
|
+
* request, and the unload flush could not see them. pagehide in that
|
|
249
|
+
* window shipped the events queued AFTER them with keepalive and
|
|
250
|
+
* never dispatched these, because an await does not resume in a
|
|
251
|
+
* document that is gone — so the server's seq-sorted file got the
|
|
252
|
+
* tail with a hole in the body. A hole is worse than a short
|
|
253
|
+
* recording: the events after it name node ids the missing batch
|
|
254
|
+
* introduced, and the flow engine resolves them against a map that
|
|
255
|
+
* does not hold them.
|
|
256
|
+
*
|
|
257
|
+
* ONLY WHILE NOTHING IS ON THE WIRE. A dispatched keepalive request
|
|
258
|
+
* outlives the document by design, and one without keepalive may
|
|
259
|
+
* still arrive; re-sending those events would duplicate them in a
|
|
260
|
+
* file the server appends to by seq. That request in flight when the
|
|
261
|
+
* document dies is the residual limit, and it is the same one every
|
|
262
|
+
* in-flight request has.
|
|
263
|
+
*
|
|
264
|
+
* `abandoned` stops every further attempt, so a document that
|
|
265
|
+
* SURVIVES its pagehide (a back/forward-cache entry, a cancelled
|
|
266
|
+
* navigation) does not send the batch a second time when the backoff
|
|
267
|
+
* resumes, and run()'s catch does not unshift what has already gone.
|
|
268
|
+
*/
|
|
269
|
+
private reclaimInFlight;
|
|
270
|
+
/**
|
|
271
|
+
* The next waiting flight as a FRESH ATTEMPT CONTEXT, or null.
|
|
272
|
+
*
|
|
273
|
+
* `abandoned` does one job: it stops the sendWithRetry loop that was
|
|
274
|
+
* running when the unload flush took the flight. That loop still
|
|
275
|
+
* holds the OLD object, so it must keep seeing `abandoned === true`
|
|
276
|
+
* -- but the re-send must not, or it returns from sendWithRetry
|
|
277
|
+
* before its first attempt and run() counts the flight as delivered.
|
|
278
|
+
* MEASURED: a flight over the keepalive quota, reclaimed at pagehide
|
|
279
|
+
* during its backoff on a document that survived (a download click,
|
|
280
|
+
* a cancelled navigation, a bfcache restore), was shifted by the next
|
|
281
|
+
* flush and dropped -- queueSize 2 -> 0, zero requests, where the
|
|
282
|
+
* pre-batchId transport delivered both events. So the flags are
|
|
283
|
+
* per attempt and the id and bytes are per flight.
|
|
284
|
+
*/
|
|
285
|
+
private reviveNext;
|
|
286
|
+
/**
|
|
287
|
+
* A new flight from the head of the queue, or null when it is empty.
|
|
288
|
+
* The id and the bytes are minted together, once.
|
|
289
|
+
*
|
|
290
|
+
* CUT BY BYTES AS WELL AS BY COUNT, the way the unload flush cuts
|
|
291
|
+
* against the keepalive quota: at the first event that would take the
|
|
292
|
+
* decoded body over MAX_BATCH_BYTES, everything behind it holding for
|
|
293
|
+
* the next flight. The first event always fits -- enqueue() refused
|
|
294
|
+
* anything that alone would not -- so a flight is never empty while
|
|
295
|
+
* the queue is not.
|
|
296
|
+
*/
|
|
297
|
+
private formFlight;
|
|
298
|
+
/** The event's JSON bytes: what enqueue() measured, or measured now for an event that reached the queue another way. */
|
|
299
|
+
private eventBytes;
|
|
300
|
+
/**
|
|
301
|
+
* The SDK's idempotency key for one POST: 36 characters of a UUID,
|
|
302
|
+
* which is within the server's session-id shape (letters, digits,
|
|
303
|
+
* dot, dash, underscore). Falls back to time plus a counter and a
|
|
304
|
+
* random suffix where crypto.randomUUID is not available; only
|
|
305
|
+
* uniqueness within one session matters, since the server scopes the
|
|
306
|
+
* id to the session.
|
|
307
|
+
*/
|
|
308
|
+
private mintBatchId;
|
|
309
|
+
/** Events waiting to be sent, retry flights included. */
|
|
310
|
+
private pendingCount;
|
|
311
|
+
/**
|
|
312
|
+
* On a re-queue, hold the total at maxQueueSize by dropping the NEWEST
|
|
313
|
+
* queued events, as the old unshift-then-pop did. enqueue() trims the
|
|
314
|
+
* queue alone (oldest first) and does not see waiting flights, so
|
|
315
|
+
* between re-queues the total can stand one batch over maxQueueSize
|
|
316
|
+
* -- the same bound the old code had, where one batch sat off the
|
|
317
|
+
* queue in flight. A retry flight is never trimmed: dropping events
|
|
318
|
+
* from it would change the set its batchId is bound to, and the
|
|
319
|
+
* server answers that with a 409.
|
|
320
|
+
*/
|
|
321
|
+
private trimToCapacity;
|
|
322
|
+
private flushOnUnload;
|
|
323
|
+
/** One fire-and-forget keepalive POST, accounted against the quota. */
|
|
324
|
+
private postKeepalive;
|
|
325
|
+
private handleOnline;
|
|
326
|
+
private handleOffline;
|
|
327
|
+
private handleUnload;
|
|
328
|
+
private handleVisibility;
|
|
329
|
+
/** Events waiting to be sent, including those in flights waiting to be re-sent. */
|
|
330
|
+
get queueSize(): number;
|
|
331
|
+
}
|
|
332
|
+
export {};
|