-
Notifications
You must be signed in to change notification settings - Fork 0
/
getstats-shipper.js
72 lines (54 loc) · 1.71 KB
/
getstats-shipper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//@ts-check
//The @ts-check statement above enables jsdoc typechecking
// https://stackoverflow.com/a/52076280/86375
// http://demo.unified-streaming.com/players/dash.js-2.4.1/build/jsdoc/jsdoc_cheat-sheet.pdf
/**
* @param {string} name
**/
function getCookie(name) {
let value = `; ${document.cookie}`;
let parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
/**
* @return {string}
**/
function rand63string() {
var b = new BigUint64Array(1)
window.crypto.getRandomValues(b)
return BigInt.asUintN(63, b[0]).toString()
}
/**
* @param {RTCPeerConnection} pc
*/
function startGetStatsShipping(pc) {
let url = getCookie('getstats-shipper-url')
if (typeof url === 'undefined' || url == '') {
console.debug('statsUrl cookie not found')
return
}
console.debug('will send stats: statsUrl cookie WAS found')
const ct = { 'Content-Type': 'application/json' }
// a single unique 64 bit value per RTCPeerConnection
// makes SQLite analysis easier
const pcid = rand63string()
// milliseconds between reports
const period = 30 * 1000
// launch first with no delay
setTimeout(myCallback, 0)
function myCallback() {
pc.getStats(null).then(stats => {
let reports = Object.fromEntries(stats)
let body = { PCID: pcid, Reports: reports }
let json = JSON.stringify(body)
let fo = {
method: 'POST',
/** @type {RequestMode} */ mode: 'no-cors', // magic is real!
headers: ct,
body: json
}
fetch(url, fo)
setTimeout(myCallback, period)
})
}
}