Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/persistence / dist / adapters / server / localStorage.js
Size: Mime:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const readwrite_1 = require("./readwrite");
/**
 * @todo set this.console..
 */
const resolveToRoot = (x) => path_1.resolve(process.cwd(), x);
const cachePath = resolveToRoot('./.tmp/cache.json');
const TEMP_PATH = resolveToRoot('./.tmp');
const toSafePath = (x) => TEMP_PATH + '/' + encodeURIComponent(x) + '.txt';
// create if needed
if (readwrite_1.exists(cachePath) === false) {
    console.debug('[persistence]:[server] CREATING_CACHE');
    readwrite_1.write(cachePath, {});
}
const cache = readwrite_1.readJSON(cachePath);
function save(content = undefined) {
    console.debug('[persistence]:[server] writing to cache: ' + cachePath);
    readwrite_1.write(cachePath, content === undefined ? cache : content);
    return true;
}
function getItem(key) {
    const cachePathKey = toSafePath(key);
    // return key in cache ? stringify(cache[key]) : undefined
    return key in cache ? cache[key] : readwrite_1.read(cachePathKey);
}
function setItem(key, value) {
    cache[key] = value;
    // fs based cache
    const cachePathKey = toSafePath(key);
    readwrite_1.write(cachePathKey, value);
    return true;
    // return save()
}
function removeItem(key) {
    let found = key in cache;
    if (found) {
        return delete cache[key];
    }
    // @todo rimraf
    save();
    return false;
}
function clear() {
    Object.keys(cache || {}).forEach(key => {
        delete cache[key];
    });
    return save();
}
function localStorage(key, value) {
    /**
     * @todo not working yet
     */
}
exports.localStorage = localStorage;
localStorage.clear = clear;
localStorage.removeItem = removeItem;
localStorage.setItem = setItem;
localStorage.getItem = getItem;
localStorage.localStorage = localStorage;
/**
 * @todo @fixme globals
 */
if (typeof global === 'object') {
    global.localStorage = localStorage;
}
Object.defineProperty(localStorage, 'length', {
    get() {
        return Object.keys(localStorage).length;
    },
});
// if (process.env.NODE_ENV === 'test') {
Object.defineProperty(localStorage, 'type', {
    // configurable: false,
    // enumerable: false,
    // writable: false,
    value: 'server',
});
exports.default = localStorage;
//# sourceMappingURL=localStorage.js.map