Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

local-storage-store.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. cordova.define("cordova-plugin-advanced-http.local-storage-store", function(require, exports, module) {
  2. /*
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2016 Exponent
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Based on "tough-cookie-web-storage-store" v1.0.0
  26. * Thanks James Ide: https://github.com/exponentjs/tough-cookie-web-storage-store
  27. *
  28. * Modified by Sefa Ilkimen for cordova plugin integration
  29. *
  30. */
  31. 'use strict';
  32. module.exports = function init(ToughCookie, _) {
  33. function WebStorageCookieStore(storage, storeKey) {
  34. ToughCookie.Store.call(this);
  35. this._storage = storage;
  36. this._storeKey = storeKey || '__cookieStore__';
  37. this.synchronous = true;
  38. }
  39. WebStorageCookieStore.prototype = Object.create(ToughCookie.Store);
  40. WebStorageCookieStore.prototype.findCookie = function (domain, path, key, callback) {
  41. var store = this._readStore();
  42. var cookie = _.get(store, [domain, path, key], null);
  43. callback(null, ToughCookie.Cookie.fromJSON(cookie));
  44. };
  45. WebStorageCookieStore.prototype.findCookies = function (domain, path, callback) {
  46. if (!domain) {
  47. callback(null, []);
  48. return;
  49. }
  50. var that = this;
  51. var cookies = [];
  52. var store = this._readStore();
  53. var domains = ToughCookie.permuteDomain(domain) || [domain];
  54. domains.forEach(function (domain) {
  55. if (!store[domain]) {
  56. return;
  57. }
  58. var matchingPaths = Object.keys(store[domain]);
  59. if (path != null) {
  60. matchingPaths = matchingPaths.filter(function (cookiePath) {
  61. return that._isOnPath(cookiePath, path);
  62. });
  63. }
  64. matchingPaths.forEach(function (path) {
  65. Array.prototype.push.apply(cookies, _.values(store[domain][path]));
  66. });
  67. });
  68. cookies = cookies.map(function (cookie) {
  69. return ToughCookie.Cookie.fromJSON(cookie);
  70. });
  71. callback(null, cookies);
  72. };
  73. /**
  74. * Returns whether `cookiePath` is on the given `urlPath`
  75. */
  76. WebStorageCookieStore.prototype._isOnPath = function (cookiePath, urlPath) {
  77. if (!cookiePath) {
  78. return false;
  79. }
  80. if (cookiePath === urlPath) {
  81. return true;
  82. }
  83. if (urlPath.indexOf(cookiePath) !== 0) {
  84. return false;
  85. }
  86. if (cookiePath[cookiePath.length - 1] !== '/' && urlPath[cookiePath.length] !== '/') {
  87. return false;
  88. }
  89. return true;
  90. };
  91. WebStorageCookieStore.prototype.putCookie = function (cookie, callback) {
  92. var store = this._readStore();
  93. _.set(store, [cookie.domain, cookie.path, cookie.key], cookie);
  94. this._writeStore(store);
  95. callback(null);
  96. };
  97. WebStorageCookieStore.prototype.updateCookie = function (oldCookie, newCookie, callback) {
  98. this.putCookie(newCookie, callback);
  99. };
  100. WebStorageCookieStore.prototype.removeCookie = function (domain, path, key, callback) {
  101. var store = this._readStore();
  102. _.unset(store, [domain, path, key]);
  103. this._writeStore(store);
  104. callback(null);
  105. };
  106. WebStorageCookieStore.prototype.removeCookies = function (domain, path, callback) {
  107. var store = this._readStore();
  108. if (path == null) {
  109. _.unset(store, [domain]);
  110. } else {
  111. _.unset(store, [domain, path]);
  112. }
  113. this._writeStore(store);
  114. callback(null);
  115. };
  116. WebStorageCookieStore.prototype.getAllCookies = function (callback) {
  117. var cookies = [];
  118. var store = this._readStore();
  119. Object.keys(store).forEach(function (domain) {
  120. Object.keys(store[domain]).forEach(function (path) {
  121. Array.prototype.push.apply(cookies, _.values(store[domain][path]));
  122. });
  123. });
  124. cookies = cookies.map(function (cookie) {
  125. return ToughCookie.Cookie.fromJSON(cookie);
  126. });
  127. cookies.sort(function (c1, c2) {
  128. return (c1.creationIndex || 0) - (c2.creationIndex || 0);
  129. });
  130. callback(null, cookies);
  131. };
  132. WebStorageCookieStore.prototype._readStore = function () {
  133. var json = this._storage.getItem(this._storeKey);
  134. if (json !== null) {
  135. try {
  136. return JSON.parse(json);
  137. } catch (e) { }
  138. }
  139. return {};
  140. };
  141. WebStorageCookieStore.prototype._writeStore = function (store) {
  142. this._storage.setItem(this._storeKey, JSON.stringify(store));
  143. };
  144. return WebStorageCookieStore;
  145. };
  146. });