http-content-range-format

raw JSON →
1.0.0 verified Sat Apr 25 auth: no javascript

Format HTTP Content-Range headers according to RFC 7233. Version 1.0.0 is the latest and stable. This package provides a simple function to construct Content-Range header strings from numeric components. It is minimal, dependency-free, and focused on server-side use cases. No significant breaking changes or known vulnerabilities as of the release.

error TypeError: Cannot read property 'first' of undefined
cause Calling format() without arguments or with undefined/null.
fix
Pass an object with required properties: format({ first: 0, last: 499, length: 1234 })
error TypeError: format is not a function
cause Incorrect import (e.g., destructuring default export in CommonJS).
fix
Use default import: const format = require('http-content-range-format')
gotcha The library expects object properties 'first', 'last', 'length', and 'total' exactly as documented; missing required fields will cause errors.
fix Ensure all required fields are provided: for a standard range, include 'first' and 'last'; for suffix range, include 'length' and 'last'; for unsatisfied, include 'length' (or 'total' without 'first'/'last').
gotcha The 'total' field must be provided for a complete range; if omitted, the header will use '*' for total length.
fix Provide 'total' when the total size is known; otherwise, it's optional.
npm install http-content-range-format
yarn add http-content-range-format
pnpm add http-content-range-format

Demonstrates formatting Content-Range headers with different range types: first-last, suffix, and unsatisfied.

import format from 'http-content-range-format';

// Format a Content-Range header for bytes 0-499/1234
const header = format({ first: 0, last: 499, length: 1234 });
console.log(header); // 'bytes 0-499/1234'

// Format a suffix range (last N bytes)
const suffixHeader = format({ length: 100, last: 99, total: 1000 });
console.log(suffixHeader); // 'bytes -99/1000'

// Format an unsatisfied range (length only)
const unsatisfiedHeader = format({ length: 0, total: 500 });
console.log(unsatisfiedHeader); // 'bytes */500'