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

local-storage-store.js 5.1KB

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