# header-range-parser

> Range header field string parser.

Latest version **2.0.0** (published 2026-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install header-range-parser
pnpm add header-range-parser
yarn add header-range-parser
bun add header-range-parser
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-05-19 |
| First published | 2021-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.19.0 |
| Dependencies | 0 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Anton Trofymenko |
| Maintainers | r37r0m0d3l |
| Keywords | header, http, parser, range |

## Links

- npm: https://www.npmjs.com/package/header-range-parser
- Repository: https://github.com/r37r0m0d3l/header-range-parser
- Issues: https://github.com/r37r0m0d3l/header-range-parser/issues
- npm.io page: https://npm.io/package/header-range-parser

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2026-05-19
- 1.1.5 — 2026-02-04
- 1.1.4 — 2026-02-03
- 1.1.3 — 2022-01-06
- 1.1.2 — 2021-12-23
- 1.1.1 — 2021-10-18
- 1.1.0 — 2021-10-18
- 1.0.0 — 2021-10-02
- 0.0.7 — 2021-10-02
- 0.0.6 — 2021-10-01
- 0.0.5 — 2021-10-01
- 0.0.4 — 2021-10-01
- 0.0.3 — 2021-09-30
- 0.0.2 — 2021-09-30
- 0.0.1 — 2021-09-30

## README

# Header • Range • Parser

![Header • Range • Parser](https://raw.githubusercontent.com/r37r0m0d3l/header-range-parser/master/.github/assets/logo_200.webp?raw=true "Header • Range • Parser")

Range header field parser. Fork of  a̶b̶a̶n̶d̶o̶n̶e̶d̶ [range-parser](https://github.com/jshttp/range-parser).

[![NPM Version][npm-version-img]][npm-version-url]
[![NPM Downloads][npm-downloads-img]][npm-downloads-url]
[![Node.js Version][node-image]][node-url]
[![TypeScript Typings][ts-img]][ts-url]
[![Codacy Badge][codacy-img]][codacy-url]
[![CodeFactor][codefactor-img]][codefactor-url]

[//]: # ([![GitHub Checks][gh-checks-img]][gh-checks-url])
[//]: # ([![GitHub Stars][gh-stars-img]][gh-stars-url])
[//]: # ([![LGTM][lgtm-img]][lgtm-url])
[//]: # ([![Maintainability Rating][sonarcloud-img]][sonarcloud-url])
[//]: # ([![Snyk][snyk-img]][snyk-url])
[//]: # ([![Travis CI][travis-img]][travis-url])

[comment]: <> ([![Dependabot][dependabot-img]][dependabot-url])

## Installation

```bash
npm install header-range-parser
```

The package ships a single ESM file build. On Node.js 20.19+, CommonJS callers can keep using `require()`.

## API

<!-- eslint-disable no-unused-vars -->

```js
import {
  ERROR_INVALID_ARGUMENT,
  ERROR_STRING_IS_NOT_HEADER,
  ERROR_UNSATISFIABLE_RESULT,
  parseRange,
} from "header-range-parser";
```

```typescript
import {
  ERROR_INVALID_ARGUMENT,
  ERROR_STRING_IS_NOT_HEADER,
  ERROR_UNSATISFIABLE_RESULT,
  ResultInvalid,
  ResultUnsatisfiable,
  ResultWrongArgument,
  parseRange,
} from "header-range-parser";
```

### parseRange(size, header, options)

```typescript
import {
  Result, Ranges, parseRange, Options,
} from "header-range-parser";

declare function parseRange(
  size: number, header: string, options?: Options,
): Ranges | Result;
```

| Parameter | Type     | Description                                          |
|:----------|:---------|:-----------------------------------------------------|
| `size`    | `number` | **Required**. Size in bytes.                         |
| `header`  | `string` | **Required**. String containing header.              |
| `options` | `object` | Optional options: combine (bool), throwError (bool). |

Parse the given `header` string where `size` is the size of the selected
representation that is to be partitioned into sub-ranges. An array of sub-ranges
will be returned or negative numbers indicating an error parsing.

- `-1` or `ERROR_UNSATISFIABLE_RESULT` or ` ResultUnsatisfiable` signals an unsatisfiable range

- `-2` or `ERROR_STRING_IS_NOT_HEADER` or `ResultInvalid` signals a malformed header string

- `-3` or `ERROR_INVALID_ARGUMENT` or `ResultWrongArgument` invalid parameters

<!-- eslint-disable no-undef -->

```js
// parse header from request
const subRanges = parseRange(
  size,
  request.headers.range,
);

// the type of the subranges
if (subRanges.type === "bytes") {
  // the ranges
  subRanges.forEach((range) => {
    // do something
    // with range.start
    // and range.end
  });
}
```

#### Options

These properties are accepted in the options object.

##### combine

Specifies if overlapping and adjacent sub-ranges should be combined, defaults to `false`.

When `true`, ranges will be combined and returned as if they were specified that way in the header.

##### throwError

Throw or suppress errors. Defaults to `true`.

<!-- eslint-disable no-undef -->

```js
parseRange(
  100,
  "bytes=50-55,0-10,5-10,56-60",
  {
    combine: true,
    throwError: false,
  });
//  [
//    { start: 0,  end: 10 },
//    { start: 50, end: 60 }
//  ]
```

[//]: # (## See also)

[//]: # ()
[//]: # ([💾 My other projects]&#40;https://r37r0m0d3l.icu/open_source_map&#41;)

[//]: # ()
[//]: # (<img alt="Open Source" src="https://raw.githubusercontent.com/r37r0m0d3l/r37r0m0d3l/master/osmap.svg?sanitize=true" width="960" height="520" style="display:block;height:auto;margin-left:auto;margin-right:auto;min-height:520px;min-width:960px;width:100%;">)

<!-- Badges -->

[npm-version-img]: https://badgen.net/npm/v/header-range-parser?&icon=npm&label=npm&color=DD3636&v=1.1.1
[npm-version-url]: https://npmjs.com/package/header-range-parser

[npm-downloads-img]: https://badgen.net/npm/dt/header-range-parser?&icon=terminal&label=downloads&color=009688&v=1.1.1
[npm-downloads-url]: https://npmjs.com/package/header-range-parser

[gh-stars-img]: https://badgen.net/github/stars/r37r0m0d3l/header-range-parser?&icon=github&label=stars&color=FFCC33&v=1.1.1
[gh-stars-url]: https://github.com/r37r0m0d3l/header-range-parser

[node-image]: https://badgen.net/npm/node/header-range-parser
[node-url]: https://nodejs.org/en/download

[gh-checks-img]: https://badgen.net/github/checks/r37r0m0d3l/header-range-parser?&icon=github&v=1.1.1
[gh-checks-url]: https://github.com/r37r0m0d3l/header-range-parser

[travis-img]: https://badgen.net/travis/r37r0m0d3l/header-range-parser?&icon=travis&label=build&v=1.1.1
[travis-url]: https://travis-ci.com/github/r37r0m0d3l/header-range-parser

[ts-img]: https://badgen.net/npm/types/header-range-parser?&icon=typescript&label=types&color=1E90FF&v=1.1.1
[ts-url]: https://github.com/r37r0m0d3l/header-range-parser/blob/main/dist/index.d.ts

[sonarcloud-img]: https://sonarcloud.io/api/project_badges/measure?project=r37r0m0d3l_header-range-parser&metric=sqale_rating&v=1.1.1
[sonarcloud-url]: https://sonarcloud.io/dashboard?id=r37r0m0d3l_header-range-parser

[lgtm-img]: https://badgen.net/lgtm/grade/g/r37r0m0d3l/header-range-parser?&icon=lgtm&label=lgtm:js/ts&color=00C853&v=1.1.1
[lgtm-url]: https://lgtm.com/projects/g/r37r0m0d3l/header-range-parser/context:javascript

[codacy-img]: https://app.codacy.com/project/badge/Grade/b3458c991041406bbe85fdfd87498006
[codacy-url]: https://www.codacy.com/gh/r37r0m0d3l/header-range-parser/dashboard?&utm_source=github.com&amp;utm_medium=referral&amp;utm_content=r37r0m0d3l/header-range-parser&amp;utm_campaign=Badge_Grade

[snyk-img]: https://badgen.net/snyk/r37r0m0d3l/header-range-parser?&v=1.1.1
[snyk-url]: https://github.com/r37r0m0d3l/header-range-parser

[dependabot-img]: https://badgen.net/dependabot/r37r0m0d3l/header-range-parser?&icon=dependabot&v=1.1.1
[dependabot-url]: https://github.com/r37r0m0d3l/header-range-parser

[codefactor-img]: https://www.codefactor.io/repository/github/r37r0m0d3l/header-range-parser/badge?&style=flat-square&v=1.1.1
[codefactor-url]: https://www.codefactor.io/repository/github/r37r0m0d3l/header-range-parser

---
_Source: https://npm.io/package/header-range-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
