Range header field parser.
npm install range-parserimport { parse, combineRanges } from "range-parser";Parse the given header string where size is the size of the selected representation that is to be partitioned into subranges. A type and an array of subranges will be returned or negative numbers indicating an error parsing.
-2signals a malformed header string-1signals an unsatisfiable range
Note: size must be a safe integer or a TypeError is thrown. The maximum supported file size is Number.MAX_SAFE_INTEGER (9,007,199,254,740,991 bytes), about 9 PB.
// parse header from request
var header = req.headers.range;
var subranges = typeof header === "string" ? parse(size, header) : -2;
// the type of the subranges
if (typeof subranges === "object" && subranges.type === "bytes") {
// the ranges
subranges.ranges.forEach(function (r) {
// do something with r.start and r.end
});
}Overlapping and adjacent ranges will be combined and returned.
Returned ranges include the index used to preserve their original order.
const subranges = parse(100, "bytes=50-55,0-10,5-10,56-60");
if (typeof subranges === "object") {
combineRanges(subranges.ranges);
// => [
// { start: 50, end: 60, index: 0 },
// { start: 0, end: 10, index: 1 }
// ]
}