-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cachedStreamFetcher): handle Content-Range
- Loading branch information
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { parseContentRangeHeader } from '@/src/utils/parseContentRangeHeader'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
describe('parseContentRangeHeader', () => { | ||
it('should handle valid ranges', () => { | ||
let range = parseContentRangeHeader('bytes 0-1/123'); | ||
expect(range.type).toEqual('range'); | ||
if (range.type !== 'range') return; // ts can't narrow on expect() | ||
|
||
expect(range.start).toEqual(0); | ||
expect(range.end).toEqual(1); | ||
expect(range.length).toEqual(123); | ||
|
||
range = parseContentRangeHeader('bytes 2-5/*'); | ||
expect(range.type).toEqual('range'); | ||
if (range.type !== 'range') return; // ts can't narrow on expect() | ||
|
||
expect(range.start).toEqual(2); | ||
expect(range.end).toEqual(5); | ||
expect(range.length).to.be.null; | ||
}); | ||
|
||
it('should handle unsatisfied ranges', () => { | ||
const range = parseContentRangeHeader('bytes */12'); | ||
expect(range.type).toEqual('unsatisfied-range'); | ||
if (range.type !== 'unsatisfied-range') return; // ts can't narrow on expect() | ||
|
||
expect(range.length).toEqual(12); | ||
}); | ||
|
||
it('should handle invalid ranges', () => { | ||
[ | ||
'', | ||
'bytes', | ||
'bytes */*', | ||
'byte 0-1/2', | ||
'bytes 1-0/2', | ||
'bytes 0-1/1', | ||
'bytes 1-3/2', | ||
'bytes 1-/2', | ||
'bytes -1/2', | ||
].forEach((range) => { | ||
expect(parseContentRangeHeader(range).type).toEqual('invalid-range'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const CONTENT_RANGE_REGEXP = | ||
/^bytes (?<range>(?<start>\d+)-(?<end>\d+)|\*)\/(?<length>\d+|\*)$/; | ||
|
||
export type ContentRange = | ||
| { type: 'invalid-range' } | ||
| { type: 'unsatisfied-range'; length: number } | ||
| { type: 'range'; start: number; end: number; length: number | null }; | ||
|
||
/** | ||
* Parses a Content-Range header. | ||
* | ||
* Only supports bytes ranges. | ||
* @param headerValue | ||
* @returns | ||
*/ | ||
export function parseContentRangeHeader( | ||
headerValue: string | null | ||
): ContentRange { | ||
if (!headerValue) return { type: 'invalid-range' }; | ||
|
||
const match = CONTENT_RANGE_REGEXP.exec(headerValue); | ||
const groups = match?.groups; | ||
if (!groups) return { type: 'invalid-range' }; | ||
|
||
const length = groups.length === '*' ? null : parseInt(groups.length, 10); | ||
|
||
if (groups.range === '*') { | ||
if (length === null) return { type: 'invalid-range' }; | ||
return { type: 'unsatisfied-range', length }; | ||
} | ||
|
||
const start = parseInt(groups.start, 10); | ||
const end = parseInt(groups.end, 10); | ||
|
||
if (end < start || (length !== null && length <= end)) | ||
return { type: 'invalid-range' }; | ||
|
||
return { type: 'range', start, end, length }; | ||
} |