calendar
All repositories: gitoria
6.0 KB
// File transfer, resume and progress, loaded on the first File-bearing emit.import { registerFeature } from 'hl:web'client = nullstatic register = () => { return registerFeature('uploads', Uploads) }static uploadPart = 1048576static uploadRetries = 8// A STALLED CONNECTION FIRES NEITHER `error` NOR `abort` (proxy or server congestion// can leave a request with no response and no close): `uploadCall` below reads that// as DROPPED, the way a refused connection is, so `uploadFile`'s retry loop above// still ends it, instead of `flushing` staying true forever the way it did before// this (flush() below never ran again — mission 2026-09-26, "the file's entry// drained" never seen). The bound is a STALL, not a duration: progress resets it, so// a slow-but-moving upload (a real one, on a slow link) never trips it — only one// that goes silent does. `seed.upload.stall` (ms) is the app's own clock, the way// `seed.ping` is for the heartbeat; this default stands when there is none.static uploadStallDefault = 15000fileAt(list, j) {if (hlTypeName(list) == 'Hybrid') { return list[j] }return list.item(j)}// the payload with every file replaced by `{ __hlUpload = id }`, or { error, dropped }uploadAll(event, payload, q) {out = []let index = 0for (a of payload) {if (client.isFile(a)) {up = uploadFile(event, index, a, q)if (up.error != null) { return up }index = index + 1out.push({ __hlUpload = up.id })} else if (client.isFileList(a)) {ids = []let j = 0while (j < a.length) {up = uploadFile(event, index, fileAt(a, j), q)if (up.error != null) { return up }index = index + 1ids.push({ __hlUpload = up.id })j = j + 1}out.push(ids)} else {out.push(a)}}return { payload = out }}// one file, part by part: { id } once the server has all of it, or { error } — with// `dropped` when it was the connection, not the server, that said nouploadFile(event, index, f, q) {name = f.name != null ? f.name : ''size = f.sizestart = uploadCall('POST', '/__hl/upload', JSON.stringify({ name = name type = f.type size = size q = q n = index }), null)if (start.status == 0 || start.status >= 500) { return { error = name + ' did not start — the server is out of reach' dropped = true } }if (start.status != 200) { return { error = name + ' was refused — ' + uploadWhy(start) } }begun = JSON.parse(start.body)// the emit it belongs to was handled already (a replay whose ack was lost): the file// arrived then, and the frame is answered without itif (begun.handled == true) { return { id = '' } }id = begun.id// the same upload again (a replay of the same emit): it goes on from what arrivedlet have = begun.have != null ? begun.have : 0let misses = 0uploadProgress(event, index, name, 0, size)while (have < size) {at = haveto = at + uploadPart > size ? size : at + uploadPartr = uploadCall('POST', '/__hl/upload?id=' + id + '&at=' + at, f.slice(at, to), (n) => { uploadProgress(event, index, name, at + n, size) })if (r.status == 200 || r.status == 409) {have = JSON.parse(r.body).havemisses = 0uploadProgress(event, index, name, have, size)} else if (r.status == 0 || r.status >= 500) {// DROPPED: wait, ask the server what arrived, and go on from theremisses = misses + 1if (misses > uploadRetries) { return { error = name + ' stopped at ' + have + ' of ' + size + ' bytes — the connection dropped ' + misses + ' times' dropped = true } }uploadWait(misses)status = uploadCall('GET', '/__hl/upload?id=' + id, null, null)if (status.status == 200) {have = JSON.parse(status.body).have} else if (status.status != 0 && status.status < 500) {return { error = name + ' was refused — ' + uploadWhy(status) }}} else {return { error = name + ' was refused — ' + uploadWhy(r) }}}return { id = id }}// ONE REQUEST, as an answer and never a throw: a dropped connection is `status = 0`,// and so is one gone STALL — no progress and no answer for `client.seed.upload.stall` ms — the// way a real network drop is read anywhere else in this file. Progress in EITHER// direction (bytes going up, bytes of the answer coming down) resets the clock: a part// on a slow link keeps moving and is never touched by this, only a request that goes// silent is. `x.timeout` is not used for this — it counts the whole request, and a// slow-but-real transfer is exactly what it would end wrongly.// XMLHttpRequest and not `fetch`, because only it reports an upload's progress.uploadCall(method, url, body, onSent) {stallAfter = client.seed.upload != null ? client.seed.upload.stall : uploadStallDefaultreturn new Promise((res, rej) => {x = new XMLHttpRequest()x.open(method, url)let watchdog = nulllet settled = falsebump = () => {if (watchdog != null) { clearTimeout(watchdog) }watchdog = setTimeout(() => { x.abort() }, stallAfter)}finish = (v) => {if (settled) { return null }settled = trueif (watchdog != null) { clearTimeout(watchdog) }res(v)return null}x.upload.addEventListener('progress', (e) => {bump()if (onSent != null) { onSent(e.loaded) }})x.addEventListener('progress', (e) => { bump() })x.addEventListener('load', () => { finish({ status = x.status body = x.responseText }) })x.addEventListener('error', () => { finish({ status = 0 body = '' }) })x.addEventListener('abort', () => { finish({ status = 0 body = '' }) })bump()x.send(body)})}// 250 ms after the first drop, doubling up to 4 suploadWait(misses) {let ms = 250let n = 1while (n < misses && ms < 4000) { ms = ms * 2 n = n + 1 }return new Promise((res, rej) => { setTimeout(() => { res(null) }, ms) })}uploadWhy(r) {if (r.body.startsWith('{')) {b = JSON.parse(r.body)if (b.error != null) { return b.error }}return 'the server answered ' + r.status}uploadProgress(event, index, name, sent, size) {client.outward({ t = 'emit' event = 'uploadProgress' payload = [{ event = event index = index name = name sent = sent size = size }] })return null}
Branches
Latest commits
- f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
- 66341793|||PR Vendor Hybriel day61Atlas
- 36fc596bMerge branch 'vendor-day59' into mainAtlas
- b37cc1da|||PR Vendor Hybriel day59Atlas
- 05665102Merge branch 'vendor-day56' into mainAtlas
- 687296fb|||PR Vendor Hybriel day56Atlas
- a6166d4eMerge branch 'hl-standard-3' into mainAtlas
- 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
- add4b499|||PR Make calendar day actions keyboard accessibleAtlas
- 79e49b4dMerge branch 'hl-standard' into mainAtlas
- e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
- eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
- cef81394Wait for the DST fixture page instead of an unused socketmre
- 42434f47Check lazy-session cookies after login with a stored anonymous fixturemre
- 728e84e3Re-vendor complete Hybriel day41 plugin directoriesmre
- cbd7ff43Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
- 59d1da0acalendar: plain declarations instead of needless let in 9 files (calendar#17): 49 declarations that are never reassigned; reassigned ones and the timer closure's p stay letmre
- f5415cf9calendar: the Style blocks in the language's nested form (calendar#16): rules named after the View's own elements with their children nested, pseudo-classes and @media nested, merged duplicate select rule; same CSS, shots identical in Chrome at 4 sizesmre
- edb7c947calendar: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 56+4 failing → 60 passedmre
- bf8cc0fcStyles move into their components: the shell's styles into main.hl, the events and the editors into the month view's Style block; styles.hl keeps only the theme inherit, the accent and the message line; .gitignore ignores a bin symlink too; tests/styles-shots.mjs for the before/after pixel comparemre