cache-control-parser
Advanced tools
| import { CacheControl } from "./types"; | ||
| export declare const cacheControlParser: (cacheControlHeader: string) => CacheControl; | ||
| export declare const parse: (cacheControlHeader: string) => CacheControl; | ||
| export declare const stringify: (cacheControl: CacheControl) => string; |
+1
-2
@@ -1,3 +0,2 @@ | ||
| import { cacheControlParser } from "./cache-control-parser"; | ||
| export * from "./cache-control-parser"; | ||
| export { CacheControl } from "./types"; | ||
| export default cacheControlParser; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| !function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var t=r();for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,(function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var a=r[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,t),a.l=!0,a.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var a in e)t.d(n,a,function(r){return e[r]}.bind(null,a));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=1)}([function(e,r){},function(e,r,t){"use strict";function n(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){if(!(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)))return;var t=[],n=!0,a=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(t.push(i.value),!r||t.length!==r);n=!0);}catch(e){a=!0,o=e}finally{try{n||null==u.return||u.return()}finally{if(a)throw o}}return t}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}t.r(r);var a=t(0);t.d(r,"CacheControl",(function(){return a.CacheControl}));r.default=function(e){var r={},t=e.split(",").map((function(e){return e.trim().split("=").map((function(e){return e.trim()}))})),a=!0,o=!1,i=void 0;try{for(var u,c=t[Symbol.iterator]();!(a=(u=c.next()).done);a=!0){var l=n(u.value,2),f=l[0],s=l[1];switch(f){case"max-age":var p=parseInt(s,10);if(isNaN(p))continue;r.maxAge=p;break;case"s-maxage":var b=parseInt(s,10);if(isNaN(b))continue;r.sharedMaxAge=b;break;case"stale-while-revalidate":var d=parseInt(s,10);if(isNaN(d))continue;r.staleWhileRevalidate=d;break;case"stale-if-error":var v=parseInt(s,10);if(isNaN(v))continue;r.staleIfError=v;break;case"public":r.isPublic=!0;break;case"private":r.isPrivate=!0;break;case"no-store":r.noStore=!0;break;case"no-cache":r.noCache=!0;break;case"must-revalidate":r.mustRevalidate=!0;break;case"proxy-revalidate":r.proxyRevalidate=!0;break;case"immutable":r.immutable=!0;break;case"no-transform":r.noTransform=!0}}}catch(e){o=!0,i=e}finally{try{a||null==c.return||c.return()}finally{if(o)throw i}}return r}}])})); | ||
| !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.cacheControl=t():e.cacheControl=t()}(this,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(e,t){},function(e,t,r){"use strict";function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if(!(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)))return;var r=[],n=!0,o=!1,a=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){o=!0,a=e}finally{try{n||null==u.return||u.return()}finally{if(o)throw a}}return r}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}r.r(t);var a=["max-age","s-maxage","stale-while-revalidate","stale-if-error","public","private","no-store","no-cache","must-revalidate","proxy-revalidate","immutable","no-transform"],i=function(e){var t={},r=e.toLowerCase().split(",").map((function(e){return e.trim().split("=").map((function(e){return e.trim()}))})),n=!0,a=!1,i=void 0;try{for(var u,c=r[Symbol.iterator]();!(n=(u=c.next()).done);n=!0){var l=o(u.value,2),f=l[0],s=l[1];switch(f){case"max-age":var p=parseInt(s,10);if(isNaN(p))continue;t["max-age"]=p;break;case"s-maxage":var b=parseInt(s,10);if(isNaN(b))continue;t["s-maxage"]=b;break;case"stale-while-revalidate":var y=parseInt(s,10);if(isNaN(y))continue;t["stale-while-revalidate"]=y;break;case"stale-if-error":var m=parseInt(s,10);if(isNaN(m))continue;t["stale-if-error"]=m;break;case"public":t.public=!0;break;case"private":t.private=!0;break;case"no-store":t["no-store"]=!0;break;case"no-cache":t["no-cache"]=!0;break;case"must-revalidate":t["must-revalidate"]=!0;break;case"proxy-revalidate":t["proxy-revalidate"]=!0;break;case"immutable":t.immutable=!0;break;case"no-transform":t["no-transform"]=!0}}}catch(e){a=!0,i=e}finally{try{n||null==c.return||c.return()}finally{if(a)throw i}}return t},u=function(e){for(var t=[],r=0,i=Object.entries(e);r<i.length;r++){var u=o(i[r],2),c=u[0],l=u[1];if(a.includes(c))switch(n(l)){case"boolean":t.push("".concat(c));break;case"number":t.push("".concat(c,"=").concat(l))}}return t.join(", ")},c=r(0);r.d(t,"parse",(function(){return i})),r.d(t,"stringify",(function(){return u})),r.d(t,"CacheControl",(function(){return c.CacheControl}))}])})); |
+11
-11
| export interface CacheControl { | ||
| maxAge?: number; | ||
| sharedMaxAge?: number; | ||
| staleWhileRevalidate?: number; | ||
| staleIfError?: number; | ||
| isPublic?: boolean; | ||
| isPrivate?: boolean; | ||
| noStore?: boolean; | ||
| noCache?: boolean; | ||
| mustRevalidate?: boolean; | ||
| proxyRevalidate?: boolean; | ||
| "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; | ||
| noTransform?: boolean; | ||
| "no-transform"?: boolean; | ||
| } |
+3
-2
| { | ||
| "name": "cache-control-parser", | ||
| "version": "1.0.8", | ||
| "version": "2.0.0", | ||
| "description": "A lightweight cache-control parser", | ||
@@ -12,3 +12,4 @@ "homepage": "https://github.com/etienne-martin/cache-control-parser", | ||
| "cache-control", | ||
| "parser" | ||
| "parser", | ||
| "stringify" | ||
| ], | ||
@@ -15,0 +16,0 @@ "license": "MIT", |
+45
-19
| # cache-control-parser | ||
| A lightweight cache-control parser. | ||
| A humble cache-control parser. | ||
@@ -12,2 +12,4 @@ [](https://coveralls.io/github/etienne-martin/cache-control-parser) | ||
| - Fault-tolerant | ||
| - Case-insensitive | ||
| - No dependencies | ||
@@ -17,2 +19,4 @@ - Built-in TypeScript support | ||
| ✨ **New:** you can now stringify parsed cache controls | ||
| ## Getting Started | ||
@@ -30,10 +34,12 @@ | ||
| **Example** - parse a cache control string into an object: | ||
| ```javascript | ||
| import cacheControlParser from "cache-control-parser"; | ||
| import { parse } from "cache-control-parser"; | ||
| const cacheControl = cacheControlParser( | ||
| const directives = parse( | ||
| "public, max-age=86400, no-transform" | ||
| ); | ||
| console.log(cacheControl); | ||
| console.log(directives); | ||
| ``` | ||
@@ -45,8 +51,28 @@ | ||
| { | ||
| "isPublic": true, | ||
| "maxAge": 86400, | ||
| "noTransform": true | ||
| "public": true, | ||
| "max-age": 86400, | ||
| "no-transform": true | ||
| } | ||
| ``` | ||
| **Example** - stringify a cache control object into a string: | ||
| ```javascript | ||
| 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:** | ||
@@ -56,14 +82,14 @@ | ||
| { | ||
| maxAge?: number; | ||
| sharedMaxAge?: number; | ||
| staleWhileRevalidate?: number; | ||
| staleIfError?: number; | ||
| isPublic?: boolean; | ||
| isPrivate?: boolean; | ||
| noStore?: boolean; | ||
| noCache?: boolean; | ||
| mustRevalidate?: boolean; | ||
| proxyRevalidate?: boolean; | ||
| immutable?: boolean; | ||
| noTransform?: boolean; | ||
| "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; | ||
| } | ||
@@ -70,0 +96,0 @@ ``` |
10262
13.63%136
23.64%