node-localstorage
Advanced tools
Comparing version 0.5.0 to 0.5.1
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var JSONStorage, LocalStorage, QUOTA_EXCEEDED_ERR, fs, path, _emptyDirectory, _rm, | ||
var JSONStorage, LocalStorage, QUOTA_EXCEEDED_ERR, StorageEvent, events, fs, path, _emptyDirectory, _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; }, | ||
@@ -11,2 +11,4 @@ __hasProp = {}.hasOwnProperty; | ||
events = require('events'); | ||
_emptyDirectory = function(target) { | ||
@@ -51,3 +53,20 @@ var p, _i, _len, _ref, _results; | ||
LocalStorage = (function() { | ||
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'; | ||
} | ||
return StorageEvent; | ||
})(); | ||
LocalStorage = (function(_super) { | ||
var MetaKey, createMap; | ||
__extends(LocalStorage, _super); | ||
function LocalStorage(_at_location, _at_quota) { | ||
@@ -62,2 +81,4 @@ this.location = _at_location; | ||
this.keys = []; | ||
this.metaKeyMap = createMap(); | ||
this.eventUrl = "pid:" + process.pid; | ||
this._init(); | ||
@@ -67,4 +88,24 @@ this.QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; | ||
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 k, value, _i, _len, _ref, _results; | ||
var index, k, stat, _MetaKey, _decodedKey, _i, _keys, _len; | ||
if (fs.existsSync(this.location)) { | ||
@@ -75,31 +116,40 @@ if (!fs.statSync(this.location).isDirectory()) { | ||
} | ||
this.bytesInUse = 0; | ||
this.length = 0; | ||
if (!fs.existsSync(this.location)) { | ||
fs.mkdirSync(this.location); | ||
return; | ||
} | ||
this.keys = fs.readdirSync(this.location).map(decodeURIComponent); | ||
this.length = this.keys.length; | ||
this.bytesInUse = 0; | ||
_ref = this.keys; | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
k = _ref[_i]; | ||
value = this.getStat(k); | ||
if ((value != null ? value.size : void 0) != null) { | ||
_results.push(this.bytesInUse += value.size); | ||
} else { | ||
_results.push(void 0); | ||
_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); | ||
_MetaKey = new MetaKey(k, index); | ||
this.metaKeyMap[_decodedKey] = _MetaKey; | ||
stat = this.getStat(k); | ||
if ((stat != null ? stat.size : void 0) != null) { | ||
_MetaKey.size = stat.size; | ||
this.bytesInUse += stat.size; | ||
} | ||
} | ||
return _results; | ||
return this.length = _keys.length; | ||
}; | ||
LocalStorage.prototype.setItem = function(key, value) { | ||
var existsBeforeSet, filename, oldLength, valueString, valueStringLength; | ||
var encodedKey, evnt, existsBeforeSet, filename, hasListeners, metaKey, oldLength, oldValue, valueString, valueStringLength; | ||
hasListeners = events.EventEmitter.listenerCount(this, 'storage'); | ||
oldValue = null; | ||
if (hasListeners) { | ||
oldValue = this.getItem(key); | ||
} | ||
key = key.toString(); | ||
filename = path.join(this.location, encodeURIComponent(key)); | ||
existsBeforeSet = fs.existsSync(filename); | ||
encodedKey = encodeURIComponent(key); | ||
filename = path.join(this.location, encodedKey); | ||
valueString = value.toString(); | ||
valueStringLength = valueString.length; | ||
metaKey = this.metaKeyMap[key]; | ||
existsBeforeSet = !!metaKey; | ||
if (existsBeforeSet) { | ||
oldLength = this.getStat(key).size; | ||
oldLength = metaKey.size; | ||
} else { | ||
@@ -113,13 +163,20 @@ oldLength = 0; | ||
if (!existsBeforeSet) { | ||
this.keys.push(key); | ||
this.length = this.keys.length; | ||
return this.bytesInUse += valueStringLength; | ||
metaKey = new MetaKey(encodedKey, (this.keys.push(key)) - 1); | ||
metaKey.size = valueStringLength; | ||
this.metaKeyMap[key] = metaKey; | ||
this.length += 1; | ||
this.bytesInUse += valueStringLength; | ||
} | ||
if (hasListeners) { | ||
evnt = new StorageEvent(key, oldValue, value, this.eventUrl); | ||
return this.emit('storage', evnt); | ||
} | ||
}; | ||
LocalStorage.prototype.getItem = function(key) { | ||
var filename; | ||
var filename, metaKey; | ||
key = key.toString(); | ||
filename = path.join(this.location, encodeURIComponent(key)); | ||
if (fs.existsSync(filename)) { | ||
metaKey = this.metaKeyMap[key]; | ||
if (!!metaKey) { | ||
filename = path.join(this.location, metaKey.key); | ||
return fs.readFileSync(filename, 'utf8'); | ||
@@ -136,3 +193,3 @@ } else { | ||
if (fs.existsSync(filename)) { | ||
return fs.statSync(filename, 'utf8'); | ||
return fs.statSync(filename); | ||
} else { | ||
@@ -144,9 +201,22 @@ return null; | ||
LocalStorage.prototype.removeItem = function(key) { | ||
var filename; | ||
var evnt, filename, hasListeners, metaKey, oldValue; | ||
key = key.toString(); | ||
filename = path.join(this.location, encodeURIComponent(key)); | ||
if (fs.existsSync(filename)) { | ||
metaKey = this.metaKeyMap[key]; | ||
if (!!metaKey) { | ||
hasListeners = events.EventEmitter.listenerCount(this, 'storage'); | ||
oldValue = null; | ||
if (hasListeners) { | ||
oldValue = this.getItem(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); | ||
_rm(filename); | ||
if (hasListeners) { | ||
evnt = new StorageEvent(key, oldValue, null, this.eventUrl); | ||
return this.emit('storage', evnt); | ||
} | ||
} | ||
return this._init(); | ||
}; | ||
@@ -159,6 +229,12 @@ | ||
LocalStorage.prototype.clear = function() { | ||
var evnt; | ||
_emptyDirectory(this.location); | ||
this.metaKeyMap = createMap(); | ||
this.keys = []; | ||
this.length = 0; | ||
return this.bytesInUse = 0; | ||
this.bytesInUse = 0; | ||
if (events.EventEmitter.listenerCount(this, 'storage')) { | ||
evnt = new StorageEvent(null, null, null, this.eventUrl); | ||
return this.emit('storage', evnt); | ||
} | ||
}; | ||
@@ -172,2 +248,3 @@ | ||
_rm(this.location); | ||
this.metaKeyMap = {}; | ||
this.keys = []; | ||
@@ -180,3 +257,3 @@ this.length = 0; | ||
})(); | ||
})(events.EventEmitter); | ||
@@ -183,0 +260,0 @@ JSONStorage = (function(_super) { |
{ | ||
"name": "node-localstorage", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"main": "./LocalStorage", | ||
@@ -20,4 +20,4 @@ "description": "A drop-in substitute for the browser native localStorage API that runs on node.js.", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "http://github.com/lmaccherone/node-localstorage.git" | ||
"type": "git", | ||
"url": "http://github.com/lmaccherone/node-localstorage.git" | ||
}, | ||
@@ -27,14 +27,16 @@ "preferGlobal": false, | ||
"coffee-script": "^1.9.0", | ||
"runsync": "~0.1.6", | ||
"nodeunit": "~0.9.0" | ||
"gulp-run": "^1.6.8", | ||
"nodeunit": "~0.9.0", | ||
"runsync": "~0.1.6" | ||
}, | ||
"scripts": { | ||
"test": "cake test" | ||
"test": "cake test", | ||
"docompile": "cake compile" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
} | ||
] | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
} | ||
] | ||
} |
@@ -20,6 +20,12 @@ [![build status](https://secure.travis-ci.org/lmaccherone/node-localstorage.png)](http://travis-ci.org/lmaccherone/node-localstorage) | ||
* Supports the setting of a quota (default 5MB) | ||
* Events. This doesn't exactly follow the spec which states that events are NOT supposed to be emitted to the browser window | ||
that took the action that triggered the event in the first place. They are are only to be emitted to listners in | ||
other browser windows. Early browser implementations actually did it this way and we don't really have the equivalent | ||
of a browser window in node.js, so we choose to implement them in the current process. We did, however, include the pid | ||
information in place of the window uri, so if we later wanted to say think of other node.js processes accessing | ||
the same file system, we could actually implement it correctly. That would involve file watchers though and was more | ||
than we wanted to implement for now. Maybe later. | ||
### Unsupported ### | ||
* Events | ||
* Associative array syntax `localStorage['myKey'] = 'myValue'` | ||
@@ -63,2 +69,3 @@ | ||
* 0.5.1 - 2015-06-01 - Added support for events | ||
* 0.5.0 - 2015-02-02 - Added JSONStorage class which allows you set and get native JSON | ||
@@ -65,0 +72,0 @@ * 0.4.1 - 2015-02-02 - More robust publishing/tagging (like Lumenize) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30539
235
100
4