|
| 1 | +/** |
| 2 | + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie |
| 3 | + */ |
| 4 | +export class Cookie { |
| 5 | + #name; |
| 6 | + #value; |
| 7 | + #domain; |
| 8 | + #path; |
| 9 | + #expires; |
| 10 | + #maxAge; |
| 11 | + #sameSite; |
| 12 | + #httpOnly; |
| 13 | + #secure; |
| 14 | + #partitioned; |
| 15 | + |
| 16 | + constructor(name, value, { |
| 17 | + domain, |
| 18 | + path = '/', |
| 19 | + expires, |
| 20 | + maxAge, |
| 21 | + sameSite = 'Lax', |
| 22 | + httpOnly = false, |
| 23 | + secure = false, |
| 24 | + partitioned = false, |
| 25 | + } = {}) { |
| 26 | + this.name = name; |
| 27 | + this.value = value; |
| 28 | + this.path = path; |
| 29 | + this.sameSite = sameSite; |
| 30 | + this.httpOnly = httpOnly; |
| 31 | + this.secure = secure; |
| 32 | + this.partitioned = partitioned; |
| 33 | + |
| 34 | + if (typeof domain !== 'undefined') { |
| 35 | + this.domain = domain; |
| 36 | + } |
| 37 | + |
| 38 | + if (typeof expires !== 'undefined') { |
| 39 | + this.expires = expires; |
| 40 | + } |
| 41 | + |
| 42 | + if (typeof maxAge !== 'undefined') { |
| 43 | + this.maxAge = maxAge; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + get name() { |
| 48 | + return this.#name; |
| 49 | + } |
| 50 | + |
| 51 | + set name(val) { |
| 52 | + if (typeof val !== 'string' || val.legnth === 0) { |
| 53 | + throw new TypeError('Cookie name must be a non-empty string.'); |
| 54 | + } else { |
| 55 | + this.#name = val; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + set value(val) { |
| 60 | + if (typeof val === 'undefined' || val === null) { |
| 61 | + this.#value = ''; |
| 62 | + } else { |
| 63 | + this.#value = val.toString(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + get maxAge() { |
| 68 | + return this.#maxAge; |
| 69 | + } |
| 70 | + |
| 71 | + set maxAge(val) { |
| 72 | + if (typeof val !== 'number' || ! Number.isSafeInteger(val)) { |
| 73 | + throw new TypeError('Max-Age must be an integer.'); |
| 74 | + } else { |
| 75 | + this.#maxAge = val; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + get path() { |
| 80 | + return this.#path; |
| 81 | + } |
| 82 | + |
| 83 | + set path(val) { |
| 84 | + if (val instanceof URL) { |
| 85 | + this.#path = val.pathname; |
| 86 | + } else if (typeof val === 'string') { |
| 87 | + if (new URL(val, 'https://example.com').pathname !== val) { |
| 88 | + throw new Error(`Invalid path: ${val}`); |
| 89 | + } else { |
| 90 | + console.log(`Set #path to ${val}`); |
| 91 | + this.#path = val; |
| 92 | + } |
| 93 | + } else { |
| 94 | + throw new TypeError('Path must be a string or URL.'); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + get domain() { |
| 99 | + return this.#domain; |
| 100 | + } |
| 101 | + |
| 102 | + set domain(val) { |
| 103 | + if (val instanceof URL) { |
| 104 | + this.#domain = val.hostname; |
| 105 | + } else if (typeof val === 'string') { |
| 106 | + if (new URL(`https://${val}`).hostname === val) { |
| 107 | + this.#domain = val; |
| 108 | + } else { |
| 109 | + throw new Error(`Invalid domain: ${val}`); |
| 110 | + } |
| 111 | + } else { |
| 112 | + throw new TypeError('Domain must be a URL or string.'); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + get expires() { |
| 117 | + return this.#expires; |
| 118 | + } |
| 119 | + |
| 120 | + set expires(val) { |
| 121 | + if (val instanceof Date) { |
| 122 | + this.#expires = val; |
| 123 | + } else if (typeof val === 'string' || typeof val === 'number') { |
| 124 | + this.expires = new Date(val); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + get httpOnly() { |
| 129 | + return this.#httpOnly; |
| 130 | + } |
| 131 | + |
| 132 | + set httpOnly(val) { |
| 133 | + if (typeof val !== 'boolean') { |
| 134 | + throw new TypeError('HttpOnly must be a boolean.'); |
| 135 | + } else { |
| 136 | + this.#httpOnly = val; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + get secure() { |
| 141 | + return this.#secure; |
| 142 | + } |
| 143 | + |
| 144 | + set secure(val) { |
| 145 | + if (typeof val !== 'boolean') { |
| 146 | + throw new TypeError('Secure must be a boolean.'); |
| 147 | + } else { |
| 148 | + this.#secure = val; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + get partitioned() { |
| 153 | + return this.#partitioned; |
| 154 | + } |
| 155 | + |
| 156 | + set partitioned(val) { |
| 157 | + if (typeof val !== 'boolean') { |
| 158 | + throw new TypeError('Partitioned must be a boolean.'); |
| 159 | + } else { |
| 160 | + this.#partitioned = val; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + get sameSite() { |
| 165 | + return this.#sameSite; |
| 166 | + } |
| 167 | + |
| 168 | + set sameSite(val) { |
| 169 | + if (! ['Strict', 'Lax', 'None'].includes(val)) { |
| 170 | + throw new TypeError('SameSite must be a "Strict", "Lax", or "None".'); |
| 171 | + } else { |
| 172 | + this.#sameSite = val; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + toString() { |
| 177 | + let cookie = `${encodeURIComponent(this.#name)}=${encodeURIComponent(this.#value)}`; |
| 178 | + |
| 179 | + const flags = { |
| 180 | + Domain: this.#domain, |
| 181 | + Path: this.#path, |
| 182 | + Expires: this.#expires, |
| 183 | + 'Max-Age': this.#maxAge, |
| 184 | + SameSite: this.#sameSite, |
| 185 | + HttpOnly: this.#httpOnly, |
| 186 | + Secure: this.#secure, |
| 187 | + Partitioned: this.#partitioned, |
| 188 | + }; |
| 189 | + |
| 190 | + return (cookie + '; ' + Object.entries(flags).reduce((flags, [key, val]) => { |
| 191 | + if (typeof val === 'string') { |
| 192 | + flags.push(key === 'Path' ? `${key}=${val}` : `${key}=${encodeURIComponent(val)}`); |
| 193 | + } else if (typeof val === 'number' && ! Number.isNaN(val)) { |
| 194 | + flags.push(`${key}=${val}`); |
| 195 | + } else if (typeof val === 'boolean') { |
| 196 | + if (val) { |
| 197 | + flags.push(key); |
| 198 | + } |
| 199 | + } else if (val instanceof Date) { |
| 200 | + flags.push(`${key}=${val.toGMTString()}`); |
| 201 | + } |
| 202 | + |
| 203 | + return flags; |
| 204 | + }, []).join('; ')).trim(); |
| 205 | + } |
| 206 | +} |
0 commit comments