Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memstore.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 util = require("util");
  37. class MemoryCookieStore extends Store {
  38. constructor() {
  39. super();
  40. this.synchronous = true;
  41. this.idx = {};
  42. if (util.inspect.custom) {
  43. this[util.inspect.custom] = this.inspect;
  44. }
  45. }
  46. inspect() {
  47. return `{ idx: ${util.inspect(this.idx, false, 2)} }`;
  48. }
  49. findCookie(domain, path, key, cb) {
  50. if (!this.idx[domain]) {
  51. return cb(null, undefined);
  52. }
  53. if (!this.idx[domain][path]) {
  54. return cb(null, undefined);
  55. }
  56. return cb(null, this.idx[domain][path][key] || null);
  57. }
  58. findCookies(domain, path, allowSpecialUseDomain, cb) {
  59. const results = [];
  60. if (typeof allowSpecialUseDomain === "function") {
  61. cb = allowSpecialUseDomain;
  62. allowSpecialUseDomain = false;
  63. }
  64. if (!domain) {
  65. return cb(null, []);
  66. }
  67. let pathMatcher;
  68. if (!path) {
  69. // null means "all paths"
  70. pathMatcher = function matchAll(domainIndex) {
  71. for (const curPath in domainIndex) {
  72. const pathIndex = domainIndex[curPath];
  73. for (const key in pathIndex) {
  74. results.push(pathIndex[key]);
  75. }
  76. }
  77. };
  78. } else {
  79. pathMatcher = function matchRFC(domainIndex) {
  80. //NOTE: we should use path-match algorithm from S5.1.4 here
  81. //(see : https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/canonical_cookie.cc#L299)
  82. Object.keys(domainIndex).forEach(cookiePath => {
  83. if (pathMatch(path, cookiePath)) {
  84. const pathIndex = domainIndex[cookiePath];
  85. for (const key in pathIndex) {
  86. results.push(pathIndex[key]);
  87. }
  88. }
  89. });
  90. };
  91. }
  92. const domains = permuteDomain(domain, allowSpecialUseDomain) || [domain];
  93. const idx = this.idx;
  94. domains.forEach(curDomain => {
  95. const domainIndex = idx[curDomain];
  96. if (!domainIndex) {
  97. return;
  98. }
  99. pathMatcher(domainIndex);
  100. });
  101. cb(null, results);
  102. }
  103. putCookie(cookie, cb) {
  104. if (!this.idx[cookie.domain]) {
  105. this.idx[cookie.domain] = {};
  106. }
  107. if (!this.idx[cookie.domain][cookie.path]) {
  108. this.idx[cookie.domain][cookie.path] = {};
  109. }
  110. this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
  111. cb(null);
  112. }
  113. updateCookie(oldCookie, newCookie, cb) {
  114. // updateCookie() may avoid updating cookies that are identical. For example,
  115. // lastAccessed may not be important to some stores and an equality
  116. // comparison could exclude that field.
  117. this.putCookie(newCookie, cb);
  118. }
  119. removeCookie(domain, path, key, cb) {
  120. if (
  121. this.idx[domain] &&
  122. this.idx[domain][path] &&
  123. this.idx[domain][path][key]
  124. ) {
  125. delete this.idx[domain][path][key];
  126. }
  127. cb(null);
  128. }
  129. removeCookies(domain, path, cb) {
  130. if (this.idx[domain]) {
  131. if (path) {
  132. delete this.idx[domain][path];
  133. } else {
  134. delete this.idx[domain];
  135. }
  136. }
  137. return cb(null);
  138. }
  139. removeAllCookies(cb) {
  140. this.idx = {};
  141. return cb(null);
  142. }
  143. getAllCookies(cb) {
  144. const cookies = [];
  145. const idx = this.idx;
  146. const domains = Object.keys(idx);
  147. domains.forEach(domain => {
  148. const paths = Object.keys(idx[domain]);
  149. paths.forEach(path => {
  150. const keys = Object.keys(idx[domain][path]);
  151. keys.forEach(key => {
  152. if (key !== null) {
  153. cookies.push(idx[domain][path][key]);
  154. }
  155. });
  156. });
  157. });
  158. // Sort by creationIndex so deserializing retains the creation order.
  159. // When implementing your own store, this SHOULD retain the order too
  160. cookies.sort((a, b) => {
  161. return (a.creationIndex || 0) - (b.creationIndex || 0);
  162. });
  163. cb(null, cookies);
  164. }
  165. }
  166. [
  167. "findCookie",
  168. "findCookies",
  169. "putCookie",
  170. "updateCookie",
  171. "removeCookie",
  172. "removeCookies",
  173. "removeAllCookies",
  174. "getAllCookies"
  175. ].forEach(name => {
  176. MemoryCookieStore[name] = fromCallback(MemoryCookieStore.prototype[name]);
  177. });
  178. exports.MemoryCookieStore = MemoryCookieStore;