@ngx-cache/fs-storage
Version:
Fs storage for ngx-cache (server platform)
16 lines (14 loc) • 5.48 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@ngx-cache/core"),require("events"),require("fs"),require("path")):"function"==typeof define&&define.amd?define("@ngx-cache/fs-storage",["exports","@angular/core","@ngx-cache/core","events","fs","path"],e):e(((t=t||self)["ngx-cache"]=t["ngx-cache"]||{},t["ngx-cache"]["fs-storage"]={}),t.ng.core,t.ngxCache_core,t.events,t.fs,t.path)}(this,(function(t,e,n,r,i,a){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};var s=function(){},h=function(){function t(t){void 0===t&&(t={path:"./.cache",quota:5242880}),this.providedSettings=t}return Object.defineProperty(t.prototype,"path",{get:function(){return this.providedSettings.path},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"quota",{get:function(){return this.providedSettings.quota},enumerable:!0,configurable:!0}),t}(),c=function(t,e,n,r,i){void 0===i&&(i="fs-storage"),this.key=t,this.oldValue=e,this.newValue=n,this.pid=r,this.area=i},u=function(t,e){this.key=t,this.index=e},p=function(t){function n(e){var n=t.call(this)||this;if(n.loader=e,n.instances={},n.path=a.resolve(n.loader.path),n.quota=n.loader.quota,n.instances[n.path])return n.instances[n.path];n.length=0,n.keys=[],n.pid="pid:"+process.pid,n.metadata=new Map,n.bytesUsed=0;try{var r=i.statSync(n.path);if(!r.hasOwnProperty("isDirectory"))throw new Error("A file exists at the location "+n.path+" when trying to create/open localStorage");n.length=0,n.keys=i.readdirSync(n.path),n.bytesUsed=0;var o=[];n.keys.forEach((function(t,e){var i=decodeURIComponent(t);o.push(i);var a=new u(t,e);n.metadata[i]=a,(r=n.getStats(t)).hasOwnProperty("size")||(a.size=r.size,n.metadata[i]=a,n.bytesUsed+=r.size)})),n.keys=o,n.length=n.keys.length}catch(t){i.mkdirSync(n.path)}return n.instances[n.path]=n,n}return function(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(n,t),n.prototype.setItem=function(t,e){var n=r.EventEmitter.listenerCount(this,"fs-storage"),o=n?this.getItem(t):void 0,s=this.metadata[t],h=s?s.size:0;if(this.bytesUsed-h+Number(e.toString().length)>this.quota)throw new Error("Disk quota ("+this.quota/1024+"KB) has been reached!");var p=encodeURIComponent(t),d=a.join(this.path,p);if(i.writeFileSync(d,e.toString(),"utf8"),s||((s=new u(p,this.keys.push(t)-1)).size=e.toString().length,this.length+=1,this.metadata[t]=s,this.bytesUsed+=e.toString().length),!n)return!1;var f=new c(t,o,e,this.pid);return this.emit("fs-storage",f)},n.prototype.getItem=function(t){var e=this.metadata[t];if(e){var n=a.join(this.path,e.key);try{return i.readFileSync(n,"utf8")}catch(e){this.removeItem(t)}}},n.prototype.removeItem=function(t){var e=this,n=r.EventEmitter.listenerCount(this,"fs-storage"),i=n?this.getItem(t):void 0,o=this.metadata[t];if(o){delete this.metadata[t],this.length-=1,this.bytesUsed-=o.size,this.keys.splice(o.index,1),this.metadata.forEach((function(t){var n=e.metadata[t];n.index>o.index&&(n.index-=1)}));var s=a.join(this.path,o.key);try{this.deletePath(s)}catch(t){}if(!n)return!1;var h=new c(t,i,void 0,this.pid);return this.emit("fs-storage",h)}return!1},n.prototype.key=function(t){return this.keys[t]},n.prototype.clear=function(){if(this.deleteDirectory(this.path),this.length=0,this.keys=[],this.metadata=new Map,this.bytesUsed=0,!r.EventEmitter.listenerCount(this,"fs-storage"))return!1;var t=new c(void 0,void 0,void 0,this.pid);return this.emit("fs-storage",t)},n.prototype.getStats=function(t){var e=a.join(this.path,encodeURIComponent(t));try{return i.statSync(e)}catch(t){return}},n.prototype.deleteDirectory=function(t){var e=this;i.readdirSync(t).forEach((function(n){var r=a.join(t,n);e.deletePath(r)}))},n.prototype.deletePath=function(t){i.statSync(t).isDirectory()?(this.deleteDirectory(t),i.rmdirSync(t)):i.unlinkSync(t)},n.prototype.deleteInstance=function(){delete this.instances[this.path],this.deletePath(this.path),this.length=0,this.keys=[],this.metadata=new Map,this.bytesUsed=0},n.ɵfac=function(t){return new(t||n)(e.ɵɵinject(s))},n.ɵprov=e.ɵɵdefineInjectable({token:n,factory:n.ɵfac}),n}(n.Storage);e.ɵsetClassMetadata(p,[{type:e.Injectable}],(function(){return[{type:s}]}),null);t.FsStorageLoader=s,t.FsStorageService=p,t.FsStorageStaticLoader=h,t.fsStorageFactory=function(){return new h},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-cache-fs-storage.umd.min.js.map