Gathering detailed insights and metrics for @httpland/range-parser
Gathering detailed insights and metrics for @httpland/range-parser
npm install @httpland/range-parser
Typescript
Module System
Node Version
NPM Version
73.8
Supply Chain
98.8
Quality
75.6
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
9,290
Last Day
72
Last Week
177
Last Month
738
Last Year
7,574
MIT License
50 Commits
2 Branches
2 Contributors
Updated on Mar 09, 2023
Latest Version
1.2.0
Package Id
@httpland/range-parser@1.2.0
Unpacked Size
40.45 kB
Size
6.85 kB
File Count
23
NPM Version
8.19.3
Node Version
16.19.1
Published on
Mar 28, 2023
Cumulative downloads
Total Downloads
Last Day
125%
72
Compared to previous day
Last Week
7.3%
177
Compared to previous week
Last Month
118.3%
738
Compared to previous month
Last Year
341.4%
7,574
Compared to previous year
2
HTTP Range
header field parser.
Compliant with RFC 9110, 14.2 Range
Parses a string as HTTP Range
header field and yield JavaScript Object.
The field naming conventions follow RFC 9110, 14.2. Range.
1import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts"; 2import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; 3 4const actual = parseRange("bytes=0-100, 200-, -300, test"); 5 6assertEquals(actual, { 7 rangeUnit: "bytes", 8 rangeSet: [ 9 { firstPos: 0, lastPos: 100 }, 10 { firstPos: 200, lastPos: undefined }, 11 { suffixLength: 300 }, 12 "test", 13 ], 14});
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:
1interface IntRange { 2 firstPos: number; 3 lastPos: number | undefined; 4} 5interface SuffixRange { 6 suffixLength: number; 7} 8type OtherRange = string;
The parser strictly adheres to the ABNF syntax. It also checks semantics.
Specifically, the parser guarantees the following:
<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>
.Throws SyntaxError
if it detects invalid syntax.
1import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => parseRange("<invalid:input>"));
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
.
1import { parseRange } from "https://deno.land/x/range_parser@$VERSION/parse.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => parseRange("bytes=1-0"));
Serializes Range
into string.
1import { stringifyRange } from "https://deno.land/x/range_parser@$VERSION/stringify.ts"; 2import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; 3 4assertEquals( 5 stringifyRange({ 6 rangeUnit: "bytes", 7 rangeSet: [{ firstPos: 0, lastPos: 100 }, { suffixLength: 200 }], 8 }), 9 "bytes=0-100, -200", 10);
Throws TypeError
if Range
contains errors.
For error definitions, see the Parsing specification.
1import { stringifyRange } from "https://deno.land/x/range_parser@$VERSION/stringify.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => 5 stringifyRange({ 6 rangeUnit: "bytes", 7 rangeSet: [{ firstPos: NaN, lastPos: undefined }], 8 }) 9);
We provide some utilities.
Whether the RangeSpec
is IntRange
or not.
1import { 2 type IntRange, 3 isIntRange, 4 type OtherRange, 5 type SuffixRange, 6} from "https://deno.land/x/range_parser@$VERSION/mod.ts"; 7import { assert } from "https://deno.land/std/testing/asserts.ts"; 8 9declare const intRange: IntRange; 10declare const suffixRange: SuffixRange; 11declare const otherRange: OtherRange; 12 13assert(isIntRange(intRange)); 14assert(!isIntRange(suffixRange)); 15assert(!isIntRange(otherRange));
Whether the RangeSpec
is SuffixRange
or not.
1import { 2 type IntRange, 3 isSuffixRange, 4 type OtherRange, 5 type SuffixRange, 6} from "https://deno.land/x/range_parser@$VERSION/mod.ts"; 7import { assert } from "https://deno.land/std/testing/asserts.ts"; 8 9declare const intRange: IntRange; 10declare const suffixRange: SuffixRange; 11declare const otherRange: OtherRange; 12 13assert(isSuffixRange(suffixRange)); 14assert(!isSuffixRange(intRange)); 15assert(!isSuffixRange(otherRange));
Whether the RangeSpec
is OtherRange
or not.
1import { 2 type IntRange, 3 isOtherRange, 4 type OtherRange, 5 type SuffixRange, 6} from "https://deno.land/x/range_parser@$VERSION/mod.ts"; 7import { assert } from "https://deno.land/std/testing/asserts.ts"; 8 9declare const intRange: IntRange; 10declare const suffixRange: SuffixRange; 11declare const otherRange: OtherRange; 12 13assert(isOtherRange(otherRange)); 14assert(!isOtherRange(intRange)); 15assert(!isOtherRange(suffixRange));
Whether the input is HTTP Range
header field format or not.
1import { isRangeFormat } from "https://deno.land/x/range_parser@$VERSION/mod.ts"; 2import { assert } from "https://deno.land/std/testing/asserts.ts"; 3 4assert(isRangeFormat("bytes=0-100, 200-, -500")); 5assert(!isRangeFormat("<invalid>"));
Copyright © 2023-present httpland.
Released under the MIT license
No vulnerabilities found.
No security vulnerabilities found.