react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / lib / memstore.js
81146 views/*!1* Copyright (c) 2015, Salesforce.com, Inc.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are met:6*7* 1. Redistributions of source code must retain the above copyright notice,8* this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13*14* 3. Neither the name of Salesforce.com nor the names of its contributors may15* be used to endorse or promote products derived from this software without16* specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE22* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/30'use strict';31var Store = require('./store').Store;32var permuteDomain = require('./permuteDomain').permuteDomain;33var pathMatch = require('./pathMatch').pathMatch;34var util = require('util');3536function MemoryCookieStore() {37Store.call(this);38this.idx = {};39}40util.inherits(MemoryCookieStore, Store);41exports.MemoryCookieStore = MemoryCookieStore;42MemoryCookieStore.prototype.idx = null;43MemoryCookieStore.prototype.synchronous = true;4445// force a default depth:46MemoryCookieStore.prototype.inspect = function() {47return "{ idx: "+util.inspect(this.idx, false, 2)+' }';48};4950MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {51if (!this.idx[domain]) {52return cb(null,undefined);53}54if (!this.idx[domain][path]) {55return cb(null,undefined);56}57return cb(null,this.idx[domain][path][key]||null);58};5960MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {61var results = [];62if (!domain) {63return cb(null,[]);64}6566var pathMatcher;67if (!path) {68// null means "all paths"69pathMatcher = function matchAll(domainIndex) {70for (var curPath in domainIndex) {71var pathIndex = domainIndex[curPath];72for (var key in pathIndex) {73results.push(pathIndex[key]);74}75}76};7778} else {79pathMatcher = function matchRFC(domainIndex) {80//NOTE: we should use path-match algorithm from S5.1.4 here81//(see : https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/canonical_cookie.cc#L299)82Object.keys(domainIndex).forEach(function (cookiePath) {83if (pathMatch(path, cookiePath)) {84var pathIndex = domainIndex[cookiePath];8586for (var key in pathIndex) {87results.push(pathIndex[key]);88}89}90});91};92}9394var domains = permuteDomain(domain) || [domain];95var idx = this.idx;96domains.forEach(function(curDomain) {97var domainIndex = idx[curDomain];98if (!domainIndex) {99return;100}101pathMatcher(domainIndex);102});103104cb(null,results);105};106107MemoryCookieStore.prototype.putCookie = function(cookie, cb) {108if (!this.idx[cookie.domain]) {109this.idx[cookie.domain] = {};110}111if (!this.idx[cookie.domain][cookie.path]) {112this.idx[cookie.domain][cookie.path] = {};113}114this.idx[cookie.domain][cookie.path][cookie.key] = cookie;115cb(null);116};117118MemoryCookieStore.prototype.updateCookie = function updateCookie(oldCookie, newCookie, cb) {119// updateCookie() may avoid updating cookies that are identical. For example,120// lastAccessed may not be important to some stores and an equality121// comparison could exclude that field.122this.putCookie(newCookie,cb);123};124125MemoryCookieStore.prototype.removeCookie = function removeCookie(domain, path, key, cb) {126if (this.idx[domain] && this.idx[domain][path] && this.idx[domain][path][key]) {127delete this.idx[domain][path][key];128}129cb(null);130};131132MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {133if (this.idx[domain]) {134if (path) {135delete this.idx[domain][path];136} else {137delete this.idx[domain];138}139}140return cb(null);141};142143144