gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb19c478bb19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mreb19c478b/plugins/web/worker.hl

8.9 KB

  1. // The framework's SERVICE WORKER (COMPONENTS §10, D38) — the offline shell. It exists
  2. // only for an app that declares `offline = [ … ]`: hl:web compiles this file like
  3. // client.hl, serves it at /__hl/web/worker.js, and registers the entry script
  4. // /__hl/sw.js, which imports it and constructs it ONCE with the table below.
  5. //
  6. // WHAT IS KEPT IS THE APP'S DECISION (D38). `precache` holds the shared shell —
  7. // the runtime, the client half, the stylesheet, every page module, the web app
  8. // manifest and its icons — plus the DOCUMENT of each param-free route whose
  9. // component the allow-list names. `routes` is the client's route table with
  10. // `keep` on the rows the allow-list names, so a param-bearing page (`/notes/:id`)
  11. // is kept when it is VISITED, and only then. Every other page is unavailable
  12. // offline, and says so.
  13. //
  14. // THREE RULES, the ones the old hl:web ran (mission 101):
  15. // · a NAVIGATION is network-first. The network is the truth while it answers,
  16. // and a fresh document for a kept page replaces the cached one on the way past;
  17. // the cache is what an outage falls back to.
  18. // · a precached asset is cache-first. Its url carries the build's hash, so a hit
  19. // cannot be stale.
  20. // · everything else goes to the network untouched: the emit channel, a function
  21. // route, an app's own file. The socket never reaches a worker at all.
  22. //
  23. // WHY THE ENTRY SCRIPT IS NOT THIS FILE. A worker must add its listeners while its
  24. // script is first evaluated, and a `fetch` listener must hand `respondWith` its
  25. // promise before the listener returns. Every call in this language is awaited, so
  26. // neither can be written here; /__hl/sw.js does exactly those two things and calls
  27. // the methods below for everything else (WebFramework.hl, `workerEntry`).
  28. import HlwRouter from './router.hl'
  29. cfgText = '' // the table, as JSON text (the one construction argument)
  30. cfg = JSON.parse(cfgText)
  31. router = new HlwRouter(routes = cfg.routes)
  32. // INSTALL: the shell into this build's cache, one url at a time — a single failed
  33. // url must not throw the whole install away and leave the app with no shell
  34. install() {
  35. let cache = globalThis.caches.open(cfg.cache)
  36. for (url of cfg.precache) {
  37. // a data-free page is fetched as nobody: its document carries no user's state (#120)
  38. let creds = cfg.dataFree.includes(url) ? 'omit' : 'same-origin'
  39. let r = globalThis.__hlAttempt(() => { return fetch(url, { cache = 'reload' credentials = creds }) })
  40. if (r.ok && r.value.ok) { cache.put(url, r.value) }
  41. }
  42. globalThis.skipWaiting()
  43. return null
  44. }
  45. // ACTIVATE: the cache name carries the build's hash, so every other hl cache is an
  46. // older deploy's. This worker then controls the pages that are already open.
  47. activate() {
  48. for (name of globalThis.caches.keys()) {
  49. if (name != cfg.cache && name.startsWith('hl-shell-')) { globalThis.caches.delete(name) }
  50. }
  51. globalThis.clients.claim()
  52. return null
  53. }
  54. // FETCH: what answers this request. Never null — the entry script hands the answer to
  55. // `respondWith` unread.
  56. answer(req) {
  57. if (cfg.shell == false) { return fetch(req) }
  58. if (req.method != 'GET') { return fetch(req) }
  59. let url = new URL(req.url)
  60. if (url.origin != globalThis.location.origin) { return fetch(req) }
  61. if (req.mode == 'navigate') { return navigation(req, url) }
  62. if (cfg.precache.includes(url.pathname + url.search)) { return cacheFirst(req, url.pathname + url.search) }
  63. return fetch(req)
  64. }
  65. // may the document at this path be kept? Only if the route that answers it is a page
  66. // the allow-list names — the whole of D38 in one predicate
  67. kept(path) {
  68. let m = router.match(path)
  69. return m != null && m.route.keep == true
  70. }
  71. navigation(req, url) {
  72. let r = globalThis.__hlAttempt(() => { return fetch(req) })
  73. if (r.ok) {
  74. // a data-free page's copy is the anonymous one made at install, never the live render
  75. if (r.value.ok && kept(url.pathname) && !cfg.dataFree.includes(url.pathname)) { keep(url.pathname + url.search, r.value.clone()) }
  76. return r.value
  77. }
  78. let hit = globalThis.caches.match(url.pathname + url.search, { cacheName = cfg.cache })
  79. if (hit != null) { return hit }
  80. // A PAGE OF A KEPT COMPONENT THAT WAS NEVER LOADED AS A DOCUMENT (#124): any kept document
  81. // of the same component stands in — the page boots it with this url's parameters
  82. let like = standIn(url.pathname)
  83. if (like != null) { return like }
  84. // THE APP'S OWN FALLBACK (#120): a data-free page, kept once, instead of the bare notice
  85. if (cfg.fallback != null) {
  86. let fb = globalThis.caches.match(cfg.fallback, { cacheName = cfg.cache })
  87. if (fb != null) { return fb }
  88. }
  89. return unavailable(url.pathname)
  90. }
  91. // a kept document of the route's own component: this route's first, else another route's
  92. standIn(path) {
  93. let m = router.match(path)
  94. if (m == null || m.route.keep != true) { return null }
  95. let cache = globalThis.caches.open(cfg.cache)
  96. let other = null
  97. for (req of cache.keys()) {
  98. let o = router.match(new URL(req.url).pathname)
  99. if (o != null && o.route.keep == true && o.route.page == m.route.page) {
  100. if (o.index == m.index) { return cache.match(req) }
  101. if (other == null) { other = req }
  102. }
  103. }
  104. return other != null ? cache.match(other) : null
  105. }
  106. keep(key, res) {
  107. let cache = globalThis.caches.open(cfg.cache)
  108. cache.put(key, res)
  109. return null
  110. }
  111. // CACHE-FIRST for the shell. A miss still asks the network, so a worker installed
  112. // before an asset existed is not a permanent failure.
  113. cacheFirst(req, key) {
  114. let hit = globalThis.caches.match(key, { cacheName = cfg.cache })
  115. if (hit != null) { return hit }
  116. let r = globalThis.__hlAttempt(() => { return fetch(req) })
  117. if (r.ok) {
  118. if (r.value.ok) { keep(key, r.value.clone()) }
  119. return r.value
  120. }
  121. let R = globalThis.Response
  122. return new R('', { status = 503 statusText = 'Offline' })
  123. }
  124. // A MESSAGE FROM A PAGE. `keep`: a page the browser reached by a client-side
  125. // navigation was never a document request, so the client asks for it to be kept —
  126. // fetched here as the document it would be, if it is a page the allow-list names.
  127. message(data) {
  128. if (data != null && data.t == 'forget') { return forget(data.path) }
  129. if (data == null || data.t != 'keep' || !kept(data.path) || cfg.dataFree.includes(data.path)) { return null }
  130. let r = globalThis.__hlAttempt(() => { return fetch(data.path, { credentials = 'same-origin' headers = { 'Accept' = 'text/html' } }) })
  131. if (r.ok && r.value.ok) { keep(data.path, r.value) }
  132. return null
  133. }
  134. // THE USER CHANGED (#125): every kept document is another user's, so they all go; the
  135. // page that stands is fetched again as the new session sees it
  136. forget(path) {
  137. let cache = globalThis.caches.open(cfg.cache)
  138. for (req of cache.keys()) {
  139. let u = new URL(req.url)
  140. if (kept(u.pathname) && !cfg.dataFree.includes(u.pathname)) { cache.delete(req) }
  141. }
  142. if (path != null && kept(path) && !cfg.dataFree.includes(path)) {
  143. let r = globalThis.__hlAttempt(() => { return fetch(path, { credentials = 'same-origin' headers = { 'Accept' = 'text/html' } }) })
  144. if (r.ok && r.value.ok) { keep(path, r.value) }
  145. }
  146. return null
  147. }
  148. // A NOTIFICATION WAS CLICKED: the app's open window is focused, or one is opened at
  149. // the path the notification carries
  150. clicked(n) {
  151. n.close()
  152. let path = n.data != null && n.data.path != null ? n.data.path : '/'
  153. for (c of globalThis.clients.matchAll({ type = 'window' includeUncontrolled = true })) {
  154. if (c.focus != null) { return c.focus() }
  155. }
  156. return globalThis.clients.openWindow(path)
  157. }
  158. // A PUSH ARRIVED (#173): the payload is JSON text — `{ title, body, url, tag }` — and is shown as a
  159. // notification by this worker, whether or not any page is open. `url` is where a click goes (the
  160. // path rides in the notification's data, `clicked` reads it); a payload that is not JSON is
  161. // shown as the body of a notification titled with the page's origin host.
  162. pushed(text) {
  163. let r = globalThis.__hlAttempt(() => { return JSON.parse(text) })
  164. let p = r.ok && r.value != null && (r.value.title != null || r.value.body != null) ? r.value : { body = text; }
  165. let title = p.title != null ? p.title : globalThis.location.host
  166. let opts = { data = { path = p.url != null ? p.url : '/' }; }
  167. if (p.body != null) { opts.body = p.body }
  168. if (p.tag != null) { opts.tag = p.tag }
  169. if (p.icon != null) { opts.icon = p.icon }
  170. return globalThis.registration.showNotification(title, opts)
  171. }
  172. // A page this app does not carry offline. A DOCUMENT, so the tab shows something and
  173. // no navigation fails inside the browser; 503, so a probe can tell it from a page.
  174. unavailable(path) {
  175. let esc = path.replaceAll('&', '&amp;').replaceAll('<', '&lt;')
  176. let body = '<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Offline</title><link rel="stylesheet" href="' + cfg.css + '"></head><body><main data-hl-offline="1"><h1>Unavailable offline</h1><p><code>' + esc + '</code> is not one of the pages this app carries offline.</p><p><a href="/">Back to the start</a></p></main></body></html>'
  177. let R = globalThis.Response
  178. return new R(body, { status = 503 headers = { 'Content-Type' = 'text/html; charset=utf-8' } })
  179. }

Branches

Latest commits

  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre