HTTP Link Header Parser & Formatter
http-link-header is a JavaScript library designed for parsing and formatting HTTP Link headers as defined by RFC 8288 (which supersedes RFC 5988). The current stable version is 1.1.3, last published in 2018, indicating a low or absent release cadence. A key characteristic is its explicit deviation from RFC 8288 regarding relative URI-References: the library does *not* automatically resolve them, leaving this responsibility to the user. This design choice is critical for developers to be aware of, as it means the parsed URIs might not be absolute without further processing. The library provides methods for parsing single or multiple headers, checking for specific references, retrieving references by attribute, and setting/uniquely setting new references, along with stringifying Link objects back into HTTP header format.
Common errors
-
TypeError: http_link_header_1.LinkHeader is not a constructor
cause Attempting to use ES module import syntax for a CommonJS module in a TypeScript or ES module environment, where the default export is not correctly resolved.fixChange your import statement to `const LinkHeader = require('http-link-header');` or configure your TypeScript/bundler setup to handle CommonJS modules. -
TypeError: LinkHeader.parse is not a function
cause Similar to the constructor error, this occurs when the `LinkHeader` object obtained via `import` is not the expected class or lacks the static `parse` method due to incorrect CommonJS/ESM interop.fixEnsure you are using `const LinkHeader = require('http-link-header');` to get the correct CommonJS module export that exposes the `parse` static method. -
Error: Invalid or relative URI passed to further processing logic
cause The library does not resolve relative URIs in Link headers, leading to downstream errors if subsequent code expects absolute URLs.fixAfter parsing, manually resolve relative URIs against a known base URL (e.g., the URL of the document that returned the Link header) using `new URL(relativeUri, baseUrl).href`.
Warnings
- gotcha This library explicitly deviates from RFC 8288 by not automatically resolving relative URI-References. Parsed URIs remain relative and must be resolved by the user's application if absolute URIs are required.
- gotcha The `http-link-header` package is a CommonJS module and does not provide native ES Module (`import`) exports. Using `import` statements directly in modern Node.js or browser environments may lead to unexpected behavior or require bundler configuration.
- deprecated The `http-link-header` package appears to be unmaintained, with its last release in 2018. While functional for its stated purpose, users should be aware of the lack of ongoing bug fixes, security updates, or feature development. Consider newer alternatives for active projects.
Install
-
npm install http-link-header -
yarn add http-link-header -
pnpm add http-link-header
Imports
- LinkHeader (main class)
import LinkHeader from 'http-link-header'; import { LinkHeader } from 'http-link-header';const LinkHeader = require('http-link-header'); - LinkHeader.parse (static method)
import { parse } from 'http-link-header';const LinkHeader = require('http-link-header'); const link = LinkHeader.parse(headerString); - Link (instance)
import { Link } from 'http-link-header'; import Link from 'http-link-header';const LinkHeader = require('http-link-header'); const link = new LinkHeader();
Quickstart
const LinkHeader = require('http-link-header');
// Example HTTP Link header string with multiple relations and extended attributes
const headerString = '<https://example.com/next>; rel="next", ' +
'<https://example.com/prev>; rel="prev"; title="Previous Page", ' +
'<https://example.com/image.png>; rel="preload"; as="image"; type="image/png", ' +
'</extended-attr-example>; rel=start; title*=UTF-8\'en\'%E2%91%A0%E2%93%AB%E2%85%93%E3%8F%A8%E2%99%B3%F0%9D%84%9E%CE%BB';
// Parse the header string
const link = LinkHeader.parse(headerString);
console.log('Parsed Link Header:', JSON.stringify(link, null, 2));
console.log('First reference URI:', link.refs[0].uri);
console.log('All "prev" references:', link.rel('prev'));
console.log('Extended title value:', link.get('rel', 'start')[0]['title*'].value);
// Add a new unique reference
link.setUnique({
uri: 'https://example.com/canonical',
rel: 'canonical'
});
console.log('\nAfter adding canonical:', JSON.stringify(link.refs, null, 2));
// Stringify back to header format
const formattedHeader = link.toString();
console.log('\nFormatted Header:', formattedHeader);
// Check for a specific relation
console.log('\nHas "preload" relation?', link.has('rel', 'preload'));