gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/web/uploads.hl

6.0 KB

  1. // File transfer, resume and progress, loaded on the first File-bearing emit.
  2. import { registerFeature } from 'hl:web'
  3. client = null
  4. static register = () => { return registerFeature('uploads', Uploads) }
  5. static uploadPart = 1048576
  6. static uploadRetries = 8
  7. // A STALLED CONNECTION FIRES NEITHER `error` NOR `abort` (proxy or server congestion
  8. // can leave a request with no response and no close): `uploadCall` below reads that
  9. // as DROPPED, the way a refused connection is, so `uploadFile`'s retry loop above
  10. // still ends it, instead of `flushing` staying true forever the way it did before
  11. // this (flush() below never ran again — mission 2026-09-26, "the file's entry
  12. // drained" never seen). The bound is a STALL, not a duration: progress resets it, so
  13. // a slow-but-moving upload (a real one, on a slow link) never trips it — only one
  14. // that goes silent does. `seed.upload.stall` (ms) is the app's own clock, the way
  15. // `seed.ping` is for the heartbeat; this default stands when there is none.
  16. static uploadStallDefault = 15000
  17. fileAt(list, j) {
  18. if (hlTypeName(list) == 'Hybrid') { return list[j] }
  19. return list.item(j)
  20. }
  21. // the payload with every file replaced by `{ __hlUpload = id }`, or { error, dropped }
  22. uploadAll(event, payload, q) {
  23. out = []
  24. let index = 0
  25. for (a of payload) {
  26. if (client.isFile(a)) {
  27. up = uploadFile(event, index, a, q)
  28. if (up.error != null) { return up }
  29. index = index + 1
  30. out.push({ __hlUpload = up.id })
  31. } else if (client.isFileList(a)) {
  32. ids = []
  33. let j = 0
  34. while (j < a.length) {
  35. up = uploadFile(event, index, fileAt(a, j), q)
  36. if (up.error != null) { return up }
  37. index = index + 1
  38. ids.push({ __hlUpload = up.id })
  39. j = j + 1
  40. }
  41. out.push(ids)
  42. } else {
  43. out.push(a)
  44. }
  45. }
  46. return { payload = out }
  47. }
  48. // one file, part by part: { id } once the server has all of it, or { error } — with
  49. // `dropped` when it was the connection, not the server, that said no
  50. uploadFile(event, index, f, q) {
  51. name = f.name != null ? f.name : ''
  52. size = f.size
  53. start = uploadCall('POST', '/__hl/upload', JSON.stringify({ name = name type = f.type size = size q = q n = index }), null)
  54. if (start.status == 0 || start.status >= 500) { return { error = name + ' did not start — the server is out of reach' dropped = true } }
  55. if (start.status != 200) { return { error = name + ' was refused — ' + uploadWhy(start) } }
  56. begun = JSON.parse(start.body)
  57. // the emit it belongs to was handled already (a replay whose ack was lost): the file
  58. // arrived then, and the frame is answered without it
  59. if (begun.handled == true) { return { id = '' } }
  60. id = begun.id
  61. // the same upload again (a replay of the same emit): it goes on from what arrived
  62. let have = begun.have != null ? begun.have : 0
  63. let misses = 0
  64. uploadProgress(event, index, name, 0, size)
  65. while (have < size) {
  66. at = have
  67. to = at + uploadPart > size ? size : at + uploadPart
  68. r = uploadCall('POST', '/__hl/upload?id=' + id + '&at=' + at, f.slice(at, to), (n) => { uploadProgress(event, index, name, at + n, size) })
  69. if (r.status == 200 || r.status == 409) {
  70. have = JSON.parse(r.body).have
  71. misses = 0
  72. uploadProgress(event, index, name, have, size)
  73. } else if (r.status == 0 || r.status >= 500) {
  74. // DROPPED: wait, ask the server what arrived, and go on from there
  75. misses = misses + 1
  76. if (misses > uploadRetries) { return { error = name + ' stopped at ' + have + ' of ' + size + ' bytes — the connection dropped ' + misses + ' times' dropped = true } }
  77. uploadWait(misses)
  78. status = uploadCall('GET', '/__hl/upload?id=' + id, null, null)
  79. if (status.status == 200) {
  80. have = JSON.parse(status.body).have
  81. } else if (status.status != 0 && status.status < 500) {
  82. return { error = name + ' was refused — ' + uploadWhy(status) }
  83. }
  84. } else {
  85. return { error = name + ' was refused — ' + uploadWhy(r) }
  86. }
  87. }
  88. return { id = id }
  89. }
  90. // ONE REQUEST, as an answer and never a throw: a dropped connection is `status = 0`,
  91. // and so is one gone STALL — no progress and no answer for `client.seed.upload.stall` ms — the
  92. // way a real network drop is read anywhere else in this file. Progress in EITHER
  93. // direction (bytes going up, bytes of the answer coming down) resets the clock: a part
  94. // on a slow link keeps moving and is never touched by this, only a request that goes
  95. // silent is. `x.timeout` is not used for this — it counts the whole request, and a
  96. // slow-but-real transfer is exactly what it would end wrongly.
  97. // XMLHttpRequest and not `fetch`, because only it reports an upload's progress.
  98. uploadCall(method, url, body, onSent) {
  99. stallAfter = client.seed.upload != null ? client.seed.upload.stall : uploadStallDefault
  100. return new Promise((res, rej) => {
  101. x = new XMLHttpRequest()
  102. x.open(method, url)
  103. let watchdog = null
  104. let settled = false
  105. bump = () => {
  106. if (watchdog != null) { clearTimeout(watchdog) }
  107. watchdog = setTimeout(() => { x.abort() }, stallAfter)
  108. }
  109. finish = (v) => {
  110. if (settled) { return null }
  111. settled = true
  112. if (watchdog != null) { clearTimeout(watchdog) }
  113. res(v)
  114. return null
  115. }
  116. x.upload.addEventListener('progress', (e) => {
  117. bump()
  118. if (onSent != null) { onSent(e.loaded) }
  119. })
  120. x.addEventListener('progress', (e) => { bump() })
  121. x.addEventListener('load', () => { finish({ status = x.status body = x.responseText }) })
  122. x.addEventListener('error', () => { finish({ status = 0 body = '' }) })
  123. x.addEventListener('abort', () => { finish({ status = 0 body = '' }) })
  124. bump()
  125. x.send(body)
  126. })
  127. }
  128. // 250 ms after the first drop, doubling up to 4 s
  129. uploadWait(misses) {
  130. let ms = 250
  131. let n = 1
  132. while (n < misses && ms < 4000) { ms = ms * 2 n = n + 1 }
  133. return new Promise((res, rej) => { setTimeout(() => { res(null) }, ms) })
  134. }
  135. uploadWhy(r) {
  136. if (r.body.startsWith('{')) {
  137. b = JSON.parse(r.body)
  138. if (b.error != null) { return b.error }
  139. }
  140. return 'the server answered ' + r.status
  141. }
  142. uploadProgress(event, index, name, sent, size) {
  143. client.outward({ t = 'emit' event = 'uploadProgress' payload = [{ event = event index = index name = name sent = sent size = size }] })
  144. return null
  145. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre