Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { parse, format, combineRanges } from "range-parser";
Parse the given `header` string where `size` is the size of the selected representation that is to be partitioned into subranges. Always returns an object with `code`, `type`, and `ranges` properties.

- `code: 0` means all ranges were parsed successfully.
- `code: 1` means the header was malformed.
- `code: 1` means the header was malformed or contained an invalid range (such as reversed bounds `50-20`).
- `code: 2` means one or more ranges were unsatisfiable.
- `code: 3` means both malformed and unsatisfiable ranges were encountered (`1 | 2`).
- `code: 3` means both malformed or invalid ranges and unsatisfiable ranges were encountered (`1 | 2`).

Type is always lower case and is not validated against RFC token syntax, make sure to validate before using (e.g. `type === "bytes"`).

Expand Down Expand Up @@ -71,7 +71,8 @@ if (subranges && subranges.type === "bytes") {
if (ranges.length === 0) {
// Send 200 OK with `Content-Length: size` (including 0 for empty files).
} else if (ranges.length === 1) {
// Send 206 with `Content-Range: bytes ${range[0]}-${ranges[1]}/${size}` header.
const { start, end } = ranges[0];
// Send 206 with `Content-Range: bytes ${start}-${end}/${size}` header.
} else {
// Send 206 with `Content-Type: multipart/byteranges; boundary=...`.
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ describe("parse edge cases", () => {
["malformed", "bytes=abc-def"],
["unsatisfiable", "bytes=2000-3000"],
["overflowing position", `bytes=0-${"9".repeat(400)}`],
["long start position", `bytes=${"9".repeat(400)}-`],
["long malformed range", `bytes=${"x".repeat(400)}`],
["many ranges without dashes", `bytes=${"x,".repeat(100)}`],
["many invalid ranges", `bytes=${"x,".repeat(100)}0-99`],
]) {
test(name, ({ bench }) => {
Expand Down
46 changes: 32 additions & 14 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ describe("parse(size, str)", () => {
});
});

it("should retain a valid range after a range missing its dash", () => {
expect(parse(200, "bytes=100200,0-9")).toEqual({
code: 1,
type: "bytes",
ranges: [{ start: 0, end: 9 }],
});
});

it("should return code 1 for empty range value", () => {
expect(parse(200, "bytes=")).toEqual({
code: 1,
Expand Down Expand Up @@ -325,38 +333,48 @@ describe("parse(size, str)", () => {
});
});

it.each([
"bytes=50-20",
"bytes=500-20",
"bytes=600-500",
"bytes=9007199254740993-9007199254740992",
`bytes=${"9".repeat(400)}-${"8".repeat(400)}`,
])("should return code 2 for reversed bounds in %s", (header) => {
expect(parse(200, header)).toEqual({ code: 2, type: "bytes", ranges: [] });
});
it.each(["bytes=50-20", "bytes=500-20", "bytes=600-500"])(
"should return code 1 for reversed bounds in %s",
(header) => {
expect(parse(200, header)).toEqual({
code: 1,
type: "bytes",
ranges: [],
});
},
);

it("should preserve valid ranges when explicit bounds are reversed", () => {
expect(parse(200, "bytes=50-20,0-10")).toEqual({
code: 2,
code: 1,
type: "bytes",
ranges: [{ start: 0, end: 10 }],
});
});

it("should report reversed bounds together with unsatisfiable ranges", () => {
expect(parse(200, "bytes=50-20,500-600")).toEqual({
code: 2,
code: 3,
type: "bytes",
ranges: [],
});
});

it.each([
"bytes=9007199254740993-9007199254740992",
`bytes=${"9".repeat(400)}-${"8".repeat(400)}`,
"bytes=9007199254740992-9007199254740993",
`bytes=${"8".repeat(400)}-${"9".repeat(400)}`,
])("should keep large ordered bounds unsatisfiable in %s", (header) => {
expect(parse(200, header)).toEqual({ code: 2, type: "bytes", ranges: [] });
});
])(
"should report code 2 for bounds beyond numeric precision in %s",
(header) => {
expect(parse(200, header)).toEqual({
code: 2,
type: "bytes",
ranges: [],
});
},
);

it("should return code 3 when ranges are malformed and unsatisfiable", () => {
expect(parse(200, "bytes=abc-def,500-999")).toEqual({
Expand Down
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function parse(size: number, str: string): Result {
let index = eqIndex + 1;
let code: Result["code"] = 0;
let hasRange = false;
let dashIndex = -1;

do {
const commaIndex = str.indexOf(",", index);
Expand Down Expand Up @@ -99,8 +100,13 @@ export function parse(size: number, str: string): Result {

hasRange = true;

const dashIndex = str.indexOf("-", startStrIndex);
if (dashIndex === -1 || dashIndex > endStrIndex) {
// Find the next dash.
if (dashIndex < startStrIndex) {
dashIndex = str.indexOf("-", startStrIndex);
if (dashIndex === -1) dashIndex = len;
}

if (dashIndex >= endStrIndex) {
code |= 1; // Invalid range.
continue;
}
Expand All @@ -109,9 +115,10 @@ export function parse(size: number, str: string): Result {
let end = NaN;

if (startStrIndex === dashIndex) {
// Just "-" is not valid.
if (endStrIndex === dashIndex + 1) {
code |= 1; // Invalid range.
continue; // Just "-" is not valid.
continue;
}

// suffix-byte-range-spec, e.g. "-500"
Expand All @@ -128,11 +135,19 @@ export function parse(size: number, str: string): Result {
end = size - 1;
} else {
end = parsePos(str, dashIndex + 1, endStrIndex);

// Malformed reversed range.
if (start > end) {
code |= 1;
continue;
}

// Incorrect end, resize.
if (end > size - 1) end = size - 1;
}
}

if (isNaN(start) || isNaN(end)) {
if (Number.isNaN(start) || Number.isNaN(end)) {
code |= 1; // Unable to parse.
continue;
}
Expand Down
Loading