@secure-storage/common
Version:
NPM package for storing and managing data across window local-storage and session-storage securely
2 lines • 3.9 kB
JavaScript
var w=process.env;import f from"crypto-js";var i=typeof process=="object"&&(process==null?void 0:process.env)&&typeof process.env=="object"&&process.env||null,c={secret:(i==null?void 0:i.SECURE_STORAGE_SECRET)||"x1bQYQA4vSEcR6RQ05XtJg",prefix:(i==null?void 0:i.SECURE_STORAGE_PREFIX)||"@secst"},y=(e,r)=>{if(!(typeof e>"u")&&(typeof e!="string"||!e.trim()||typeof(r==null?void 0:r.appendChecker)=="function"&&!r.appendChecker(e)))throw new Error((r==null?void 0:r.message)||"Value must be a valid string")},E=e=>{try{if(e&&typeof e=="object"){y(e.prefix,{message:"Prefix must be a valid string"}),y(e.secret,{message:"Secret must be a valid string"});for(let r in e)typeof e[r]<"u"&&(c[r]=e[r])}}catch(r){console.error("Error secure storage => update-configuration :",r)}};var u=c.secret,m=e=>{try{if(typeof e!="string")throw new Error("Value must be string");return f.AES.encrypt(e,u).toString()}catch(r){return console.error("Error secure storage => encrypt :",r),null}},d=e=>{try{if(typeof e!="string")throw new Error("Value must be string");return f.AES.decrypt(e,u).toString(f.enc.Utf8)}catch(r){return console.error("Error secure storage => decrypt :",r),null}};var n,h=e=>{n=e},b=()=>{try{let e=c.prefix;if(typeof e!="string")throw new Error("Invalid prefix, not a string");return`${e}`}catch(e){return console.error("Error secure storage => prefix :",e),null}},s=b();var x=e=>{try{if(typeof e=="function"||typeof e=="symbol")throw new Error("Data must not be a function or symbol");return JSON.stringify({data:e,type:typeof e})}catch(r){return console.error("Error secure storage => modify data :",r),null}},T=e=>{try{if(typeof e!="string")throw new Error("Value must be string");let r=JSON.parse(e);if(typeof r!="object"||!Object.getOwnPropertyNames(r).includes("data")||!Object.getOwnPropertyNames(r).includes("type"))throw new Error("Invalid data scheme");let t=typeof r.data;if(r.type=t,t==="symbol"||t==="function")throw new Error("Invalid data type, does not supports function or symbol");return r}catch(r){return console.error("Error secure storage => get data :",r),null}},v=["local","session"],S=e=>{if(typeof e!="string"||!v.includes(e))throw new Error("Store type must be one of [local, session]");if(typeof n!="object"||typeof n.localStorage!="object"||typeof n.sessionStorage!="object")throw new Error("Invalid window, please use only in browser")},p=(e,r,t)=>{if(typeof e!="string")throw new Error("Prefix is invalid");if(typeof r!="string")throw new Error("Key must be string");S(t)},C=e=>{try{let r=e.length;for(let t=0;t<r;t++){let o=e.key(t);o!=null&&o.startsWith(`${s}.`)&&e.removeItem(o)}}catch(r){console.error("Error secure storage => matched-clear :",r)}},g=class{constructor(r){this.store="local";this.storage=n.localStorage;this.setItem=(r,t)=>{try{p(s,r,this.store);let o=x(t);if(typeof o!="string")return;let a=m(o);if(typeof a!="string")return;let l=`${s}.${r}`;this.storage.setItem(l,a)}catch(o){console.error("Error secure storage => setItem :",o)}};this.getItem=r=>{try{p(s,r,this.store);let t=`${s}.${r}`,o=null;o=this.storage.getItem(t);let a=d(o);if(typeof a!="string")return null;let l=T(a);return l?l.data:null}catch(t){return console.error("Error secure storage => setItem :",t),null}};this.removeItem=r=>{try{p(s,r,this.store);let t=`${s}.${r}`;this.storage.removeItem(t)}catch(t){console.error("Error secure storage => removeItem :",t)}};this.clear=()=>{try{p(s,"",this.store),C(this.storage)}catch(r){console.error("Error secure storage => clear :",r)}};this.forceClear=()=>{try{p(s,"",this.store),this.storage.clear()}catch(r){console.error("Error secure storage => force-clear :",r)}};try{S(r),this.store=r,r==="local"&&(this.storage=n.localStorage),r==="session"&&(this.storage=n.sessionStorage)}catch{}}};h(window);var _=new g("local"),k=new g("session");export{E as configure,_ as localStorage,k as sessionStorage};
//# sourceMappingURL=index.js.map