2 * Copyright (c) 2015, Salesforce.com, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
32 var Store = require('./store').Store;
33 var permuteDomain = require('./permuteDomain').permuteDomain;
34 var pathMatch = require('./pathMatch').pathMatch;
35 var util = require('util');
37 function MemoryCookieStore() {
41 util.inherits(MemoryCookieStore, Store);
42 exports.MemoryCookieStore = MemoryCookieStore;
43 MemoryCookieStore.prototype.idx = null;
45 // Since it's just a struct in RAM, this Store is synchronous
46 MemoryCookieStore.prototype.synchronous = true;
48 // force a default depth:
49 MemoryCookieStore.prototype.inspect = function() {
50 return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
53 MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
54 if (!this.idx[domain]) {
55 return cb(null,undefined);
57 if (!this.idx[domain][path]) {
58 return cb(null,undefined);
60 return cb(null,this.idx[domain][path][key]||null);
63 MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
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]);
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];
89 for (var key in pathIndex) {
90 results.push(pathIndex[key]);
97 var domains = permuteDomain(domain) || [domain];
99 domains.forEach(function(curDomain) {
100 var domainIndex = idx[curDomain];
104 pathMatcher(domainIndex);
110 MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
111 if (!this.idx[cookie.domain]) {
112 this.idx[cookie.domain] = {};
114 if (!this.idx[cookie.domain][cookie.path]) {
115 this.idx[cookie.domain][cookie.path] = {};
117 this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
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);
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];
135 MemoryCookieStore.prototype.removeCookies = function(domain, path, cb) {
136 if (this.idx[domain]) {
138 delete this.idx[domain][path];
140 delete this.idx[domain];
146 MemoryCookieStore.prototype.getAllCookies = function(cb) {
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) {
157 cookies.push(idx[domain][path][key]);
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);