Skip to content

About

Range header field parser

Topics

Resources

Code of conduct

Security policy

Stars

99 stars

Watchers

10 watching

Forks

Latest commit

 

History

173 Commits

Folders and files

Repository files navigation

range-parser

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Range header field parser.

Installation

npm install range-parser

API

import { parse, combineRanges } from "range-parser";

parse(size, header)

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.

  • -2 signals a malformed header string
  • -1 signals 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
  });
}

combineRanges(ranges)

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 }
  //    ]
}

License

MIT

About

Range header field parser

Topics

Resources

Code of conduct

Security policy

Stars

99 stars

Watchers

10 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages