gitoria

jukebox

All repositories: gitoria

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

6.3 KB

  1. // THE ROUTER — realm-neutral, like view.hl: the same matcher runs on the server
  2. // (which route answers a request) and in the browser (is this link a route of
  3. // this app, and which). Ported from hl:web v1's client/router.hl, itself the port
  4. // of the server matcher: one algorithm, one file, both realms.
  5. //
  6. // A route table is a list of hybrids. The PATTERN is the URLPattern `pathname`
  7. // subset; the second key names the route's KIND (hybrilior 0.3.0's types):
  8. // { pattern = "/posts/:id" component = "./components/post.hl" } a page (SSR + client)
  9. // { pattern = "/health" function = (route, req) => { … } } server code answers
  10. // { pattern = "/robots.txt" direct = "User-agent: *" } a fixed body
  11. // { pattern = "/favicon.ico" file = "./assets/favicon.ico" } one file
  12. // { pattern = "/assets/*" directory = "./assets" } a directory, `*` is the rest
  13. // { pattern = "/device" socket = Device } one application WebSocket connection
  14. // First match wins: authored order IS priority.
  15. //
  16. // PATTERN SYNTAX: `/a/b` literal segments, `:name` one segment into params.name,
  17. // `:name?` optional (absent ⇒ null), `*` the rest into params["0"], and `*name` the
  18. // rest into params.name (ticket #81): a page component reads it as its member `name`,
  19. // the way it reads a `:name` group — "0" is no name a member can have.
  20. // The network-first window for both document and in-shell kept navigation (#420).
  21. static navigationWait = 1500
  22. routes = [] // bound by `new Router(routes = …)`
  23. // the route's kind, by which key it carries
  24. kindOf(r) {
  25. if (r.socket != null) { return 'socket' }
  26. if (r.component != null) { return 'component' }
  27. if (r.function != null) { return 'function' }
  28. if (r.direct != null) { return 'direct' }
  29. if (r.file != null) { return 'file' }
  30. if (r.directory != null) { return 'directory' }
  31. return null
  32. }
  33. // Split a URL path into its non-empty segments:
  34. // "/" -> [] ; "/notes/42" -> ["notes","42"] ; "/notes/42/" -> ["notes","42"].
  35. segments(path) {
  36. let raw = path.split("/")
  37. let out = []
  38. for (s of raw) {
  39. if (s.length > 0) { out[] = s }
  40. }
  41. return out
  42. }
  43. // Strip a query string / hash off a URL path before matching ("/a?b=1" -> "/a").
  44. cleanPath(path) {
  45. let p = "" + path
  46. let q = p.indexOf("?")
  47. if (q >= 0) { p = p.slice(0, q) }
  48. let h = p.indexOf("#")
  49. if (h >= 0) { p = p.slice(0, h) }
  50. if (p.length == 0) { return "/" }
  51. return p
  52. }
  53. // Match ONE pattern's segments against the path's. On success the captured
  54. // groups (possibly empty, which is still a MATCH); null on failure. `trailing`
  55. // says the path ended in a slash — segmentation drops the empty tail, and a `*`
  56. // at the end must still match "/files/" with { "0": "" } as the standard does.
  57. matchPattern(pattern, segs, trailing) {
  58. let psegs = segments(pattern)
  59. let groups = {}
  60. let i = 0
  61. while (i < psegs.length) {
  62. let p = psegs[i]
  63. if (p.startsWith("*")) {
  64. let wild = p == "*" ? "0" : p.slice(1)
  65. if (i >= segs.length) {
  66. if (trailing) {
  67. groups[wild] = ""
  68. return groups
  69. }
  70. return null
  71. }
  72. let rest = ""
  73. let j = i
  74. while (j < segs.length) {
  75. if (rest.length > 0) { rest = rest + "/" }
  76. rest = rest + segs[j]
  77. j = j + 1
  78. }
  79. groups[wild] = rest
  80. return groups
  81. }
  82. if (p.startsWith(":")) {
  83. let name = p.slice(1)
  84. let optional = false
  85. if (name.endsWith("?")) {
  86. optional = true
  87. name = name.slice(0, name.length - 1)
  88. }
  89. if (i < segs.length) {
  90. groups[name] = segs[i]
  91. } else {
  92. if (optional == false) { return null }
  93. groups[name] = null // an ABSENT optional group is null
  94. }
  95. } else {
  96. if (i >= segs.length) { return null }
  97. if (p != segs[i]) { return null }
  98. }
  99. i = i + 1
  100. }
  101. if (segs.length > psegs.length) { return null }
  102. return groups
  103. }
  104. // the params key a pattern's `*` fills: "0" for a bare one, the name after it
  105. // otherwise, "" when the pattern has none (no group has that key: `rest` is null)
  106. wildOf(pattern) {
  107. for (p of segments(pattern)) {
  108. if (p.startsWith("*")) { return p == "*" ? "0" : p.slice(1) }
  109. }
  110. return ""
  111. }
  112. // Match a path against the WHOLE table. `rest` is what the `*` took, named or not. Answers { route, kind, params, path } or
  113. // null — in the browser a null is a real navigation, on the server a 404.
  114. // `start` is the index to resume at: a static mount that holds no file for the url
  115. // does not consume it (COMPONENTS §11, D20), so the server asks again past it
  116. match(path, Number start = 0) {
  117. let clean = cleanPath(path)
  118. let segs = segments(clean)
  119. let trailing = false
  120. if (clean.length > 1 && clean.endsWith("/")) { trailing = true }
  121. let i = 0
  122. for (r of routes) {
  123. if (i >= start) {
  124. let groups = matchPattern(r.pattern, segs, trailing)
  125. if (groups != null) {
  126. return { route = r; kind = kindOf(r); params = groups; path = clean; index = i; rest = groups[wildOf(r.pattern)]; }
  127. }
  128. }
  129. i = i + 1
  130. }
  131. return null
  132. }
  133. // The table the BROWSER may hold: the routes it can show itself. A function is
  134. // server code and does not travel; direct, file and directory answers are the
  135. // server's too — the browser reaches those with a plain request, so it only
  136. // needs to know they are not pages.
  137. clientTable() {
  138. let out = []
  139. for (r of routes) {
  140. // hl:web's own urls (/__hl/*, the runtime, the stylesheet, a component's module) are never a page and the browser
  141. // never shows them itself: the framework's routes are not shipped
  142. if (r.framework == true || r.socket != null) { continue }
  143. let k = kindOf(r)
  144. // the client needs only "a page": the component may be a CLASS value, which
  145. // does not survive the wire — the page's module rides the navigation's ack
  146. if (k == 'component') {
  147. row = { pattern = r.pattern; component = true; }
  148. if (r.redirect != null) { row.redirect = true }
  149. out.push(row)
  150. }
  151. else if (k != 'function') { out.push({ pattern = r.pattern; kind = k; }) }
  152. else { out.push({ pattern = r.pattern; kind = 'function'; }) }
  153. }
  154. return out
  155. }
  156. // Search parameters for a navigation URL, matching the HTTP request's query hybrid.
  157. queryOf(String path) {
  158. out = {}
  159. url = path.split('#')[0]
  160. q = url.indexOf('?')
  161. if (q < 0) { return out }
  162. for (part of url.slice(q + 1).split('&')) {
  163. if (part.length > 0) {
  164. at = part.indexOf('=')
  165. key = at < 0 ? part : part.slice(0, at)
  166. value = at < 0 ? '' : part.slice(at + 1)
  167. out[decodeURIComponent(key.replaceAll('+', ' '))] = decodeURIComponent(value.replaceAll('+', ' '))
  168. }
  169. }
  170. return out
  171. }

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