Coder Social home page Coder Social logo

jessecollier / cache-control-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from etienne-martin/cache-control-parser

0.0 1.0 0.0 3.31 MB

A humble cache-control parser.

License: MIT License

JavaScript 25.77% Shell 6.41% TypeScript 67.82%

cache-control-parser's Introduction

cache-control-parser

A humble cache-control parser.

Coveralls github CircleCI build npm version npm monthly downloads

Features

  • Fault-tolerant
  • Case-insensitive
  • No dependencies
  • Built-in TypeScript support
  • Thoroughly tested

โœจ New: you can now stringify cache control objects

Getting Started

Installation

To use cache-control-parser in your project, run:

npm install cache-control-parser

Usage

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 cacheControl = parse(
  "max-age=300, s-maxage=0"
);

const ttl = cacheControl["s-maxage"] ?? cacheControl["max-age"];

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

Supported cache-control directives:

{
  "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;
}

TypeScript

Type definitions are included in this library and exposed via:

import { CacheControl } from "cache-control-parser";

Built with

  • node.js - Cross-platform JavaScript run-time environment for executing JavaScript code server-side.
  • TypeScript - Typed superset of JavaScript that compiles to plain JavaScript.
  • Jest - Delightful JavaScript Testing.

Contributing

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.

Testing

Run the full test suite:

yarn test

Run tests in watch mode:

yarn test:watch

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details.

cache-control-parser's People

Contributors

etienne-martin avatar dependabot[bot] avatar whacked avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.