memstore.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!
  2. * Copyright (c) 2015, Salesforce.com, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of Salesforce.com nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. "use strict";
  32. const { fromCallback } = require("universalify");
  33. const Store = require("./store").Store;
  34. const permuteDomain = require("./permuteDomain").permuteDomain;
  35. const pathMatch = require("./pathMatch").pathMatch;
  36. const { getCustomInspectSymbol, getUtilInspect } = require("./utilHelper");
  37. class MemoryCookieStore extends Store {
  38. constructor() {
  39. super();
  40. this.synchronous = true;
  41. this.idx = Object.create(null);
  42. const customInspectSymbol = getCustomInspectSymbol();
  43. if (customInspectSymbol) {
  44. this[customInspectSymbol] = this.inspect;
  45. }
  46. }
  47. inspect() {
  48. const util = { inspect: getUtilInspect(inspectFallback) };
  49. return `{ idx: ${util.inspect(this.idx, false, 2)} }`;
  50. }
  51. findCookie(domain, path, key, cb) {
  52. if (!this.idx[domain]) {
  53. return cb(null, undefined);
  54. }
  55. if (!this.idx[domain][path]) {
  56. return cb(null, undefined);
  57. }
  58. return cb(null, this.idx[domain][path][key] || null);
  59. }
  60. findCookies(domain, path, allowSpecialUseDomain, cb) {
  61. const results = [];
  62. if (typeof allowSpecialUseDomain === "function") {
  63. cb = allowSpecialUseDomain;
  64. allowSpecialUseDomain = true;
  65. }
  66. if (!domain) {
  67. return cb(null, []);
  68. }
  69. let pathMatcher;
  70. if (!path) {
  71. // null means "all paths"
  72. pathMatcher = function matchAll(domainIndex) {
  73. for (const curPath in domainIndex) {
  74. const pathIndex = domainIndex[curPath];
  75. for (const key in pathIndex) {
  76. results.push(pathIndex[key]);
  77. }
  78. }
  79. };
  80. } else {
  81. pathMatcher = function matchRFC(domainIndex) {
  82. //NOTE: we should use path-match algorithm from S5.1.4 here
  83. //(see : https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/canonical_cookie.cc#L299)
  84. Object.keys(domainIndex).forEach(cookiePath => {
  85. if (pathMatch(path, cookiePath)) {
  86. const pathIndex = domainIndex[cookiePath];
  87. for (const key in pathIndex) {
  88. results.push(pathIndex[key]);
  89. }
  90. }
  91. });
  92. };
  93. }
  94. const domains = permuteDomain(domain, allowSpecialUseDomain) || [domain];
  95. const idx = this.idx;
  96. domains.forEach(curDomain => {
  97. const domainIndex = idx[curDomain];
  98. if (!domainIndex) {
  99. return;
  100. }
  101. pathMatcher(domainIndex);
  102. });
  103. cb(null, results);
  104. }
  105. putCookie(cookie, cb) {
  106. if (!this.idx[cookie.domain]) {
  107. this.idx[cookie.domain] = Object.create(null);
  108. }
  109. if (!this.idx[cookie.domain][cookie.path]) {
  110. this.idx[cookie.domain][cookie.path] = Object.create(null);
  111. }
  112. this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
  113. cb(null);
  114. }
  115. updateCookie(oldCookie, newCookie, cb) {
  116. // updateCookie() may avoid updating cookies that are identical. For example,
  117. // lastAccessed may not be important to some stores and an equality
  118. // comparison could exclude that field.
  119. this.putCookie(newCookie, cb);
  120. }
  121. removeCookie(domain, path, key, cb) {
  122. if (
  123. this.idx[domain] &&
  124. this.idx[domain][path] &&
  125. this.idx[domain][path][key]
  126. ) {
  127. delete this.idx[domain][path][key];
  128. }
  129. cb(null);
  130. }
  131. removeCookies(domain, path, cb) {
  132. if (this.idx[domain]) {
  133. if (path) {
  134. delete this.idx[domain][path];
  135. } else {
  136. delete this.idx[domain];
  137. }
  138. }
  139. return cb(null);
  140. }
  141. removeAllCookies(cb) {
  142. this.idx = Object.create(null);
  143. return cb(null);
  144. }
  145. getAllCookies(cb) {
  146. const cookies = [];
  147. const idx = this.idx;
  148. const domains = Object.keys(idx);
  149. domains.forEach(domain => {
  150. const paths = Object.keys(idx[domain]);
  151. paths.forEach(path => {
  152. const keys = Object.keys(idx[domain][path]);
  153. keys.forEach(key => {
  154. if (key !== null) {
  155. cookies.push(idx[domain][path][key]);
  156. }
  157. });
  158. });
  159. });
  160. // Sort by creationIndex so deserializing retains the creation order.
  161. // When implementing your own store, this SHOULD retain the order too
  162. cookies.sort((a, b) => {
  163. return (a.creationIndex || 0) - (b.creationIndex || 0);
  164. });
  165. cb(null, cookies);
  166. }
  167. }
  168. [
  169. "findCookie",
  170. "findCookies",
  171. "putCookie",
  172. "updateCookie",
  173. "removeCookie",
  174. "removeCookies",
  175. "removeAllCookies",
  176. "getAllCookies"
  177. ].forEach(name => {
  178. MemoryCookieStore.prototype[name] = fromCallback(
  179. MemoryCookieStore.prototype[name]
  180. );
  181. });
  182. exports.MemoryCookieStore = MemoryCookieStore;
  183. function inspectFallback(val) {
  184. const domains = Object.keys(val);
  185. if (domains.length === 0) {
  186. return "[Object: null prototype] {}";
  187. }
  188. let result = "[Object: null prototype] {\n";
  189. Object.keys(val).forEach((domain, i) => {
  190. result += formatDomain(domain, val[domain]);
  191. if (i < domains.length - 1) {
  192. result += ",";
  193. }
  194. result += "\n";
  195. });
  196. result += "}";
  197. return result;
  198. }
  199. function formatDomain(domainName, domainValue) {
  200. const indent = " ";
  201. let result = `${indent}'${domainName}': [Object: null prototype] {\n`;
  202. Object.keys(domainValue).forEach((path, i, paths) => {
  203. result += formatPath(path, domainValue[path]);
  204. if (i < paths.length - 1) {
  205. result += ",";
  206. }
  207. result += "\n";
  208. });
  209. result += `${indent}}`;
  210. return result;
  211. }
  212. function formatPath(pathName, pathValue) {
  213. const indent = " ";
  214. let result = `${indent}'${pathName}': [Object: null prototype] {\n`;
  215. Object.keys(pathValue).forEach((cookieName, i, cookieNames) => {
  216. const cookie = pathValue[cookieName];
  217. result += ` ${cookieName}: ${cookie.inspect()}`;
  218. if (i < cookieNames.length - 1) {
  219. result += ",";
  220. }
  221. result += "\n";
  222. });
  223. result += `${indent}}`;
  224. return result;
  225. }
  226. exports.inspectFallback = inspectFallback;