@shotik/react-native 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +26 -0
- package/README.md +36 -1
- package/README.ru.md +37 -1
- package/dist/analytics/core.d.ts +96 -11
- package/dist/analytics/core.js +295 -22
- package/dist/analytics/privacy.d.ts +4 -0
- package/dist/analytics/privacy.js +46 -0
- package/dist/analytics/replay.d.ts +41 -0
- package/dist/analytics/replay.js +192 -0
- package/dist/analytics/rn.d.ts +52 -2
- package/dist/analytics/rn.js +669 -62
- package/dist/analytics/wire.d.ts +54 -2
- package/dist/analytics/wire.js +44 -4
- package/dist/client.d.ts +93 -0
- package/dist/client.js +247 -9
- package/dist/flow.d.ts +48 -0
- package/dist/flow.js +56 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/parts.d.ts +42 -1
- package/dist/parts.js +72 -14
- package/dist/react.d.ts +24 -3
- package/dist/react.js +42 -15
- package/dist/sheet.d.ts +2 -1
- package/dist/sheet.js +206 -15
- package/dist/support.js +2 -2
- package/dist/theme.d.ts +43 -0
- package/dist/theme.js +88 -2
- package/package.json +15 -2
- package/recipe.json +1 -1
- package/src/analytics/README.md +10 -6
- package/src/analytics/core.ts +314 -25
- package/src/analytics/privacy.ts +50 -0
- package/src/analytics/recipe.json +1 -1
- package/src/analytics/replay.ts +190 -0
- package/src/analytics/rn.tsx +620 -63
- package/src/analytics/wire.ts +99 -8
- package/src/client.ts +334 -9
- package/src/flow.ts +100 -0
- package/src/index.ts +4 -4
- package/src/parts.tsx +102 -14
- package/src/react.tsx +57 -16
- package/src/sheet.tsx +422 -19
- package/src/support.ts +2 -2
- package/src/theme.ts +88 -2
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Wireframe replay (PROTOCOL.md 4.7): turns walks into keyframe / diff chunks and decides where they go. A sampled session
|
|
2
|
+
// sends every chunk; any other session keeps the last 30 s in a ring (<= 1 MB, starting at a keyframe) and releases it
|
|
3
|
+
// only when a signal fires (rage, dead, unresponsive or error tap, error, crash). Platform-free, like core.ts.
|
|
4
|
+
|
|
5
|
+
import type { KeyedNode, ReplayChunk, ReplayRun, ReplayScroll, ReplayWhy } from './wire'
|
|
6
|
+
|
|
7
|
+
export interface ReplayWalk {
|
|
8
|
+
screen: string
|
|
9
|
+
w: number
|
|
10
|
+
h: number
|
|
11
|
+
nodes: KeyedNode[]
|
|
12
|
+
scroll: ReplayScroll[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// A built chunk with its JSON (encoded once, when it is made) and why it is being sent.
|
|
16
|
+
export interface Built {
|
|
17
|
+
c: ReplayChunk
|
|
18
|
+
s: string
|
|
19
|
+
key: boolean
|
|
20
|
+
why: ReplayWhy
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const RING_MS = 30_000
|
|
24
|
+
export const MAX_BYTES = 1 << 20
|
|
25
|
+
const TAIL_MS = 5000
|
|
26
|
+
const BODY = 64 * 1024
|
|
27
|
+
const KEY_SHARE = 0.6
|
|
28
|
+
const REKEY_MS = RING_MS / 2
|
|
29
|
+
// A later signal inside the 5 s tail relabels its release when it ranks higher (a rage burst first releases on its
|
|
30
|
+
// unresponsive taps).
|
|
31
|
+
const RANK: ReplayWhy[] = ['sample', 'unresponsive', 'dead', 'errtap', 'rage', 'error', 'crash']
|
|
32
|
+
|
|
33
|
+
const same = (a: KeyedNode, b: KeyedNode) => a[1] === b[1] && a[4] === b[4] && a[5] === b[5] && (a[6] ?? null) === (b[6] ?? null) && (a[7] ?? null) === (b[7] ?? null) && (a[8] ?? null) === (b[8] ?? null)
|
|
34
|
+
const at = (s: ReplayScroll) => (s.x ?? 0) + ',' + s.y
|
|
35
|
+
|
|
36
|
+
export class Replay {
|
|
37
|
+
out: Built[] = []
|
|
38
|
+
off = false
|
|
39
|
+
private ring: Built[] = []
|
|
40
|
+
private why: ReplayWhy = 'sample'
|
|
41
|
+
private sess = ''
|
|
42
|
+
private seq = 0
|
|
43
|
+
private prev: Map<string, KeyedNode> | null = null
|
|
44
|
+
private screen = ''
|
|
45
|
+
private offsets = new Map<string, string>()
|
|
46
|
+
private lastKey = 0
|
|
47
|
+
private tail = 0
|
|
48
|
+
|
|
49
|
+
// One capture. Returns the chunk it built, or null when nothing changed since the last one.
|
|
50
|
+
frame(sess: string, sampled: boolean, w: ReplayWalk, t: number): ReplayChunk | null {
|
|
51
|
+
if (this.off) return null
|
|
52
|
+
if (sess !== this.sess) {
|
|
53
|
+
this.sess = sess
|
|
54
|
+
this.seq = 0
|
|
55
|
+
this.prev = null
|
|
56
|
+
this.ring = []
|
|
57
|
+
this.tail = 0
|
|
58
|
+
}
|
|
59
|
+
const ring = !sampled && t > this.tail
|
|
60
|
+
const next = new Map<string, KeyedNode>()
|
|
61
|
+
for (const n of w.nodes) next.set(n[0], n)
|
|
62
|
+
let key = !this.prev || w.screen !== this.screen || (ring && t - this.lastKey > REKEY_MS)
|
|
63
|
+
let c: ReplayChunk | null = null
|
|
64
|
+
if (!key) {
|
|
65
|
+
const prev = this.prev!
|
|
66
|
+
const scroll = w.scroll.filter((s) => this.offsets.get(s.node) !== at(s) && (this.offsets.has(s.node) || s.y || s.x))
|
|
67
|
+
const rm: string[] = []
|
|
68
|
+
for (const k of prev.keys()) if (!next.has(k)) rm.push(k)
|
|
69
|
+
const add: ReplayRun[] = []
|
|
70
|
+
const mv: [string, number, number][] = []
|
|
71
|
+
let run: ReplayRun | null = null
|
|
72
|
+
let adds = 0
|
|
73
|
+
for (let i = 0; i < w.nodes.length; i++) {
|
|
74
|
+
const n = w.nodes[i]
|
|
75
|
+
const p = prev.get(n[0])
|
|
76
|
+
if (p && same(p, n)) {
|
|
77
|
+
if (p[2] !== n[2] || p[3] !== n[3]) mv.push([n[0], n[2], n[3]])
|
|
78
|
+
run = null
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
adds++
|
|
82
|
+
if (!run) add.push((run = { after: i ? w.nodes[i - 1][0] : null, nodes: [] }))
|
|
83
|
+
run.nodes.push(n)
|
|
84
|
+
}
|
|
85
|
+
if (adds > KEY_SHARE * w.nodes.length) key = true
|
|
86
|
+
else if (adds || rm.length || mv.length || scroll.length) {
|
|
87
|
+
const diff: NonNullable<ReplayChunk['diff']> = {}
|
|
88
|
+
if (add.length) diff.add = add
|
|
89
|
+
if (rm.length) diff.rm = rm
|
|
90
|
+
if (mv.length) diff.mv = mv
|
|
91
|
+
c = { sess, seq: this.seq + 1, t, ...(adds || rm.length || mv.length ? { diff } : {}), ...(scroll.length ? { scroll } : {}) }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (key) {
|
|
95
|
+
const sc = w.scroll.filter((s) => s.y || s.x)
|
|
96
|
+
c = { sess, seq: this.seq + 1, t, keyframe: { screen: w.screen, w: w.w, h: w.h, nodes: w.nodes }, ...(sc.length ? { scroll: sc } : {}) }
|
|
97
|
+
this.lastKey = t
|
|
98
|
+
}
|
|
99
|
+
this.prev = next
|
|
100
|
+
this.screen = w.screen
|
|
101
|
+
this.offsets.clear()
|
|
102
|
+
for (const s of w.scroll) this.offsets.set(s.node, at(s))
|
|
103
|
+
if (!c) return null
|
|
104
|
+
this.seq = c.seq
|
|
105
|
+
const b: Built = { c, s: JSON.stringify(c), key, why: sampled ? 'sample' : this.why }
|
|
106
|
+
if (ring) {
|
|
107
|
+
this.ring.push(b)
|
|
108
|
+
this.trimRing(t)
|
|
109
|
+
} else {
|
|
110
|
+
this.out.push(b)
|
|
111
|
+
this.cap(this.out)
|
|
112
|
+
}
|
|
113
|
+
return c
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// A signal releases the ring to the outbox; chunks built in the next 5 s follow it.
|
|
117
|
+
signal(why: ReplayWhy, t: number): void {
|
|
118
|
+
if (this.off) return
|
|
119
|
+
if (t <= this.tail) {
|
|
120
|
+
if (RANK.indexOf(why) <= RANK.indexOf(this.why)) return
|
|
121
|
+
for (const b of this.out) if (b.why === this.why && b.c.sess === this.sess) b.why = why
|
|
122
|
+
this.why = why
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
this.why = why
|
|
126
|
+
this.tail = t + TAIL_MS
|
|
127
|
+
for (const b of this.ring) this.out.push({ ...b, why })
|
|
128
|
+
this.ring = []
|
|
129
|
+
this.cap(this.out)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Ring: drop everything before the newest keyframe that is at least 30 s old (a keyframe is re-taken every 15 s, so the
|
|
133
|
+
// ring spans 30-45 s), then keep it under 1 MB.
|
|
134
|
+
private trimRing(t: number): void {
|
|
135
|
+
let k = -1
|
|
136
|
+
for (let i = 0; i < this.ring.length; i++) if (this.ring[i].key && this.ring[i].c.t <= t - RING_MS) k = i
|
|
137
|
+
if (k > 0) this.ring.splice(0, k)
|
|
138
|
+
this.cap(this.ring)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Over 1 MB: drop the oldest chunks, then up to the next keyframe, so what stays starts decodable. Emptied: the next
|
|
142
|
+
// capture is a keyframe.
|
|
143
|
+
private cap(list: Built[]): void {
|
|
144
|
+
let bytes = 0
|
|
145
|
+
for (const b of list) bytes += b.s.length
|
|
146
|
+
if (bytes <= MAX_BYTES) return
|
|
147
|
+
let i = 0
|
|
148
|
+
while (i < list.length && bytes > MAX_BYTES) bytes -= list[i++].s.length
|
|
149
|
+
while (i < list.length && !list[i].key) i++
|
|
150
|
+
list.splice(0, i)
|
|
151
|
+
if (!list.length) this.prev = null
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get ringBytes(): number {
|
|
155
|
+
let n = 0
|
|
156
|
+
for (const b of this.ring) n += b.s.length
|
|
157
|
+
return n
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// The next request: consecutive outbox chunks with one why, up to 64 KB (a larger single chunk goes alone).
|
|
161
|
+
take(): { why: ReplayWhy; items: Built[] } | null {
|
|
162
|
+
if (!this.out.length) return null
|
|
163
|
+
const why = this.out[0].why
|
|
164
|
+
const items: Built[] = []
|
|
165
|
+
let n = 0
|
|
166
|
+
for (const b of this.out) {
|
|
167
|
+
if (b.why !== why || (items.length && n + b.s.length > BODY)) break
|
|
168
|
+
items.push(b)
|
|
169
|
+
n += b.s.length
|
|
170
|
+
}
|
|
171
|
+
return { why, items }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
done(items: Built[]): void {
|
|
175
|
+
const sent = new Set(items)
|
|
176
|
+
this.out = this.out.filter((b) => !sent.has(b))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
restore(why: ReplayWhy, chunks: ReplayChunk[]): void {
|
|
180
|
+
this.out = chunks.map((c): Built => ({ c, s: JSON.stringify(c), key: !!c.keyframe, why })).concat(this.out)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Consent denied, or stop (a 403: replay off or over quota) until the next launch.
|
|
184
|
+
clear(stop = false): void {
|
|
185
|
+
this.out = []
|
|
186
|
+
this.ring = []
|
|
187
|
+
this.prev = null
|
|
188
|
+
if (stop) this.off = true
|
|
189
|
+
}
|
|
190
|
+
}
|