-
Notifications
You must be signed in to change notification settings - Fork 2
/
CookieFileStore.ts
131 lines (120 loc) · 3.16 KB
/
CookieFileStore.ts
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
import { readFile, writeFile } from 'fs';
import { Cookie, CookieStore } from '../src/EDCompanionAPI';
export interface CookieStorage {
[domain: string]: {
[path: string]: {
[key: string]: Cookie;
};
};
}
export class CookieFileStore implements CookieStore {
public synchronous = true;
private data?: CookieStorage;
constructor(private fileName: string, private forceReadOnAccess = false) {}
public findCookie(
domain: string,
path: string,
key: string,
cb: (err?: Error, cookie?: Cookie) => void,
) {
this.loadData((err, data) => {
if (err) {
return cb(err);
}
if (!data![domain]) {
return cb();
}
if (!data![domain][path]) {
return cb();
}
cb(undefined, data![domain][path][key] || null);
});
}
public findCookies(domain: string, _path: string, cb: (err?: Error, cookies?: Cookie[]) => void) {
this.loadData((err, data) => {
if (err) {
return cb(err);
}
if (!data![domain]) {
return cb(undefined, []);
}
if (!data![domain]) {
return cb();
}
let out: Cookie[] = [];
for (const domainData of Object.values(data![domain])) {
out = out.concat(Object.values(domainData));
}
cb(undefined, out);
});
}
public putCookie(cookie: Cookie, cb: (err: Error) => void) {
this.loadData((err, data) => {
if (err) {
return cb(err);
}
if (!data![cookie.domain]) {
data![cookie.domain] = {};
}
if (!data![cookie.domain][cookie.path]) {
data![cookie.domain][cookie.path] = {};
}
data![cookie.domain][cookie.path][cookie.key] = cookie;
this.saveData(cb);
});
}
public updateCookie(_oldCookie: Cookie, newCookie: Cookie, cb: (err: Error) => void) {
this.putCookie(newCookie, cb);
}
public removeCookie(domain: string, path: string, key: string, cb: (err: Error) => void) {
this.loadData((err, data) => {
if (err) {
return cb(err);
}
if (data![domain] && data![domain][path] && data![domain][path][key]) {
delete data![domain][path][key];
}
this.saveData(cb);
});
}
public removeCookies(domain: string, path: string, cb: (err?: Error) => void) {
this.loadData((err, data) => {
if (err) {
return cb(err);
}
if (!data![domain]) {
return cb();
}
if (path) {
delete data![domain][path];
} else {
delete data![domain];
}
this.saveData(cb);
});
}
private loadData(cb: (err: undefined | Error, data?: CookieStorage) => void): void {
if (this.data && !this.forceReadOnAccess) {
return cb(undefined, this.data);
}
readFile(this.fileName, 'utf8', (error, data) => {
if (error) {
return cb(error);
}
try {
this.data = JSON.parse(data);
cb(undefined, this.data);
} catch (error) {
cb(error);
}
});
}
private saveData(cb: (err: Error) => void) {
this.loadData(err => {
if (err) {
return cb(err);
}
writeFile(this.fileName, JSON.stringify(this.data), cb);
});
}
}