node-localstorage
Advanced tools
Comparing version
@@ -1,6 +0,7 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var JSONStorage, KEY_FOR_EMPTY_STRING, LocalStorage, QUOTA_EXCEEDED_ERR, StorageEvent, events, fs, path, _emptyDirectory, _escapeKey, _rm, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
__hasProp = {}.hasOwnProperty; | ||
var JSONStorage, KEY_FOR_EMPTY_STRING, LocalStorage, MetaKey, QUOTA_EXCEEDED_ERR, StorageEvent, _emptyDirectory, _escapeKey, _rm, createMap, events, fs, path, | ||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
hasProp = {}.hasOwnProperty, | ||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
@@ -16,10 +17,10 @@ path = require('path'); | ||
_emptyDirectory = function(target) { | ||
var p, _i, _len, _ref, _results; | ||
_ref = fs.readdirSync(target); | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
p = _ref[_i]; | ||
_results.push(_rm(path.join(target, p))); | ||
var i, len, p, ref, results; | ||
ref = fs.readdirSync(target); | ||
results = []; | ||
for (i = 0, len = ref.length; i < len; i++) { | ||
p = ref[i]; | ||
results.push(_rm(path.join(target, p))); | ||
} | ||
return _results; | ||
return results; | ||
}; | ||
@@ -46,7 +47,7 @@ | ||
QUOTA_EXCEEDED_ERR = (function(_super) { | ||
__extends(QUOTA_EXCEEDED_ERR, _super); | ||
QUOTA_EXCEEDED_ERR = (function(superClass) { | ||
extend(QUOTA_EXCEEDED_ERR, superClass); | ||
function QUOTA_EXCEEDED_ERR(_at_message) { | ||
this.message = _at_message != null ? _at_message : 'Unknown error.'; | ||
function QUOTA_EXCEEDED_ERR(message) { | ||
this.message = message != null ? message : 'Unknown error.'; | ||
if (Error.captureStackTrace != null) { | ||
@@ -67,8 +68,8 @@ Error.captureStackTrace(this, this.constructor); | ||
StorageEvent = (function() { | ||
function StorageEvent(_at_key, _at_oldValue, _at_newValue, _at_url, _at_storageArea) { | ||
this.key = _at_key; | ||
this.oldValue = _at_oldValue; | ||
this.newValue = _at_newValue; | ||
this.url = _at_url; | ||
this.storageArea = _at_storageArea != null ? _at_storageArea : 'localStorage'; | ||
function StorageEvent(key1, oldValue1, newValue1, url, storageArea) { | ||
this.key = key1; | ||
this.oldValue = oldValue1; | ||
this.newValue = newValue1; | ||
this.url = url; | ||
this.storageArea = storageArea != null ? storageArea : 'localStorage'; | ||
} | ||
@@ -80,67 +81,90 @@ | ||
LocalStorage = (function(_super) { | ||
var MetaKey, createMap; | ||
MetaKey = (function() { | ||
function MetaKey(key1, index1) { | ||
this.key = key1; | ||
this.index = index1; | ||
if (!(this instanceof MetaKey)) { | ||
return new MetaKey(this.key, this.index); | ||
} | ||
} | ||
__extends(LocalStorage, _super); | ||
return MetaKey; | ||
function LocalStorage(_at_location, _at_quota) { | ||
this.location = _at_location; | ||
this.quota = _at_quota != null ? _at_quota : 5 * 1024 * 1024; | ||
})(); | ||
createMap = function() { | ||
var Map; | ||
Map = function() {}; | ||
Map.prototype = Object.create(null); | ||
return new Map(); | ||
}; | ||
LocalStorage = (function(superClass) { | ||
extend(LocalStorage, superClass); | ||
function LocalStorage(_location, quota) { | ||
var handler, reserved; | ||
this._location = _location; | ||
this.quota = quota != null ? quota : 5 * 1024 * 1024; | ||
if (!(this instanceof LocalStorage)) { | ||
return new LocalStorage(this.location, this.quota); | ||
return new LocalStorage(this._location, this.quota); | ||
} | ||
this.length = 0; | ||
this.bytesInUse = 0; | ||
this.keys = []; | ||
this.metaKeyMap = createMap(); | ||
this.eventUrl = "pid:" + process.pid; | ||
this._bytesInUse = 0; | ||
this._keys = []; | ||
this._metaKeyMap = createMap(); | ||
this._eventUrl = "pid:" + process.pid; | ||
this._init(); | ||
this.QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; | ||
this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; | ||
if (typeof Proxy !== "undefined" && Proxy !== null) { | ||
reserved = ["length", "setItem", "getItem", "removeItem", "key", "clear", "_deleteLocation", "_location", "_metaKeyMap", "_keys", "_bytesInUse", "_init", "_eventUrl", "_QUOTA_EXCEEDED_ERR"]; | ||
handler = { | ||
set: (function(_this) { | ||
return function(receiver, key, value) { | ||
if (indexOf.call(reserved, key) >= 0) { | ||
return _this[key]; | ||
} else { | ||
return _this.setItem(key, value); | ||
} | ||
}; | ||
})(this), | ||
get: (function(_this) { | ||
return function(receiver, key) { | ||
if (indexOf.call(reserved, key) >= 0) { | ||
return _this[key]; | ||
} else { | ||
return _this.getItem(key); | ||
} | ||
}; | ||
})(this) | ||
}; | ||
return Proxy.create(handler, this); | ||
} | ||
} | ||
MetaKey = (function() { | ||
function MetaKey(_at_key, _at_index) { | ||
this.key = _at_key; | ||
this.index = _at_index; | ||
if (!(this instanceof MetaKey)) { | ||
return new MetaKey(this.key, this.index); | ||
} | ||
} | ||
return MetaKey; | ||
})(); | ||
createMap = function() { | ||
var Map; | ||
Map = function() {}; | ||
Map.prototype = Object.create(null); | ||
return new Map(); | ||
}; | ||
LocalStorage.prototype._init = function() { | ||
var index, k, stat, _MetaKey, _decodedKey, _i, _keys, _len; | ||
var _MetaKey, _decodedKey, _keys, error, i, index, k, len, stat; | ||
try { | ||
stat = fs.statSync(this.location); | ||
stat = fs.statSync(this._location); | ||
if ((stat != null) && !stat.isDirectory()) { | ||
throw new Error("A file exists at the location '" + this.location + "' when trying to create/open localStorage"); | ||
throw new Error("A file exists at the location '" + this._location + "' when trying to create/open localStorage"); | ||
} | ||
this.bytesInUse = 0; | ||
this._bytesInUse = 0; | ||
this.length = 0; | ||
_keys = fs.readdirSync(this.location); | ||
for (index = _i = 0, _len = _keys.length; _i < _len; index = ++_i) { | ||
_keys = fs.readdirSync(this._location); | ||
for (index = i = 0, len = _keys.length; i < len; index = ++i) { | ||
k = _keys[index]; | ||
_decodedKey = decodeURIComponent(k); | ||
this.keys.push(_decodedKey); | ||
this._keys.push(_decodedKey); | ||
_MetaKey = new MetaKey(k, index); | ||
this.metaKeyMap[_decodedKey] = _MetaKey; | ||
stat = this.getStat(k); | ||
this._metaKeyMap[_decodedKey] = _MetaKey; | ||
stat = this._getStat(k); | ||
if ((stat != null ? stat.size : void 0) != null) { | ||
_MetaKey.size = stat.size; | ||
this.bytesInUse += stat.size; | ||
this._bytesInUse += stat.size; | ||
} | ||
} | ||
this.length = _keys.length; | ||
} catch (_error) { | ||
fs.mkdirSync(this.location); | ||
} catch (error) { | ||
fs.mkdirSync(this._location); | ||
} | ||
@@ -158,6 +182,6 @@ }; | ||
encodedKey = encodeURIComponent(key); | ||
filename = path.join(this.location, encodedKey); | ||
filename = path.join(this._location, encodedKey); | ||
valueString = value.toString(); | ||
valueStringLength = valueString.length; | ||
metaKey = this.metaKeyMap[key]; | ||
metaKey = this._metaKeyMap[key]; | ||
existsBeforeSet = !!metaKey; | ||
@@ -169,3 +193,3 @@ if (existsBeforeSet) { | ||
} | ||
if (this.bytesInUse - oldLength + valueStringLength > this.quota) { | ||
if (this._bytesInUse - oldLength + valueStringLength > this.quota) { | ||
throw new QUOTA_EXCEEDED_ERR(); | ||
@@ -175,10 +199,10 @@ } | ||
if (!existsBeforeSet) { | ||
metaKey = new MetaKey(encodedKey, (this.keys.push(key)) - 1); | ||
metaKey = new MetaKey(encodedKey, (this._keys.push(key)) - 1); | ||
metaKey.size = valueStringLength; | ||
this.metaKeyMap[key] = metaKey; | ||
this._metaKeyMap[key] = metaKey; | ||
this.length += 1; | ||
this.bytesInUse += valueStringLength; | ||
this._bytesInUse += valueStringLength; | ||
} | ||
if (hasListeners) { | ||
evnt = new StorageEvent(key, oldValue, value, this.eventUrl); | ||
evnt = new StorageEvent(key, oldValue, value, this._eventUrl); | ||
return this.emit('storage', evnt); | ||
@@ -191,5 +215,5 @@ } | ||
key = _escapeKey(key); | ||
metaKey = this.metaKeyMap[key]; | ||
metaKey = this._metaKeyMap[key]; | ||
if (!!metaKey) { | ||
filename = path.join(this.location, metaKey.key); | ||
filename = path.join(this._location, metaKey.key); | ||
return fs.readFileSync(filename, 'utf8'); | ||
@@ -201,9 +225,9 @@ } else { | ||
LocalStorage.prototype.getStat = function(key) { | ||
var filename; | ||
LocalStorage.prototype._getStat = function(key) { | ||
var error, filename; | ||
key = _escapeKey(key); | ||
filename = path.join(this.location, encodeURIComponent(key)); | ||
filename = path.join(this._location, encodeURIComponent(key)); | ||
try { | ||
return fs.statSync(filename); | ||
} catch (_error) { | ||
} catch (error) { | ||
return null; | ||
@@ -214,5 +238,5 @@ } | ||
LocalStorage.prototype.removeItem = function(key) { | ||
var evnt, filename, hasListeners, k, meta, metaKey, oldValue, v, _ref; | ||
var evnt, filename, hasListeners, k, meta, metaKey, oldValue, ref, v; | ||
key = _escapeKey(key); | ||
metaKey = this.metaKeyMap[key]; | ||
metaKey = this._metaKeyMap[key]; | ||
if (!!metaKey) { | ||
@@ -224,11 +248,11 @@ hasListeners = events.EventEmitter.listenerCount(this, 'storage'); | ||
} | ||
delete this.metaKeyMap[key]; | ||
delete this._metaKeyMap[key]; | ||
this.length -= 1; | ||
this.bytesInUse -= metaKey.size; | ||
filename = path.join(this.location, metaKey.key); | ||
this.keys.splice(metaKey.index, 1); | ||
_ref = this.metaKeyMap; | ||
for (k in _ref) { | ||
v = _ref[k]; | ||
meta = this.metaKeyMap[k]; | ||
this._bytesInUse -= metaKey.size; | ||
filename = path.join(this._location, metaKey.key); | ||
this._keys.splice(metaKey.index, 1); | ||
ref = this._metaKeyMap; | ||
for (k in ref) { | ||
v = ref[k]; | ||
meta = this._metaKeyMap[k]; | ||
if (meta.index > metaKey.index) { | ||
@@ -240,3 +264,3 @@ meta.index -= 1; | ||
if (hasListeners) { | ||
evnt = new StorageEvent(key, oldValue, null, this.eventUrl); | ||
evnt = new StorageEvent(key, oldValue, null, this._eventUrl); | ||
return this.emit('storage', evnt); | ||
@@ -248,3 +272,3 @@ } | ||
LocalStorage.prototype.key = function(n) { | ||
return this.keys[n]; | ||
return this._keys[n]; | ||
}; | ||
@@ -254,9 +278,9 @@ | ||
var evnt; | ||
_emptyDirectory(this.location); | ||
this.metaKeyMap = createMap(); | ||
this.keys = []; | ||
_emptyDirectory(this._location); | ||
this._metaKeyMap = createMap(); | ||
this._keys = []; | ||
this.length = 0; | ||
this.bytesInUse = 0; | ||
this._bytesInUse = 0; | ||
if (events.EventEmitter.listenerCount(this, 'storage')) { | ||
evnt = new StorageEvent(null, null, null, this.eventUrl); | ||
evnt = new StorageEvent(null, null, null, this._eventUrl); | ||
return this.emit('storage', evnt); | ||
@@ -266,12 +290,12 @@ } | ||
LocalStorage.prototype.getBytesInUse = function() { | ||
return this.bytesInUse; | ||
LocalStorage.prototype._getBytesInUse = function() { | ||
return this._bytesInUse; | ||
}; | ||
LocalStorage.prototype._deleteLocation = function() { | ||
_rm(this.location); | ||
this.metaKeyMap = {}; | ||
this.keys = []; | ||
_rm(this._location); | ||
this._metaKeyMap = {}; | ||
this._keys = []; | ||
this.length = 0; | ||
return this.bytesInUse = 0; | ||
return this._bytesInUse = 0; | ||
}; | ||
@@ -283,4 +307,4 @@ | ||
JSONStorage = (function(_super) { | ||
__extends(JSONStorage, _super); | ||
JSONStorage = (function(superClass) { | ||
extend(JSONStorage, superClass); | ||
@@ -287,0 +311,0 @@ function JSONStorage() { |
{ | ||
"name": "node-localstorage", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "./LocalStorage", | ||
@@ -17,3 +17,3 @@ "description": "A drop-in substitute for the browser native localStorage API that runs on node.js.", | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=0.12" | ||
}, | ||
@@ -26,3 +26,3 @@ "repository": { | ||
"devDependencies": { | ||
"coffee-script": "^1.9.0", | ||
"coffee-script": "^1.10.0", | ||
"lodash": "^3.10.1", | ||
@@ -41,3 +41,4 @@ "nodeunit": "~0.9.0" | ||
} | ||
] | ||
], | ||
"dependencies": {} | ||
} |
@@ -9,3 +9,3 @@ [](http://travis-ci.org/lmaccherone/node-localstorage) | ||
### Working ### | ||
### Fully implements the localStorage specfication including: ### | ||
@@ -29,7 +29,9 @@ * All methods in the [localStorage spec](http://www.w3.org/TR/webstorage/#storage) | ||
than we wanted to implement for now. Maybe later. | ||
* (experimental) Associative array `localStorage['myKey'] = 'myValue'` and dot property `localStorage.myKey = 'myValue'` | ||
syntax. This only works if you use the --harmony-proxies flag. It senses the existence of the Proxy object in the root | ||
scope. If you have added your own Proxy object, then you could have a problem. Another potential risk is around | ||
the "private", underscore preceded methods and properties. I had to reserve those in addition to the standard ones, | ||
so you won't be able to use keys that overlap with those underscore preceded properties and methods in a harmony-proxies | ||
environment. | ||
### Unsupported ### | ||
* Associative array syntax `localStorage['myKey'] = 'myValue'` | ||
## Credits ## | ||
@@ -71,2 +73,4 @@ | ||
* 1.1.0 - 2016-01-03 - **Backward breaking** if you used any of the non-standard methods. They are now all preceded with | ||
an underscore. Big upgrade for this version is experimental support for associative array and dot-property syntax. | ||
* 1.0.0 - 2016-01-03 - Fixed bug with empty string key (thanks, tinybike) | ||
@@ -73,0 +77,0 @@ * 0.6.0 - 2015-09-11 - Removed references to deprecated fs.existsSync() (thanks, josephbosire) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60170
3.55%277
9.92%108
3.85%