function JSONStorage() { return JSONStorage.__super__.constructor.apply(this, arguments); }
n/a
function LocalStorage(_location, quota) { this._location = _location; this.quota = quota != null ? quota : 5 * 1024 * 1024; if (!(this instanceof LocalStorage)) { return new LocalStorage(this._location, this.quota); } this._location = path.resolve(this._location); if (instanceMap[this._location] != null) { return instanceMap[this._location]; } this.length = 0; this._bytesInUse = 0; this._keys = []; this._metaKeyMap = createMap(); this._eventUrl = "pid:" + process.pid; this._init(); this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; instanceMap[this._location] = this; return instanceMap[this._location]; }
n/a
function QUOTA_EXCEEDED_ERR(message) { this.message = message != null ? message : 'Unknown error.'; if (Error.captureStackTrace != null) { Error.captureStackTrace(this, this.constructor); } this.name = this.constructor.name; }
n/a
function JSONStorage() { return JSONStorage.__super__.constructor.apply(this, arguments); }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
init = function () { this.domain = null; if (EventEmitter.usingDomains) { // if there is an active domain, then attach to it. domain = domain || require('domain'); if (domain.active && !(this instanceof domain.Domain)) { this.domain = domain.active; } } if (!this._events || this._events === Object.getPrototypeOf(this)._events) { this._events = new EventHandlers(); this._eventsCount = 0; } this._maxListeners = this._maxListeners || undefined; }
n/a
listenerCount = function (emitter, type) { if (typeof emitter.listenerCount === 'function') { return emitter.listenerCount(type); } else { return listenerCount.call(emitter, type); } }
...
} catch (error) {
fs.mkdirSync(this._location);
}
};
LocalStorage.prototype.setItem = function(key, value) {
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 = _escapeKey(key);
encodedKey = encodeURIComponent(key);
filename = path.join(this._location, encodedKey);
...
function JSONStorage() { return JSONStorage.__super__.constructor.apply(this, arguments); }
n/a
getItem = function (key) { return JSON.parse(JSONStorage.__super__.getItem.call(this, key)); }
...
### CoffeeScript ###
unless localStorage?
{LocalStorage} = require('../') # require('node-localstorage') for you
localStorage = new LocalStorage('./scratch')
localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue
localStorage._deleteLocation() # cleans up ./scratch created during doctest
### JavaScript ###
```JavaScript
...
setItem = function (key, value) { var newValue; newValue = JSON.stringify(value); return JSONStorage.__super__.setItem.call(this, key, newValue); }
...
### CoffeeScript ###
unless localStorage?
{LocalStorage} = require('../') # require('node-localstorage') for you
localStorage = new LocalStorage('./scratch')
localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue
localStorage._deleteLocation() # cleans up ./scratch created during doctest
### JavaScript ###
...
function LocalStorage(_location, quota) { this._location = _location; this.quota = quota != null ? quota : 5 * 1024 * 1024; if (!(this instanceof LocalStorage)) { return new LocalStorage(this._location, this.quota); } this._location = path.resolve(this._location); if (instanceMap[this._location] != null) { return instanceMap[this._location]; } this.length = 0; this._bytesInUse = 0; this._keys = []; this._metaKeyMap = createMap(); this._eventUrl = "pid:" + process.pid; this._init(); this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; instanceMap[this._location] = this; return instanceMap[this._location]; }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
init = function () { this.domain = null; if (EventEmitter.usingDomains) { // if there is an active domain, then attach to it. domain = domain || require('domain'); if (domain.active && !(this instanceof domain.Domain)) { this.domain = domain.active; } } if (!this._events || this._events === Object.getPrototypeOf(this)._events) { this._events = new EventHandlers(); this._eventsCount = 0; } this._maxListeners = this._maxListeners || undefined; }
n/a
listenerCount = function (emitter, type) { if (typeof emitter.listenerCount === 'function') { return emitter.listenerCount(type); } else { return listenerCount.call(emitter, type); } }
...
} catch (error) {
fs.mkdirSync(this._location);
}
};
LocalStorage.prototype.setItem = function(key, value) {
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 = _escapeKey(key);
encodedKey = encodeURIComponent(key);
filename = path.join(this._location, encodedKey);
...
_deleteLocation = function () { delete instanceMap[this._location]; _rm(this._location); this._metaKeyMap = {}; this._keys = []; this.length = 0; return this._bytesInUse = 0; }
...
{LocalStorage} = require('../') # require('node-localstorage') for you
localStorage = new LocalStorage('./scratch')
localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue
localStorage._deleteLocation() # cleans up ./scratch created during doctest
### JavaScript ###
```JavaScript
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
...
_getBytesInUse = function () { return this._bytesInUse; }
n/a
_getStat = function (key) { var error, filename; key = _escapeKey(key); filename = path.join(this._location, encodeURIComponent(key)); try { return fs.statSync(filename); } catch (error) { return null; } }
...
_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;
}
}
this.length = _keys.length;
} catch (error) {
...
_init = function () { var _MetaKey, _decodedKey, _keys, error, i, index, k, len, stat; try { 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"); } this._bytesInUse = 0; this.length = 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; } } this.length = _keys.length; } catch (error) { fs.mkdirSync(this._location); } }
...
return instanceMap[this._location];
}
this.length = 0;
this._bytesInUse = 0;
this._keys = [];
this._metaKeyMap = createMap();
this._eventUrl = "pid:" + process.pid;
this._init();
this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR;
instanceMap[this._location] = this;
return instanceMap[this._location];
}
LocalStorage.prototype._init = function() {
var _MetaKey, _decodedKey, _keys, error, i, index, k, len, stat;
...
clear = function () { var evnt; _emptyDirectory(this._location); this._metaKeyMap = createMap(); this._keys = []; this.length = 0; this._bytesInUse = 0; if (events.EventEmitter.listenerCount(this, 'storage')) { evnt = new StorageEvent(null, null, null, this._eventUrl); return this.emit('storage', evnt); } }
n/a
function LocalStorage(_location, quota) { this._location = _location; this.quota = quota != null ? quota : 5 * 1024 * 1024; if (!(this instanceof LocalStorage)) { return new LocalStorage(this._location, this.quota); } this._location = path.resolve(this._location); if (instanceMap[this._location] != null) { return instanceMap[this._location]; } this.length = 0; this._bytesInUse = 0; this._keys = []; this._metaKeyMap = createMap(); this._eventUrl = "pid:" + process.pid; this._init(); this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; instanceMap[this._location] = this; return instanceMap[this._location]; }
n/a
getItem = function (key) { var filename, metaKey; key = _escapeKey(key); metaKey = this._metaKeyMap[key]; if (!!metaKey) { filename = path.join(this._location, metaKey.key); return fs.readFileSync(filename, 'utf8'); } else { return null; } }
...
### CoffeeScript ###
unless localStorage?
{LocalStorage} = require('../') # require('node-localstorage') for you
localStorage = new LocalStorage('./scratch')
localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue
localStorage._deleteLocation() # cleans up ./scratch created during doctest
### JavaScript ###
```JavaScript
...
key = function (n) { return this._keys[n]; }
n/a
removeItem = function (key) { var evnt, filename, hasListeners, k, meta, metaKey, oldValue, ref, v; key = _escapeKey(key); 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); ref = this._metaKeyMap; for (k in ref) { v = ref[k]; meta = this._metaKeyMap[k]; if (meta.index > metaKey.index) { meta.index -= 1; } } _rm(filename); if (hasListeners) { evnt = new StorageEvent(key, oldValue, null, this._eventUrl); return this.emit('storage', evnt); } } }
n/a
setItem = function (key, value) { 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 = _escapeKey(key); encodedKey = encodeURIComponent(key); filename = path.join(this._location, encodedKey); valueString = value.toString(); valueStringLength = valueString.length; metaKey = this._metaKeyMap[key]; existsBeforeSet = !!metaKey; if (existsBeforeSet) { oldLength = metaKey.size; } else { oldLength = 0; } if (this._bytesInUse - oldLength + valueStringLength > this.quota) { throw new QUOTA_EXCEEDED_ERR(); } writeSync(filename, valueString, 'utf8'); if (!existsBeforeSet) { 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); } }
...
### CoffeeScript ###
unless localStorage?
{LocalStorage} = require('../') # require('node-localstorage') for you
localStorage = new LocalStorage('./scratch')
localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue
localStorage._deleteLocation() # cleans up ./scratch created during doctest
### JavaScript ###
...
function QUOTA_EXCEEDED_ERR(message) { this.message = message != null ? message : 'Unknown error.'; if (Error.captureStackTrace != null) { Error.captureStackTrace(this, this.constructor); } this.name = this.constructor.name; }
n/a
function captureStackTrace() { [native code] }
...
QUOTA_EXCEEDED_ERR = (function(superClass) {
extend(QUOTA_EXCEEDED_ERR, superClass);
function QUOTA_EXCEEDED_ERR(message) {
this.message = message != null ? message : 'Unknown error.';
if (Error.captureStackTrace != null) {
Error.captureStackTrace(this, this.constructor);
}
this.name = this.constructor.name;
}
QUOTA_EXCEEDED_ERR.prototype.toString = function() {
return this.name + ": " + this.message;
};
...
function QUOTA_EXCEEDED_ERR(message) { this.message = message != null ? message : 'Unknown error.'; if (Error.captureStackTrace != null) { Error.captureStackTrace(this, this.constructor); } this.name = this.constructor.name; }
n/a
toString = function () { return this.name + ": " + this.message; }
...
};
_escapeKey = function(key) {
var newKey;
if (key === '') {
newKey = KEY_FOR_EMPTY_STRING;
} else {
newKey = key.toString();
}
return newKey;
};
QUOTA_EXCEEDED_ERR = (function(superClass) {
extend(QUOTA_EXCEEDED_ERR, superClass);
...