"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/tough-cookie/lib/memstore.js" (11 Apr 2017, 5514 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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