UNPKG

shelf.js

Version:

A modular, powerful wrapper library for persistent objects in the browser and Node.js

9 lines 4.19 kB
/** * # Shelf.JS * Copyright 2014 Stefano Balietti * GPL licenses. * * Persistent Client-Side Storage * --- */ (function(e){var t="5.1",n,r;r="volatile",n=e.store=function(e,t,r,i){r=r||{},i=r.type&&r.type in n.types?r.type:n.type;if(!i||!n.types[i]){n.log("Cannot save/load value. Invalid storage type selected: "+i,"ERR");return}return n.log("Accessing "+i+" storage"),n.types[i](e,t,r)},n.prefix="__shelf__",n.verbosity=0,n.types={};try{Object.defineProperty(n,"type",{set:function(e){return"undefined"==typeof n.types[e]?(n.log("Cannot set store.type to an invalid type: "+e),!1):(r=e,e)},get:function(){return r},configurable:!1,enumerable:!0})}catch(i){n.type=r}n.addType=function(e,t){n.types[e]=t,n[e]=function(t,r,i){return i=i||{},i.type=e,n(t,r,i)};if(!n.type||n.type==="volatile")n.type=e},n.onquotaerror=undefined,n.error=function(){console.log("shelf quota exceeded"),"function"==typeof n.onquotaerror&&n.onquotaerror(null)},n.log=function(e){n.verbosity>0&&console.log("Shelf v."+t+": "+e)},n.isPersistent=function(){return n.types?n.type==="volatile"?!1:!0:!1};try{Object.defineProperty(n,"persistent",{set:function(){},get:n.isPersistent,configurable:!1})}catch(i){n.persistent=!1}n.decycle=function(e){return JSON&&JSON.decycle&&"function"==typeof JSON.decycle&&(e=JSON.decycle(e)),e},n.retrocycle=function(e){return JSON&&JSON.retrocycle&&"function"==typeof JSON.retrocycle&&(e=JSON.retrocycle(e)),e},n.stringify=function(e){if(!JSON||!JSON.stringify||"function"!=typeof JSON.stringify)throw new Error("JSON.stringify not found. Received non-stringvalue and could not serialize.");return e=n.decycle(e),JSON.stringify(e)},n.parse=function(e){if("undefined"==typeof e)return undefined;if(JSON&&JSON.parse&&"function"==typeof JSON.parse)try{e=JSON.parse(e)}catch(t){n.log("Error while parsing a value: "+t,"ERR"),n.log(e)}return e=n.retrocycle(e),e},function(){function r(e){return n.parse(n.stringify(e))}var e={},t={};n.addType("volatile",function(n,i,s){return n?i===undefined?r(e[n]):(t[n]&&(clearTimeout(t[n]),delete t[n]),i===null?(delete e[n],null):(e[n]=i,s.expires&&(t[n]=setTimeout(function(){delete e[n],delete t[n]},s.expires)),i)):r(e)})}()})("undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports:this),function(e){function i(){if(u())return!1;for(var e=0;e<r.length;e++)r[e].call(r[e])}function s(){n=!0}function o(){n=!1}function u(){return n}function a(e){r.push(e)}var t=e.store;if(!t)throw new Error("fs.shelf.js: shelf.js core not found.");var n=!1,r=[],f=0;t.filename="./shelf.out";var l=require("fs"),c=require("path"),h=require("util"),p=65536,d=new Buffer(p),v=function(e,t){var n,r,i,s;r=l.openSync(e,"r"),i=l.openSync(t,"w"),n=1,s=0;while(n>0)n=l.readSync(r,d,0,p,s),l.writeSync(i,d,0,n),s+=n;return l.closeSync(r),l.closeSync(i)},m={},g=function(e,n){if(u())return a(this),!1;s();var r=e||t.filename;if(!r)return t.log("You must specify a valid file.","ERR"),!1;var f=c.dirname(r)+"/."+c.basename(r);v(r,f);var h=t.stringify(n);return h=h.substr(1,h=h.substr(0,h.legth-1)),l.writeFileSync(r,h,"utf-8"),l.unlinkSync(f),o(),i(),!0};if("undefined"!=typeof l.appendFileSync)var y=function(e,n,r){var i=e||t.filename;if(!i)return t.log("You must specify a valid file.","ERR"),!1;if(!n)return;var s=t.stringify(n)+": "+t.stringify(r)+",\n";return l.appendFileSync(i,s,"utf-8")};else var y=function(e,n,r){var i=e||t.filename;if(!i)return t.log("You must specify a valid file.","ERR"),!1;if(!n)return;var s=t.stringify(n)+": "+t.stringify(r)+",\n",o=l.openSync(i,"a","0666");return l.writeSync(o,s,null,"utf8"),l.closeSync(o),!0};var b=function(e,n){var r=e||t.filename;if(!r)return t.log("You must specify a valid file.","ERR"),!1;var i=l.readFileSync(r,"utf-8");i=i.substr(0,i.length-2);var s=t.parse("{"+i+"}");return n?s[n]:s},w=function(e,n){var r=e||t.filename,i=b(r);return delete i[n],g(r,i),null};t.addType("fs",function(e,n,r){var i=r.file||t.filename;return e?n===undefined?b(i,e):(m[e]&&(clearTimeout(m[e]),w(i,e)),n===null?(w(i,e),null):(y(i,e,n),r.expires&&(m[e]=setTimeout(function(){w(i,e)},r.expires)),n)):b(i)})}("undefined"!=typeof module&&"function"==typeof require?module.exports||module.parent.exports:{})