
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
cache-control-parser
Advanced tools
A humble cache-control parser.
⨠New: you can now stringify cache control objects
To use cache-control-parser in your project, run:
npm install cache-control-parser
Example - parse a string of cache control directives into an object:
import { parse } from "cache-control-parser";
const directives = parse(
"public, max-age=300, no-transform"
);
console.log(directives);
Output:
{
"public": true,
"max-age": 300,
"no-transform": true
}
Example - destructuring the cache control object:
import { parse } from "cache-control-parser";
const directives = parse(
"max-age=300, s-maxage=0"
);
const { "max-age": maxAge, "s-maxage": sMaxAge } = directives;
Example - retrieve the shared proxy cache TTL:
import { parse } from "cache-control-parser";
const { "max-age": maxAge, "s-maxage": ttl = maxAge } = parse(
"max-age=300, s-maxage=0"
);
console.log("ttl:", ttl);
Output:
ttl: 0
Example - stringify a cache control object:
import { stringify } from "cache-control-parser";
const cacheControl = stringify({
"max-age": 300,
"s-maxage": 3600,
"public": true
});
console.log(cacheControl);
Output:
max-age=300, s-maxage=3600, public
Example - Next.js API route response header:
import { stringify } from "cache-control-parser";
import type { NextApiRequest, NextApiResponse } from "next";
export default (req: NextApiRequest, res: NextApiResponse) => {
res.setHeader(
"Cache-Control",
stringify({
"max-age": 300,
})
);
res.send("Hello world");
};
{
"max-age"?: number;
"s-maxage"?: number;
"stale-while-revalidate"?: number;
"stale-if-error"?: number;
"public"?: boolean;
"private"?: boolean;
"no-store"?: boolean;
"no-cache"?: boolean;
"must-revalidate"?: boolean;
"proxy-revalidate"?: boolean;
"immutable"?: boolean;
"no-transform"?: boolean;
}
Type definitions are included in this library and exposed via:
import type { CacheControl } from "cache-control-parser";
When contributing to this project, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.
Update the README.md with details of changes to the library.
Execute yarn test
and update the tests if needed.
Run the full test suite:
yarn test
Run tests in watch mode:
yarn test:watch
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A humble cache-control parser
The npm package cache-control-parser receives a total of 21,207 weekly downloads. As such, cache-control-parser popularity was classified as popular.
We found that cache-control-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600Ă faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.