@vitrinka/web 0.2.1 → 0.3.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 (44) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +49 -7
  3. package/build/hud.d.ts +1 -1
  4. package/build/hud.iife.js +9 -8
  5. package/build/recorder/RecorderProvider.d.ts +2 -1
  6. package/build/recorder/RecorderProvider.js +18 -4
  7. package/build/recorder/capture/click.d.ts +6 -0
  8. package/build/recorder/capture/click.js +15 -4
  9. package/build/recorder/capture/net.js +16 -10
  10. package/build/recorder/capture/redact.d.ts +11 -0
  11. package/build/recorder/capture/redact.js +18 -13
  12. package/build/recorder/capture/rrweb.d.ts +7 -1
  13. package/build/recorder/capture/rrweb.js +23 -7
  14. package/build/recorder/capture/selector.d.ts +7 -1
  15. package/build/recorder/capture/selector.js +14 -4
  16. package/build/recorder/config.d.ts +6 -0
  17. package/build/recorder/config.js +1 -0
  18. package/build/recorder/flight.d.ts +63 -0
  19. package/build/recorder/flight.js +93 -0
  20. package/build/recorder/hud/Hud.d.ts +2 -1
  21. package/build/recorder/hud/Hud.js +113 -11
  22. package/build/recorder/hud/Menu.d.ts +4 -2
  23. package/build/recorder/hud/Menu.js +2 -2
  24. package/build/recorder/hud/RecorderPill.d.ts +2 -1
  25. package/build/recorder/hud/RecorderPill.js +11 -5
  26. package/build/recorder/hud/Sheet.d.ts +11 -2
  27. package/build/recorder/hud/Sheet.js +13 -4
  28. package/build/recorder/hud/controller.d.ts +29 -0
  29. package/build/recorder/hud/flow.d.ts +13 -3
  30. package/build/recorder/hud/flow.js +17 -6
  31. package/build/recorder/hud/icons.d.ts +1 -0
  32. package/build/recorder/hud/icons.js +3 -0
  33. package/build/recorder/hud/styles.d.ts +1 -1
  34. package/build/recorder/hud/styles.js +1 -0
  35. package/build/recorder/index.d.ts +5 -0
  36. package/build/recorder/index.js +9 -1
  37. package/build/recorder/page-controller.js +4 -0
  38. package/build/recorder/queue.d.ts +11 -4
  39. package/build/recorder/queue.js +15 -8
  40. package/build/recorder/report.d.ts +36 -0
  41. package/build/recorder/report.js +250 -0
  42. package/build/recorder/session.d.ts +12 -2
  43. package/build/recorder/session.js +25 -12
  44. package/package.json +2 -2
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Recorder root — mounted ONLY when `url` + `key` are present (see index.ts).
3
3
  * Installs the capture lanes once per document, recovers a session that
4
- * survived a reload, and keeps the rrweb lane in step with the session.
4
+ * survived a reload, and keeps the rrweb lane in step with the session — or,
5
+ * idle under a mounted pill, with the flight recorder (report.ts).
5
6
  */
6
7
  import { type ReactElement, type ReactNode } from 'react';
7
8
  import { type RecorderConfig } from './config';
@@ -2,19 +2,22 @@ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
2
  /**
3
3
  * Recorder root — mounted ONLY when `url` + `key` are present (see index.ts).
4
4
  * Installs the capture lanes once per document, recovers a session that
5
- * survived a reload, and keeps the rrweb lane in step with the session.
5
+ * survived a reload, and keeps the rrweb lane in step with the session — or,
6
+ * idle under a mounted pill, with the flight recorder (report.ts).
6
7
  */
7
8
  import { useEffect } from 'react';
8
9
  import { installClickLane } from './capture/click';
9
10
  import { patchConsole, unpatchConsole } from './capture/console';
10
11
  import { installNavLane, noteNavigation, primeNavigation } from './capture/nav';
11
12
  import { patchNetwork, unpatchNetwork } from './capture/net';
12
- import { checkoutRRWeb, flushRRWeb, startRRWeb, stopRRWeb } from './capture/rrweb';
13
+ import { checkoutRRWeb, flushRRWeb, startFlightRRWeb, startRRWeb, stopRRWeb } from './capture/rrweb';
13
14
  import { configureRecorder } from './config';
14
15
  import { installControl } from './control';
16
+ import { flightActive } from './flight';
15
17
  import { installUnauthorizedHandler } from './link';
16
18
  import { insideHud } from './hud/host';
17
19
  import { armReconcile, flush, getState, persistNow, reconcile, scheduleFlush } from './queue';
20
+ import { dropFlight, syncFlight } from './report';
18
21
  import { onBeforeStop, recoverRedactionPolicy } from './session';
19
22
  import { annotateState, setTabIdentity, subscribe } from './state';
20
23
  const TAB_KEY = 'vitrinka.tab';
@@ -44,7 +47,7 @@ export function RecorderProvider({ config, children, }) {
44
47
  // The fetch/XHR and console patches are idempotent (a globalThis mark)
45
48
  // and UNINSTALLED on unmount below, so the recorder never outlives its
46
49
  // tree; the History wrap is idempotent too and stays (a nav event with no
47
- // session is dropped at the queue, so it costs nothing).
50
+ // session is dropped at the queue unless the flight recorder holds it).
48
51
  patchNetwork();
49
52
  patchConsole();
50
53
  installNavLane();
@@ -54,20 +57,30 @@ export function RecorderProvider({ config, children, }) {
54
57
  const uninstallControl = installControl();
55
58
  const uninstall401 = installUnauthorizedHandler();
56
59
  // Keep the rrweb lane in step with the session: start on record, a fresh
57
- // checkout on resume, ship-and-stop on stop.
60
+ // checkout on resume, ship-and-stop on stop. While idle the same lane
61
+ // feeds the pill's flight recorder; it hands rrweb over (one record() at
62
+ // a time) before a recording takes it.
58
63
  let wasRecording = false;
59
64
  let wasPaused = false;
65
+ let wasFlying = false;
60
66
  const syncLanes = () => {
67
+ void syncFlight();
61
68
  const rec = getState();
62
69
  const recording = rec !== null && !rec.dead;
70
+ const flying = flightActive();
71
+ if (wasFlying && !flying)
72
+ stopRRWeb();
63
73
  if (recording && !wasRecording)
64
74
  startRRWeb();
65
75
  else if (!recording && wasRecording)
66
76
  stopRRWeb();
67
77
  else if (recording && wasPaused && !rec.paused)
68
78
  checkoutRRWeb();
79
+ if (flying && !wasFlying)
80
+ startFlightRRWeb();
69
81
  wasRecording = recording;
70
82
  wasPaused = rec?.paused ?? false;
83
+ wasFlying = flying;
71
84
  };
72
85
  const unsubscribe = subscribe(syncLanes);
73
86
  const offBeforeStop = onBeforeStop(flushRRWeb);
@@ -104,6 +117,7 @@ export function RecorderProvider({ config, children, }) {
104
117
  uninstallControl();
105
118
  uninstall401();
106
119
  stopRRWeb();
120
+ dropFlight();
107
121
  unpatchNetwork();
108
122
  unpatchConsole();
109
123
  };
@@ -7,6 +7,12 @@ export declare function imageRect(el: Element): {
7
7
  w: number;
8
8
  h: number;
9
9
  };
10
+ /**
11
+ * A click's `text` under the active rules: '' when a `maskAllText` policy
12
+ * has rrweb mask every text node, otherwise `elementText` (no form-field
13
+ * values, nothing rr-masked) through the free-text scrub.
14
+ */
15
+ export declare function clickText(el: Element): string;
10
16
  export interface ClickLaneOptions {
11
17
  /** True while the HUD owns the pointer (annotate mode) or the target is HUD chrome. */
12
18
  ignore: (target: EventTarget | null) => boolean;
@@ -2,12 +2,13 @@
2
2
  * Click lane — a capture-phase document listener (sees clicks the app
3
3
  * swallows). Shape matches the extension's content script exactly:
4
4
  * `{selector, text, rect}` — `shortSelector` (id > data-testid > a ≤4-hop
5
- * tag.class path), the element's text (redacted, 80 chars) and its bounding
6
- * rect in device pixels — plus `route`.
5
+ * tag.class path), the element's text (`clickText`: redacted, 80 chars,
6
+ * never more than the DOM lane shows) and its bounding rect in device pixels
7
+ * — plus `route`.
7
8
  */
8
9
  import { pushEvent } from '../queue';
9
10
  import { currentRoute } from '../state';
10
- import { redactText } from './redact';
11
+ import { maskDirectives, redactText } from './redact';
11
12
  import { elementText, shortSelector } from './selector';
12
13
  export { elementText, shortSelector };
13
14
  const CLICKABLE = 'a,button,[role=button],input,select,textarea,label';
@@ -22,6 +23,16 @@ export function imageRect(el) {
22
23
  h: Math.round(r.height * s),
23
24
  };
24
25
  }
26
+ /**
27
+ * A click's `text` under the active rules: '' when a `maskAllText` policy
28
+ * has rrweb mask every text node, otherwise `elementText` (no form-field
29
+ * values, nothing rr-masked) through the free-text scrub.
30
+ */
31
+ export function clickText(el) {
32
+ if (maskDirectives().maskAllText)
33
+ return '';
34
+ return redactText(elementText(el)) ?? '';
35
+ }
25
36
  /** Install the click lane; returns the uninstaller. */
26
37
  export function installClickLane(opts) {
27
38
  const onClick = (e) => {
@@ -34,7 +45,7 @@ export function installClickLane(opts) {
34
45
  return;
35
46
  pushEvent('click', {
36
47
  selector: shortSelector(el),
37
- text: redactText(elementText(el)) ?? '',
48
+ text: clickText(el),
38
49
  rect: imageRect(el),
39
50
  route: currentRoute.pathname,
40
51
  }, { tabId: currentRoute.tabId, tabHost: currentRoute.tabHost });
@@ -14,6 +14,7 @@
14
14
  * completion; fetch clones).
15
15
  */
16
16
  import { isVitrinkaUrl } from '../config';
17
+ import { flightTarget } from '../flight';
17
18
  import { getState, pushEvent, trackCapture } from '../queue';
18
19
  import { currentRoute } from '../state';
19
20
  import { redactAndCap, redactHeaders, redactUrl } from './redact';
@@ -37,23 +38,28 @@ export function __setBodyReadDeadlineForTests(ms) {
37
38
  BODY_READ_DEADLINE_MS = prev;
38
39
  };
39
40
  }
40
- /** Is a session actively capturing right now? Gates all body work. */
41
+ /** Is a session (or the idle flight recorder) capturing right now? Gates all body work. */
41
42
  function capturing() {
42
- return activeSessionId() !== null;
43
+ return captureTarget() !== null;
43
44
  }
44
- /** Id of the capturing session, else null (paused AND dead count as null). */
45
- function activeSessionId() {
45
+ /**
46
+ * Id of what captures right now: the live session (paused AND dead count as
47
+ * null), else the idle flight recorder's buffer, else null.
48
+ */
49
+ function captureTarget() {
46
50
  const rec = getState();
47
- return rec !== null && !rec.paused && !rec.dead ? rec.sessionId : null;
51
+ if (rec)
52
+ return !rec.paused && !rec.dead ? rec.sessionId : null;
53
+ return flightTarget();
48
54
  }
49
55
  /**
50
- * Is `id` STILL the capturing session? In-flight requests must be bound to the
56
+ * Is `id` STILL the capture target? In-flight requests must be bound to the
51
57
  * session they started in: a request begun in session A that completes after A
52
58
  * stopped and B started would otherwise be appended to B, with B's route
53
- * attached.
59
+ * attached — and one begun in the flight buffer never lands in a session.
54
60
  */
55
61
  function stillCapturing(id) {
56
- return id !== null && activeSessionId() === id;
62
+ return id !== null && captureTarget() === id;
57
63
  }
58
64
  /**
59
65
  * Redact + cap in the shape-appropriate ORDER — see `redactAndCap`
@@ -366,7 +372,7 @@ export function patchNetwork() {
366
372
  const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
367
373
  // Not recording (or recorder's own traffic) → pass straight through: no
368
374
  // clone, no body read, zero added cost.
369
- const origin = activeSessionId();
375
+ const origin = captureTarget();
370
376
  if (isVitrinkaUrl(url) || origin === null)
371
377
  return origFetch(input, init);
372
378
  const method = init?.method ??
@@ -461,7 +467,7 @@ export function patchNetwork() {
461
467
  });
462
468
  const patchedSend = (XHR.prototype.send = function (body) {
463
469
  const meta = this.__vt;
464
- const origin = activeSessionId();
470
+ const origin = captureTarget();
465
471
  if (meta && !isVitrinkaUrl(meta.url) && origin !== null) {
466
472
  const started = Date.now();
467
473
  this.addEventListener('loadend', () => {
@@ -11,6 +11,7 @@
11
11
  * Capture layers import the bound helpers below so every call site stays a
12
12
  * one-liner and can never forget to pass the rules.
13
13
  */
14
+ import type { eventWithTime, metaEvent } from '@rrweb/types';
14
15
  import { type MaskDirectives, type RedactionPolicy, type RuleSet } from '@vitrinka/redact';
15
16
  export { isSecretKey, REDACTED, type RedactionPolicy } from '@vitrinka/redact';
16
17
  /**
@@ -32,6 +33,16 @@ export declare function redactText(text: string | undefined): string | undefined
32
33
  export declare function redactAndCap(body: string, cap: number, contentType?: string): string | undefined;
33
34
  /** Scrub URL query/fragment secrets under the active rules. */
34
35
  export declare function redactUrl(url: string): string;
36
+ /** rrweb's Meta event — the head of every full snapshot (start, each checkout, resume). */
37
+ export declare function isMetaEvent(ev: eventWithTime): ev is metaEvent & eventWithTime;
38
+ /**
39
+ * Scrub an rrweb event before it is buffered. The Meta event that opens every
40
+ * full snapshot (start, each checkout, resume) carries the page's raw
41
+ * `location.href`, query and fragment included; it gets `redactUrl`. Every
42
+ * other event passes through as is — the DOM itself is masked by rrweb under
43
+ * `maskDirectives`.
44
+ */
45
+ export declare function redactRRWebEvent(ev: eventWithTime): eventWithTime;
35
46
  /** Scrub + cap a captured header map under the active rules. */
36
47
  export declare function redactHeaders(headers: Record<string, unknown> | undefined): Record<string, string> | undefined;
37
48
  /** rrweb masking options under the active rules (maskAllInputs / maskAllText). */
@@ -1,16 +1,3 @@
1
- /**
2
- * Redaction bridge — binds the shared engine (@vitrinka/redact) to the
3
- * CURRENT session's workspace policy.
4
- *
5
- * The engine itself owns all semantics (key classification, JSON/form/
6
- * multipart bodies, URL query+fragment scrubbing, patterns, fullFidelity);
7
- * this module owns which RULES apply right now: the safe defaults until a
8
- * session starts, the session's fetched policy after. FAIL CLOSED — a failed
9
- * or slow policy fetch means the defaults, never capture-everything.
10
- *
11
- * Capture layers import the bound helpers below so every call site stays a
12
- * one-liner and can never forget to pass the rules.
13
- */
14
1
  import { compileRules, redactAndCap as engineRedactAndCap, redactHeaders as engineRedactHeaders, redactText as engineRedactText, redactUrl as engineRedactUrl, maskDirectives as engineMaskDirectives, } from '@vitrinka/redact';
15
2
  // Engine primitives that need no binding, re-exported for tests and callers.
16
3
  export { isSecretKey, REDACTED } from '@vitrinka/redact';
@@ -44,6 +31,24 @@ export function redactAndCap(body, cap, contentType) {
44
31
  export function redactUrl(url) {
45
32
  return engineRedactUrl(rules, url);
46
33
  }
34
+ /** rrweb's `EventType.Meta`; `@rrweb/types` is type-only here, so the value. */
35
+ const RRWEB_META = 4;
36
+ /** rrweb's Meta event — the head of every full snapshot (start, each checkout, resume). */
37
+ export function isMetaEvent(ev) {
38
+ return ev.type === RRWEB_META;
39
+ }
40
+ /**
41
+ * Scrub an rrweb event before it is buffered. The Meta event that opens every
42
+ * full snapshot (start, each checkout, resume) carries the page's raw
43
+ * `location.href`, query and fragment included; it gets `redactUrl`. Every
44
+ * other event passes through as is — the DOM itself is masked by rrweb under
45
+ * `maskDirectives`.
46
+ */
47
+ export function redactRRWebEvent(ev) {
48
+ if (!isMetaEvent(ev))
49
+ return ev;
50
+ return { ...ev, data: { ...ev.data, href: redactUrl(ev.data.href) } };
51
+ }
47
52
  /** Scrub + cap a captured header map under the active rules. */
48
53
  export function redactHeaders(headers) {
49
54
  return engineRedactHeaders(rules, headers);
@@ -1,7 +1,13 @@
1
1
  import { RRWEB_BLOCK_ATTR } from '../block';
2
2
  export { RRWEB_BLOCK_ATTR };
3
- /** Start recording the DOM; idempotent. */
3
+ /** Start recording the DOM into the live session; idempotent. */
4
4
  export declare function startRRWeb(): void;
5
+ /**
6
+ * Start recording the DOM into the idle pill's flight recorder (flight.ts):
7
+ * a checkout every 30 s, each event straight into the in-memory windows.
8
+ * Idempotent; `stopRRWeb` stops it.
9
+ */
10
+ export declare function startFlightRRWeb(): void;
5
11
  /** Ship the tail and stop recording. */
6
12
  export declare function stopRRWeb(): void;
7
13
  /** Take a fresh full snapshot (resume after pause, policy change). */
@@ -1,8 +1,9 @@
1
1
  import { record } from 'rrweb';
2
2
  import { RRWEB_BLOCK_ATTR } from '../block';
3
+ import { FLIGHT_CHECKOUT_MS, pushFlightRRWeb } from '../flight';
3
4
  import { pushRRWebBatch } from '../queue';
4
5
  import { currentRoute } from '../state';
5
- import { maskDirectives } from './redact';
6
+ import { maskDirectives, redactRRWebEvent } from './redact';
6
7
  export { RRWEB_BLOCK_ATTR };
7
8
  const BATCH_MS = 2000;
8
9
  /** A fresh full snapshot every 5 min keeps long recordings seekable. */
@@ -19,17 +20,31 @@ function ship() {
19
20
  // paused, and the next resume starts from a fresh checkout.
20
21
  pushRRWebBatch(events, { tabId: currentRoute.tabId, tabHost: currentRoute.tabHost });
21
22
  }
22
- /** Start recording the DOM; idempotent. */
23
+ /** Start recording the DOM into the live session; idempotent. */
23
24
  export function startRRWeb() {
25
+ startRecord('session');
26
+ }
27
+ /**
28
+ * Start recording the DOM into the idle pill's flight recorder (flight.ts):
29
+ * a checkout every 30 s, each event straight into the in-memory windows.
30
+ * Idempotent; `stopRRWeb` stops it.
31
+ */
32
+ export function startFlightRRWeb() {
33
+ startRecord('flight');
34
+ }
35
+ function startRecord(sink) {
24
36
  if (stop)
25
37
  return;
26
38
  const mask = maskDirectives();
27
39
  try {
28
40
  const stopFn = record({
29
- emit: (ev) => {
30
- buf.push(ev);
31
- },
32
- checkoutEveryNms: CHECKOUT_MS,
41
+ // Scrubbed on the way in: nothing raw ever sits in the buffer.
42
+ emit: sink === 'flight'
43
+ ? (ev) => pushFlightRRWeb(redactRRWebEvent(ev))
44
+ : (ev) => {
45
+ buf.push(redactRRWebEvent(ev));
46
+ },
47
+ checkoutEveryNms: sink === 'flight' ? FLIGHT_CHECKOUT_MS : CHECKOUT_MS,
33
48
  // Never inline: to read a no-CORS cross-origin image rrweb sets
34
49
  // crossOrigin='anonymous' on the page's own <img>, the browser refetches
35
50
  // it in CORS mode and a host without ACAO shows a broken image — the
@@ -46,7 +61,8 @@ export function startRRWeb() {
46
61
  console.warn('vitrinka: rrweb failed to start', e);
47
62
  return;
48
63
  }
49
- timer = setInterval(ship, BATCH_MS);
64
+ if (sink === 'session')
65
+ timer = setInterval(ship, BATCH_MS);
50
66
  }
51
67
  /** Ship the tail and stop recording. */
52
68
  export function stopRRWeb() {
@@ -4,5 +4,11 @@
4
4
  */
5
5
  /** The extension's selector heuristic — id, then testid, then a short path. */
6
6
  export declare function shortSelector(el: Element | null): string;
7
- /** The element's own visible text or value, trimmed and capped. */
7
+ /**
8
+ * The element's own visible text, trimmed and capped — never more than the
9
+ * DOM lane shows. Never a form field's value or text (rrweb masks every
10
+ * input, so a click on a filled password field must not record it), and ''
11
+ * for an element at, under or around `.rr-mask` / `.rr-block`: `innerText`
12
+ * of a wrapper would carry its masked child's text.
13
+ */
8
14
  export declare function elementText(el: Element): string;
@@ -27,9 +27,19 @@ export function shortSelector(el) {
27
27
  }
28
28
  return parts.join(' > ');
29
29
  }
30
- /** The element's own visible text or value, trimmed and capped. */
30
+ /** Form fields: rrweb masks every input value, so their text is never read. */
31
+ const FORM_FIELD = 'input,textarea,select';
32
+ /** What the DOM lane already hides — rrweb's mask and block classes. */
33
+ const MASKED = '.rr-mask,.rr-block';
34
+ /**
35
+ * The element's own visible text, trimmed and capped — never more than the
36
+ * DOM lane shows. Never a form field's value or text (rrweb masks every
37
+ * input, so a click on a filled password field must not record it), and ''
38
+ * for an element at, under or around `.rr-mask` / `.rr-block`: `innerText`
39
+ * of a wrapper would carry its masked child's text.
40
+ */
31
41
  export function elementText(el) {
32
- const html = el;
33
- const raw = html.innerText || (typeof html.value === 'string' ? html.value : '') || '';
34
- return raw.trim().slice(0, TEXT_CAP);
42
+ if (el.matches(FORM_FIELD) || el.closest(MASKED) || el.querySelector(MASKED))
43
+ return '';
44
+ return (el.innerText || '').trim().slice(0, TEXT_CAP);
35
45
  }
@@ -27,6 +27,12 @@ export interface RecorderConfig {
27
27
  project?: string;
28
28
  /** Device-link label; defaults to `<browser> on <os> · <host>`. */
29
29
  label?: string;
30
+ /**
31
+ * The idle pill's in-memory last minute that "Report a bug" sends
32
+ * (flight.ts). `false` turns it off: nothing is captured until a recording
33
+ * starts, and Report a bug is offered only during one.
34
+ */
35
+ flightRecorder?: boolean;
30
36
  }
31
37
  export declare function configureRecorder(next: RecorderConfig): void;
32
38
  export declare function recorderConfig(): Readonly<RecorderConfig>;
@@ -8,6 +8,7 @@ export function configureRecorder(next) {
8
8
  config.environment = next.environment;
9
9
  config.project = next.project;
10
10
  config.label = next.label;
11
+ config.flightRecorder = next.flightRecorder;
11
12
  }
12
13
  export function recorderConfig() {
13
14
  return config;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * The flight recorder (react-recorder decisions D9/D10): while the pill is
3
+ * mounted and idle, the last minute of the page lives HERE, in memory only —
4
+ * never in storage, never on the network — so "Report a bug" can send what
5
+ * led up to it (report.ts). A reload starts a fresh buffer.
6
+ *
7
+ * - rrweb runs with `checkoutEveryNms: 30 s`; every Meta event (rrweb emits
8
+ * one at the head of each full snapshot) opens a window, and only the
9
+ * current and the previous window are kept — so a clip always starts at a
10
+ * Meta + FullSnapshot and covers the last 30–60 s.
11
+ * - The click, nav, console and net lanes reach `pushFlightEvent` through
12
+ * the queue's `pushEvent` when no session is live: the same capture code
13
+ * and the same redaction as a recording, held instead of sent. They are
14
+ * trimmed to the oldest kept snapshot.
15
+ * - Caps: ≤ 4 MiB of serialized rrweb (each event measured at emit), ≤ 20 000
16
+ * rrweb events, ≤ 500 lane events. Over an rrweb cap the older window goes;
17
+ * a window over a cap on its own goes too, and buffering resumes at the
18
+ * next checkout (a clip must open on a full snapshot).
19
+ *
20
+ * DOM-free and React-free: the rrweb lane (capture/rrweb.ts) feeds it.
21
+ */
22
+ import type { eventWithTime } from '@rrweb/types';
23
+ export declare const FLIGHT_CHECKOUT_MS = 30000;
24
+ export declare const FLIGHT_MAX_RRWEB_BYTES: number;
25
+ export declare const FLIGHT_MAX_RRWEB_EVENTS = 20000;
26
+ export declare const FLIGHT_MAX_LANE_EVENTS = 500;
27
+ /** One lane event as captured — stamped with its ORIGINAL time, no seq yet. */
28
+ export interface FlightLaneEvent {
29
+ ts: string;
30
+ tabId: string;
31
+ tabHost: string;
32
+ kind: string;
33
+ payload?: Record<string, unknown>;
34
+ }
35
+ /** One checkout window: a Meta + FullSnapshot and what followed it. */
36
+ export interface FlightWindow {
37
+ events: eventWithTime[];
38
+ /** UTF-8 bytes of the events' JSON. */
39
+ bytes: number;
40
+ /** The Meta event's (already redacted) page URL. */
41
+ href: string;
42
+ }
43
+ /** What a report sends: oldest window first, lanes in capture order. */
44
+ export interface FlightClip {
45
+ windows: FlightWindow[];
46
+ lanes: FlightLaneEvent[];
47
+ }
48
+ /** Start a fresh, empty buffer. */
49
+ export declare function startFlightBuffer(): void;
50
+ /** Stop buffering and forget everything held. */
51
+ export declare function stopFlightBuffer(): void;
52
+ export declare function flightActive(): boolean;
53
+ /** The capture target id while buffering (the net lane binds in-flight requests to it), else null. */
54
+ export declare function flightTarget(): string | null;
55
+ /** One rrweb event, already redacted. Events before the first snapshot are dropped. */
56
+ export declare function pushFlightRRWeb(ev: eventWithTime): void;
57
+ /** One lane event (the queue's idle path). Returns its ts, null when not buffering. */
58
+ export declare function pushFlightEvent(kind: string, payload: Record<string, unknown> | undefined, route: {
59
+ tabId: string;
60
+ tabHost: string;
61
+ }): string | null;
62
+ /** A copy of what the buffer holds now (it keeps rolling); null when not buffering. */
63
+ export declare function takeFlightClip(): FlightClip | null;
@@ -0,0 +1,93 @@
1
+ import { isMetaEvent } from './capture/redact';
2
+ export const FLIGHT_CHECKOUT_MS = 30_000;
3
+ export const FLIGHT_MAX_RRWEB_BYTES = 4 * 1024 * 1024;
4
+ export const FLIGHT_MAX_RRWEB_EVENTS = 20_000;
5
+ export const FLIGHT_MAX_LANE_EVENTS = 500;
6
+ const encoder = new TextEncoder();
7
+ let active = false;
8
+ /** Bumped on every start, so a capture begun in one buffer never lands in the next. */
9
+ let epoch = 0;
10
+ let windows = [];
11
+ let lanes = [];
12
+ /** Start a fresh, empty buffer. */
13
+ export function startFlightBuffer() {
14
+ active = true;
15
+ epoch++;
16
+ windows = [];
17
+ lanes = [];
18
+ }
19
+ /** Stop buffering and forget everything held. */
20
+ export function stopFlightBuffer() {
21
+ active = false;
22
+ windows = [];
23
+ lanes = [];
24
+ }
25
+ export function flightActive() {
26
+ return active;
27
+ }
28
+ /** The capture target id while buffering (the net lane binds in-flight requests to it), else null. */
29
+ export function flightTarget() {
30
+ return active ? `flight:${epoch}` : null;
31
+ }
32
+ function oldestSnapshotMs() {
33
+ return windows[0]?.events[0]?.timestamp;
34
+ }
35
+ function trimLanes() {
36
+ const from = oldestSnapshotMs();
37
+ if (from === undefined)
38
+ return;
39
+ const keep = lanes.findIndex((e) => Date.parse(e.ts) >= from);
40
+ if (keep === -1)
41
+ lanes = [];
42
+ else if (keep > 0)
43
+ lanes = lanes.slice(keep);
44
+ }
45
+ function overCap() {
46
+ let bytes = 0;
47
+ let count = 0;
48
+ for (const w of windows) {
49
+ bytes += w.bytes;
50
+ count += w.events.length;
51
+ }
52
+ return bytes > FLIGHT_MAX_RRWEB_BYTES || count > FLIGHT_MAX_RRWEB_EVENTS;
53
+ }
54
+ /** One rrweb event, already redacted. Events before the first snapshot are dropped. */
55
+ export function pushFlightRRWeb(ev) {
56
+ if (!active)
57
+ return;
58
+ const oldest = windows[0];
59
+ if (isMetaEvent(ev)) {
60
+ windows.push({ events: [], bytes: 0, href: ev.data.href });
61
+ if (windows.length > 2)
62
+ windows.shift();
63
+ }
64
+ const current = windows.at(-1);
65
+ if (!current)
66
+ return;
67
+ current.events.push(ev);
68
+ current.bytes += encoder.encode(JSON.stringify(ev)).length;
69
+ while (windows.length > 0 && overCap())
70
+ windows.shift();
71
+ if (windows[0] !== oldest)
72
+ trimLanes();
73
+ }
74
+ /** One lane event (the queue's idle path). Returns its ts, null when not buffering. */
75
+ export function pushFlightEvent(kind, payload, route) {
76
+ if (!active)
77
+ return null;
78
+ const ts = new Date().toISOString();
79
+ lanes.push({ ts, tabId: route.tabId, tabHost: route.tabHost, kind, payload });
80
+ if (lanes.length > FLIGHT_MAX_LANE_EVENTS)
81
+ lanes.splice(0, lanes.length - FLIGHT_MAX_LANE_EVENTS);
82
+ return ts;
83
+ }
84
+ /** A copy of what the buffer holds now (it keeps rolling); null when not buffering. */
85
+ export function takeFlightClip() {
86
+ if (!active)
87
+ return null;
88
+ trimLanes();
89
+ return {
90
+ windows: windows.map((w) => ({ ...w, events: w.events.slice() })),
91
+ lanes: lanes.slice(),
92
+ };
93
+ }
@@ -4,7 +4,8 @@
4
4
  * recorder's, or another host's. Owns the dock (where the HUD rests), the
5
5
  * pill's inline flows (flow.ts), the sheet (open/title/ctx/pick + the
6
6
  * surviving draft) and where it opens, the ⋯ menu, the tooltip and details
7
- * floats, annotate mode, the keyboard shortcuts, the Esc-anywhere /
7
+ * floats, annotate mode, the bug report (its sheet, "Mark on screen" through
8
+ * annotate mode, its send flow), the keyboard shortcuts, the Esc-anywhere /
8
9
  * click-outside close and the status line a screen reader hears while the
9
10
  * pill is folded.
10
11
  */