Skip to content

Commit 16772a5

Browse files
authored
[NOCSL] Check if storage available before attempting access (#51)
* check if storage available before attempting access * add tests
1 parent e14a383 commit 16772a5

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

spec/005-storage.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var expect = require('chai').expect;
22
var jsdom = require('jsdom');
3-
var sinon = require('sinon');
3+
var sinon = require('sinon');
44
var helper = require('./helper');
55
var ConstructorioID = require('../src/constructorio-id.js');
66

@@ -28,6 +28,13 @@ describe('ConstructorioID', function () {
2828
expect(adventuretime.jake).to.be.true;
2929
});
3030

31+
it('should not throw error when localStorage is not available', function () {
32+
// Delete window so accessing local storage will throw
33+
delete global.window;
34+
var session = new ConstructorioID();
35+
expect(session.get_local_object('adventuretime')).to.not.throw;
36+
});
37+
3138
it('should not return a local string', function () {
3239
window.localStorage.setItem('adventuretime', 'Come on grab your friends');
3340
var session = new ConstructorioID();
@@ -43,6 +50,13 @@ describe('ConstructorioID', function () {
4350
expect(window.localStorage.adventuretime).to.be.a.string;
4451
expect(JSON.parse(window.localStorage.adventuretime)).to.deep.equal({ marceline: true });
4552
});
53+
54+
it('should not throw error when localStorage is not available', function () {
55+
// Delete window so accessing local storage will throw
56+
delete global.window;
57+
var session = new ConstructorioID();
58+
expect(session.set_local_object('adventuretime', 'test')).to.not.throw;
59+
});
4660
});
4761

4862
describe('generate_session_id', function () {

src/constructorio-id.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
(function () {
22
// Object.assign polyfill
33
// - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
4-
Object.assign || Object.defineProperty(Object, 'assign', {enumerable: !1, configurable: !0, writable: !0, value: function (e) {'use strict'; if (void 0 === e || e === null) throw new TypeError('Cannot convert first argument to object'); for (var r = Object(e), t = 1; t < arguments.length; t++) {var n = arguments[t]; if (void 0 !== n && n !== null) {n = Object(n); for (var o = Object.keys(Object(n)), a = 0, c = o.length; c > a; a++) {var i = o[a], b = Object.getOwnPropertyDescriptor(n, i); void 0 !== b && b.enumerable && (r[i] = n[i]);}}} return r;}}); // eslint-disable-line
4+
Object.assign || Object.defineProperty(Object, 'assign', { enumerable: !1, configurable: !0, writable: !0, value: function(e) { 'use strict'; if (void 0 === e || e === null) throw new TypeError('Cannot convert first argument to object'); for (var r = Object(e), t = 1; t < arguments.length; t++) { var n = arguments[t]; if (void 0 !== n && n !== null) { n = Object(n); for (var o = Object.keys(Object(n)), a = 0, c = o.length; c > a; a++) { var i = o[a], b = Object.getOwnPropertyDescriptor(n, i); void 0 !== b && b.enumerable && (r[i] = n[i]); } } } return r; } }); // eslint-disable-line
5+
6+
function storageAvailable(type) {
7+
let storage;
8+
try {
9+
storage = window[type];
10+
const x = '__storage_test__';
11+
storage.setItem(x, x);
12+
storage.removeItem(x);
13+
return true;
14+
} catch (e) {
15+
return (
16+
e instanceof DOMException &&
17+
e.name === 'QuotaExceededError' &&
18+
// acknowledge QuotaExceededError only if there's something already stored
19+
storage &&
20+
storage.length !== 0
21+
);
22+
}
23+
}
524

625
var ConstructorioID = function (options) {
726
var defaults = {
@@ -158,8 +177,8 @@
158177

159178
ConstructorioID.prototype.get_local_object = function (key) {
160179
var data;
161-
var localStorage = window && window.localStorage;
162-
if (localStorage && typeof key === 'string') {
180+
var localStorage = storageAvailable('localStorage') && window && window.localStorage;
181+
if (localStorage && typeof key === 'string') {
163182
try {
164183
data = JSON.parse(localStorage.getItem(key));
165184
} catch (e) {
@@ -170,7 +189,7 @@
170189
};
171190

172191
ConstructorioID.prototype.set_local_object = function (key, data) {
173-
var localStorage = window && window.localStorage;
192+
var localStorage = storageAvailable('localStorage') && window && window.localStorage;
174193

175194
if (localStorage && typeof key === 'string') {
176195
if (typeof data === 'object') {
@@ -192,7 +211,7 @@
192211
};
193212

194213
ConstructorioID.prototype.delete_local_object = function (key) {
195-
var localStorage = window && window.localStorage;
214+
var localStorage = storageAvailable('localStorage') && window && window.localStorage;
196215

197216
if (localStorage && typeof key === 'string') {
198217
try {

0 commit comments

Comments
 (0)