npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@httpland/range-parser

v1.2.0

Published

HTTP Range header field parser

Downloads

333

Readme

range-parser

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

HTTP Range header field parser.

Compliant with RFC 9110, 14.2 Range

Deserialization

Parses a string as HTTP Range header field and yield JavaScript Object.

The field naming conventions follow RFC 9110, 14.2. Range.

import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const actual = parseRange("bytes=0-100, 200-, -300, test");

assertEquals(actual, {
  rangeUnit: "bytes",
  rangeSet: [
    { firstPos: 0, lastPos: 100 },
    { firstPos: 200, lastPos: undefined },
    { suffixLength: 300 },
    "test",
  ],
});

rangeSet is a list of one or more <int-range>, <suffix-range> and <other-range> according to the definition of <range-spec>.

It has the following data structure:

interface IntRange {
  firstPos: number;
  lastPos: number | undefined;
}
interface SuffixRange {
  suffixLength: number;
}
type OtherRange = string;

Parsing specification

The parser strictly adheres to the ABNF syntax. It also checks semantics.

Specifically, the parser guarantees the following:

  • The <int-range> or <suffix-range> number is a non-negative integer
  • <range-unit> and <other-range> are syntactically valid strings
  • <int-range>, <first-pos> is equal to or greater than <last-pos>.

Syntax error

Throws SyntaxError if it detects invalid syntax.

import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => parseRange("<invalid:input>"));

Semantic error

The following cases are semantic error:

  • <int-range>, <last-pos> less than <first-pos>.

see RFC 9110, 14.1.1. Range Specifiers

In this case, it throws a RangeError.

import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => parseRange("bytes=1-0"));

Serialization

Serializes Range into string.

import { stringifyRange } from "https://deno.land/x/range_parser@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

assertEquals(
  stringifyRange({
    rangeUnit: "bytes",
    rangeSet: [{ firstPos: 0, lastPos: 100 }, { suffixLength: 200 }],
  }),
  "bytes=0-100, -200",
);

Throwing error

Throws TypeError if Range contains errors.

For error definitions, see the Parsing specification.

import { stringifyRange } from "https://deno.land/x/range_parser@$VERSION/stringify.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() =>
  stringifyRange({
    rangeUnit: "bytes",
    rangeSet: [{ firstPos: NaN, lastPos: undefined }],
  })
);

Utility

We provide some utilities.

isIntRange

Whether the RangeSpec is IntRange or not.

import {
  type IntRange,
  isIntRange,
  type OtherRange,
  type SuffixRange,
} from "https://deno.land/x/range_parser@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const intRange: IntRange;
declare const suffixRange: SuffixRange;
declare const otherRange: OtherRange;

assert(isIntRange(intRange));
assert(!isIntRange(suffixRange));
assert(!isIntRange(otherRange));

isSuffixRange

Whether the RangeSpec is SuffixRange or not.

import {
  type IntRange,
  isSuffixRange,
  type OtherRange,
  type SuffixRange,
} from "https://deno.land/x/range_parser@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const intRange: IntRange;
declare const suffixRange: SuffixRange;
declare const otherRange: OtherRange;

assert(isSuffixRange(suffixRange));
assert(!isSuffixRange(intRange));
assert(!isSuffixRange(otherRange));

isOtherRange

Whether the RangeSpec is OtherRange or not.

import {
  type IntRange,
  isOtherRange,
  type OtherRange,
  type SuffixRange,
} from "https://deno.land/x/range_parser@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const intRange: IntRange;
declare const suffixRange: SuffixRange;
declare const otherRange: OtherRange;

assert(isOtherRange(otherRange));
assert(!isOtherRange(intRange));
assert(!isOtherRange(suffixRange));

isRangeFormat

Whether the input is HTTP Range header field format or not.

import { isRangeFormat } from "https://deno.land/x/range_parser@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

assert(isRangeFormat("bytes=0-100, 200-, -500"));
assert(!isRangeFormat("<invalid>"));

License

Copyright © 2023-present httpland.

Released under the MIT license