(cookieStr: string, name: string)
| 7 | */ |
| 8 | |
| 9 | export function parseCookieValue(cookieStr: string, name: string): string | null { |
| 10 | name = encodeURIComponent(name); |
| 11 | |
| 12 | for (const cookie of cookieStr.split(';')) { |
| 13 | const eqIndex = cookie.indexOf('='); |
| 14 | const [cookieName, cookieValue]: string[] = |
| 15 | eqIndex == -1 ? [cookie, ''] : [cookie.slice(0, eqIndex), cookie.slice(eqIndex + 1)]; |
| 16 | |
| 17 | if (cookieName.trim() !== name) { |
| 18 | continue; |
| 19 | } |
| 20 | |
| 21 | let value = cookieValue; |
| 22 | try { |
| 23 | value = decodeURIComponent(cookieValue); |
| 24 | } catch { |
| 25 | // Fall back to raw cookie value if decoding fails (e.g. malformed percent-encoding). |
| 26 | } |
| 27 | |
| 28 | if (value.length > 1 && value[0] === '"' && value[value.length - 1] === '"') { |
| 29 | value = value.slice(1, -1); |
| 30 | } |
| 31 | |
| 32 | return value; |
| 33 | } |
| 34 | |
| 35 | return null; |
| 36 | } |
no test coverage detected