Registry / http-networking / cache-parser

cache-parser

JSON →
library1.2.6jsnpmunverified

Cache Parser is a minimal JavaScript/TypeScript library designed specifically for parsing and building the HTTP Cache-Control header. It provides the `parse()` function to convert a raw Cache-Control string into a structured object representing its directives (e.g., `maxAge`, `public`, `noCache`) and the `tokenize()` function to convert a directives object back into an array of tokens suitable for HTTP headers. The current stable version is 1.2.6. It is part of the `tinylibs` monorepo, suggesting a release cadence tied to other utilities within that project, with frequent patch updates as seen in recent changelogs. Its key differentiator lies in its focused, minimal approach to handling only the Cache-Control header, providing clear TSDoc comments for usage and strong type safety for TypeScript users. It targets both Node.js and browser environments, with ESM, CJS, and UMD distributions available.

npm install cache-parser
INSTALL
IMPORT
SIG · CACHE-PARSER
C
cache-parser
http-networkingjavascriptv1.2.6
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
✓ import { parse } from 'cache-parser';
✗ const { parse } = require('cache-parser');
For Node.js projects with 'type': 'module', use the ESM import. CommonJS `require` is also supported but not recommended for new ESM-first projects.
tokenize
✓ import { tokenize } from 'cache-parser';
✗ const tokenize = require('cache-parser').tokenize;
This is a named export, requiring destructuring when using CommonJS `require` or standard named import for ESM.
CacheControl
✓ import type { CacheControl } from 'cache-parser';
This is a TypeScript type definition for the parsed Cache-Control object structure. Use `import type` for type-only imports.

This example demonstrates how to parse a raw 'Cache-Control' header string into an object and how to build a header string from a directives object using `parse` and `tokenize` respectively.

import { parse, tokenize, CacheControl } from 'cache-parser'; const rawHeader = 'public, max-age=3600, must-revalidate'; // Parsing a Cache-Control string const parsedDirectives = parse(rawHeader); console.log('Public:', parsedDirectives.public); // true console.log('Max Age:', parsedDirectives.maxAge); // 3600 console.log('Immutable:', parsedDirectives.immutable); // undefined console.log('Must Revalidate:', parsedDirectives.mustRevalidate); // true // Building a Cache-Control header from an object const cacheProperties: CacheControl = { public: true, maxAge: 7200, noTransform: true }; const cacheTokens = tokenize(cacheProperties); // cacheTokens will be ['public', 'max-age=7200', 'no-transform'] const httpHeader = cacheTokens.join(', '); console.log('Generated HTTP Header:', httpHeader); // 'public, max-age=7200, no-transform'
Debug
Known issues
gotchaThe `parse()` function returns an object where absent Cache-Control directives are represented by `undefined` properties, not `false` or `0`. Always check for `undefined` if a directive's presence is important.
fix
When accessing parsed properties, use a nullish coalescing operator (`??`) or explicit `typeof` checks if a default value or different logic is required for absent directives.
affects: >=1.0.0
gotchaWhile `cache-parser` supports both CommonJS `require()` and ES Modules `import`, projects with strict ESM environments (e.g., Node.js `"type": "module"` or modern bundlers) should use ES Module `import` syntax. Mixing them incorrectly can lead to runtime errors.
fix
For new projects or ESM-only environments, always use `import { namedExport } from 'cache-parser';`. If you encounter issues in a mixed environment, check your `tsconfig.json` or bundler configuration for module resolution settings.
affects: >=1.0.0
gotcha`cache-parser` is part of the `tinylibs` monorepo. While it is a standalone utility, be aware that other packages within `tinylibs` might have their own major version updates, such as `object-code@2.0.0`'s breaking change in hashing algorithms. Ensure to check release notes for `cache-parser` specifically.
fix
Regularly consult the release notes and changelog for `cache-parser` on npm or GitHub to stay informed about any specific changes, even when updating other `tinylibs` packages.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , cache_parser__WEBPACK_IMPORTED_MODULE_0__.parse) is not a function
This error typically occurs in bundled environments (like Webpack/Rollup) when an ES Module named import is incorrectly treated as a default import or when module resolution issues arise between CommonJS and ESM.
fix
Ensure you are using `import { parse } from 'cache-parser';`. If the issue persists, verify your bundler configuration to correctly handle ES Modules.
ReferenceError: require is not defined in ES module scope
You are attempting to use the CommonJS `require()` function in a JavaScript file that is being interpreted as an ES Module (e.g., in a Node.js project with `"type": "module"` in `package.json`).
fix
Replace `const { parse, tokenize } = require('cache-parser');` with `import { parse, tokenize } from 'cache-parser';`.
TS2305: Module ''cache-parser'' has no exported member 'CacheControl'.
The TypeScript compiler cannot find the `CacheControl` type. This usually means a typo in the import, or an older version of the package is installed that does not export this type.
fix
Verify the spelling of `CacheControl` and ensure you are using `import type { CacheControl } from 'cache-parser';`. If the problem persists, check your `cache-parser` package version to ensure it is up-to-date.
Upgrade
Version history
1.2.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
31 hits · last 30 days
node
26
Amazon
3
OpenAI (training)
1
Resources
cache-parser — npm install cache-parser · libregistry