You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@hishprorg/eius-totam

Package Overview
Dependencies
32
Maintainers
0
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hishprorg/eius-totam

A humble [cache-control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) parser.


Version published
Maintainers
0
Created

Readme

Source

cache-control-parser

A humble cache-control parser.

Build status Coveralls github 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 "@hishprorg/eius-totam"

Usage

Example - parse a string of cache control directives into an object:

import { parse } from "@hishprorg/eius-totam";

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 "@hishprorg/eius-totam";

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 "@hishprorg/eius-totam";

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 "@hishprorg/eius-totam";

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 "@hishprorg/eius-totam";
import type { NextApiRequest, NextApiResponse } from "next";

export default (req: NextApiRequest, res: NextApiResponse) => {
  res.setHeader(
    "Cache-Control",
    stringify({
      "max-age": 300,
    }),
  );

  res.send("Hello world");
};

Supported 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 type { CacheControl } from "@hishprorg/eius-totam";

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.

优化了stringify,如果boolean值为false,则不返回对应的key

Keywords

FAQs

Package last updated on 20 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc