@se-oss/range-parser is a lightweight, fast parser for HTTP Range headers. It supports multi-range requests, range combination.
npm i @se-oss/range-parser
import { parseRange } from '@se-oss/range-parser';
const size = 1000;
const str = 'bytes=500-999';
console.log(parseRange(size, str));
// [ { start: 500, end: 999 }, type: 'bytes' ]
const multi = 'bytes=0-4,90-99,5-75,100-199,101-102';
console.log(parse(150, multi, { combine: true }));
// [ { start: 0, end: 75 }, { start: 90, end: 149 }, type: 'bytes' ]
For all configuration options, please see the API docs.
/**
* Parse "Range" header `str` relative to the given file `size`.
*
* @param size - The size of the file.
* @param str - The Range header string.
* @param options - Optional configuration.
* @returns Array of ranges if valid, -1 for unsatisfiable ranges, or -2 for malformed header.
*/
declare function parseRange(size: number, str: string, options?: RangeParserOptions): RangeArray | -1 | -2;
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! π
This package is a TypeScript port of jshttp/range-parser with support for both CommonJS and ECMAScript modules.
MIT Β© Shahrad Elahi and contributors.