cordova-plugin-advanced-http/www/local-storage-store.js

182 lines
5.1 KiB
JavaScript
Raw Normal View History

2016-12-06 00:52:20 +08:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Exponent
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Based on "tough-cookie-web-storage-store" v1.0.0
* Thanks James Ide: https://github.com/exponentjs/tough-cookie-web-storage-store
*
* Modified by Sefa Ilkimen for cordova plugin integration
*
*/
'use strict';
module.exports = function init(ToughCookie, _) {
function WebStorageCookieStore(storage, storeKey) {
2016-12-06 00:52:20 +08:00
ToughCookie.Store.call(this);
this._storage = storage;
2016-12-06 01:25:14 +08:00
this._storeKey = storeKey || '__cookieStore__';
2016-12-06 00:52:20 +08:00
this.synchronous = true;
}
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype = Object.create(ToughCookie.Store);
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.findCookie = function (domain, path, key, callback) {
2016-12-06 00:52:20 +08:00
var store = this._readStore();
var cookie = _.get(store, [domain, path, key], null);
2016-12-06 01:25:14 +08:00
callback(null, ToughCookie.Cookie.fromJSON(cookie));
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.findCookies = function (domain, path, callback) {
2016-12-06 00:52:20 +08:00
if (!domain) {
callback(null, []);
return;
2016-12-06 00:52:20 +08:00
}
var that = this;
var cookies = [];
var store = this._readStore();
var domains = ToughCookie.permuteDomain(domain) || [domain];
domains.forEach(function (domain) {
if (!store[domain]) {
return;
}
2016-12-06 00:52:20 +08:00
var matchingPaths = Object.keys(store[domain]);
2016-12-06 00:52:20 +08:00
if (path != null) {
matchingPaths = matchingPaths.filter(function (cookiePath) {
return that._isOnPath(cookiePath, path);
2016-12-06 00:52:20 +08:00
});
}
matchingPaths.forEach(function (path) {
Array.prototype.push.apply(cookies, _.values(store[domain][path]));
});
2016-12-06 00:52:20 +08:00
});
cookies = cookies.map(function (cookie) {
return ToughCookie.Cookie.fromJSON(cookie);
2016-12-06 00:52:20 +08:00
});
callback(null, cookies);
};
2016-12-06 00:52:20 +08:00
/**
* Returns whether `cookiePath` is on the given `urlPath`
*/
WebStorageCookieStore.prototype._isOnPath = function (cookiePath, urlPath) {
2016-12-06 00:52:20 +08:00
if (!cookiePath) {
return false;
2016-12-06 00:52:20 +08:00
}
if (cookiePath === urlPath) {
return true;
2016-12-06 00:52:20 +08:00
}
if (urlPath.indexOf(cookiePath) !== 0) {
return false;
2016-12-06 00:52:20 +08:00
}
if (cookiePath[cookiePath.length - 1] !== '/' && urlPath[cookiePath.length] !== '/') {
return false;
2016-12-06 00:52:20 +08:00
}
return true;
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.putCookie = function (cookie, callback) {
var store = this._readStore();
2016-12-06 00:52:20 +08:00
_.set(store, [cookie.domain, cookie.path, cookie.key], cookie);
this._writeStore(store);
callback(null);
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.updateCookie = function (oldCookie, newCookie, callback) {
2016-12-06 00:52:20 +08:00
this.putCookie(newCookie, callback);
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.removeCookie = function (domain, path, key, callback) {
2016-12-06 00:52:20 +08:00
var store = this._readStore();
_.unset(store, [domain, path, key]);
this._writeStore(store);
callback(null);
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.removeCookies = function (domain, path, callback) {
2016-12-06 00:52:20 +08:00
var store = this._readStore();
if (path == null) {
_.unset(store, [domain]);
2016-12-06 00:52:20 +08:00
} else {
_.unset(store, [domain, path]);
2016-12-06 00:52:20 +08:00
}
this._writeStore(store);
callback(null);
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype.getAllCookies = function (callback) {
2016-12-06 00:52:20 +08:00
var cookies = [];
var store = this._readStore();
Object.keys(store).forEach(function (domain) {
Object.keys(store[domain]).forEach(function (path) {
Array.protype.push.apply(cookies, _.values(store[domain][path]));
});
2016-12-06 00:52:20 +08:00
});
cookies = cookies.map(function (cookie) {
return ToughCookie.Cookie.fromJSON(cookie);
2016-12-06 00:52:20 +08:00
});
cookies.sort(function (c1, c2) {
return (c1.creationIndex || 0) - (c2.creationIndex || 0);
2016-12-06 00:52:20 +08:00
});
callback(null, cookies);
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype._readStore = function () {
2016-12-06 01:25:14 +08:00
var json = this._storage.getItem(this._storeKey);
2016-12-06 00:52:20 +08:00
if (json !== null) {
try {
return JSON.parse(json);
} catch (e) { }
2016-12-06 00:52:20 +08:00
}
return {};
};
2016-12-06 00:52:20 +08:00
WebStorageCookieStore.prototype._writeStore = function (store) {
2016-12-06 01:25:14 +08:00
this._storage.setItem(this._storeKey, JSON.stringify(store));
};
2016-12-06 00:52:20 +08:00
return WebStorageCookieStore;
};