'use strict'; function isNonEmptyString(str) { return typeof str === "string" && !!str.trim(); } function parseNameValuePair(str) { let name = ""; let value = ""; const nameValueArr = str.split("="); if (nameValueArr.length > 1) { name = nameValueArr.shift(); value = nameValueArr.join("="); } else { value = str; } return { name, value }; } const defaultParseOptions = { decodeValues: true, map: false, silent: false }; function parseString(setCookieValue, options = {}) { options = { ...defaultParseOptions, ...options }; const parts = setCookieValue.split(";").filter((str) => isNonEmptyString(str)); const nameValuePairStr = parts.shift(); const parsed = parseNameValuePair(nameValuePairStr); const name = parsed.name; let value = parsed.value; try { value = options.decodeValues ? decodeURIComponent(value) : value; } catch (error) { console.error( `set-cookie-parser-es encountered an error while decoding a cookie with value "${value}". Set options.decodeValues to false to disable this feature.`, error ); } const cookie = { name, value }; for (const part of parts) { const sides = part.split("="); const key = sides.shift().trimStart().toLowerCase(); const value2 = sides.join("="); switch (key) { case "expires": { cookie.expires = new Date(value2); break; } case "max-age": { cookie.maxAge = Number.parseInt(value2, 10); break; } case "secure": { cookie.secure = true; break; } case "httponly": { cookie.httpOnly = true; break; } case "samesite": { cookie.sameSite = value2; break; } default: { cookie[key] = value2; } } } return cookie; } function parse(input, options = {}) { options = { ...defaultParseOptions, ...options }; if (!input) { return options.map ? {} : []; } if (!Array.isArray(input)) { input = [input]; } if (!options.map) { return input.filter((str) => isNonEmptyString(str)).map((str) => parseString(str, options)); } const cookies = {}; for (const str of input) { if (!isNonEmptyString(str)) { continue; } const cookie = parseString(str, options); if (cookie.name) { cookies[cookie.name] = cookie; } } return cookies; } function splitCookiesString(cookiesString) { const cookiesStrings = []; let pos = 0; let start; let ch; let lastComma; let nextStart; let cookiesSeparatorFound; function skipWhitespace() { while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { pos += 1; } return pos < cookiesString.length; } function notSpecialChar() { ch = cookiesString.charAt(pos); return ch !== "=" && ch !== ";" && ch !== ","; } while (pos < cookiesString.length) { start = pos; cookiesSeparatorFound = false; while (skipWhitespace()) { ch = cookiesString.charAt(pos); if (ch === ",") { lastComma = pos; pos += 1; skipWhitespace(); nextStart = pos; while (pos < cookiesString.length && notSpecialChar()) { pos += 1; } if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") { cookiesSeparatorFound = true; pos = nextStart; cookiesStrings.push(cookiesString.slice(start, lastComma)); start = pos; } else { pos = lastComma + 1; } } else { pos += 1; } } if (!cookiesSeparatorFound || pos >= cookiesString.length) { cookiesStrings.push(cookiesString.slice(start, cookiesString.length)); } } return cookiesStrings; } exports.parse = parse; exports.parseString = parseString; exports.splitCookiesString = splitCookiesString;