@spectosh/browser 0.1.2 → 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/LICENSE +21 -0
- package/README.md +44 -10
- package/dist/specto-browser.cjs +23 -21
- package/dist/specto-browser.js +2224 -1583
- package/package.json +22 -7
- package/src/index.js +36 -5
- package/src/recorder.js +664 -176
- package/src/recorder.worker.js +14 -25
- package/src/uploader.js +95 -0
package/src/recorder.worker.js
CHANGED
|
@@ -1,28 +1,17 @@
|
|
|
1
|
-
// Specto upload worker —
|
|
2
|
-
// Receives already-clean event batches (privacy transforms happen on
|
|
3
|
-
// thread before hand-off)
|
|
4
|
-
|
|
1
|
+
// Specto upload worker — serializing, compressing and retrying off the main
|
|
2
|
+
// thread. Receives already-clean event batches (privacy transforms happen on
|
|
3
|
+
// the main thread before hand-off) and reports delivery outcomes back.
|
|
4
|
+
import { createUploader } from './uploader.js'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const m = e.data
|
|
8
|
-
if (m.t === 'init') cfg = m.cfg
|
|
9
|
-
else if (m.t === 'upload') upload(m.batch)
|
|
10
|
-
}
|
|
6
|
+
let up = null
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
const body = JSON.stringify({ sessionId: cfg.sessionId, key: cfg.key, meta: cfg.meta, events: batch })
|
|
22
|
-
const gz = await gzip(body)
|
|
23
|
-
try {
|
|
24
|
-
await fetch(cfg.endpoint, gz
|
|
25
|
-
? { method: 'POST', headers: { 'Content-Type': 'application/octet-stream', 'x-specto-gzip': '1' }, body: gz, keepalive: true }
|
|
26
|
-
: { method: 'POST', headers: { 'Content-Type': 'application/json' }, body, keepalive: true })
|
|
27
|
-
} catch { /* periodic cadence limits loss; tail is beaconed from main thread */ }
|
|
8
|
+
self.onmessage = (e) => {
|
|
9
|
+
const m = e.data || {}
|
|
10
|
+
if (m.t === 'init') {
|
|
11
|
+
up = createUploader({ endpoint: m.endpoint, onEvent: (ev) => self.postMessage(ev) })
|
|
12
|
+
self.postMessage({ t: 'ready' }) // the recorder keeps batches until it hears this
|
|
13
|
+
}
|
|
14
|
+
else if (m.t === 'upload') up?.push(m.item)
|
|
15
|
+
else if (m.t === 'nudge') up?.nudge()
|
|
16
|
+
else if (m.t === 'stop') up?.stop()
|
|
28
17
|
}
|
package/src/uploader.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Batch uploader shared by the Web Worker and the main-thread fallback (a
|
|
2
|
+
// customer's CSP may forbid blob: workers). Responsibilities:
|
|
3
|
+
// - ordered delivery: one request in flight, batches sent in order
|
|
4
|
+
// - retries with exponential backoff (honours Retry-After) on network
|
|
5
|
+
// errors, 429 rate_limited, 503 and other 5xx — batches are numbered,
|
|
6
|
+
// so the server de-duplicates a retried batch
|
|
7
|
+
// - terminal answers stop retrying and tell the recorder why:
|
|
8
|
+
// quota_exceeded / recording_disabled / 401 / 403 → stop recording
|
|
9
|
+
// session_limit, session_key → start a new session
|
|
10
|
+
// - bounded memory: past MAX_PENDING the oldest batch is dropped
|
|
11
|
+
// - uploads are text/plain (gzip detected server-side by magic bytes):
|
|
12
|
+
// a CORS "simple" request, so no preflight per batch
|
|
13
|
+
const MAX_PENDING = 40
|
|
14
|
+
const MAX_TRIES = 7
|
|
15
|
+
const KEEPALIVE_MAX = 60_000 // browsers cap keepalive bodies at 64 KB
|
|
16
|
+
|
|
17
|
+
async function gzip(str) {
|
|
18
|
+
if (typeof CompressionStream === 'undefined') return null
|
|
19
|
+
try { return await new Response(new Blob([str]).stream().pipeThrough(new CompressionStream('gzip'))).blob() } catch { return null }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createUploader({ endpoint, onEvent = () => {} }) {
|
|
23
|
+
const queue = [] // { json, b, tries }
|
|
24
|
+
let busy = false
|
|
25
|
+
let timer = null
|
|
26
|
+
let stopped = false
|
|
27
|
+
|
|
28
|
+
const retry = (item, afterSec) => {
|
|
29
|
+
item.tries++
|
|
30
|
+
if (item.tries >= MAX_TRIES) {
|
|
31
|
+
queue.shift()
|
|
32
|
+
onEvent({ t: 'drop', b: item.b, reason: 'retries' })
|
|
33
|
+
return pump()
|
|
34
|
+
}
|
|
35
|
+
const backoff = afterSec > 0 ? afterSec * 1000 : Math.min(30_000, 1000 * 2 ** (item.tries - 1)) * (0.75 + Math.random() / 2)
|
|
36
|
+
timer = setTimeout(() => { timer = null; pump() }, backoff)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function pump() {
|
|
40
|
+
if (busy || timer || stopped || !queue.length) return
|
|
41
|
+
busy = true
|
|
42
|
+
const item = queue[0]
|
|
43
|
+
try {
|
|
44
|
+
const gz = await gzip(item.json)
|
|
45
|
+
const body = gz || item.json
|
|
46
|
+
const size = gz ? gz.size : item.json.length
|
|
47
|
+
const res = await fetch(endpoint, {
|
|
48
|
+
method: 'POST', body, credentials: 'omit', mode: 'cors',
|
|
49
|
+
headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
|
|
50
|
+
keepalive: size < KEEPALIVE_MAX,
|
|
51
|
+
})
|
|
52
|
+
if (res.ok) {
|
|
53
|
+
queue.shift()
|
|
54
|
+
onEvent({ t: 'ack', b: item.b })
|
|
55
|
+
} else {
|
|
56
|
+
let code = ''
|
|
57
|
+
try { code = (await res.json())?.code || '' } catch { /* non-JSON */ }
|
|
58
|
+
// session_key: the stored session lost its key (an older SDK started
|
|
59
|
+
// it) or the server refused it — a new session fixes both
|
|
60
|
+
if (code === 'session_limit' || code === 'session_key') {
|
|
61
|
+
queue.length = 0
|
|
62
|
+
onEvent({ t: 'rotate', code })
|
|
63
|
+
} else if (code === 'quota_exceeded' || code === 'recording_disabled' || res.status === 401 || res.status === 403) {
|
|
64
|
+
queue.length = 0
|
|
65
|
+
stopped = true
|
|
66
|
+
onEvent({ t: 'stop', code: code || String(res.status) })
|
|
67
|
+
} else if (res.status === 429 || res.status >= 500) {
|
|
68
|
+
busy = false
|
|
69
|
+
return retry(item, Number(res.headers.get('Retry-After')) || 0)
|
|
70
|
+
} else {
|
|
71
|
+
queue.shift() // 400/413: this batch can never succeed
|
|
72
|
+
onEvent({ t: 'drop', b: item.b, reason: String(res.status) })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
busy = false
|
|
77
|
+
return retry(item, 0)
|
|
78
|
+
}
|
|
79
|
+
busy = false
|
|
80
|
+
pump()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
push(item) {
|
|
85
|
+
if (stopped) return
|
|
86
|
+
queue.push({ ...item, tries: 0 })
|
|
87
|
+
if (queue.length > MAX_PENDING) onEvent({ t: 'drop', b: queue.shift().b, reason: 'overflow' })
|
|
88
|
+
pump()
|
|
89
|
+
},
|
|
90
|
+
// a network change or a returning tab: try now instead of waiting out a backoff
|
|
91
|
+
nudge() { if (timer) { clearTimeout(timer); timer = null } pump() },
|
|
92
|
+
stop() { stopped = true; queue.length = 0; if (timer) clearTimeout(timer) },
|
|
93
|
+
pending: () => queue.length,
|
|
94
|
+
}
|
|
95
|
+
}
|