@vitrinka/web 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -0
- package/README.md +127 -4
- package/build/hud.iife.js +7 -7
- package/build/index.d.ts +1 -0
- package/build/recorder/RecorderProvider.js +5 -18
- package/build/recorder/api.d.ts +20 -0
- package/build/recorder/api.js +21 -9
- package/build/recorder/capture/console.d.ts +6 -1
- package/build/recorder/capture/console.js +28 -5
- package/build/recorder/capture/net.d.ts +10 -1
- package/build/recorder/capture/net.js +14 -1
- package/build/recorder/capture/redact.d.ts +1 -0
- package/build/recorder/capture/redact.js +6 -1
- package/build/recorder/capture/rrweb-lane.d.ts +19 -0
- package/build/recorder/capture/rrweb-lane.js +109 -0
- package/build/recorder/capture/rrweb.d.ts +2 -9
- package/build/recorder/capture/rrweb.js +11 -86
- package/build/recorder/config.d.ts +3 -1
- package/build/recorder/config.js +15 -2
- package/build/recorder/flight.d.ts +141 -22
- package/build/recorder/flight.js +312 -27
- package/build/recorder/hud/host-style.d.ts +14 -0
- package/build/recorder/hud/host-style.js +15 -0
- package/build/recorder/hud/host.js +1 -1
- package/build/recorder/hud/styles.d.ts +1 -11
- package/build/recorder/hud/styles.js +1 -12
- package/build/recorder/state.d.ts +18 -1
- package/build/recorder/state.js +40 -1
- package/build/recorder/storage/capture.d.ts +44 -0
- package/build/recorder/storage/capture.js +136 -0
- package/build/recorder/version.d.ts +2 -2
- package/build/recorder/version.js +1 -1
- package/build/report/bundle.d.ts +4 -0
- package/build/report/bundle.js +30 -0
- package/build/report/element.d.ts +86 -0
- package/build/report/element.js +146 -0
- package/build/report/engine.d.ts +144 -0
- package/build/report/engine.js +402 -0
- package/build/report/i18n.d.ts +78 -0
- package/build/report/i18n.js +151 -0
- package/build/report/identity.d.ts +26 -0
- package/build/report/identity.js +36 -0
- package/build/report/index.d.ts +12 -0
- package/build/report/index.js +15 -0
- package/build/report/keybind.d.ts +29 -0
- package/build/report/keybind.js +99 -0
- package/build/report/markers.d.ts +77 -0
- package/build/report/markers.js +162 -0
- package/build/report/profile.d.ts +62 -0
- package/build/report/profile.js +115 -0
- package/build/report/recorder-face.d.ts +2 -0
- package/build/report/recorder-face.js +20 -0
- package/build/report/styles.d.ts +7 -0
- package/build/report/styles.js +226 -0
- package/build/report/widget.d.ts +111 -0
- package/build/report/widget.js +927 -0
- package/build/report/wire.d.ts +68 -0
- package/build/report/wire.js +96 -0
- package/build/report-widget.mjs +229 -0
- package/package.json +11 -4
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { parseLocale } from './i18n';
|
|
2
|
+
import { ReportWidget } from './widget';
|
|
3
|
+
export const REPORT_TAG = 'vitrinka-report';
|
|
4
|
+
const ATTRS = ['key', 'base', 'open-selector', 'locale', 'button'];
|
|
5
|
+
function trimBase(raw) {
|
|
6
|
+
return raw.trim().replace(/\/+$/, '');
|
|
7
|
+
}
|
|
8
|
+
/** Define `<vitrinka-report>` once per page (the first build to load wins). */
|
|
9
|
+
export function defineReportElement(faces) {
|
|
10
|
+
if (typeof customElements === 'undefined' || customElements.get(REPORT_TAG))
|
|
11
|
+
return;
|
|
12
|
+
class ReportElement extends HTMLElement {
|
|
13
|
+
static get observedAttributes() {
|
|
14
|
+
return ATTRS;
|
|
15
|
+
}
|
|
16
|
+
options = {};
|
|
17
|
+
widget = null;
|
|
18
|
+
pendingIdentity = undefined;
|
|
19
|
+
connectedCallback() {
|
|
20
|
+
this.hidden = true;
|
|
21
|
+
this.mountWidget();
|
|
22
|
+
}
|
|
23
|
+
disconnectedCallback() {
|
|
24
|
+
this.widget?.unmount();
|
|
25
|
+
this.widget = null;
|
|
26
|
+
}
|
|
27
|
+
attributeChangedCallback(name, old, value) {
|
|
28
|
+
if (old === value)
|
|
29
|
+
return;
|
|
30
|
+
if (!this.widget) {
|
|
31
|
+
// Connected but waiting for its key or base (a host that configures later): mount now.
|
|
32
|
+
if (this.isConnected && (name === 'key' || name === 'base'))
|
|
33
|
+
this.mountWidget();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (name === 'locale')
|
|
37
|
+
this.widget.setLocale(value);
|
|
38
|
+
else if (name === 'button')
|
|
39
|
+
this.widget.setButton(value !== 'false');
|
|
40
|
+
else if (name === 'open-selector')
|
|
41
|
+
this.widget.setOpenSelector(value);
|
|
42
|
+
else {
|
|
43
|
+
this.widget.unmount();
|
|
44
|
+
this.widget = null;
|
|
45
|
+
this.mountWidget();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
open() {
|
|
49
|
+
this.widget?.open();
|
|
50
|
+
}
|
|
51
|
+
close() {
|
|
52
|
+
this.widget?.close();
|
|
53
|
+
}
|
|
54
|
+
identify(who) {
|
|
55
|
+
this.pendingIdentity = who;
|
|
56
|
+
this.widget?.identify(who);
|
|
57
|
+
}
|
|
58
|
+
setLocale(locale) {
|
|
59
|
+
// The attribute is the one source: it repaints through attributeChangedCallback.
|
|
60
|
+
this.setAttribute('locale', String(locale));
|
|
61
|
+
}
|
|
62
|
+
/** Idempotent: an upgrade runs attributeChangedCallback before connectedCallback, and both land here. */
|
|
63
|
+
mountWidget() {
|
|
64
|
+
if (this.widget)
|
|
65
|
+
return;
|
|
66
|
+
const key = (this.getAttribute('key') ?? '').trim();
|
|
67
|
+
const base = trimBase(this.getAttribute('base') ?? faces.defaultBase());
|
|
68
|
+
if (!key || !base) {
|
|
69
|
+
console.warn(`vitrinka: <${REPORT_TAG}> needs a key${base ? '' : ' and a base'} — nothing mounted`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.widget = new ReportWidget({
|
|
73
|
+
base,
|
|
74
|
+
key,
|
|
75
|
+
locale: parseLocale(this.getAttribute('locale')),
|
|
76
|
+
button: this.getAttribute('button') !== 'false',
|
|
77
|
+
openSelector: this.getAttribute('open-selector'),
|
|
78
|
+
identity: this.pendingIdentity,
|
|
79
|
+
appVersion: this.options.appVersion,
|
|
80
|
+
capture: this.options.capture,
|
|
81
|
+
sanitizeUrl: this.options.sanitizeUrl,
|
|
82
|
+
loadRecord: faces.loadRecord,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
customElements.define(REPORT_TAG, ReportElement);
|
|
87
|
+
}
|
|
88
|
+
/** Mount Report a problem (or, with `face: 'recorder'`, the recorder pill) and get its handle. */
|
|
89
|
+
export function mountReport(opts, faces) {
|
|
90
|
+
if (opts.face === 'recorder')
|
|
91
|
+
return mountRecorder(opts, faces);
|
|
92
|
+
defineReportElement(faces);
|
|
93
|
+
const el = document.createElement(REPORT_TAG);
|
|
94
|
+
el.setAttribute('key', opts.key);
|
|
95
|
+
if (opts.base)
|
|
96
|
+
el.setAttribute('base', opts.base);
|
|
97
|
+
if (opts.locale)
|
|
98
|
+
el.setAttribute('locale', String(opts.locale));
|
|
99
|
+
if (opts.button === false)
|
|
100
|
+
el.setAttribute('button', 'false');
|
|
101
|
+
if (opts.openSelector)
|
|
102
|
+
el.setAttribute('open-selector', opts.openSelector);
|
|
103
|
+
el.options = { appVersion: opts.appVersion, capture: opts.capture, sanitizeUrl: opts.sanitizeUrl };
|
|
104
|
+
// null is a choice (anonymous, whatever the page-wide identity); only undefined defers.
|
|
105
|
+
if (opts.identity !== undefined)
|
|
106
|
+
el.identify(opts.identity);
|
|
107
|
+
(opts.parent ?? document.body).append(el);
|
|
108
|
+
return {
|
|
109
|
+
face: 'report',
|
|
110
|
+
open: () => el.open(),
|
|
111
|
+
close: () => el.close(),
|
|
112
|
+
identify: (who) => el.identify(who),
|
|
113
|
+
setLocale: (l) => el.setLocale(l),
|
|
114
|
+
unmount: () => el.remove(),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function mountRecorder(opts, faces) {
|
|
118
|
+
const base = trimBase(opts.base ?? faces.defaultBase());
|
|
119
|
+
let unmount = null;
|
|
120
|
+
let gone = false;
|
|
121
|
+
if (!faces.recorderFace) {
|
|
122
|
+
console.warn('vitrinka: the recorder face needs the npm package (@vitrinka/web/report) — nothing mounted');
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
faces
|
|
126
|
+
.recorderFace()
|
|
127
|
+
.then((mount) => {
|
|
128
|
+
if (!gone)
|
|
129
|
+
unmount = mount({ base, key: opts.key, appVersion: opts.appVersion, sanitizeUrl: opts.sanitizeUrl });
|
|
130
|
+
})
|
|
131
|
+
.catch((e) => console.warn('vitrinka: the recorder face did not load', e));
|
|
132
|
+
}
|
|
133
|
+
// The pill is always on screen and English-only, and speaks for the device link: no card to open.
|
|
134
|
+
const inert = () => undefined;
|
|
135
|
+
return {
|
|
136
|
+
face: 'recorder',
|
|
137
|
+
open: inert,
|
|
138
|
+
close: inert,
|
|
139
|
+
identify: inert,
|
|
140
|
+
setLocale: inert,
|
|
141
|
+
unmount: () => {
|
|
142
|
+
gone = true;
|
|
143
|
+
unmount?.();
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The report face's capture runtime — the shared engine (QA Studio D6)
|
|
3
|
+
* driven by the project's capture profile instead of a recording:
|
|
4
|
+
*
|
|
5
|
+
* 1. `GET /api/v1/report-config` with the `vkb_` key → the capture profile
|
|
6
|
+
* (narrowed by the host's `capture` option) and the masking profile. An
|
|
7
|
+
* unreachable or older server means the safe fallback: memory, the last
|
|
8
|
+
* minute, strict masking.
|
|
9
|
+
* 2. The redaction rules first, then the lanes — so capture runs under the
|
|
10
|
+
* project's rules from its first byte: the console lane (errors, every
|
|
11
|
+
* level with `console`), the network lane in metadata-only mode
|
|
12
|
+
* (`errors`: failures, `network`: every request), and the rrweb lane
|
|
13
|
+
* into the flight buffer (`interactions`).
|
|
14
|
+
* 3. The flight buffer under the profile's window and cap, in memory or
|
|
15
|
+
* also in IndexedDB (`storage: browser`, storage/capture.ts), whose
|
|
16
|
+
* expired windows the start sweeps.
|
|
17
|
+
*
|
|
18
|
+
* One engine per page however many widgets mount (the lanes and the buffer
|
|
19
|
+
* are page-wide); the last widget to leave stops it. The runtime is the
|
|
20
|
+
* recorder face's whenever one is mounted (state.ts `claimCapture`): then
|
|
21
|
+
* the engine captures nothing, and it never writes the recorder's
|
|
22
|
+
* configuration — every call it makes names its own target.
|
|
23
|
+
*/
|
|
24
|
+
import type { eventWithTime } from '@rrweb/types';
|
|
25
|
+
import { type RecordFn } from '../recorder/capture/rrweb-lane';
|
|
26
|
+
import { type FlightClip, type FlightLaneEvent } from '../recorder/flight';
|
|
27
|
+
import { EVENTS_PER_POST } from '../recorder/queue';
|
|
28
|
+
import { type Marker } from './markers';
|
|
29
|
+
import { type CaptureLanes, type CaptureNarrowing, type CaptureProfile, type MaskingProfile } from './profile';
|
|
30
|
+
export { EVENTS_PER_POST };
|
|
31
|
+
/** Loads rrweb's `record` lazily: `import('rrweb')` from npm, vitrinka's `/vendor` in the single-file widget. */
|
|
32
|
+
export type RecordLoader = (base: string) => Promise<RecordFn | null>;
|
|
33
|
+
export interface EngineOptions {
|
|
34
|
+
/** vitrinka's origin, no trailing slash. */
|
|
35
|
+
base: string;
|
|
36
|
+
/** The project's `vkb_` report key. */
|
|
37
|
+
key: string;
|
|
38
|
+
appVersion?: string;
|
|
39
|
+
sanitizeUrl?: (url: string) => string;
|
|
40
|
+
capture?: CaptureNarrowing;
|
|
41
|
+
loadRecord: RecordLoader;
|
|
42
|
+
}
|
|
43
|
+
export interface ReportConfig {
|
|
44
|
+
project: string;
|
|
45
|
+
/** The profile in force (the project's, narrowed by the host). */
|
|
46
|
+
profile: CaptureProfile;
|
|
47
|
+
lanes: CaptureLanes;
|
|
48
|
+
masking: MaskingProfile;
|
|
49
|
+
/** False when the config read failed: the fallback profile runs. */
|
|
50
|
+
reachable: boolean;
|
|
51
|
+
/** This widget captures (false while the recorder face runs the page's capture). */
|
|
52
|
+
capturing: boolean;
|
|
53
|
+
}
|
|
54
|
+
export interface ConsoleEntry {
|
|
55
|
+
level: string;
|
|
56
|
+
ts: string;
|
|
57
|
+
text: string;
|
|
58
|
+
}
|
|
59
|
+
export interface NetEntry {
|
|
60
|
+
method: string;
|
|
61
|
+
path: string;
|
|
62
|
+
status: number;
|
|
63
|
+
ms: number;
|
|
64
|
+
}
|
|
65
|
+
/** The capture as frozen when the card opened (the buffer keeps rolling). */
|
|
66
|
+
export interface Frozen {
|
|
67
|
+
clip: FlightClip | null;
|
|
68
|
+
/** Earlier loads' windows of the clip, read when it froze (the buffer may expire them before Send). */
|
|
69
|
+
stored: Promise<Map<string, {
|
|
70
|
+
events: eventWithTime[];
|
|
71
|
+
lanes: FlightLaneEvent[];
|
|
72
|
+
}>>;
|
|
73
|
+
}
|
|
74
|
+
export interface CaptureChunk {
|
|
75
|
+
/** 1..n: the chunk's leaf seq and its `rrweb` row's seq. */
|
|
76
|
+
seq: number;
|
|
77
|
+
/** Its last event's time. */
|
|
78
|
+
ts: string;
|
|
79
|
+
count: number;
|
|
80
|
+
body: string;
|
|
81
|
+
}
|
|
82
|
+
/** What one report sends of the capture. */
|
|
83
|
+
export interface BuiltCapture {
|
|
84
|
+
chunks: CaptureChunk[];
|
|
85
|
+
truncated: boolean;
|
|
86
|
+
console: ConsoleEntry[];
|
|
87
|
+
network: NetEntry[];
|
|
88
|
+
}
|
|
89
|
+
/** A request URL (already through the redaction engine) as the wire's path: pathname only, token runs scrubbed. */
|
|
90
|
+
export declare function reportPath(url: unknown): string;
|
|
91
|
+
/**
|
|
92
|
+
* Whole windows only: rrweb's stream is stateful, so a window holding an
|
|
93
|
+
* event over the wire cap (a huge full snapshot) goes entirely — its
|
|
94
|
+
* incrementals are worthless without it — and the replay says truncated.
|
|
95
|
+
*/
|
|
96
|
+
export declare function wholeWindows(events: readonly eventWithTime[], hardBytes: number): {
|
|
97
|
+
events: eventWithTime[];
|
|
98
|
+
dropped: boolean;
|
|
99
|
+
};
|
|
100
|
+
export declare class CaptureEngine {
|
|
101
|
+
readonly opts: EngineOptions;
|
|
102
|
+
readonly ready: Promise<ReportConfig>;
|
|
103
|
+
private store;
|
|
104
|
+
private stopped;
|
|
105
|
+
/** The release token of this engine's claim on the page's capture runtime. */
|
|
106
|
+
private claim;
|
|
107
|
+
/** The config `ready` resolved with — kept true to what the engine does now. */
|
|
108
|
+
private config;
|
|
109
|
+
private readonly listeners;
|
|
110
|
+
/** …and runs it now: false before the claim, without it, and after it yielded. */
|
|
111
|
+
private capturing;
|
|
112
|
+
/** The engine's own lifetime (its base registration). */
|
|
113
|
+
private readonly offs;
|
|
114
|
+
/** The capture runtime's teardown, run when it yields to a recorder or stops. */
|
|
115
|
+
private readonly capture;
|
|
116
|
+
constructor(opts: EngineOptions);
|
|
117
|
+
private start;
|
|
118
|
+
/** Hear the engine's state change (it stopped capturing): the widget repaints. */
|
|
119
|
+
onChange(fn: () => void): () => void;
|
|
120
|
+
/** The recorder mounted: hand it the runtime — this widget goes on reporting without a capture. */
|
|
121
|
+
private standDown;
|
|
122
|
+
/** Freeze the capture now (the card opened); the buffer rolls on. */
|
|
123
|
+
freeze(): Frozen;
|
|
124
|
+
/**
|
|
125
|
+
* The rrweb ids of the page nodes under a hide right now — taken when the
|
|
126
|
+
* hide is drawn (the page sits frozen under the marking layer); null when
|
|
127
|
+
* the running recorder has no mirror to say (then a hide withholds the replay).
|
|
128
|
+
*/
|
|
129
|
+
hiddenIdsAt(rect: Marker['rect']): Set<number> | null;
|
|
130
|
+
/**
|
|
131
|
+
* What one report sends: the frozen windows (stored ones read back), the
|
|
132
|
+
* hides applied, split under the chunk cap. `markedIds` are the nodes each
|
|
133
|
+
* hide covered when it was drawn; the nodes under it at send join them. A
|
|
134
|
+
* hide whose nodes cannot be named (no mirror) withholds the replay — fail
|
|
135
|
+
* closed, never a box over text that still rides along.
|
|
136
|
+
*/
|
|
137
|
+
build(frozen: Frozen, markers: readonly Marker[], markedIds?: ReadonlySet<number>): Promise<BuiltCapture>;
|
|
138
|
+
/** A report went out: forget what it carried, in memory and in the store. */
|
|
139
|
+
clear(): void;
|
|
140
|
+
stop(): void;
|
|
141
|
+
}
|
|
142
|
+
/** The page's one engine (started by the first widget); null when another base/key already owns it. */
|
|
143
|
+
export declare function acquireEngine(opts: EngineOptions): CaptureEngine | null;
|
|
144
|
+
export declare function releaseEngine(engine: CaptureEngine | null): void;
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { api } from '../recorder/api';
|
|
2
|
+
import { patchConsole, unpatchConsole } from '../recorder/capture/console';
|
|
3
|
+
import { patchNetwork, unpatchNetwork } from '../recorder/capture/net';
|
|
4
|
+
import { setCaptureSanitizer, setRedactionPolicy } from '../recorder/capture/redact';
|
|
5
|
+
import { checkoutRRWeb, runningRecord, startRecordWith, stopRRWeb } from '../recorder/capture/rrweb-lane';
|
|
6
|
+
import { addOwnBase } from '../recorder/config';
|
|
7
|
+
import { clearFlight, clipHasSnapshot, FLIGHT_MAX_LANE_EVENTS, flightWrites, persistFlightNow, startFlightBuffer, stopFlightBuffer, takeFlightClip, } from '../recorder/flight';
|
|
8
|
+
import { listen } from '../recorder/hud/listen';
|
|
9
|
+
import { EVENTS_PER_POST, splitRRWebEvents, utf8Bytes } from '../recorder/queue';
|
|
10
|
+
import { browserTabId, claimCapture, currentRoute, releaseCapture, setTabIdentity } from '../recorder/state';
|
|
11
|
+
import { deleteCaptureStore, openCaptureStore } from '../recorder/storage/capture';
|
|
12
|
+
import { applyHides, hiddenNodeIds } from './markers';
|
|
13
|
+
import { lanesOf, legacyProfile, maskingOf, maskingPolicy, narrowProfile, parseProfile, UNMASK_SELECTOR, } from './profile';
|
|
14
|
+
export { EVENTS_PER_POST };
|
|
15
|
+
/** The report wire's chunk cap is 4 MiB; pack a little under it. */
|
|
16
|
+
const CHUNK_PACK_BYTES = 3.5 * 1024 * 1024;
|
|
17
|
+
const CHUNK_HARD_BYTES = 4 * 1024 * 1024 - 64;
|
|
18
|
+
const MAX_CONSOLE_TEXT = 2000;
|
|
19
|
+
const MAX_ENTRIES = 500;
|
|
20
|
+
/** A checkout every minute at most: a short window checks out as often as it is long. */
|
|
21
|
+
const MAX_CHECKOUT_MS = 60_000;
|
|
22
|
+
/** Token-shaped runs in a request path (reset links, signed URLs) are scrubbed: the server keeps paths as sent. */
|
|
23
|
+
const TOKENY = /[A-Za-z0-9_-]{20,}/g;
|
|
24
|
+
function str(v, fallback = '') {
|
|
25
|
+
return typeof v === 'string' ? v : fallback;
|
|
26
|
+
}
|
|
27
|
+
/** A request URL (already through the redaction engine) as the wire's path: pathname only, token runs scrubbed. */
|
|
28
|
+
export function reportPath(url) {
|
|
29
|
+
const raw = str(url);
|
|
30
|
+
let path;
|
|
31
|
+
try {
|
|
32
|
+
path = new URL(raw, globalThis.location?.href ?? 'http://localhost').pathname;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
path = raw.split(/[?#]/)[0] ?? '';
|
|
36
|
+
}
|
|
37
|
+
return path.replace(TOKENY, '•••').slice(0, 300);
|
|
38
|
+
}
|
|
39
|
+
/** rrweb's `EventType.Meta`, the head of every window (`@rrweb/types` is type-only here). */
|
|
40
|
+
const RRWEB_META = 4;
|
|
41
|
+
/**
|
|
42
|
+
* Whole windows only: rrweb's stream is stateful, so a window holding an
|
|
43
|
+
* event over the wire cap (a huge full snapshot) goes entirely — its
|
|
44
|
+
* incrementals are worthless without it — and the replay says truncated.
|
|
45
|
+
*/
|
|
46
|
+
export function wholeWindows(events, hardBytes) {
|
|
47
|
+
const out = [];
|
|
48
|
+
let dropped = false;
|
|
49
|
+
let window = [];
|
|
50
|
+
let tooBig = false;
|
|
51
|
+
const close = () => {
|
|
52
|
+
if (tooBig)
|
|
53
|
+
dropped = true;
|
|
54
|
+
else
|
|
55
|
+
out.push(...window);
|
|
56
|
+
window = [];
|
|
57
|
+
tooBig = false;
|
|
58
|
+
};
|
|
59
|
+
for (const ev of events) {
|
|
60
|
+
if (ev.type === RRWEB_META && window.length)
|
|
61
|
+
close();
|
|
62
|
+
if (!tooBig && utf8Bytes(JSON.stringify(ev)) + 1 > hardBytes)
|
|
63
|
+
tooBig = true;
|
|
64
|
+
window.push(ev);
|
|
65
|
+
}
|
|
66
|
+
close();
|
|
67
|
+
return { events: out, dropped };
|
|
68
|
+
}
|
|
69
|
+
/** What the flight buffer keeps of each lane event under the profile's classes: the wire's shapes only. */
|
|
70
|
+
function laneKeeper(lanes) {
|
|
71
|
+
return (kind, payload) => {
|
|
72
|
+
if (kind === 'console') {
|
|
73
|
+
const level = str(payload?.level, 'error');
|
|
74
|
+
if (level === 'error' ? !(lanes.errors || lanes.console) : !lanes.console)
|
|
75
|
+
return null;
|
|
76
|
+
return { level, text: str(payload?.text).slice(0, MAX_CONSOLE_TEXT) };
|
|
77
|
+
}
|
|
78
|
+
if (kind === 'net') {
|
|
79
|
+
const status = typeof payload?.status === 'number' ? payload.status : 0;
|
|
80
|
+
const failed = status === 0 || status >= 400 || payload?.error !== undefined;
|
|
81
|
+
if (!(lanes.network || (lanes.errors && failed)))
|
|
82
|
+
return null;
|
|
83
|
+
return {
|
|
84
|
+
method: str(payload?.method, 'GET').toUpperCase(),
|
|
85
|
+
path: reportPath(payload?.url),
|
|
86
|
+
status,
|
|
87
|
+
ms: typeof payload?.ms === 'number' ? Math.max(0, Math.round(payload.ms)) : 0,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async function fetchConfig(target) {
|
|
94
|
+
try {
|
|
95
|
+
const res = await api('GET', '/api/v1/report-config', undefined, { target });
|
|
96
|
+
const flight = typeof res.flightRecorder === 'boolean' ? res.flightRecorder : undefined;
|
|
97
|
+
return {
|
|
98
|
+
project: str(res.project),
|
|
99
|
+
profile: res.captureProfile == null ? legacyProfile(flight) : parseProfile(res.captureProfile, flight),
|
|
100
|
+
masking: maskingOf(res.profile),
|
|
101
|
+
reachable: true,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
console.warn('vitrinka: report config unavailable — capturing the last minute, strictly masked', e);
|
|
106
|
+
return { project: '', profile: legacyProfile(), masking: 'strict', reachable: false };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** rrweb's mirror off the running recorder, when it carries one (the vendor build may not). */
|
|
110
|
+
function mirrorOf(record) {
|
|
111
|
+
const mirror = record?.mirror;
|
|
112
|
+
if (typeof mirror !== 'object' || mirror === null)
|
|
113
|
+
return null;
|
|
114
|
+
const getId = mirror.getId;
|
|
115
|
+
return typeof getId === 'function' ? { getId: (n) => Number(getId.call(mirror, n)) } : null;
|
|
116
|
+
}
|
|
117
|
+
export class CaptureEngine {
|
|
118
|
+
opts;
|
|
119
|
+
ready;
|
|
120
|
+
store = null;
|
|
121
|
+
stopped = false;
|
|
122
|
+
/** The release token of this engine's claim on the page's capture runtime. */
|
|
123
|
+
claim = null;
|
|
124
|
+
/** The config `ready` resolved with — kept true to what the engine does now. */
|
|
125
|
+
config = null;
|
|
126
|
+
listeners = new Set();
|
|
127
|
+
/** …and runs it now: false before the claim, without it, and after it yielded. */
|
|
128
|
+
capturing = false;
|
|
129
|
+
/** The engine's own lifetime (its base registration). */
|
|
130
|
+
offs = [];
|
|
131
|
+
/** The capture runtime's teardown, run when it yields to a recorder or stops. */
|
|
132
|
+
capture = [];
|
|
133
|
+
constructor(opts) {
|
|
134
|
+
this.opts = opts;
|
|
135
|
+
this.ready = this.start();
|
|
136
|
+
}
|
|
137
|
+
async start() {
|
|
138
|
+
const { opts } = this;
|
|
139
|
+
// Every call is targeted (ApiTarget): the recorder's configuration is
|
|
140
|
+
// never written. Its lanes skip this base's traffic all the same.
|
|
141
|
+
this.offs.push(addOwnBase(opts.base));
|
|
142
|
+
const fetched = await fetchConfig({ url: opts.base, key: opts.key });
|
|
143
|
+
const profile = narrowProfile(fetched.profile, opts.capture);
|
|
144
|
+
const lanes = lanesOf(profile.classes);
|
|
145
|
+
const config = {
|
|
146
|
+
project: fetched.project,
|
|
147
|
+
profile,
|
|
148
|
+
lanes,
|
|
149
|
+
masking: fetched.masking,
|
|
150
|
+
reachable: fetched.reachable,
|
|
151
|
+
capturing: false,
|
|
152
|
+
};
|
|
153
|
+
if (this.stopped)
|
|
154
|
+
return config;
|
|
155
|
+
// The page-wide runtime (lanes, buffer, rrweb, rules) is the recorder's
|
|
156
|
+
// whenever one is mounted; this widget then reports without a capture.
|
|
157
|
+
this.config = config;
|
|
158
|
+
this.claim = claimCapture('report', () => this.standDown());
|
|
159
|
+
if (!this.claim) {
|
|
160
|
+
console.warn('vitrinka: the recorder runs this page’s capture — Report a problem sends without one');
|
|
161
|
+
return config;
|
|
162
|
+
}
|
|
163
|
+
this.capturing = true;
|
|
164
|
+
config.capturing = true;
|
|
165
|
+
setTabIdentity(browserTabId(), globalThis.location?.host ?? '');
|
|
166
|
+
currentRoute.pathname = globalThis.location?.pathname ?? '/';
|
|
167
|
+
setRedactionPolicy(maskingPolicy(config.masking));
|
|
168
|
+
setCaptureSanitizer(opts.sanitizeUrl);
|
|
169
|
+
this.capture.push(() => setCaptureSanitizer(null));
|
|
170
|
+
const windowMs = profile.windowSec * 1000;
|
|
171
|
+
// What stored windows were captured FOR: another project, or a laxer
|
|
172
|
+
// masking or wider classes than now, never comes back (sweep drops it).
|
|
173
|
+
const scope = [opts.base, fetched.project, config.masking, profile.classes.join(',')].join('|');
|
|
174
|
+
let prior = [];
|
|
175
|
+
let priorLanes = [];
|
|
176
|
+
if (profile.storage === 'browser') {
|
|
177
|
+
try {
|
|
178
|
+
this.store = await openCaptureStore();
|
|
179
|
+
const kept = await this.store.sweep(Date.now(), windowMs, currentRoute.tabId, scope);
|
|
180
|
+
// Replay windows and lane spans both: the buffer expires each in its turn.
|
|
181
|
+
prior = kept;
|
|
182
|
+
// The console and network entries of earlier loads, replay or not.
|
|
183
|
+
const store = this.store;
|
|
184
|
+
const spans = await Promise.all(kept.filter((w) => w.kind === 'lanes').map((w) => store.read(w.id)));
|
|
185
|
+
priorLanes = spans.flatMap((s) => s.lanes).sort((a, b) => Date.parse(a.ts) - Date.parse(b.ts));
|
|
186
|
+
}
|
|
187
|
+
catch (e) {
|
|
188
|
+
console.warn('vitrinka: browser capture storage unavailable — keeping the capture in memory', e);
|
|
189
|
+
this.store = null;
|
|
190
|
+
prior = [];
|
|
191
|
+
priorLanes = [];
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
// Memory means memory: nothing a browser profile once stored stays behind.
|
|
196
|
+
deleteCaptureStore();
|
|
197
|
+
}
|
|
198
|
+
if (!this.capturing) {
|
|
199
|
+
this.store?.close();
|
|
200
|
+
this.store = null;
|
|
201
|
+
return config;
|
|
202
|
+
}
|
|
203
|
+
startFlightBuffer({
|
|
204
|
+
windowMs,
|
|
205
|
+
checkoutMs: Math.min(MAX_CHECKOUT_MS, windowMs),
|
|
206
|
+
maxBytes: profile.maxBytes,
|
|
207
|
+
maxEvents: Math.max(20_000, Math.round(profile.maxBytes / 200)),
|
|
208
|
+
maxLaneEvents: FLIGHT_MAX_LANE_EVENTS,
|
|
209
|
+
keepLane: laneKeeper(lanes),
|
|
210
|
+
store: this.store,
|
|
211
|
+
scope,
|
|
212
|
+
prior,
|
|
213
|
+
priorLanes,
|
|
214
|
+
});
|
|
215
|
+
this.capture.push(stopFlightBuffer);
|
|
216
|
+
if (this.store && typeof window !== 'undefined') {
|
|
217
|
+
const persist = () => persistFlightNow();
|
|
218
|
+
this.capture.push(listen(window, 'pagehide', persist), listen(document, 'visibilitychange', persist));
|
|
219
|
+
}
|
|
220
|
+
// Each acquisition names its own settings, so nothing of the report face
|
|
221
|
+
// outlives its release into the next face that patches these lanes.
|
|
222
|
+
if (lanes.errors || lanes.console) {
|
|
223
|
+
patchConsole({ levels: lanes.console ? ['warn', 'info', 'log'] : [] });
|
|
224
|
+
this.capture.push(unpatchConsole);
|
|
225
|
+
}
|
|
226
|
+
if (lanes.errors || lanes.network) {
|
|
227
|
+
patchNetwork({ detail: 'meta' });
|
|
228
|
+
this.capture.push(unpatchNetwork);
|
|
229
|
+
}
|
|
230
|
+
if (lanes.replay) {
|
|
231
|
+
const record = await opts.loadRecord(opts.base).catch((e) => {
|
|
232
|
+
// A report without a replay is still a report: never block the card.
|
|
233
|
+
console.warn('vitrinka: the page recorder did not load — reports go without a replay', e);
|
|
234
|
+
return null;
|
|
235
|
+
});
|
|
236
|
+
if (record && this.capturing) {
|
|
237
|
+
startRecordWith(record, 'flight', { unmaskSelector: UNMASK_SELECTOR });
|
|
238
|
+
this.capture.push(stopRRWeb);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return config;
|
|
242
|
+
}
|
|
243
|
+
/** Hear the engine's state change (it stopped capturing): the widget repaints. */
|
|
244
|
+
onChange(fn) {
|
|
245
|
+
this.listeners.add(fn);
|
|
246
|
+
return () => this.listeners.delete(fn);
|
|
247
|
+
}
|
|
248
|
+
/** The recorder mounted: hand it the runtime — this widget goes on reporting without a capture. */
|
|
249
|
+
standDown() {
|
|
250
|
+
if (!this.capturing)
|
|
251
|
+
return;
|
|
252
|
+
this.capturing = false;
|
|
253
|
+
if (this.config)
|
|
254
|
+
this.config.capturing = false;
|
|
255
|
+
for (const off of this.capture.splice(0).reverse())
|
|
256
|
+
off();
|
|
257
|
+
const store = this.store;
|
|
258
|
+
this.store = null;
|
|
259
|
+
if (store)
|
|
260
|
+
void flightWrites().then(() => store.close());
|
|
261
|
+
for (const fn of this.listeners)
|
|
262
|
+
fn();
|
|
263
|
+
}
|
|
264
|
+
/** Freeze the capture now (the card opened); the buffer rolls on. */
|
|
265
|
+
freeze() {
|
|
266
|
+
// Not this widget's runtime (the recorder runs it): nothing to take.
|
|
267
|
+
if (!this.capturing)
|
|
268
|
+
return { clip: null, stored: Promise.resolve(new Map()) };
|
|
269
|
+
currentRoute.pathname = globalThis.location?.pathname ?? currentRoute.pathname;
|
|
270
|
+
let clip = takeFlightClip();
|
|
271
|
+
// No screen of this page yet (the recorder only just started, a window
|
|
272
|
+
// went over the cap): take one now so the report shows the current view.
|
|
273
|
+
if (clip && runningRecord() && !clipHasSnapshot(clip)) {
|
|
274
|
+
checkoutRRWeb();
|
|
275
|
+
clip = takeFlightClip();
|
|
276
|
+
}
|
|
277
|
+
// Earlier loads' windows are read NOW: the buffer keeps rolling while the
|
|
278
|
+
// reporter writes and may expire them before Send.
|
|
279
|
+
const store = this.store;
|
|
280
|
+
const ids = clip?.windows.filter((w) => w.stored).map((w) => w.id) ?? [];
|
|
281
|
+
const stored = store && ids.length ? Promise.all(ids.map(async (id) => [id, await store.read(id)])).then((rows) => new Map(rows)) : Promise.resolve(new Map());
|
|
282
|
+
stored.catch(() => undefined); // build reports a failed read
|
|
283
|
+
return { clip, stored };
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The rrweb ids of the page nodes under a hide right now — taken when the
|
|
287
|
+
* hide is drawn (the page sits frozen under the marking layer); null when
|
|
288
|
+
* the running recorder has no mirror to say (then a hide withholds the replay).
|
|
289
|
+
*/
|
|
290
|
+
hiddenIdsAt(rect) {
|
|
291
|
+
if (!this.capturing)
|
|
292
|
+
return null;
|
|
293
|
+
const mirror = mirrorOf(runningRecord());
|
|
294
|
+
return mirror ? hiddenNodeIds([rect], mirror) : null;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* What one report sends: the frozen windows (stored ones read back), the
|
|
298
|
+
* hides applied, split under the chunk cap. `markedIds` are the nodes each
|
|
299
|
+
* hide covered when it was drawn; the nodes under it at send join them. A
|
|
300
|
+
* hide whose nodes cannot be named (no mirror) withholds the replay — fail
|
|
301
|
+
* closed, never a box over text that still rides along.
|
|
302
|
+
*/
|
|
303
|
+
async build(frozen, markers, markedIds = new Set()) {
|
|
304
|
+
const hides = markers.filter((m) => m.shape === 'hide').map((m) => m.rect);
|
|
305
|
+
const mirror = mirrorOf(runningRecord());
|
|
306
|
+
const withheld = hides.length > 0 && !mirror;
|
|
307
|
+
if (withheld)
|
|
308
|
+
console.warn('vitrinka: this page recorder cannot name what a hide covers — the report goes without its replay');
|
|
309
|
+
const hiddenIds = new Set(markedIds);
|
|
310
|
+
if (hides.length && mirror)
|
|
311
|
+
for (const id of hiddenNodeIds(hides, mirror))
|
|
312
|
+
hiddenIds.add(id);
|
|
313
|
+
const events = [];
|
|
314
|
+
let truncated = frozen.clip?.truncated ?? false;
|
|
315
|
+
const pinned = await frozen.stored.catch((e) => {
|
|
316
|
+
console.warn('vitrinka: the stored part of the capture could not be read — the replay starts later', e);
|
|
317
|
+
return null;
|
|
318
|
+
});
|
|
319
|
+
for (const w of frozen.clip?.windows ?? []) {
|
|
320
|
+
if (!w.stored) {
|
|
321
|
+
events.push(...w.events);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
const read = pinned?.get(w.id);
|
|
325
|
+
// Gone before the card opened, or unreadable: say the replay is short.
|
|
326
|
+
if (!read?.events.length)
|
|
327
|
+
truncated = true;
|
|
328
|
+
else
|
|
329
|
+
events.push(...read.events);
|
|
330
|
+
}
|
|
331
|
+
// The console and network entries, earlier loads' included (priorLanes).
|
|
332
|
+
const lanes = frozen.clip?.lanes ?? [];
|
|
333
|
+
const whole = withheld ? { events: [], dropped: false } : wholeWindows(applyHides(events, hides, hiddenIds), CHUNK_HARD_BYTES);
|
|
334
|
+
if (whole.dropped)
|
|
335
|
+
truncated = true;
|
|
336
|
+
const { parts, dropped } = splitRRWebEvents(whole.events, CHUNK_PACK_BYTES, CHUNK_HARD_BYTES);
|
|
337
|
+
const chunks = [];
|
|
338
|
+
let at = 0;
|
|
339
|
+
for (const p of parts) {
|
|
340
|
+
at += p.count;
|
|
341
|
+
const last = whole.events[at - 1]?.timestamp ?? Date.now();
|
|
342
|
+
chunks.push({ seq: chunks.length + 1, ts: new Date(last).toISOString(), count: p.count, body: p.body });
|
|
343
|
+
}
|
|
344
|
+
const consoleEntries = lanes
|
|
345
|
+
.filter((e) => e.kind === 'console')
|
|
346
|
+
.slice(-MAX_ENTRIES)
|
|
347
|
+
.map((e) => ({ level: str(e.payload?.level, 'error'), ts: e.ts, text: str(e.payload?.text) }));
|
|
348
|
+
const network = lanes
|
|
349
|
+
.filter((e) => e.kind === 'net')
|
|
350
|
+
.slice(-MAX_ENTRIES)
|
|
351
|
+
.map((e) => ({
|
|
352
|
+
method: str(e.payload?.method, 'GET'),
|
|
353
|
+
path: str(e.payload?.path),
|
|
354
|
+
status: typeof e.payload?.status === 'number' ? e.payload.status : 0,
|
|
355
|
+
ms: typeof e.payload?.ms === 'number' ? e.payload.ms : 0,
|
|
356
|
+
}));
|
|
357
|
+
return { chunks, truncated: truncated || dropped.length > 0, console: consoleEntries, network };
|
|
358
|
+
}
|
|
359
|
+
/** A report went out: forget what it carried, in memory and in the store. */
|
|
360
|
+
clear() {
|
|
361
|
+
if (this.capturing)
|
|
362
|
+
clearFlight();
|
|
363
|
+
}
|
|
364
|
+
stop() {
|
|
365
|
+
this.stopped = true;
|
|
366
|
+
this.listeners.clear();
|
|
367
|
+
this.standDown();
|
|
368
|
+
releaseCapture(this.claim);
|
|
369
|
+
this.claim = null;
|
|
370
|
+
for (const off of this.offs.splice(0).reverse())
|
|
371
|
+
off();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
let shared = null;
|
|
375
|
+
/** The page's one engine (started by the first widget); null when another base/key already owns it. */
|
|
376
|
+
export function acquireEngine(opts) {
|
|
377
|
+
if (shared) {
|
|
378
|
+
const owner = shared.engine.opts;
|
|
379
|
+
// Sharing means one capture: only for a widget asking exactly what the
|
|
380
|
+
// first one did — never another target, narrowing or URL sanitizer.
|
|
381
|
+
const same = owner.base === opts.base &&
|
|
382
|
+
owner.key === opts.key &&
|
|
383
|
+
owner.sanitizeUrl === opts.sanitizeUrl &&
|
|
384
|
+
JSON.stringify(owner.capture ?? {}) === JSON.stringify(opts.capture ?? {});
|
|
385
|
+
if (!same) {
|
|
386
|
+
console.warn('vitrinka: one capture engine per page — this widget reports without a capture (another widget’s key, base, capture or sanitizeUrl owns it)');
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
shared.refs++;
|
|
390
|
+
return shared.engine;
|
|
391
|
+
}
|
|
392
|
+
shared = { engine: new CaptureEngine(opts), refs: 1 };
|
|
393
|
+
return shared.engine;
|
|
394
|
+
}
|
|
395
|
+
export function releaseEngine(engine) {
|
|
396
|
+
if (!engine || !shared || shared.engine !== engine)
|
|
397
|
+
return;
|
|
398
|
+
if (--shared.refs > 0)
|
|
399
|
+
return;
|
|
400
|
+
shared = null;
|
|
401
|
+
engine.stop();
|
|
402
|
+
}
|