jukebox
All repositories: gitoria
4.2 KB
// 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// 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// 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// 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)`// (hybriel#233), which reads only that range; the size of the file comes from `stat(path)`.// A request without a Range header is answered like `bytes=0-` (206): a media element always sends one.import { Response } from 'hl:http1'import { readBytes, stat } from 'hl:fs'import { songById } from './db.hl'static chunk = 1048576static kinds = { 'mp3' = 'audio/mpeg' 'ogg' = 'audio/ogg' 'oga' = 'audio/ogg' 'opus' = 'audio/ogg' 'm4a' = 'audio/mp4' 'aac' = 'audio/aac' 'flac' = 'audio/flac''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' }static contentType = (format) => {t = kinds[format]return t == null ? 'application/octet-stream' : t}static fail = (status, text) => {return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' } })}// a song id is mpackdb's twelve characters of 0-9 a-z (anything else is not a song)static validId = (id) => {if (id == null || id.length < 4 || id.length > 24) { return false }let i = 0while (i < id.length) {c = id.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 45 || c == 95)) { return false }i = i + 1}return true}// 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 endstatic rangeOf = (asked, size) => {if (asked == null || !asked.startsWith('bytes=') || asked.includes(',')) { return null }parts = asked.slice(6).split('-')if (parts.length != 2) { return null }let start = nulllet end = nullif (parts[0] == '' && parts[1] != '') {n = toNumber(parts[1])if (n == null || n <= 0) { return null }start = n >= size ? 0 : size - nend = size - 1} else if (parts[0] != '') {start = toNumber(parts[0])end = parts[1] == '' ? size - 1 : toNumber(parts[1])if (start == null || end == null) { return null }if (end >= size) { end = size - 1 }} else { return null }if (start > end || start >= size) { return { bad = true } }return { start = start end = end }}// GET /stream/<id>static serve = (route, req) => {if (req.method != 'GET' && req.method != 'HEAD') { return fail(405, 'GET only') }id = route.params.idif (!validId(id)) { return fail(404, 'no such song') }song = songById(id)if (song == null || song.size == null || song.size <= 0) { return fail(404, 'no such song') }size = song.sizeh = { 'Content-Type' = contentType(song.format) 'Accept-Ranges' = 'bytes' 'Cache-Control' = 'no-cache' 'X-Content-Type-Options' = 'nosniff' }asked = req.headers['range'] != null ? req.headers['range'] : req.headers['Range']r = rangeOf(asked, size)if (r != null && r.bad == true) { return new Response('', { status = 416 headers = { 'Content-Range' = 'bytes */' + size } }) }let start = r == null ? 0 : r.startlet end = r == null ? size - 1 : r.endif (end - start + 1 > chunk) { end = start + chunk - 1 }if (req.method == 'HEAD') {h['Content-Range'] = 'bytes ' + start + '-' + end + '/' + sizereturn new Response('', { status = 206 headers = h })}if (stat(song.path) == null) { return fail(404, 'the file cannot be read') }data = readBytes(song.path, start, end - start + 1)if (data == null) { return fail(404, 'the file cannot be read') }// the file may have changed since the scan: say what was really readlen = data.lengthif (len == 0) { return fail(404, 'the file cannot be read') }h['Content-Range'] = 'bytes ' + start + '-' + (start + len - 1) + '/' + sizereturn new Response(data, { status = 206 headers = h })}
Branches
Latest commits
- 3c8a2e12Mission 007: #20 sort scrollmre
- deb8cef5Records: mission 006 startedmre
- 4f0ad42fMission 006: jukebox#20mre
- 800cae06Records: #21 planmre
- 64bc7823Records: #21 causemre
- 76639655Records: B1 questionmre
- d7906b36Records: mission 005 startedmre
- d3df773cMission 005: doc linemre
- 6a5d840aMission 005: jukebox#22mre
- 13e5e748Records: mission 004 startedmre
- a8098271Mission 004: jukebox#21mre
- df8f5205Records: jukebox#19-21, #11 closedmre
- b7a4e692Records: hybriel#298mre
- 9bcbb017Records: day38b waits for day39 fixmre
- f80504e1Records: day38b repro sentmre
- 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
- 68aace7fRecords: mission 003 day38bmre
- d105d8a9Records: mpackdb integrity check cleanmre
- 6e980c53Records: mpackdb rule refinementmre
- 6d335ac4Records: storage copy rulingmre