OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note commits / 8d090ad

Add parseRange helper for HTTP Range header support
Adds exported parseRange(header, totalSize) function to media-store.js for parsing HTTP Range request headers (bytes=start-end, open-end, suffix forms). Includes unit tests covering all valid forms and invalid/edge-case inputs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8d090ad0aa1ecb25d73fb1cb0f582341d0050362
by plankatron <fleeky@gmail.com> · 2026-03-19T16:19:01.000Z

Changed files (2) +51 -0

lib/media-store.js +24 -0
……@@ -57,4 +57,28 @@ export class MediaStore {
5757 }
5858}
5959
60export function parseRange (header, totalSize) {
61 if (!header || !header.startsWith('bytes=')) return null
62 const spec = header.replace('bytes=', '')
63 const parts = spec.split('-')
64
65 let start, end
66
67 if (parts[0] === '') {
68 const suffix = parseInt(parts[1], 10)
69 if (isNaN(suffix)) return null
70 start = totalSize - suffix
71 end = totalSize - 1
72 } else if (parts[1] === '') {
73 start = parseInt(parts[0], 10)
74 end = totalSize - 1
75 } else {
76 start = parseInt(parts[0], 10)
77 end = parseInt(parts[1], 10)
78 }
79
80 if (isNaN(start) || isNaN(end) || start < 0 || end >= totalSize || start > end) return null
81 return { start, end }
82}
83
6084export default MediaStore
test/unit/range-serve.test.js +27 -0
……@@ -0,0 +1,27 @@
1import test from 'brittle'
2import { parseRange } from '../../lib/media-store.js'
3
4test('parseRange - parses "bytes=0-499"', async (t) => {
5 const range = parseRange('bytes=0-499', 1000)
6 t.is(range.start, 0)
7 t.is(range.end, 499)
8})
9
10test('parseRange - parses "bytes=500-" (open end)', async (t) => {
11 const range = parseRange('bytes=500-', 1000)
12 t.is(range.start, 500)
13 t.is(range.end, 999)
14})
15
16test('parseRange - parses "bytes=-200" (suffix)', async (t) => {
17 const range = parseRange('bytes=-200', 1000)
18 t.is(range.start, 800)
19 t.is(range.end, 999)
20})
21
22test('parseRange - returns null for invalid range', async (t) => {
23 t.is(parseRange('invalid', 1000), null)
24 t.is(parseRange(undefined, 1000), null)
25 t.is(parseRange('bytes=500-200', 1000), null) // start > end
26 t.is(parseRange('bytes=0-1500', 1000), null) // end >= totalSize
27})