Skip to content

Commit

Permalink
rewrite quota and move measure out of it
Browse files Browse the repository at this point in the history
  • Loading branch information
nbubna committed Apr 19, 2013
1 parent 1c5ed91 commit 6ff745f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 43 deletions.
61 changes: 61 additions & 0 deletions src/store.measure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2013 ESHA Research
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* store.remainingSpace();// returns remainingSpace value (if browser supports it)
* store.charsUsed();// returns length of all data when stringified
* store.charsLeft([true]);// tests how many more chars we can fit (crash threat!)
* store.charsTotal([true]);// charsUsed + charsLeft, duh.
*
* TODO: byte/string conversions
*
* Status: ALPHA - changing API *and* crash threats :)
*/
;(function(store, _) {

_.fn('remainingSpace', function() {
return this._area.remainingSpace;
});
_.fn('charsUsed', function() {
return _.stringify(this.getAll()).length - 2;
});
_.fn('charsLeft', function(test) {
if (this.isFake()){ return; }
if (arguments.length === 0) {
test = window.confirm('Calling store.charsLeft() may crash some browsers!');
}
if (test) {
var s = 's ', add = s;
// grow add for speed
while (put(store._area, s)) {
s += add;
if (add.length < 50000) {
add = s;
}
}
// shrink add for accuracy
while (add.length > 2) {
s = s.substring(0, s.length - (add.length/2));
while (put(store._area, s)) {
s += add;
}
add = add.substring(add.length/2);
}
_.remove(store._area, "__test__");
return s.length + 8;
}
});
_.fn('charsTotal', function(test) {
return store.charsUsed() + store.charsLeft(test);
});

function put(area, s) {
try {
area.setItem("__test__", s);
return true;
} catch (e) {}
}

})(window.store, window.store._);
68 changes: 25 additions & 43 deletions src/store.quota.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,42 @@
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Allows user to register handlers for quota errors, if a handler returns true
* other handlers are not called and the error is suppressed. Also provides methods
* to test available space, but *these are expensive and crash-prone*!
* Bind handlers to quota errors:
* store.quota(function(e, area, key, str) {
* console.log(e, area, key, str);
* });
* If a handler returns true other handlers are not called and
* the error is suppressed.
*
* Status: ALPHA - possibly useful, with some dangerous features (store.remaining())
* Think quota errors will never happen to you? Think again:
* http://spin.atomicobject.com/2013/01/23/ios-private-browsing-localstorage/
* (this affects sessionStorage too)
*
* Status: ALPHA - API could use unbind feature
*/
;(function(store, _) {
var set = _.set,
list = [];
store.full = function(fn){ list.push(fn); },
store.full.handlers = list;
_.set = function() {

store.quota = function(fn) {
store.quota.fns.push(fn);
};
store.quota.fns = [];

var _set = _.set;
_.set = function(area, key, str) {
try {
set.apply(this, arguments);
_set.apply(this, arguments);
} catch (e) {
if (e.name === 'QUOTA_EXCEEDED_ERR' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
for (var i=0,m=list.length; i<m; i++) {
if (true === list[i].apply(this, arguments)) {
if (e.name === 'QUOTA_EXCEEDED_ERR' ||
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
var fns = store.quota.fns;
for (var i=0,m=fns.length; i<m; i++) {
if (true === fns[i].call(this, e, area, key, str)) {
return;
}
}
}
throw e;
}
};
var test = function(s) {
try {
set(localStorage, "__test__", s);
return s;
} catch (e) {}
};
store.existing = function(){ return _.stringify(store()).length; };
store.remaining = function() {
if (store.isFake()){ return; }
if (store._area.remainingSpace){ return store._area.remainingSpace; }
var s = 's ', add = s;
// grow add for speed
while (test(s)) {
s += add;
if (add.length < 50000) {
add = s;
}
}
// shrink add for accuracy
while (add.length > 2) {
s = s.substring(0, s.length - (add.length/2));
while (test(s)) {
s += add;
}
add = add.substring(add.length/2);
}
_.remove(localStorage, "__test__");
return s.length + 8;
};
store.quota = function(){ return store.existing() + store.remaining(); };

})(window.store, window.store._);

0 comments on commit 6ff745f

Please sign in to comment.