@httpland/accept-ranges-parser
Version:
HTTP Accept-Ranges header field parser
88 lines (57 loc) • 3.22 kB
Markdown
# accept-ranges-parser
[](https://deno.land/x/accept_ranges_parser)
[](https://doc.deno.land/https/deno.land/x/accept_ranges_parser/mod.ts)
[](https://github.com/httpland/accept-ranges-parser/releases)
[](https://codecov.io/gh/httpland/accept-ranges-parser)
[](https://github.com/httpland/accept-ranges-parser/blob/main/LICENSE)
[](https://github.com/httpland/accept-ranges-parser/actions/workflows/test.yaml)
[](https://nodei.co/npm/@httpland/accept-ranges-parser/)
HTTP Accept-Ranges header field parser.
Compliant with
[RFC 9110, 14.3. Accept-Ranges](https://www.rfc-editor.org/rfc/rfc9110#section-14.3).
## Deserialization
Parses string into [AcceptRanges](#acceptranges).
```ts
import { parseAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals(parseAcceptRanges(`none`), ["none"]);
assertEquals(parseAcceptRanges(`bytes, unknown`), ["bytes", "unknown"]);
```
### Throwing error
Throws `SyntaxError` if the input is invalid
[`<Accept-Ranges>`](https://www.rfc-editor.org/rfc/rfc9110#section-14.3-2)
syntax.
```ts
import { parseAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => parseAcceptRanges("<invalid>"));
```
## Serialization
Serialize string of array into string.
```ts
import { stringifyAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals(stringifyAcceptRanges(["bytes"]), "bytes");
```
### Throwing error
Throws `TypeError` if the element contains invalid
[`<range-unit>`](https://www.rfc-editor.org/rfc/rfc9110#section-14.1-3) syntax.
```ts
import { stringifyAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/stringify.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => stringifyAcceptRanges(["<invalid>", "none"]));
```
## AcceptRanges
`AcceptRanges` is a subtype of array with the following characteristics.
- It has one or more elements.
- The elements are [Token](#token).
### Token
Represents a character set that compliant with
[RFC 9110, 5.6.2. Tokens](https://www.rfc-editor.org/rfc/rfc9110#section-5.6.2)
as much as possible.
## API
All APIs can be found in the
[deno doc](https://doc.deno.land/https/deno.land/x/accept_ranges_parser/mod.ts).
## License
Copyright © 2023-present [httpland](https://github.com/httpland).
Released under the [MIT](./LICENSE) license