range-parsest v1.0.0
range-parsest
Range header field parser.
Installation
You can install range-parsest
through NPM:
npm i range-parser
Methods
import { parse } from "range-parsest";
parse(size, header, options)
Parse the given header
string where size
is the size of the selected
representation that is to be partitioned into subranges. A ParseResult will be returned.
Example ParseResult:
// A file of size 255 and a header requesting the first 100 bytes.
const result = parse(255, "bytes=0-100");
// Result is
({
type: "bytes",
ranges: [{ start: 0, end: 100 }],
rangeCount: 1,
});
You can use a destructuring assignment to get these values quickly. For example:
// parse header from request
const { type, ranges } = parse(size, req.headers.range);
// the type of the subranges
if (type === "bytes") {
for (const { start, end } of ranges) {
// do something with start and end
}
}
parseCompat
This function only exists for easy switching to range-parsest, please use the normal parse()
method instead.
This function is fully compatible with the original range-parser's parseRange. You use the exact same API, but instead of importing it as
var parseRange = require("range-parser");
you import it as
import { parseCompat as parseRange } from "range-parsest";
ParseOptions
These properties are accepted in the options parameter of the parse function:
combine
Specifies if overlapping & adjacent subranges should be combined, defaults to
false
. When true
, ranges will be combined and returned as if they were
specified that way in the header.
parseRange(100, "bytes=50-55,0-10,5-10,56-60", { combine: true });
// Results in
({
type: "bytes",
ranges: [
{ start: 50, end: 55 },
{ start: 0, end: 10 },
{ start: 50, end: 60 },
],
rangeCount: 3,
});
License
MIT
4 years ago