bucket-storage
Version:
Bucket-storage is an easy storage. It supports rich functions
2 lines (1 loc) • 6.59 kB
JavaScript
function h(a,t){console.error(`bucket-storage ${a} error: `+t)}function f(a,t){return toString.call(a)===`[object ${t}]`}function s(a){return typeof a!="undefined"}function U(a){return!s(a)}function l(a){return a===null}function u(a){return U(a)||l(a)}function d(a){return!u(a)&&f(a,"Object")}function V(a){return!u(a)&&f(a,"Array")}function o(a){return!V(a)&&!d(a)}function N(a){return f(a,"Date")}var T=new Map,O={key(a){return null},setItem:(a,t)=>{T.set(a,t)},get length(){return T.size},getItem:a=>{let t=T.get(a);return s(t)?t:null},removeItem:a=>{T.delete(a)},clear:()=>{T.clear()}};var i={local:window.localStorage,session:window.sessionStorage,memory:O};function p(a){return!u(a)&&Object.keys(i).includes(a.toString())}var B="__BUCKET-STORAGE__",m="_BUCKET-STORAGE_",k="@BUCKET-STORAGE",x="__BUCKET_STORAGE_KEYS__";var S=class{constructor(t,e){this.bucketName=e,this.storageType=t,this.manager=new Map}getBucketName(t){return!u(t)&&o(t)?t.toString():this.bucketName}getStorageType(t){return!u(t)&&p(t)?t:this.storageType}getStorage(t){return i[this.getStorageType(t)]}getCurrentStorageTypeManager(t){var e;return((e=this.manager)==null?void 0:e.get(this.getStorageType(t)))||null}getCurrentBucket(t,e){var r;return((r=this.getCurrentStorageTypeManager(e))==null?void 0:r[this.getBucketName(t)])||null}};import{encrypt as W,decrypt as I}from"crypto-js/aes";import R from"crypto-js/mode-ecb";import b from"crypto-js/enc-utf8";import j from"crypto-js/pad-pkcs7";import v from"crypto-js/enc-base64";import{parse as C}from"crypto-js/enc-utf8";var E=class{constructor(t={}){this.key="";let{key:e,iv:r}=t;this.key=C(e||m),this.iv=C(r||m)}get getOptions(){return{mode:R,padding:j,iv:this.iv}}encrypt(t){return W(t,this.key,this.getOptions).toString()}decrypt(t){return I(t,this.key,this.getOptions).toString(b)}};function w(a){return b.parse(a).toString(v)}function _(a){return v.parse(a).toString(b)}var M=class extends S{constructor(t,e){super(t,e);this.init()}init(){let t=e=>{let g=this.getStorage(e).getItem(x);if(!!g)try{let n=JSON.parse(_(g));this.manager.set(e,n)}catch(n){this.manager.delete(e),h(`init ${e} keys`,n)}};Object.keys(i).forEach(e=>{t(e)})}exist(t,e){var r;return((r=this.getCurrentBucket(e==null?void 0:e.bucketName,e==null?void 0:e.storageType))==null?void 0:r.includes(t))||!1}add(t,e){if(this.exist(t,e))return;let r=this.getStorageType(e==null?void 0:e.storageType);!this.manager.has(r)&&this.manager.set(r,{});let g=this.getCurrentStorageTypeManager(r),n=this.getBucketName(e==null?void 0:e.bucketName);!g[n]&&(g[n]=[]),g[n].push(t),this.durable(e==null?void 0:e.storageType)}remove(t,e){if(!this.exist(t,e))return;let r=this.getCurrentBucket(e==null?void 0:e.bucketName,e==null?void 0:e.storageType);r.splice(r.indexOf(t),1),this.durable(e==null?void 0:e.storageType)}clear(t,e){let r=this.getCurrentBucket(t,e);!(r==null?void 0:r.length)||(r.splice(0,r.length),this.durable(e))}durable(t){t=this.getStorageType(t);let e=this.getCurrentStorageTypeManager(t);if(u(e))return;let r=w(JSON.stringify(e));this.getStorage(t).setItem(x,r)}};var K=class extends S{constructor(t,e){super(t,e)}exist(t,e,r){var g,n;return((n=(g=this.getCurrentBucket(r==null?void 0:r.bucketName,r==null?void 0:r.storageType))==null?void 0:g[t])==null?void 0:n.includes(e))||!1}add(t,e,r){if(this.exist(t,e,r))return;let g=this.getStorageType(r==null?void 0:r.storageType);!this.manager.has(g)&&this.manager.set(g,{});let n=this.getCurrentStorageTypeManager(g),c=this.getBucketName(r==null?void 0:r.bucketName);!n[c]&&(n[c]={}),!n[c][t]&&(n[c][t]=[]),n[c][t].push(e)}get(t,e){var r;return((r=this.getCurrentBucket(e==null?void 0:e.bucketName,e==null?void 0:e.storageType))==null?void 0:r[t])||[]}remove(t,e,r){if(!this.exist(t,e,r))return;let g=this.getCurrentBucket(r==null?void 0:r.bucketName,r==null?void 0:r.storageType);g[t].splice(g[t].indexOf(e),1)}clear(t,e){var g;let r=this.getCurrentBucket(e==null?void 0:e.bucketName,e==null?void 0:e.storageType);l(r)||!((g=r[t])==null?void 0:g.length)||r[t].splice(0,r[t].length)}};var D=class{constructor(t){this.encrypt=!0;this.bucketName=B;this.expire=null;this.storageType="session";let{encrypt:e,bucketName:r,expire:g,storageType:n,encryptOption:c}=t||{};this.bucketName=!u(r)&&o(r)?r==null?void 0:r.toString():B,this.expire=Number(g)?N(g)?g:Number(g):null,this.storageType=!u(n)&&p(n)?n:"session",this.encrypt=(e==null?void 0:e.toString())!=="false",this.encryption=new E(d(c)?c:{iv:m,key:m}),this.keyManager=new M(this.storageType,this.bucketName),this.watchManager=new K(this.storageType,this.bucketName)}set(t,e,r){let g;this.keyManager.exist(t,r)&&(g=this.get(t,r));let n=JSON.stringify({value:s(e)?e:null,time:Date.now(),expire:this.getExpire(r==null?void 0:r.expire)}),c=this.encryptStr(n,r==null?void 0:r.encrypt);this.getStorage(r==null?void 0:r.storageType).setItem(this.getKey(t,r==null?void 0:r.bucketName),c),s(g)&&this.watchManager.get(t,r).forEach(y=>{y(e,g)}),this.keyManager.add(t,r)}get(t,e){if(!this.keyManager.exist(t,e))return null;let r=this.getStorage(e==null?void 0:e.storageType).getItem(this.getKey(t,e==null?void 0:e.bucketName));if(l(r))return null;try{let g=this.decryptStr(r),n=JSON.parse(g),{value:c,expire:y}=n;return u(y)||y>=Date.now()?c:(this.remove(t),null)}catch(g){return h(`get ${t} value`,g),null}}remove(t,e){if(!this.keyManager.exist(t,e))return;let r=this.getStorage(e==null?void 0:e.storageType),g=this.watchManager.get(t,e);t=this.getKey(t,e==null?void 0:e.bucketName);let n=(g==null?void 0:g.length)?r.getItem(t):void 0;if(r.removeItem(t),!u(n)){let c=this.decryptStr(n),y=JSON.parse(c),{value:A}=y;g.forEach(P=>{P(null,A)})}this.keyManager.remove(t,e)}clear(t,e){var n;let r=(n=this.keyManager)==null?void 0:n.getCurrentBucket(t,e==null?void 0:e.storageType);if(!(r==null?void 0:r.length))return;let g=this.getStorage(e==null?void 0:e.storageType);r.forEach(c=>{g.removeItem(this.getKey(c,t))}),this.keyManager.clear(t,e==null?void 0:e.storageType)}watch(t,e,r){this.watchManager.add(t,e,r)}unWatch(t,e,r){this.watchManager.remove(t,e,r)}getStorageType(t){return!u(t)&&p(t)?t:this.storageType}getStorage(t){return i[this.getStorageType(t)]}getBucketName(t){return!u(t)&&o(t)?t.toString():this.bucketName}getKey(t,e){return e=this.getBucketName(e),`${e}__${t}`}getExpire(t){let e=r=>Number(r)?N(r)?r.getTime():new Date().getTime()+Number(r):null;return s(t)?e(t):e(this.expire)}encryptStr(t,e){return e=s(e)?e.toString()!=="false":this.encrypt,e?this.encryption.encrypt(t)+k:t}decryptStr(t){return t.endsWith(k)?this.encryption.decrypt(t.substr(0,t.lastIndexOf(k))):t}};export{D as default};