UNPKG

amos-tool

Version:

amos ui tool

71 lines (63 loc) 2.17 kB
"use strict"; var decode = decodeURIComponent, encode = encodeURIComponent, pairSplitRegExp = /; */, fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; function parse(e, t) { if ("string" != typeof e) throw new TypeError("argument str must be a string"); for (var r = {}, i = t || {}, n = e.split(pairSplitRegExp), o = i.decode || decode, a = 0; a < n.length; a++) { var s = n[a], p = s.indexOf("="); if (!(p < 0)) { var d = s.substr(0, p).trim(), f = s.substr(++p, s.length).trim(); '"' == f[0] && (f = f.slice(1, -1)), null == r[d] && (r[d] = tryDecode(f, o)); } } return r; } function serialize(e, t, r) { var i = r || {}, n = i.encode || encode; if ("function" != typeof n) throw new TypeError("option encode is invalid"); if (!fieldContentRegExp.test(e)) throw new TypeError("argument name is invalid"); var o = n(t); if (o && !fieldContentRegExp.test(o)) throw new TypeError("argument val is invalid"); var a = e + "=" + o; if (null != i.maxAge) { var s = i.maxAge - 0; if (isNaN(s)) throw new Error("maxAge should be a Number"); a += "; Max-Age=" + Math.floor(s); } if (i.domain) { if (!fieldContentRegExp.test(i.domain)) throw new TypeError("option domain is invalid"); a += "; Domain=" + i.domain; } if (i.path) { if (!fieldContentRegExp.test(i.path)) throw new TypeError("option path is invalid"); a += "; Path=" + i.path; } if (i.expires) { if ("function" != typeof i.expires.toUTCString) throw new TypeError("option expires is invalid"); a += "; Expires=" + i.expires.toUTCString(); } if (i.httpOnly && (a += "; HttpOnly"), i.secure && (a += "; Secure"), i.sameSite) switch ("string" == typeof i.sameSite ? i.sameSite.toLowerCase() : i.sameSite) { case !0: a += "; SameSite=Strict"; break; case "lax": a += "; SameSite=Lax"; break; case "strict": a += "; SameSite=Strict"; break; default: throw new TypeError("option sameSite is invalid"); } return a; } function tryDecode(e, t) { try { return t(e); } catch (t) { return e; } } module.exports = { parse: parse, serialize: serialize };