@keploy/capture-sdk 3.1.0-rc.2 → 3.2.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 (43) hide show
  1. package/README.md +259 -122
  2. package/dist/body-jobs.d.ts +108 -10
  3. package/dist/body-redact.d.ts +6 -0
  4. package/dist/body-redact.js +1 -1
  5. package/dist/body-redact.js.map +1 -1
  6. package/dist/body-redact.mjs +1 -1
  7. package/dist/body-redact.mjs.map +1 -1
  8. package/dist/body-worker-none.js +1 -1
  9. package/dist/body-worker-none.js.map +1 -1
  10. package/dist/body-worker-none.mjs +1 -1
  11. package/dist/body-worker-none.mjs.map +1 -1
  12. package/dist/body-worker-start.js +1 -1
  13. package/dist/body-worker-start.js.map +1 -1
  14. package/dist/body-worker-start.mjs +1 -1
  15. package/dist/body-worker-start.mjs.map +1 -1
  16. package/dist/body-worker-unbundled.mjs +1 -1
  17. package/dist/body-worker-unbundled.mjs.map +1 -1
  18. package/dist/body-worker.js +1 -1
  19. package/dist/body-worker.js.map +1 -1
  20. package/dist/index.bundler.mjs +1 -1
  21. package/dist/index.bundler.mjs.map +1 -1
  22. package/dist/index.d.ts +76 -14
  23. package/dist/index.js +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mjs +1 -1
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/module-of.d.ts +17 -0
  28. package/dist/network.d.ts +51 -34
  29. package/dist/page-stream.d.ts +24 -0
  30. package/dist/privacy.d.ts +4 -1
  31. package/dist/recorder.d.ts +3 -3
  32. package/dist/redact.d.ts +25 -2
  33. package/dist/request-term.js +1 -1
  34. package/dist/request-term.js.map +1 -1
  35. package/dist/request-term.mjs +1 -1
  36. package/dist/request-term.mjs.map +1 -1
  37. package/dist/strict-attributes.js +1 -1
  38. package/dist/strict-attributes.js.map +1 -1
  39. package/dist/strict-attributes.mjs +1 -1
  40. package/dist/strict-attributes.mjs.map +1 -1
  41. package/dist/transport.d.ts +10 -10
  42. package/dist/types.d.ts +27 -6
  43. package/package.json +2 -1
package/README.md CHANGED
@@ -5,18 +5,15 @@ It records what a real user does in your web app — the DOM through
5
5
  [rrweb](https://github.com/rrweb-io/rrweb), and every `fetch` and
6
6
  `XMLHttpRequest` exchange the page makes — masks personal data before
7
7
  anything leaves the browser, and posts the recording in batches to
8
- Keploy. Keploy turns the recordings into deduplicated flows; turning
9
- flows into Playwright tests that replay against recorded mocks is the
10
- next step of the product and not in this release (see below).
8
+ Keploy. Keploy turns the recordings into deduplicated flows and, in
9
+ beta, into Playwright tests that replay recorded journeys against
10
+ recorded mocks. This package only records: generating and running the
11
+ tests is Keploy's side, not the SDK's (see below).
11
12
 
12
13
  This package runs inside your bundle, on your users' machines, which is
13
14
  why it is **Apache-2.0** while the rest of the product is proprietary.
14
15
  It is the only part of Keploy UI Capture on npm.
15
16
 
16
- > The UI Capture guide and the two screenshots below are published with
17
- > the docs release that accompanies this SDK release; until then those
18
- > links answer 404.
19
-
20
17
  ## What ships today
21
18
 
22
19
  **The version is the git tag's** -- the committed manifest says
@@ -35,14 +32,16 @@ over the REST API (`/client/v1/apps/<appId>/ui/capture-status` and
35
32
  [UI Capture guide](https://keploy.io/docs/running-keploy/ui-capture/)
36
33
  walks through all three.
37
34
 
38
- Not in this release: generating Playwright tests from flows and running
39
- them. Flows are what you get today.
35
+ In beta: generating Playwright tests from recorded journeys (a visit's
36
+ flows, chained in order) and running them against recorded mocks. That
37
+ is Keploy's side, not this package's: the SDK only records, and nothing
38
+ in it generates or runs a test.
40
39
 
41
40
  ## What you get
42
41
 
43
42
  Each visit becomes flows, listed per app in the Keploy dashboard:
44
43
 
45
- ![The UI Flows list: each flow with its page path, step count and the network calls it made](https://raw.githubusercontent.com/keploy/docs/main/static/img/ui-capture/ui-capture-flows.webp)
44
+ ![The Flows tab's table of flows, each with its name, session, last update time and status, under the note that each route is its own flow since SDK 3.1.0](https://raw.githubusercontent.com/keploy/docs/main/static/img/ui-capture/ui-capture-flows.webp)
46
45
 
47
46
  Each flow is the sequence of steps a user took, with every network call
48
47
  those steps made recorded beside them:
@@ -63,9 +62,9 @@ are no peer dependencies. The package ships ESM and CommonJS
63
62
  rather than the `exports` map (webpack 4, Parcel by default): the same
64
63
  SDK without the worker start a page with no bundler loads from beside
65
64
  it (see below), so webpack 4 never reads `import.meta`, for the web or
66
- for node. The bundle is 40.7 kB
65
+ for node. The bundle is 40.9 kB
67
66
  (decimal) gzipped with rrweb excluded -- the ESM build; the CommonJS
68
- build is 40.9 kB and the `module` field's build is 40.6 kB, and
67
+ build is 41.1 kB and the `module` field's build is 40.9 kB, and
69
68
  `privacyMode: "strict"` alone loads its 1.3 kB attribute allow-list
70
69
  beside it -- MEASURED on the repository's build at its
71
70
  placeholder version (`pnpm build`, which runs `node scripts/build-main.mjs`);
@@ -73,16 +72,16 @@ the published build carries its version in place of the placeholder,
73
72
  which moves both figures by a few bytes either way (MEASURED for
74
73
  3.1.0-rc.1: 2 B more on each build). It is fully minified, so its
75
74
  local names are shortened: that is what keeps an SDK that redacts what
76
- it records, in the browser, under its budget: 40 KiB, and 41,316 B for
75
+ it records, in the browser, within its budget: 40 KiB, and 41,316 B for
77
76
  the CommonJS build, set on 2026-11-11 as 40 KiB plus the 356 B its
78
- format cost then (as built now, the format costs 226 B). Body
77
+ format cost then (as built now, the format costs 196 B). Body
79
78
  redaction is not in it: it runs in
80
79
  a Web Worker, `dist/body-worker.js` (9.3 kB gzipped), which your bundler
81
80
  emits beside your app and the SDK starts when a capture that redacts
82
81
  bodies starts, in the page's next idle period; its main-thread copy,
83
- `dist/body-redact.mjs` (9.4 kB), is a chunk `init()` asks for at once
82
+ `dist/body-redact.mjs` (9.7 kB gzipped), is a chunk `init()` asks for at once
84
83
  for such a capture, beside rrweb, while your page's network is recorded
85
- from the start (see [Your page's own network](#your-pages-own-network)).
84
+ from the moment `init()` is called (see [Your page's own network](#your-pages-own-network)).
86
85
  A capture that masks bodies whole (`privacy.maskNetworkBodies`, which
87
86
  `privacyMode: "strict"` sets) loads neither. The source maps ship
88
87
  beside it as separate files, `dist/index.mjs.map`,
@@ -185,7 +184,7 @@ SDK uses when you leave them out:
185
184
  | option | default | what it does |
186
185
  | --------------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
187
186
  | `privacyMode` | `"balanced"` | `"balanced"` masks every typed input value; `"strict"`, for public sites, records no page text -- it also masks every visible text node, blanks every attribute, `<meta>` content and comment that holds text, records no request or response body and replaces every query value in a URL; `"off"` masks only password and hidden fields — see [Privacy](#privacy) |
188
- | `sampleRate` | `1` | fraction of sessions recorded, decided once when a session starts. `0.1` records one visitor in ten; the rest never start |
187
+ | `sampleRate` | `1` | fraction of captures recorded, rolled when `init()` starts one: `0.1` records one in ten; the rest never start. A later `init()` returns the same capture, sampled out or not, and rolls again only after `stop()` or the session's end. Under `continuous`, each session after a recorded one is recorded too, with no new roll |
189
188
  | `networkCapture` | `true` | record `fetch` and `XMLHttpRequest` exchanges. `false` records the DOM only |
190
189
  | `maxSessionDuration` | `600000` | milliseconds after which a session ends on its own (10 minutes): recording stops, the queue is flushed, and a later `init()` starts a new session -- or, with `continuous`, the next one starts by itself |
191
190
  | `continuous` | `false` | keep capturing for the life of the page, one session at a time: a session that ends (at `maxSessionDuration`, or because the server completed or filled it) is followed by a new one at the visitor's next click, tap or key press, so an idle tab posts nothing. Has no effect with a `sessionIdGenerator`: that id is yours, and a second session under it would be refused — see [Single-page apps](#single-page-apps) |
@@ -197,7 +196,7 @@ SDK uses when you leave them out:
197
196
  | `unmaskSelectors` | — | CSS selectors whose text is left in the clear, overriding `maskSelectors` and `"strict"` for the subtree below them. It cannot unmask an input value |
198
197
  | `privacy` | — | a `Partial<PrivacyConfig>`: `maskSelectors`, `unmaskSelectors`, `blockSelectors`, `maskNetworkBodies`, `sensitiveHeaders`, `piiPatterns`, `secretRoutes`, `keepRoutes` — see [Privacy](#privacy) |
199
198
  | `sessionIdGenerator` | — | supply your own session id instead of the SDK's UUID v4, to correlate with your own systems. It must be a string of 1-200 characters of letters, digits, dot, dash and underscore with no empty dot-separated segment (`tenant-7.user_42.run-3`; encode a `user:42/session 1` first) — the SDK checks what the generator produces before sending and refuses to start on anything else, a throw included (`KUI_E_SESSION_ID_INVALID`). Two live sessions with one id are refused by the server (a terminal `409`) |
200
- | `honorPrivacySignals` | `true` | when the browser sends Do Not Track (`navigator.doNotTrack === "1"`) or Global Privacy Control (`navigator.globalPrivacyControl === true`), `init()` records, patches and sends nothing and reports `KUI_E_CAPTURE_DECLINED` to `onError` on every declined `init()` and by `console.info` once per page. `false` records regardless — only where the site has its own lawful basis and says so in its privacy policy |
199
+ | `honorPrivacySignals` | `true` | when the browser sends Do Not Track (`navigator.doNotTrack === "1"`) or Global Privacy Control (`navigator.globalPrivacyControl === true`), `init()` records, patches and sends nothing and reports `KUI_E_CAPTURE_DECLINED` to `onError` a task later, paced, for every declined `init()` but one made while 16 wait (see `onError` below), and by `console.info` once per page. `false` records regardless — only where the site has its own lawful basis and says so in its privacy policy |
201
200
  | `onError` | — | called once when a terminal condition ends the capture — see [When the server rejects a batch](#when-the-server-rejects-a-batch) |
202
201
 
203
202
  After `init`, the static surface is:
@@ -207,6 +206,7 @@ KeployCapture.pause(); // keep the session, record nothing more
207
206
  KeployCapture.resume();
208
207
  KeployCapture.getSessionId(); // "" when no session is live
209
208
  KeployCapture.getCaptureIdentity(); // the browser half of the backend join, or why there is none
209
+ KeployCapture.getInstance(); // the capture, sampled out too; null before init(), after stop(), a declined or refused init(), between sessions
210
210
  KeployCapture.stop(); // stop recording and send what is queued
211
211
  ```
212
212
 
@@ -413,20 +413,25 @@ then, with what had arrived, cut and marked `...[truncated]` as when the
413
413
  page leaves, and nothing of it after, `resume()` included; the page's
414
414
  own read of it goes on untouched.
415
415
 
416
- What network capture **is** gated on is the rrweb import. `init()`
417
- awaits `import("rrweb")` before it patches `fetch` and
418
- `XMLHttpRequest`, and in your bundle that is normally a separate chunk
419
- fetched over the network. Until the import resolves, `fetch` and
420
- `XMLHttpRequest` are still the browser's own: a request the page makes
421
- in that window is not recorded at all, in a hidden tab exactly as in a
422
- visible one. Calling `init()` as early in startup as you can narrows
423
- that window; nothing closes it.
424
-
425
- An `init()` called while the browser is offline is the exception: it
426
- does not ask for rrweb until the browser is back online (a browser keeps
427
- a failed module import for the life of the page), patches `fetch` and
428
- `XMLHttpRequest` at once, and sends nothing it records until rrweb has
429
- loaded -- if it cannot, the capture is refused with
416
+ Network capture starts synchronously inside `init()` -- once privacy
417
+ signals, route declarations, sampling and `sessionIdGenerator` have passed
418
+ -- before `import("rrweb")` (and under `strict`, `strict-attributes`)
419
+ resolves. `fetch` and `XMLHttpRequest` are patched immediately so requests
420
+ your page fires right after an unawaited `init()` are recorded while rrweb
421
+ loads, and the transport is held so nothing leaves the browser until rrweb
422
+ has loaded. Once rrweb loads, held events are delivered in the same session
423
+ alongside the initial DOM snapshot. If rrweb (or under `strict`,
424
+ `strict-attributes`) cannot be loaded, or if `stop()` is called mid-import,
425
+ `fetch` and `XMLHttpRequest` are restored synchronously and all held events
426
+ and bodies are dropped unsent. Requests made *before* `init()` is called
427
+ are still outside the SDK's reach, so call `init()` as early in startup as
428
+ you can.
429
+
430
+ An `init()` called while the browser is offline uses the same synchronous
431
+ patch-and-hold path: it does not ask for rrweb until the browser is back
432
+ online (a browser keeps a failed module import for the life of the page),
433
+ patches `fetch` and `XMLHttpRequest` at once, and holds what it records
434
+ until rrweb has loaded -- if it cannot, the capture is refused with
430
435
  `KUI_E_DOM_RECORDER_UNAVAILABLE` and what was recorded meanwhile is
431
436
  dropped. That the browser is back is heard as the `online` event or
432
437
  read, once a second while the capture waits, from `navigator.onLine`: a
@@ -511,11 +516,15 @@ value only when its name is the page's structure -- `id`, `class`,
511
516
  URL (query values replaced), CSS, a `srcset`, a `srcdoc` (its text and
512
517
  attributes masked the same way, its scripts included). Every other
513
518
  value is recorded as `""`: `title`, `alt`, `placeholder`, `aria-label`
514
- and the other `aria-*` text, every `data-*` but `data-testid`, every
515
- `value` rrweb records in clear (a field's that rrweb masked keeps its
516
- asterisks), a framework's state (`wire:snapshot`, `x-data`, `hx-vals`,
517
- an Astro island's `props`), a custom element's attributes but its
518
- global ones (`id`, `class`, `slot`, ARIA state), an input's `min`,
519
+ and the other `aria-*` text, every `data-*` but `data-testid`,
520
+ `data-src`, `data-href` and `data-srcset` (a lazy loader's URLs:
521
+ `data-srcset` read by the `srcset` rule, and `data-src` and `data-href`
522
+ by the URL rule when the value can be a URL -- no space, quote or brace;
523
+ text in one is blanked), every `value` rrweb records in clear (a field's
524
+ that rrweb masked keeps its asterisks), a framework's state
525
+ (`wire:snapshot`, `x-data`, `hx-vals`, an Astro island's `props`), a
526
+ custom element's attributes but its global ones (`id`, `class`, `slot`,
527
+ ARIA state), an input's `min`,
519
528
  `max` and `pattern` (a date range copies the start the visitor picked
520
529
  into the end's `min`; a confirm field copies a typed email into
521
530
  `pattern`) and `aria-valuenow` (a slider's chosen value). SVG geometry
@@ -560,8 +569,9 @@ custom element as on any other; text in one is blanked.
560
569
  What strict still records, so do not put a visitor's text there: URL
561
570
  PATHS (`/search/<term>`, a `#/search/<q>` route, a `<link
562
571
  rel="canonical">`; only query and fragment-pair values are replaced),
563
- `id`s and `class`es, `data-testid` (an app that builds one from user
564
- data, `row-${email}`, records it), a field's `name`, SVG geometry (a
572
+ `id`s and `class`es, `data-testid` (kept as written because the replay
573
+ finds elements by it; an app that builds one from user data,
574
+ `row-${email}`, records it), a field's `name`, SVG geometry (a
565
575
  signature pad's strokes are its `d`) and text under `unmaskSelectors`. A comment
566
576
  outside `<html>` that changes after the snapshot is recorded as rrweb
567
577
  records it. And the replay's look pays for it: CSS keyed on a `data-*`
@@ -601,7 +611,8 @@ count are `navigator.doNotTrack === "1"` -- or as older browsers spell
601
611
  it: `"yes"` (Firefox before 32), `window.doNotTrack === "1"` (Safari,
602
612
  IE 11), `navigator.msDoNotTrack === "1"` (IE 10) -- and
603
613
  `navigator.globalPrivacyControl === true`; `onError` hears every
604
- declined `init()`, and the console line is written once per page.
614
+ declined `init()` but one made while 16 wait for their hand-over (it
615
+ resolves at once, untold), and the console line is written once per page.
605
616
 
606
617
  **On the network.** For every recorded exchange:
607
618
 
@@ -841,18 +852,16 @@ expected, actual}`, a header list `[{name: "Set-Cookie", value}]` —
841
852
  passed in `fetch()`'s `init` or to `send()` is decided from its kind
842
853
  and size, never serialised or decoded (only a string one is scanned,
843
854
  for its `requestTerm`, below), and the body chunk is never loaded.
844
- The browser still makes a body passed in `fetch()`'s `init` the body
845
- of the `Request` the SDK builds from the call for its URL and headers,
846
- so it byte-copies a buffer, UTF-8 encodes a string or URLSearchParams
847
- and multipart-encodes a FormData once more than the page's own
848
- `fetch()` does (a Blob or a stream is taken as it is); the SDK reads
849
- none of it. One body is still read: a `Request` object's own
855
+ One body is still read: a `Request` object's own
850
856
  (`fetch(new Request(url, { body }))`, as ky and axios's fetch adapter
851
- send one) is copied and read for its `requestTerm` (below), only as
852
- far as the term needs -- whole when it starts as a JSON object, to its
853
- first character otherwise (where a `Request` has no body stream, as
854
- in Firefox, the copy is taken whole as a Blob and only its first KiB
855
- decoded), and a multipart one is parsed. A body that is absent keeps `null`: a
857
+ send one) is copied as the page calls `fetch()` and read for its
858
+ `requestTerm` (below) after that call returns, only as far as the
859
+ term needs -- whole when it starts as a JSON object, its first KiB
860
+ otherwise, however the browser hands the copy over (whole, as WebKit
861
+ does a body it holds in memory, in chunks of its own size, as
862
+ Chromium does, or, where a `Request` has no body stream, as in
863
+ Firefox, whole as a Blob), and a
864
+ multipart one is parsed. A body that is absent keeps `null`: a
856
865
  request sent with none or an empty one (but an XHR's Blob, which is
857
866
  named however small), and a response to a HEAD, a 204, 205 or 304,
858
867
  or one whose `Content-Length` is 0. A response that does not declare
@@ -868,7 +877,27 @@ expected, actual}`, a header list `[{name: "Set-Cookie", value}]` —
868
877
  a body that is no GraphQL, or `body:*` for one the SDK could not read
869
878
  or hash. It is computed over the body before redaction and never holds
870
879
  a variable or a literal: it is what replay tells GraphQL calls apart by
871
- (spec/canonical-key.v2.json).
880
+ (spec/canonical-key.v2.json). A `Request` object's own body
881
+ (`fetch(new Request(url, { body }))`) is read for it in a microtask
882
+ queued right after the page's `fetch()` call returns, in every mode:
883
+ never inside the call, and never behind a timer, which a hidden tab
884
+ throttles. One whose `body` is `null` is recorded with none (`null`,
885
+ `""`) at once, whatever its `Content-Type`. Keploy's hosted service
886
+ does not use the term yet: it keys the flows it stores, and so the
887
+ beta tests that replay them, by canonical-key.v1
888
+ (spec/canonical-key.v1.json), with no term: GraphQL calls of one
889
+ method and URL are answered in the order they were recorded, whatever
890
+ operation each runs, and a redacted query value (under strict, every
891
+ one with a value) that v1 keeps in its key stays the literal
892
+ `[REDACTED]` there, matching only itself, never the live value (below).
893
+ - In every mode, the SDK copies no body passed in `fetch()`'s `init` to
894
+ record the call: the `Request` it builds for the call's URL, method
895
+ and headers is given an empty body of the same kind when `init` is a
896
+ plain object (each of its own members kept as it is), so the browser
897
+ copies or encodes nothing the page's own `fetch()` does not. Any other
898
+ `init` (a class's, or another realm's, as from an iframe), and a body
899
+ of another realm (a `FormData`, `URLSearchParams` or buffer from an
900
+ iframe), is passed as it is, and copied once more as before.
872
901
  - One rule reads every URL the SDK records: a request's URL and every
873
902
  URL its request and response headers carry (`Location`,
874
903
  `Content-Location`, `Link` and `Refresh` -- its delay first or not --
@@ -913,8 +942,8 @@ expected, actual}`, a header list `[{name: "Set-Cookie", value}]` —
913
942
  is recorded as `/search?q=[REDACTED]&page=[REDACTED]`; an empty value
914
943
  and a bare name (`?flag`, recorded as written, whatever it holds) are
915
944
  kept. Replaying such a request needs a replay key that matches a value
916
- redacted at record against any live value (canonical-key.v2, not yet
917
- specified in spec/); with
945
+ redacted at record against any live value (canonical-key.v2,
946
+ spec/canonical-key.v2.json); with
918
947
  it, two requests that differ only in a redacted value
919
948
  (`?session=morning` and `?session=evening`, two `?pin=`s) are one
920
949
  request to the replay, answered in the order they were recorded.
@@ -1280,8 +1309,9 @@ expected, actual}`, a header list `[{name: "Set-Cookie", value}]` —
1280
1309
  URL is kept; a URL with
1281
1310
  nothing to scrub is recorded exactly as it was, and an event the scrub
1282
1311
  cannot read is dropped rather than sent, as one the credential scrub
1283
- cannot read is. A test holds a 50,000-node page with 20,000 such URLs
1284
- under a second.
1312
+ cannot read is. A unit test scrubs a synthetic tree of 50,000 elements
1313
+ holding 30,000 URLs, 20,000 of them with a query, in under a second of
1314
+ CPU time in Node: a bound the test holds, not a measurement on a page.
1285
1315
  - The page's TEXT -- each text node of the full snapshot and of a node
1286
1316
  added after it, each text change, a value typed under `off`, a
1287
1317
  `srcdoc`'s markup -- has every JWT and provider-prefixed key in it
@@ -1356,22 +1386,65 @@ records each body `[MASKED]`, never in the clear. Changed in this version: it us
1356
1386
  With network capture on, `fetch()` resolves when the response's headers
1357
1387
  arrive, exactly as without the SDK, and the body reaches the page as the
1358
1388
  network delivers it: a streamed response -- SSE over `fetch`, NDJSON, an
1359
- LLM's tokens, a live log view read with `getReader()` -- streams. The SDK
1360
- reads each body once (none when bodies are masked whole), as its only
1361
- reader, and hands every chunk on to
1362
- the page; it reads ahead of the page only until it has the recording
1363
- (the whole body, or `maxBodySize` of it), so a body the page reads slowly
1364
- is not buffered for it, and a page that cancels (`reader.cancel()`, a
1365
- `for await` `break`, an aborted pipe) or aborts (`AbortSignal`) stops
1366
- the download at once. A body the page never reads is still recorded. A
1367
- page that stops reading before the body has ended (and before
1368
- `maxBodySize`) is recorded with exactly what it read; once the SDK has
1369
- the whole body, the whole body is what is recorded. A body that fails
1370
- part-way -- an abort, a dropped connection, a navigation (Firefox and
1371
- WebKit abort a page's fetches as it leaves) -- is recorded with what had
1372
- arrived, cut and marked `...[truncated]`; the SDK may have read that far
1373
- ahead of the page. Each of these, whole or cut, is redacted exactly as
1374
- any other recorded body is.
1389
+ LLM's tokens, a live log view read with `getReader()` -- streams. The
1390
+ page gets the network's own `Response`, and the SDK reads nothing until
1391
+ the page reads (none when bodies are masked whole):
1392
+
1393
+ - read whole -- `text()`, `json()`, `arrayBuffer()`, `bytes()`,
1394
+ `blob()` -- the browser's own method answers (`json()` is its
1395
+ `text()`, parsed by `JSON.parse`). The SDK records what your app gets:
1396
+ the text or bytes before your app's own code after the read runs, a
1397
+ `Blob` read back from that same `Blob`. Bytes are decoded only as far
1398
+ as `maxBodySize` needs. `formData()`, whose result is not the body's
1399
+ text, and a `json()` your app put on `Response.prototype` (a reviver,
1400
+ a lossless parser), which answers as your app's own, are recorded from
1401
+ a clone taken just before, read only as far as `maxBodySize` needs and
1402
+ then cancelled: the SDK holds no more of the body than that while your
1403
+ app reads the rest. A clone's read (`res.clone().text()`, as ky does)
1404
+ records the same way; the first read to complete is the recording. No
1405
+ JavaScript stream reader touches the body your app reads, so the
1406
+ request ends as it does without the SDK: in Chromium a load whose body
1407
+ a stream reader takes to its end before the load completes is
1408
+ reported `net::ERR_ABORTED`, though the page has every byte.
1409
+ - read through `body` -- the SDK reads the network once for each read
1410
+ of the page's, nothing ahead, so a body the page reads slowly is not
1411
+ buffered for it, and a page that cancels (`reader.cancel()`, a
1412
+ `for await` `break`, an aborted pipe) or aborts (`AbortSignal`) stops
1413
+ the download at once. A page that stops reading before the body has
1414
+ ended (and before `maxBodySize`) is recorded with exactly what it
1415
+ read. A clone's `body` is the browser's own, never relayed nor
1416
+ recorded: monitoring tools (Datadog RUM, Sentry) read a clone of every
1417
+ response through its stream, and the recording stays your app's own
1418
+ read. A body your app reads only through a clone's `body` is recorded
1419
+ as not read. This relay is part of the body code's chunk, which
1420
+ `init()` asks for at once: a `body` your app takes before that chunk
1421
+ has arrived (or while the page is leaving) is the browser's own
1422
+ stream, handed over at once -- the SDK never makes a read wait for a
1423
+ chunk. Taking it records nothing: an app that only looks at `body`
1424
+ and then reads whole (ofetch, Nuxt's `$fetch`: `response.body ||`,
1425
+ then `text()`) is recorded as ever. One that reads nothing of it whole
1426
+ is recorded with its body as not captured (below, with `N` 0, the SDK
1427
+ having seen none of it, and its own reason, `early`). `body` is the
1428
+ stream the browser has for the response at each look: after `clone()`
1429
+ it is the new one, as without the SDK. Bodies read whole need no chunk.
1430
+ - never read -- the SDK never reads it either. It is recorded as
1431
+ `[Response body not read by the page]`, with its status and headers,
1432
+ once the page's `Response`, its clones and its `body` stream are
1433
+ collected (Chromium aborts that load then), or when the tab is hidden,
1434
+ the page leaves, the capture pauses or `stop()` ends; replay sends
1435
+ that text as the body. A tab may be discarded once hidden (mobile
1436
+ browsers do) with no further event, so a body first read after the
1437
+ tab is shown again stays recorded as not read. A body a browser API
1438
+ reads past JavaScript (`cache.put(req, res)`,
1439
+ `WebAssembly.instantiateStreaming(res)`) is read by the browser
1440
+ itself, untouched, and recorded as not read too. Changed in this
1441
+ version: such a body used to be read and recorded whole.
1442
+
1443
+ A body that fails part-way -- an abort, a dropped connection, a
1444
+ navigation (Firefox and WebKit abort a page's fetches as it leaves) --
1445
+ is recorded with what had arrived, cut and marked `...[truncated]`
1446
+ (nothing of it, for a body the page was reading whole). Each of these,
1447
+ whole or cut, is redacted exactly as any other recorded body is.
1375
1448
 
1376
1449
  The page has its body first. The SDK hands each body to a Web Worker,
1377
1450
  which redacts it off your page's main thread. It does so in a task of
@@ -1388,7 +1461,32 @@ run of large POSTs costs it no redaction, and nothing the SDK does can
1388
1461
  interleave with a page's `text()`. MEASURED before the page-first
1389
1462
  change: 50 parallel 64 KB JSON fetches took 32 ms without the SDK and
1390
1463
  382 ms with it in Chromium. The exchange keeps its time and its place
1391
- in the recording. The page leaving and `stop()`'s one-second bound
1464
+ in the recording. What the SDK holds for redaction is bounded, across
1465
+ every worker the page makes, from the moment it takes a body: waiting for
1466
+ its idle task, for the worker's start, for the body chunk on a page with no
1467
+ worker, or for the worker's answer. Each copy counts -- the SDK's own, and
1468
+ the one posted to the worker, which the browser keeps until the worker
1469
+ answers -- with 1,024 UTF-16 units more each. The SDK takes an exchange
1470
+ only while the copies come to under 8 Mi units (16 MiB of strings,
1471
+ beside the 16 MiB cap on the events it holds unsent): with one body of the
1472
+ default `maxBodySize` each, 8 exchanges posted to the worker, or 16 waiting
1473
+ for its start or their idle task. Its own copies come to at most 8 Mi units
1474
+ and one exchange, and with the copies posted to the worker at most twice
1475
+ that, 16 Mi units and two exchanges; they come nearest that ceiling when
1476
+ everything the SDK holds is posted before the worker answers (for example
1477
+ the exchanges that waited for its start, as it lands). An exchange that
1478
+ comes once the copies are at 8 Mi units or more -- a burst before your
1479
+ page is idle, a chunk or the
1480
+ worker's script not yet arrived, or a worker behind your page -- leaves
1481
+ none of its bodies in the SDK's events or jobs: the SDK drops the
1482
+ exchange's job as it comes, and records it in its own task, as any
1483
+ exchange, with the cut note and nothing kept,
1484
+ `[Body cut: 0 of N characters kept, unverified]`,
1485
+ which replay never serves as a body, with no redaction and nothing logged.
1486
+ So a healthy page can lose bodies this way: with the default `maxBodySize`,
1487
+ past 16 exchanges in a burst before it is idle, or past 8 a slower worker
1488
+ has not answered. An exchange is taken whatever its size when nothing else
1489
+ is held. The page leaving and `stop()`'s one-second bound
1392
1490
  record at once every body still in the worker, on the main thread up to
1393
1491
  64 KiB in all and cut past it -- with the main thread's chunk, which
1394
1492
  `init()` asks for at once, beside rrweb, for a capture that redacts
@@ -1409,17 +1507,31 @@ each body the main thread must redact the same way, with its own reason
1409
1507
  [Body not captured: the redaction code could not load]
1410
1508
  ```
1411
1509
 
1510
+ A body your app took through `res.body` before the body code had loaded,
1511
+ and that nothing read whole (see above), says so, with `N` 0:
1512
+
1513
+ ```
1514
+ [Body cut: 0 of 0 characters kept, unverified]
1515
+ [Body not captured: the page took the body's stream before the redaction code had loaded]
1516
+ ```
1517
+
1412
1518
  (`notCapturedBodyNote(N, why)` in `@keploy/contract/privacy`, `why`
1413
- being `"left"` or `"code-failed"`, and `isNotCapturedBody(body, why?)`,
1414
- which tells either from a body cut at the budget, and one cause from the
1415
- other). Replay never serves it, as any cut body.
1519
+ being `"left"`, `"code-failed"` or `"early"`, and
1520
+ `isNotCapturedBody(body, why?)`, which tells each from a body cut at the
1521
+ budget, and one cause from another). Replay never serves it, as any cut
1522
+ body.
1416
1523
 
1417
1524
  A page that is leaving starts none of the SDK's own loads (its chunks,
1418
1525
  rrweb's import and the body worker), and a load its leave refused or
1419
1526
  cancelled is held, not judged failed: a body the page leaves on is
1420
1527
  recorded with the first reason, and nothing is logged. A single-page
1421
- app's route the Navigation API intercepted is no leave (the SDK reads
1422
- `navigation.transition` a task after the `navigate`). The leave ends only
1528
+ app's route the Navigation API intercepted is no leave: a task after
1529
+ each `navigate`, the SDK judges it by what became of it in this
1530
+ document -- the app cancelled it, it committed here
1531
+ (`currententrychange`; an intercepted navigate commits as its dispatch
1532
+ ends, before its handler runs), or its transition is still under way (a
1533
+ commit the app deferred) -- never by `navigation.transition` alone,
1534
+ which a route whose handler has already settled no longer has. The leave ends only
1423
1535
  on something a navigation still on its way cannot produce: Chromium's
1424
1536
  abort of the leaving navigation when no navigate came after it (an
1425
1537
  "unsaved changes" prompt the visitor cancelled; a link clicked twice,
@@ -1487,24 +1599,13 @@ and `stop()` record at once what fits in that budget and leave the rest
1487
1599
  to the worker; `pause()` cuts a body still arriving and records it when
1488
1600
  the worker answers.
1489
1601
 
1490
- The Response your page gets is one the SDK built around that body
1491
- (unless bodies are masked whole, or it answers a HEAD or is a 204,
1492
- 205 or 304, which have no body: then it is the network's own, and
1493
- none of what follows applies). Its
1494
- `url`, `redirected`, `type`, `headers` and `clone()` answer as the
1495
- network's do, and `instanceof Response`, `bodyUsed` and BYOB readers
1496
- behave as usual. Three things differ:
1497
-
1498
- - A **Cache API** round trip (`cache.put()`, then `cache.match()`) keeps
1499
- the body, status and headers but not the network's `url` and `type`:
1500
- they come back `""` and `"default"`. That is the platform's limit, not
1501
- a choice: the Cache API stores a Response's internal URL list and type,
1502
- which a Response built in JavaScript cannot be given, and a clone --
1503
- the only way to keep the network's own object -- cannot keep the
1504
- page's backpressure in Chromium. MEASURED in Chromium, the same page
1505
- with and without the SDK: every other member equal, these two not.
1506
- `WebAssembly.instantiateStreaming()` likewise does not see the
1507
- response's URL (it still compiles).
1602
+ The Response your page gets is the network's own: its `url`,
1603
+ `redirected`, `type`, `headers`, `instanceof Response`, `bodyUsed` and
1604
+ BYOB readers are the browser's, and a Cache API round trip stores the
1605
+ network's response exactly as without the SDK. The SDK answers its body
1606
+ readers, `body` and `clone()` with non-enumerable members of its own on
1607
+ that one object. One thing differs:
1608
+
1508
1609
  - A **streamed upload built into a `Request`** (`new Request(url, { body:
1509
1610
  stream, duplex: "half" })`) that the server answers early is pulled
1510
1611
  further than without the SDK -- up to the chunk that crosses
@@ -1522,18 +1623,13 @@ stream, duplex: "half" })`) that the server answers early is pulled
1522
1623
  without the SDK, 10 with it at the default cap. A stream passed as
1523
1624
  `init.body` is not read at all (recorded `[ReadableStream]`) and is
1524
1625
  pulled exactly as without the SDK; only Chromium streams request bodies.
1525
- - A **`fetch` wrapper installed before the SDK** that reads or clones the
1526
- network's Response later than the moment it hands it on finds its body
1527
- already taken by the SDK. Wrappers that clone at once (most APM and
1528
- error-tracking SDKs) are unaffected; initialise this SDK first if
1529
- yours does not.
1530
-
1531
- `KeployCapture.stop()` waits up to one second for exchanges already
1532
- answered to finish their bodies, and records any still open then (an
1533
- endless stream) cut at that point and marked `...[truncated]` -- as it
1534
- does for exchanges still open when the page leaves. `KeployCapture.pause()`
1535
- cuts them the same way at once, with no wait: a pause records nothing
1536
- more.
1626
+ `KeployCapture.stop()` waits up to one second for exchanges already
1627
+ answered to finish their bodies, and records any still open then (an
1628
+ endless stream) cut at that point and marked `...[truncated]` -- as it
1629
+ does for exchanges still open when the page leaves; one whose body the
1630
+ page has not read is recorded as `[Response body not read by the page]`.
1631
+ `KeployCapture.pause()` records them the same way at once, with no wait:
1632
+ a pause records nothing more.
1537
1633
 
1538
1634
  ## What a large body looks like
1539
1635
 
@@ -1561,7 +1657,7 @@ Over the bound, there are three answers, not one:
1561
1657
  a size it is the figure the SDK had, not always a measurement —
1562
1658
  `[Response body too large: N bytes]` repeats the server's
1563
1659
  `Content-Length` for a body it never downloaded. So a note is not a
1564
- way to size a body against `maxBodySize`. There are seven, one for
1660
+ way to size a body against `maxBodySize`. There are eight, one for
1565
1661
  each way a body can be one the SDK does not read:
1566
1662
  - `[Blob: N bytes, truncated]` — a `fetch` Blob over the bound.
1567
1663
  - `[Blob: N bytes]` — any Blob sent through `XMLHttpRequest`, which
@@ -1572,6 +1668,10 @@ Over the bound, there are three answers, not one:
1572
1668
  text.
1573
1669
  - `[Response body too large: N bytes]` — from a `Content-Length` over
1574
1670
  the bound, so the body is never downloaded.
1671
+ - `[Response body not read by the page]` — a response body your app
1672
+ never read, so the SDK never reads it either (see "Your page's own
1673
+ network"). Replay sends this text as the body, with the recorded
1674
+ status and headers. No size: nothing of it was read.
1575
1675
  - `[ReadableStream]` — a streaming request body, which reading would
1576
1676
  consume out from under your app. No size: measuring a stream means
1577
1677
  consuming it too.
@@ -1596,7 +1696,9 @@ characters kept, unverified]`. The same note marks a body still in the
1596
1696
  worker when the page leaves, past the 64 KiB the page's last moment
1597
1697
  redacts (0 kept when nothing was left), and, with nothing kept and a
1598
1698
  second line saying why, a body the SDK did not capture: the page left
1599
- before the body chunk had arrived, or the chunk could not load (see
1699
+ before the body chunk had arrived, the chunk could not load, or the
1700
+ page took the body's stream before the chunk had arrived and nothing
1701
+ read it whole (see
1600
1702
  [Your page's own network](#your-pages-own-network)).
1601
1703
  It is not the response the server sent, and replay never serves it as
1602
1704
  one: the request is answered as a failed one and a test that reaches it
@@ -1880,30 +1982,60 @@ codes and their evidence are the entries of the same name in
1880
1982
  `@keploy/contract`'s error corpus. The SDK carries each entry's code, a
1881
1983
  short summary of what happened and the link to the entry's page, and
1882
1984
  no remedy text, which stays on that page; a test holds the summary, the
1883
- link and the evidence to the corpus. The `docs` pages are published
1884
- with the same docs release as the guide above, and answer 404 until
1885
- then. The status-to-code mapping is
1886
- `spec/sdk-errors.v4.json`; the session-id rule is
1985
+ link and the evidence to the corpus. The status-to-code mapping is
1986
+ `spec/sdk-errors.v5.json`; the session-id rule is
1887
1987
  `spec/session-id.v1.json`, the api-server's rule stated once — the
1888
1988
  SDK and the sidecar execute the file today, and the api-server's own
1889
1989
  executor lands with its next vendoring of the corpus. `onError` runs
1890
1990
  after the capture has ended and the singleton is released, so a handler
1891
- may call `init()` again and get a fresh capture:
1991
+ may call `init()` again and get a fresh capture -- once it has changed
1992
+ what was refused. A declined visitor, a conflicting config and an
1993
+ invalid route are refused again by the next `init()` given the same
1994
+ config; a session-id generator runs again on each `init()`, and a chunk
1995
+ that failed to load is asked for again (a browser may keep the failure
1996
+ until the page is reloaded), so each is refused again until what failed
1997
+ has changed. A refusal raised while `init()` runs
1998
+ reaches `onError` in a task of its own (a `setTimeout`), never inside that
1999
+ `init()` call, and `init()` resolves once the handler has run. A
2000
+ refused `init()` is told, in the order the refusals of its code came, but
2001
+ repeated identical refusals are paced and bounded: a code has at most one
2002
+ hand-over waiting, which tells the refusals of that code made while it
2003
+ waited, at most 16 of them -- a refusal that comes while 16 wait resolves
2004
+ its `init()` at once without calling `onError`, and nothing of it is
2005
+ kept; a hand-over that follows the code's last one closely waits twice as
2006
+ long as the one before, from 50 ms up to 30 s, so no refused `init()` is
2007
+ scheduled more than 30 s out, however often the page calls it; and a code
2008
+ quiet for that wait plus a second is handed over in the next task again.
2009
+ So a handler that re-inits the same config for every code, even twice for
2010
+ each refusal, neither recurses, freezes nor loads the page: it goes round
2011
+ 11 times in its first minute, then twice a minute, at most 16 handlers a
2012
+ round. A page that clears timers it does not own, or a test that leaves
2013
+ fake timers, can drop a hand-over's timer, so each refusal reads the
2014
+ clock once (`performance.now()`): the next refusal of that code more
2015
+ than 31 s past its due time hands over what waits in the next task, and
2016
+ until one comes what waits stays unresolved. The
2017
+ console line of a refusal raised while `init()` runs is
2018
+ written once per page per code. Re-init only after changing what was
2019
+ refused:
1892
2020
 
1893
2021
  ```ts
1894
- import { KeployCapture } from "@keploy/capture-sdk";
2022
+ import { KeployCapture, type CaptureConfig } from "@keploy/capture-sdk";
1895
2023
 
1896
- await KeployCapture.init({
2024
+ const config: CaptureConfig = {
1897
2025
  endpoint,
1898
2026
  apiKey,
2027
+ sessionIdGenerator: mySessionId,
1899
2028
  onError: (err) => {
1900
2029
  // The person using the page said no (Do Not Track or Global Privacy
1901
2030
  // Control). Not a failure: keep it out of your error tracker --
1902
2031
  // Brave and DuckDuckGo send GPC by default.
1903
2032
  if (err.code === "KUI_E_CAPTURE_DECLINED") return;
1904
- if (err.code === "KUI_E_SESSION_ID_INVALID")
1905
- fixGenerator(err.evidence.received);
1906
- else if (
2033
+ if (err.code === "KUI_E_SESSION_ID_INVALID") {
2034
+ report(err.code, err.evidence.received);
2035
+ // Changed what was refused, so a re-init can succeed: the SDK's ids.
2036
+ delete config.sessionIdGenerator;
2037
+ void KeployCapture.init(config);
2038
+ } else if (
1907
2039
  err.code === "KUI_E_DOM_RECORDER_UNAVAILABLE" ||
1908
2040
  err.code === "KUI_E_STRICT_ATTRIBUTES_UNAVAILABLE"
1909
2041
  )
@@ -1916,7 +2048,8 @@ await KeployCapture.init({
1916
2048
  else
1917
2049
  report(err.code, err.serverMessage, err.evidence.issues, err.remedy.docs);
1918
2050
  },
1919
- });
2051
+ };
2052
+ await KeployCapture.init(config);
1920
2053
  ```
1921
2054
 
1922
2055
  A `429`, a `5xx` or a network failure gets three attempts: up to 1 s
@@ -1941,7 +2074,9 @@ is dropped (a batch that failed and goes back to the queue past
1941
2074
  record time and replay time cannot disagree.
1942
2075
  - **No `Math.random` fallback for the session nonce**, so a recording
1943
2076
  is either joinable or says why it is not.
1944
- - **No sampling by user.** `sampleRate` is decided per session start.
2077
+ - **No sampling by user.** `sampleRate` is rolled when `init()` starts a
2078
+ capture, so once per page load (again only after `stop()` or the
2079
+ session's end), never per visitor across loads.
1945
2080
  - **No guessing which generic field holds a secret.** A name/value pair
1946
2081
  is read by its `key`, `name` or `header`; a pair spelled otherwise
1947
2082
  (`{field, val}`), or one whose name is not a secret's own (an
@@ -1953,6 +2088,8 @@ is dropped (a batch that failed and goes back to the queue past
1953
2088
 
1954
2089
  - [UI Capture guide](https://keploy.io/docs/running-keploy/ui-capture/) — set up capture
1955
2090
  from the dashboard, the REST API or an AI agent, and read the flows
2091
+ - [Release notes (3.1.0)](https://keploy.io/docs/ui-capture/capture-sdk-3-1-0-release-notes/) —
2092
+ what changed from 3.0.1 and what to check before upgrading
1956
2093
  - [Keploy documentation](https://keploy.io/docs/)
1957
2094
  - [Keploy community Slack](https://join.slack.com/t/keploy/shared_invite/zt-3zcnuqfgl-WYK1NMhslVHsCtNcA1ULwA)
1958
2095
  - <support@keploy.io>