twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

memstore.js (5827B)


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