{"id":12943,"library":"cache-parser","title":"Cache Parser","description":"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.","status":"active","version":"1.2.6","language":"javascript","source_language":"en","source_url":"https://github.com/arthurfiorette/tinylibs","tags":["javascript","typescript"],"install":[{"cmd":"npm install cache-parser","lang":"bash","label":"npm"},{"cmd":"yarn add cache-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add cache-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For Node.js projects with 'type': 'module', use the ESM import. CommonJS `require` is also supported but not recommended for new ESM-first projects.","wrong":"const { parse } = require('cache-parser');","symbol":"parse","correct":"import { parse } from 'cache-parser';"},{"note":"This is a named export, requiring destructuring when using CommonJS `require` or standard named import for ESM.","wrong":"const tokenize = require('cache-parser').tokenize;","symbol":"tokenize","correct":"import { tokenize } from 'cache-parser';"},{"note":"This is a TypeScript type definition for the parsed Cache-Control object structure. Use `import type` for type-only imports.","symbol":"CacheControl","correct":"import type { CacheControl } from 'cache-parser';"}],"quickstart":{"code":"import { parse, tokenize, CacheControl } from 'cache-parser';\n\nconst rawHeader = 'public, max-age=3600, must-revalidate';\n\n// Parsing a Cache-Control string\nconst parsedDirectives = parse(rawHeader);\nconsole.log('Public:', parsedDirectives.public); // true\nconsole.log('Max Age:', parsedDirectives.maxAge); // 3600\nconsole.log('Immutable:', parsedDirectives.immutable); // undefined\nconsole.log('Must Revalidate:', parsedDirectives.mustRevalidate); // true\n\n// Building a Cache-Control header from an object\nconst cacheProperties: CacheControl = { \n  public: true, \n  maxAge: 7200, \n  noTransform: true \n};\n\nconst cacheTokens = tokenize(cacheProperties);\n// cacheTokens will be ['public', 'max-age=7200', 'no-transform']\nconst httpHeader = cacheTokens.join(', ');\nconsole.log('Generated HTTP Header:', httpHeader); // 'public, max-age=7200, no-transform'","lang":"typescript","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"`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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `import { parse } from 'cache-parser';`. If the issue persists, verify your bundler configuration to correctly handle ES Modules.","cause":"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.","error":"TypeError: (0 , cache_parser__WEBPACK_IMPORTED_MODULE_0__.parse) is not a function"},{"fix":"Replace `const { parse, tokenize } = require('cache-parser');` with `import { parse, tokenize } from 'cache-parser';`.","cause":"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`).","error":"ReferenceError: require is not defined in ES module scope"},{"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.","cause":"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.","error":"TS2305: Module ''cache-parser'' has no exported member 'CacheControl'."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}