gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-day39d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmrerecords-day39/lib/stream.hl

4.2 KB

  1. // lib/stream.hl — THE AUDIO ROUTE: GET /stream/<song id>. The id is looked up in the songs table; the file path comes from the database and never
  2. // from the URL, and the read is only ever of a file that table lists. HTTP RANGE like a media element needs it: `Range: bytes=a-b`, `a-`, `-n` answer 206
  3. // with Content-Range; a start behind the end of the file answers 416. A single answer is never bigger than `chunk` (1 MiB): `bytes=0-` (what Chrome sends
  4. // first) gets the first megabyte and the browser asks for the next one as it plays, so no file is read into memory whole. The bytes come from hl:fs `readBytes(path, offset, length)`
  5. // (hybriel#233), which reads only that range; the size of the file comes from `stat(path)`.
  6. // A request without a Range header is answered like `bytes=0-` (206): a media element always sends one.
  7. import { Response } from 'hl:http1'
  8. import { readBytes, stat } from 'hl:fs'
  9. import { songById } from './db.hl'
  10. static chunk = 1048576
  11. static kinds = { 'mp3' = 'audio/mpeg' 'ogg' = 'audio/ogg' 'oga' = 'audio/ogg' 'opus' = 'audio/ogg' 'm4a' = 'audio/mp4' 'aac' = 'audio/aac' 'flac' = 'audio/flac'
  12. 'wav' = 'audio/wav' 'wma' = 'audio/x-ms-wma' 'ape' = 'audio/x-ape' 'mp4' = 'video/mp4' 'm4v' = 'video/mp4' 'webm' = 'video/webm' 'mkv' = 'video/x-matroska' }
  13. static contentType = (format) => {
  14. t = kinds[format]
  15. return t == null ? 'application/octet-stream' : t
  16. }
  17. static fail = (status, text) => {
  18. return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' } })
  19. }
  20. // a song id is mpackdb's twelve characters of 0-9 a-z (anything else is not a song)
  21. static validId = (id) => {
  22. if (id == null || id.length < 4 || id.length > 24) { return false }
  23. let i = 0
  24. while (i < id.length) {
  25. c = id.charCodeAt(i)
  26. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 45 || c == 95)) { return false }
  27. i = i + 1
  28. }
  29. return true
  30. }
  31. // the range asked for as { start, end } within a file of `size` bytes; null = no usable Range header (a list of ranges, garbage); { bad = true } = past the end
  32. static rangeOf = (asked, size) => {
  33. if (asked == null || !asked.startsWith('bytes=') || asked.includes(',')) { return null }
  34. parts = asked.slice(6).split('-')
  35. if (parts.length != 2) { return null }
  36. let start = null
  37. let end = null
  38. if (parts[0] == '' && parts[1] != '') {
  39. n = toNumber(parts[1])
  40. if (n == null || n <= 0) { return null }
  41. start = n >= size ? 0 : size - n
  42. end = size - 1
  43. } else if (parts[0] != '') {
  44. start = toNumber(parts[0])
  45. end = parts[1] == '' ? size - 1 : toNumber(parts[1])
  46. if (start == null || end == null) { return null }
  47. if (end >= size) { end = size - 1 }
  48. } else { return null }
  49. if (start > end || start >= size) { return { bad = true } }
  50. return { start = start end = end }
  51. }
  52. // GET /stream/<id>
  53. static serve = (route, req) => {
  54. if (req.method != 'GET' && req.method != 'HEAD') { return fail(405, 'GET only') }
  55. id = route.params.id
  56. if (!validId(id)) { return fail(404, 'no such song') }
  57. song = songById(id)
  58. if (song == null || song.size == null || song.size <= 0) { return fail(404, 'no such song') }
  59. size = song.size
  60. h = { 'Content-Type' = contentType(song.format) 'Accept-Ranges' = 'bytes' 'Cache-Control' = 'no-cache' 'X-Content-Type-Options' = 'nosniff' }
  61. asked = req.headers['range'] != null ? req.headers['range'] : req.headers['Range']
  62. r = rangeOf(asked, size)
  63. if (r != null && r.bad == true) { return new Response('', { status = 416 headers = { 'Content-Range' = 'bytes */' + size } }) }
  64. let start = r == null ? 0 : r.start
  65. let end = r == null ? size - 1 : r.end
  66. if (end - start + 1 > chunk) { end = start + chunk - 1 }
  67. if (req.method == 'HEAD') {
  68. h['Content-Range'] = 'bytes ' + start + '-' + end + '/' + size
  69. return new Response('', { status = 206 headers = h })
  70. }
  71. if (stat(song.path) == null) { return fail(404, 'the file cannot be read') }
  72. data = readBytes(song.path, start, end - start + 1)
  73. if (data == null) { return fail(404, 'the file cannot be read') }
  74. // the file may have changed since the scan: say what was really read
  75. len = data.length
  76. if (len == 0) { return fail(404, 'the file cannot be read') }
  77. h['Content-Range'] = 'bytes ' + start + '-' + (start + len - 1) + '/' + size
  78. return new Response(data, { status = 206 headers = h })
  79. }

Branches

Latest commits

  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
  • 277a2722README: provenance linemre
  • 4169ca6aREADME: sync pinmre
  • a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
  • f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
  • d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
  • df7920ceRecords: R3 startedmre