All files cookies.ts

95% Statements 19/20
79.16% Branches 19/24
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                5x 5x 4x 5x   5x 6x 6x 4x 4x 4x   2x         4x 4x 4x 4x 4x             2x                
import type { HarCookie, NameValue } from "../types/har";
 
/**
 * Returns the complete Set-Cookie attribute text when the response header is
 * available. HAR 1.2's structured cookie object cannot represent modern
 * attributes such as SameSite, Max-Age, Priority, or Partitioned.
 */
export function cookieAttributeTexts(cookies: HarCookie[], headers: NameValue[] | undefined): string[] {
  const setCookies = (headers ?? [])
    .filter((header) => header.name.toLowerCase() === "set-cookie")
    .map((header) => parseSetCookie(header.value));
  const used = new Set<number>();
 
  return cookies.map((cookie) => {
    const match = setCookies.findIndex((candidate, index) => !used.has(index) && candidate?.name === cookie.name);
    if (match >= 0) {
      used.add(match);
      const attributes = setCookies[match]?.attributes;
      Eif (attributes) return attributes;
    }
    return structuredAttributes(cookie);
  });
}
 
function parseSetCookie(value: string): { name: string; attributes: string } | undefined {
  const semicolon = value.indexOf(";");
  const pair = (semicolon >= 0 ? value.slice(0, semicolon) : value).trim();
  const equals = pair.indexOf("=");
  Iif (equals <= 0) return undefined;
  return {
    name: pair.slice(0, equals).trim(),
    attributes: semicolon >= 0 ? value.slice(semicolon + 1).trim() : "",
  };
}
 
function structuredAttributes(cookie: HarCookie): string {
  return [
    cookie.path && `path=${cookie.path}`,
    cookie.domain && `domain=${cookie.domain}`,
    cookie.expires && `expires=${cookie.expires}`,
    cookie.httpOnly && "httpOnly",
    cookie.secure && "secure",
  ].filter(Boolean).join("; ");
}