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.

TypescriptData Integrity

Code

typescriptsnippet
37 lines
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 early throw when 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

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.