Gathering detailed insights and metrics for @httpland/content-range-parser
Gathering detailed insights and metrics for @httpland/content-range-parser
Gathering detailed insights and metrics for @httpland/content-range-parser
Gathering detailed insights and metrics for @httpland/content-range-parser
npm install @httpland/content-range-parser
Typescript
Module System
Node Version
NPM Version
73.6
Supply Chain
99.4
Quality
75.2
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
1,791
Last Day
1
Last Week
3
Last Month
20
Last Year
1,222
MIT License
27 Commits
2 Branches
2 Contributors
Updated on Mar 29, 2023
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
@httpland/content-range-parser@1.0.0
Unpacked Size
42.98 kB
Size
7.59 kB
File Count
32
NPM Version
8.19.3
Node Version
16.19.1
Published on
Mar 29, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-40%
3
Compared to previous week
Last Month
122.2%
20
Compared to previous month
Last Year
114.8%
1,222
Compared to previous year
1
HTTP Content-Range header field parser.
Compliant with RFC 9110, 14.4. Content-Range.
Parses string into ContentRange.
1import { parseContentRange } from "https://deno.land/x/content_range_parser@$VERSION/parse.ts"; 2import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; 3 4assertEquals(parseContentRange("bytes 0-100/1000"), { 5 rangeUnit: "bytes", 6 firstPos: 0, 7 lastPos: 100, 8 completeLength: 1000, 9}); 10assertEquals(parseContentRange("bytes 100-200/*"), { 11 rangeUnit: "bytes", 12 firstPos: 100, 13 lastPos: 200, 14 completeLength: undefined, 15}); 16assertEquals(parseContentRange("bytes */1000"), { 17 rangeUnit: "bytes", 18 completeLength: 1000, 19});
If input is invalid, the following error occurs:
Throws SyntaxError
if the input is invalid
<Content-Range>
syntax.
1import { parseContentRange } from "https://deno.land/x/content_range_parser@$VERSION/parse.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => parseContentRange("<invalid>"));
Throw Error
if the input contains invalid semantics.
Invalid semantics are indicated as follows:
A Content-Range field value is invalid if it contains a range-resp that has a last-pos value less than its first-pos value, or a complete-length value less than or equal to its last-pos value. The recipient of an invalid Content-Range MUST NOT attempt to recombine the received content with a stored representation.
1import { parseContentRange } from "https://deno.land/x/content_range_parser@$VERSION/parse.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => parseContentRange("bytes 100-0/*")); 5assertThrows(() => parseContentRange("bytes 100-200/0"));
Serialize ContentRange into string.
1import { stringifyContentRange } from "https://deno.land/x/content_range_parser@$VERSION/stringify.ts";
2import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3
4assertEquals(
5 stringifyContentRange({
6 rangeUnit: "bytes",
7 firstPos: 0,
8 lastPos: 100,
9 completeLength: 1000,
10 }),
11 "bytes 0-100/1000",
12);
13assertEquals(
14 stringifyContentRange({
15 rangeUnit: "bytes",
16 firstPos: 100,
17 lastPos: 200,
18 completeLength: undefined,
19 }),
20 "bytes 100-200/*",
21);
22assertEquals(
23 stringifyContentRange({ rangeUnit: "bytes", completeLength: 1000 }),
24 "bytes */1000",
25);
Throws TypeError
if ContentRange contains invalid value.
1import { stringifyContentRange } from "https://deno.land/x/content_range_parser@$VERSION/stringify.ts"; 2import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; 3 4assertThrows(() => 5 stringifyContentRange({ rangeUnit: "<range-unit>", completeLength: NaN }) 6);
then, semantic errors are also checked.
1import { stringifyContentRange } from "https://deno.land/x/content_range_parser@$VERSION/stringify.ts";
2import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
3
4assertThrows(() =>
5 stringifyContentRange({
6 rangeUnit: "<range-unit>",
7 firstPos: 1,
8 lastPos: 0, // firstPos <= lastPos
9 completeLength: undefined,
10 })
11);
12assertThrows(() =>
13 stringifyContentRange({
14 rangeUnit: "<range-unit>",
15 firstPos: 0,
16 lastPos: 100,
17 completeLength: 0, // lastPos < completeLength
18 })
19);
ContentRange
is a structured object for Content-Range
header.
Each field name is from camel Case of ABNF definition name.
Name | Type | Description |
---|---|---|
rangeUnit | string | Representation of <range-unit> . |
and
Name | Type | Description |
---|---|---|
firstPost | number | Representation of <first-pos> . |
lastPos | number | Representation of <last-pos> . |
completeLength | number | undefined | Representation of <unsatisfied-range> . |
or
Name | Type | Description |
---|---|---|
completeLength | number | Representation of <unsatisfied-range> |
It provides utilities.
Whether the input is RangeResp
or not.
1import { 2 type ContentRange, 3 isRangeResp, 4} from "https://deno.land/x/content_range_parser@$VERSION/mod.ts"; 5import { assert } from "https://deno.land/std/testing/asserts.ts"; 6 7declare const contentRange: ContentRange; 8assert(isRangeResp(contentRange));
Whether the input is UnsatisfiedRange
or not.
1import { 2 type ContentRange, 3 isUnsatisfiedRange, 4} from "https://deno.land/x/content_range_parser@$VERSION/mod.ts"; 5import { assert } from "https://deno.land/std/testing/asserts.ts"; 6 7declare const contentRange: ContentRange; 8assert(isUnsatisfiedRange(contentRange));
All APIs can be found in the deno doc.
Copyright © 2023-present httpland.
Released under the MIT license
No vulnerabilities found.
No security vulnerabilities found.