@ngx-cache/fs-storage
Version:
Fs storage for ngx-cache (server platform)
235 lines (228 loc) • 8.4 kB
JavaScript
import { __extends } from 'tslib';
import { ɵɵinject, ɵɵdefineInjectable, ɵsetClassMetadata, Injectable } from '@angular/core';
import { Storage } from '@ngx-cache/core';
import { EventEmitter } from 'events';
import { statSync, readdirSync, mkdirSync, writeFileSync, readFileSync, rmdirSync, unlinkSync } from 'fs';
import { resolve, join } from 'path';
var FsStorageLoader = (function () {
function FsStorageLoader() {
}
return FsStorageLoader;
}());
var FsStorageStaticLoader = (function () {
function FsStorageStaticLoader(providedSettings) {
if (providedSettings === void 0) { providedSettings = {
path: './.cache',
quota: 5 * 1024 * 1024
}; }
this.providedSettings = providedSettings;
}
Object.defineProperty(FsStorageStaticLoader.prototype, "path", {
get: function () {
return this.providedSettings.path;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FsStorageStaticLoader.prototype, "quota", {
get: function () {
return this.providedSettings.quota;
},
enumerable: true,
configurable: true
});
return FsStorageStaticLoader;
}());
var FsEvent = (function () {
function FsEvent(key, oldValue, newValue, pid, area) {
if (area === void 0) { area = 'fs-storage'; }
this.key = key;
this.oldValue = oldValue;
this.newValue = newValue;
this.pid = pid;
this.area = area;
}
return FsEvent;
}());
var FsItemMetadata = (function () {
function FsItemMetadata(key, index) {
this.key = key;
this.index = index;
}
return FsItemMetadata;
}());
var FsStorageService = (function (_super) {
__extends(FsStorageService, _super);
function FsStorageService(loader) {
var _this = _super.call(this) || this;
_this.loader = loader;
_this.instances = {};
_this.path = resolve(_this.loader.path);
_this.quota = _this.loader.quota;
if (_this.instances[_this.path]) {
return _this.instances[_this.path];
}
_this.length = 0;
_this.keys = [];
_this.pid = "pid:" + process.pid;
_this.metadata = new Map();
_this.bytesUsed = 0;
try {
var stat_1 = statSync(_this.path);
if (!stat_1.hasOwnProperty('isDirectory')) {
throw new Error("A file exists at the location " + _this.path + " when trying to create/open localStorage");
}
_this.length = 0;
_this.keys = readdirSync(_this.path);
_this.bytesUsed = 0;
var decodedKeys_1 = [];
_this.keys.forEach(function (key, index) {
var decodedKey = decodeURIComponent(key);
decodedKeys_1.push(decodedKey);
var item = new FsItemMetadata(key, index);
_this.metadata[decodedKey] = item;
stat_1 = _this.getStats(key);
if (!stat_1.hasOwnProperty('size')) {
item.size = stat_1.size;
_this.metadata[decodedKey] = item;
_this.bytesUsed += stat_1.size;
}
});
_this.keys = decodedKeys_1;
_this.length = _this.keys.length;
}
catch (error) {
mkdirSync(_this.path);
}
_this.instances[_this.path] = _this;
return _this;
}
FsStorageService.prototype.setItem = function (key, value) {
var hasListeners = EventEmitter.listenerCount(this, 'fs-storage');
var oldValue = hasListeners ? this.getItem(key) : undefined;
var item = this.metadata[key];
var oldLength = item ? item.size : 0;
if (this.bytesUsed - oldLength + Number(value.toString().length) > this.quota) {
throw new Error("Disk quota (" + this.quota / 1024 + "KB) has been reached!");
}
var encodedKey = encodeURIComponent(key);
var filename = join(this.path, encodedKey);
writeFileSync(filename, value.toString(), 'utf8');
if (!item) {
item = new FsItemMetadata(encodedKey, this.keys.push(key) - 1);
item.size = value.toString().length;
this.length += 1;
this.metadata[key] = item;
this.bytesUsed += value.toString().length;
}
if (!hasListeners) {
return false;
}
var e = new FsEvent(key, oldValue, value, this.pid);
return this.emit('fs-storage', e);
};
FsStorageService.prototype.getItem = function (key) {
var item = this.metadata[key];
if (item) {
var filename = join(this.path, item.key);
try {
return readFileSync(filename, 'utf8');
}
catch (error) {
this.removeItem(key);
}
}
return undefined;
};
FsStorageService.prototype.removeItem = function (key) {
var _this = this;
var hasListeners = EventEmitter.listenerCount(this, 'fs-storage');
var oldValue = hasListeners ? this.getItem(key) : undefined;
var item = this.metadata[key];
if (item) {
delete this.metadata[key];
this.length -= 1;
this.bytesUsed -= item.size;
this.keys.splice(item.index, 1);
var metadataRef = this.metadata;
metadataRef.forEach(function (k) {
var i = _this.metadata[k];
if (i.index > item.index) {
i.index -= 1;
}
});
var itemPath = join(this.path, item.key);
try {
this.deletePath(itemPath);
}
catch (error) {
}
if (!hasListeners) {
return false;
}
var e = new FsEvent(key, oldValue, undefined, this.pid);
return this.emit('fs-storage', e);
}
return false;
};
FsStorageService.prototype.key = function (index) {
return this.keys[index];
};
FsStorageService.prototype.clear = function () {
this.deleteDirectory(this.path);
this.length = 0;
this.keys = [];
this.metadata = new Map();
this.bytesUsed = 0;
var hasListeners = EventEmitter.listenerCount(this, 'fs-storage');
if (!hasListeners) {
return false;
}
var e = new FsEvent(undefined, undefined, undefined, this.pid);
return this.emit('fs-storage', e);
};
FsStorageService.prototype.getStats = function (key) {
var filename = join(this.path, encodeURIComponent(key));
try {
return statSync(filename);
}
catch (error) {
return undefined;
}
};
FsStorageService.prototype.deleteDirectory = function (dirPath) {
var _this = this;
var contents = readdirSync(dirPath);
contents.forEach(function (path) {
var joined = join(dirPath, path);
_this.deletePath(joined);
});
};
FsStorageService.prototype.deletePath = function (path) {
var isDirectory = statSync(path).isDirectory();
if (isDirectory) {
this.deleteDirectory(path);
rmdirSync(path);
}
else {
unlinkSync(path);
}
};
FsStorageService.prototype.deleteInstance = function () {
delete this.instances[this.path];
this.deletePath(this.path);
this.length = 0;
this.keys = [];
this.metadata = new Map();
this.bytesUsed = 0;
};
FsStorageService.ɵfac = function FsStorageService_Factory(t) { return new (t || FsStorageService)(ɵɵinject(FsStorageLoader)); };
FsStorageService.ɵprov = ɵɵdefineInjectable({ token: FsStorageService, factory: FsStorageService.ɵfac });
return FsStorageService;
}(Storage));
(function () { ɵsetClassMetadata(FsStorageService, [{
type: Injectable
}], function () { return [{ type: FsStorageLoader }]; }, null); })();
var fsStorageFactory = function () { return new FsStorageStaticLoader(); };
export { FsStorageLoader, FsStorageService, FsStorageStaticLoader, fsStorageFactory };
//# sourceMappingURL=ngx-cache-fs-storage.js.map