The `parse-headers` package is a lightweight utility designed to transform raw HTTP header strings into a JavaScript object. It is currently at version 2.0.6, with its last update occurring approximately one year ago (as of early 2024), indicating a slow release cadence and minimal active development. The library's core functionality involves parsing a multi-line header string, lowercasing all header names, and consolidating multiple instances of the same header into an array of values. It distinguishes itself by being a zero-dependency solution, making it ideal for environments where a minimal footprint is critical, such as older browser environments via Browserify or for basic XHR header processing. Unlike more comprehensive, RFC-strict parsers that handle structured field values, `parse-headers` offers a straightforward, object-based representation suitable for common, less complex HTTP header scenarios.
npm install parse-headersVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a multi-line HTTP header string into a JavaScript object, showing how duplicate headers are handled as arrays.
For applications requiring active maintenance, adherence to the latest HTTP specifications (e.g., Structured Field Values for HTTP RFC 8941/9651), or advanced header parsing capabilities, consider more actively developed alternatives like `structured-headers` or `http-headers`.
Access header values using their lowercase keys (e.g., `parsed['content-type']`). If original casing is critical, post-processing or a different parsing library is required.
When accessing a potentially repeated header, check if the value is an array (`Array.isArray(parsedHeader)`) and process accordingly. For example, `const firstCustomHeader = Array.isArray(parsed['x-custom-header']) ? parsed['x-custom-header'][0] : parsed['x-custom-header'];`
Change the import statement to `import parse from 'parse-headers';` for ES Module compatibility. If strictly needing CommonJS, ensure the file is treated as CommonJS (e.g., `.cjs` extension or no `"type": "module"` in `package.json`).
Correct the import statement to use a default import: `import parse from 'parse-headers';`
Always anticipate that repeated HTTP headers will be represented as an array of strings in the parsed object. Implement logic to handle both single string (for non-repeated headers) and array (for repeated headers) outcomes, or explicitly check `Array.isArray()` before processing. Example: `const values = Array.isArray(parsed['header-name']) ? parsed['header-name'] : [parsed['header-name']];`
No dependency data recorded yet.