mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* fix: Byte range handling for media in Safari Safari loads video and audio with byte range requests. Several forms of Range header were mishandled by files.get: a suffix range fell through to a full 200 response, a single byte request served the whole file, a range past the end of the file produced an invalid Content-Range and a truncated body, and a range starting past the end returned 400 rather than 416. Also stop the service worker intercepting these requests. Partial responses cannot be stored by the Cache API, so the route only produced failed writes, and serving media through a service worker prevents playback in Safari. * Refactor to ByteRangeHelper
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { ByteRangeHelper } from "./ByteRangeHelper";
|
|
|
|
const { unsatisfiable } = ByteRangeHelper;
|
|
|
|
describe("ByteRangeHelper.parse", () => {
|
|
it("should serve the whole file when no header is present", () => {
|
|
expect(ByteRangeHelper.parse(undefined, 100)).toBeUndefined();
|
|
expect(ByteRangeHelper.parse("", 100)).toBeUndefined();
|
|
});
|
|
|
|
it("should serve the whole file when the header is malformed", () => {
|
|
expect(ByteRangeHelper.parse("bytes=abc-", 100)).toBeUndefined();
|
|
expect(ByteRangeHelper.parse("bytes=-", 100)).toBeUndefined();
|
|
expect(ByteRangeHelper.parse("items=0-1", 100)).toBeUndefined();
|
|
expect(ByteRangeHelper.parse("0-1", 100)).toBeUndefined();
|
|
});
|
|
|
|
it("should serve the whole file when multiple ranges are requested", () => {
|
|
expect(ByteRangeHelper.parse("bytes=0-1, 10-20", 100)).toBeUndefined();
|
|
});
|
|
|
|
it("should parse a closed range", () => {
|
|
expect(ByteRangeHelper.parse("bytes=0-1", 100)).toEqual({
|
|
start: 0,
|
|
end: 1,
|
|
});
|
|
expect(ByteRangeHelper.parse("bytes=10-20", 100)).toEqual({
|
|
start: 10,
|
|
end: 20,
|
|
});
|
|
});
|
|
|
|
it("should parse a single byte range", () => {
|
|
expect(ByteRangeHelper.parse("bytes=0-0", 100)).toEqual({
|
|
start: 0,
|
|
end: 0,
|
|
});
|
|
expect(ByteRangeHelper.parse("bytes=99-99", 100)).toEqual({
|
|
start: 99,
|
|
end: 99,
|
|
});
|
|
});
|
|
|
|
it("should tolerate surrounding whitespace", () => {
|
|
expect(ByteRangeHelper.parse(" bytes=0-1 ", 100)).toEqual({
|
|
start: 0,
|
|
end: 1,
|
|
});
|
|
});
|
|
|
|
it("should parse an open ended range", () => {
|
|
expect(ByteRangeHelper.parse("bytes=10-", 100)).toEqual({
|
|
start: 10,
|
|
end: 99,
|
|
});
|
|
});
|
|
|
|
it("should parse a suffix range", () => {
|
|
expect(ByteRangeHelper.parse("bytes=-10", 100)).toEqual({
|
|
start: 90,
|
|
end: 99,
|
|
});
|
|
});
|
|
|
|
it("should clamp a suffix range longer than the file", () => {
|
|
expect(ByteRangeHelper.parse("bytes=-500", 100)).toEqual({
|
|
start: 0,
|
|
end: 99,
|
|
});
|
|
});
|
|
|
|
it("should clamp a range that runs past the end of the file", () => {
|
|
expect(ByteRangeHelper.parse("bytes=0-1000", 100)).toEqual({
|
|
start: 0,
|
|
end: 99,
|
|
});
|
|
});
|
|
|
|
it("should be unsatisfiable when the range starts past the end", () => {
|
|
expect(ByteRangeHelper.parse("bytes=100-", 100)).toBe(unsatisfiable);
|
|
expect(ByteRangeHelper.parse("bytes=100-200", 100)).toBe(unsatisfiable);
|
|
});
|
|
|
|
it("should be unsatisfiable when the end is before the start", () => {
|
|
expect(ByteRangeHelper.parse("bytes=5-2", 100)).toBe(unsatisfiable);
|
|
});
|
|
|
|
it("should be unsatisfiable for a zero length suffix", () => {
|
|
expect(ByteRangeHelper.parse("bytes=-0", 100)).toBe(unsatisfiable);
|
|
});
|
|
|
|
it("should be unsatisfiable for an empty file", () => {
|
|
expect(ByteRangeHelper.parse("bytes=0-", 0)).toBe(unsatisfiable);
|
|
expect(ByteRangeHelper.parse("bytes=-1", 0)).toBe(unsatisfiable);
|
|
});
|
|
});
|