oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1// Docs live push: timing through the real CARLOS edge (spec
2// 2026-11-26-live-push-design.md, §1 and question 4). Paste the whole of this
3// into the browser console while a SCRATCH document is open at
4// https://<team>.docs.oficina.works/d/<sqid>. Your session never leaves your
5// browser: it only makes same-origin requests from that page.
6//
7// What it does, in about ten seconds:
8// 1. opens a second live stream for this document and times its first event;
9// 2. three empty fetches (GET /updates?since=<current>): the fetch round trip;
10// 3. three empty updates (POST /updates with the two bytes 00 00, a valid Yjs
11// update that changes nothing): the time to the server's answer, and the
12// time until this stream announces it. If the edge buffered the stream,
13// the second would be far behind the first.
14// It changes nothing you can see. Each empty update adds one row to this
15// document's history, so use a scratch document and delete it afterwards.
16// On a server with live push it also reports the push stream's own frame.
17(async () => {
18 const m = location.pathname.match(/^\/d\/([^/]+)\/?$/)
19 if (!m) { console.log('Open the scratch document first (a /d/... page).'); return }
20 const base = `/d/${m[1]}`
21 const now = () => performance.now()
22 const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
23 const median = (xs) => { const s = [...xs].sort((a, b) => a - b); return s.length ? s[Math.floor(s.length / 2)] : NaN }
24 const r1 = (x) => Math.round(x * 10) / 10
25
26 const events = [] // { seq, t, kind }, every announcement in arrival order
27 const waiters = []
28 let streamID = null, current = null, pushServer = false
29 const t0 = now()
30 const es = new EventSource(`${base}/stream?push=1`, { withCredentials: true })
31 const seen = (seq, kind) => {
32 const ev = { seq, t: now(), kind }
33 events.push(ev)
34 for (const w of waiters.splice(0)) if (seq >= w.seq) w.resolve(ev); else waiters.push(w)
35 }
36 // The announcement of seq: already here (it can beat the POST's answer), or the next one.
37 const announcement = (seq, after) => {
38 const ev = events.find((e) => e.seq >= seq && e.t >= after)
39 return ev ? Promise.resolve(ev) : new Promise((resolve) => waiters.push({ seq, resolve }))
40 }
41 const first = new Promise((resolve, reject) => {
42 es.addEventListener('hello', (e) => { pushServer = true; const [id, seq] = e.data.split(' '); streamID = id; current = Number(seq); resolve(now() - t0) })
43 es.addEventListener('update', (e) => { const seq = Number(e.data); if (current === null) { current = seq; resolve(now() - t0) } else seen(seq, 'update') })
44 es.addEventListener('push', (e) => seen(Number(e.data.split(' ')[0]), 'push'))
45 es.addEventListener('own', (e) => seen(Number(e.data.split(' ')[0]), 'own'))
46 es.onerror = () => reject(new Error('the stream failed to open'))
47 setTimeout(() => reject(new Error('no stream event within 10 s')), 10000)
48 })
49 const result = { page: location.host, protocol: null }
50 try {
51 result.streamFirstEventMs = r1(await first)
52 result.pushServer = pushServer
53
54 const gets = []
55 for (let i = 0; i < 3; i++) {
56 const s = now()
57 const r = await fetch(`${base}/updates?since=${current}`, { credentials: 'same-origin', cache: 'no-store' })
58 await r.arrayBuffer()
59 gets.push(now() - s)
60 await sleep(300)
61 }
62 result.fetchRoundTripMs = gets.map(r1)
63 const entry = performance.getEntriesByType('resource').filter((e) => e.name.includes(`${base}/updates?since=`)).pop()
64 result.protocol = entry && entry.nextHopProtocol ? entry.nextHopProtocol : 'unknown'
65
66 const answers = [], announced = [], kinds = []
67 for (let i = 0; i < 3; i++) {
68 const headers = { 'Content-Type': 'application/octet-stream' }
69 if (streamID) headers['Docs-Stream'] = `${streamID}.${i + 1}`
70 const s = now()
71 const r = await fetch(`${base}/updates`, { method: 'POST', credentials: 'same-origin', headers, body: new Uint8Array([0, 0]) })
72 const answered = now()
73 if (!r.ok) throw new Error(`POST answered ${r.status}`)
74 const seq = Number(r.headers.get('Docs-Seq'))
75 const ev = await Promise.race([announcement(seq, s), sleep(5000).then(() => null)])
76 answers.push(answered - s)
77 announced.push(ev ? ev.t - s : NaN)
78 kinds.push(ev ? ev.kind : 'none within 5 s')
79 await sleep(500)
80 }
81 result.postAnswerMs = answers.map(r1)
82 result.postToStreamEventMs = announced.map(r1)
83 result.streamEventKinds = kinds
84 result.summary = `fetch RTT ~${r1(median(gets))} ms; upload answered ~${r1(median(answers))} ms; ` +
85 `stream announced it ~${r1(median(announced))} ms after sending ` +
86 `(${r1(median(announced) - median(answers))} ms after the answer; near 0 or negative means the edge does not buffer the stream)`
87 } catch (err) {
88 result.error = String(err)
89 } finally {
90 es.close()
91 }
92 console.log('DOCS EDGE TIMING ' + JSON.stringify(result, null, 2))
93 return result
94})()
95