Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ludicrous
Path: blob/main/corrosion/lib/cookie-parser.js
1223 views
1
// -------------------------------------------------------------
2
// WARNING: this file is used by both the client and the server.
3
// Do not use any browser or node-specific API!
4
// -------------------------------------------------------------
5
class CookieStore {
6
constructor(val = ''){
7
this.data = {};
8
val.split(';').map(cookie => {
9
var [ name, val = ''] = cookie.trimStart().split('=');
10
if (name) this.data[name] = val;
11
});
12
};
13
has(name){
14
if (!name || !this.data[name]) return false;
15
return true;
16
};
17
get(name){
18
return this.has(name) ? this.data[name] : null;
19
};
20
set(name, val){
21
if (!name || !val) return;
22
return this.data[name] = val;
23
};
24
delete(name){
25
if (!name) return;
26
return delete this.data[name];
27
};
28
forEach(action = (node, key) => null){
29
for (let prop in this.data) action(this.data[prop], prop);
30
};
31
serialize(){
32
var str = '';
33
for (let i in this.data) str += ` ${i}=${this.data[i]};`;
34
return str.substr(1);
35
};
36
};
37
38
class SetCookie {
39
constructor(val = ''){
40
41
var [ [ name, value = '' ], ...data ] = val.split(';').map(str => str.trimStart().split('='));
42
43
this.name = name;
44
this.value = value;
45
this.expires = null;
46
this.maxAge = null;
47
this.domain = null;
48
this.secure = false;
49
this.httpOnly = false;
50
this.path = null;
51
this.sameSite = null;
52
53
data.forEach(([name = null, value = null]) => {
54
if (typeof name == 'string') switch(name.toLowerCase()){
55
case 'domain':
56
this.domain = value;
57
break;
58
case 'secure':
59
this.secure = true;
60
break;
61
case 'httponly':
62
this.httpOnly = true;
63
break;
64
case 'samesite':
65
this.sameSite = value;
66
break;
67
case 'path':
68
this.path = value;
69
break;
70
case 'expires':
71
this.expires = value;
72
break;
73
case 'maxage':
74
this.maxAge = value;
75
break;
76
};
77
});
78
};
79
serialize(){
80
if (!this.name) return;
81
var str = `${this.name}=${this.value};`;
82
if (this.expires) str += ` Expires=${this.expires};`;
83
if (this.maxAge) str += ` Max-Age=${this.max_age};`;
84
if (this.domain) str += ` Domain=${this.domain};`;
85
if (this.secure) str += ` Secure;`;
86
if (this.httpOnly) str += ` HttpOnly;`;
87
if (this.path) str += ` Path=${this.path};`;
88
if (this.sameSite) str += ` SameSite=${this.sameSite};`;
89
return str;
90
};
91
};
92
93
exports.CookieStore = CookieStore;
94
exports.SetCookie = SetCookie;
95
96