From 5bd620a6cb2f32c2d8085f92e5dc131859527655 Mon Sep 17 00:00:00 2001 From: yxshee <84263676+yxshee@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:18:00 +0530 Subject: [PATCH] Accept tab as optional whitespace around ranges The TypeScript rewrite only skips spaces around each range-spec, so a tab (also OWS in RFC 9110) makes the range invalid. 1.2.1 used trim() and accepted it. --- src/index.spec.ts | 12 ++++++++++++ src/index.ts | 11 +++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index e962ffe..17a6a40 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -212,6 +212,18 @@ describe("parse(len, str)", () => { }); }); + it("should parse str with tab whitespace", () => { + var range = parse(1000, "bytes=\t40-80,\t81-90\t,-1\t"); + expect(range).toEqual({ + type: "bytes", + ranges: [ + { start: 40, end: 80 }, + { start: 81, end: 90 }, + { start: 999, end: 999 }, + ], + }); + }); + it("should parse non-byte range", () => { var range = parse(1000, "items=0-5"); expect(range).toEqual({ diff --git a/src/index.ts b/src/index.ts index 8640bea..d5b18f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,8 +51,8 @@ export function parse(size: number, str: string): Result | -1 | -2 { // handle ows around numbers let startStrIndex = index; let endStrIndex = endIndex; - while (str.charCodeAt(startStrIndex) === 32) startStrIndex++; - while (str.charCodeAt(endStrIndex - 1) === 32) endStrIndex--; + while (isOws(str.charCodeAt(startStrIndex))) startStrIndex++; + while (isOws(str.charCodeAt(endStrIndex - 1))) endStrIndex--; let start = NaN; let end = NaN; @@ -103,6 +103,13 @@ export function parse(size: number, str: string): Result | -1 | -2 { return { type, ranges }; } +/** + * Check for optional whitespace (SP / HTAB), RFC 9110 section 5.6.3. + */ +function isOws(code: number): boolean { + return code === 32 || code === 9; +} + /** * Parse string to integer. */