generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
206 lines (179 loc) · 3.99 KB
/
cookie.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
*/
export class Cookie {
#name;
#value;
#domain;
#path;
#expires;
#maxAge;
#sameSite;
#httpOnly;
#secure;
#partitioned;
constructor(name, value, {
domain,
path = '/',
expires,
maxAge,
sameSite = 'Lax',
httpOnly = false,
secure = false,
partitioned = false,
} = {}) {
this.name = name;
this.value = value;
this.path = path;
this.sameSite = sameSite;
this.httpOnly = httpOnly;
this.secure = secure;
this.partitioned = partitioned;
if (typeof domain !== 'undefined') {
this.domain = domain;
}
if (typeof expires !== 'undefined') {
this.expires = expires;
}
if (typeof maxAge !== 'undefined') {
this.maxAge = maxAge;
}
}
get name() {
return this.#name;
}
set name(val) {
if (typeof val !== 'string' || val.legnth === 0) {
throw new TypeError('Cookie name must be a non-empty string.');
} else {
this.#name = val;
}
}
set value(val) {
if (typeof val === 'undefined' || val === null) {
this.#value = '';
} else {
this.#value = val.toString();
}
}
get maxAge() {
return this.#maxAge;
}
set maxAge(val) {
if (typeof val !== 'number' || ! Number.isSafeInteger(val)) {
throw new TypeError('Max-Age must be an integer.');
} else {
this.#maxAge = val;
}
}
get path() {
return this.#path;
}
set path(val) {
if (val instanceof URL) {
this.#path = val.pathname;
} else if (typeof val === 'string') {
if (new URL(val, 'https://example.com').pathname !== val) {
throw new Error(`Invalid path: ${val}`);
} else {
console.log(`Set #path to ${val}`);
this.#path = val;
}
} else {
throw new TypeError('Path must be a string or URL.');
}
}
get domain() {
return this.#domain;
}
set domain(val) {
if (val instanceof URL) {
this.#domain = val.hostname;
} else if (typeof val === 'string') {
if (new URL(`https://${val}`).hostname === val) {
this.#domain = val;
} else {
throw new Error(`Invalid domain: ${val}`);
}
} else {
throw new TypeError('Domain must be a URL or string.');
}
}
get expires() {
return this.#expires;
}
set expires(val) {
if (val instanceof Date) {
this.#expires = val;
} else if (typeof val === 'string' || typeof val === 'number') {
this.expires = new Date(val);
}
}
get httpOnly() {
return this.#httpOnly;
}
set httpOnly(val) {
if (typeof val !== 'boolean') {
throw new TypeError('HttpOnly must be a boolean.');
} else {
this.#httpOnly = val;
}
}
get secure() {
return this.#secure;
}
set secure(val) {
if (typeof val !== 'boolean') {
throw new TypeError('Secure must be a boolean.');
} else {
this.#secure = val;
}
}
get partitioned() {
return this.#partitioned;
}
set partitioned(val) {
if (typeof val !== 'boolean') {
throw new TypeError('Partitioned must be a boolean.');
} else {
this.#partitioned = val;
}
}
get sameSite() {
return this.#sameSite;
}
set sameSite(val) {
if (! ['Strict', 'Lax', 'None'].includes(val)) {
throw new TypeError('SameSite must be a "Strict", "Lax", or "None".');
} else {
this.#sameSite = val;
}
}
toString() {
let cookie = `${encodeURIComponent(this.#name)}=${encodeURIComponent(this.#value)}`;
const flags = {
Domain: this.#domain,
Path: this.#path,
Expires: this.#expires,
'Max-Age': this.#maxAge,
SameSite: this.#sameSite,
HttpOnly: this.#httpOnly,
Secure: this.#secure,
Partitioned: this.#partitioned,
};
return (cookie + '; ' + Object.entries(flags).reduce((flags, [key, val]) => {
if (typeof val === 'string') {
flags.push(key === 'Path' ? `${key}=${val}` : `${key}=${encodeURIComponent(val)}`);
} else if (typeof val === 'number' && ! Number.isNaN(val)) {
flags.push(`${key}=${val}`);
} else if (typeof val === 'boolean') {
if (val) {
flags.push(key);
}
} else if (val instanceof Date) {
flags.push(`${key}=${val.toGMTString()}`);
}
return flags;
}, []).join('; ')).trim();
}
}