(setCookieString: string)
| 16 | * @see https:class="cm">//developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie |
| 17 | */ |
| 18 | export function parse(setCookieString: string): Cookie { |
| 19 | const parts = setCookieString.split(class="st">"; "); |
| 20 | const i = parts[0].indexOf(class="st">"="); |
| 21 | |
| 22 | if (i === -1) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | const name = parts[0].substring(0, i).trim(); |
| 27 | |
| 28 | if (!name.length) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | let value = parts[0].substring(i + 1).trim(); |
| 33 | |
| 34 | if (value.charCodeAt(0) === 0x22) { |
| 35 | class="cm">// remove double quotes |
| 36 | value = value.slice(1, -1); |
| 37 | } |
| 38 | |
| 39 | const cookie: Cookie = { |
| 40 | name, |
| 41 | value, |
| 42 | }; |
| 43 | |
| 44 | for (let j = 1; j < parts.length; j++) { |
| 45 | const subParts = parts[j].split(class="st">"="); |
| 46 | if (subParts.length !== 2) { |
| 47 | continue; |
| 48 | } |
| 49 | const key = subParts[0].trim(); |
| 50 | const value = subParts[1].trim(); |
| 51 | switch (key) { |
| 52 | case class="st">"Expires": |
| 53 | cookie.expires = new Date(value); |
| 54 | break; |
| 55 | case class="st">"Max-Age": |
| 56 | const expiration = new Date(); |
| 57 | expiration.setUTCSeconds( |
| 58 | expiration.getUTCSeconds() + parseInt(value, 10), |
| 59 | ); |
| 60 | cookie.expires = expiration; |
| 61 | break; |
| 62 | default: |
| 63 | class="cm">// ignore other keys |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return cookie; |
| 68 | } |
| 69 | |
| 70 | export class CookieJar { |
| 71 | private _cookies = new Map<string, Cookie>(); |
no outgoing calls
no test coverage detected