Skip to content

Commit

Permalink
Closed #5: allow memoryStorage to be the default fallback for session…
Browse files Browse the repository at this point in the history
…Storage
  • Loading branch information
jherax committed Jun 21, 2017
1 parent fe5e04c commit 805af58
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 633 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

<!-- markdownlint-disable MD024 MD033 -->

## 2.2.0

### Improvements

1. Allow `memoryStorage` to be the default fallback for `sessionStorage`

---

## 2.1.3

### Improvements
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ $ yarn add proxy-storage
<script src="https://unpkg.com/proxy-storage/dist/proxy-storage.min.js"></script>

<!-- or from rawgit.com -->
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.1.3/dist/proxy-storage.min.js"></script>
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.2.0/dist/proxy-storage.min.js"></script>
```

In the above case, [`proxyStorage`](#api) is included as a global object
Expand Down Expand Up @@ -164,9 +164,9 @@ usually `localStorage`, which is established when the library is initialized.
The availability of the storage mechanisms is determined in the following order:

1. **`localStorage`**: adapter of the [window.localStorage] object.
1. **`sessionStorage`**: adapter of the [window.sessionStorage] object.
1. **`cookieStorage`**: adapter of the [document.cookie] object, and
normalized with the [`WebStorage`](#webstorage) interface.
1. **`sessionStorage`**: adapter of the [window.sessionStorage] object.
1. **`memoryStorage`**: internal storage mechanism that can be used as
_fallback_ when none of the above mechanisms are available. The behavior
of `memoryStorage` is similar to `sessionStorage`, which let you to persist
Expand Down Expand Up @@ -274,11 +274,11 @@ import { WebStorage, isAvailable } from 'proxy-storage';
isAvailable.sessionStorage = false;

const sessionStore = new WebStorage('sessionStorage');
// sessionStorage is not available. Falling back to localStorage
// sessionStorage is not available. Falling back to memoryStorage
sessionStore.setItem('ulugrun', 3.1415926);

// as sessionStorage is not available, the instance obtained
// is the first storage mechanism available: localStorage
// is the first storage mechanism available: memoryStorage
console.dir(sessionStore);
```

Expand Down Expand Up @@ -526,8 +526,8 @@ Determines which storage mechanisms are available to read/write/delete data.
It contains the following flags:

- **`localStorage`**: is set to `true` if the local storage is available.
- **`sessionStorage`**: is set to `true` if the session storage is available.
- **`cookieStorage`**: is set to `true` if the cookie storage is available.
- **`sessionStorage`**: is set to `true` if the session storage is available.
- **`memoryStorage`**: always is set to `true`.

**Example**
Expand All @@ -538,7 +538,7 @@ import storage, * as proxyStorage from 'proxy-storage';

const flags = proxyStorage.isAvailable;

if (!flags.localStorage && !flags.sessionStorage) {
if (!flags.sessionStorage) {
// forces the storage mechanism to memoryStorage
proxyStorage.configStorage.set('memoryStorage');
}
Expand Down
56 changes: 28 additions & 28 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! proxyStorage@v2.1.3. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
/*! proxyStorage@v2.2.0. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand All @@ -12,41 +12,41 @@
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;

/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };

/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/
/******/ // Flag the module as loaded
/******/ module.l = true;

/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };

/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
Expand All @@ -57,7 +57,7 @@ return /******/ (function(modules) { // webpackBootstrap
/******/ });
/******/ }
/******/ };

/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
Expand All @@ -66,13 +66,13 @@ return /******/ (function(modules) { // webpackBootstrap
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };

/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };

/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 6);
/******/ })
Expand Down Expand Up @@ -180,9 +180,10 @@ Object.defineProperty(exports, "__esModule", {
*/
var isAvailable = exports.isAvailable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
memoryStorage: true };
sessionStorage: false,
memoryStorage: true // fallback storage
};

/***/ }),
/* 2 */
Expand Down Expand Up @@ -322,8 +323,10 @@ var webStorageSettings = {
*/
function storageAvailable(storageType) {
if (webStorageSettings.isAvailable[storageType]) return storageType;
console.warn(storageType + ' is not available. Falling back to ' + webStorageSettings.default); // eslint-disable-line
return webStorageSettings.default;
var fallback = storageType === 'sessionStorage' ? 'memoryStorage' : webStorageSettings.default;
var msg = storageType + ' is not available. Falling back to ' + fallback;
console.warn(msg); // eslint-disable-line
return fallback;
}

/**
Expand All @@ -339,7 +342,6 @@ function storageAvailable(storageType) {
*/

var WebStorage = function () {

/**
* Creates an instance of WebStorage.
*
Expand Down Expand Up @@ -542,7 +544,8 @@ var $cookie = {
set: function set(value) {
document.cookie = value;
},
data: {} };
data: {} // metadata associated to the cookies
};

/**
* @private
Expand Down Expand Up @@ -888,9 +891,6 @@ var configStorage = {
* @return {void}
*/
set: function set(storageType) {
if (!Object.prototype.hasOwnProperty.call(_webStorage.proxy, storageType)) {
throw new Error('Storage type "' + storageType + '" is not valid');
}
exports.default = storage = new _webStorage2.default(storageType);
}
};
Expand All @@ -909,10 +909,10 @@ function isStorageAvailable(storageType) {
try {
storageObj.setItem(data, data);
storageObj.removeItem(data);
return true;
} catch (e) {
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -940,8 +940,8 @@ function storageAvailable(storageType) {
*/
function init() {
_isAvailable.isAvailable.localStorage = isStorageAvailable('localStorage');
_isAvailable.isAvailable.sessionStorage = isStorageAvailable('sessionStorage');
_isAvailable.isAvailable.cookieStorage = isStorageAvailable('cookieStorage');
_isAvailable.isAvailable.sessionStorage = isStorageAvailable('sessionStorage');
_webStorage.webStorageSettings.isAvailable = _isAvailable.isAvailable;
// sets the default storage mechanism available
Object.keys(_isAvailable.isAvailable).some(storageAvailable);
Expand Down
4 changes: 2 additions & 2 deletions dist/proxy-storage.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/proxy-storage.min.map

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-storage",
"version": "2.1.3",
"version": "2.2.0",
"description": "Storage mechanism that implements the Web Storage interface",
"author": "David Rivera <jherax@gmail.com>",
"main": "dist/proxy-storage.js",
Expand Down Expand Up @@ -35,17 +35,17 @@
},
"license": "MIT",
"devDependencies": {
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.4.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"clean-webpack-plugin": "^0.1.15",
"eslint": "^3.17.1",
"eslint-config-airbnb-base": "^11.1.1",
"eslint-loader": "^1.6.3",
"eslint-plugin-import": "^2.2.0",
"webpack": "^2.2.1"
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"eslint": "^4.0.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-loader": "^1.8.0",
"eslint-plugin-import": "^2.3.0",
"webpack": "^2.6.1"
},
"babel": {
"presets": [
Expand Down
2 changes: 1 addition & 1 deletion src/is-available.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
export const isAvailable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
sessionStorage: false,
memoryStorage: true, // fallback storage
};
7 changes: 2 additions & 5 deletions src/proxy-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ const configStorage = {
* @return {void}
*/
set(storageType) {
if (!Object.prototype.hasOwnProperty.call(proxy, storageType)) {
throw new Error(`Storage type "${storageType}" is not valid`);
}
storage = new WebStorage(storageType);
},
};
Expand All @@ -66,10 +63,10 @@ function isStorageAvailable(storageType) {
try {
storageObj.setItem(data, data);
storageObj.removeItem(data);
return true;
} catch (e) {
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -97,8 +94,8 @@ function storageAvailable(storageType) {
*/
function init() {
isAvailable.localStorage = isStorageAvailable('localStorage');
isAvailable.sessionStorage = isStorageAvailable('sessionStorage');
isAvailable.cookieStorage = isStorageAvailable('cookieStorage');
isAvailable.sessionStorage = isStorageAvailable('sessionStorage');
webStorageSettings.isAvailable = isAvailable;
// sets the default storage mechanism available
Object.keys(isAvailable).some(storageAvailable);
Expand Down
8 changes: 5 additions & 3 deletions src/web-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ const webStorageSettings = {
*/
function storageAvailable(storageType) {
if (webStorageSettings.isAvailable[storageType]) return storageType;
console.warn(`${storageType} is not available. Falling back to ${webStorageSettings.default}`); // eslint-disable-line
return webStorageSettings.default;
const fallback = (storageType === 'sessionStorage' ?
'memoryStorage' : webStorageSettings.default);
const msg = `${storageType} is not available. Falling back to ${fallback}`;
console.warn(msg); // eslint-disable-line
return fallback;
}

/**
Expand All @@ -128,7 +131,6 @@ function storageAvailable(storageType) {
* @type {class}
*/
class WebStorage {

/**
* Creates an instance of WebStorage.
*
Expand Down
32 changes: 12 additions & 20 deletions webpack/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config = {
path: PATHS.dist.folder,
filename: '[name].js',
libraryTarget: 'umd',
library: 'proxyStorage', // global var in the browser
library: 'proxyStorage', // global
},
module: {
rules: [
Expand All @@ -25,8 +25,15 @@ const config = {
},
{
test: PATHS.source.folder,
exclude: /node_modules/,
enforce: 'pre', // preLoaders
loader: 'eslint-loader',
options: {
// https://github.com/MoOx/eslint-loader
configFile: '.eslintrc.json',
failOnWarning: false,
failOnError: true,
},
},
],
},
Expand All @@ -36,31 +43,16 @@ const config = {
verbose: true,
// exclude: [],
}),
// https://webpack.js.org/guides/migrating/#uglifyjsplugin-minimize-loaders
new webpack.LoaderOptionsPlugin({
debug: false,
minimize: true,
options: {
eslint: {
// https://github.com/MoOx/eslint-loader
// https://survivejs.com/webpack/developing/linting/#configuring-eslint-further
configFile: '.eslintrc.json',
failOnWarning: false,
failOnError: true,
},
},
}),
// http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
// https://webpack.js.org/plugins/uglifyjs-webpack-plugin/
new webpack.optimize.UglifyJsPlugin({
test,
minimize: true,
sourceMap: true, // map error message locations to modules
// https://github.com/mishoo/UglifyJS2#compressor-options
// https://github.com/mishoo/UglifyJS2#compress-options
compress: {
warnings: true,
dead_code: true,
dead_code: true, // remove unreachable code
drop_debugger: true,
drop_console: false,
pure_funcs: ['console.log'],
},
mangle: {
except: ['WebStorage'],
Expand Down
Loading

0 comments on commit 805af58

Please sign in to comment.