Skip to content

Commit

Permalink
Fixed #4: Removing cookies are failing when the domain or path were s…
Browse files Browse the repository at this point in the history
…et in the options
  • Loading branch information
jherax committed Apr 20, 2017
1 parent 9d8600f commit 7117075
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
46 changes: 29 additions & 17 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function executeInterceptors(command) {
}
return _interceptors[command].reduce(function (val, action) {
var transformed = action.apply(undefined, [key, val].concat(args));
if (transformed === undefined) return val;
if (transformed == null) return val;
return transformed;
}, value);
}
Expand Down Expand Up @@ -379,7 +379,9 @@ var WebStorage = function () {
var v = executeInterceptors('setItem', key, value, options);
if (v !== undefined) value = v;
this[key] = value;
// TODO: add keepRAW as instance property to prevent storing as JSON
value = JSON.stringify(value);
// TODO: remove expired cookies through setTimeout for expires
_proxyMechanism.proxy[this.__storage__].setItem(key, value, options);
}

Expand All @@ -401,6 +403,7 @@ var WebStorage = function () {
delete this[key];
value = null;
} else {
// TODO: add keepRAW as instance property to prevent reading from JSON
value = tryParse(value);
this[key] = value;
}
Expand Down Expand Up @@ -520,8 +523,8 @@ var $cookie = {
},
set: function set(value) {
document.cookie = value;
}
};
},
data: {} };

/**
* @private
Expand All @@ -536,9 +539,14 @@ var $cookie = {
/* eslint-disable no-invalid-this */
function buildExpirationString(date) {
var expires = date instanceof Date ? (0, _utils.alterDate)({ date: date }) : (0, _utils.alterDate)(date);
return '; expires=' + expires.toUTCString();
return expires.toUTCString();
}

// @private
var buildStringFor = function buildStringFor(key, data) {
return data[key] && '; ' + key + '=' + data[key] || '';
};

/**
* @private
*
Expand All @@ -564,24 +572,23 @@ function findCookie(cookie) {
function cookieStorage() {
var api = {
setItem: function setItem(key, value, options) {
var domain = '',
expires = '';
options = Object.assign({ path: '/' }, options);
// keep track of the metadata associated to the cookie
$cookie.data[key] = $cookie.data[key] || {};
var metadata = $cookie.data[key];
if ((0, _utils.isObject)(options.expires) || options.expires instanceof Date) {
expires = buildExpirationString(options.expires);
metadata.expires = buildExpirationString(options.expires);
}
// http://stackoverflow.com/a/5671466/2247494
if (typeof options.domain === 'string') {
domain = '; domain=' + options.domain.trim();
if (options.domain && typeof options.domain === 'string') {
metadata.domain = options.domain.trim();
}
var cookie = key + '=' + encodeURIComponent(value) + expires + domain + '; path=' + options.path;
// TODO: add metadata to store options for the cookie
// TODO: remove cookies are failing when domain or path were set
metadata.path = options.path;
var expires = buildStringFor('expires', metadata);
var domain = buildStringFor('domain', metadata);
var path = buildStringFor('path', metadata);
var cookie = key + '=' + encodeURIComponent(value) + expires + domain + path;
// TODO: prevent adding cookies when the domain or path are not valid
// TODO: remove expired cookies through getItem or setTimeout for expires
// console.log('before set', $cookie.get()); // eslint-disable-line
$cookie.set(cookie);
// console.log('after set', $cookie.get()); // eslint-disable-line
},
getItem: function getItem(key) {
var value = null;
Expand All @@ -592,10 +599,15 @@ function cookieStorage() {
value = cookie.trim().substring(nameEQ.length, cookie.length);
value = decodeURIComponent(value);
}
if (value === null) delete $cookie.data[key];
return value;
},
removeItem: function removeItem(key) {
api.setItem(key, '', { expires: { days: -1 } });
// TODO: remove cookies are failing when the domain or path were set, because of the lack of metadata
var metadata = Object.assign({}, $cookie.data[key]);
metadata.expires = { days: -1 };
api.setItem(key, '', metadata);
delete $cookie.data[key];
},
clear: function clear() {
var eq = '=';
Expand Down
2 changes: 1 addition & 1 deletion dist/proxy-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/proxy-storage.min.map

Large diffs are not rendered by default.

Loading

0 comments on commit 7117075

Please sign in to comment.