* @see https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1 * @param {import('./index').Cookie} cookie
(cookie)
| 207 | * @param {import('./index').Cookie} cookie |
| 208 | */ |
| 209 | function stringify (cookie) { |
| 210 | if (cookie.name.length === 0) { |
| 211 | return null |
| 212 | } |
| 213 | |
| 214 | validateCookieName(cookie.name) |
| 215 | validateCookieValue(cookie.value) |
| 216 | |
| 217 | const out = [`${cookie.name}=${cookie.value}`] |
| 218 | |
| 219 | // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1 |
| 220 | // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.2 |
| 221 | if (cookie.name.startsWith('__Secure-')) { |
| 222 | cookie.secure = true |
| 223 | } |
| 224 | |
| 225 | if (cookie.name.startsWith('__Host-')) { |
| 226 | cookie.secure = true |
| 227 | cookie.domain = null |
| 228 | cookie.path = '/' |
| 229 | } |
| 230 | |
| 231 | if (cookie.secure) { |
| 232 | out.push('Secure') |
| 233 | } |
| 234 | |
| 235 | if (cookie.httpOnly) { |
| 236 | out.push('HttpOnly') |
| 237 | } |
| 238 | |
| 239 | if (typeof cookie.maxAge === 'number') { |
| 240 | validateCookieMaxAge(cookie.maxAge) |
| 241 | out.push(`Max-Age=${cookie.maxAge}`) |
| 242 | } |
| 243 | |
| 244 | if (cookie.domain) { |
| 245 | validateCookieDomain(cookie.domain) |
| 246 | out.push(`Domain=${cookie.domain}`) |
| 247 | } |
| 248 | |
| 249 | if (cookie.path) { |
| 250 | validateCookiePath(cookie.path) |
| 251 | out.push(`Path=${cookie.path}`) |
| 252 | } |
| 253 | |
| 254 | if (cookie.expires && cookie.expires.toString() !== 'Invalid Date') { |
| 255 | out.push(`Expires=${toIMFDate(cookie.expires)}`) |
| 256 | } |
| 257 | |
| 258 | if (cookie.sameSite) { |
| 259 | out.push(`SameSite=${cookie.sameSite}`) |
| 260 | } |
| 261 | |
| 262 | for (const part of cookie.unparsed) { |
| 263 | if (!part.includes('=')) { |
| 264 | throw new Error('Invalid unparsed') |
| 265 | } |
| 266 |
no test coverage detected