{"id":17696,"library":"http-range","title":"HTTP Range Header Parser","description":"The `http-range` package provides a Node.js-specific parser and creator for HTTP/1.1 `Content-Range` and `Range` header fields. It offers dedicated classes `ContentRange` and `Range` for handling these headers, allowing developers to parse incoming header strings into structured objects and construct valid header strings from component parts. Additionally, it exposes a `RangeSpec` class for granular control over individual range specifications. The package is currently at version 1.0.0, indicating a stable and mature API. Its primary focus is on strict adherence to RFC2616 specifications for range headers, differentiating it by offering distinct classes for both Content-Range and Range headers rather than a single generic utility function. Release cadence is not explicitly defined but the project appears to be stable with a specific and limited scope.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/clns/node-http-range","tags":["javascript","content-range","range","http","header","parser","http/1.1"],"install":[{"cmd":"npm install http-range","lang":"bash","label":"npm"},{"cmd":"yarn add http-range","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-range","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package primarily uses CommonJS `require()` syntax as shown in its README. For modern Node.js applications using ES modules, named imports are the correct approach. The `ContentRange` class is exported as a named property of the module.","wrong":"const ContentRange = require('http-range');","symbol":"ContentRange","correct":"import { ContentRange } from 'http-range';"},{"note":"Similar to `ContentRange`, the `Range` class is a named export. Ensure to use named imports when working with ES modules.","wrong":"const Range = require('http-range');","symbol":"Range","correct":"import { Range } from 'http-range';"},{"note":"The `RangeSpec` class, which represents individual byte range specifications, is also exported as a named member of the module.","wrong":"import RangeSpec from 'http-range';","symbol":"RangeSpec","correct":"import { RangeSpec } from 'http-range';"}],"quickstart":{"code":"const { ContentRange, Range } = require('http-range');\n\n// Example 1: Parsing and creating 'Content-Range' header\nconst contentRangeInput = 'bytes 0-49/50';\nconst parsedContentRange = new ContentRange().parse(contentRangeInput);\nconsole.log(`Parsed Content-Range: ${JSON.stringify(parsedContentRange.range)} / ${parsedContentRange.length}`);\nconst newContentRange = new ContentRange('bytes', '0-49', 50);\nconsole.log(`Created Content-Range: ${newContentRange.toString()}`);\n\n// Example 2: Parsing and creating 'Range' header with multiple specs\nconst rangeInput = 'bytes=0-49,50-99,-30';\nconst parsedRange = new Range().parse(rangeInput);\nconsole.log(`\\nParsed Range (unit): ${parsedRange.unit}`);\nparsedRange.ranges.forEach((r, i) => console.log(` - Range Spec ${i + 1}: low=${r.low ?? 'undefined'}, high=${r.high ?? 'undefined'}`));\nconst newRange = new Range('bytes', '0-49');\nconsole.log(`Created Range: ${newRange.toString()}`);\n\n// Example 3: Using RangeSpec directly (less common, for single range definition)\nconst rangeSpecInput = '100-200';\nconst parsedRangeSpec = new ContentRange().range.parse(rangeSpecInput); // Using ContentRange's prototype to parse\nconsole.log(`\\nParsed RangeSpec: low=${parsedRangeSpec.low}, high=${parsedRangeSpec.high}`);","lang":"javascript","description":"Demonstrates parsing and creating HTTP `Content-Range` and `Range` headers using the `ContentRange` and `Range` classes, including handling multiple range specifications and direct `RangeSpec` usage."},"warnings":[{"fix":"Wrap calls to constructors and `parse` methods (e.g., `new ContentRange().parse(input)`) in `try...catch` blocks to handle malformed header strings or invalid arguments gracefully.","message":"Invalid input to `ContentRange`, `Range`, or `RangeSpec` constructors or `parse` methods will throw synchronous errors. The library does not return null or a Promise for errors; it directly throws exceptions.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure all arguments passed to `ContentRange`, `Range`, or `RangeSpec` constructors conform to their expected types and formats (e.g., `length` must be a number or `'*'`). Consult the API documentation for each class's constructor signature.","cause":"Attempting to create an instance of `ContentRange`, `Range`, or `RangeSpec` with malformed or missing required arguments, such as passing a non-numeric value for `length` in `ContentRange`.","error":"Error: Invalid arguments for [ClassName] constructor"},{"fix":"Validate the input string against RFC2616 for `Content-Range` (e.g., `bytes 0-49/50`) or `Range` (e.g., `bytes=0-49,100-199`) before attempting to parse, or wrap the parse call in a `try...catch` block to handle parsing errors.","cause":"The input string provided to the `parse()` method (e.g., `new ContentRange().parse(input)`) does not conform to the HTTP/1.1 `Content-Range` or `Range` header specification.","error":"Error: Invalid [HeaderType] string"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}