Content-Range Header Builder and Parser
Format and parse Content-Range response headers for byte ranges, validating start, end and total values in both directions.
Code
export interface ContentRange {
unit: "bytes";
start: number;
end: number;
total: number | "*";
}
export function formatContentRange(range: ContentRange): string {
if (range.unit !== "bytes") {
throw new RangeError("only the bytes unit is supported");
}
if (!Number.isInteger(range.start) || range.start < 0) {
throw new RangeError("start must be a non-negative integer");
}
if (!Number.isInteger(range.end) || range.end < range.start) {
throw new RangeError("end must be an integer not less than start");
}
if (
range.total !== "*" &&
(!Number.isInteger(range.total) || range.total <= range.end)
) {
throw new RangeError("total must be * or an integer greater than end");
}
return "bytes " + range.start + "-" + range.end + "/" + range.total;
}
export function parseContentRange(value: string): ContentRange {
if (typeof value !== "string") throw new TypeError("value must be a string");
const match = /^bytes (\d+)-(\d+)\/(\d+|\*)$/.exec(value.trim());
if (!match) throw new RangeError("invalid Content-Range header");
const start = Number(match[1]);
const end = Number(match[2]);
const total = match[3] === "*" ? "*" : Number(match[3]);
const range: ContentRange = { unit: "bytes", start, end, total };
formatContentRange(range);
return range;
}How it works
Content-Range Header Builder and Parser keeps everything in the functions formatContentRange and parseContentRange.
RangeError and TypeError is the guard’s signal — thrown at the branch, before any partial work happens.
Caveats
- The guard
range.unit !== "bytes"short-circuits the flow with an earlythrowwhen its condition holds.
Putting it to use
The primary signature is export function formatContentRange(range: ContentRange)
Import formatContentRange from the file and call it — the rest of the code is implementation detail.
Core lines
if (typeof value !== "string") throw new TypeError("value must be a string");
throw new RangeError("total must be * or an integer greater than end");
return "bytes " + range.start + "-" + range.end + "/" + range.total;
Interactive Tools for This Topic
Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.
FAQ
- How are precondition violations signaled?
- `RangeError` and `TypeError` is raised at the guard (`range.unit !== "bytes"`), so invalid state never reaches the main logic.
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `formatContentRange`, `parseContentRange` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.