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

feat: ability to use custom storage for cookies #434

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions www/cookie-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function init(storage, ToughCookie, WebStorageCookieStore) {
setCookie: setCookie,
getCookieString: getCookieString,
clearCookies: clearCookies,
removeCookies: removeCookies
removeCookies: removeCookies,
setStorageImpl: setStorageImpl
};

function splitCookieString(cookieStr) {
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = function init(storage, ToughCookie, WebStorageCookieStore) {
}

function clearCookies() {
window.localStorage.removeItem(storeKey);
storage.removeItem(storeKey);
}

function removeCookies(url, cb) {
Expand All @@ -67,4 +68,9 @@ module.exports = function init(storage, ToughCookie, WebStorageCookieStore) {
cookieJar.store.removeCookies(domain, null, cb);
});
}

function setStorageImpl(_storage) {
store.setStorageImpl(_storage)
storage = _storage;
}
};
4 changes: 4 additions & 0 deletions www/local-storage-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = function init(ToughCookie, _) {

WebStorageCookieStore.prototype = Object.create(ToughCookie.Store);

WebStorageCookieStore.prototype.setStorageImpl = function (storage) {
this._storage = storage;
};

WebStorageCookieStore.prototype.findCookie = function (domain, path, key, callback) {
var store = this._readStore();
var cookie = _.get(store, [domain, path, key], null);
Expand Down
13 changes: 13 additions & 0 deletions www/public-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
setHeader: setHeader,
getDataSerializer: getDataSerializer,
setDataSerializer: setDataSerializer,
setCookieStorageImpl: setCookieStorageImpl,
setCookie: setCookie,
clearCookies: clearCookies,
removeCookies: removeCookies,
Expand Down Expand Up @@ -81,6 +82,18 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
globalConfigs.serializer = helpers.checkSerializer(serializer);
}

/**
* Provides a custom cookie storage, to override the default localStorage
* @param {Object} storage
* @param {(name: string, value: string) => void} storage.setItem
* @param {(name: string) => string} storage.getItem
* @param {(name: string) => void} storage.removeItem
*/
function setCookieStorageImpl(storage)
{
cookieHandler.setStorageImpl(storage);
}

function setCookie(url, cookie, options) {
cookieHandler.setCookie(url, cookie, options);
}
Expand Down