cache-parser

A minimal Cache-Control parser

Usage no npm install needed!

<script type="module">
  import cacheParser from 'https://cdn.skypack.dev/cache-parser';
</script>

README


  

⌛ Cache Parser




Stars License Size Downloads NPM Coverage



cache-parser is a minimal parser for the Cache-Control header



Table of contents


Installing

Node

npm install --save cache-parser
# or
yarn add cache-parser
const { parse, tokenize } = require('cache-parser');
// or
import { parse, tokenize } from 'cache-parser';

Browser

Downloads

<script
  crossorigin
  src="https://cdn.jsdelivr.net/npm/cache-parser@1/index.umd.js"
></script>
<!-- or -->
<script crossorigin src="https://unpkg.com/cache-parser@1/dist/index.umd.js"></script>
const { parse, tokenize } = window.CacheParser;

Getting Started

This library consists of two important functions: parse and tokenize.

This is a pretty straightforward library, so, every documentation needed by every piece of code is in form of TSDoc and JSDoc comments.

Some examples

Simple header parsing:

import { parse } from 'cache-parser';

const rawHeader = 'public, max-age=3600';

const { public, maxAge, immutable } = parse(rawHeader);

console.log(public); // true
console.log(maxAge); // 3600
console.log(typeof maxAge); // number
console.log(immutable); // undefined

Simple header building:

import { tokenize } from 'cache-parser';

/** @type {import('cache-parser').CacheControl} */
const cacheProperties = { public: true, maxAge: 3600 };

// ['public', 'max-age=3600']
const cacheTokens = tokenize(cacheProperties);

// 'public, max-age=3600'
response.headers['Cache-Control'] = tokens.join(', ');

License

Licensed under the MIT. See LICENSE for more informations.


Contact

See my contact information on my github profile or open a new issue.