const decode = decodeURIComponent; const encode = encodeURIComponent; const pairSplitRegExp = /; */; const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; function parse(str, options) { if (typeof str !== "string") { throw new TypeError("argument str must be a string"); } let obj = {}; let opt = options || {}; let pairs = str.split(pairSplitRegExp); let dec = opt.decode || decode; for (let i = 0; i < pairs.length; i++) { let pair = pairs[i]; let eq_idx = pair.indexOf("="); if (eq_idx < 0) { continue; } let key = pair.substr(0, eq_idx).trim(); let val = pair.substr(++eq_idx, pair.length).trim(); if (val[0] == '"') { val = val.slice(1, -1); } if (obj[key] == void 0) { obj[key] = tryDecode(val, dec); } } return obj; } function serialize(name, value, options) { let opt = options || {}; let enc = opt.encode || encode; if (typeof enc !== "function") { throw new TypeError("option encode is invalid"); } if (!fieldContentRegExp.test(name)) { throw new TypeError("argument name is invalid"); } let encodedValue = enc(value); if (encodedValue && !fieldContentRegExp.test(encodedValue)) { throw new TypeError("argument val is invalid"); } let str = name + "=" + encodedValue; if (opt.maxAge != null) { let maxAge = opt.maxAge - 0; if (isNaN(maxAge) || !isFinite(maxAge)) { throw new TypeError("option maxAge is invalid"); } str += "; Max-Age=" + Math.floor(maxAge); } if (opt.domain) { if (!fieldContentRegExp.test(opt.domain)) { throw new TypeError("option domain is invalid"); } str += "; Domain=" + opt.domain; } if (opt.path) { if (!fieldContentRegExp.test(opt.path)) { throw new TypeError("option path is invalid"); } str += "; Path=" + opt.path; } if (opt.expires) { if (typeof opt.expires.toUTCString !== "function") { throw new TypeError("option expires is invalid"); } str += "; Expires=" + opt.expires.toUTCString(); } if (opt.httpOnly) { str += "; HttpOnly"; } if (opt.secure) { str += "; Secure"; } if (opt.sameSite) { let sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite; switch (sameSite) { case true: str += "; SameSite=Strict"; break; case "lax": str += "; SameSite=Lax"; break; case "strict": str += "; SameSite=Strict"; break; case "none": str += "; SameSite=None"; break; default: throw new TypeError("option sameSite is invalid"); } } return str; } function tryDecode(str, decode2) { try { return decode2(str); } catch (e) { return str; } } export { parse, serialize };