@keploy/capture-sdk 3.0.1 → 3.1.0-rc.2

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 (55) hide show
  1. package/README.md +1617 -130
  2. package/body-redact/package.json +6 -0
  3. package/body-worker-start/package.json +6 -0
  4. package/dist/body-jobs.d.ts +134 -0
  5. package/dist/body-redact.d.ts +23 -0
  6. package/dist/body-redact.js +1 -0
  7. package/dist/body-redact.js.map +1 -0
  8. package/dist/body-redact.mjs +1 -0
  9. package/dist/body-redact.mjs.map +1 -0
  10. package/dist/body-worker-none.d.ts +3 -0
  11. package/dist/body-worker-none.js +1 -0
  12. package/dist/body-worker-none.js.map +1 -0
  13. package/dist/body-worker-none.mjs +1 -0
  14. package/dist/body-worker-none.mjs.map +1 -0
  15. package/dist/body-worker-start.d.ts +3 -0
  16. package/dist/body-worker-start.js +1 -0
  17. package/dist/body-worker-start.js.map +1 -0
  18. package/dist/body-worker-start.mjs +1 -0
  19. package/dist/body-worker-start.mjs.map +1 -0
  20. package/dist/body-worker-unbundled.d.ts +1 -0
  21. package/dist/body-worker-unbundled.mjs +1 -0
  22. package/dist/body-worker-unbundled.mjs.map +1 -0
  23. package/dist/body-worker.d.ts +1 -0
  24. package/dist/body-worker.js +1 -0
  25. package/dist/body-worker.js.map +1 -0
  26. package/dist/errors.d.ts +224 -28
  27. package/dist/index.bundler.mjs +1 -0
  28. package/dist/index.bundler.mjs.map +1 -0
  29. package/dist/index.d.ts +23 -28
  30. package/dist/index.js +1 -2354
  31. package/dist/index.js.map +1 -0
  32. package/dist/index.mjs +1 -2297
  33. package/dist/index.mjs.map +1 -0
  34. package/dist/leave.d.ts +67 -0
  35. package/dist/module-of.d.ts +58 -0
  36. package/dist/network.d.ts +163 -4
  37. package/dist/privacy.d.ts +387 -12
  38. package/dist/recorder.d.ts +304 -64
  39. package/dist/redact.d.ts +48 -6
  40. package/dist/request-term.d.ts +23 -0
  41. package/dist/request-term.js +1 -0
  42. package/dist/request-term.js.map +1 -0
  43. package/dist/request-term.mjs +1 -0
  44. package/dist/request-term.mjs.map +1 -0
  45. package/dist/strict-attributes.d.ts +9 -0
  46. package/dist/strict-attributes.js +1 -0
  47. package/dist/strict-attributes.js.map +1 -0
  48. package/dist/strict-attributes.mjs +1 -0
  49. package/dist/strict-attributes.mjs.map +1 -0
  50. package/dist/term-candidate.d.ts +16 -0
  51. package/dist/transport.d.ts +269 -130
  52. package/dist/types.d.ts +152 -9
  53. package/package.json +37 -4
  54. package/request-term/package.json +6 -0
  55. package/strict-attributes/package.json +6 -0
@@ -0,0 +1,67 @@
1
+ /** HOLDING: the page is leaving, from the leave's first sign; no load starts. */
2
+ export declare const isHolding: () => boolean;
3
+ /** STANDING: the leave survived its check; what fails closed reads this. */
4
+ export declare const isStanding: () => boolean;
5
+ export declare const leaveMark: () => number;
6
+ /**
7
+ * Called as the page stays, before what was held is asked for, `cancelled`
8
+ * true after a leave that may have cancelled its loads (one a navigation
9
+ * began: not a navigate the app intercepted or cancelled): body-jobs.ts's
10
+ * worker not yet running, whose script's load the leave cancelled unsaid,
11
+ * and the running one it kept for the leave.
12
+ */
13
+ export declare const stays: Array<(cancelled: boolean) => void>;
14
+ /**
15
+ * The page stayed: the leave is over, and every load it held is asked for
16
+ * once more -- in a task of its own, never inside the visitor's input that
17
+ * proved it (a worker's start posts every body held), and held again if
18
+ * the page has begun to leave by then.
19
+ */
20
+ export declare function stayed(cancelled?: boolean): void;
21
+ /**
22
+ * `load`'s answer (its failure rejects), or undefined as the leave next
23
+ * holds a load: what awaits one awaits it no longer (recorder.ts's
24
+ * start(), so init() never hangs on a held load; network.ts's term). And
25
+ * undefined at once for a load not yet `answered` asked for while the page
26
+ * is leaving: held, or held as it is asked for, or cancelled as the page
27
+ * goes -- a leave that never ends (a 204, a download) tells nothing more,
28
+ * and each wait it kept, a request's or a session's, stayed for the
29
+ * page's life. Losing it fails closed.
30
+ */
31
+ export declare const unlessHeld: <T>(load: Promise<T>, answered?: boolean) => Promise<T | undefined>;
32
+ /**
33
+ * Run `run` once the leave has been judged -- it stands, or the page
34
+ * stayed -- when the page is HOLDING and not yet STANDING (the window up to
35
+ * the newest sign's check, a task after it): true when it waits. No clock.
36
+ */
37
+ export declare function whenJudged(run: () => void): boolean;
38
+ /** Hold `run` until the page has stayed; true when it waits (the page is leaving). */
39
+ export declare function whenStayed(run: () => void): boolean;
40
+ /**
41
+ * `get()`, a lazy load, through the leave gate: started only while the
42
+ * page is not leaving, and, refused or cancelled while it is, asked for
43
+ * once more when it has stayed. Rejects with a failure judged outside a
44
+ * leave: a real one.
45
+ */
46
+ export declare function staying<T>(get: () => T | PromiseLike<T>): Promise<T>;
47
+ /**
48
+ * Watch the page's leave (`on`, at a capture's start, which is itself
49
+ * proof that the page stayed unless the visitor's input started it:
50
+ * `proof`) or stop watching it (at the last capture's stop). A load still
51
+ * held then waits for the next capture's start.
52
+ */
53
+ export declare function watchLeave(on: boolean, proof?: boolean): void;
54
+ /** Exposed for tests. */
55
+ export declare const __leave: {
56
+ /** Forget the leave, what it held and the watch. */
57
+ reset(): void;
58
+ leaving: typeof isHolding;
59
+ /** The leave stands (isStanding). */
60
+ stands: typeof isStanding;
61
+ /** What waits for the leave's check (whenJudged). */
62
+ judging: () => number;
63
+ /** Loads held for the leave's end. */
64
+ held: () => number;
65
+ /** What awaits a load besides its answer (unlessHeld). */
66
+ waiting: () => number;
67
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The module an import() resolved to, whichever way the page's bundler
3
+ * hands it over. A bundler that takes the target as CommonJS gives an ES
4
+ * module that imports it only `{ default: module.exports }`: MEASURED with
5
+ * webpack 4.47.0 and babel-loader (Create React App 4's setup) on the
6
+ * packed SDK, rrweb and the request-term chunk both arrived so --
7
+ * `rrweb.record` was undefined and init() rejected with `TypeError: r is
8
+ * not a function`, and every GraphQL call was recorded with no term. The
9
+ * namespace is passed in whole, never read member by member at the
10
+ * import(): Parcel 2 checks each member read off an import()'s namespace
11
+ * against the module's exports, and neither module has a `default`.
12
+ */
13
+ export declare function moduleOf<T>(namespace: T): T;
14
+ /**
15
+ * One of the SDK's own chunks: `bare`, an import() of the package's own
16
+ * name -- what a bundler resolves (webpack 4 through the subpath's
17
+ * package.json) and an import map may map -- or, when that rejects,
18
+ * `beside`, an import() of the built file by its path beside
19
+ * dist/index.mjs. AN UNBUNDLED PAGE RESOLVES NO BARE NAME IT DOES NOT
20
+ * MAP: MEASURED on SDK be157850 with the journey-chain probe's pages (an
21
+ * import map naming `rrweb` alone, as enterprise-ui's e2e page and the
22
+ * README's no-bundler install have it), every response body was recorded
23
+ * `[Body cut: 0 of N characters kept, unverified]` in Chromium, Firefox
24
+ * and WebKit, privacy off included, where 3.1.0-rc.1, the redactor in its
25
+ * bundle, recorded all 7 whole (sdk-unbundled.spec.ts holds it). A
26
+ * relative import() is resolved against the module that makes it,
27
+ * dist/index.mjs wherever the page serves it from, so no entry is needed.
28
+ *
29
+ * ONLY dist/index.mjs HAS `beside` (`__BESIDE__`, scripts/build-main.mjs):
30
+ * the build a page with no bundler loads, and modern bundlers through the
31
+ * `exports` map, which bundle each path, as written, into the chunk the
32
+ * name loads, so no native import() reaches their output.
33
+ * dist/index.bundler.mjs (the `module` field: webpack 4, Parcel by
34
+ * default) and dist/index.js (CommonJS: Nuxt 2's and Angular Universal's
35
+ * server builds) have none: webpack 4 cannot parse the worker start's
36
+ * `import.meta`, and for node reads no `browser` field; and for an .mjs
37
+ * file it resolves the name and the path to two files, each chunk twice.
38
+ * When both loads fail, both errors are reported: the first says why in a bundled build, the
39
+ * second on a page with no bundler.
40
+ *
41
+ * A CHUNK OF ANOTHER VERSION IS A FAILED LOAD. Each chunk exports the
42
+ * version it was built as (the package's, as transport.ts reads it), and
43
+ * one that is not the SDK's own is refused, so the file beside the SDK is
44
+ * asked for. MEASURED on 3.1.0-rc.1 (CDN-FINDINGS.txt): unpkg's
45
+ * `?module` rewrites the SDK's import of its own name to
46
+ * `https://unpkg.com/@keploy/capture-sdk@latest/<chunk>?module` -- the
47
+ * package does not depend on itself, so the CDN knows no version for it
48
+ * -- which loads whatever version is latest, never the page's. A chunk
49
+ * that exports no version (3.0.x's) is another version too.
50
+ *
51
+ * EACH LOAD GOES THROUGH THE LEAVE GATE (leave.ts, staying): never started
52
+ * while the page is leaving, and, refused or cancelled by the leave, asked
53
+ * for once more when the page has stayed -- so a bare import cancelled by
54
+ * a leave never asks for `beside` mid-leave, and one the module map keeps
55
+ * as failed falls to `beside` once the page has stayed. Every chunk the
56
+ * SDK loads is one of these.
57
+ */
58
+ export declare function ownChunk<T>(bare: () => Promise<T>, beside?: () => Promise<T>): Promise<T>;
package/dist/network.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { NetworkEventCallback, PrivacyConfig } from "./types";
1
+ import type { CaptureConfig, NetworkEventCallback, PrivacyConfig } from "./types";
2
+ type TermCode = typeof import("./request-term");
2
3
  /**
3
4
  * Cut a recorded body at `maxBodySize`.
4
5
  *
@@ -6,7 +7,7 @@ import type { NetworkEventCallback, PrivacyConfig } from "./types";
6
7
  * missed the seventh: an XHR FormData body was assembled and recorded
7
8
  * WHOLE, however large. An upload form with a base64 field put
8
9
  * megabytes into one event — past the queue's byte accounting, and far
9
- * past the 64 KiB the page-unload flush can carry, so it held back
10
+ * past the 32 KiB the page-unload flush can carry, so it held back
10
11
  * everything behind it there too.
11
12
  *
12
13
  * THE CAP COUNTS UTF-16 CODE UNITS, not bytes: the unit a JS string is
@@ -67,24 +68,182 @@ export declare class NetworkInterceptor {
67
68
  /** Absolute URLs never observed — the SDK's own ingest endpoint. */
68
69
  private ignored;
69
70
  private active;
71
+ /**
72
+ * Exchanges answered while recording whose bodies are still being read
73
+ * for the recording, and events reported and still waiting for their
74
+ * task (see emit). The page has its Response already (see
75
+ * relayResponse); what is pending is only the event. stop() lets them
76
+ * report, and settle() bounds the wait, so an exchange answered before
77
+ * stop() is still recorded -- as it was when the page's fetch() waited
78
+ * for the read.
79
+ */
80
+ private inflight;
81
+ /** An answered exchange's synchronous report, until it has reported. */
82
+ private unloadCuts;
83
+ /**
84
+ * Bodies reported and not yet recorded: each is redacted off the main
85
+ * thread (body-jobs.ts, see emit), and recorded now, in the order they
86
+ * were reported, when the page leaves or the capture stops waiting
87
+ * (cutInflight).
88
+ */
89
+ private bodies;
90
+ /** Takes an event's place in the recording when it is reported (see start()). */
91
+ private place?;
92
+ /**
93
+ * Whether an exchange is recorded at all, asked at the exchange's OWN
94
+ * moments -- when its request leaves, and again when a fetch is
95
+ * answered or an XHR finishes -- never when its event is emitted. A fetch's event follows
96
+ * its body, which the page reads AFTER its fetch() resolved: asked at
97
+ * emit, `pause(); await fetch(card); resume()` recorded the card
98
+ * exchange (the resume() came before the body was read), and `await
99
+ * fetch(x); pause()` lost x. Every event the callback gets has been
100
+ * admitted.
101
+ */
102
+ private admit;
103
+ /** Which start() an exchange belongs to: one from before a stop() never reports into a later start(). */
104
+ private run;
70
105
  private originalFetch;
71
106
  private originalXhrOpen;
72
107
  private originalXhrSend;
73
108
  private originalXhrSetHeader;
74
- constructor(privacyConfig?: Partial<PrivacyConfig>, maxBodySize?: number, ignoreUrls?: string[]);
109
+ constructor(privacyConfig?: Partial<PrivacyConfig>, maxBodySize?: number, ignoreUrls?: string[],
110
+ /** The capture's privacyMode: `strict` records no body and no query value. */
111
+ privacyMode?: CaptureConfig["privacyMode"]);
75
112
  /** True for a request the interceptor must pass through untouched. */
76
113
  private ignores;
77
- start(callback: NetworkEventCallback): void;
114
+ /**
115
+ * `place`, when given, is asked for an exchange's place in the
116
+ * recording at the moment it is reported -- where it was recorded
117
+ * before its masking moved to a task of its own (see emit) -- and the
118
+ * answer is handed to `callback` beside the event.
119
+ */
120
+ start(callback: NetworkEventCallback, admit?: () => boolean, place?: () => number): void;
121
+ /**
122
+ * Ask for the body code now (body-jobs.ts, askForCode), for a capture
123
+ * that will redact bodies with it: none for one that masks them whole.
124
+ */
125
+ askForCode(): void;
78
126
  stop(): void;
127
+ /**
128
+ * Wait at most `timeoutMs` for the exchanges answered before stop() to
129
+ * report, then report nothing more. An endless stream never ends; the
130
+ * bound is what ends the wait. The page's own streams are not touched:
131
+ * they go on flowing through the SDK's relay, unrecorded.
132
+ */
133
+ settle(timeoutMs: number): Promise<void>;
134
+ /**
135
+ * Report, now and synchronously, every answered exchange still reading
136
+ * its body: what the SDK has read of it, cut and marked. Called at
137
+ * pagehide, right before the transport's last flush. The page gets its
138
+ * Response at the headers, so a page that leaves as soon as it has one
139
+ * (a login form that navigates on `res.ok`) can leave while the body
140
+ * is still arriving; the exchange was recorded when fetch() waited for
141
+ * the body, and must still be.
142
+ */
143
+ cutInflight(): void;
144
+ /**
145
+ * Report every answered exchange still reading its body, cut where the
146
+ * SDK had it (see cutInflight): at pause(), whose exchanges are then
147
+ * recorded as their bodies come back from the worker -- an exchange
148
+ * answered before the pause is admitted.
149
+ */
150
+ cutBodies(): void;
151
+ /**
152
+ * Record now, on this thread and in the order they were reported, the
153
+ * bodies still being redacted (see emit) that fit the main thread's
154
+ * budget: when the tab is hidden, before the drain that may be its
155
+ * last, when the browser is back online, before the upload that ends
156
+ * an outage, and at stop(). The rest are left to the worker; pagehide and
157
+ * settle()'s bound record them all (cutInflight()).
158
+ */
159
+ reportDue(): void;
160
+ /** True while an exchange answered while recording is still reading its body for the recording. */
161
+ hasInflight(): boolean;
162
+ /** True when an exchange is recorded: asked when its request leaves and again when it ends. */
163
+ private admits;
164
+ /** Report `event` for an exchange of start() number `run`, stamped `at` (see NetworkEventCallback); `after` is when its relay is over (see emit). */
165
+ private emitFor;
166
+ /** Track a pending recording until it has reported. */
167
+ private track;
168
+ /**
169
+ * The Response the page gets for `response`, and, through `report`, its
170
+ * body as recorded. See relayResponse. Bodies the SDK does not relay
171
+ * are recorded as they always were.
172
+ */
173
+ private recordResponse;
79
174
  private patchFetch;
80
175
  private restoreFetch;
81
176
  private patchXHR;
82
177
  private restoreXHR;
178
+ /**
179
+ * Report an exchange whose request body (`body`, when not yet read),
180
+ * term or response body (`response`, a fetch's) may still be settling:
181
+ * once all are known, or -- cut first, at pagehide, pause() or settle()'s
182
+ * bound after stop() -- at once, with the fail-closed note for a request
183
+ * body not yet read, body:* (unknown) for a term not yet known, and the
184
+ * response body as the SDK has it, cut (its relay's cut). So an exchange
185
+ * is never lost waiting for its term (its chunk loading, its hash
186
+ * running), and is reported once.
187
+ *
188
+ * `response` is handed the report of the response body and returns its
189
+ * cut, and when the page's body is relayed, when the relay is over
190
+ * (`done`): what the main thread does for the event waits for that too
191
+ * (emit) -- its redaction where there is no worker -- so it never runs
192
+ * while the page reads the rest of a body past the cap (but for the copy
193
+ * in a browser with no idle periods, body-jobs.ts redact()). It is
194
+ * called once the cut is in place, so a body known at once (none, or one
195
+ * too large to read) reports into it.
196
+ */
197
+ private reportWhenKnown;
198
+ /** Whether this capture has said KUI_E_REQUEST_TERM_FAILED yet. */
199
+ private termWarned;
200
+ /**
201
+ * canonical-key.v2's term for a request body, FAIL-SAFE: the SDK runs
202
+ * inside the customer's page, so a failure of its own instrumentation
203
+ * -- a lexer, a parser or a page's SubtleCrypto that throws, at once
204
+ * or later -- must never reach the page's fetch or XHR. Any throw
205
+ * becomes NO term (the event is recorded with no requestTerm, as an
206
+ * SDK older than canonical-key.v2 records it); a term chunk that did
207
+ * not load (a CSP, offline, a 404) makes the term unknown, body:*, for
208
+ * this request and, the failed load being kept, for every later one of
209
+ * the page. Either is said once per capture, typed, on the console --
210
+ * the channel the SDK's other non-terminal warnings use.
211
+ */
212
+ private termFor;
213
+ private termFailed;
214
+ /**
215
+ * THE PAGE HAS ITS RESPONSE FIRST; IT IS RECORDED AFTER, AND ITS BODIES
216
+ * ARE REDACTED OFF THE PAGE'S THREAD. The relay reports a body when the
217
+ * network's ends -- before the page's stream is handed its end -- and
218
+ * the masking, the scrub and the event's serialisation all used to run
219
+ * there, synchronously: the page's `await res.json()` waited for them.
220
+ * MEASURED (MEASURE.md, uic-sdk-perf): 50 parallel 64 KB JSON fetches
221
+ * took 32 -> 382 ms in Chromium, and WebKit's time to headers went
222
+ * 39 -> 322 ms. So the report only takes the exchange's place in the
223
+ * recording (`place`, the order it was recorded in before) and hands
224
+ * its bodies to body-jobs.ts, which redacts them in a Web Worker (or,
225
+ * where the page has none, on this thread in a task of its own, under
226
+ * a budget); the headers, the URL and the event's serialisation run
227
+ * when the bodies come back. Its task -- the copy to the worker (where
228
+ * the browser has idle periods; Safari copies at once), or its
229
+ * redaction where there is no worker -- waits for the relay to be over
230
+ * too (`after`):
231
+ * one past the cap is reported at the cap, while the page is still
232
+ * reading the rest.
233
+ *
234
+ * Nothing recorded is lost or changed: the event is the same, stamped
235
+ * when it was answered (`at`), and in the same place. It is tracked
236
+ * until it is recorded; settle()'s bound and pagehide (cutInflight())
237
+ * record at once what the worker still holds, and a hidden tab and
238
+ * stop() what fits the main thread's budget (reportDue()).
239
+ */
83
240
  private emit;
84
241
  }
85
242
  /** Exposed for tests: the cut and the XHR body shape, without an XHR. */
86
243
  export declare const __internals: {
87
244
  truncate: typeof truncate;
88
245
  serializeXhrBody: typeof serializeXhrBody;
246
+ /** Load the term code with `load` from now on (the page's import() when none), forgetting any load so far. */
247
+ termLoader(load?: () => Promise<TermCode>): void;
89
248
  };
90
249
  export {};