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/modules / ___dist / persistance / server / localStorage.js
Size: Mime:
"use strict";

const _require = require('path'),
      resolve = _require.resolve;

const _require2 = require('chain-able-boost'),
      stringify = _require2.stringify; // @todo @fixme
// const { resolveToRoot } = require('config/resolveToRoot')


const _require3 = require("./readwrite"),
      readJSON = _require3.readJSON,
      read = _require3.read,
      write = _require3.write,
      exists = _require3.exists;

const resolveToRoot = x => 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 (exists(cachePath) === false) {
  console.log('CREATING_CACHE');
  write(cachePath, {});
}

const cache = readJSON(cachePath);

function save(content = undefined) {
  console.log('writing to cache: ', cachePath);
  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] : read(cachePathKey);
}

function setItem(key, value) {
  cache[key] = value; // fs based cache

  const cachePathKey = toSafePath(key);
  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) {//
}

localStorage.clear = clear;
localStorage.removeItem = removeItem;
localStorage.setItem = setItem;
localStorage.getItem = getItem;
localStorage.localStorage = localStorage;

if (typeof global === 'object') {
  global.localStorage = localStorage;
}

Object.defineProperty(localStorage, 'length', {
  get() {
    return Object.keys(localStorage).length;
  }

});
Object.defineProperty(localStorage, 'type', {
  configurable: false,
  enumerable: false,
  writable: false,
  value: 'server'
});
module.exports = localStorage;