| 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 |
|
|---|
| 38 | class MemoryCookieStore extends Store {
|
|---|
| 39 | constructor() {
|
|---|
| 40 | super();
|
|---|
| 41 | this.synchronous = true;
|
|---|
| 42 | this.idx = Object.create(null);
|
|---|
| 43 | const customInspectSymbol = getCustomInspectSymbol();
|
|---|
| 44 | if (customInspectSymbol) {
|
|---|
| 45 | this[customInspectSymbol] = this.inspect;
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | inspect() {
|
|---|
| 50 | const util = { inspect: getUtilInspect(inspectFallback) };
|
|---|
| 51 | return `{ idx: ${util.inspect(this.idx, false, 2)} }`;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | findCookie(domain, path, key, cb) {
|
|---|
| 55 | if (!this.idx[domain]) {
|
|---|
| 56 | return cb(null, undefined);
|
|---|
| 57 | }
|
|---|
| 58 | if (!this.idx[domain][path]) {
|
|---|
| 59 | return cb(null, undefined);
|
|---|
| 60 | }
|
|---|
| 61 | return cb(null, this.idx[domain][path][key] || null);
|
|---|
| 62 | }
|
|---|
| 63 | findCookies(domain, path, allowSpecialUseDomain, cb) {
|
|---|
| 64 | const results = [];
|
|---|
| 65 | if (typeof allowSpecialUseDomain === "function") {
|
|---|
| 66 | cb = allowSpecialUseDomain;
|
|---|
| 67 | allowSpecialUseDomain = true;
|
|---|
| 68 | }
|
|---|
| 69 | if (!domain) {
|
|---|
| 70 | return cb(null, []);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | let pathMatcher;
|
|---|
| 74 | if (!path) {
|
|---|
| 75 | // null means "all paths"
|
|---|
| 76 | pathMatcher = function matchAll(domainIndex) {
|
|---|
| 77 | for (const curPath in domainIndex) {
|
|---|
| 78 | const pathIndex = domainIndex[curPath];
|
|---|
| 79 | for (const key in pathIndex) {
|
|---|
| 80 | results.push(pathIndex[key]);
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | };
|
|---|
| 84 | } else {
|
|---|
| 85 | pathMatcher = function matchRFC(domainIndex) {
|
|---|
| 86 | //NOTE: we should use path-match algorithm from S5.1.4 here
|
|---|
| 87 | //(see : https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/canonical_cookie.cc#L299)
|
|---|
| 88 | Object.keys(domainIndex).forEach(cookiePath => {
|
|---|
| 89 | if (pathMatch(path, cookiePath)) {
|
|---|
| 90 | const pathIndex = domainIndex[cookiePath];
|
|---|
| 91 | for (const key in pathIndex) {
|
|---|
| 92 | results.push(pathIndex[key]);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | });
|
|---|
| 96 | };
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | const domains = permuteDomain(domain, allowSpecialUseDomain) || [domain];
|
|---|
| 100 | const idx = this.idx;
|
|---|
| 101 | domains.forEach(curDomain => {
|
|---|
| 102 | const domainIndex = idx[curDomain];
|
|---|
| 103 | if (!domainIndex) {
|
|---|
| 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 | pathMatcher(domainIndex);
|
|---|
| 107 | });
|
|---|
| 108 |
|
|---|
| 109 | cb(null, results);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | putCookie(cookie, cb) {
|
|---|
| 113 | if (!this.idx[cookie.domain]) {
|
|---|
| 114 | this.idx[cookie.domain] = Object.create(null);
|
|---|
| 115 | }
|
|---|
| 116 | if (!this.idx[cookie.domain][cookie.path]) {
|
|---|
| 117 | this.idx[cookie.domain][cookie.path] = Object.create(null);
|
|---|
| 118 | }
|
|---|
| 119 | this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
|
|---|
| 120 | cb(null);
|
|---|
| 121 | }
|
|---|
| 122 | updateCookie(oldCookie, newCookie, cb) {
|
|---|
| 123 | // updateCookie() may avoid updating cookies that are identical. For example,
|
|---|
| 124 | // lastAccessed may not be important to some stores and an equality
|
|---|
| 125 | // comparison could exclude that field.
|
|---|
| 126 | this.putCookie(newCookie, cb);
|
|---|
| 127 | }
|
|---|
| 128 | removeCookie(domain, path, key, cb) {
|
|---|
| 129 | if (
|
|---|
| 130 | this.idx[domain] &&
|
|---|
| 131 | this.idx[domain][path] &&
|
|---|
| 132 | this.idx[domain][path][key]
|
|---|
| 133 | ) {
|
|---|
| 134 | delete this.idx[domain][path][key];
|
|---|
| 135 | }
|
|---|
| 136 | cb(null);
|
|---|
| 137 | }
|
|---|
| 138 | removeCookies(domain, path, cb) {
|
|---|
| 139 | if (this.idx[domain]) {
|
|---|
| 140 | if (path) {
|
|---|
| 141 | delete this.idx[domain][path];
|
|---|
| 142 | } else {
|
|---|
| 143 | delete this.idx[domain];
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | return cb(null);
|
|---|
| 147 | }
|
|---|
| 148 | removeAllCookies(cb) {
|
|---|
| 149 | this.idx = Object.create(null);
|
|---|
| 150 | return cb(null);
|
|---|
| 151 | }
|
|---|
| 152 | getAllCookies(cb) {
|
|---|
| 153 | const cookies = [];
|
|---|
| 154 | const idx = this.idx;
|
|---|
| 155 |
|
|---|
| 156 | const domains = Object.keys(idx);
|
|---|
| 157 | domains.forEach(domain => {
|
|---|
| 158 | const paths = Object.keys(idx[domain]);
|
|---|
| 159 | paths.forEach(path => {
|
|---|
| 160 | const keys = Object.keys(idx[domain][path]);
|
|---|
| 161 | keys.forEach(key => {
|
|---|
| 162 | if (key !== null) {
|
|---|
| 163 | cookies.push(idx[domain][path][key]);
|
|---|
| 164 | }
|
|---|
| 165 | });
|
|---|
| 166 | });
|
|---|
| 167 | });
|
|---|
| 168 |
|
|---|
| 169 | // Sort by creationIndex so deserializing retains the creation order.
|
|---|
| 170 | // When implementing your own store, this SHOULD retain the order too
|
|---|
| 171 | cookies.sort((a, b) => {
|
|---|
| 172 | return (a.creationIndex || 0) - (b.creationIndex || 0);
|
|---|
| 173 | });
|
|---|
| 174 |
|
|---|
| 175 | cb(null, cookies);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | [
|
|---|
| 180 | "findCookie",
|
|---|
| 181 | "findCookies",
|
|---|
| 182 | "putCookie",
|
|---|
| 183 | "updateCookie",
|
|---|
| 184 | "removeCookie",
|
|---|
| 185 | "removeCookies",
|
|---|
| 186 | "removeAllCookies",
|
|---|
| 187 | "getAllCookies"
|
|---|
| 188 | ].forEach(name => {
|
|---|
| 189 | MemoryCookieStore.prototype[name] = fromCallback(
|
|---|
| 190 | MemoryCookieStore.prototype[name]
|
|---|
| 191 | );
|
|---|
| 192 | });
|
|---|
| 193 |
|
|---|
| 194 | exports.MemoryCookieStore = MemoryCookieStore;
|
|---|
| 195 |
|
|---|
| 196 | function inspectFallback(val) {
|
|---|
| 197 | const domains = Object.keys(val);
|
|---|
| 198 | if (domains.length === 0) {
|
|---|
| 199 | return "[Object: null prototype] {}";
|
|---|
| 200 | }
|
|---|
| 201 | let result = "[Object: null prototype] {\n";
|
|---|
| 202 | Object.keys(val).forEach((domain, i) => {
|
|---|
| 203 | result += formatDomain(domain, val[domain]);
|
|---|
| 204 | if (i < domains.length - 1) {
|
|---|
| 205 | result += ",";
|
|---|
| 206 | }
|
|---|
| 207 | result += "\n";
|
|---|
| 208 | });
|
|---|
| 209 | result += "}";
|
|---|
| 210 | return result;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | function formatDomain(domainName, domainValue) {
|
|---|
| 214 | const indent = " ";
|
|---|
| 215 | let result = `${indent}'${domainName}': [Object: null prototype] {\n`;
|
|---|
| 216 | Object.keys(domainValue).forEach((path, i, paths) => {
|
|---|
| 217 | result += formatPath(path, domainValue[path]);
|
|---|
| 218 | if (i < paths.length - 1) {
|
|---|
| 219 | result += ",";
|
|---|
| 220 | }
|
|---|
| 221 | result += "\n";
|
|---|
| 222 | });
|
|---|
| 223 | result += `${indent}}`;
|
|---|
| 224 | return result;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | function formatPath(pathName, pathValue) {
|
|---|
| 228 | const indent = " ";
|
|---|
| 229 | let result = `${indent}'${pathName}': [Object: null prototype] {\n`;
|
|---|
| 230 | Object.keys(pathValue).forEach((cookieName, i, cookieNames) => {
|
|---|
| 231 | const cookie = pathValue[cookieName];
|
|---|
| 232 | result += ` ${cookieName}: ${cookie.inspect()}`;
|
|---|
| 233 | if (i < cookieNames.length - 1) {
|
|---|
| 234 | result += ",";
|
|---|
| 235 | }
|
|---|
| 236 | result += "\n";
|
|---|
| 237 | });
|
|---|
| 238 | result += `${indent}}`;
|
|---|
| 239 | return result;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | exports.inspectFallback = inspectFallback;
|
|---|