New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cacheability

Package Overview
Dependencies
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cacheability - npm Package Compare versions

Comparing version 0.0.47 to 0.0.48

lib/browser/cacheability/index.js

167

lib/browser/index.js

@@ -1,167 +0,2 @@

import "core-js/modules/es6.symbol";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.regexp.match";
import "core-js/modules/es6.regexp.split";
import "core-js/modules/es6.object.assign";
import "core-js/modules/es7.object.values";
import _kebabCase from "lodash/kebabCase";
import _isString from "lodash/isString";
import _isPlainObject from "lodash/isPlainObject";
import _isNumber from "lodash/isNumber";
import _isBoolean from "lodash/isBoolean";
import _camelCase from "lodash/camelCase";
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
if (!process.env.WEB_ENV) {
require("isomorphic-fetch");
}
var Cacheability = function () {
function Cacheability() {
_classCallCheck(this, Cacheability);
}
_createClass(Cacheability, [{
key: "checkTTL",
value: function checkTTL() {
if (!this.metadata || !this.metadata.ttl) {
throw new TypeError("checkTTL expected this._metadata.ttl to be a number.");
}
return this.metadata.ttl > Date.now();
}
}, {
key: "parseCacheControl",
value: function parseCacheControl(cacheControl) {
if (!_isString(cacheControl)) {
throw new TypeError("parseCacheControl expected cacheControl to be a string.");
}
var parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
}, {
key: "parseHeaders",
value: function parseHeaders(headers) {
if (!(headers instanceof Headers) && !_isPlainObject(headers)) {
var message = "parseHeaders expected headers to be an instance of Headers or a plain object.";
throw new TypeError(message);
}
var _ref = headers instanceof Headers ? Cacheability._parseHeaders(headers) : headers,
_ref$cacheControl = _ref.cacheControl,
cacheControl = _ref$cacheControl === void 0 ? "" : _ref$cacheControl,
etag = _ref.etag;
var parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
etag: etag,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
}, {
key: "printCacheControl",
value: function printCacheControl() {
if (!this.metadata || !this.metadata.cacheControl) {
throw new TypeError("printCacheControl expected this._metadata.cacheControl to be an object");
}
if (!Object.values(this.metadata.cacheControl).length) return "";
var cacheControl = Object.assign({}, this.metadata.cacheControl);
if (cacheControl.sMaxage || cacheControl.maxAge) {
var maxAge = this.checkTTL() ? Math.round((this.metadata.ttl - Date.now()) / 1000) : 0;
if (cacheControl.sMaxage) cacheControl.sMaxage = maxAge;
if (cacheControl.maxAge) cacheControl.maxAge = maxAge;
}
var directives = [];
Object.keys(cacheControl).forEach(function (key) {
if (_isBoolean(cacheControl[key])) {
directives.push(_kebabCase(key));
return;
}
directives.push("".concat(_kebabCase(key), "=").concat(cacheControl[key]));
});
return directives.join(", ");
}
}], [{
key: "_getDirectives",
value: function _getDirectives(cacheControl) {
return cacheControl.split(", ");
}
}, {
key: "_parseCacheControl",
value: function _parseCacheControl(cacheControl) {
var obj = {};
if (!cacheControl.length) return obj;
var directives = Cacheability._getDirectives(cacheControl);
directives.forEach(function (dir) {
if (dir.match(/=/)) {
var _dir$split = dir.split("="),
_dir$split2 = _slicedToArray(_dir$split, 2),
key = _dir$split2[0],
value = _dir$split2[1];
obj[_camelCase(key)] = Number(value);
return;
}
obj[_camelCase(dir)] = true;
});
return obj;
}
}, {
key: "_parseHeaders",
value: function _parseHeaders(headers) {
var metadata = {};
Cacheability._headerKeys.forEach(function (key) {
var headerValue = headers.get(key);
if (!headerValue) return;
var metadataKey = _camelCase(key);
metadata[metadataKey] = headerValue;
});
return metadata;
}
}, {
key: "_setTTL",
value: function _setTTL(_ref2) {
var maxAge = _ref2.maxAge,
sMaxage = _ref2.sMaxage;
var sec = sMaxage || maxAge;
if (!_isNumber(sec)) return Infinity;
var ms = sec * 1000;
return Date.now() + ms;
}
}]);
return Cacheability;
}();
export { Cacheability as default };
Cacheability._headerKeys = ["cache-control", "etag"];
export { Cacheability } from "./cacheability";
//# sourceMappingURL=index.js.map

@@ -1,15 +0,4 @@

import { CacheControl, CacheHeaders, Metadata } from "./types";
import { CacheControl, Metadata } from "./types";
export declare type CacheabilityCacheControl = CacheControl;
export declare type CacheabilityMetadata = Metadata;
export default class Cacheability {
private static _headerKeys;
private static _getDirectives(cacheControl);
private static _parseCacheControl(cacheControl);
private static _parseHeaders(headers);
private static _setTTL({maxAge, sMaxage});
metadata: Metadata;
checkTTL(): boolean;
parseCacheControl(cacheControl: string): Metadata;
parseHeaders(headers: Headers | CacheHeaders): Metadata;
printCacheControl(): string;
}
export { Cacheability } from "./cacheability";
"use strict";
require("core-js/modules/es6.symbol");
require("core-js/modules/web.dom.iterable");
require("core-js/modules/es7.object.values");
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
Object.defineProperty(exports, "__esModule", {

@@ -17,130 +7,5 @@ value: true

const lodash_1 = require("lodash");
var cacheability_1 = require("./cacheability");
if (!process.env.WEB_ENV) {
require("isomorphic-fetch");
}
let Cacheability = class Cacheability {
static _getDirectives(cacheControl) {
return cacheControl.split(", ");
}
static _parseCacheControl(cacheControl) {
const obj = {};
if (!cacheControl.length) return obj;
const directives = Cacheability._getDirectives(cacheControl);
directives.forEach(dir => {
if (dir.match(/=/)) {
const _dir$split = dir.split("="),
_dir$split2 = _slicedToArray(_dir$split, 2),
key = _dir$split2[0],
value = _dir$split2[1];
obj[lodash_1.camelCase(key)] = Number(value);
return;
}
obj[lodash_1.camelCase(dir)] = true;
});
return obj;
}
static _parseHeaders(headers) {
const metadata = {};
Cacheability._headerKeys.forEach(key => {
const headerValue = headers.get(key);
if (!headerValue) return;
const metadataKey = lodash_1.camelCase(key);
metadata[metadataKey] = headerValue;
});
return metadata;
}
static _setTTL({
maxAge,
sMaxage
}) {
const sec = sMaxage || maxAge;
if (!lodash_1.isNumber(sec)) return Infinity;
const ms = sec * 1000;
return Date.now() + ms;
}
checkTTL() {
if (!this.metadata || !this.metadata.ttl) {
throw new TypeError("checkTTL expected this._metadata.ttl to be a number.");
}
return this.metadata.ttl > Date.now();
}
parseCacheControl(cacheControl) {
if (!lodash_1.isString(cacheControl)) {
throw new TypeError("parseCacheControl expected cacheControl to be a string.");
}
const parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
parseHeaders(headers) {
if (!(headers instanceof Headers) && !lodash_1.isPlainObject(headers)) {
const message = "parseHeaders expected headers to be an instance of Headers or a plain object.";
throw new TypeError(message);
}
const _ref = headers instanceof Headers ? Cacheability._parseHeaders(headers) : headers,
_ref$cacheControl = _ref.cacheControl,
cacheControl = _ref$cacheControl === void 0 ? "" : _ref$cacheControl,
etag = _ref.etag;
const parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
etag,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
printCacheControl() {
if (!this.metadata || !this.metadata.cacheControl) {
throw new TypeError("printCacheControl expected this._metadata.cacheControl to be an object");
}
if (!Object.values(this.metadata.cacheControl).length) return "";
const cacheControl = Object.assign({}, this.metadata.cacheControl);
if (cacheControl.sMaxage || cacheControl.maxAge) {
const maxAge = this.checkTTL() ? Math.round((this.metadata.ttl - Date.now()) / 1000) : 0;
if (cacheControl.sMaxage) cacheControl.sMaxage = maxAge;
if (cacheControl.maxAge) cacheControl.maxAge = maxAge;
}
const directives = [];
Object.keys(cacheControl).forEach(key => {
if (lodash_1.isBoolean(cacheControl[key])) {
directives.push(lodash_1.kebabCase(key));
return;
}
directives.push(`${lodash_1.kebabCase(key)}=${cacheControl[key]}`);
});
return directives.join(", ");
}
};
Cacheability._headerKeys = ["cache-control", "etag"];
exports.default = Cacheability;
exports.Cacheability = cacheability_1.Cacheability;
//# sourceMappingURL=index.js.map

@@ -1,143 +0,2 @@

import "core-js/modules/es6.symbol";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es7.object.values";
import _kebabCase from "lodash/kebabCase";
import _isString from "lodash/isString";
import _isPlainObject from "lodash/isPlainObject";
import _isNumber from "lodash/isNumber";
import _isBoolean from "lodash/isBoolean";
import _camelCase from "lodash/camelCase";
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
if (!process.env.WEB_ENV) {
require("isomorphic-fetch");
}
let Cacheability = class Cacheability {
static _getDirectives(cacheControl) {
return cacheControl.split(", ");
}
static _parseCacheControl(cacheControl) {
const obj = {};
if (!cacheControl.length) return obj;
const directives = Cacheability._getDirectives(cacheControl);
directives.forEach(dir => {
if (dir.match(/=/)) {
const _dir$split = dir.split("="),
_dir$split2 = _slicedToArray(_dir$split, 2),
key = _dir$split2[0],
value = _dir$split2[1];
obj[_camelCase(key)] = Number(value);
return;
}
obj[_camelCase(dir)] = true;
});
return obj;
}
static _parseHeaders(headers) {
const metadata = {};
Cacheability._headerKeys.forEach(key => {
const headerValue = headers.get(key);
if (!headerValue) return;
const metadataKey = _camelCase(key);
metadata[metadataKey] = headerValue;
});
return metadata;
}
static _setTTL({
maxAge,
sMaxage
}) {
const sec = sMaxage || maxAge;
if (!_isNumber(sec)) return Infinity;
const ms = sec * 1000;
return Date.now() + ms;
}
checkTTL() {
if (!this.metadata || !this.metadata.ttl) {
throw new TypeError("checkTTL expected this._metadata.ttl to be a number.");
}
return this.metadata.ttl > Date.now();
}
parseCacheControl(cacheControl) {
if (!_isString(cacheControl)) {
throw new TypeError("parseCacheControl expected cacheControl to be a string.");
}
const parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
parseHeaders(headers) {
if (!(headers instanceof Headers) && !_isPlainObject(headers)) {
const message = "parseHeaders expected headers to be an instance of Headers or a plain object.";
throw new TypeError(message);
}
const _ref = headers instanceof Headers ? Cacheability._parseHeaders(headers) : headers,
_ref$cacheControl = _ref.cacheControl,
cacheControl = _ref$cacheControl === void 0 ? "" : _ref$cacheControl,
etag = _ref.etag;
const parsedCacheControl = Cacheability._parseCacheControl(cacheControl);
this.metadata = {
cacheControl: parsedCacheControl,
etag,
ttl: Cacheability._setTTL(parsedCacheControl)
};
return this.metadata;
}
printCacheControl() {
if (!this.metadata || !this.metadata.cacheControl) {
throw new TypeError("printCacheControl expected this._metadata.cacheControl to be an object");
}
if (!Object.values(this.metadata.cacheControl).length) return "";
const cacheControl = Object.assign({}, this.metadata.cacheControl);
if (cacheControl.sMaxage || cacheControl.maxAge) {
const maxAge = this.checkTTL() ? Math.round((this.metadata.ttl - Date.now()) / 1000) : 0;
if (cacheControl.sMaxage) cacheControl.sMaxage = maxAge;
if (cacheControl.maxAge) cacheControl.maxAge = maxAge;
}
const directives = [];
Object.keys(cacheControl).forEach(key => {
if (_isBoolean(cacheControl[key])) {
directives.push(_kebabCase(key));
return;
}
directives.push(`${_kebabCase(key)}=${cacheControl[key]}`);
});
return directives.join(", ");
}
};
export { Cacheability as default };
Cacheability._headerKeys = ["cache-control", "etag"];
export { Cacheability } from "./cacheability";
//# sourceMappingURL=index.js.map
{
"name": "cacheability",
"version": "0.0.47",
"version": "0.0.48",
"description": "A utility class to parse, store and print http cache headers.",

@@ -5,0 +5,0 @@ "author": "Dylan Aubrey <dylanaubrey@gmail.com>",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc