Skip to content

Range request example is off-by-one #28030

@DavidBuchanan314

Description

@DavidBuchanan314

What is the type of issue?

Example code is not working

What is the issue?

Bun.serve({
fetch(req) {
// parse `Range` header
const [start = 0, end = Infinity] = req.headers
.get("Range") // Range: bytes=0-100
.split("=") // ["Range: bytes", "0-100"]
.at(-1) // "0-100"
.split("-") // ["0", "100"]
.map(Number); // [0, 100]
// return a slice of the file
const bigFile = Bun.file("./big-video.mp4");
return new Response(bigFile.slice(start, end));
},
});

The HTTP range header is end-inclusive, so the last line should probably use end + 1 to be correct. I think the ["Range: bytes", "0-100"] comment is likely misleading too, presumably it's supposed to be ["bytes", "0-100"].

Where did you find it?

https://bun.com/docs/runtime/http/routing

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions