Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/expire #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { ClientStorage } from 'meteor/ostrio:cstorage';

- `ClientStorage.get('key')` - Read a record. If the key doesn't exist a null value will be returned;
- `key` - `{String}` - Record's key;
- `ClientStorage.set('key', value)` - Create/overwrite a value in storage;
- `ClientStorage.set('key', value, time)` - Create/overwrite a value in storage, optional time to expire in milliseconds;
- `key` - `{String}` - Record's key;
- `value` - `{String|[mix]|Boolean|Object}` - Record's value (content);
- `ClientStorage.remove('key')` - Remove a record;
Expand Down
114 changes: 73 additions & 41 deletions client-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function __Cookies(_cookies) {
@summary Read a cookie. If the cookie doesn't exist a null value will be returned.
@returns {String|null}
*/
__Cookies.prototype.get = function(key) {
__Cookies.prototype.get = function (key) {
if (!key) {
return void 0;
}
Expand All @@ -72,7 +72,7 @@ __Cookies.prototype.get = function(key) {
@summary Create/overwrite a cookie.
@returns {Boolean}
*/
__Cookies.prototype.set = function(key, value) {
__Cookies.prototype.set = function (key, value) {
if (key) {
this.cookies[key] = value;
document.cookie = escape(key) + '=' + escape(value) + '; Expires=Fri, 31 Dec 9999 23:59:59 GMT; Path=/';
Expand All @@ -90,7 +90,7 @@ __Cookies.prototype.set = function(key, value) {
@summary Remove a cookie(s).
@returns {Boolean}
*/
__Cookies.prototype.remove = function(key) {
__Cookies.prototype.remove = function (key) {
var keys = Object.keys(this.cookies);
if (key) {
if (!this.cookies.hasOwnProperty(key)) {
Expand All @@ -117,7 +117,7 @@ __Cookies.prototype.remove = function(key) {
@summary Check whether a cookie key is exists
@returns {Boolean}
*/
__Cookies.prototype.has = function(key) {
__Cookies.prototype.has = function (key) {
if (!key) {
return false;
}
Expand All @@ -133,7 +133,7 @@ __Cookies.prototype.has = function(key) {
@summary Returns an array of all readable cookies from this location.
@returns {[String]}
*/
__Cookies.prototype.keys = function() {
__Cookies.prototype.keys = function () {
return Object.keys(this.cookies);
};

Expand All @@ -152,33 +152,33 @@ function ClientStorage(driver) {
}

switch (driver) {
case 'localStorage':
if (this.LSSupport) {
this.ls = window.localStorage || localStorage;
} else {
console.warn('ClientStorage is set to "localStorage", but it is not supported on this browser');
}
break;
case 'cookies':
if (this.cookies) {
case 'localStorage':
hakod marked this conversation as resolved.
Show resolved Hide resolved
if (this.LSSupport) {
this.ls = window.localStorage || localStorage;
} else {
console.warn('ClientStorage is set to "localStorage", but it is not supported on this browser');
}
break;
case 'cookies':
if (this.cookies) {
this.LSSupport = false;
this.ls = null;
} else {
console.warn('ClientStorage is set to "cookies", but Cookies is disabled on this browser');
}
break;
case 'js':
this.cookies = false;
this.LSSupport = false;
this.ls = null;
} else {
console.warn('ClientStorage is set to "cookies", but Cookies is disabled on this browser');
}
break;
case 'js':
this.cookies = false;
this.LSSupport = false;
this.ls = null;
break;
default:
if (this.LSSupport) {
this.ls = window.localStorage || localStorage;
} else {
this.ls = null;
}
break;
break;
default:
if (this.LSSupport) {
this.ls = window.localStorage || localStorage;
} else {
this.ls = null;
}
break;
}
}

Expand All @@ -191,7 +191,7 @@ function ClientStorage(driver) {
@summary Read a record. If the record doesn't exist a null value will be returned.
@returns {mixed}
*/
ClientStorage.prototype.get = function(key) {
ClientStorage.prototype.get = function (key) {
if (!this.has(key)) {
return void 0;
}
Expand All @@ -213,7 +213,24 @@ ClientStorage.prototype.get = function(key) {
@summary Create/overwrite a value in storage.
@returns {Boolean}
*/
ClientStorage.prototype.set = function(key, value) {
ClientStorage.prototype.set = function (key, value, time) {
if (time) {
if (this.LSSupport) {
let expire = new Date().getTime() + time;
this.ls.setItem(key, this.__escape(value));
this.ls.setItem(key + "._expiredAt", expire);
} else if (this.cookies) {
this.cookies.set(key, this.__escape(value));
let seconds = time / 1000;
document.cookie = key + "=" + value + "; max-age=" + seconds + "; path=/";
hakod marked this conversation as resolved.
Show resolved Hide resolved
} else {
this._data[key] = value;
setTimeout(() => {
delete this._data[key];
}, time)
}
return true
hakod marked this conversation as resolved.
Show resolved Hide resolved
}
if (this.LSSupport) {
this.ls.setItem(key, this.__escape(value));
} else if (this.cookies) {
Expand All @@ -224,6 +241,21 @@ ClientStorage.prototype.set = function(key, value) {
return true;
};

function LSExpire() {
setInterval(() => {
for (var key in localStorage) {
if (key.slice(-11) === "._expiredAt") {
hakod marked this conversation as resolved.
Show resolved Hide resolved
if (window.localStorage.getItem(key) < new Date().getTime()) {
window.localStorage.removeItem(key);
window.localStorage.removeItem(key.replace("._expiredAt", ""))
}
}
}
}, 2000)
}

LSExpire();
hakod marked this conversation as resolved.
Show resolved Hide resolved


/*
@function
Expand All @@ -233,7 +265,7 @@ ClientStorage.prototype.set = function(key, value) {
@summary Remove a record.
@returns {Boolean}
*/
ClientStorage.prototype.remove = function(key) {
ClientStorage.prototype.remove = function (key) {
if (key && this.has(key)) {
if (this.LSSupport) {
this.ls.removeItem(key);
Expand All @@ -256,7 +288,7 @@ ClientStorage.prototype.remove = function(key) {
@summary Check if record exists
@returns {Boolean}
*/
ClientStorage.prototype.has = function(key) {
ClientStorage.prototype.has = function (key) {
if (this.LSSupport) {
return !!this.ls.getItem(key);
} else if (this.cookies) {
Expand All @@ -273,7 +305,7 @@ ClientStorage.prototype.has = function(key) {
@summary Returns all storage keys
@returns {[String]]}
*/
ClientStorage.prototype.keys = function() {
ClientStorage.prototype.keys = function () {
if (this.LSSupport) {
var i = this.ls.length;
var results = [];
Expand All @@ -295,16 +327,16 @@ ClientStorage.prototype.keys = function() {
@summary Empty storage (remove all key/value pairs)
@returns {Boolean}
*/
ClientStorage.prototype.empty = function() {
ClientStorage.prototype.empty = function () {
if (this.LSSupport && this.ls.length > 0) {
var self = this;
this.keys().forEach(function(key) {
this.keys().forEach(function (key) {
return self.remove(key);
});
return true;
} else if (this.cookies) {
return this.cookies.remove();
} else if (Object.keys(this._data).length){
} else if (Object.keys(this._data).length) {
this._data = {};
return true;
}
Expand All @@ -317,7 +349,7 @@ ClientStorage.prototype.empty = function() {
@memberOf ClientStorage
@name __escape
*/
ClientStorage.prototype.__escape = function(value) {
ClientStorage.prototype.__escape = function (value) {
try {
return JSON.stringify(value);
} catch (e) {
Expand All @@ -334,7 +366,7 @@ ClientStorage.prototype.__escape = function(value) {
@memberOf ClientStorage
@name __unescape
*/
ClientStorage.prototype.__unescape = function(value) {
ClientStorage.prototype.__unescape = function (value) {
try {
return JSON.parse(value);
} catch (e) {
Expand All @@ -348,7 +380,7 @@ ClientStorage.prototype.__unescape = function(value) {
@name LSSupport
@summary Test browser for localStorage support
*/
ClientStorage.prototype.LSSupport = (function() {
ClientStorage.prototype.LSSupport = (function () {
try {
var support = 'localStorage' in window && window.localStorage !== null;
if (support) {
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Package.describe({
Package.onUse((api) => {
api.versionsFrom('1.4');
api.use('ecmascript', 'client');
api.mainModule('cstorage.js', 'client');
api.mainModule('client-storage.js', 'client');
});

Package.onTest((api) => {
Expand Down