Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/provider-html-local/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ export async function serveHtmlProgram(root: string): Promise<{ readonly url: st
}
const info = await stat(path);
if (!info.isFile()) { response.writeHead(404).end(); return; }
const type = mediaTypes[extname(path).toLowerCase()] ?? "application/octet-stream";
const range = /^bytes=(\d+)-(\d*)$/u.exec(request.headers.range ?? "");
const start = range === null ? 0 : Number(range[1]);
const end = range === null || range[2] === "" ? info.size - 1 : Number(range[2]);
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end < start || end >= info.size) {
if (range === null) {
response.writeHead(200, { "Content-Type": type, "Content-Length": info.size, "Accept-Ranges": "bytes" });
// An empty asset is a complete 200, not an unsatisfiable range.
if (info.size === 0) { response.end(); return; }
createReadStream(path).on("error", () => response.destroy()).pipe(response);
return;
}
const start = Number(range[1]);
// RFC 9110 §14.1.2: a last position at or past the end means "to the end".
const end = range[2] === "" ? info.size - 1 : Math.min(Number(range[2]), info.size - 1);
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start >= info.size || end < start) {
response.writeHead(416, { "Content-Range": `bytes */${info.size}` }).end(); return;
}
response.writeHead(range === null ? 200 : 206, {
"Content-Type": mediaTypes[extname(path).toLowerCase()] ?? "application/octet-stream",
response.writeHead(206, {
"Content-Type": type,
"Content-Length": end - start + 1,
"Accept-Ranges": "bytes",
...(range === null ? {} : { "Content-Range": `bytes ${start}-${end}/${info.size}` }),
"Content-Range": `bytes ${start}-${end}/${info.size}`,
});
createReadStream(path, { start, end }).on("error", () => response.destroy()).pipe(response);
} catch (error) {
Expand Down
31 changes: 31 additions & 0 deletions packages/provider-html-local/test/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import assert from "node:assert/strict";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";

import { serveHtmlProgram } from "../src/server.js";

test("the capture server answers byte ranges as HTTP defines them", async () => {
const root = await mkdtemp(join(tmpdir(), "hypit-capture-server-"));
await writeFile(join(root, "clip.mp4"), new Uint8Array(1_000).map((_, index) => index % 251));
await writeFile(join(root, "empty.webp"), new Uint8Array());
const server = await serveHtmlProgram(root);
const get = async (path: string, range?: string) => {
const response = await fetch(new URL(path, server.url), range === undefined ? {} : { headers: { range } });
return { status: response.status, range: response.headers.get("content-range"), bytes: (await response.arrayBuffer()).byteLength };
};
try {
assert.deepEqual(await get("clip.mp4"), { status: 200, range: null, bytes: 1_000 });
assert.deepEqual(await get("clip.mp4", "bytes=0-499"), { status: 206, range: "bytes 0-499/1000", bytes: 500 });
assert.deepEqual(await get("clip.mp4", "bytes=500-"), { status: 206, range: "bytes 500-999/1000", bytes: 500 });
// A last position past the end is clamped; only a first position past the end is unsatisfiable.
assert.deepEqual(await get("clip.mp4", "bytes=500-1499"), { status: 206, range: "bytes 500-999/1000", bytes: 500 });
assert.deepEqual(await get("clip.mp4", "bytes=1000-"), { status: 416, range: "bytes */1000", bytes: 0 });
assert.deepEqual(await get("clip.mp4", "bytes=600-500"), { status: 416, range: "bytes */1000", bytes: 0 });
assert.deepEqual(await get("empty.webp"), { status: 200, range: null, bytes: 0 });
} finally {
await server.close();
await rm(root, { recursive: true, force: true });
}
});