Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

incache

Package Overview
Dependencies
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

incache - npm Package Compare versions

Comparing version 3.1.1 to 4.0.0

10

CHANGELOG.md

@@ -7,2 +7,12 @@ # Change Log

## [4.0.0] - 2017-08-22
- Changed configuration
- Added new properties
- `maxAge`
- `expires`
- `silent`
- **Deprecated properties**
- `global.life`
- `global.silent`
## [3.1.1] - 2017-08-21

@@ -9,0 +19,0 @@ - Fixed readme

90

dist/incache.js

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

// [AIV] InCache Build version: 3.1.1
// [AIV] InCache Build version: 4.0.0
var incache =

@@ -285,11 +285,15 @@ /******/ (function(modules) { // webpackBootstrap

var InCache = function () {
/**
* Create instance
* @param [opts] {Object} configuration object
* @param [opts.maxAge=0] {number} max age in milliseconds. If 0 not expire
* @param [opts.expires] {Date|string} a Date for expiration. (overwrites `opts.maxAge`)
* @param [opts.silent=false] {boolean} if true no event will be triggered
* @param [opts.save=true] {boolean} if true saves cache in disk. (server only)
* @param [opts.filePath=.incache] {string} cache file path
* @param [opts.storeName] {string} store name
* @param [opts.global] {Object} global record configuration
* @param [opts.global.silent=false] {boolean} if true no event will be triggered
* @param [opts.global.life=0] {number} max age. If 0 not expire
* @param [opts.global] {Object} **deprecated:** global record configuration
* @param [opts.global.silent=false] {boolean} **deprecated:** if true no event will be triggered, use `silent` instead
* @param [opts.global.life=0] {number} **deprecated:** max age in seconds. If 0 not expire, use `maxAge` instead
* @constructor

@@ -327,2 +331,4 @@ */

filePath: '.incache',
maxAge: 0,
silent: false,
global: {

@@ -381,8 +387,3 @@ silent: false,

* @param [opts] {Object} configuration object
* @param [opts.save=true] {boolean} if true saves cache in disk. (server only)
* @param [opts.filePath=.incache] {string} cache file path
* @param [opts.storeName] {string} store name
* @param [opts.global] {Object} global record configuration
* @param [opts.global.silent=false] {boolean} if true no event will be triggered
* @param [opts.global.life=0] {number} max age. If 0 not expire
* @see {@link constructor} for further information
*/

@@ -407,2 +408,7 @@

if (opts.global) {
helper.deprecated(opts.global.life, 'global.life is deprecated use maxAge instead');
helper.deprecated(opts.global.silent, 'global.silent is deprecated use silent instead');
}
this._root[this.GLOBAL_KEY].config = helper.defaults(opts, this.DEFAULT_CONFIG);

@@ -429,2 +435,12 @@

/**
* InCache record
* @typedef {Object} InCache~record
* @property {boolean} isNew - indicates if is a new record
* @property {Date|null} createdOn - creation date
* @property {Date|null} updatedOn - update date
* @property {Date|null} expiresOn - expiry date
* @property {any} value - record value
*/
/**
* Set/update record

@@ -435,8 +451,10 @@ * @param key {any}

* @param [opts.silent=false] {boolean} if true no event will be triggered. (overwrites global configuration)
* @param [opts.life=0] {number} max age. If 0 not expire. (overwrites global configuration)
* @returns {{isNew: boolean, createdOn: Date|null, updatedOn: Date|null, value: *}}
* @param [opts.maxAge=0] {number} max age in milliseconds. If 0 not expire. (overwrites global configuration)
* @param [opts.expires] {Date|string} a Date for expiration. (overwrites global configuration and `opts.maxAge`)
* @param [opts.life=0] {number} **deprecated:** max age in seconds. If 0 not expire. (overwrites global configuration)
* @returns {InCache~record}
* @example
* inCache.set('my key', 'my value');
* inCache.set('my object', {a: 1, b: 2});
* inCache.set('my boolean', true, {life: 2}); // Expires after 2 seconds
* inCache.set('my boolean', true, {maxAge: 2000}); // Expires after 2 seconds
*/

@@ -457,6 +475,11 @@

opts = helper.defaults(opts, this.DEFAULT_CONFIG.global);
opts = helper.defaults(opts, this.DEFAULT_CONFIG);
if (opts.life && helper.is(opts.life, 'number')) {
if (opts.maxAge && helper.is(opts.maxAge, 'number')) {
record.expiresOn = helper.addMSToNow(opts.maxAge);
} else if (opts.life && helper.is(opts.life, 'number')) {
helper.deprecated(opts.life, 'life is deprecated use maxAge instead');
record.expiresOn = helper.addSecondsToNow(opts.life);
} else if (opts.expires && (helper.is(opts.expires, 'date') || helper.is(opts.expires, 'string'))) {
record.expiresOn = new Date(opts.expires);
}

@@ -505,3 +528,3 @@

* @param [onlyValue=true] {boolean} if false get InCache record
* @returns {any|null}
* @returns {any|null|InCache~record}
* @example

@@ -550,3 +573,3 @@ * inCache.get('my key');

* @param value {any}
* @returns {*}
* @returns {InCache~record}
* @example

@@ -574,3 +597,3 @@ * inCache.set('myArray', ['hello', 'world']);

* @param value {any}
* @returns {*}
* @returns {InCache~record}
* @example

@@ -825,3 +848,3 @@ * inCache.set('myArray', ['hello', 'world']);

* @param key {string} key of record created
* @param record {Object} record object
* @param record {InCache~record} record object
*/

@@ -848,3 +871,3 @@

* @param key {string} key of record updated
* @param record {Object} record object
* @param record {InCache~record} record object
*/

@@ -940,2 +963,3 @@

* @returns {Date}
* @deprecated
*/

@@ -948,2 +972,12 @@ helper.addSecondsToNow = function (seconds) {

/**
* Adds milliseconds to current date
* @param ms {number} number of milliseconds to add
* @returns {Date}
*/
helper.addMSToNow = function (ms) {
var now = new Date();
return new Date(now.setMilliseconds(now.getMilliseconds() + ms));
};
/**
* Check if is Node environment

@@ -956,2 +990,18 @@ * @returns {boolean}

/**
* Throw deprecated
* @param prop
* @param msg
* @param [type=warn]
*/
helper.deprecated = function (prop, msg) {
var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'warn';
if (typeof prop !== 'undefined') {
console[type](msg || prop);
return true;
}
return false;
};
module.exports = helper;

@@ -958,0 +1008,0 @@ /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)))

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

// [AIV] InCache Build version: 3.1.1
var incache=function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get: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,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,r){"use strict";function n(){throw new Error("setTimeout has not been defined")}function o(){throw new Error("clearTimeout has not been defined")}function i(e){if(c===setTimeout)return setTimeout(e,0);if((c===n||!c)&&setTimeout)return c=setTimeout,setTimeout(e,0);try{return c(e,0)}catch(t){try{return c.call(null,e,0)}catch(t){return c.call(this,e,0)}}}function a(e){if(h===clearTimeout)return clearTimeout(e);if((h===o||!h)&&clearTimeout)return h=clearTimeout,clearTimeout(e);try{return h(e)}catch(t){try{return h.call(null,e)}catch(t){return h.call(this,e)}}}function s(){p&&y&&(p=!1,y.length?v=y.concat(v):m=-1,v.length&&u())}function u(){if(!p){var e=i(s);p=!0;for(var t=v.length;t;){for(y=v,v=[];++m<t;)y&&y[m].run();m=-1,t=v.length}y=null,p=!1,a(e)}}function f(e,t){this.fun=e,this.array=t}function l(){}var c,h,d=e.exports={};!function(){try{c="function"==typeof setTimeout?setTimeout:n}catch(e){c=n}try{h="function"==typeof clearTimeout?clearTimeout:o}catch(e){h=o}}();var y,v=[],p=!1,m=-1;d.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];v.push(new f(e,t)),1!==v.length||p||i(u)},f.prototype.run=function(){this.fun.apply(null,this.array)},d.title="browser",d.browser=!0,d.env={},d.argv=[],d.version="",d.versions={},d.on=l,d.addListener=l,d.once=l,d.off=l,d.removeListener=l,d.removeAllListeners=l,d.emit=l,d.prependListener=l,d.prependOnceListener=l,d.listeners=function(e){return[]},d.binding=function(e){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(e){throw new Error("process.chdir is not supported")},d.umask=function(){return 0}},function(e,t,r){"use strict";e.exports=r(2)},function(e,t,r){"use strict";(function(t,n){function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),a=r(4),s=r(5),u=function(){function e(){var r=this,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};o(this,e),this.GLOBAL_KEY="___InCache___storage___global___key___",this._root=a.isServer()?t:window,this.DEFAULT_CONFIG={storeName:"",save:!0,filePath:".incache",global:{silent:!1,life:0}},this._onRemoved=function(){},this._onCreated=function(){},this._onUpdated=function(){},a.isServer()&&(n.stdin.resume(),n.on("exit",function(){r._write()}),n.on("SIGINT",function(){r._write()})),this.setConfig(i)}return i(e,[{key:"_write",value:function(){var e=this._memory,t=e.config,r=e.data;if(t.save){var n=JSON.stringify(r);s.writeFileSync(t.filePath,n)}}},{key:"_read",value:function(){var e=this._memory.config;if(e.save&&s.existsSync(e.filePath)){var t=s.readFileSync(e.filePath);try{this._storage=this._memory.data=JSON.parse(t)}catch(e){this._storage=this._memory.data={}}}}},{key:"setConfig",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};e.storeName&&(this.GLOBAL_KEY+=e.storeName),this._root[this.GLOBAL_KEY]||(this._root[this.GLOBAL_KEY]={metadata:{lastSave:null},data:{},config:this.DEFAULT_CONFIG}),this._root[this.GLOBAL_KEY].config=a.defaults(e,this.DEFAULT_CONFIG),this._memory=this._root[this.GLOBAL_KEY],this._storage=this._memory.data,a.isServer()&&this._read()}},{key:"getConfig",value:function(){return this._memory.config}},{key:"set",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n={isNew:!0,createdOn:null,updatedOn:null,expiresOn:null,value:t};return r=a.defaults(r,this.DEFAULT_CONFIG.global),r.life&&a.is(r.life,"number")&&(n.expiresOn=a.addSecondsToNow(r.life)),this.has(e)?(n.isNew=!1,n.updatedOn=new Date,r.silent||this._onUpdated.call(this,e,n)):(n.createdOn=new Date,r.silent||this._onCreated.call(this,e,n)),this._storage[e]=n,n}},{key:"bulkSet",value:function(e){if(!a.is(e,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");for(var t=0;t<e.length;t++){if(a.is(e[t].key,"undefined")||a.is(e[t].value,"undefined"))throw new Error("key and value properties are required");this.set(e[t].key,e[t].value,{silent:!0,fromBulk:!0})}}},{key:"get",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return this.has(e)?this.expired(e)?(this.remove(e,!0),null):t?this._storage[e].value:this._storage[e]:null}},{key:"remove",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];arguments.length>2&&void 0!==arguments[2]&&arguments[2];delete this._storage[e],t||this._onRemoved.call(this,e)}},{key:"addTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.push(t),this.set(e,r)}},{key:"prependTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.unshift(t),this.set(e,r)}},{key:"updateIn",value:function(e,t,r){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("value cannot be undefined");if(a.is(r,"undefined"))throw new Error("where cannot be undefined");var n=this.get(e);if(!a.is(n,"array"))throw new Error("value must be an array");var o=!1;for(var i in n)if(n.hasOwnProperty(i)){var s=[];for(var u in r)r.hasOwnProperty(u)&&(a.is(r,"object")?s.push(void 0!==n[i][u]&&n[i][u]===r[u]):s.push(n[i]===r));s.length&&-1===s.indexOf(!1)&&(o=!0,n[i]=t)}o&&this.set(e,n)}},{key:"removeFrom",value:function(e,t){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("where cannot be undefined");var r=this.get(e);if(!a.is(r,"array"))throw new Error("value must be an array");var n=r.length;for(var o in r)if(r.hasOwnProperty(o)){var i=[];for(var s in t)t.hasOwnProperty(s)&&(a.is(t,"object")?i.push(void 0!==r[o][s]&&r[o][s]===t[s]):i.push(r[o]===t));i.length&&-1===i.indexOf(!1)&&r.splice(o,1)}n!==r.length&&this.set(e,r)}},{key:"bulkRemove",value:function(e){if(!a.is(e,"array"))throw new Error("keys must be an array of keys");for(var t=0;t<e.length;t++)this.remove(e[t],!0,{fromBulk:!0})}},{key:"clean",value:function(e){if(!a.is(e,"string"))throw new Error("key must be a string");for(var t in this._storage)this._storage.hasOwnProperty(t)&&-1!==t.indexOf(e)&&delete this._storage[t]}},{key:"all",value:function(){var e=[];for(var t in this._storage)this._storage.hasOwnProperty(t)&&(this.expired(t)?this.remove(t,!0):e.push({key:t,value:this._storage[t].value}));return e}},{key:"expired",value:function(e){if(this._storage[e]&&this._storage[e].expiresOn){return new Date>new Date(this._storage[e].expiresOn)}return!1}},{key:"clear",value:function(){this._storage=this._memory.data={}}},{key:"has",value:function(e){return this._storage.hasOwnProperty(e)}},{key:"onRemoved",value:function(e){this._onRemoved=e}},{key:"onCreated",value:function(e){this._onCreated=e}},{key:"onUpdated",value:function(e){this._onUpdated=e}}]),e}();e.exports=u}).call(t,r(3),r(0))},function(e,t,r){"use strict";var n,o="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};n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(e){"object"===("undefined"==typeof window?"undefined":o(window))&&(n=window)}e.exports=n},function(e,t,r){"use strict";(function(t){var r="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},n={};n.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},n.defaults=function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e.hasOwnProperty(o)?"object"===r(e[o])&&n.defaults(e[o],t[o]):e[o]=t[o]);return e},n.addSecondsToNow=function(e){var t=new Date;return new Date(t.setSeconds(t.getSeconds()+e))},n.isServer=function(){return"object"===(void 0===t?"undefined":r(t))&&void 0!==t.pid},e.exports=n}).call(t,r(0))},function(e,t,r){"use strict"}]);
// [AIV] InCache Build version: 4.0.0
var incache=function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get: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,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,r){"use strict";function n(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function o(e){if(f===setTimeout)return setTimeout(e,0);if((f===n||!f)&&setTimeout)return f=setTimeout,setTimeout(e,0);try{return f(e,0)}catch(t){try{return f.call(null,e,0)}catch(t){return f.call(this,e,0)}}}function a(e){if(h===clearTimeout)return clearTimeout(e);if((h===i||!h)&&clearTimeout)return h=clearTimeout,clearTimeout(e);try{return h(e)}catch(t){try{return h.call(null,e)}catch(t){return h.call(this,e)}}}function s(){p&&v&&(p=!1,v.length?y=v.concat(y):m=-1,y.length&&u())}function u(){if(!p){var e=o(s);p=!0;for(var t=y.length;t;){for(v=y,y=[];++m<t;)v&&v[m].run();m=-1,t=y.length}v=null,p=!1,a(e)}}function l(e,t){this.fun=e,this.array=t}function c(){}var f,h,d=e.exports={};!function(){try{f="function"==typeof setTimeout?setTimeout:n}catch(e){f=n}try{h="function"==typeof clearTimeout?clearTimeout:i}catch(e){h=i}}();var v,y=[],p=!1,m=-1;d.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];y.push(new l(e,t)),1!==y.length||p||o(u)},l.prototype.run=function(){this.fun.apply(null,this.array)},d.title="browser",d.browser=!0,d.env={},d.argv=[],d.version="",d.versions={},d.on=c,d.addListener=c,d.once=c,d.off=c,d.removeListener=c,d.removeAllListeners=c,d.emit=c,d.prependListener=c,d.prependOnceListener=c,d.listeners=function(e){return[]},d.binding=function(e){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(e){throw new Error("process.chdir is not supported")},d.umask=function(){return 0}},function(e,t,r){"use strict";e.exports=r(2)},function(e,t,r){"use strict";(function(t,n){function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),a=r(4),s=r(5),u=function(){function e(){var r=this,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this.GLOBAL_KEY="___InCache___storage___global___key___",this._root=a.isServer()?t:window,this.DEFAULT_CONFIG={storeName:"",save:!0,filePath:".incache",maxAge:0,silent:!1,global:{silent:!1,life:0}},this._onRemoved=function(){},this._onCreated=function(){},this._onUpdated=function(){},a.isServer()&&(n.stdin.resume(),n.on("exit",function(){r._write()}),n.on("SIGINT",function(){r._write()})),this.setConfig(o)}return o(e,[{key:"_write",value:function(){var e=this._memory,t=e.config,r=e.data;if(t.save){var n=JSON.stringify(r);s.writeFileSync(t.filePath,n)}}},{key:"_read",value:function(){var e=this._memory.config;if(e.save&&s.existsSync(e.filePath)){var t=s.readFileSync(e.filePath);try{this._storage=this._memory.data=JSON.parse(t)}catch(e){this._storage=this._memory.data={}}}}},{key:"setConfig",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};e.storeName&&(this.GLOBAL_KEY+=e.storeName),this._root[this.GLOBAL_KEY]||(this._root[this.GLOBAL_KEY]={metadata:{lastSave:null},data:{},config:this.DEFAULT_CONFIG}),e.global&&(a.deprecated(e.global.life,"global.life is deprecated use maxAge instead"),a.deprecated(e.global.silent,"global.silent is deprecated use silent instead")),this._root[this.GLOBAL_KEY].config=a.defaults(e,this.DEFAULT_CONFIG),this._memory=this._root[this.GLOBAL_KEY],this._storage=this._memory.data,a.isServer()&&this._read()}},{key:"getConfig",value:function(){return this._memory.config}},{key:"set",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n={isNew:!0,createdOn:null,updatedOn:null,expiresOn:null,value:t};return r=a.defaults(r,this.DEFAULT_CONFIG),r.maxAge&&a.is(r.maxAge,"number")?n.expiresOn=a.addMSToNow(r.maxAge):r.life&&a.is(r.life,"number")?(a.deprecated(r.life,"life is deprecated use maxAge instead"),n.expiresOn=a.addSecondsToNow(r.life)):r.expires&&(a.is(r.expires,"date")||a.is(r.expires,"string"))&&(n.expiresOn=new Date(r.expires)),this.has(e)?(n.isNew=!1,n.updatedOn=new Date,r.silent||this._onUpdated.call(this,e,n)):(n.createdOn=new Date,r.silent||this._onCreated.call(this,e,n)),this._storage[e]=n,n}},{key:"bulkSet",value:function(e){if(!a.is(e,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");for(var t=0;t<e.length;t++){if(a.is(e[t].key,"undefined")||a.is(e[t].value,"undefined"))throw new Error("key and value properties are required");this.set(e[t].key,e[t].value,{silent:!0,fromBulk:!0})}}},{key:"get",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return this.has(e)?this.expired(e)?(this.remove(e,!0),null):t?this._storage[e].value:this._storage[e]:null}},{key:"remove",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];arguments.length>2&&void 0!==arguments[2]&&arguments[2];delete this._storage[e],t||this._onRemoved.call(this,e)}},{key:"addTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.push(t),this.set(e,r)}},{key:"prependTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.unshift(t),this.set(e,r)}},{key:"updateIn",value:function(e,t,r){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("value cannot be undefined");if(a.is(r,"undefined"))throw new Error("where cannot be undefined");var n=this.get(e);if(!a.is(n,"array"))throw new Error("value must be an array");var i=!1;for(var o in n)if(n.hasOwnProperty(o)){var s=[];for(var u in r)r.hasOwnProperty(u)&&(a.is(r,"object")?s.push(void 0!==n[o][u]&&n[o][u]===r[u]):s.push(n[o]===r));s.length&&-1===s.indexOf(!1)&&(i=!0,n[o]=t)}i&&this.set(e,n)}},{key:"removeFrom",value:function(e,t){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("where cannot be undefined");var r=this.get(e);if(!a.is(r,"array"))throw new Error("value must be an array");var n=r.length;for(var i in r)if(r.hasOwnProperty(i)){var o=[];for(var s in t)t.hasOwnProperty(s)&&(a.is(t,"object")?o.push(void 0!==r[i][s]&&r[i][s]===t[s]):o.push(r[i]===t));o.length&&-1===o.indexOf(!1)&&r.splice(i,1)}n!==r.length&&this.set(e,r)}},{key:"bulkRemove",value:function(e){if(!a.is(e,"array"))throw new Error("keys must be an array of keys");for(var t=0;t<e.length;t++)this.remove(e[t],!0,{fromBulk:!0})}},{key:"clean",value:function(e){if(!a.is(e,"string"))throw new Error("key must be a string");for(var t in this._storage)this._storage.hasOwnProperty(t)&&-1!==t.indexOf(e)&&delete this._storage[t]}},{key:"all",value:function(){var e=[];for(var t in this._storage)this._storage.hasOwnProperty(t)&&(this.expired(t)?this.remove(t,!0):e.push({key:t,value:this._storage[t].value}));return e}},{key:"expired",value:function(e){if(this._storage[e]&&this._storage[e].expiresOn){return new Date>new Date(this._storage[e].expiresOn)}return!1}},{key:"clear",value:function(){this._storage=this._memory.data={}}},{key:"has",value:function(e){return this._storage.hasOwnProperty(e)}},{key:"onRemoved",value:function(e){this._onRemoved=e}},{key:"onCreated",value:function(e){this._onCreated=e}},{key:"onUpdated",value:function(e){this._onUpdated=e}}]),e}();e.exports=u}).call(t,r(3),r(0))},function(e,t,r){"use strict";var n,i="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};n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(e){"object"===("undefined"==typeof window?"undefined":i(window))&&(n=window)}e.exports=n},function(e,t,r){"use strict";(function(t){var r="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},n={};n.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},n.defaults=function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e.hasOwnProperty(i)?"object"===r(e[i])&&n.defaults(e[i],t[i]):e[i]=t[i]);return e},n.addSecondsToNow=function(e){var t=new Date;return new Date(t.setSeconds(t.getSeconds()+e))},n.addMSToNow=function(e){var t=new Date;return new Date(t.setMilliseconds(t.getMilliseconds()+e))},n.isServer=function(){return"object"===(void 0===t?"undefined":r(t))&&void 0!==t.pid},n.deprecated=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"warn";return void 0!==e&&(console[r](t||e),!0)},e.exports=n}).call(t,r(0))},function(e,t,r){"use strict"}]);
{
"name": "incache",
"version": "3.1.1",
"version": "4.0.0",
"description": "Simple key/value in-memory storage or on disk to persist some data",

@@ -48,2 +48,3 @@ "main": "index.js",

"mocha-lcov-reporter": "^1.3.0",
"typis": "^1.0.2",
"unminified-webpack-plugin": "^1.2.0",

@@ -50,0 +51,0 @@ "webpack": "^3.5.5",

@@ -13,3 +13,3 @@ <div align="center">

## Why?
## What does?
InCache is a module that store any info in memory, it can be used for example for storing **server sessions**, **caching http response** or **sharing singleton object** in your apps.

@@ -43,3 +43,5 @@ It also give you the possibility to save data on disk so you can avoid the data loss when the process exit or restart.

// Expires after 2 seconds
incache.set('my string', 'hello world', {life: 2});
incache.set('my string', 'hello world', {maxAge: 2000});
// Or expires on...
incache.set('my string', 'hello world', {expires: '2028-08-22 12:00:00'});
```

@@ -46,0 +48,0 @@

@@ -38,2 +38,3 @@ const helper = {};

* @returns {Date}
* @deprecated
*/

@@ -46,2 +47,12 @@ helper.addSecondsToNow = (seconds) => {

/**
* Adds milliseconds to current date
* @param ms {number} number of milliseconds to add
* @returns {Date}
*/
helper.addMSToNow = (ms) => {
let now = new Date();
return new Date(now.setMilliseconds(now.getMilliseconds() + ms));
};
/**
* Check if is Node environment

@@ -54,2 +65,16 @@ * @returns {boolean}

/**
* Throw deprecated
* @param prop
* @param msg
* @param [type=warn]
*/
helper.deprecated = (prop, msg, type='warn') => {
if(typeof prop !== 'undefined') {
console[type](msg || prop);
return true;
}
return false;
};
module.exports = helper;

@@ -5,11 +5,15 @@ const helper = require('./helper');

class InCache {
/**
* Create instance
* @param [opts] {Object} configuration object
* @param [opts.maxAge=0] {number} max age in milliseconds. If 0 not expire
* @param [opts.expires] {Date|string} a Date for expiration. (overwrites `opts.maxAge`)
* @param [opts.silent=false] {boolean} if true no event will be triggered
* @param [opts.save=true] {boolean} if true saves cache in disk. (server only)
* @param [opts.filePath=.incache] {string} cache file path
* @param [opts.storeName] {string} store name
* @param [opts.global] {Object} global record configuration
* @param [opts.global.silent=false] {boolean} if true no event will be triggered
* @param [opts.global.life=0] {number} max age. If 0 not expire
* @param [opts.global] {Object} **deprecated:** global record configuration
* @param [opts.global.silent=false] {boolean} **deprecated:** if true no event will be triggered, use `silent` instead
* @param [opts.global.life=0] {number} **deprecated:** max age in seconds. If 0 not expire, use `maxAge` instead
* @constructor

@@ -42,2 +46,4 @@ */

filePath: '.incache',
maxAge: 0,
silent: false,
global: {

@@ -90,8 +96,3 @@ silent: false,

* @param [opts] {Object} configuration object
* @param [opts.save=true] {boolean} if true saves cache in disk. (server only)
* @param [opts.filePath=.incache] {string} cache file path
* @param [opts.storeName] {string} store name
* @param [opts.global] {Object} global record configuration
* @param [opts.global.silent=false] {boolean} if true no event will be triggered
* @param [opts.global.life=0] {number} max age. If 0 not expire
* @see {@link constructor} for further information
*/

@@ -112,2 +113,7 @@ setConfig(opts = {}) {

if(opts.global) {
helper.deprecated(opts.global.life, 'global.life is deprecated use maxAge instead');
helper.deprecated(opts.global.silent, 'global.silent is deprecated use silent instead');
}
this._root[this.GLOBAL_KEY].config = helper.defaults(opts, this.DEFAULT_CONFIG);

@@ -132,2 +138,12 @@

/**
* InCache record
* @typedef {Object} InCache~record
* @property {boolean} isNew - indicates if is a new record
* @property {Date|null} createdOn - creation date
* @property {Date|null} updatedOn - update date
* @property {Date|null} expiresOn - expiry date
* @property {any} value - record value
*/
/**
* Set/update record

@@ -138,8 +154,10 @@ * @param key {any}

* @param [opts.silent=false] {boolean} if true no event will be triggered. (overwrites global configuration)
* @param [opts.life=0] {number} max age. If 0 not expire. (overwrites global configuration)
* @returns {{isNew: boolean, createdOn: Date|null, updatedOn: Date|null, value: *}}
* @param [opts.maxAge=0] {number} max age in milliseconds. If 0 not expire. (overwrites global configuration)
* @param [opts.expires] {Date|string} a Date for expiration. (overwrites global configuration and `opts.maxAge`)
* @param [opts.life=0] {number} **deprecated:** max age in seconds. If 0 not expire. (overwrites global configuration)
* @returns {InCache~record}
* @example
* inCache.set('my key', 'my value');
* inCache.set('my object', {a: 1, b: 2});
* inCache.set('my boolean', true, {life: 2}); // Expires after 2 seconds
* inCache.set('my boolean', true, {maxAge: 2000}); // Expires after 2 seconds
*/

@@ -155,6 +173,11 @@ set(key, value, opts = {}) {

opts = helper.defaults(opts, this.DEFAULT_CONFIG.global);
opts = helper.defaults(opts, this.DEFAULT_CONFIG);
if (opts.life && helper.is(opts.life, 'number')) {
if (opts.maxAge && helper.is(opts.maxAge, 'number')) {
record.expiresOn = helper.addMSToNow(opts.maxAge);
} else if (opts.life && helper.is(opts.life, 'number')) {
helper.deprecated(opts.life, 'life is deprecated use maxAge instead');
record.expiresOn = helper.addSecondsToNow(opts.life);
} else if (opts.expires && (helper.is(opts.expires, 'date') || helper.is(opts.expires, 'string'))) {
record.expiresOn = new Date(opts.expires);
}

@@ -204,3 +227,3 @@

* @param [onlyValue=true] {boolean} if false get InCache record
* @returns {any|null}
* @returns {any|null|InCache~record}
* @example

@@ -239,3 +262,3 @@ * inCache.get('my key');

* @param value {any}
* @returns {*}
* @returns {InCache~record}
* @example

@@ -261,3 +284,3 @@ * inCache.set('myArray', ['hello', 'world']);

* @param value {any}
* @returns {*}
* @returns {InCache~record}
* @example

@@ -498,3 +521,3 @@ * inCache.set('myArray', ['hello', 'world']);

* @param key {string} key of record created
* @param record {Object} record object
* @param record {InCache~record} record object
*/

@@ -518,3 +541,3 @@

* @param key {string} key of record updated
* @param record {Object} record object
* @param record {InCache~record} record object
*/

@@ -521,0 +544,0 @@ }

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