@oodle-ai/mobile-sdk-reactnative 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/OodleMobileSdkReactNative.podspec +24 -0
- package/README.md +191 -2
- package/android/build.gradle +55 -0
- package/android/proguard-rules.pro +5 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/ai/oodle/rum/Connectivity.kt +19 -0
- package/android/src/main/java/ai/oodle/rum/CrashReporter.kt +88 -0
- package/android/src/main/java/ai/oodle/rum/DeviceInfo.kt +50 -0
- package/android/src/main/java/ai/oodle/rum/ExitReasons.kt +104 -0
- package/android/src/main/java/ai/oodle/rum/NativeEvents.kt +24 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumModule.kt +96 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumPackage.kt +26 -0
- package/android/src/main/java/ai/oodle/rum/Storage.kt +84 -0
- package/android/src/main/java/ai/oodle/rum/Vitals.kt +188 -0
- package/android/src/main/java/ai/oodle/rum/Watchdog.kt +81 -0
- package/android/src/main/java/ai/oodle/rum/WireframeCapture.kt +398 -0
- package/bin/oodle-rum-upload.mjs +104 -0
- package/ios/OodleCrashReporter.h +16 -0
- package/ios/OodleCrashReporter.m +202 -0
- package/ios/OodleRum.h +18 -0
- package/ios/OodleRum.mm +129 -0
- package/ios/OodleRumImpl.swift +450 -0
- package/ios/WireframeWalker.swift +279 -0
- package/lib/core/clock.d.ts +19 -0
- package/lib/core/clock.js +47 -0
- package/lib/core/clock.js.map +1 -0
- package/lib/core/config.d.ts +142 -0
- package/lib/core/config.js +178 -0
- package/lib/core/config.js.map +1 -0
- package/lib/core/consent.d.ts +5 -0
- package/lib/core/consent.js +33 -0
- package/lib/core/consent.js.map +1 -0
- package/lib/core/context.d.ts +36 -0
- package/lib/core/context.js +172 -0
- package/lib/core/context.js.map +1 -0
- package/lib/core/flags.d.ts +3 -0
- package/lib/core/flags.js +20 -0
- package/lib/core/flags.js.map +1 -0
- package/lib/core/ids.d.ts +7 -0
- package/lib/core/ids.js +40 -0
- package/lib/core/ids.js.map +1 -0
- package/lib/core/internal.d.ts +6 -0
- package/lib/core/internal.js +10 -0
- package/lib/core/internal.js.map +1 -0
- package/lib/core/logger.d.ts +8 -0
- package/lib/core/logger.js +38 -0
- package/lib/core/logger.js.map +1 -0
- package/lib/core/native.d.ts +71 -0
- package/lib/core/native.js +156 -0
- package/lib/core/native.js.map +1 -0
- package/lib/core/rate-limiter.d.ts +7 -0
- package/lib/core/rate-limiter.js +31 -0
- package/lib/core/rate-limiter.js.map +1 -0
- package/lib/core/session.d.ts +51 -0
- package/lib/core/session.js +196 -0
- package/lib/core/session.js.map +1 -0
- package/lib/core/size.d.ts +15 -0
- package/lib/core/size.js +68 -0
- package/lib/core/size.js.map +1 -0
- package/lib/core/tags.d.ts +10 -0
- package/lib/core/tags.js +33 -0
- package/lib/core/tags.js.map +1 -0
- package/lib/core/telemetry.d.ts +6 -0
- package/lib/core/telemetry.js +37 -0
- package/lib/core/telemetry.js.map +1 -0
- package/lib/core/tracing.d.ts +9 -0
- package/lib/core/tracing.js +57 -0
- package/lib/core/tracing.js.map +1 -0
- package/lib/core/transport.d.ts +76 -0
- package/lib/core/transport.js +560 -0
- package/lib/core/transport.js.map +1 -0
- package/lib/core/user.d.ts +12 -0
- package/lib/core/user.js +41 -0
- package/lib/core/user.js.map +1 -0
- package/lib/events/actions.d.ts +13 -0
- package/lib/events/actions.js +147 -0
- package/lib/events/actions.js.map +1 -0
- package/lib/events/emit.d.ts +21 -0
- package/lib/events/emit.js +144 -0
- package/lib/events/emit.js.map +1 -0
- package/lib/events/errors.d.ts +19 -0
- package/lib/events/errors.js +257 -0
- package/lib/events/errors.js.map +1 -0
- package/lib/events/lifecycle.d.ts +7 -0
- package/lib/events/lifecycle.js +101 -0
- package/lib/events/lifecycle.js.map +1 -0
- package/lib/events/longtasks.d.ts +2 -0
- package/lib/events/longtasks.js +42 -0
- package/lib/events/longtasks.js.map +1 -0
- package/lib/events/resources.d.ts +15 -0
- package/lib/events/resources.js +156 -0
- package/lib/events/resources.js.map +1 -0
- package/lib/events/tap-target.d.ts +42 -0
- package/lib/events/tap-target.js +191 -0
- package/lib/events/tap-target.js.map +1 -0
- package/lib/events/view-state.d.ts +13 -0
- package/lib/events/view-state.js +25 -0
- package/lib/events/view-state.js.map +1 -0
- package/lib/events/views.d.ts +21 -0
- package/lib/events/views.js +347 -0
- package/lib/events/views.js.map +1 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +24 -0
- package/lib/index.js.map +1 -0
- package/lib/navigation/react-navigation.d.ts +33 -0
- package/lib/navigation/react-navigation.js +61 -0
- package/lib/navigation/react-navigation.js.map +1 -0
- package/lib/react/OodlePrivacy.d.ts +15 -0
- package/lib/react/OodlePrivacy.js +13 -0
- package/lib/react/OodlePrivacy.js.map +1 -0
- package/lib/react/OodleProvider.d.ts +18 -0
- package/lib/react/OodleProvider.js +72 -0
- package/lib/react/OodleProvider.js.map +1 -0
- package/lib/replay/recorder.d.ts +22 -0
- package/lib/replay/recorder.js +317 -0
- package/lib/replay/recorder.js.map +1 -0
- package/lib/replay/rrweb.d.ts +105 -0
- package/lib/replay/rrweb.js +335 -0
- package/lib/replay/rrweb.js.map +1 -0
- package/lib/rum.d.ts +52 -0
- package/lib/rum.js +281 -0
- package/lib/rum.js.map +1 -0
- package/lib/specs/NativeOodleRum.d.ts +43 -0
- package/lib/specs/NativeOodleRum.js +5 -0
- package/lib/specs/NativeOodleRum.js.map +1 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +6 -0
- package/lib/version.js.map +1 -0
- package/package.json +91 -4
- package/react-native.config.js +10 -0
- package/src/core/clock.ts +43 -0
- package/src/core/config.ts +344 -0
- package/src/core/consent.ts +32 -0
- package/src/core/context.ts +189 -0
- package/src/core/flags.ts +15 -0
- package/src/core/ids.ts +35 -0
- package/src/core/internal.ts +6 -0
- package/src/core/logger.ts +37 -0
- package/src/core/native.ts +201 -0
- package/src/core/rate-limiter.ts +34 -0
- package/src/core/session.ts +209 -0
- package/src/core/size.ts +79 -0
- package/src/core/tags.ts +30 -0
- package/src/core/telemetry.ts +35 -0
- package/src/core/tracing.ts +60 -0
- package/src/core/transport.ts +590 -0
- package/src/core/user.ts +45 -0
- package/src/events/actions.ts +157 -0
- package/src/events/emit.ts +131 -0
- package/src/events/errors.ts +283 -0
- package/src/events/lifecycle.ts +102 -0
- package/src/events/longtasks.ts +40 -0
- package/src/events/resources.ts +167 -0
- package/src/events/tap-target.ts +210 -0
- package/src/events/view-state.ts +30 -0
- package/src/events/views.ts +356 -0
- package/src/index.ts +34 -0
- package/src/navigation/react-navigation.ts +72 -0
- package/src/react/OodlePrivacy.tsx +25 -0
- package/src/react/OodleProvider.tsx +88 -0
- package/src/replay/recorder.ts +310 -0
- package/src/replay/rrweb.ts +460 -0
- package/src/rum.ts +295 -0
- package/src/specs/NativeOodleRum.ts +51 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns native wireframe snapshots into rrweb events, so the replay player
|
|
3
|
+
* that plays browser sessions plays mobile sessions without change.
|
|
4
|
+
*
|
|
5
|
+
* Each wireframe becomes one absolutely positioned <div> under <body>, in
|
|
6
|
+
* draw order. The first snapshot is an rrweb full snapshot. Later snapshots
|
|
7
|
+
* are compared with the one before and sent as a mutation: removed views,
|
|
8
|
+
* added views, changed styles and changed text.
|
|
9
|
+
*/
|
|
10
|
+
import type { TextAndInputPrivacy } from '../core/config';
|
|
11
|
+
|
|
12
|
+
export type WireframeKind = 's' | 't' | 'i' | 'in' | 'p' | 'h' | 'sw' | 'wv';
|
|
13
|
+
|
|
14
|
+
export interface Wireframe {
|
|
15
|
+
/** Stable for the life of the native view. */
|
|
16
|
+
id: number;
|
|
17
|
+
k: WireframeKind;
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
w: number;
|
|
21
|
+
h: number;
|
|
22
|
+
/** Insets (top, right, bottom, left) that a clipping parent hides. */
|
|
23
|
+
c?: [number, number, number, number];
|
|
24
|
+
bg?: string;
|
|
25
|
+
bc?: string;
|
|
26
|
+
bw?: number;
|
|
27
|
+
r?: number;
|
|
28
|
+
o?: number;
|
|
29
|
+
tx?: string;
|
|
30
|
+
/** Hint shown by an empty text input. */
|
|
31
|
+
ph?: string;
|
|
32
|
+
tc?: string;
|
|
33
|
+
fs?: number;
|
|
34
|
+
fw?: number;
|
|
35
|
+
ta?: 'left' | 'center' | 'right';
|
|
36
|
+
/** Switch state. */
|
|
37
|
+
on?: boolean;
|
|
38
|
+
/** Privacy override from an OodlePrivacy wrapper. */
|
|
39
|
+
pv?: 'mask' | 'allow';
|
|
40
|
+
/** Password, email, phone and similar inputs. */
|
|
41
|
+
sen?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface WireframeSnapshot {
|
|
45
|
+
width: number;
|
|
46
|
+
height: number;
|
|
47
|
+
nodes: Wireframe[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// rrweb constants (rrweb 2.x, @rrweb/types).
|
|
51
|
+
export const EventType = { FullSnapshot: 2, IncrementalSnapshot: 3, Meta: 4 } as const;
|
|
52
|
+
export const IncrementalSource = {
|
|
53
|
+
Mutation: 0,
|
|
54
|
+
MouseMove: 1,
|
|
55
|
+
MouseInteraction: 2,
|
|
56
|
+
ViewportResize: 4,
|
|
57
|
+
TouchMove: 6,
|
|
58
|
+
} as const;
|
|
59
|
+
export const MouseInteraction = { Click: 2, TouchStart: 7, TouchEnd: 9 } as const;
|
|
60
|
+
const NodeType = { Document: 0, DocumentType: 1, Element: 2, Text: 3 } as const;
|
|
61
|
+
|
|
62
|
+
export interface RRWebEvent {
|
|
63
|
+
type: number;
|
|
64
|
+
data: Record<string, unknown>;
|
|
65
|
+
timestamp: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface SerializedNode {
|
|
69
|
+
type: number;
|
|
70
|
+
id: number;
|
|
71
|
+
tagName?: string;
|
|
72
|
+
attributes?: Record<string, string>;
|
|
73
|
+
childNodes?: SerializedNode[];
|
|
74
|
+
textContent?: string;
|
|
75
|
+
name?: string;
|
|
76
|
+
publicId?: string;
|
|
77
|
+
systemId?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const DOCUMENT_ID = 1;
|
|
81
|
+
const DOCTYPE_ID = 2;
|
|
82
|
+
const HTML_ID = 3;
|
|
83
|
+
const HEAD_ID = 4;
|
|
84
|
+
export const BODY_ID = 5;
|
|
85
|
+
const FIRST_NODE_ID = 6;
|
|
86
|
+
|
|
87
|
+
const FONT =
|
|
88
|
+
"-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif";
|
|
89
|
+
|
|
90
|
+
export interface PrivacyOptions {
|
|
91
|
+
textAndInputPrivacy: TextAndInputPrivacy;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface Rendered {
|
|
95
|
+
nid: number;
|
|
96
|
+
kind: WireframeKind;
|
|
97
|
+
style: string;
|
|
98
|
+
text: string | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface Tracked extends Rendered {
|
|
102
|
+
rid: number;
|
|
103
|
+
tid: number | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function px(n: number): string {
|
|
107
|
+
return `${Math.round(n * 10) / 10}px`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function maskText(s: string): string {
|
|
111
|
+
return s.replace(/[^\s]/g, 'x');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Applies the privacy level to the text a wireframe would show. */
|
|
115
|
+
export function visibleText(w: Wireframe, privacy: PrivacyOptions): string | null {
|
|
116
|
+
const level = privacy.textAndInputPrivacy;
|
|
117
|
+
if (w.k === 't') {
|
|
118
|
+
const text = w.tx ?? '';
|
|
119
|
+
if (!text) return null;
|
|
120
|
+
const masked = w.pv === 'mask' || (level === 'mask-all' && w.pv !== 'allow');
|
|
121
|
+
return masked ? maskText(text) : text;
|
|
122
|
+
}
|
|
123
|
+
if (w.k === 'in') {
|
|
124
|
+
const value = w.tx ?? '';
|
|
125
|
+
if (value) {
|
|
126
|
+
// A sensitive field is masked at every level, allow included.
|
|
127
|
+
const masked =
|
|
128
|
+
w.sen === true ||
|
|
129
|
+
w.pv === 'mask' ||
|
|
130
|
+
(level !== 'mask-sensitive-inputs' && w.pv !== 'allow');
|
|
131
|
+
return masked ? '***' : value;
|
|
132
|
+
}
|
|
133
|
+
const hint = w.ph ?? '';
|
|
134
|
+
if (!hint) return null;
|
|
135
|
+
const maskHint = w.pv === 'mask' || (level === 'mask-all' && w.pv !== 'allow');
|
|
136
|
+
return maskHint ? maskText(hint) : hint;
|
|
137
|
+
}
|
|
138
|
+
if (w.k === 'h') return 'Hidden';
|
|
139
|
+
if (w.k === 'wv') return 'Web view';
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function styleFor(w: Wireframe): string {
|
|
144
|
+
const parts = [
|
|
145
|
+
'position:absolute',
|
|
146
|
+
`left:${px(w.x)}`,
|
|
147
|
+
`top:${px(w.y)}`,
|
|
148
|
+
`width:${px(w.w)}`,
|
|
149
|
+
`height:${px(w.h)}`,
|
|
150
|
+
'box-sizing:border-box',
|
|
151
|
+
'margin:0',
|
|
152
|
+
];
|
|
153
|
+
if (w.o !== undefined && w.o < 1) parts.push(`opacity:${Math.round(w.o * 100) / 100}`);
|
|
154
|
+
if (w.r) parts.push(`border-radius:${px(w.r)}`);
|
|
155
|
+
if (w.c) {
|
|
156
|
+
const [t, r, b, l] = w.c;
|
|
157
|
+
parts.push(`clip-path:inset(${px(t)} ${px(r)} ${px(b)} ${px(l)})`);
|
|
158
|
+
}
|
|
159
|
+
switch (w.k) {
|
|
160
|
+
case 'i':
|
|
161
|
+
parts.push('background-color:rgba(127,127,127,0.25)');
|
|
162
|
+
break;
|
|
163
|
+
case 'p':
|
|
164
|
+
case 'h':
|
|
165
|
+
case 'wv':
|
|
166
|
+
parts.push(
|
|
167
|
+
'background-color:rgba(127,127,127,0.18)',
|
|
168
|
+
'border:1px dashed rgba(127,127,127,0.6)',
|
|
169
|
+
'color:rgba(80,80,80,0.9)',
|
|
170
|
+
'font-size:11px',
|
|
171
|
+
`font-family:${FONT}`,
|
|
172
|
+
'display:flex',
|
|
173
|
+
'align-items:center',
|
|
174
|
+
'justify-content:center',
|
|
175
|
+
'overflow:hidden',
|
|
176
|
+
);
|
|
177
|
+
break;
|
|
178
|
+
case 'sw':
|
|
179
|
+
parts.push(
|
|
180
|
+
`background-color:${w.on ? (w.bg ?? '#34c759') : '#c7c7cc'}`,
|
|
181
|
+
`border-radius:${px(w.h / 2)}`,
|
|
182
|
+
);
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
if (w.bg) parts.push(`background-color:${w.bg}`);
|
|
186
|
+
}
|
|
187
|
+
if ((w.k === 's' || w.k === 't' || w.k === 'in') && w.bw && w.bw > 0) {
|
|
188
|
+
parts.push(`border:${px(w.bw)} solid ${w.bc ?? 'transparent'}`);
|
|
189
|
+
}
|
|
190
|
+
if (w.k === 't' || w.k === 'in') {
|
|
191
|
+
const align = w.ta ?? 'left';
|
|
192
|
+
parts.push(
|
|
193
|
+
`color:${w.tc ?? '#000000'}`,
|
|
194
|
+
`font-size:${px(w.fs ?? 14)}`,
|
|
195
|
+
`font-weight:${w.fw ?? 400}`,
|
|
196
|
+
`font-family:${FONT}`,
|
|
197
|
+
`text-align:${align}`,
|
|
198
|
+
'line-height:1.2',
|
|
199
|
+
'white-space:pre-wrap',
|
|
200
|
+
'overflow:hidden',
|
|
201
|
+
'word-break:break-word',
|
|
202
|
+
);
|
|
203
|
+
if (w.k === 'in') {
|
|
204
|
+
parts.push(
|
|
205
|
+
'display:flex',
|
|
206
|
+
'align-items:center',
|
|
207
|
+
`justify-content:${align === 'center' ? 'center' : align === 'right' ? 'flex-end' : 'flex-start'}`,
|
|
208
|
+
'padding:0 4px',
|
|
209
|
+
);
|
|
210
|
+
if (!w.tx && w.ph) parts.push('opacity:0.6');
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return parts.join(';');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function bodyStyle(width: number, height: number): string {
|
|
217
|
+
return [
|
|
218
|
+
'margin:0',
|
|
219
|
+
'padding:0',
|
|
220
|
+
'position:relative',
|
|
221
|
+
'overflow:hidden',
|
|
222
|
+
`width:${px(width)}`,
|
|
223
|
+
`height:${px(height)}`,
|
|
224
|
+
'background-color:#ffffff',
|
|
225
|
+
].join(';');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function render(snapshot: WireframeSnapshot, privacy: PrivacyOptions): Rendered[] {
|
|
229
|
+
const out: Rendered[] = [];
|
|
230
|
+
const seen = new Set<number>();
|
|
231
|
+
for (const w of snapshot.nodes) {
|
|
232
|
+
if (!w || typeof w.id !== 'number' || seen.has(w.id)) continue;
|
|
233
|
+
if (!(w.w > 0 && w.h > 0)) continue;
|
|
234
|
+
seen.add(w.id);
|
|
235
|
+
out.push({ nid: w.id, kind: w.k, style: styleFor(w), text: visibleText(w, privacy) });
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export interface DiffResult {
|
|
241
|
+
/** True when the result is a full snapshot rather than a mutation. */
|
|
242
|
+
full: boolean;
|
|
243
|
+
events: RRWebEvent[];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export class WireframeRecorder {
|
|
247
|
+
private nextId = FIRST_NODE_ID;
|
|
248
|
+
private tracked = new Map<number, Tracked>();
|
|
249
|
+
private order: number[] = [];
|
|
250
|
+
private width = 0;
|
|
251
|
+
private height = 0;
|
|
252
|
+
private hasFull = false;
|
|
253
|
+
|
|
254
|
+
constructor(private privacy: PrivacyOptions) {}
|
|
255
|
+
|
|
256
|
+
setPrivacy(p: PrivacyOptions) {
|
|
257
|
+
this.privacy = p;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Forgets the screen so the next snapshot is a full one. */
|
|
261
|
+
reset() {
|
|
262
|
+
this.tracked.clear();
|
|
263
|
+
this.order = [];
|
|
264
|
+
this.hasFull = false;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
get initialized(): boolean {
|
|
268
|
+
return this.hasFull;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private alloc(): number {
|
|
272
|
+
return this.nextId++;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private serialize(r: Rendered): { node: SerializedNode; tracked: Tracked } {
|
|
276
|
+
const rid = this.alloc();
|
|
277
|
+
const tid = r.text !== null ? this.alloc() : null;
|
|
278
|
+
const node: SerializedNode = {
|
|
279
|
+
type: NodeType.Element,
|
|
280
|
+
tagName: 'div',
|
|
281
|
+
attributes: { style: r.style },
|
|
282
|
+
childNodes:
|
|
283
|
+
tid !== null ? [{ type: NodeType.Text, textContent: r.text ?? '', id: tid }] : [],
|
|
284
|
+
id: rid,
|
|
285
|
+
};
|
|
286
|
+
return { node, tracked: { ...r, rid, tid } };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Returns the events for this snapshot: a Meta event and a full snapshot
|
|
291
|
+
* when `forceFull` is set, nothing is recorded yet, or the screen size or
|
|
292
|
+
* draw order changed; otherwise one mutation, or none when nothing changed.
|
|
293
|
+
*/
|
|
294
|
+
apply(
|
|
295
|
+
snapshot: WireframeSnapshot,
|
|
296
|
+
timestamp: number,
|
|
297
|
+
href: string,
|
|
298
|
+
forceFull = false,
|
|
299
|
+
): DiffResult {
|
|
300
|
+
const rendered = render(snapshot, this.privacy);
|
|
301
|
+
const sizeChanged = snapshot.width !== this.width || snapshot.height !== this.height;
|
|
302
|
+
if (forceFull || !this.hasFull || sizeChanged || this.orderChanged(rendered)) {
|
|
303
|
+
const events: RRWebEvent[] = [];
|
|
304
|
+
if (this.hasFull && sizeChanged) {
|
|
305
|
+
events.push({
|
|
306
|
+
type: EventType.IncrementalSnapshot,
|
|
307
|
+
data: {
|
|
308
|
+
source: IncrementalSource.ViewportResize,
|
|
309
|
+
width: snapshot.width,
|
|
310
|
+
height: snapshot.height,
|
|
311
|
+
},
|
|
312
|
+
timestamp,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
events.push(...this.full(snapshot, rendered, timestamp, href));
|
|
316
|
+
return { full: true, events };
|
|
317
|
+
}
|
|
318
|
+
const mutation = this.diff(rendered);
|
|
319
|
+
if (!mutation) return { full: false, events: [] };
|
|
320
|
+
return {
|
|
321
|
+
full: false,
|
|
322
|
+
events: [
|
|
323
|
+
{
|
|
324
|
+
type: EventType.IncrementalSnapshot,
|
|
325
|
+
data: { source: IncrementalSource.Mutation, ...mutation },
|
|
326
|
+
timestamp,
|
|
327
|
+
},
|
|
328
|
+
],
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private orderChanged(next: Rendered[]): boolean {
|
|
333
|
+
const nextIds = new Set(next.map((r) => r.nid));
|
|
334
|
+
const prevCommon = this.order.filter((id) => nextIds.has(id));
|
|
335
|
+
let i = 0;
|
|
336
|
+
for (const r of next) {
|
|
337
|
+
if (!this.tracked.has(r.nid)) continue;
|
|
338
|
+
if (prevCommon[i] !== r.nid) return true;
|
|
339
|
+
i++;
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private full(
|
|
345
|
+
snapshot: WireframeSnapshot,
|
|
346
|
+
rendered: Rendered[],
|
|
347
|
+
timestamp: number,
|
|
348
|
+
href: string,
|
|
349
|
+
): RRWebEvent[] {
|
|
350
|
+
this.tracked.clear();
|
|
351
|
+
this.order = [];
|
|
352
|
+
const children: SerializedNode[] = [];
|
|
353
|
+
for (const r of rendered) {
|
|
354
|
+
const { node, tracked } = this.serialize(r);
|
|
355
|
+
children.push(node);
|
|
356
|
+
this.tracked.set(r.nid, tracked);
|
|
357
|
+
this.order.push(r.nid);
|
|
358
|
+
}
|
|
359
|
+
this.width = snapshot.width;
|
|
360
|
+
this.height = snapshot.height;
|
|
361
|
+
this.hasFull = true;
|
|
362
|
+
const doc: SerializedNode = {
|
|
363
|
+
type: NodeType.Document,
|
|
364
|
+
id: DOCUMENT_ID,
|
|
365
|
+
childNodes: [
|
|
366
|
+
{ type: NodeType.DocumentType, name: 'html', publicId: '', systemId: '', id: DOCTYPE_ID },
|
|
367
|
+
{
|
|
368
|
+
type: NodeType.Element,
|
|
369
|
+
tagName: 'html',
|
|
370
|
+
attributes: {},
|
|
371
|
+
id: HTML_ID,
|
|
372
|
+
childNodes: [
|
|
373
|
+
{ type: NodeType.Element, tagName: 'head', attributes: {}, childNodes: [], id: HEAD_ID },
|
|
374
|
+
{
|
|
375
|
+
type: NodeType.Element,
|
|
376
|
+
tagName: 'body',
|
|
377
|
+
attributes: { style: bodyStyle(snapshot.width, snapshot.height) },
|
|
378
|
+
childNodes: children,
|
|
379
|
+
id: BODY_ID,
|
|
380
|
+
},
|
|
381
|
+
],
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
};
|
|
385
|
+
return [
|
|
386
|
+
{
|
|
387
|
+
type: EventType.Meta,
|
|
388
|
+
data: { href, width: Math.round(snapshot.width), height: Math.round(snapshot.height) },
|
|
389
|
+
timestamp,
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
type: EventType.FullSnapshot,
|
|
393
|
+
data: { node: doc, initialOffset: { top: 0, left: 0 } },
|
|
394
|
+
timestamp,
|
|
395
|
+
},
|
|
396
|
+
];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
private diff(next: Rendered[]): Record<string, unknown[]> | null {
|
|
400
|
+
const removes: Array<{ parentId: number; id: number }> = [];
|
|
401
|
+
const adds: Array<{ parentId: number; nextId: number | null; node: SerializedNode }> = [];
|
|
402
|
+
const attributes: Array<{ id: number; attributes: Record<string, string> }> = [];
|
|
403
|
+
const texts: Array<{ id: number; value: string }> = [];
|
|
404
|
+
|
|
405
|
+
const nextIds = new Set(next.map((r) => r.nid));
|
|
406
|
+
for (const id of this.order) {
|
|
407
|
+
if (nextIds.has(id)) continue;
|
|
408
|
+
const t = this.tracked.get(id)!;
|
|
409
|
+
removes.push({ parentId: BODY_ID, id: t.rid });
|
|
410
|
+
this.tracked.delete(id);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// A view whose kind changed is replaced, not edited.
|
|
414
|
+
for (const r of next) {
|
|
415
|
+
const t = this.tracked.get(r.nid);
|
|
416
|
+
if (t && t.kind !== r.kind) {
|
|
417
|
+
removes.push({ parentId: BODY_ID, id: t.rid });
|
|
418
|
+
this.tracked.delete(r.nid);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Adds go last to first, so each one's next sibling is already in place.
|
|
423
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
424
|
+
const r = next[i];
|
|
425
|
+
if (this.tracked.has(r.nid)) continue;
|
|
426
|
+
const following = i + 1 < next.length ? this.tracked.get(next[i + 1].nid) : undefined;
|
|
427
|
+
const { node, tracked } = this.serialize(r);
|
|
428
|
+
adds.push({ parentId: BODY_ID, nextId: following ? following.rid : null, node });
|
|
429
|
+
this.tracked.set(r.nid, tracked);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
for (const r of next) {
|
|
433
|
+
const t = this.tracked.get(r.nid)!;
|
|
434
|
+
if (t.style !== r.style) {
|
|
435
|
+
attributes.push({ id: t.rid, attributes: { style: r.style } });
|
|
436
|
+
t.style = r.style;
|
|
437
|
+
}
|
|
438
|
+
if (t.text === r.text) continue;
|
|
439
|
+
if (t.tid !== null && r.text !== null) {
|
|
440
|
+
texts.push({ id: t.tid, value: r.text });
|
|
441
|
+
} else if (t.tid !== null) {
|
|
442
|
+
removes.push({ parentId: t.rid, id: t.tid });
|
|
443
|
+
t.tid = null;
|
|
444
|
+
} else if (r.text !== null) {
|
|
445
|
+
const tid = this.alloc();
|
|
446
|
+
adds.push({
|
|
447
|
+
parentId: t.rid,
|
|
448
|
+
nextId: null,
|
|
449
|
+
node: { type: NodeType.Text, textContent: r.text, id: tid },
|
|
450
|
+
});
|
|
451
|
+
t.tid = tid;
|
|
452
|
+
}
|
|
453
|
+
t.text = r.text;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
this.order = next.map((r) => r.nid);
|
|
457
|
+
if (!removes.length && !adds.length && !attributes.length && !texts.length) return null;
|
|
458
|
+
return { texts, attributes, removes, adds };
|
|
459
|
+
}
|
|
460
|
+
}
|