node-localstorage
Advanced tools
Comparing version 2.2.1 to 3.0.0
@@ -1,6 +0,4 @@ | ||
// Generated by CoffeeScript 1.12.7 | ||
// Generated by CoffeeScript 2.7.0 | ||
(function() { | ||
var JSONStorage, KEY_FOR_EMPTY_STRING, LocalStorage, MetaKey, QUOTA_EXCEEDED_ERR, StorageEvent, _emptyDirectory, _escapeKey, _rm, createMap, events, fs, path, writeSync, | ||
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; | ||
var JSONStorage, KEY_FOR_EMPTY_STRING, LocalStorage, MetaKey, QUOTA_EXCEEDED_ERR, StorageEvent, _emptyDirectory, _escapeKey, _rm, createMap, events, fs, path, writeSync; | ||
@@ -15,3 +13,3 @@ path = require('path'); | ||
KEY_FOR_EMPTY_STRING = '---.EMPTY_STRING.---'; | ||
KEY_FOR_EMPTY_STRING = '---.EMPTY_STRING.---'; // Chose something that no one is likely to ever use | ||
@@ -43,3 +41,3 @@ _emptyDirectory = function(target) { | ||
} else { | ||
newKey = "" + key; | ||
newKey = `${key}`; | ||
} | ||
@@ -49,8 +47,6 @@ return newKey; | ||
QUOTA_EXCEEDED_ERR = (function(superClass) { | ||
extend(QUOTA_EXCEEDED_ERR, superClass); | ||
function QUOTA_EXCEEDED_ERR(message) { | ||
this.message = message != null ? message : 'Unknown error.'; | ||
QUOTA_EXCEEDED_ERR.__super__.constructor.call(this); | ||
QUOTA_EXCEEDED_ERR = class QUOTA_EXCEEDED_ERR extends Error { | ||
constructor(message = 'Unknown error.') { | ||
super(); | ||
this.message = message; | ||
if (Error.captureStackTrace != null) { | ||
@@ -62,12 +58,10 @@ Error.captureStackTrace(this, this.constructor); | ||
QUOTA_EXCEEDED_ERR.prototype.toString = function() { | ||
return this.name + ": " + this.message; | ||
}; | ||
toString() { | ||
return `${this.name}: ${this.message}`; | ||
} | ||
return QUOTA_EXCEEDED_ERR; | ||
}; | ||
})(Error); | ||
StorageEvent = (function() { | ||
function StorageEvent(key1, oldValue1, newValue1, url, storageArea) { | ||
StorageEvent = class StorageEvent { | ||
constructor(key1, oldValue1, newValue1, url, storageArea = 'localStorage') { | ||
this.key = key1; | ||
@@ -77,11 +71,9 @@ this.oldValue = oldValue1; | ||
this.url = url; | ||
this.storageArea = storageArea != null ? storageArea : 'localStorage'; | ||
this.storageArea = storageArea; | ||
} | ||
return StorageEvent; | ||
}; | ||
})(); | ||
MetaKey = (function() { | ||
function MetaKey(key1, index1) { | ||
MetaKey = class MetaKey { // MetaKey contains key and size | ||
constructor(key1, index1) { | ||
this.key = key1; | ||
@@ -94,7 +86,5 @@ this.index = index1; | ||
return MetaKey; | ||
}; | ||
})(); | ||
createMap = function() { | ||
createMap = function() { // createMap contains Metakeys as properties | ||
var Map; | ||
@@ -106,250 +96,246 @@ Map = function() {}; | ||
LocalStorage = (function(superClass) { | ||
LocalStorage = (function() { | ||
var instanceMap; | ||
extend(LocalStorage, superClass); | ||
instanceMap = {}; | ||
function LocalStorage(_location, quota) { | ||
var handler; | ||
this._location = _location; | ||
this.quota = quota != null ? quota : 5 * 1024 * 1024; | ||
LocalStorage.__super__.constructor.call(this); | ||
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; | ||
if (typeof Proxy !== "undefined" && Proxy !== null) { | ||
handler = { | ||
set: (function(_this) { | ||
return function(receiver, key, value) { | ||
if (_this[key] != null) { | ||
return _this[key] = value; | ||
class LocalStorage extends events.EventEmitter { | ||
constructor(_location, quota = 5 * 1024 * 1024) { | ||
var handler; | ||
super(); | ||
this._location = _location; | ||
this.quota = quota; | ||
// super(_location, quota) | ||
// @_location = _location | ||
// @quota = quota | ||
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; // !TODO: Maybe change this to a property with __defineProperty__ | ||
this._bytesInUse = 0; | ||
this._keys = []; | ||
this._metaKeyMap = createMap(); | ||
this._eventUrl = "pid:" + process.pid; | ||
this._init(); | ||
this._QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR; | ||
if (typeof Proxy !== "undefined" && Proxy !== null) { | ||
handler = { | ||
set: (receiver, key, value) => { | ||
if (this[key] != null) { | ||
this[key] = value; | ||
return true; | ||
} else { | ||
return _this.setItem(key, value); | ||
return this.setItem(key, value); | ||
} | ||
}; | ||
})(this), | ||
get: (function(_this) { | ||
return function(receiver, key) { | ||
if (_this[key] != null) { | ||
return _this[key]; | ||
}, | ||
get: (receiver, key) => { | ||
if (this[key] != null) { | ||
return this[key]; | ||
} else { | ||
return _this.getItem(key); | ||
return this.getItem(key); | ||
} | ||
}; | ||
})(this) | ||
}; | ||
instanceMap[this._location] = new Proxy(this, handler); | ||
} | ||
}; | ||
instanceMap[this._location] = new Proxy(this, handler); | ||
return instanceMap[this._location]; | ||
} | ||
// else it'll return this | ||
instanceMap[this._location] = this; | ||
return instanceMap[this._location]; | ||
} | ||
instanceMap[this._location] = this; | ||
return instanceMap[this._location]; | ||
} | ||
LocalStorage.prototype._init = function() { | ||
var _MetaKey, _decodedKey, _keys, e, 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; | ||
_init() { | ||
var _MetaKey, _decodedKey, _keys, e, 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.length = _keys.length; | ||
} catch (error) { | ||
e = error; | ||
if (e.code !== "ENOENT") { | ||
throw e; | ||
} | ||
try { | ||
fs.mkdirSync(this._location, { | ||
recursive: true | ||
}); | ||
// At this point, it exists and is definitely a directory. So read it. | ||
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) { | ||
e = error; | ||
if (e.code !== "EEXIST") { | ||
// If it errors, that might mean it didn't exist, so try to create it | ||
if (e.code !== "ENOENT") { | ||
throw e; | ||
} | ||
try { | ||
fs.mkdirSync(this._location, { | ||
recursive: true | ||
}); | ||
} catch (error) { | ||
e = error; | ||
if (e.code !== "EEXIST") { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
LocalStorage.prototype.setItem = function(key, value) { | ||
var encodedKey, evnt, existsBeforeSet, filename, hasListeners, metaKey, oldLength, oldValue, valueString, valueStringLength; | ||
hasListeners = this.listenerCount('storage'); | ||
oldValue = null; | ||
if (hasListeners) { | ||
oldValue = this.getItem(key); | ||
setItem(key, value) { | ||
var encodedKey, evnt, existsBeforeSet, filename, hasListeners, metaKey, oldLength, oldValue, valueString, valueStringLength; | ||
hasListeners = this.listenerCount('storage'); | ||
oldValue = null; | ||
if (hasListeners) { | ||
oldValue = this.getItem(key); | ||
} | ||
key = _escapeKey(key); | ||
encodedKey = encodeURIComponent(key).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); | ||
filename = path.join(this._location, encodedKey); | ||
valueString = `${value}`; | ||
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, { | ||
encoding: '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); | ||
} | ||
} | ||
key = _escapeKey(key); | ||
encodedKey = encodeURIComponent(key).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); | ||
filename = path.join(this._location, encodedKey); | ||
valueString = "" + value; | ||
valueStringLength = valueString.length; | ||
metaKey = this._metaKeyMap[key]; | ||
existsBeforeSet = !!metaKey; | ||
if (existsBeforeSet) { | ||
oldLength = metaKey.size; | ||
} else { | ||
oldLength = 0; | ||
getItem(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; | ||
} | ||
} | ||
if (this._bytesInUse - oldLength + valueStringLength > this.quota) { | ||
throw new QUOTA_EXCEEDED_ERR(); | ||
_getStat(key) { | ||
var filename; | ||
key = _escapeKey(key); | ||
filename = path.join(this._location, encodeURIComponent(key)); | ||
try { | ||
return fs.statSync(filename); | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
writeSync(filename, valueString, { | ||
encoding: '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); | ||
} | ||
}; | ||
LocalStorage.prototype.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; | ||
removeItem(key) { | ||
var evnt, filename, hasListeners, k, meta, metaKey, oldValue, ref, v; | ||
key = _escapeKey(key); | ||
metaKey = this._metaKeyMap[key]; | ||
if (!!metaKey) { | ||
hasListeners = this.listenerCount('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); | ||
} | ||
} | ||
} | ||
}; | ||
LocalStorage.prototype._getStat = function(key) { | ||
var filename; | ||
key = _escapeKey(key); | ||
filename = path.join(this._location, encodeURIComponent(key)); | ||
try { | ||
return fs.statSync(filename); | ||
} catch (error) { | ||
return null; | ||
key(n) { | ||
var rawKey; | ||
rawKey = this._keys[n]; | ||
if (rawKey === KEY_FOR_EMPTY_STRING) { | ||
return ''; | ||
} else { | ||
return rawKey; | ||
} | ||
} | ||
}; | ||
LocalStorage.prototype.removeItem = function(key) { | ||
var evnt, filename, hasListeners, k, meta, metaKey, oldValue, ref, v; | ||
key = _escapeKey(key); | ||
metaKey = this._metaKeyMap[key]; | ||
if (!!metaKey) { | ||
hasListeners = this.listenerCount('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); | ||
clear() { | ||
var evnt; | ||
_emptyDirectory(this._location); | ||
this._metaKeyMap = createMap(); | ||
this._keys = []; | ||
this.length = 0; | ||
this._bytesInUse = 0; | ||
if (this.listenerCount('storage')) { | ||
evnt = new StorageEvent(null, null, null, this._eventUrl); | ||
return this.emit('storage', evnt); | ||
} | ||
} | ||
}; | ||
LocalStorage.prototype.key = function(n) { | ||
var rawKey; | ||
rawKey = this._keys[n]; | ||
if (rawKey === KEY_FOR_EMPTY_STRING) { | ||
return ''; | ||
} else { | ||
return rawKey; | ||
_getBytesInUse() { | ||
return this._bytesInUse; | ||
} | ||
}; | ||
LocalStorage.prototype.clear = function() { | ||
var evnt; | ||
_emptyDirectory(this._location); | ||
this._metaKeyMap = createMap(); | ||
this._keys = []; | ||
this.length = 0; | ||
this._bytesInUse = 0; | ||
if (this.listenerCount('storage')) { | ||
evnt = new StorageEvent(null, null, null, this._eventUrl); | ||
return this.emit('storage', evnt); | ||
_deleteLocation() { | ||
delete instanceMap[this._location]; | ||
_rm(this._location); | ||
this._metaKeyMap = {}; | ||
this._keys = []; | ||
this.length = 0; | ||
return this._bytesInUse = 0; | ||
} | ||
}; | ||
LocalStorage.prototype._getBytesInUse = function() { | ||
return this._bytesInUse; | ||
}; | ||
LocalStorage.prototype._deleteLocation = function() { | ||
delete instanceMap[this._location]; | ||
_rm(this._location); | ||
this._metaKeyMap = {}; | ||
this._keys = []; | ||
this.length = 0; | ||
return this._bytesInUse = 0; | ||
}; | ||
instanceMap = {}; | ||
return LocalStorage; | ||
})(events.EventEmitter); | ||
}).call(this); | ||
JSONStorage = (function(superClass) { | ||
extend(JSONStorage, superClass); | ||
function JSONStorage() { | ||
return JSONStorage.__super__.constructor.apply(this, arguments); | ||
} | ||
JSONStorage.prototype.setItem = function(key, value) { | ||
JSONStorage = class JSONStorage extends LocalStorage { | ||
setItem(key, value) { | ||
var newValue; | ||
newValue = JSON.stringify(value); | ||
return JSONStorage.__super__.setItem.call(this, key, newValue); | ||
}; | ||
return super.setItem(key, newValue); | ||
} | ||
JSONStorage.prototype.getItem = function(key) { | ||
return JSON.parse(JSONStorage.__super__.getItem.call(this, key)); | ||
}; | ||
getItem(key) { | ||
return JSON.parse(super.getItem(key)); | ||
} | ||
return JSONStorage; | ||
}; | ||
})(LocalStorage); | ||
exports.LocalStorage = LocalStorage; | ||
@@ -356,0 +342,0 @@ |
{ | ||
"name": "node-localstorage", | ||
"version": "2.2.1", | ||
"version": "3.0.0", | ||
"main": "./LocalStorage", | ||
@@ -13,3 +13,3 @@ "description": "A drop-in substitute for the browser native localStorage API that runs on node.js.", | ||
"name": "Larry Maccherone", | ||
"url": "http://maccherone.com" | ||
"url": "https://www.LinkedIn.com/in/LarryMaccherone/" | ||
}, | ||
@@ -26,8 +26,8 @@ "homepage": "https://github.com/lmaccherone/node-localstorage", | ||
"devDependencies": { | ||
"coffeescript": "^1.12.7", | ||
"coffeescript": "^2.7.0", | ||
"coffeetape": "^2.0.0", | ||
"lodash": "^4.17.15", | ||
"nyc": "^14.1.1", | ||
"nyc": "^15.1.0", | ||
"tap-nyc": "^1.0.3", | ||
"tap-spec": "^5.0.0" | ||
"tap-spec": "^5.0.0", | ||
"tape": "^5.6.6" | ||
}, | ||
@@ -48,4 +48,4 @@ "scripts": { | ||
"dependencies": { | ||
"write-file-atomic": "^1.1.4" | ||
"write-file-atomic": "^5.0.1" | ||
} | ||
} |
@@ -1,4 +0,7 @@ | ||
[![build status](https://secure.travis-ci.org/lmaccherone/node-localstorage.png)](http://travis-ci.org/lmaccherone/node-localstorage) | ||
# node-localstorage # | ||
![CI](https://github.com/lmaccherone/node-localstorage/actions/workflows/master-checks.yml/badge.svg) | ||
![Code QL](https://github.com/lmaccherone/node-localstorage/actions/workflows/codeql-analysis.yml/badge.svg) | ||
# node-localstorage [![sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/lmaccherone) # | ||
Copyright (c) 2012, Lawrence S. Maccherone, Jr. | ||
@@ -94,2 +97,3 @@ | ||
* 3.0.0 - 2023-07-25 - **Backward breaking** Upgrade node.js requirements to 16.x. Bug and compatability fixes | ||
* 2.2.1 - 2021-06-04 - Fixed serveral small issues reported by users | ||
@@ -96,0 +100,0 @@ * 2.1.7 - 2020-06-08 - Fixed stringifying null and undefined (thanks @gamesaucer) |
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
19493
6
133
306
+ Addedsignal-exit@4.1.0(transitive)
+ Addedwrite-file-atomic@5.0.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedslide@1.1.6(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)
Updatedwrite-file-atomic@^5.0.1