forked from expo/tough-cookie-web-storage-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebStorageCookieStore.js
137 lines (113 loc) · 3.13 KB
/
WebStorageCookieStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
let ToughCookie = require('tough-cookie');
let get = require('lodash/get');
let set = require('lodash/set');
let unset = require('lodash/unset');
let values = require('lodash/values');
let Cookie = ToughCookie.Cookie;
const STORE_KEY = '__cookieStore__';
class WebStorageCookieStore extends ToughCookie.Store {
constructor(storage) {
super();
this._storage = storage;
this.synchronous = true;
}
findCookie(domain, path, key, callback) {
let store = this._readStore();
let cookie = get(store, [domain, path, key], null);
callback(null, Cookie.fromJSON(cookie));
}
findCookies(domain, path, callback) {
if (!domain) {
callback(null, []);
return;
}
let cookies = [];
let store = this._readStore();
let domains = ToughCookie.permuteDomain(domain) || [domain];
for (let domain of domains) {
if (!store[domain]) {
continue;
}
let matchingPaths = Object.keys(store[domain]);
if (path != null) {
matchingPaths = matchingPaths
.filter(cookiePath => this._isOnPath(cookiePath, path));
}
for (let path of matchingPaths) {
cookies.push(...values(store[domain][path]));
}
}
cookies = cookies.map(cookie => Cookie.fromJSON(cookie));
callback(null, cookies);
}
/**
* Returns whether `cookiePath` is on the given `urlPath`
*/
_isOnPath(cookiePath, urlPath) {
if (!cookiePath) {
return false;
}
if (cookiePath === urlPath) {
return true;
}
if (!urlPath.startsWith(cookiePath)) {
return false;
}
if (cookiePath[cookiePath.length - 1] !== '/' &&
urlPath[cookiePath.length] !== '/') {
return false;
}
return true;
}
putCookie(cookie, callback) {
let store = this._readStore();
set(store, [cookie.domain, cookie.path, cookie.key], cookie);
this._writeStore(store);
callback(null);
}
updateCookie(oldCookie, newCookie, callback) {
this.putCookie(newCookie, callback);
}
removeCookie(domain, path, key, callback) {
let store = this._readStore();
unset(store, [domain, path, key]);
this._writeStore(store);
callback(null);
}
removeCookies(domain, path, callback) {
let store = this._readStore();
if (path == null) {
unset(store, [domain]);
} else {
unset(store, [domain, path]);
}
this._writeStore(store);
callback(null);
}
getAllCookies(callback) {
let cookies = [];
let store = this._readStore();
for (let domain of Object.keys(store)) {
for (let path of Object.keys(store[domain])) {
cookies.push(...values(store[domain][path]));
}
}
cookies = cookies.map(cookie => Cookie.fromJSON(cookie));
cookies.sort((c1, c2) => (c1.creationIndex || 0) - (c2.creationIndex || 0));
callback(null, cookies);
}
_readStore() {
let json = this._storage.getItem(STORE_KEY);
if (json != null) {
try {
return JSON.parse(json);
} catch (e) { }
}
return {};
}
_writeStore(store) {
this._storage.setItem(STORE_KEY, JSON.stringify(store));
}
}
module.exports = WebStorageCookieStore;